mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
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:
1 parent
e34acf2fbb
commit
f6b08a0303
8 files changed
+212
-10
No files matched your search
@@ -0,0 +1,55 @@
|
||||
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() {}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ import java.util.function.Predicate;
|
||||
*
|
||||
* <p>The JVM resolves a class only when code first needs it, so a class nobody has is not an
|
||||
* error: bundled libraries reference optional dependencies they only use when present. A class
|
||||
* that Iris has but hides, a class of a JDK module packs cannot see, or a class missing from a
|
||||
* package Iris exposes always fails the load.
|
||||
* that Iris has but hides, a class of a JDK module packs cannot see or of a JDK package packs may
|
||||
* not use, or a class missing from a package Iris exposes always fails the load.
|
||||
*/
|
||||
final class PackReferenceCheck {
|
||||
private static final int MAX_REPORTED_REFERENCES_PER_JAR = 10;
|
||||
@@ -36,6 +36,12 @@ final class PackReferenceCheck {
|
||||
"uses classes of JDK modules that are not available to modules:",
|
||||
"Modules can use only the JDK modules of the platform class loader, such as java.base, java.sql and"
|
||||
+ " java.net.http."),
|
||||
/** A package of an available JDK module that it does not export to packs, such as jdk.internal.misc. */
|
||||
NOT_EXPORTED_JDK_CLASS(
|
||||
"uses classes of JDK packages that are not exported to modules:",
|
||||
"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."),
|
||||
MISSING_HOST_CLASS(
|
||||
"uses classes that this Iris does not have, in packages Iris provides to modules:",
|
||||
"The jar was built against a different Iris API or Iris version.");
|
||||
@@ -66,8 +72,8 @@ final class PackReferenceCheck {
|
||||
jars.forEach(jar -> packClasses.addAll(jar.classNames()));
|
||||
}
|
||||
|
||||
/** Fails when a jar uses a class Iris hides, a class of a JDK module packs cannot see, or a class
|
||||
* missing from a package Iris exposes.
|
||||
/** Fails when a jar uses a class Iris hides, a class of a JDK module packs cannot see or of a JDK
|
||||
* package packs may not use, or a class missing from a package Iris exposes.
|
||||
* Returns the classes nobody has, per jar, for jars that use any.
|
||||
*/
|
||||
static List<MissingClasses> requireResolvable(List<ModuleJar> jars, ExposedHostClassLoader host) {
|
||||
@@ -93,7 +99,9 @@ final class PackReferenceCheck {
|
||||
return outsidePack.computeIfAbsent(className, this::resolveOutsidePack);
|
||||
}
|
||||
|
||||
/** Access comes first: whichever loader finds a JDK class, its module still decides who may use it. */
|
||||
private Resolution resolveOutsidePack(String className) {
|
||||
if (NonExportedJdkPackages.packageOf(className).isPresent()) return Resolution.NOT_EXPORTED_JDK_CLASS;
|
||||
if (host.hasClass(className)) return Resolution.PROVIDED;
|
||||
if (host.exposed().exposes(className)) return Resolution.MISSING_HOST_CLASS;
|
||||
if (UnavailableJdkModules.moduleOf(className).isPresent()) return Resolution.UNAVAILABLE_JDK_CLASS;
|
||||
@@ -121,8 +129,15 @@ final class PackReferenceCheck {
|
||||
private record Reference(String source, String target, Resolution resolution) {
|
||||
String describe() {
|
||||
var text = source + " -> " + target;
|
||||
if (resolution != Resolution.UNAVAILABLE_JDK_CLASS) return text;
|
||||
return text + " (JDK module " + UnavailableJdkModules.moduleOf(target).orElseThrow() + ")";
|
||||
return switch (resolution) {
|
||||
case UNAVAILABLE_JDK_CLASS -> text + " (JDK module " + UnavailableJdkModules.moduleOf(target).orElseThrow() + ")";
|
||||
case NOT_EXPORTED_JDK_CLASS -> text + " (" + describeNotExported(NonExportedJdkPackages.packageOf(target).orElseThrow()) + ")";
|
||||
default -> text;
|
||||
};
|
||||
}
|
||||
|
||||
private static String describeNotExported(NonExportedJdkPackages.JdkPackage jdkPackage) {
|
||||
return "package " + jdkPackage.name() + " of " + jdkPackage.module() + " is not exported";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,15 @@ final class UnavailableJdkModules {
|
||||
|
||||
private static boolean platformLoaderProvides(String moduleName) {
|
||||
return ModuleLayer.boot().findModule(moduleName)
|
||||
.filter(module -> isPlatformOrBootLoader(module.getClassLoader()))
|
||||
.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();
|
||||
|
||||
Reference in new issue
Block a user