Skip to content

Commit dd8dcad

Browse files
committed
[GR-53420] Replicate hosted module graph relations for unnamed modules
PullRequest: graal/17658
2 parents 97bf193 + 6a065ce commit dd8dcad

File tree

2 files changed

+64
-64
lines changed

2 files changed

+64
-64
lines changed

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

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,15 @@ private void scanRuntimeBootLayerPrototype(BeforeAnalysisAccessImpl accessImpl)
201201
public void afterAnalysis(AfterAnalysisAccess access) {
202202
AfterAnalysisAccessImpl accessImpl = (AfterAnalysisAccessImpl) access;
203203

204-
Set<Module> runtimeImageNamedModules = accessImpl.getUniverse().getTypes()
204+
Set<Module> runtimeImageModules = accessImpl.getUniverse().getTypes()
205205
.stream()
206206
.filter(ModuleLayerFeature::typeIsReachable)
207207
.map(t -> t.getJavaClass().getModule())
208-
.filter(Module::isNamed)
209208
.collect(Collectors.toSet());
210209

210+
Set<Module> runtimeImageNamedModules = runtimeImageModules.stream().filter(Module::isNamed).collect(Collectors.toSet());
211+
Set<Module> runtimeImageUnnamedModules = runtimeImageModules.stream().filter(Predicate.not(Module::isNamed)).collect(Collectors.toSet());
212+
211213
/*
212214
* Parse explicitly added modules via --add-modules. This is done early as this information
213215
* is required when filtering the analysis reachable module set.
@@ -257,7 +259,7 @@ public void afterAnalysis(AfterAnalysisAccess access) {
257259
* Ensure that runtime modules have the same relations (i.e., reads, opens and exports) as
258260
* the originals.
259261
*/
260-
replicateVisibilityModifications(runtimeBootLayer, accessImpl, accessImpl.imageClassLoader, runtimeImageNamedModules);
262+
replicateVisibilityModifications(runtimeBootLayer, accessImpl, accessImpl.imageClassLoader, runtimeImageNamedModules, runtimeImageUnnamedModules);
261263
replicateNativeAccess(accessImpl, runtimeImageNamedModules);
262264
}
263265

@@ -461,61 +463,70 @@ private ModuleLayer synthesizeRuntimeModuleLayer(List<ModuleLayer> parentLayers,
461463
}
462464
}
463465

