Skip to content

Commit 96a968b

Browse files
Zeaveeloicottet
authored andcommitted
Ensure the list of forbidden modules for layered images is always complete when accessed
1 parent 16d1374 commit 96a968b

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/AbstractAnalysisEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public void tryRegisterNativeMethodsForBaseImage(ResolvedJavaType type) {
421421
* Some modules contain native methods that should not be included in the image because they
422422
* are hosted only, or because they are currently unsupported.
423423
*/
424-
Set<Module> forbiddenModules = hostVM.getForbiddenModules();
424+
Set<Module> forbiddenModules = hostVM.getSharedLayerForbiddenModules();
425425
if (forbiddenModules.contains(OriginalClassProvider.getJavaClass(type).getModule())) {
426426
return;
427427
}

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/api/HostVM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public boolean preventConstantFolding(AnalysisField aField) {
457457
return false;
458458
}
459459

460-
public Set<Module> getForbiddenModules() {
460+
public Set<Module> getSharedLayerForbiddenModules() {
461461
return Set.of();
462462
}
463463

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

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ public enum UsageKind {
247247
private final boolean buildingExtensionLayer = ImageLayerBuildingSupport.buildingExtensionLayer();
248248

249249
// All elements below are from the host VM universe, not the analysis universe
250-
private Set<ResolvedJavaField> sharedLayerExcludedFields;
250+
private final Set<ResolvedJavaField> sharedLayerExcludedFields;
251+
/**
252+
* Some modules contain native methods that should never be in the image, as they are either
253+
* hosted only, or currently unsupported in layered images.
254+
*/
255+
protected final Set<Module> sharedLayerForbiddenModules;
251256
private final ResolvedJavaType optionKeyType;
252257
private final ResolvedJavaType featureType;
253258

@@ -261,12 +266,6 @@ public enum UsageKind {
261266
private final boolean trackDynamicAccess;
262267
private DynamicAccessDetectionSupport dynamicAccessDetectionSupport = null;
263268

264-
/**
265-
* Some modules contain native methods that should never be in the image, as they are either
266-
* hosted only, or currently unsupported in layered images.
267-
*/
268-
private final Set<Module> forbiddenModules = new HashSet<>();
269-
270269
@SuppressWarnings("this-escape")
271270
public SVMHost(OptionValues options, ImageClassLoader loader, ClassInitializationSupport classInitializationSupport, AnnotationSubstitutionProcessor annotationSubstitutions,
272271
MissingRegistrationSupport missingRegistrationSupport) {
@@ -298,7 +297,11 @@ public SVMHost(OptionValues options, ImageClassLoader loader, ClassInitializatio
298297
}
299298
layerId = buildingImageLayer ? DynamicImageLayerInfo.getCurrentLayerNumber() : 0;
300299
if (buildingSharedLayer) {
301-
initializeSharedLayerExcludedFields();
300+
sharedLayerExcludedFields = initializeSharedLayerExcludedFields();
301+
sharedLayerForbiddenModules = initializeSharedLayerForbiddenModules();
302+
} else {
303+
sharedLayerExcludedFields = null;
304+
sharedLayerForbiddenModules = null;
302305
}
303306
layeredStaticFieldSupport = buildingImageLayer ? LayeredStaticFieldSupport.singleton() : null;
304307

@@ -1015,41 +1018,60 @@ private ResolvedJavaField lookupOriginalDeclaredField(Class<?> declaringClass, S
10151018
return originalMetaAccess.lookupJavaField(ReflectionUtil.lookupField(declaringClass, fieldName));
10161019
}
10171020

1018-
private void initializeSharedLayerExcludedFields() {
1019-
sharedLayerExcludedFields = new HashSet<>();
1021+
private Set<ResolvedJavaField> initializeSharedLayerExcludedFields() {
1022+
Set<ResolvedJavaField> excludedFields = new HashSet<>();
1023+
10201024
/*
10211025
* These fields need to be folded as they are used in snippets, and they must be accessed
10221026
* without producing reads with side effects.
10231027
*/
1024-
1025-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "layoutEncoding"));
1026-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "numClassTypes"));
1027-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "numIterableInterfaceTypes"));
1028-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldTypeCheckSlots"));
1029-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldInterfaceHashParam"));
1030-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldInterfaceHashTable"));
1031-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "interfaceID"));
1032-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "typeIDDepth"));
1033-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "typeID"));
1034-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "monitorOffset"));
1035-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "hubType"));
1036-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "companion"));
1037-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHubCompanion.class, "arrayHub"));
1038-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHubCompanion.class, "additionalFlags"));
1028+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "layoutEncoding"));
1029+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "numClassTypes"));
1030+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "numIterableInterfaceTypes"));
1031+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldTypeCheckSlots"));
1032+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldInterfaceHashParam"));
1033+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "openTypeWorldInterfaceHashTable"));
1034+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "interfaceID"));
1035+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "typeIDDepth"));
1036+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "typeID"));
1037+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "monitorOffset"));
1038+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "hubType"));
1039+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "companion"));
1040+
excludedFields.add(lookupOriginalDeclaredField(DynamicHubCompanion.class, "arrayHub"));
1041+
excludedFields.add(lookupOriginalDeclaredField(DynamicHubCompanion.class, "additionalFlags"));
10391042

