Skip to content

Commit ffbfca9

Browse files
[GR-57993] Avoid the use of HotSpot JVMCI types to detect host vm objects.
PullRequest: graal/18762
2 parents f16e655 + b50452b commit ffbfca9

File tree

10 files changed

+115
-86
lines changed

10 files changed

+115
-86
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/TruffleDebugJavaMethod.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package jdk.graal.compiler.truffle;
2626

27+
import java.util.Objects;
28+
2729
import com.oracle.truffle.compiler.TruffleCompilable;
2830
import com.oracle.truffle.compiler.TruffleCompilationTask;
2931

@@ -51,27 +53,44 @@ public final class TruffleDebugJavaMethod implements JavaMethod {
5153
}
5254
}
5355

54-
private static class TruffleJavaType implements JavaType {
56+
public static final class TruffleSignature implements Signature {
57+
private final TruffleJavaType returnType;
5558

56-
private final String name;
59+
public TruffleSignature(TruffleJavaType returnType) {
60+
this.returnType = returnType;
61+
}
5762

58-
final Signature signature = new Signature() {
63+
@Override
64+
public JavaType getReturnType(ResolvedJavaType accessingClass) {
65+
return returnType;
66+
}
5967

60-
@Override
61-
public JavaType getReturnType(ResolvedJavaType accessingClass) {
62-
return TruffleJavaType.this;
63-
}
68+
@Override
69+
public int getParameterCount(boolean receiver) {
70+
return 0;
71+
}
6472

65-
@Override
66-
public int getParameterCount(boolean receiver) {
67-
return 0;
68-
}
73+
@Override
74+
public JavaType getParameterType(int index, ResolvedJavaType accessingClass) {
75+
throw new IndexOutOfBoundsException();
76+
}
6977

70-
@Override
71-
public JavaType getParameterType(int index, ResolvedJavaType accessingClass) {
72-
throw new IndexOutOfBoundsException();
78+
@Override
79+
public boolean equals(Object obj) {
80+
if (!(obj instanceof TruffleSignature truffleSignature)) {
81+
return false;
7382
}
74-
};
83+
return returnType.equals(truffleSignature.returnType);
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
return returnType.hashCode();
89+
}
90+
}
91+
92+
public static final class TruffleJavaType implements JavaType {
93+
private final String name;
7594

7695
TruffleJavaType(int tier) {
7796
this.name = "LTruffleIR/Tier" + tier + ";";
@@ -141,12 +160,12 @@ public int hashCode() {
141160

142161
@Override
143162
public Signature getSignature() {
144-
return TIERS[task.tier()].signature;
163+
return new TruffleSignature(TIERS[task.tier()]);
145164
}
146165

147166
@Override
148167
public String getName() {
149-
return (compilable.getName() + "").replace('.', '_').replace(' ', '_');
168+
return Objects.toString(compilable.getName()).replace('.', '_').replace(' ', '_');
150169
}
151170

152171
@Override

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/heap/ImageHeapScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private Optional<JavaConstant> maybeReplace(JavaConstant constant, ScanReason re
417417
/* Enhance the unsupported feature message with the object trace and rethrow. */
418418
StringBuilder backtrace = new StringBuilder();
419419
ObjectScanner.buildObjectBacktrace(bb, reason, backtrace);
420-
throw new UnsupportedFeatureException(e.getMessage() + System.lineSeparator() + backtrace);
420+
throw new UnsupportedFeatureException(e.getMessage() + System.lineSeparator() + backtrace, e);
421421
}
422422

423423
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/meta/CompressedNullConstant.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package com.oracle.svm.core.meta;
2626

2727
import jdk.graal.compiler.core.common.type.CompressibleConstant;
28-
2928
import jdk.vm.ci.meta.JavaConstant;
3029
import jdk.vm.ci.meta.JavaKind;
3130

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/SubstrateGraalUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.EnumMap;
3232
import java.util.Map;
3333

34-
import jdk.graal.compiler.phases.util.Providers;
3534
import org.graalvm.collections.EconomicMap;
3635
import org.graalvm.nativeimage.ImageSingletons;
3736

@@ -70,8 +69,8 @@
7069
import jdk.graal.compiler.options.OptionValues;
7170
import jdk.graal.compiler.phases.OptimisticOptimizations;
7271
import jdk.graal.compiler.phases.tiers.Suites;
72+
import jdk.graal.compiler.phases.util.Providers;
7373
import jdk.vm.ci.code.Architecture;
74-
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
7574
import jdk.vm.ci.meta.ConstantReflectionProvider;
7675
import jdk.vm.ci.meta.JavaConstant;
7776

@@ -232,22 +231,23 @@ public static JavaConstant hostedToRuntime(JavaConstant constant, ConstantReflec
232231
}
233232

234233
/**
235-
* Prepares a hosted {@link ImageHeapConstant} for runtime compilation: it unwraps the
236-
* {@link HotSpotObjectConstant} and wraps the hosted object into a
237-
* {@link SubstrateObjectConstant}. We reuse the identity hash code of the heap constant.
234+
* Prepares a hosted {@link ImageHeapConstant} for runtime compilation: it unwraps the host
235+
* {@link JavaConstant} and wraps the hosted object into a {@link SubstrateObjectConstant}. We
236+
* reuse the identity hash code of the heap constant.
238237
*/
239238
public static JavaConstant hostedToRuntime(ImageHeapConstant heapConstant, ConstantReflectionProvider constantReflection) {
240239
JavaConstant hostedConstant = heapConstant.getHostedObject();
241-
VMError.guarantee(hostedConstant instanceof HotSpotObjectConstant, "Expected to find HotSpotObjectConstant, found %s", hostedConstant);
240+
VMError.guarantee(hostedConstant.getJavaKind().isObject() && !hostedConstant.isDefaultForKind() && !(hostedConstant instanceof ImageHeapConstant),
241+
"Expected to find host object JavaConstant, found %s", hostedConstant);
242242
Object hostedObject = GraalAccess.getOriginalSnippetReflection().asObject(Object.class, hostedConstant);
243243
return SubstrateObjectConstant.forObject(hostedObject, ((IdentityHashCodeProvider) constantReflection).identityHashCode(heapConstant));
244244
}
245245

246246
/**
247247
* Transforms a {@link SubstrateObjectConstant} from an encoded graph into an
248248
* {@link ImageHeapConstant} for hosted processing: it unwraps the hosted object from the
249-
* {@link SubstrateObjectConstant}, wraps it into an {@link HotSpotObjectConstant}, then
250-
* redirects the lookup through the {@link ImageHeapScanner}.
249+
* {@link SubstrateObjectConstant}, wraps it into a host {@link JavaConstant}, then redirects
250+
* the lookup through the {@link ImageHeapScanner}.
251251
*/
252252
public static JavaConstant runtimeToHosted(JavaConstant constant, ImageHeapScanner scanner) {
253253
if (constant instanceof SubstrateObjectConstant) {

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/runtimecompilation/GraalGraphObjectReplacer.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
import com.oracle.graal.pointsto.constraints.UnsupportedFeatureException;
3636
import com.oracle.graal.pointsto.heap.ImageHeapConstant;
37+
import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider;
38+
import com.oracle.graal.pointsto.infrastructure.OriginalFieldProvider;
39+
import com.oracle.graal.pointsto.infrastructure.OriginalMethodProvider;
3740
import com.oracle.graal.pointsto.infrastructure.ResolvedSignature;
3841
import com.oracle.graal.pointsto.meta.AnalysisField;
3942
import com.oracle.graal.pointsto.meta.AnalysisMethod;
@@ -42,8 +45,10 @@
4245
import com.oracle.svm.common.meta.MultiMethod;
4346
import com.oracle.svm.core.code.CodeInfoTable;
4447
import com.oracle.svm.core.code.ImageCodeInfo;
48+
import com.oracle.svm.core.graal.meta.SharedRuntimeMethod;
4549
import com.oracle.svm.core.graal.nodes.SubstrateFieldLocationIdentity;
4650
import com.oracle.svm.core.hub.DynamicHub;
51+
import com.oracle.svm.core.meta.SubstrateObjectConstant;
4752
import com.oracle.svm.core.option.HostedOptionKey;
4853
import com.oracle.svm.core.util.HostedStringDeduplication;
4954
import com.oracle.svm.core.util.ObservableImageHeapMapProvider;
@@ -76,22 +81,21 @@
7681
import jdk.graal.compiler.hotspot.HotSpotBackendFactory;
7782
import jdk.graal.compiler.hotspot.SnippetResolvedJavaMethod;
7883
import jdk.graal.compiler.hotspot.SnippetResolvedJavaType;
84+
import jdk.graal.compiler.hotspot.SnippetSignature;
7985
import jdk.graal.compiler.nodes.FieldLocationIdentity;
8086
import jdk.graal.compiler.options.Option;
8187
import jdk.graal.compiler.phases.util.Providers;
88+
import jdk.graal.compiler.truffle.TruffleDebugJavaMethod;
8289
import jdk.vm.ci.code.Architecture;
8390
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
84-
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
85-
import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
86-
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
87-
import jdk.vm.ci.hotspot.HotSpotResolvedJavaType;
88-
import jdk.vm.ci.hotspot.HotSpotSignature;
8991
import jdk.vm.ci.meta.JavaConstant;
9092
import jdk.vm.ci.meta.JavaType;
9193
import jdk.vm.ci.meta.MetaAccessProvider;
9294
import jdk.vm.ci.meta.ResolvedJavaField;
9395
import jdk.vm.ci.meta.ResolvedJavaMethod;
9496
import jdk.vm.ci.meta.ResolvedJavaType;
97+
import jdk.vm.ci.meta.Signature;
98+
import jdk.vm.ci.runtime.JVMCIRuntime;
9599

96100
/**
97101
* Replaces Graal related objects during analysis in the universe.
@@ -142,6 +146,7 @@ public void setAnalysisAccess(BeforeAnalysisAccessImpl beforeAnalysisAccess) {
142146
this.beforeAnalysisAccess = beforeAnalysisAccess;
143147
}
144148

149+
@SuppressWarnings("unchecked")
145150
@Override
146151
public Object apply(Object source) {
147152

@@ -155,13 +160,14 @@ public Object apply(Object source) {
155160
return dest;
156161
}
157162

158-
if (source instanceof SnippetResolvedJavaMethod || source instanceof SnippetResolvedJavaType) {
163+
if (source instanceof SnippetResolvedJavaMethod || source instanceof SnippetResolvedJavaType || source instanceof SnippetSignature) {
164+
// skip checks for "hotspot" in the class name
159165
return source;
160166
}
161167
if (source instanceof MetaAccessProvider) {
162168
dest = sProviders.getMetaAccessProvider();
163-
} else if (source instanceof HotSpotJVMCIRuntime) {
164-
throw new UnsupportedFeatureException("HotSpotJVMCIRuntime should not appear in the image: " + source);
169+
} else if (source instanceof JVMCIRuntime) {
170+
throw new UnsupportedFeatureException("JVMCIRuntime should not appear in the image: " + source);
165171
} else if (source instanceof GraalHotSpotVMConfig) {
166172
throw new UnsupportedFeatureException("GraalHotSpotVMConfig should not appear in the image: " + source);
167173
} else if (source instanceof HotSpotBackendFactory) {
@@ -188,26 +194,38 @@ public Object apply(Object source) {
188194
/* Ensure lazily initialized shortName field is computed. */
189195
((NodeClass<?>) source).shortName();
190196

191-
} else if (source instanceof HotSpotResolvedJavaMethod) {
192-
throw new UnsupportedFeatureException(source.toString());
193-
} else if (source instanceof HotSpotResolvedJavaField) {
194-
throw new UnsupportedFeatureException(source.toString());
195-
} else if (source instanceof HotSpotResolvedJavaType) {
196-
throw new UnsupportedFeatureException(source.toString());
197-
} else if (source instanceof HotSpotSignature) {
198-
throw new UnsupportedFeatureException(source.toString());
199-
} else if (source instanceof HotSpotObjectConstant) {
200-
throw new UnsupportedFeatureException(source.toString());
201-
} else if (source instanceof ResolvedJavaMethod && !(source instanceof SubstrateMethod)) {
202-
dest = createMethod((ResolvedJavaMethod) source);
203-
} else if (source instanceof ResolvedJavaField && !(source instanceof SubstrateField)) {
204-
dest = createField((ResolvedJavaField) source);
205-
} else if (source instanceof ResolvedJavaType && !(source instanceof SubstrateType)) {
206-
dest = createType((ResolvedJavaType) source);
197+
} else if (source instanceof ResolvedJavaMethod) {
198+
if (source instanceof OriginalMethodProvider) {
199+
dest = createMethod((ResolvedJavaMethod) source);
200+
} else if (!(source instanceof SharedRuntimeMethod)) {
201+
throw new UnsupportedFeatureException(source.toString());
202+
}
203+
} else if (source instanceof ResolvedJavaField) {
204+
if (source instanceof OriginalFieldProvider) {
205+
dest = createField((ResolvedJavaField) source);
206+
} else if (!(source instanceof SubstrateField)) {
207+
throw new UnsupportedFeatureException(source.toString());
208+
}
209+
} else if (source instanceof ResolvedJavaType) {
210+
if (source instanceof OriginalClassProvider) {
211+
dest = createType((ResolvedJavaType) source);
212+
} else if (!(source instanceof SubstrateType)) {
213+
throw new UnsupportedFeatureException(source.toString());
214+
}
215+
} else if (source instanceof Signature) {
216+
if (source instanceof ResolvedSignature) {
217+
dest = createSignature((ResolvedSignature<AnalysisType>) source);
218+
} else if (!(source instanceof SubstrateSignature || source instanceof TruffleDebugJavaMethod.TruffleSignature)) {
219+
throw new UnsupportedFeatureException(source.toString());
220+
}
221+
} else if (source instanceof JavaConstant cst) {
222+
if (source instanceof ImageHeapConstant heapConstant) {
223+
dest = SubstrateGraalUtils.hostedToRuntime(heapConstant, beforeAnalysisAccess.getBigBang().getConstantReflectionProvider());
224+
} else if (cst.getJavaKind().isObject() && !cst.isDefaultForKind() && !(cst instanceof SubstrateObjectConstant)) {
225+
throw new UnsupportedFeatureException(source.toString());
226+
}
207227
} else if (source instanceof FieldLocationIdentity && !(source instanceof SubstrateFieldLocationIdentity)) {
208228
dest = createFieldLocationIdentity((FieldLocationIdentity) source);
209-
} else if (source instanceof ImageHeapConstant heapConstant) {
210-
dest = SubstrateGraalUtils.hostedToRuntime(heapConstant, beforeAnalysisAccess.getBigBang().getConstantReflectionProvider());
211229
}
212230

213231
assert dest != null;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,8 +1798,8 @@ public static void checkName(String name, AnalysisMethod method, BigBang bb) {
17981798
/*
17991799
* We do not want any parts of the native image generator in the generated image. Therefore,
18001800
* no element whose name contains "hosted" must be seen as reachable by the static analysis.
1801-
* The same holds for "hotspot" elements, which come from the hosting HotSpot VM, unless
1802-
* they are JDK internal types.
1801+
* The same holds for "host VM" elements, which come from the hosting VM, unless they are
1802+
* JDK internal types.
18031803
*/
18041804
String message = checkName(name);
18051805
if (message != null) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/FieldValueInterceptionSupport.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.oracle.graal.pointsto.infrastructure.OriginalFieldProvider;
4242
import com.oracle.graal.pointsto.meta.AnalysisField;
4343
import com.oracle.graal.pointsto.meta.AnalysisType;
44-
import com.oracle.graal.pointsto.meta.BaseLayerField;
4544
import com.oracle.graal.pointsto.util.GraalAccess;
4645
import com.oracle.svm.core.BuildPhaseProvider;
4746
import com.oracle.svm.core.RuntimeAssertionsSupport;
@@ -66,7 +65,6 @@
6665
import jdk.graal.compiler.nodes.java.LoadFieldNode;
6766
import jdk.graal.compiler.nodes.spi.CoreProviders;
6867
import jdk.graal.compiler.word.Word;
69-
import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
7068
import jdk.vm.ci.meta.JavaConstant;
7169
import jdk.vm.ci.meta.JavaKind;
7270
import jdk.vm.ci.meta.ResolvedJavaField;
@@ -115,7 +113,7 @@ public void registerFieldValueTransformer(Field reflectionField, FieldValueTrans
115113
}
116114

117115
public void registerFieldValueTransformer(ResolvedJavaField oField, FieldValueTransformer transformer) {
118-
assert oField instanceof HotSpotResolvedJavaField : oField;
116+
assert oField != null && !(oField instanceof OriginalFieldProvider) : oField;
119117
if (annotationSubstitutions.isDeleted(oField)) {
120118
throw UserError.abort("Cannot register a field value transformer for field %s: %s", oField.format("%H.%n"),
121119
"The field is marked as deleted, i.e., the field is not available on this platform");
@@ -155,8 +153,6 @@ private Object computeAndCacheFieldValueInterceptor(AnalysisField field) {
155153
field.beforeFieldValueAccess();
156154

157155
ResolvedJavaField oField = OriginalFieldProvider.getOriginalField(field);
158-
assert oField == null || oField instanceof HotSpotResolvedJavaField || oField instanceof BaseLayerField : oField;
159-
160156
FieldValueComputer computer = createFieldValueComputer(field);
161157
Object result;
162158
if (computer != null) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/SVMHostedValueProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.oracle.svm.hosted.classinitialization.SimulateClassInitializerSupport;
4343
import com.oracle.svm.hosted.meta.RelocatableConstant;
4444

45-
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
4645
import jdk.vm.ci.meta.ConstantReflectionProvider;
4746
import jdk.vm.ci.meta.JavaConstant;
4847
import jdk.vm.ci.meta.PrimitiveConstant;
@@ -135,9 +134,9 @@ public <T> T asObject(Class<T> type, JavaConstant constant) {
135134
* object from ImageHeapInfo we reference a {@link ImageHeapConstant} which allows using
136135
* simulated constant as partition limits. However, since the original
137136
* {@link ConstantReflectionProvider} is not aware of {@link ImageHeapConstant} it always treats
138-
* them as hosted objects and wraps them into a {@link HotSpotObjectConstant}. Therefore, we
139-
* need intercept the {@link HotSpotObjectConstant} and if it wraps an {@link ImageHeapConstant}
140-
* unwrap it and return the original constant.</li>
137+
* them as hosted objects and wraps them into a host {@link JavaConstant}. Therefore, we need
138+
* intercept the host {@link JavaConstant} and if it wraps an {@link ImageHeapConstant} unwrap
139+
* it and return the original constant.</li>
141140
* <li>Second, intercept {@link WordBase} constants. See {@link #interceptWordType(Object)} for
142141
* details.</li>
143142
* </ul>

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedSnippetReflectionProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
3838
import jdk.graal.compiler.word.WordTypes;
39-
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
4039
import jdk.vm.ci.meta.JavaConstant;
4140
import jdk.vm.ci.meta.JavaKind;
4241
import jdk.vm.ci.meta.ResolvedJavaField;
@@ -80,8 +79,8 @@ public <T> T asObject(Class<T> type, JavaConstant c) {
8079
return null;
8180
}
8281
}
83-
84-
if (type == Class.class && constant instanceof HotSpotObjectConstant) {
82+
VMError.guarantee(!(constant instanceof ImageHeapConstant));
83+
if (type == Class.class && constant.getJavaKind().isObject()) {
8584
/* Only unwrap the DynamicHub if a Class object is required explicitly. */
8685
if (heapScanner.getHostedValuesProvider().asObject(Object.class, constant) instanceof DynamicHub hub) {
8786
return type.cast(hub.getHostedJavaClass());

0 commit comments

Comments
 (0)