-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[GR-41977] Adapt to JVMCI API changes. #5455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b521dcf
3491c52
a721e08
5075a46
989316d
1eaf01e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ | |
|
|
||
| import java.lang.reflect.Executable; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Objects; | ||
|
|
||
| import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; | ||
| import org.graalvm.compiler.debug.GraalError; | ||
|
|
@@ -39,52 +39,24 @@ | |
|
|
||
| import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; | ||
| import jdk.vm.ci.hotspot.HotSpotObjectConstant; | ||
| import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; | ||
| import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; | ||
| import jdk.vm.ci.meta.JavaConstant; | ||
| import jdk.vm.ci.meta.JavaKind; | ||
| import jdk.vm.ci.meta.ResolvedJavaField; | ||
| import jdk.vm.ci.meta.ResolvedJavaMethod; | ||
| import jdk.vm.ci.meta.ResolvedJavaType; | ||
| import jdk.vm.ci.services.Services; | ||
|
|
||
| public class HotSpotSnippetReflectionProvider implements SnippetReflectionProvider { | ||
|
|
||
| private final HotSpotGraalRuntimeProvider runtime; | ||
| private final HotSpotConstantReflectionProvider constantReflection; | ||
| private final WordTypes wordTypes; | ||
|
|
||
| /* | ||
| * GR-41976: JVMCI currently does not have public API to convert methods and fields back to | ||
| * reflection objects. So we do it via reflective invocation of JVMCI internals. | ||
| * | ||
| * These fields are intentionally not static, because we do not want libgraal to run with the | ||
| * state initialized at image build time. | ||
| */ | ||
| private final Method hotSpotJDKReflectionGetMethod; | ||
| private final Method hotSpotJDKReflectionGetField; | ||
|
|
||
| public HotSpotSnippetReflectionProvider(HotSpotGraalRuntimeProvider runtime, HotSpotConstantReflectionProvider constantReflection, WordTypes wordTypes) { | ||
| this.runtime = runtime; | ||
| this.constantReflection = constantReflection; | ||
| this.wordTypes = wordTypes; | ||
|
|
||
| if (Services.IS_IN_NATIVE_IMAGE) { | ||
| /* No access to method/field mirrors when running in libgraal. */ | ||
| hotSpotJDKReflectionGetMethod = null; | ||
| hotSpotJDKReflectionGetField = null; | ||
| } else { | ||
| try { | ||
| Class<?> hsJDKReflection = Class.forName("jdk.vm.ci.hotspot.HotSpotJDKReflection"); | ||
| hotSpotJDKReflectionGetMethod = lookupMethod(hsJDKReflection, "getMethod", Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl")); | ||
| hotSpotJDKReflectionGetField = lookupMethod(hsJDKReflection, "getField", Class.forName("jdk.vm.ci.hotspot.HotSpotResolvedJavaFieldImpl")); | ||
| } catch (ReflectiveOperationException ex) { | ||
| /* | ||
| * Note that older JVMCI versions do not have those methods even when running in JDK | ||
| * mode and not in libgraal mode. But that affects only OpenJDK 11, and we no longer | ||
| * support JDK 11 at all. OpenJDK 17 already has the necessary methods. | ||
| */ | ||
| throw GraalError.shouldNotReachHere(ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -150,52 +122,23 @@ public Class<?> originalClass(ResolvedJavaType type) { | |
| return runtime().getMirror(type); | ||
| } | ||
|
|
||
| private static Method lookupMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) throws ReflectiveOperationException { | ||
| Method result = declaringClass.getDeclaredMethod(methodName, parameterTypes); | ||
| result.setAccessible(true); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public Executable originalMethod(ResolvedJavaMethod method) { | ||
| Objects.requireNonNull(method); | ||
| GraalError.guarantee(method instanceof HotSpotResolvedJavaMethod, "Unexpected implementation class: %s", method.getClass()); | ||
|
|
||
| if (method.isClassInitializer()) { | ||
| /* <clinit> methods never have a corresponding java.lang.reflect.Method. */ | ||
| return null; | ||
| } | ||
|
|
||
| if (hotSpotJDKReflectionGetMethod == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return (Executable) hotSpotJDKReflectionGetMethod.invoke(null, method); | ||
| } catch (ReflectiveOperationException ex) { | ||
| throw rethrow(ex.getCause()); | ||
| } | ||
| return runtime().getMirror(method); | ||
| } | ||
|
|
||
| @Override | ||
| public Field originalField(ResolvedJavaField field) { | ||
| if (hotSpotJDKReflectionGetField == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return (Field) hotSpotJDKReflectionGetField.invoke(null, field); | ||
| } catch (ReflectiveOperationException ex) { | ||
| if (ex.getCause() instanceof IllegalArgumentException) { | ||
| /** | ||
| * GR-41974: A bug in JVMCI prevents the lookup of the java.lang.reflect.Field. | ||
| * Since even calling getName() on the ResolvedJavaField crashes for such fields, we | ||
| * also cannot use Class.getDeclaredField as a workaround for lookup. Our only | ||
| * option is to return null for now. | ||
| */ | ||
| return null; | ||
| } | ||
| throw rethrow(ex.getCause()); | ||
| } | ||
| } | ||
| Objects.requireNonNull(field); | ||
| GraalError.guarantee(field instanceof HotSpotResolvedJavaField, "Unexpected implementation class: %s", field.getClass()); | ||
|
|
||
| @SuppressWarnings({"unchecked"}) | ||
| private static <E extends Throwable> RuntimeException rethrow(Throwable ex) throws E { | ||
| throw (E) ex; | ||
| return runtime().getMirror(field); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This results in: When building with JDK 17 (both OpenJDK and LabsJDK). See https://github.com/graalvm/mandrel/actions/runs/3499526212/jobs/5861191268#step:8:445 and https://github.com/graalvm/mandrel/actions/runs/3499602031/jobs/5861349246#step:7:330, respectively. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This results in:
When building with JDK 17 (both OpenJDK and LabsJDK).
See https://github.com/graalvm/mandrel/actions/runs/3499526212/jobs/5861191268#step:8:445 and https://github.com/graalvm/mandrel/actions/runs/3499602031/jobs/5861349246#step:7:330, respectively.