Skip to content

Commit 43b6039

Browse files
committed
Handle HotSpotJavaType returned by BootstrapMethodInvocation.getStaticArguments
1 parent eb792f6 commit 43b6039

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/WrappedConstantPool.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import java.util.stream.Collectors;
3333

3434
import com.oracle.graal.pointsto.constraints.UnresolvedElementException;
35+
import com.oracle.graal.pointsto.util.GraalAccess;
3536
import com.oracle.svm.util.ReflectionUtil;
3637

38+
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
3739
import jdk.graal.compiler.core.common.BootstrapMethodIntrospection;
3840
import jdk.graal.compiler.debug.GraalError;
3941
import jdk.graal.compiler.serviceprovider.BootstrapMethodIntrospectionImpl;
@@ -64,7 +66,27 @@ public int length() {
6466
}
6567

6668
private JavaConstant lookupConstant(JavaConstant constant) {
67-
return universe.lookup(constant);
69+
return universe.lookup(extractResolvedType(constant));
70+
}
71+
72+
public JavaConstant extractResolvedType(JavaConstant constant) {
73+
if (constant != null && constant.getJavaKind().isObject() && !constant.isNull()) {
74+
SnippetReflectionProvider snippetReflection = GraalAccess.getOriginalSnippetReflection();
75+
if (snippetReflection.asObject(Object.class, constant) instanceof ResolvedJavaType resolvedJavaType) {
76+
/*
77+
* BootstrapMethodInvocation.getStaticArguments can output a constant containing a
78+
* HotspotJavaType when a static argument of type Class if loaded lazily in pull
79+
* mode. In this case, the type has to be converted back to a Class, as it would
80+
* cause a hotspot value to be reachable otherwise.
81+
*
82+
* If the constant contains an UnresolvedJavaType, it cannot be converted as a
83+
* Class. It is not a problem for this type to be reachable, so those constants can
84+
* be handled later.
85+
*/
86+
return snippetReflection.forObject(OriginalClassProvider.getJavaClass(resolvedJavaType));
87+
}
88+
}
89+
return constant;
6890
}
6991

7092
/**

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/AnalysisGraphBuilderPhase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import jdk.vm.ci.meta.ResolvedJavaField;
8484
import jdk.vm.ci.meta.ResolvedJavaMethod;
8585
import jdk.vm.ci.meta.ResolvedJavaType;
86+
import jdk.vm.ci.meta.UnresolvedJavaType;
8687

8788
public class AnalysisGraphBuilderPhase extends SharedGraphBuilderPhase {
8889

@@ -175,6 +176,16 @@ protected void genInvokeDynamic(int cpi, int opcode) {
175176
JavaConstant type = ((ImageHeapInstance) bootstrap.getType()).getHostedObject();
176177
MethodType methodType = (MethodType) ((DirectSubstrateObjectConstant) type).getObject();
177178

179+
for (JavaConstant argument : staticArgumentsList) {
180+
if (argument instanceof ImageHeapInstance imageHeapInstance) {
181+
Object arg = ((DirectSubstrateObjectConstant) imageHeapInstance.getHostedObject()).getObject();
182+
if (arg instanceof UnresolvedJavaType unresolvedJavaType) {
183+
handleUnresolvedType(unresolvedJavaType);
184+
return;
185+
}
186+
}
187+
}
188+
178189
if (!bootstrapMethodHandler.checkBootstrapParameters(bootstrap.getMethod(), staticArgumentsList, false)) {
179190
WrongMethodTypeException cause = new WrongMethodTypeException("Cannot convert " + methodType + " to correct MethodType");
180191
replaceWithThrowingAtRuntime(this, new BootstrapMethodError("Bootstrap method initialization exception", cause));
@@ -187,6 +198,10 @@ protected void genInvokeDynamic(int cpi, int opcode) {
187198
*/
188199

189200
Object initializedCallSite = bootstrapMethodHandler.resolveLinkedObject(bci, cpi, opcode, bootstrap, parameterLength, staticArgumentsList, isVarargs, false);
201+
if (initializedCallSite instanceof UnresolvedJavaType unresolvedJavaType) {
202+
handleUnresolvedType(unresolvedJavaType);
203+
return;
204+
}
190205
if (initializedCallSite instanceof Throwable) {
191206
return;
192207
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/SharedGraphBuilderPhase.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
import jdk.vm.ci.meta.ResolvedJavaField;
143143
import jdk.vm.ci.meta.ResolvedJavaMethod;
144144
import jdk.vm.ci.meta.ResolvedJavaType;
145+
import jdk.vm.ci.meta.UnresolvedJavaType;
145146

146147
public abstract class SharedGraphBuilderPhase extends GraphBuilderPhase.Instance {
147148
final WordTypes wordTypes;
@@ -923,6 +924,15 @@ private Object loadConstantDynamic(int cpi, int opcode) {
923924
DynamicHub typeClass = (DynamicHub) ((DirectSubstrateObjectConstant) type).getObject();
924925
boolean isPrimitive = typeClass.isPrimitive();
925926

927+
for (JavaConstant argument : staticArguments) {
928+
if (argument instanceof ImageHeapInstance imageHeapInstance) {
929+
Object arg = ((DirectSubstrateObjectConstant) imageHeapInstance.getHostedObject()).getObject();
930+
if (arg instanceof UnresolvedJavaType) {
931+
return arg;
932+
}
933+
}
934+
}
935+
926936
if (isBootstrapInvocationInvalid(bootstrap, parameterLength, staticArguments, isVarargs, typeClass.getHostedJavaClass())) {
927937
/*
928938
* The number of provided arguments does not match the signature of the
@@ -1015,7 +1025,7 @@ protected Object resolveLinkedObject(int bci, int cpi, int opcode, BootstrapMeth
10151025
Object argConstant = loadConstantDynamic(argCpi, opcode == Opcodes.INVOKEDYNAMIC ? Opcodes.LDC : opcode);
10161026
if (argConstant instanceof ValueNode valueNode) {
10171027
currentNode = valueNode;
1018-
} else if (argConstant instanceof Throwable) {
1028+
} else if (argConstant instanceof Throwable || argConstant instanceof UnresolvedJavaType) {
10191029
/* A nested constant dynamic threw. */
10201030
return argConstant;
10211031
} else {

0 commit comments

Comments
 (0)