10401043
/* Needs to be immutable for correct lowering of SubstrateIdentityHashCodeNode. */
1041-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "identityHashOffset"));
1044+
excludedFields.add(lookupOriginalDeclaredField(DynamicHub.class, "identityHashOffset"));
10421045

10431046
/*
10441047
* Including this field makes ThreadLocalAllocation.getTlabDescriptorSize reachable through
10451048
* ThreadLocalAllocation.regularTLAB which is accessed with
10461049
* FastThreadLocalBytes.getSizeSupplier
10471050
*/
1048-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(VMThreadLocalInfo.class, "sizeSupplier"));
1051+
excludedFields.add(lookupOriginalDeclaredField(VMThreadLocalInfo.class, "sizeSupplier"));
10491052
/* This field cannot be written to (see documentation) */
1050-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(Counter.Group.class, "enabled"));
1053+
excludedFields.add(lookupOriginalDeclaredField(Counter.Group.class, "enabled"));
10511054
/* This field can contain a reference to a Thread, which is not allowed in the heap */
1052-
sharedLayerExcludedFields.add(lookupOriginalDeclaredField(NativeLibraries.class, "nativeLibraryLockMap"));
1055+
excludedFields.add(lookupOriginalDeclaredField(NativeLibraries.class, "nativeLibraryLockMap"));
1056+
1057+
return excludedFields;
1058+
}
1059+
1060+
protected Set<Module> initializeSharedLayerForbiddenModules() {
1061+
Set<Module> forbiddenModules = new HashSet<>();
1062+
forbiddenModules.add(JVMCI.class.getModule());
1063+
addForbiddenModule(forbiddenModules, "com.oracle.svm.shadowed.org.bytedeco.llvm.global.LLVM");
1064+
addForbiddenModule(forbiddenModules, "com.oracle.svm.shadowed.org.bytedeco.javacpp.presets.javacpp");
1065+
addForbiddenModule(forbiddenModules, "com.oracle.truffle.polyglot.JDKSupport");
1066+
addForbiddenModule(forbiddenModules, "com.oracle.truffle.runtime.hotspot.libgraal.LibGraal");
1067+
return forbiddenModules;
1068+
}
1069+
1070+
protected static void addForbiddenModule(Set<Module> sharedLayerForbiddenModules, String className) {
1071+
Class<?> clazz = ReflectionUtil.lookupClass(true, className);
1072+
if (clazz != null) {
1073+
sharedLayerForbiddenModules.add(clazz.getModule());
1074+
}
10531075
}
10541076

10551077
/** If it's not one of the known builder types it must be an original VM type. */
@@ -1549,26 +1571,7 @@ public ConstantExpressionRegistry getConstantExpressionRegistry() {
15491571
}
15501572

15511573
@Override
1552-
public Set<Module> getForbiddenModules() {
1553-
if (forbiddenModules.isEmpty()) {
1554-
forbiddenModules.add(JVMCI.class.getModule());
1555-
Class<?> llvm = ReflectionUtil.lookupClass(true, "com.oracle.svm.shadowed.org.bytedeco.llvm.global.LLVM");
1556-
if (llvm != null) {
1557-
forbiddenModules.add(llvm.getModule());
1558-
}
1559-
Class<?> javacpp = ReflectionUtil.lookupClass(true, "com.oracle.svm.shadowed.org.bytedeco.javacpp.presets.javacpp");
1560-
if (javacpp != null) {
1561-
forbiddenModules.add(javacpp.getModule());
1562-
}
1563-
Class<?> truffle = ReflectionUtil.lookupClass(true, "com.oracle.truffle.polyglot.JDKSupport");
1564-
if (truffle != null) {
1565-
forbiddenModules.add(truffle.getModule());
1566-
}
1567-
Class<?> libGraal = ReflectionUtil.lookupClass(true, "com.oracle.truffle.runtime.hotspot.libgraal.LibGraal");
1568-
if (libGraal != null) {
1569-
forbiddenModules.add(libGraal.getModule());
1570-
}
1571-
}
1572-
return forbiddenModules;
1574+
public Set<Module> getSharedLayerForbiddenModules() {
1575+
return sharedLayerForbiddenModules;
15731576
}
15741577
}

0 commit comments

Comments
 (0)