464-
private void replicateVisibilityModifications(ModuleLayer runtimeBootLayer, AfterAnalysisAccessImpl accessImpl, ImageClassLoader cl, Set<Module> analysisReachableNamedModules) {
466+
private void replicateVisibilityModifications(ModuleLayer runtimeBootLayer, AfterAnalysisAccessImpl accessImpl, ImageClassLoader cl, Set<Module> analysisReachableNamedModules,
467+
Set<Module> analysisReachableUnnamedModules) {
465468
List<Module> applicationModules = findApplicationModules(runtimeBootLayer, cl.applicationModulePath());
466469

467-
Map<Module, Module> modulePairs = analysisReachableNamedModules
470+
Map<Module, Module> namedModulePairs = analysisReachableNamedModules
468471
.stream()
469-
.collect(Collectors.toMap(m -> m, m -> moduleLayerFeatureUtils.getRuntimeModuleForHostedModule(m, false)));
470-
modulePairs.put(moduleLayerFeatureUtils.allUnnamedModule, moduleLayerFeatureUtils.allUnnamedModule);
471-
modulePairs.put(moduleLayerFeatureUtils.everyoneModule, moduleLayerFeatureUtils.everyoneModule);
472+
.collect(Collectors.toMap(Function.identity(), m -> moduleLayerFeatureUtils.getRuntimeModuleForHostedModule(m, false)));
473+
Map<Module, Module> unnamedModulePairs = analysisReachableUnnamedModules
474+
.stream()
475+
.collect(Collectors.toMap(Function.identity(), m -> moduleLayerFeatureUtils.getRuntimeModuleForHostedModule(m, false)));
476+
unnamedModulePairs.put(moduleLayerFeatureUtils.allUnnamedModule, moduleLayerFeatureUtils.allUnnamedModule);
477+
unnamedModulePairs.put(moduleLayerFeatureUtils.everyoneModule, moduleLayerFeatureUtils.everyoneModule);
478+
479+
try {
480+
for (Map.Entry<Module, Module> e1 : namedModulePairs.entrySet()) {
481+
Module hostedFrom = e1.getKey();
482+
Module runtimeFrom = e1.getValue();
483+
for (Map.Entry<Module, Module> e2 : namedModulePairs.entrySet()) {
484+
replicateVisibilityModification(accessImpl, applicationModules, hostedFrom, e2.getKey(), runtimeFrom, e2.getValue());
485+
}
486+
for (Map.Entry<Module, Module> e2 : unnamedModulePairs.entrySet()) {
487+
replicateVisibilityModification(accessImpl, applicationModules, hostedFrom, e2.getKey(), runtimeFrom, e2.getValue());
488+
}
489+
}
490+
} catch (IllegalAccessException ex) {
491+
throw VMError.shouldNotReachHere("Failed to transfer hosted module relations to the runtime boot module layer.", ex);
492+
}
493+
}
494+
495+
private void replicateVisibilityModification(AfterAnalysisAccessImpl accessImpl, List<Module> applicationModules, Module hostedFrom, Module hostedTo, Module runtimeFrom, Module runtimeTo)
496+
throws IllegalAccessException {
497+
if (hostedTo == hostedFrom) {
498+
return;
499+
}
472500

473501
Module builderModule = ModuleLayerFeatureUtils.getBuilderModule();
474502
assert builderModule != null;
475503

476-
try {
477-
for (Map.Entry<Module, Module> e1 : modulePairs.entrySet()) {
478-
Module hostedFrom = e1.getKey();
479-
if (!hostedFrom.isNamed()) {
480-
continue;
504+
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.canRead(hostedTo)) {
505+
moduleLayerFeatureUtils.addReads(accessImpl, runtimeFrom, runtimeTo);
506+
if (hostedFrom == builderModule) {
507+
for (Module appModule : applicationModules) {
508+
moduleLayerFeatureUtils.addReads(accessImpl, appModule, runtimeTo);
481509
}
482-
Module runtimeFrom = e1.getValue();
483-
for (Map.Entry<Module, Module> e2 : modulePairs.entrySet()) {
484-
Module hostedTo = e2.getKey();
485-
if (hostedTo == hostedFrom) {
486-
continue;
487-
}
488-
Module runtimeTo = e2.getValue();
489-
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.canRead(hostedTo)) {
490-
moduleLayerFeatureUtils.addReads(accessImpl, runtimeFrom, runtimeTo);
491-
if (hostedFrom == builderModule) {
492-
for (Module appModule : applicationModules) {
493-
moduleLayerFeatureUtils.addReads(accessImpl, appModule, runtimeTo);
494-
}
495-
}
510+
}
511+
}
512+
513+
for (String pn : runtimeFrom.getPackages()) {
514+
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.isOpen(pn, hostedTo)) {
515+
moduleLayerFeatureUtils.addOpens(accessImpl, runtimeFrom, pn, runtimeTo);
516+
if (hostedTo == builderModule) {
517+
for (Module appModule : applicationModules) {
518+
moduleLayerFeatureUtils.addOpens(accessImpl, runtimeFrom, pn, appModule);
496519
}
497-
for (String pn : runtimeFrom.getPackages()) {
498-
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.isOpen(pn, hostedTo)) {
499-
moduleLayerFeatureUtils.addOpens(accessImpl, runtimeFrom, pn, runtimeTo);
500-
if (hostedTo == builderModule) {
501-
for (Module appModule : applicationModules) {
502-
moduleLayerFeatureUtils.addOpens(accessImpl, runtimeFrom, pn, appModule);
503-
}
504-
}
505-
}
506-
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.isExported(pn, hostedTo)) {
507-
moduleLayerFeatureUtils.addExports(accessImpl, runtimeFrom, pn, runtimeTo);
508-
if (hostedTo == builderModule) {
509-
for (Module appModule : applicationModules) {
510-
moduleLayerFeatureUtils.addExports(accessImpl, runtimeFrom, pn, appModule);
511-
}
512-
}
513-
}
520+
}
521+
}
522+
if (ModuleLayerFeatureUtils.isModuleSynthetic(hostedFrom) || hostedFrom.isExported(pn, hostedTo)) {
523+
moduleLayerFeatureUtils.addExports(accessImpl, runtimeFrom, pn, runtimeTo);
524+
if (hostedTo == builderModule) {
525+
for (Module appModule : applicationModules) {
526+
moduleLayerFeatureUtils.addExports(accessImpl, runtimeFrom, pn, appModule);
514527
}
515528
}
516529
}
517-
} catch (IllegalAccessException ex) {
518-
throw VMError.shouldNotReachHere("Failed to transfer hosted module relations to the runtime boot module layer.", ex);
519530
}
520531
}
521532

@@ -761,24 +772,8 @@ public ModuleFinder getAppModuleFinder() {
761772
public Module getRuntimeModuleForHostedModule(Module hostedModule, boolean optional) {
762773
if (hostedModule.isNamed()) {
763774
return getRuntimeModuleForHostedModule(hostedModule.getClassLoader(), hostedModule.getName(), optional);
764-
}
765-
766-
/*
767-
* EVERYONE and ALL_UNNAMED modules are unnamed module instances that are used as
768-
* markers throughout the JDK and therefore we need them in the image heap.
769-
*
770-
* We make an optimization that all hosted unnamed modules except EVERYONE module have
771-
* the same runtime unnamed module. This does not break the module visibility semantics
772-
* as unnamed modules can access all named modules, and visibility modifications that
773-
* include unnamed modules do not depend on the actual instance, but only on the fact
774-
* that the module is unnamed e.g., calling addExports from/to an unnamed module will do
775-
* nothing.
776-
*/
777-
778-
if (hostedModule == everyoneModule) {
779-
return everyoneModule;
780775
} else {
781-
return allUnnamedModule;
776+
return hostedModule;
782777
}
783778
}
784779

@@ -808,7 +803,7 @@ public Module getOrCreateRuntimeModuleForHostedModule(Module hostedModule) {
808803
if (hostedModule.isNamed()) {
809804
return getOrCreateRuntimeModuleForHostedModule(hostedModule.getClassLoader(), hostedModule.getName(), hostedModule.getDescriptor());
810805
} else {
811-
return hostedModule == everyoneModule ? everyoneModule : allUnnamedModule;
806+
return hostedModule;
812807
}
813808
}
814809

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,9 @@ public static void accessModuleByClass(Access access, Class<?> accessingClass, M
141141
}
142142
access.giveAccess(namedAccessingModule, declaringModule, packageName);
143143
}
144+
145+
@Platforms(Platform.HOSTED_ONLY.class)
146+
public static void accessModule(Access access, Module accessingModule, Module declaringModule, String packageName) {
147+
access.giveAccess(accessingModule, declaringModule, packageName);
148+
}
144149
}

0 commit comments

Comments
 (0)