Skip to content

Commit d6da064

Browse files
committed
[GR-59858] Mark type of hidden field reachable.
PullRequest: graal/19330
2 parents 3b151de + f642457 commit d6da064

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,18 @@ public TypeData computeTypeData(AnalysisType type) {
203203
return data;
204204
}
205205

206+
void markTypeReachable(AnalysisType type, ScanReason reason) {
207+
if (universe.sealed() && !type.isReachable()) {
208+
throw AnalysisError.typeNotFound(type);
209+
}
210+
type.registerAsReachable(reason);
211+
}
212+
206213
void markTypeInstantiated(AnalysisType type, ScanReason reason) {
207-
if (universe.sealed()) {
208-
AnalysisError.guarantee(type.isReachable(), "The type %s should have been reachable during analysis.", type);
209-
AnalysisError.guarantee(type.isInstantiated(), "The type %s should have been instantiated during analysis.", type);
210-
} else {
211-
type.registerAsInstantiated(reason);
214+
if (universe.sealed() && !type.isInstantiated()) {
215+
throw AnalysisError.typeNotFound(type);
212216
}
217+
type.registerAsInstantiated(reason);
213218
}
214219

215220
public JavaConstant getImageHeapConstant(JavaConstant constant) {
@@ -278,9 +283,9 @@ protected ImageHeapConstant getOrCreateImageHeapConstant(JavaConstant javaConsta
278283
return existingTask instanceof ImageHeapConstant ? (ImageHeapConstant) existingTask : ((AnalysisFuture<ImageHeapConstant>) existingTask).ensureDone();
279284
}
280285

281-
private static void ensureFieldPositionsComputed(ImageHeapConstant baseLayerConstant, ScanReason reason) {
286+
private void ensureFieldPositionsComputed(ImageHeapConstant baseLayerConstant, ScanReason reason) {
282287
AnalysisType objectType = baseLayerConstant.getType();
283-
objectType.registerAsReachable(reason);
288+
markTypeReachable(objectType, reason);
284289
objectType.getStaticFields();
285290
objectType.getInstanceFields(true);
286291
}
@@ -331,7 +336,7 @@ private ImageHeapArray createImageHeapObjectArray(JavaConstant constant, Analysi
331336
/* Read hosted array element values only when the array is initialized. */
332337
array.constantData.hostedValuesReader = new AnalysisFuture<>(() -> {
333338
checkSealed(reason, "Trying to materialize an ImageHeapObjectArray for %s after the ImageHeapScanner is sealed.", constant);
334-
type.registerAsReachable(reason);
339+
markTypeReachable(type, reason);
335340
ScanReason arrayReason = new ArrayScan(type, array, reason);
336341
Object[] elementValues = new Object[length];
337342
for (int idx = 0; idx < length; idx++) {
@@ -367,10 +372,10 @@ private ImageHeapInstance createImageHeapInstance(JavaConstant constant, Analysi
367372
/* If this is a Class constant register the corresponding type as reachable. */
368373
AnalysisType typeFromClassConstant = (AnalysisType) constantReflection.asJavaType(instance);
369374
if (typeFromClassConstant != null) {
370-
typeFromClassConstant.registerAsReachable(reason);
375+
markTypeReachable(typeFromClassConstant, reason);
371376
}
372377
/* We are about to query the type's fields, the type must be marked as reachable. */
373-
type.registerAsReachable(reason);
378+
markTypeReachable(type, reason);
374379
ResolvedJavaField[] instanceFields = type.getInstanceFields(true);
375380
Object[] hostedFieldValues = new Object[instanceFields.length];
376381
for (ResolvedJavaField javaField : instanceFields) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageCodeCache.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.Map;
4747
import java.util.Map.Entry;
48+
import java.util.Optional;
4849
import java.util.Set;
4950
import java.util.stream.Collectors;
5051

@@ -321,8 +322,14 @@ protected void buildRuntimeMetadata(DebugContext debug, SnippetReflectionProvide
321322
// Build run-time metadata.
322323
HostedFrameInfoCustomization frameInfoCustomization = new HostedFrameInfoCustomization();
323324
CodeInfoEncoder.Encoders encoders = new CodeInfoEncoder.Encoders(true, clazz -> {
324-
if (clazz != null && !imageHeap.hMetaAccess.optionalLookupJavaType(clazz).isPresent()) {
325-
throw VMError.shouldNotReachHere("Type added to the runtime metadata without being seen by the analysis: %s", clazz);
325+
if (clazz != null) {
326+
Optional<HostedType> hostedType = imageHeap.hMetaAccess.optionalLookupJavaType(clazz);
327+
if (hostedType.isPresent()) {
328+
boolean reachable = hostedType.get().getWrapped().isReachable();
329+
VMError.guarantee(reachable, "Type added to the runtime metadata was seen by the analysis, but not marked as reachable: %s", clazz);
330+
} else {
331+
throw VMError.shouldNotReachHere("Type added to the runtime metadata without being seen by the analysis: %s", clazz);
332+
}
326333
}
327334
});
328335
HostedConstantAccess hostedConstantAccess = new HostedConstantAccess(snippetReflection);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/reflect/ReflectionDataBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ private void checkSubtypeForOverridingFields(AnalysisType subtype, Collection<An
683683
AnalysisField subclassField = (AnalysisField) javaField;
684684
if (subclassField.getName().equals(registeredField.getName())) {
685685
hidingFields.add(subclassField);
686+
subclassField.getType().registerAsReachable("Is the declared type of a hiding Field used by reflection");
686687
}
687688
}
688689
}

0 commit comments

Comments
 (0)