Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"labsjdk-ce-19": {"name": "labsjdk", "version": "ce-19+36-jvmci-23.0-b01", "platformspecific": true },
"labsjdk-ce-19Debug": {"name": "labsjdk", "version": "ce-19+36-jvmci-23.0-b01-debug", "platformspecific": true },
"labsjdk-ce-19-llvm": {"name": "labsjdk", "version": "ce-19+36-jvmci-23.0-b01-sulong", "platformspecific": true },
"labsjdk-ee-19": {"name": "labsjdk", "version": "ee-19.0.1+10-jvmci-23.0-b01", "platformspecific": true },
"labsjdk-ee-19Debug": {"name": "labsjdk", "version": "ee-19.0.1+10-jvmci-23.0-b01-debug", "platformspecific": true },
"labsjdk-ee-19": {"name": "labsjdk", "version": "ee-19.0.1+10-jvmci-41977.1-b01-20221107143847-ds_GR_41977-master-798a7efecb+697030422a", "platformspecific": true },
"labsjdk-ee-19Debug": {"name": "labsjdk", "version": "ee-19.0.1+10-jvmci-41977.1-b01-20221107143847-ds_GR_41977-master-798a7efecb+697030422a-debug", "platformspecific": true },
"labsjdk-ee-19-llvm": {"name": "labsjdk", "version": "ee-19.0.1+10-jvmci-23.0-b01-sulong", "platformspecific": true }
},

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in:

/home/runner/work/mandrel/mandrel/mandrel/compiler/src/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/meta/HotSpotSnippetReflectionProvider.java:134: error: incompatible types: ResolvedJavaMethod cannot be converted to ResolvedJavaType
        return runtime().getMirror(method);

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.

}

@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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in:

/home/runner/work/mandrel/mandrel/mandrel/compiler/src/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/meta/HotSpotSnippetReflectionProvider.java:142: error: incompatible types: ResolvedJavaField cannot be converted to ResolvedJavaType
        return runtime().getMirror(field);

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.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@

package com.oracle.graal.pointsto.standalone.heap;

import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;

import com.oracle.graal.pointsto.BigBang;
import com.oracle.graal.pointsto.ObjectScanningObserver;
import com.oracle.graal.pointsto.heap.ImageHeap;
import com.oracle.graal.pointsto.heap.ImageHeapScanner;
import com.oracle.graal.pointsto.heap.value.ValueSupplier;
import com.oracle.graal.pointsto.meta.AnalysisField;
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
import com.oracle.graal.pointsto.meta.UninitializedStaticFieldValueReader;
import com.oracle.graal.pointsto.util.AnalysisError;

import jdk.vm.ci.meta.ConstantReflectionProvider;
import jdk.vm.ci.meta.JavaConstant;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;

public class StandaloneImageHeapScanner extends ImageHeapScanner {
private ClassLoader classLoader;
Expand All @@ -63,8 +64,15 @@ protected Class<?> getClass(String className) {
protected ValueSupplier<JavaConstant> readHostedFieldValue(AnalysisField field, JavaConstant receiver) {
ValueSupplier<JavaConstant> ret = super.readHostedFieldValue(field, receiver);
if (ret.get() == null) {
JavaConstant constant = UninitializedStaticFieldValueReader.readUninitializedStaticValue(field, value -> universe.getSnippetReflection().forObject(value));
return ValueSupplier.eagerValue(constant);
/*
* Use the value from the constant pool attribute for the static field. That is the
* value before the class initializer is executed.
*/
JavaConstant constantValue = field.getConstantValue();
if (constantValue == null) {
constantValue = JavaConstant.defaultForKind(field.getStorageKind());
}
return ValueSupplier.eagerValue(constantValue);
} else {
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import com.oracle.graal.pointsto.meta.AnalysisField;
import com.oracle.graal.pointsto.meta.AnalysisType;
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
import com.oracle.graal.pointsto.meta.UninitializedStaticFieldValueReader;
import com.oracle.graal.pointsto.standalone.StandaloneHost;

import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
Expand All @@ -52,7 +52,7 @@ public final JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant r
AnalysisField analysisField = (AnalysisField) field;
JavaConstant ret = universe.lookup(super.readFieldValue(analysisField.wrapped, universe.toHosted(receiver)));
if (ret == null) {
ret = UninitializedStaticFieldValueReader.readUninitializedStaticValue(analysisField, value -> universe.getSnippetReflection().forObject(value));
ret = analysisField.getConstantValue();
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.oracle.graal.pointsto.util.AnalysisError.TypeNotFoundError;
import com.oracle.svm.util.ReflectionUtil;

import jdk.vm.ci.hotspot.HotSpotConstantPool;
import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaField;
Expand Down Expand Up @@ -87,18 +86,11 @@ public int length() {
private static final Method bsmGetType = bsmClass == null ? null : ReflectionUtil.lookupMethod(bsmClass, "getType");
private static final Method bsmGetStaticArguments = bsmClass == null ? null : ReflectionUtil.lookupMethod(bsmClass, "getStaticArguments");

public static void loadReferencedType(ConstantPool cp, int cpi, int opcode, boolean initialize) {
ConstantPool root = cp;
while (root instanceof WrappedConstantPool) {
root = ((WrappedConstantPool) root).wrapped;
}

@Override
public void loadReferencedType(int cpi, int opcode, boolean initialize) {
GraalError.guarantee(!initialize, "Must not initialize classes");
try {
/*
* GR-41975: loadReferencedType without triggering class initialization is available in
* HotSpotConstantPool, but not yet in ConstantPool.
*/
((HotSpotConstantPool) root).loadReferencedType(cpi, opcode, initialize);
wrapped.loadReferencedType(cpi, opcode, initialize);
} catch (Throwable ex) {
Throwable cause = ex;
if (cause instanceof BootstrapMethodError && cause.getCause() != null) {
Expand All @@ -112,7 +104,7 @@ public static void loadReferencedType(ConstantPool cp, int cpi, int opcode, bool

@Override
public void loadReferencedType(int cpi, int opcode) {
loadReferencedType(wrapped, cpi, opcode, false);
loadReferencedType(cpi, opcode, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.oracle.svm.util.UnsafePartitionKind;

import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaType;
Expand Down Expand Up @@ -552,4 +553,9 @@ public interface AnalysisFieldObserver {

TypeState interceptTypeState(AnalysisField field, TypeState typestate);
}

@Override
public JavaConstant getConstantValue() {
return getUniverse().lookup(getWrapped().getConstantValue());
}
}
Loading