Skip to content

Commit d87f6a5

Browse files
author
Christian Wimmer
committed
[GR-33636] Do not use a separate class for each reflectively invoked method.
PullRequest: graal/9731
2 parents b4754fc + 0ea2b6c commit d87f6a5

File tree

27 files changed

+982
-1338
lines changed

27 files changed

+982
-1338
lines changed

substratevm/mx.substratevm/suite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
"requiresConcealed" : {
205205
"java.base" : [
206206
"jdk.internal.module",
207+
"jdk.internal.reflect",
207208
"jdk.internal.misc",
208209
"jdk.internal.logger",
209210
"jdk.internal.loader",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.jdk11.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.ImageSingletons;
32+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
33+
import org.graalvm.nativeimage.hosted.Feature;
34+
35+
import com.oracle.svm.core.annotate.AutomaticFeature;
36+
import com.oracle.svm.core.reflect.SubstrateConstructorAccessor;
37+
import com.oracle.svm.core.reflect.SubstrateMethodAccessor;
38+
import com.oracle.svm.core.reflect.SubstrateReflectionAccessorFactory;
39+
40+
final class SubstrateReflectionAccessorFactoryJDK11 implements SubstrateReflectionAccessorFactory {
41+
@Override
42+
public SubstrateMethodAccessor createMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
43+
return new SubstrateMethodAccessorJDK11(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
44+
}
45+
46+
@Override
47+
public SubstrateConstructorAccessor createConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
48+
return new SubstrateConstructorAccessorJDK11(member, newInstanceFunctionPointer);
49+
}
50+
}
51+
52+
@AutomaticFeature
53+
final class SubstrateReflectionAccessorFactoryJDK11Feature implements Feature {
54+
@Override
55+
public void afterRegistration(AfterRegistrationAccess access) {
56+
ImageSingletons.add(SubstrateReflectionAccessorFactory.class, new SubstrateReflectionAccessorFactoryJDK11());
57+
}
58+
}
59+
60+
final class SubstrateMethodAccessorJDK11 extends SubstrateMethodAccessor implements jdk.internal.reflect.MethodAccessor {
61+
SubstrateMethodAccessorJDK11(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
62+
super(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
63+
}
64+
}
65+
66+
final class SubstrateConstructorAccessorJDK11 extends SubstrateConstructorAccessor implements jdk.internal.reflect.ConstructorAccessor {
67+
SubstrateConstructorAccessorJDK11(Executable member, CFunctionPointer newInstanceFunctionPointer) {
68+
super(member, newInstanceFunctionPointer);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.jdk8.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.ImageSingletons;
32+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
33+
import org.graalvm.nativeimage.hosted.Feature;
34+
35+
import com.oracle.svm.core.annotate.AutomaticFeature;
36+
import com.oracle.svm.core.reflect.SubstrateConstructorAccessor;
37+
import com.oracle.svm.core.reflect.SubstrateMethodAccessor;
38+
import com.oracle.svm.core.reflect.SubstrateReflectionAccessorFactory;
39+
40+
final class SubstrateReflectionAccessorFactoryJDK8 implements SubstrateReflectionAccessorFactory {
41+
@Override
42+
public SubstrateMethodAccessor createMethodAccessor(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
43+
return new SubstrateMethodAccessorJDK8(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
44+
}
45+
46+
@Override
47+
public SubstrateConstructorAccessor createConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
48+
return new SubstrateConstructorAccessorJDK8(member, newInstanceFunctionPointer);
49+
}
50+
}
51+
52+
@AutomaticFeature
53+
final class SubstrateReflectionAccessorFactoryJDK8Feature implements Feature {
54+
@Override
55+
public void afterRegistration(AfterRegistrationAccess access) {
56+
ImageSingletons.add(SubstrateReflectionAccessorFactory.class, new SubstrateReflectionAccessorFactoryJDK8());
57+
}
58+
}
59+
60+
final class SubstrateMethodAccessorJDK8 extends SubstrateMethodAccessor implements sun.reflect.MethodAccessor {
61+
SubstrateMethodAccessorJDK8(Executable member, CFunctionPointer invokeFunctionPointer, CFunctionPointer invokeSpecialFunctionPointer) {
62+
super(member, invokeFunctionPointer, invokeSpecialFunctionPointer);
63+
}
64+
}
65+
66+
final class SubstrateConstructorAccessorJDK8 extends SubstrateConstructorAccessor implements sun.reflect.ConstructorAccessor {
67+
SubstrateConstructorAccessorJDK8(Executable member, CFunctionPointer newInstanceFunctionPointer) {
68+
super(member, newInstanceFunctionPointer);
69+
}
70+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/classinitialization/ClassInitializationInfo.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525
package com.oracle.svm.core.classinitialization;
2626

2727
// Checkstyle: stop
28+
2829
import java.util.concurrent.locks.Condition;
2930
import java.util.concurrent.locks.ReentrantLock;
3031

31-
import com.oracle.svm.core.jdk.InternalVMMethod;
3232
import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;
3333
import org.graalvm.nativeimage.Platform;
3434
import org.graalvm.nativeimage.Platforms;
3535
import org.graalvm.nativeimage.c.function.CFunctionPointer;
3636

3737
import com.oracle.svm.core.annotate.InvokeJavaFunctionPointer;
3838
import com.oracle.svm.core.hub.DynamicHub;
39+
import com.oracle.svm.core.jdk.InternalVMMethod;
3940
import com.oracle.svm.core.snippets.SubstrateForeignCallTarget;
41+
import com.oracle.svm.core.util.VMError;
4042

4143
import sun.misc.Unsafe;
4244
// Checkstyle: resume
@@ -294,7 +296,7 @@ private static void initialize(ClassInitializationInfo info, DynamicHub hub) {
294296
Throwable exception = null;
295297
try {
296298
/* Step 9: Next, execute the class or interface initialization method of C. */
297-
info.invokeClassInitializer();
299+
info.invokeClassInitializer(hub);
298300
} catch (Throwable ex) {
299301
exception = ex;
300302
}
@@ -372,9 +374,17 @@ private void setInitializationStateAndNotify(InitState state) {
372374
}
373375
}
374376

375-
private void invokeClassInitializer() {
377+
private void invokeClassInitializer(DynamicHub hub) {
376378
if (classInitializer != null) {
377-
((ClassInitializerFunctionPointer) classInitializer.functionPointer).invoke();
379+
ClassInitializerFunctionPointer functionPointer = (ClassInitializerFunctionPointer) classInitializer.functionPointer;
380+
if (functionPointer.isNull()) {
381+
throw invokeClassInitializerError(hub);
382+
}
383+
functionPointer.invoke();
378384
}
379385
}
386+
387+
private static RuntimeException invokeClassInitializerError(DynamicHub hub) {
388+
throw VMError.shouldNotReachHere("No classInitializer.functionPointer for class " + hub.getName());
389+
}
380390
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.reflect;
26+
27+
import com.oracle.svm.core.jdk.InternalVMMethod;
28+
import com.oracle.svm.core.util.VMError;
29+
30+
/**
31+
* This class is used as the declaring class for reflection invocation methods. These methods have
32+
* manually created Graal IR, and are invoked via a function pointer call from
33+
* {@link SubstrateMethodAccessor} and {@link SubstrateConstructorAccessor}
34+
*/
35+
@InternalVMMethod
36+
@SuppressWarnings("unused")
37+
public final class ReflectionAccessorHolder {
38+
39+
/**
40+
* Signature prototype for invoking a method via a {@link SubstrateMethodAccessor}.
41+
*/
42+
private static Object invokePrototype(Object obj, Object[] args) {
43+
throw VMError.shouldNotReachHere("Only used as a prototype for generated methods");
44+
}
45+
46+
/**
47+
* Signature prototype for allocating a new instance via a {@link SubstrateConstructorAccessor}.
48+
*/
49+
private static Object newInstancePrototype(Object[] args) {
50+
throw VMError.shouldNotReachHere("Only used as a prototype for generated methods");
51+
}
52+
53+
/*
54+
* Methods for throwing exceptions when a method or constructor is used in an illegal way.
55+
*/
56+
57+
private static Object invokeSpecialError(Object obj, Object[] args) {
58+
throw new IllegalArgumentException("Static or abstract method cannot be invoked using invokeSpecial");
59+
}
60+
61+
private static Object newInstanceError(Object[] args) throws InstantiationException {
62+
throw new InstantiationException("Only non-abstract instance classes can be instantiated using reflection");
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.reflect;
26+
27+
// Checkstyle: allow reflection
28+
29+
import java.lang.reflect.Executable;
30+
31+
import org.graalvm.nativeimage.c.function.CFunctionPointer;
32+
33+
import com.oracle.svm.core.annotate.InvokeJavaFunctionPointer;
34+
import com.oracle.svm.core.jdk.InternalVMMethod;
35+
import com.oracle.svm.core.util.VMError;
36+
37+
@InternalVMMethod
38+
public abstract class SubstrateConstructorAccessor {
39+
40+
interface ConstructorNewInstanceFunctionPointer extends CFunctionPointer {
41+
@InvokeJavaFunctionPointer
42+
Object invoke(Object[] args);
43+
}
44+
45+
private final Executable member;
46+
private final CFunctionPointer newInstanceFunctionPointer;
47+
48+
protected SubstrateConstructorAccessor(Executable member, CFunctionPointer newInstanceFunctionPointer) {
49+
this.member = member;
50+
this.newInstanceFunctionPointer = newInstanceFunctionPointer;
51+
}
52+
53+
public Object newInstance(Object[] args) {
54+
ConstructorNewInstanceFunctionPointer functionPointer = (ConstructorNewInstanceFunctionPointer) this.newInstanceFunctionPointer;
55+
if (functionPointer.isNull()) {
56+
throw newInstanceError();
57+
}
58+
return functionPointer.invoke(args);
59+
}
60+
61+
private RuntimeException newInstanceError() {
62+
throw VMError.shouldNotReachHere("No SubstrateConstructorAccessor.newInstanceFunctionPointer for " + member);
63+
}
64+
}

0 commit comments

Comments
 (0)