Skip to content

Commit

Permalink
8345259: Disallow ALL-MODULE-PATH without explicit --module-path
Browse files Browse the repository at this point in the history
Reviewed-by: mchung
  • Loading branch information
jerboaa committed Dec 20, 2024
1 parent 054c644 commit bcb1bda
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 107 deletions.
49 changes: 35 additions & 14 deletions src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,6 @@ int run(String[] args) {
return EXIT_OK;
}

if (options.modulePath.isEmpty()) {
// no --module-path specified - try to set $JAVA_HOME/jmods if that exists
Path jmods = getDefaultModulePath();
if (jmods != null) {
options.modulePath.add(jmods);
}
}

JlinkConfiguration config = initJlinkConfig();
outputPath = config.getOutput();
if (options.suggestProviders) {
Expand Down Expand Up @@ -377,8 +369,13 @@ public static void createImage(JlinkConfiguration config,
// the token for "all modules on the module path"
private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
private JlinkConfiguration initJlinkConfig() throws BadArgs {
// Empty module path not allowed with ALL-MODULE-PATH in --add-modules
if (options.addMods.contains(ALL_MODULE_PATH) && options.modulePath.isEmpty()) {
throw taskHelper.newBadArgs("err.no.module.path");
}
ModuleFinder appModuleFinder = newModuleFinder(options.modulePath);
ModuleFinder finder = appModuleFinder;

boolean isLinkFromRuntime = false;
if (!appModuleFinder.find("java.base").isPresent()) {
// If the application module finder doesn't contain the
Expand All @@ -393,8 +390,9 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
// include the java.base module.
Path defModPath = getDefaultModulePath();
if (defModPath != null) {
options.modulePath.add(defModPath);
finder = newModuleFinder(options.modulePath);
List<Path> combinedPaths = new ArrayList<>(options.modulePath);
combinedPaths.add(defModPath);
finder = newModuleFinder(combinedPaths);
}
// We've just added the default module path ('jmods'). If we still
// don't find java.base, we must resolve JDK modules from the
Expand All @@ -419,8 +417,31 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
Set<String> roots = new HashSet<>();
for (String mod : options.addMods) {
if (mod.equals(ALL_MODULE_PATH)) {
ModuleFinder mf = newLimitedFinder(finder, options.limitMods,
Set.of());
// Using --limit-modules with ALL-MODULE-PATH is an error
if (!options.limitMods.isEmpty()) {
throw taskHelper.newBadArgs("err.limit.modules");
}
// all observable modules in the app module path are roots
Set<String> initialRoots = appModuleFinder.findAll()
.stream()
.map(ModuleReference::descriptor)
.map(ModuleDescriptor::name)
.collect(Collectors.toSet());

// Error if no module is found on the app module path
if (initialRoots.isEmpty()) {
String modPath = options.modulePath.stream()
.map(a -> a.toString())
.collect(Collectors.joining(", "));
throw taskHelper.newBadArgs("err.empty.module.path", modPath);
}

// Use a module finder with limited observability, as determined
// by initialRoots, to find the observable modules from the
// application module path (--module-path option) only. We must
// not include JDK modules from the default module path or the
// run-time image.
ModuleFinder mf = limitFinder(finder, initialRoots, Set.of());
mf.findAll()
.stream()
.map(ModuleReference::descriptor)
Expand All @@ -430,7 +451,7 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
roots.add(mod);
}
}
finder = newLimitedFinder(finder, options.limitMods, roots);
finder = limitFinder(finder, options.limitMods, roots);

// --keep-packaged-modules doesn't make sense as we are not linking
// from packaged modules to begin with.
Expand Down Expand Up @@ -497,7 +518,7 @@ public static Path getDefaultModulePath() {
* specified in {@code limitMods} plus other modules specified in the
* {@code roots} set.
*/
public static ModuleFinder newLimitedFinder(ModuleFinder finder,
public static ModuleFinder limitFinder(ModuleFinder finder,
Set<String> limitMods,
Set<String> roots) {
// if limitMods is specified then limit the universe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ err.runtime.link.packaged.mods=This JDK has no packaged modules.\
err.runtime.link.modified.file={0} has been modified
err.runtime.link.patched.module=jlink does not support linking from the run-time image\
\ when running on a patched runtime with --patch-module
err.empty.module.path=empty module path
err.no.module.path=--module-path option must be specified with --add-modules ALL-MODULE-PATH
err.empty.module.path=No module found in module path ''{0}'' with --add-modules ALL-MODULE-PATH
err.limit.modules=--limit-modules not allowed with --add-modules ALL-MODULE-PATH
err.jlink.version.mismatch=jlink version {0}.{1} does not match target java.base version {2}.{3}
err.automatic.module:automatic module cannot be used with jlink: {0} from {1}
err.unknown.byte.order:unknown byte order {0}
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/tools/jlink/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static void test() throws Exception {
boolean linkFromRuntime = false;
JlinkConfiguration config = new Jlink.JlinkConfiguration(output,
mods,
JlinkTask.newLimitedFinder(JlinkTask.newModuleFinder(modulePaths), limits, mods),
JlinkTask.limitFinder(JlinkTask.newModuleFinder(modulePaths), limits, mods),
linkFromRuntime,
false /* ignore modified runtime */,
false /* generate run-time image */);
Expand Down
8 changes: 3 additions & 5 deletions test/jdk/tools/jlink/JLinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@
import java.lang.module.ModuleDescriptor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import jdk.tools.jlink.plugin.Plugin;
import jdk.tools.jlink.internal.PluginRepository;
import jdk.tools.jlink.plugin.Plugin;
import tests.Helper;
import tests.JImageGenerator;

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

{
// No --module-path specified. --add-modules ALL-MODULE-PATH specified.
String imageDir = "bug8189777-all-module-path";
String imageDir = "bug8345259-all-module-path";
JImageGenerator.getJLinkTask()
.output(helper.createNewImageDir(imageDir))
.addMods("ALL-MODULE-PATH")
.call().assertSuccess();
.call().assertFailure();
}

{
Expand Down
Loading

0 comments on commit bcb1bda

Please sign in to comment.