Skip to content

Commit bcb1bda

Browse files
committed
8345259: Disallow ALL-MODULE-PATH without explicit --module-path
Reviewed-by: mchung
1 parent 054c644 commit bcb1bda

File tree

6 files changed

+214
-107
lines changed

6 files changed

+214
-107
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java

+35-14
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,6 @@ int run(String[] args) {
277277
return EXIT_OK;
278278
}
279279

280-
if (options.modulePath.isEmpty()) {
281-
// no --module-path specified - try to set $JAVA_HOME/jmods if that exists
282-
Path jmods = getDefaultModulePath();
283-
if (jmods != null) {
284-
options.modulePath.add(jmods);
285-
}
286-
}
287-
288280
JlinkConfiguration config = initJlinkConfig();
289281
outputPath = config.getOutput();
290282
if (options.suggestProviders) {
@@ -377,8 +369,13 @@ public static void createImage(JlinkConfiguration config,
377369
// the token for "all modules on the module path"
378370
private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
379371
private JlinkConfiguration initJlinkConfig() throws BadArgs {
372+
// Empty module path not allowed with ALL-MODULE-PATH in --add-modules
373+
if (options.addMods.contains(ALL_MODULE_PATH) && options.modulePath.isEmpty()) {
374+
throw taskHelper.newBadArgs("err.no.module.path");
375+
}
380376
ModuleFinder appModuleFinder = newModuleFinder(options.modulePath);
381377
ModuleFinder finder = appModuleFinder;
378+
382379
boolean isLinkFromRuntime = false;
383380
if (!appModuleFinder.find("java.base").isPresent()) {
384381
// If the application module finder doesn't contain the
@@ -393,8 +390,9 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
393390
// include the java.base module.
394391
Path defModPath = getDefaultModulePath();
395392
if (defModPath != null) {
396-
options.modulePath.add(defModPath);
397-
finder = newModuleFinder(options.modulePath);
393+
List<Path> combinedPaths = new ArrayList<>(options.modulePath);
394+
combinedPaths.add(defModPath);
395+
finder = newModuleFinder(combinedPaths);
398396
}
399397
// We've just added the default module path ('jmods'). If we still
400398
// don't find java.base, we must resolve JDK modules from the
@@ -419,8 +417,31 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
419417
Set<String> roots = new HashSet<>();
420418
for (String mod : options.addMods) {
421419
if (mod.equals(ALL_MODULE_PATH)) {
422-
ModuleFinder mf = newLimitedFinder(finder, options.limitMods,
423-
Set.of());
420+
// Using --limit-modules with ALL-MODULE-PATH is an error
421+
if (!options.limitMods.isEmpty()) {
422+
throw taskHelper.newBadArgs("err.limit.modules");
423+
}
424+
// all observable modules in the app module path are roots
425+
Set<String> initialRoots = appModuleFinder.findAll()
426+
.stream()
427+
.map(ModuleReference::descriptor)
428+
.map(ModuleDescriptor::name)
429+
.collect(Collectors.toSet());
430+
431+
// Error if no module is found on the app module path
432+
if (initialRoots.isEmpty()) {
433+
String modPath = options.modulePath.stream()
434+
.map(a -> a.toString())
435+
.collect(Collectors.joining(", "));
436+
throw taskHelper.newBadArgs("err.empty.module.path", modPath);
437+
}
438+
439+
// Use a module finder with limited observability, as determined
440+
// by initialRoots, to find the observable modules from the
441+
// application module path (--module-path option) only. We must
442+
// not include JDK modules from the default module path or the
443+
// run-time image.
444+
ModuleFinder mf = limitFinder(finder, initialRoots, Set.of());
424445
mf.findAll()
425446
.stream()
426447
.map(ModuleReference::descriptor)
@@ -430,7 +451,7 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
430451
roots.add(mod);
431452
}
432453
}
433-
finder = newLimitedFinder(finder, options.limitMods, roots);
454+
finder = limitFinder(finder, options.limitMods, roots);
434455

435456
// --keep-packaged-modules doesn't make sense as we are not linking
436457
// from packaged modules to begin with.
@@ -497,7 +518,7 @@ public static Path getDefaultModulePath() {
497518
* specified in {@code limitMods} plus other modules specified in the
498519
* {@code roots} set.
499520
*/
500-
public static ModuleFinder newLimitedFinder(ModuleFinder finder,
521+
public static ModuleFinder limitFinder(ModuleFinder finder,
501522
Set<String> limitMods,
502523
Set<String> roots) {
503524
// if limitMods is specified then limit the universe

src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ err.runtime.link.packaged.mods=This JDK has no packaged modules.\
127127
err.runtime.link.modified.file={0} has been modified
128128
err.runtime.link.patched.module=jlink does not support linking from the run-time image\
129129
\ when running on a patched runtime with --patch-module
130-
err.empty.module.path=empty module path
130+
err.no.module.path=--module-path option must be specified with --add-modules ALL-MODULE-PATH
131+
err.empty.module.path=No module found in module path ''{0}'' with --add-modules ALL-MODULE-PATH
132+
err.limit.modules=--limit-modules not allowed with --add-modules ALL-MODULE-PATH
131133
err.jlink.version.mismatch=jlink version {0}.{1} does not match target java.base version {2}.{3}
132134
err.automatic.module:automatic module cannot be used with jlink: {0} from {1}
133135
err.unknown.byte.order:unknown byte order {0}

test/jdk/tools/jlink/IntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static void test() throws Exception {
157157
boolean linkFromRuntime = false;
158158
JlinkConfiguration config = new Jlink.JlinkConfiguration(output,
159159
mods,
160-
JlinkTask.newLimitedFinder(JlinkTask.newModuleFinder(modulePaths), limits, mods),
160+
JlinkTask.limitFinder(JlinkTask.newModuleFinder(modulePaths), limits, mods),
161161
linkFromRuntime,
162162
false /* ignore modified runtime */,
163163
false /* generate run-time image */);

test/jdk/tools/jlink/JLinkTest.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727
import java.lang.module.ModuleDescriptor;
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
30-
import java.nio.file.Paths;
3130
import java.util.ArrayList;
32-
import java.util.Collections;
3331
import java.util.List;
3432
import java.util.spi.ToolProvider;
3533
import java.util.stream.Collectors;
3634
import java.util.stream.IntStream;
3735
import java.util.stream.Stream;
3836

39-
import jdk.tools.jlink.plugin.Plugin;
4037
import jdk.tools.jlink.internal.PluginRepository;
38+
import jdk.tools.jlink.plugin.Plugin;
4139
import tests.Helper;
4240
import tests.JImageGenerator;
4341

@@ -135,11 +133,11 @@ public static void main(String[] args) throws Exception {
135133

136134
{
137135
// No --module-path specified. --add-modules ALL-MODULE-PATH specified.
138-
String imageDir = "bug8189777-all-module-path";
136+
String imageDir = "bug8345259-all-module-path";
139137
JImageGenerator.getJLinkTask()
140138
.output(helper.createNewImageDir(imageDir))
141139
.addMods("ALL-MODULE-PATH")
142-
.call().assertSuccess();
140+
.call().assertFailure();
143141
}
144142

145143
{

0 commit comments

Comments
 (0)