diff --git a/README.md b/README.md index e1adf5dc4..7e5022ef8 100644 --- a/README.md +++ b/README.md @@ -370,7 +370,10 @@ Duplicate module example:moss from modules/moss.jar (com.example.moss.MossModule `ModuleJarCheck.check(jar, exposedHostPackages)` (also its `main`) runs the load's class checks on one jar, without loading it: for a build that writes a module jar and wants to fail on a jar a pack would -refuse. +refuse. A build that checks against a full copy of an exposed library, while the host's jar strips some +of its classes, passes the stripped ones too (`check(jar, exposedHostPackages, leftOutClasses)`, or +`--left-out=` on the command line; one Ant pattern or class per line): a jar that references one +fails, naming the class. A module compiles against the Iris API as `compileOnly` (Maven: `provided`) and never ships it; Iris provides the API at runtime. At runtime a module can use the Iris API (`com.volmit.*`), the diff --git a/src/main/java/com/volmit/host/ExposedHostClassLoader.java b/src/main/java/com/volmit/host/ExposedHostClassLoader.java index 979ac5cb9..3a124b552 100644 --- a/src/main/java/com/volmit/host/ExposedHostClassLoader.java +++ b/src/main/java/com/volmit/host/ExposedHostClassLoader.java @@ -28,12 +28,21 @@ final class ExposedHostClassLoader extends ClassLoader { private final ClassLoader hostLoader; private final ExposedPackages exposed; + private final HostClassesLeftOut leftOut; /** {@code hostLoader} is the loader that defines the API; in production the Iris plugin's. */ ExposedHostClassLoader(ClassLoader hostLoader, ExposedPackages exposed) { + this(hostLoader, exposed, HostClassesLeftOut.none()); + } + + /** For a build that checks a jar against more classes than the real host has: {@code leftOut} names the + * classes the real host's loader lacks, which {@link #hasClass} then denies and {@link #leavesOut} reports. + */ + ExposedHostClassLoader(ClassLoader hostLoader, ExposedPackages exposed, HostClassesLeftOut leftOut) { super(NAME, ClassLoader.getPlatformClassLoader()); this.hostLoader = Objects.requireNonNull(hostLoader, "hostLoader"); this.exposed = Objects.requireNonNull(exposed, "exposed"); + this.leftOut = Objects.requireNonNull(leftOut, "leftOut"); } ExposedPackages exposed() { @@ -42,7 +51,12 @@ final class ExposedHostClassLoader extends ClassLoader { /** Whether this loader can load the class, judged by its class file so that nothing is loaded. */ boolean hasClass(String binaryName) { - return getResource(classFile(binaryName)) != null; + return !leavesOut(binaryName) && getResource(classFile(binaryName)) != null; + } + + /** Whether the real host lacks the class although the loader a build checks against has it. */ + boolean leavesOut(String binaryName) { + return leftOut.leavesOut(binaryName); } /** Whether the host's loader has the class, whether or not this loader shows it to packs. In diff --git a/src/main/java/com/volmit/host/HostClassesLeftOut.java b/src/main/java/com/volmit/host/HostClassesLeftOut.java new file mode 100644 index 000000000..6c1af41cb --- /dev/null +++ b/src/main/java/com/volmit/host/HostClassesLeftOut.java @@ -0,0 +1,71 @@ +package com.volmit.host; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +/** Classes that exist on the class path a build checks a module jar against, but that the real host leaves + * out of its jar, such as the parts of a shared library Iris strips when it packages itself. A module that + * uses one passes a check against the full library and fails on a server, so the check treats them as + * absent. + * + *

Entries are class file paths relative to the class root, as Ant patterns: {@code **} crosses + * packages, {@code *} does not. An entry that neither ends in {@code .class} nor contains {@code *} names a + * class and takes its nested classes with it. + */ +final class HostClassesLeftOut { + private static final HostClassesLeftOut NONE = new HostClassesLeftOut(List.of()); + private static final String CLASS_FILE_SUFFIX = ".class"; + private static final String COMMENT_START = "#"; + + private final List patterns; + + private HostClassesLeftOut(List patterns) { + this.patterns = List.copyOf(patterns); + } + + static HostClassesLeftOut none() { + return NONE; + } + + /** The entries of a list file's lines; blank lines and text after {@code #} are ignored. */ + static HostClassesLeftOut parse(Collection lines) { + Objects.requireNonNull(lines, "lines"); + var patterns = lines.stream() + .map(line -> line.contains(COMMENT_START) ? line.substring(0, line.indexOf(COMMENT_START)) : line) + .map(String::strip) + .filter(entry -> !entry.isEmpty()) + .flatMap(HostClassesLeftOut::classFilePatterns) + .map(HostClassesLeftOut::compile) + .toList(); + return patterns.isEmpty() ? NONE : new HostClassesLeftOut(patterns); + } + + boolean leavesOut(String binaryName) { + var classFile = binaryName.replace('.', '/') + CLASS_FILE_SUFFIX; + return patterns.stream().anyMatch(pattern -> pattern.matcher(classFile).matches()); + } + + private static Stream classFilePatterns(String entry) { + if (entry.endsWith(CLASS_FILE_SUFFIX) || entry.contains("*")) return Stream.of(entry); + return Stream.of(entry + CLASS_FILE_SUFFIX, entry + "$*" + CLASS_FILE_SUFFIX); + } + + private static Pattern compile(String antPattern) { + var regex = new StringBuilder(); + for (int at = 0; at < antPattern.length(); at++) { + char c = antPattern.charAt(at); + if (c != '*') { + regex.append(Pattern.quote(String.valueOf(c))); + } else if (at + 1 < antPattern.length() && antPattern.charAt(at + 1) == '*') { + regex.append(".*"); + at++; + } else { + regex.append("[^/]*"); + } + } + return Pattern.compile(regex.toString()); + } +} diff --git a/src/main/java/com/volmit/host/ModuleJarCheck.java b/src/main/java/com/volmit/host/ModuleJarCheck.java index 1b4c0de68..1b6349aa7 100644 --- a/src/main/java/com/volmit/host/ModuleJarCheck.java +++ b/src/main/java/com/volmit/host/ModuleJarCheck.java @@ -1,6 +1,9 @@ package com.volmit.host; import com.volmit.IrisModule; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; @@ -16,17 +19,28 @@ import java.util.Objects; *

The host classes come from the class loader that defines the API, so run this with the API and the * host's classes on the class path. {@link #main} takes a jar path followed by the host packages that * pack modules may use, and exits with 1 and the message on stderr when the jar fails. + * + *

The class path the check runs on may hold classes of an exposed library that the real host's jar + * leaves out. {@code --left-out=} (or the {@code leftOutClasses} parameter) lists them, one entry per + * line as described by {@code HostClassesLeftOut}, and a module that references one fails the check naming it. */ public final class ModuleJarCheck { private static final int FAILED_EXIT_CODE = 1; private static final int MISSING_ARGUMENT_EXIT_CODE = 2; + private static final String LEFT_OUT_OPTION = "--left-out="; private ModuleJarCheck() {} /** Returns the module classes the jar names, in the order it lists them, or fails naming the jar. */ public static List check(Path jar, Collection exposedHostPackages) { + return check(jar, exposedHostPackages, List.of()); + } + + /** Like {@link #check(Path, Collection)}, treating the classes {@code leftOutClasses} describes as missing from the host. */ + public static List check(Path jar, Collection exposedHostPackages, Collection leftOutClasses) { Objects.requireNonNull(jar, "jar"); - var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages)); + var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages), + HostClassesLeftOut.parse(leftOutClasses)); var read = ModuleJar.read(new JarSnapshot.CopiedJar(jar.toString(), jar)); requireEntryClassesInJar(read); PackClassCheck.requireLinkable(List.of(read), host); @@ -43,17 +57,27 @@ public final class ModuleJarCheck { } public static void main(String[] arguments) { - if (arguments.length == 0) { - System.err.println("Usage: ModuleJarCheck [exposed host package...]"); + var options = Arrays.stream(arguments).filter(argument -> argument.startsWith(LEFT_OUT_OPTION)).toList(); + var positional = Arrays.stream(arguments).filter(argument -> !argument.startsWith(LEFT_OUT_OPTION)).toList(); + if (positional.isEmpty()) { + System.err.println("Usage: ModuleJarCheck [--left-out=] [exposed host package...]"); System.exit(MISSING_ARGUMENT_EXIT_CODE); } - var exposed = Arrays.asList(arguments).subList(1, arguments.length); try { - var modules = check(Path.of(arguments[0]), exposed); - System.out.println("Module jar " + arguments[0] + " passes the pack module checks: " + modules); + var leftOut = options.stream().flatMap(option -> readLines(Path.of(option.substring(LEFT_OUT_OPTION.length()))).stream()).toList(); + var modules = check(Path.of(positional.getFirst()), positional.subList(1, positional.size()), leftOut); + System.out.println("Module jar " + positional.getFirst() + " passes the pack module checks: " + modules); } catch (RuntimeException failure) { System.err.println(failure.getMessage()); System.exit(FAILED_EXIT_CODE); } } + + private static List readLines(Path file) { + try { + return Files.readAllLines(file); + } catch (IOException unreadable) { + throw new UncheckedIOException("Cannot read the left-out classes file " + file, unreadable); + } + } } diff --git a/src/main/java/com/volmit/host/PackReferenceCheck.java b/src/main/java/com/volmit/host/PackReferenceCheck.java index d8d49343e..429faf517 100644 --- a/src/main/java/com/volmit/host/PackReferenceCheck.java +++ b/src/main/java/com/volmit/host/PackReferenceCheck.java @@ -44,7 +44,12 @@ final class PackReferenceCheck { + " --add-exports /=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."); + "The jar was built against a different Iris API or Iris version."), + /** A class of an exposed package that the library has but the host's own jar leaves out. */ + LEFT_OUT_HOST_CLASS( + "uses classes of the libraries Iris provides to modules that Iris's jar leaves out:", + "Iris strips classes it does not use itself from the libraries it ships. Use another class, or have" + + " Iris keep the class you need."); private final String header; private final String advice; @@ -102,6 +107,7 @@ final class PackReferenceCheck { /** 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.leavesOut(className)) return Resolution.LEFT_OUT_HOST_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; diff --git a/src/test/java/com/volmit/host/HostClassesLeftOutTest.java b/src/test/java/com/volmit/host/HostClassesLeftOutTest.java new file mode 100644 index 000000000..a0df3a148 --- /dev/null +++ b/src/test/java/com/volmit/host/HostClassesLeftOutTest.java @@ -0,0 +1,42 @@ +package com.volmit.host; + +import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +class HostClassesLeftOutTest { + @Test + void nothingIsLeftOutWithoutEntries() { + assertFalse(HostClassesLeftOut.none().leavesOut("a.B")); + assertFalse(HostClassesLeftOut.parse(List.of("", " # only a comment")).leavesOut("a.B")); + } + + @Test + void anEntryWithoutSuffixOrWildcardNamesAClassAndItsNestedClasses() { + var leftOut = HostClassesLeftOut.parse(List.of("a/b/C")); + + assertTrue(leftOut.leavesOut("a.b.C")); + assertTrue(leftOut.leavesOut("a.b.C$Inner")); + assertFalse(leftOut.leavesOut("a.b.Cat")); + assertFalse(leftOut.leavesOut("a.b.sub.C")); + } + + @Test + void doubleStarCrossesPackagesAndSingleStarDoesNot() { + var leftOut = HostClassesLeftOut.parse(List.of("a/noise/**", "a/stream/Basic*.class")); + + assertTrue(leftOut.leavesOut("a.noise.Simplex")); + assertTrue(leftOut.leavesOut("a.noise.deep.Cell")); + assertTrue(leftOut.leavesOut("a.stream.BasicStream")); + assertFalse(leftOut.leavesOut("a.stream.sub.BasicStream")); + assertFalse(leftOut.leavesOut("a.stream.Other")); + } + + @Test + void commentsAndBlankLinesAreIgnored() { + var leftOut = HostClassesLeftOut.parse(List.of("# header", "", " a/b/C # why", "a/D")); + + assertTrue(leftOut.leavesOut("a.b.C")); + assertTrue(leftOut.leavesOut("a.D")); + } +} diff --git a/src/test/java/com/volmit/host/ModuleJarCheckTest.java b/src/test/java/com/volmit/host/ModuleJarCheckTest.java index 82ce4a78f..d85fdd067 100644 --- a/src/test/java/com/volmit/host/ModuleJarCheckTest.java +++ b/src/test/java/com/volmit/host/ModuleJarCheckTest.java @@ -84,4 +84,30 @@ class ModuleJarCheckTest { assertTrue(failure(() -> ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES)).contains("has no entry point")); } + + @Test + void jarThatUsesAnExposedClassTheHostJarLeavesOutFailsNamingTheClass() { + var jar = module("pack.User", "user:core", NO_MEMBERS, + "graph.define(Stage.placement(\"user:core/carved\")).requires(example.builtin.CarveModule.CLEAR).run(context -> {});") + .compileAgainstHostOf(CarveModule.class) + .writeInto(build, "user.jar"); + var exposed = List.of(CarveModule.class.getPackageName()); + var leftOut = List.of("# stripped from the host jar", "example/builtin/CarveModule"); + + var message = failure(() -> ModuleJarCheck.check(jar, exposed, leftOut)); + + assertTrue(message.contains("leaves out"), message); + assertTrue(message.contains("pack.User -> example.builtin.CarveModule"), message); + } + + @Test + void jarThatUsesAnExposedClassNotLeftOutStillPasses() { + var jar = module("pack.User", "user:core", NO_MEMBERS, + "graph.define(Stage.placement(\"user:core/carved\")).requires(example.builtin.CarveModule.CLEAR).run(context -> {});") + .compileAgainstHostOf(CarveModule.class) + .writeInto(build, "user.jar"); + + assertEquals(List.of("pack.User"), ModuleJarCheck.check(jar, List.of(CarveModule.class.getPackageName()), + List.of("example/builtin/OtherModule"))); + } }