Limit pack modules to the API, the JDK and their own pack

The pack class loader's parent is now ApiAndJdkClassLoader: com.volmit.
classes and resources come from the API's loader, everything else from
the platform loader, so modules no longer see Iris internals or the
libraries Iris bundles.

Before any class is defined, a pack load now fails when a jar holds a
com.volmit. class, and when a class references a class that is not in
the API, the JDK or a jar of the pack (read with java.lang.classfile).
Linkage errors while installing name the module's jar. ModuleInfo names
and versions reject line breaks and other control characters.
This commit is contained in:
piexel committed 2026-10-05 18:24:53 +02:00
1 parent 862f4d19be
commit 1553959cf4
11 files changed
+563 -60

No files matched your search

+61 -11
View File
@@ -10,21 +10,25 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
/** One jar in a pack's modules directory, the entry class it names and the classes it holds.
/** One jar in a pack's modules directory, the entry class it names and the classes it holds,
* each with the classes it links against.
*
* <p>The entry point is the jar's own {@code META-INF/services/com.volmit.IrisModule} file, in
* the format {@link java.util.ServiceLoader} reads. The host reads that file from this jar only:
* {@code ServiceLoader} would also return providers from parent class loaders and could not tell
* which jar a broken entry came from.
*/
record ModuleJar(Path path, String origin, String entryClass, List<String> classNames) {
record ModuleJar(Path path, String origin, String entryClass, List<JarClass> classes) {
static final String ENTRY_POINT_FILE = "META-INF/services/" + IrisModule.class.getName();
private static final String COMMENT_START = "#";
private static final String CLASS_FILE_SUFFIX = ".class";
@@ -35,22 +39,37 @@ record ModuleJar(Path path, String origin, String entryClass, List<String> class
Objects.requireNonNull(path, "path");
Objects.requireNonNull(origin, "origin");
Objects.requireNonNull(entryClass, "entryClass");
classNames = List.copyOf(classNames);
classes = List.copyOf(classes);
}
/** A class in the jar, by binary name, and the binary names of the classes it links against. */
record JarClass(String name, SortedSet<String> references) {
JarClass {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(references, "references");
}
}
/** Reads the single entry class a jar names and the classes it holds. The origin is the jar
* path relative to its pack.
* path relative to its pack. Fails if the jar holds a class of the Iris API or a class file that
* cannot be parsed.
*/
static ModuleJar read(Path packRoot, Path jar) {
var origin = packRoot.relativize(jar).toString();
try (var file = openAsClassLoaderSeesIt(jar)) {
var entryClass = requireSingleEntryClass(origin, readEntryClasses(file));
return new ModuleJar(jar, origin, entryClass, readClassNames(file));
var classEntries = findClassEntries(file);
requireNoApiClasses(origin, classEntries);
return new ModuleJar(jar, origin, entryClass, readClasses(origin, file, classEntries));
} catch (IOException exception) {
throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
}
}
List<String> classNames() {
return classes.stream().map(JarClass::name).toList();
}
URL url() {
try {
return path.toUri().toURL();
@@ -102,18 +121,16 @@ record ModuleJar(Path path, String origin, String entryClass, List<String> class
}
}
/** Binary names of the classes in the jar, sorted. A multi-release jar contributes each class
/** The class file entries of the jar, sorted by name. A multi-release jar contributes each class
* once, under its base name, in the version this runtime would load. Anything left below
* {@code META-INF/}, such as versioned entries of a jar that is not multi-release, cannot be
* loaded as a class: {@code META-INF} is not a valid package name.
*/
private static List<String> readClassNames(JarFile file) {
private static List<JarEntry> findClassEntries(JarFile file) {
return file.versionedStream()
.filter(Predicate.not(JarEntry::isDirectory))
.map(JarEntry::getName)
.filter(ModuleJar::isClassFile)
.map(ModuleJar::toBinaryName)
.sorted()
.filter(entry -> isClassFile(entry.getName()))
.sorted(Comparator.comparing(entry -> toBinaryName(entry.getName())))
.toList();
}
@@ -123,6 +140,39 @@ record ModuleJar(Path path, String origin, String entryClass, List<String> class
&& !entryName.startsWith(META_INF);
}
/** The API package prefix is reserved for the API that Iris provides at runtime. The pack loader
* asks the host for those classes first, so a packaged API class would be silently replaced by the
* host's copy, and a module's own class there could be replaced by a later API class of that name.
*/
private static void requireNoApiClasses(String origin, List<JarEntry> classEntries) {
var apiClasses = classEntries.stream()
.map(entry -> toBinaryName(entry.getName()))
.filter(ApiAndJdkClassLoader::isApiClass)
.toList();
if (apiClasses.isEmpty()) return;
var others = apiClasses.size() == 1 ? "" : " and " + (apiClasses.size() - 1) + " more";
throw new IllegalArgumentException("Module jar " + origin + " contains Iris API class " + apiClasses.getFirst()
+ others + "; Iris provides the API at runtime, so mark the Iris API dependency compileOnly"
+ " (Maven: provided) and keep module classes out of " + ApiAndJdkClassLoader.API_PACKAGE_PREFIX + "*");
}
private static List<JarClass> readClasses(String origin, JarFile file, List<JarEntry> classEntries) throws IOException {
var classes = new ArrayList<JarClass>(classEntries.size());
for (var entry : classEntries) {
classes.add(new JarClass(toBinaryName(entry.getName()), readReferences(origin, file, entry)));
}
return classes;
}
private static SortedSet<String> readReferences(String origin, JarFile file, JarEntry entry) throws IOException {
try (var input = file.getInputStream(entry)) {
return ClassReferences.read(input.readAllBytes());
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException("Module jar " + origin + " holds a class file this runtime cannot read, "
+ entry.getRealName() + ": " + exception.getMessage(), exception);
}
}
private static String toBinaryName(String entryName) {
return entryName.substring(0, entryName.length() - CLASS_FILE_SUFFIX.length()).replace('/', '.');
}