Files
Iris/src/main/java/com/volmit/host/UnavailableJdkModules.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

54 lines
2.4 KiB
Java

package com.volmit.host;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/** The JDK modules whose classes pack modules cannot use, by package. Packs see the JDK through the
* platform class loader, which provides the modules defined to it and to the boot loader. Other JDK
* modules, such as {@code jdk.compiler} or {@code jdk.jshell}, are defined to the application class
* loader, or not resolved at all, so a pack never sees them. Judged from module descriptors, so
* nothing is loaded.
*/
final class UnavailableJdkModules {
private static final char PACKAGE_SEPARATOR = '.';
private UnavailableJdkModules() {}
/** The JDK module that holds the class's package, if packs cannot use that module. */
static Optional<String> moduleOf(String binaryName) {
int separator = binaryName.lastIndexOf(PACKAGE_SEPARATOR);
if (separator < 0) return Optional.empty();
return Optional.ofNullable(ModulesByPackage.UNAVAILABLE.get(binaryName.substring(0, separator)));
}
private static boolean platformLoaderProvides(String moduleName) {
return ModuleLayer.boot().findModule(moduleName)
.filter(UnavailableJdkModules::platformLoaderProvides)
.isPresent();
}
/** Whether packs see the module's classes, which they do through the platform class loader. */
static boolean platformLoaderProvides(Module module) {
return isPlatformOrBootLoader(module.getClassLoader());
}
/** The boot loader is {@code null}; the platform loader delegates to it. */
private static boolean isPlatformOrBootLoader(ClassLoader loader) {
return loader == null || loader == ClassLoader.getPlatformClassLoader();
}
/** Built on first use, once per runtime: the JDK's modules do not change while it runs. */
private static final class ModulesByPackage {
static final Map<String, String> UNAVAILABLE = ModuleFinder.ofSystem().findAll().stream()
.map(ModuleReference::descriptor)
.filter(descriptor -> !platformLoaderProvides(descriptor.name()))
.flatMap(descriptor -> descriptor.packages().stream().map(name -> Map.entry(name, descriptor.name())))
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
private ModulesByPackage() {}
}
}