mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
ModuleJarCheck rejects a module that references a class the host's jar leaves out of an exposed library
This commit is contained in:
1 parent
1a319d842b
commit
d2d746734f
7 files changed
+195
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>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<Pattern> patterns;
|
||||
|
||||
private HostClassesLeftOut(List<Pattern> 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<String> 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<String> 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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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=<file>} (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<String> check(Path jar, Collection<String> 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<String> check(Path jar, Collection<String> exposedHostPackages, Collection<String> 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 <module jar> [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 <module jar> [--left-out=<file>] [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<String> readLines(Path file) {
|
||||
try {
|
||||
return Files.readAllLines(file);
|
||||
} catch (IOException unreadable) {
|
||||
throw new UncheckedIOException("Cannot read the left-out classes file " + file, unreadable);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,12 @@ final class PackReferenceCheck {
|
||||
+ " --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.");
|
||||
"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;
|
||||
|
||||
Reference in new issue
Block a user