Skip to content

Commit

Permalink
8343839: Detect patched modules and abort run-time image link early
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Nov 11, 2024
1 parent 2ec3580 commit 2d6d1f4
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/java.base/share/classes/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
jdk.crypto.cryptoki,
jdk.incubator.vector,
jdk.jfr,
jdk.jlink,
jdk.jshell,
jdk.nio.mapmode,
jdk.unsupported,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ private IOException newIOException(RuntimeImageLinkException e) throws IOExcepti
e.printStackTrace();
}
String message = switch (e.getReason()) {
case PATCH_MODULE -> helper.getMessage("err.runtime.link.patched.module", e.getFile());
case MODIFIED_FILE -> helper.getMessage("err.runtime.link.modified.file", e.getFile());
default -> throw new AssertionError("Unexpected value: " + e.getReason());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import static jdk.tools.jlink.internal.LinkableRuntimeImage.RESPATH_PATTERN;
import static jdk.tools.jlink.internal.runtimelink.RuntimeImageLinkException.Reason.MODIFIED_FILE;
import static jdk.tools.jlink.internal.runtimelink.RuntimeImageLinkException.Reason.PATCH_MODULE;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -37,7 +36,6 @@
import java.lang.module.ModuleReference;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
Expand Down Expand Up @@ -460,16 +458,7 @@ public long size() {
// the underlying base path is a JrtPath with the
// JrtFileSystem underneath which is able to handle
// this size query.
try {
return Files.size(archive.getPath().resolve(resPath));
} catch (NoSuchFileException file) {
// This indicates that we don't find the class in the
// modules image using the JRT FS provider. Yet, we find
// the class using the system module finder. Therefore,
// we have a patched module. Mention that module patching
// is not supported.
throw new RuntimeImageLinkException(file.getFile(), PATCH_MODULE);
}
return Files.size(archive.getPath().resolve(resPath));
}
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ private static ImageHelper createImageProvider(JlinkConfiguration config,
String msg = taskHelper.getMessage("err.runtime.link.jdk.jlink.prohibited");
throw new IllegalArgumentException(msg);
}
// Do not permit linking from run-time image when the current image
// is being patched
if (jdk.internal.misc.VM.getSavedProperties().get("jdk.module.patch.0") != null) {
String msg = taskHelper.getMessage("err.runtime.link.patched.module");
throw new IllegalArgumentException(msg);
}

// Print info message indicating linking from the run-time image
if (verbose && log != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class RuntimeImageLinkException extends RuntimeException {
private static final long serialVersionUID = -1848914673073119403L;

public static enum Reason {
PATCH_MODULE, /* link exception due to patched module */
MODIFIED_FILE, /* link exception due to modified file */
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ err.runtime.link.jdk.jlink.prohibited=This JDK does not contain packaged modules
err.runtime.link.packaged.mods=This JDK has no packaged modules.\
\ --keep-packaged-modules is not supported
err.runtime.link.modified.file={0} has been modified
err.runtime.link.patched.module=File {0} not found in the modules image.\
err.runtime.link.patched.module=The current runtime includes module patches.\
\ --patch-module is not supported when linking from the run-time image
err.empty.module.path=empty module path
err.jlink.version.mismatch=jlink version {0}.{1} does not match target java.base version {2}.{3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean test(OutputAnalyzer t) {
if (analyzer.getExitValue() == 0) {
throw new AssertionError("Expected jlink to fail due to patched module!");
}
analyzer.stdoutShouldContain("MyJlinkPatchInteger.class not found in the modules image.");
analyzer.stdoutShouldContain("The current runtime includes module patches.");
analyzer.stdoutShouldContain("--patch-module is not supported");
// Verify the error message is reasonable
analyzer.stdoutShouldNotContain("jdk.tools.jlink.internal.RunImageLinkException");
Expand Down

0 comments on commit 2d6d1f4

Please sign in to comment.