diff --git a/CONTRACTS.md b/CONTRACTS.md index 78060467e..d074f9c70 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -31,9 +31,12 @@ context, outputs and accumulators. Modules compile against the API `compileOnly` and never ship it. At runtime they can use the API (`com.volmit.*`), the host packages the host chooses to expose, the JDK and the classes in their own pack's jars. They may use any library they ship in the pack, never Iris's copy. JDK modules -outside the platform class loader, such as `jdk.compiler`, are not available. A class Iris has but -hides, a class of an unavailable JDK module, or one missing from an exposed package fails the pack -load, naming the jar and the classes. A class nobody has (an optional dependency) only warns, unless +outside the platform class loader, such as `jdk.compiler`, are not available, and neither are JDK +packages not exported to unnamed modules, such as `jdk.internal.misc`, unless the server is started +with `--add-exports /=ALL-UNNAMED`. `sun.misc.Unsafe` from `jdk.unsupported` is the +supported way for modules to use Unsafe. A class Iris has but hides, a class of an unavailable JDK +module or non-exported JDK package, or one missing from an exposed package fails the pack load, +naming the jar and the classes. A class nobody has (an optional dependency) only warns, unless its package is one the module declares optional with `ModuleInfo.withOptionalPackages(...)` (`PackageName`). That declaration never excuses a load error. diff --git a/DESIGN.md b/DESIGN.md index 4f1ca5b24..e89f7a732 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -59,7 +59,8 @@ API): 4. Fails if a jar holds a class of an exposed package, naming the jar and the first such class. 5. Fails if a class is in two jars, naming the class and both jars. 6. Resolves every class each class uses (see below). A class that Iris has but hides, a class of a - JDK module packs cannot see, or a class missing from an exposed package fails the load, listing + JDK module packs cannot see or of a JDK package packs may not use, or a class missing from an + exposed package fails the load, listing each referencing and referenced class per jar and kind. Classes that nobody has are collected per jar. Nothing has been defined up to here. 7. Creates one `URLClassLoader` for all jars of the pack, parent = `ExposedHostClassLoader`. @@ -96,6 +97,14 @@ The platform loader provides the JDK modules defined to it and to the boot loade the application loader, such as `jdk.compiler`, `jdk.jshell` and `jdk.attach`, are not available to modules, and neither are JDK modules the runtime did not resolve. +Seeing a class is not enough to use it. Pack classes live in their loader's unnamed module, and a +JDK module lets an unnamed module use only the packages it exports to all unnamed modules. Internal +packages such as `jdk.internal.misc` (`jdk.internal.misc.Unsafe`) are not, so code using them would +fail with `IllegalAccessError` when it runs. Packages exported only to named JDK modules (qualified +exports) count as not exported. The supported way to use Unsafe is `sun.misc.Unsafe`, which +`jdk.unsupported` exports. A server started with `--add-exports /=ALL-UNNAMED` +exports that package to every unnamed module, and then modules may use it. + The exposed packages (`ExposedPackages`) are the API's package, `com.volmit` (the package of `IrisModule`), plus whatever the host passes to `PackModules.load`. The host decides those; the intended use is the packages that hold built-in modules' stage constants, so that a pack jar can @@ -132,13 +141,20 @@ file, so nothing is loaded or initialized: | The class is | Result | | --- | --- | +| in a package of a JDK module the platform loader provides, not exported to unnamed modules, such as `jdk.internal.misc` | load error: names the package and module; points to `sun.misc.Unsafe` and `--add-exports` | | in a jar of the pack, an exposed package of the host, or the JDK | resolves | | in an exposed package, but the host does not have it | load error: the jar was built against a different Iris API or Iris version | | in a JDK module the platform loader does not provide, such as `jdk.compiler` | load error: names the JDK module, which is not available to modules | | not exposed, but the host's loader has it (Iris internals, Iris's copy of a library) | load error: modules cannot use Iris's classes; ship the library in the pack or use the API | | nowhere | the pack loads, with a warning unless the module declares the package optional | -The rows are checked in that order. The JDK row comes before the host row because the host's loader +The rows are checked in that order. The export row comes first because access does not depend on +which loader finds the class: whoever loads `jdk.internal.misc.Unsafe`, `java.base` decides who may +use it. It asks the boot layer's `Module` objects whether they export the package to an unnamed +module that no export names (`Module.isExported(package, unnamedModule)`), so exports added with +`--add-exports ...=ALL-UNNAMED` count, exactly as they do at run time, and qualified exports do not. +Modules the platform loader does not provide are left to the unavailable-module row, so the two +never overlap. The unavailable-module row comes before the host row because the host's loader can usually load application-loader JDK classes, which would otherwise be reported as hidden Iris classes. Whether a class belongs to such a module is decided by its package, from the JDK's module descriptors (`ModuleFinder.ofSystem()`) and the loader the boot layer gives each module, so nothing @@ -171,8 +187,8 @@ load reports as a broken entry point of the jar. The declaration lives in the entry point because all module metadata does. That fixes its timing: `info()` exists only once the entry class is defined, after the reference check. This is safe because the declaration only removes warnings. Every load error is decided in step 6, without it, -so a declared package can never turn a hidden Iris class, an unavailable JDK class or a class -missing from an exposed package into a pass. `missingClasses()` drops the excused classes instead +so a declared package can never turn a hidden Iris class, an unavailable or non-exported JDK class +or a class missing from an exposed package into a pass. `missingClasses()` drops the excused classes instead of flagging them: it then says exactly what the warning says, and a class the author declared expected is not a problem to show in studio. diff --git a/README.md b/README.md index 929843c64..f73f35208 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,15 @@ public ModuleInfo info() { ``` A declaration covers only that module's own jar. It never excuses a load error: a class Iris hides, -a JDK class modules cannot see or a class missing from an exposed package fails even inside a -declared package. Package names follow the rules of exposed packages (whole packages, trailing dot +a JDK class modules cannot see or use, or a class missing from an exposed package fails even inside +a declared package. Package names follow the rules of exposed packages (whole packages, trailing dot accepted, wildcards rejected). Modules see the JDK through the platform class loader. JDK modules outside it, such as -`jdk.compiler` and `jdk.jshell`, are not available to modules. +`jdk.compiler` and `jdk.jshell`, are not available to modules. Of the available modules, modules +can use only the packages exported to them: `jdk.internal.misc.Unsafe` fails the load, while +`sun.misc.Unsafe` from `jdk.unsupported` is the supported way to use Unsafe. A server started with +`--add-exports /=ALL-UNNAMED` makes that package usable. Iris never downloads modules and does not check which API version a jar was built for. A pack ships its module jars, and keeping them compatible with the Iris version it targets is the pack @@ -119,7 +122,8 @@ point per jar, in jar file name order, and loads every jar of the pack through o Closing it closes that loader. A pack without a `modules` directory has no modules. Errors name the jar: no entry point, several entry points, a class of an exposed package in the jar, a class that is also in another jar of the pack (both jars are named), a class that uses a class Iris has but hides, -a class of a JDK module modules cannot see or a class missing from an exposed package, or an entry +a class of a JDK module modules cannot see, a class of a JDK package not exported to modules, a +class missing from an exposed package, or an entry class that cannot be loaded, is not an `IrisModule`, is not in that jar, fails to construct or whose `info()` fails (an invalid optional package, for example). All checks before the last run before any class of the pack is defined. The last error below lists every kind of unusable class the jar @@ -137,6 +141,9 @@ Modules cannot use Iris's own classes or its copies of libraries; ship the libra Module jar modules/moss.jar uses classes of JDK modules that are not available to modules: com.example.moss.Scripts -> com.sun.source.tree.Tree (JDK module jdk.compiler) Modules can use only the JDK modules of the platform class loader, such as java.base, java.sql and java.net.http. +Module jar modules/moss.jar uses classes of JDK packages that are not exported to modules: + com.example.moss.FastBuffer -> 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 /=ALL-UNNAMED. Module jar modules/moss.jar uses classes that this Iris does not have, in packages Iris provides to modules: com.example.moss.MossModule -> com.volmit.stage.NewerStage The jar was built against a different Iris API or Iris version. @@ -198,14 +205,19 @@ The tests cover graph resolution and every error path, fusion, activation cascad rejection and rollback, and install-order determinism. `PackModulesTest` compiles real module jars into a temporary pack and loads them, including every jar error, duplicate classes in plain and multi-release jars, API and other exposed classes in a jar, hidden host classes, JDK classes -outside the platform loader, several kinds of unusable class in one error, classes missing from an +outside the platform loader, non-exported JDK packages (`sun.misc.Unsafe` and `java.lang` still +load), several kinds of unusable class in one error, classes missing from an exposed package, optional dependencies that only warn, declared optional packages (fully and partly excused, per jar, never excusing an error, invalid names), references that must still load (arrays, the JDK, other pack jars, missing annotation types), linkage errors during install, and a pack jar that requires a stage of a built-in module in `example.builtin`, outside `com.volmit`, which loads only when the host exposes that package. `ExposedHostClassLoaderTest` checks what the pack loader's parent shows, `ExposedPackagesTest` which package names it accepts and how they -match, and `UnavailableJdkModulesTest` which JDK modules packs cannot see. `PackModulesSummaryTest` and `MissingClassesWarningTest` check the log texts. The example +match, `UnavailableJdkModulesTest` which JDK modules packs cannot see, and `NonExportedJdkPackagesTest` +which JDK packages they may not use. `PackModulesAddExportsTest` runs in its own JVM, the +`addExportsTest` task that `check` runs, started with +`--add-exports java.base/jdk.internal.misc=ALL-UNNAMED`; it checks that the exported package then +loads and that other internal packages are still reported. `PackModulesSummaryTest` and `MissingClassesWarningTest` check the log texts. The example ports in `src/test/java/com/volmit/example` rebuild preparation, anchoring, block writes, stilt and snow as separate modules, add a third-party moss module, and add a two-module chunk pipeline. There is no Minecraft integration or production storage in these tests.