Fail the load when a module uses a JDK package that is not exported to it

A class such as jdk.internal.misc.Unsafe passed the reference check,
because the platform loader finds it, but java.base does not export
jdk.internal.misc to unnamed modules, so the module failed with an
IllegalAccessError once the code ran. It is now a load error with its
own group:

  Module jar modules/user.jar uses classes of JDK packages that are not exported to modules:
    pack.user.User -> jdk.internal.misc.Unsafe (package jdk.internal.misc of java.base is not exported)
  Use the supported alternative where there is one, such as sun.misc.Unsafe from jdk.unsupported
  instead of jdk.internal.misc.Unsafe. Otherwise the server must be started with
  --add-exports <module>/<package>=ALL-UNNAMED.

NonExportedJdkPackages asks the boot layer's platform and boot loader
modules whether they export each package to an unnamed module that no
export names, so qualified exports do not count and a server started
with --add-exports <module>/<package>=ALL-UNNAMED passes, as it would at
run time. Nothing is loaded. sun.misc (jdk.unsupported) and java.lang
stay usable. The check runs first among classes outside the pack: the
module decides access whichever loader finds the class.

The --add-exports case runs in its own JVM, the addExportsTest task
(tag jdk-add-exports), which check depends on; the other tests run
without the flag. TestModuleJar can pass --add-exports to javac.
This commit is contained in:
piexel committed 2026-10-05 21:10:53 +02:00
1 parent e34acf2fbb
commit f6b08a0303
8 files changed
+212 -10

No files matched your search

@@ -0,0 +1,35 @@
package com.volmit.host;
import com.volmit.host.NonExportedJdkPackages.JdkPackage;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
class NonExportedJdkPackagesTest {
@Test
void namesPackagesTheirModuleDoesNotExport() {
assertEquals(Optional.of(new JdkPackage("jdk.internal.misc", "java.base")),
NonExportedJdkPackages.packageOf("jdk.internal.misc.Unsafe"));
assertEquals(Optional.of(new JdkPackage("sun.nio.ch", "java.base")), NonExportedJdkPackages.packageOf("sun.nio.ch.Net"));
}
@Test
void packagesExportedOnlyToNamedModulesAreNotExported() {
assertEquals(Optional.of(new JdkPackage("jdk.internal.access", "java.base")),
NonExportedJdkPackages.packageOf("jdk.internal.access.SharedSecrets"));
}
@Test
void exportedPackagesAreUsable() {
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("sun.misc.Unsafe"));
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("java.lang.String"));
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("java.sql.Connection"));
}
@Test
void modulesOutsideThePlatformLoaderAndNonJdkClassesAreNotListed() {
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("com.sun.tools.javac.code.Type"));
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("org.slf4j.Logger"));
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("TopLevel"));
}
}
@@ -0,0 +1,40 @@
package com.volmit.host;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import static com.volmit.support.TestModuleJar.module;
import static org.junit.jupiter.api.Assertions.*;
/** Runs only in the {@code addExportsTest} task, whose JVM is started with
* {@code --add-exports java.base/jdk.internal.misc=ALL-UNNAMED}, like a server that grants modules
* that package.
*/
@Tag(PackModulesAddExportsTest.ADD_EXPORTS_TAG)
class PackModulesAddExportsTest {
static final String ADD_EXPORTS_TAG = "jdk-add-exports";
@TempDir
Path pack;
@Test
void packageExportedToAllUnnamedModulesOnTheCommandLineIsUsable() {
module("pack.user.User", "user:core", "static Object unsafe() { return jdk.internal.misc.Unsafe.class; }", "")
.compileWithAccessTo("java.base", "jdk.internal.misc")
.writeInto(pack, "user.jar");
try (var packModules = PackModules.load(pack)) {
assertEquals(List.of(), packModules.missingClasses());
}
}
@Test
void onlyTheExportedPackageBecomesUsable() {
assertEquals(Optional.empty(), NonExportedJdkPackages.packageOf("jdk.internal.misc.Unsafe"),
"this JVM must run with --add-exports java.base/jdk.internal.misc=ALL-UNNAMED (gradle addExportsTest)");
assertTrue(NonExportedJdkPackages.packageOf("sun.nio.ch.Net").isPresent());
}
}
@@ -287,6 +287,30 @@ class PackModulesTest {
failure(() -> PackModules.load(pack)));
}
@Test
void referenceToANonExportedJdkPackageFailsNamingThePackageAndModule() {
module("pack.user.User", "user:core", using("jdk.internal.misc.Unsafe", "java.lang.String"), NO_STAGES)
.compileWithAccessTo("java.base", "jdk.internal.misc")
.writeInto(pack, "user.jar");
assertEquals("Module jar " + origin("user.jar") + " uses classes of JDK packages that are not exported to modules:\n"
+ " pack.user.User -> jdk.internal.misc.Unsafe (package jdk.internal.misc of java.base is not exported)\n"
+ "Use the supported alternative where there is one, such as sun.misc.Unsafe from jdk.unsupported"
+ " instead of jdk.internal.misc.Unsafe. Otherwise the server must be started with"
+ " --add-exports <module>/<package>=ALL-UNNAMED.",
failure(() -> PackModules.load(pack)));
}
@Test
void supportedUnsafeFromJdkUnsupportedIsUsable() {
module("pack.user.User", "user:core", using("sun.misc.Unsafe", "java.lang.String"), NO_STAGES)
.writeInto(pack, "user.jar");
try (var packModules = PackModules.load(pack)) {
assertEquals(List.of(), packModules.missingClasses());
}
}
@Test
void oneFailureReportsEveryKindOfUnusableClass() {
module("pack.user.User", "user:core", using("org.junit.jupiter.api.Test", "com.sun.source.tree.Tree"), NO_STAGES)
@@ -36,6 +36,7 @@ public final class TestModuleJar {
private boolean multiRelease;
private final List<String> entryPoints = new ArrayList<>();
private final List<Path> classpath = new ArrayList<>(List.of(classesOf(IrisModule.class)));
private final List<String> compilerExports = new ArrayList<>();
/** An entry class with the given module id whose install body may use {@code graph}. */
public static TestModuleJar module(String className, String moduleId, String members, String install) {
@@ -92,6 +93,14 @@ public final class TestModuleJar {
return compileAgainst(classesOf(type));
}
/** Lets the sources use a JDK package its module does not export, as javac's
* {@code --add-exports <module>/<package>=ALL-UNNAMED} does. Only compiling is affected.
*/
public TestModuleJar compileWithAccessTo(String module, String packageName) {
compilerExports.addAll(List.of("--add-exports", module + "/" + packageName + "=ALL-UNNAMED"));
return this;
}
/** Writes {@code <packRoot>/modules/<jarName>} and returns its path. */
public Path writeInto(Path packRoot, String jarName) {
try {
@@ -113,8 +122,9 @@ public final class TestModuleJar {
var compiler = ToolProvider.getSystemJavaCompiler();
var diagnostics = new DiagnosticCollector<JavaFileObject>();
var units = sources.entrySet().stream().map(source -> new StringSource(source.getKey(), source.getValue())).toList();
var options = List.of("-d", classes.toString(), "-proc:none", "-classpath",
classpath.stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator)));
var options = new ArrayList<>(List.of("-d", classes.toString(), "-proc:none", "-classpath",
classpath.stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator))));
options.addAll(compilerExports);
if (!compiler.getTask(null, null, diagnostics, options, null, units).call()) {
throw new IllegalStateException("Test module does not compile: " + diagnostics.getDiagnostics());
}