Files
Iris/src/main/java/com/volmit/host/NonExportedJdkPackages.java
T
piexel f6b08a0303 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.
2026-10-05 21:10:53 +02:00

56 lines
2.7 KiB
Java

package com.volmit.host;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** The packages of available JDK modules that pack modules can load classes from but not use. The
* platform class loader finds every class of its modules, but a module lets other code use only the
* packages it exports to it, so a class such as {@code jdk.internal.misc.Unsafe} fails with an
* {@link IllegalAccessError} once code touches it. Pack classes live in their loader's unnamed
* module, so a package counts as exported only when every unnamed module may use it: exported
* unqualified, or opened up with {@code --add-exports <module>/<package>=ALL-UNNAMED} on the server's
* command line. Exports to named modules only do not count. Judged from the boot layer's modules, so
* nothing is loaded.
*
* <p>Modules the platform loader does not provide are {@link UnavailableJdkModules}, not listed here.
*/
final class NonExportedJdkPackages {
private static final char PACKAGE_SEPARATOR = '.';
private NonExportedJdkPackages() {}
/** A JDK package and the module that holds it. */
record JdkPackage(String name, String module) {}
/** The class's package, if an available JDK module holds it without exporting it to packs. */
static Optional<JdkPackage> packageOf(String binaryName) {
int separator = binaryName.lastIndexOf(PACKAGE_SEPARATOR);
if (separator < 0) return Optional.empty();
return Optional.ofNullable(PackagesByName.NOT_EXPORTED.get(binaryName.substring(0, separator)));
}
private static Stream<JdkPackage> packagesNotExportedToPacks(Module module) {
return module.getPackages().stream()
.filter(name -> !module.isExported(name, PackagesByName.UNNAMED_MODULE_LIKE_A_PACK))
.map(name -> new JdkPackage(name, module.getName()));
}
/** Built on first use, once per runtime: the boot layer's exports are fixed at startup. */
private static final class PackagesByName {
/** The unnamed module of a loader that defines nothing and that no export names, so it may use
* exactly what every unnamed module may use, like a new pack loader's.
*/
static final Module UNNAMED_MODULE_LIKE_A_PACK = new ClassLoader("iris-jdk-export-probe", null) {}.getUnnamedModule();
static final Map<String, JdkPackage> NOT_EXPORTED = ModuleLayer.boot().modules().stream()
.filter(UnavailableJdkModules::platformLoaderProvides)
.flatMap(NonExportedJdkPackages::packagesNotExportedToPacks)
.collect(Collectors.toUnmodifiableMap(JdkPackage::name, Function.identity()));
private PackagesByName() {}
}
}