Skip to content

Commit 2090706

Browse files
committed
Ensure jdk.module.upgrade.path is accounted for in ModuleLayerFeature and NativeImageClassLoaderSupport ModuleLayer creation
1 parent cfcebde commit 2090706

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.svm.core.jdk.JDK11OrLater;
6363
import com.oracle.svm.core.jdk11.BootModuleLayerSupport;
6464
import com.oracle.svm.core.util.VMError;
65+
import com.oracle.svm.hosted.jdk.NativeImageClassLoaderSupportJDK11OrLater;
6566
import com.oracle.svm.util.ModuleSupport;
6667
import com.oracle.svm.util.ReflectionUtil;
6768

@@ -209,7 +210,10 @@ private static Stream<String> extractRequiredModuleNames(Module m) {
209210
}
210211

211212
private ModuleLayer synthesizeRuntimeBootLayer(ImageClassLoader cl, Set<String> reachableModules, Set<Module> syntheticModules) {
212-
Configuration cf = synthesizeRuntimeBootLayerConfiguration(cl.modulepath(), reachableModules);
213+
NativeImageClassLoaderSupportJDK11OrLater classLoaderSupport = (NativeImageClassLoaderSupportJDK11OrLater) cl.classLoaderSupport;
214+
ModuleFinder beforeFinder = classLoaderSupport.modulepathModuleFinder;
215+
ModuleFinder afterFinder = classLoaderSupport.upgradeAndSystemModuleFinder;
216+
Configuration cf = synthesizeRuntimeBootLayerConfiguration(beforeFinder, afterFinder, reachableModules);
213217
try {
214218
ModuleLayer runtimeBootLayer = moduleLayerConstructor.newInstance(cf, List.of(), null);
215219
Map<String, Module> nameToModule = moduleLayerFeatureUtils.synthesizeNameToModule(runtimeBootLayer, cl.getClassLoader());
@@ -307,10 +311,7 @@ private static List<Module> findApplicationModules(ModuleLayer runtimeBootLayer,
307311
return applicationModules;
308312
}
309313

310-
private static Configuration synthesizeRuntimeBootLayerConfiguration(List<Path> mp, Set<String> reachableModules) {
311-
ModuleFinder beforeFinder = ModuleFinder.of(mp.toArray(Path[]::new));
312-
ModuleFinder afterFinder = ModuleFinder.ofSystem();
313-
314+
private static Configuration synthesizeRuntimeBootLayerConfiguration(ModuleFinder beforeFinder, ModuleFinder afterFinder, Set<String> reachableModules) {
314315
try {
315316
ModuleFinder composed = ModuleFinder.compose(beforeFinder, afterFinder);
316317
List<String> missingModules = new ArrayList<>();

substratevm/src/com.oracle.svm.hosted.jdk11/src/com/oracle/svm/hosted/jdk/NativeImageClassLoaderSupportJDK11OrLater.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,71 @@ public class NativeImageClassLoaderSupportJDK11OrLater extends AbstractNativeIma
7070
private final List<Path> buildmp;
7171

7272
private final ClassLoader classLoader;
73+
74+
public final ModuleFinder upgradeAndSystemModuleFinder;
7375
public final ModuleLayer moduleLayerForImageBuild;
7476

77+
public final ModuleFinder modulepathModuleFinder;
78+
7579
public NativeImageClassLoaderSupportJDK11OrLater(ClassLoader defaultSystemClassLoader, String[] classpath, String[] modulePath) {
7680
super(defaultSystemClassLoader, classpath);
7781

7882
imagemp = Arrays.stream(modulePath).map(Paths::get).collect(Collectors.toUnmodifiableList());
7983
buildmp = Optional.ofNullable(System.getProperty("jdk.module.path")).stream()
8084
.flatMap(s -> Arrays.stream(s.split(File.pathSeparator))).map(Paths::get).collect(Collectors.toUnmodifiableList());
8185

86+
upgradeAndSystemModuleFinder = createUpgradeAndSystemModuleFinder();
8287
ModuleLayer moduleLayer = createModuleLayer(imagemp.toArray(Path[]::new), classPathClassLoader);
8388
adjustBootLayerQualifiedExports(moduleLayer);
8489
moduleLayerForImageBuild = moduleLayer;
90+
8591
classLoader = getSingleClassloader(moduleLayer);
92+
93+
modulepathModuleFinder = ModuleFinder.of(modulepath().toArray(Path[]::new));
8694
}
8795

88-
private static ModuleLayer createModuleLayer(Path[] modulePaths, ClassLoader parent) {
96+
private ModuleLayer createModuleLayer(Path[] modulePaths, ClassLoader parent) {
8997
ModuleFinder modulePathsFinder = ModuleFinder.of(modulePaths);
9098
Set<String> moduleNames = modulePathsFinder.findAll().stream().map(moduleReference -> moduleReference.descriptor().name()).collect(Collectors.toSet());
91-
Configuration configuration = ModuleLayer.boot().configuration().resolve(modulePathsFinder, ModuleFinder.ofSystem(), moduleNames);
99+
100+
Configuration configuration = ModuleLayer.boot().configuration().resolve(modulePathsFinder, upgradeAndSystemModuleFinder, moduleNames);
92101
/**
93102
* For the modules we want to build an image for, a ModuleLayer is needed that can be
94103
* accessed with a single classloader so we can use it for {@link ImageClassLoader}.
95104
*/
96105
return ModuleLayer.defineModulesWithOneLoader(configuration, List.of(ModuleLayer.boot()), parent).layer();
97106
}
98107

108+
/**
109+
* Gets a finder that locates the upgrade modules and the system modules, in that order.
110+
*/
111+
private static ModuleFinder createUpgradeAndSystemModuleFinder() {
112+
ModuleFinder finder = ModuleFinder.ofSystem();
113+
ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
114+
if (upgradeModulePath != null) {
115+
finder = ModuleFinder.compose(upgradeModulePath, finder);
116+
}
117+
return finder;
118+
}
119+
120+
/**
121+
* Creates a finder from a module path specified by the {@code prop} system property.
122+
*/
123+
private static ModuleFinder finderFor(String prop) {
124+
String s = System.getProperty(prop);
125+
if (s == null || s.isEmpty()) {
126+
return null;
127+
} else {
128+
String[] dirs = s.split(File.pathSeparator);
129+
Path[] paths = new Path[dirs.length];
130+
int i = 0;
131+
for (String dir : dirs) {
132+
paths[i++] = Path.of(dir);
133+
}
134+
return ModuleFinder.of(paths);
135+
}
136+
}
137+
99138
private static void adjustBootLayerQualifiedExports(ModuleLayer layer) {
100139
/*
101140
* For all qualified exports packages of modules in the the boot layer we check if layer
@@ -349,41 +388,6 @@ public ClassLoader getClassLoader() {
349388
return classLoader;
350389
}
351390

352-
private static ModuleFinder upgradeAndSystemModuleFinder;
353-
354-
/**
355-
* Creates a finder from a module path specified by the {@code prop} system property.
356-
*/
357-
private static ModuleFinder finderFor(String prop) {
358-
String s = System.getProperty(prop);
359-
if (s == null || s.isEmpty()) {
360-
return null;
361-
} else {
362-
String[] dirs = s.split(File.pathSeparator);
363-
Path[] paths = new Path[dirs.length];
364-
int i = 0;
365-
for (String dir : dirs) {
366-
paths[i++] = Path.of(dir);
367-
}
368-
return ModuleFinder.of(paths);
369-
}
370-
}
371-
372-
/**
373-
* Gets a finder that locates the upgrade modules and the system modules, in that order.
374-
*/
375-
private static ModuleFinder getUpgradeAndSystemModuleFinder() {
376-
if (upgradeAndSystemModuleFinder == null) {
377-
ModuleFinder finder = ModuleFinder.ofSystem();
378-
ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
379-
if (upgradeModulePath != null) {
380-
finder = ModuleFinder.compose(upgradeModulePath, finder);
381-
}
382-
upgradeAndSystemModuleFinder = finder;
383-
}
384-
return upgradeAndSystemModuleFinder;
385-
}
386-
387391
private class ClassInitWithModules extends ClassInit {
388392

389393
ClassInitWithModules(ForkJoinPool executor, ImageClassLoader imageClassLoader) {
@@ -396,12 +400,12 @@ protected void init() {
396400
"jdk.internal.vm.ci", "jdk.internal.vm.compiler", "com.oracle.graal.graal_enterprise",
397401
"org.graalvm.sdk", "org.graalvm.truffle");
398402

399-
for (ModuleReference moduleReference : getUpgradeAndSystemModuleFinder().findAll()) {
403+
for (ModuleReference moduleReference : upgradeAndSystemModuleFinder.findAll()) {
400404
if (requiresInit.contains(moduleReference.descriptor().name())) {
401405
initModule(moduleReference);
402406
}
403407
}
404-
for (ModuleReference moduleReference : ModuleFinder.of(modulepath().toArray(Path[]::new)).findAll()) {
408+
for (ModuleReference moduleReference : modulepathModuleFinder.findAll()) {
405409
initModule(moduleReference);
406410
}
407411

0 commit comments

Comments
 (0)