From 2d6d1f40ee1580b87b73a30974f7844aa8e9e4d4 Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Mon, 11 Nov 2024 19:38:52 +0100 Subject: [PATCH] 8343839: Detect patched modules and abort run-time image link early --- src/java.base/share/classes/module-info.java | 1 + .../jdk/tools/jlink/internal/ImageFileCreator.java | 1 - .../jdk/tools/jlink/internal/JRTArchive.java | 13 +------------ .../classes/jdk/tools/jlink/internal/JlinkTask.java | 6 ++++++ .../runtimelink/RuntimeImageLinkException.java | 1 - .../jdk/tools/jlink/resources/jlink.properties | 2 +- .../runtimeImage/PatchedJDKModuleJlinkTest.java | 2 +- 7 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/java.base/share/classes/module-info.java b/src/java.base/share/classes/module-info.java index 4e43245e520e2..8b3d29486589c 100644 --- a/src/java.base/share/classes/module-info.java +++ b/src/java.base/share/classes/module-info.java @@ -222,6 +222,7 @@ jdk.crypto.cryptoki, jdk.incubator.vector, jdk.jfr, + jdk.jlink, jdk.jshell, jdk.nio.mapmode, jdk.unsupported, diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageFileCreator.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageFileCreator.java index 466c5d0a14cfe..d05df65d98832 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageFileCreator.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImageFileCreator.java @@ -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()); }; diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JRTArchive.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JRTArchive.java index 755afea8c60b3..aa18996e20311 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JRTArchive.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JRTArchive.java @@ -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; @@ -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; @@ -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); diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java index 15998d6b92959..c1629bd854bdb 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java @@ -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) { diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/RuntimeImageLinkException.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/RuntimeImageLinkException.java index 9f54fd63476ff..7be652eed45fb 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/RuntimeImageLinkException.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/RuntimeImageLinkException.java @@ -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 */ } diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties index 9e18177d9c84f..a79fd4e6e1d3f 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties @@ -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} diff --git a/test/jdk/tools/jlink/runtimeImage/PatchedJDKModuleJlinkTest.java b/test/jdk/tools/jlink/runtimeImage/PatchedJDKModuleJlinkTest.java index 3baa824e04990..343bdd29cb043 100644 --- a/test/jdk/tools/jlink/runtimeImage/PatchedJDKModuleJlinkTest.java +++ b/test/jdk/tools/jlink/runtimeImage/PatchedJDKModuleJlinkTest.java @@ -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");