Skip to content

Resolve class references through the defining class loader - #25

Merged
atgreen merged 1 commit into
masterfrom
lazy-class-resolution
Oct 20, 2026
Merged

atgreen merged 1 commit into
masterfrom
lazy-class-resolution

Conversation

@atgreen

@atgreen atgreen commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Fixes the class loader bug behind ABCL's compile failures (from #24's known issues), and switches Class.forName to loadClass.

The bug

When OpenLDK resolved a class reference, it looked only in its own tables and on the classpath, never asking the Java class loader that defined the referring class. That applies to the classes a method refers to, at JIT time, and to a class's superclass and interfaces, at definition time. Classes a custom loader had already defined were usually found, because every class is also registered in a global by-name table. A loader that defines classes only when its findClass is asked, as ABCL's MemoryClassLoader does, never got asked. The JIT silently substituted an empty stub class (classfile.lisp:217), and the generated code then called an undefined OPENLDK::%clinit-…. That's why (compile 'make-adder) on a closure failed, cold or in an image.

The fix

%resolve-class name ldk-loader (src/openldk.lisp):

  1. the defining loader's tables, then its parents'
  2. the global tables and the classpath, as before
  3. for a user-defined loader, its Java loadClass

The JIT's resolution sites (class constants in classfile.lisp, 13 classload calls in codegen.lisp and bc-to-ir.lisp) and superclass/interface resolution in %classload-from-stream go through it, using the defining loader.

Calling Java from inside the JIT needs guards:

  • loadClass runs with *context* bound to nil, so its Java code can't see the outer compile's context.
  • A per-thread set of names being resolved stops cycles such as loadClass → defineClass → resolve the superclass → loadClass.
  • A compile claim in %claim-method-compilation now records its thread instead of t. A thread asking for a method it's already compiling gets an internal-error instead of waiting on itself forever. The image cleanup in cli.lisp drops any claim that isn't :done.

Order: the Java loader is asked last, so a child-first loader that shadows a classpath class still gets the classpath version. That's part of the deferred problem of same-named classes in two loaders, which comes from the global by-name table.

Class.forName

forName0 with a user loader now calls loadClass, which checks the loader's own already-loaded classes, delegates to its parent, and only then calls findClass. Before, it called findClass directly. With initialize=true, it now initializes the class the loader returned rather than looking it up by name in the global table.

JDK 25 leftovers this path hit

When loadClass falls through to findClass, the JDK bumps a PerfCounter. That needed jdk/internal/perf/Perf.registerNatives (JDK 9+ package) and DirectByteBuffer(long, long) in Perf.createLong (was JDK 8's (long, int)).

Tests

New aaa/lazyloader.java has a loader that defines classes only from findClass. Its classes exist nowhere else: they're made from nested Gen* classes by renaming them to Hid* (same length, so the class files stay valid). The test checks:

  • HidA's code reads a static of HidB
  • HidC's superclass HidBase is found through the loader
  • Class.forName(…, loader) on an already-defined class doesn't call findClass again (counted), and forName("java.lang.String", …, loader) returns String

Results:

  • Real JVM: OK.
  • On master: OPENLDK::|%clinit-aaa/lazyloader$HidB| is undefined.
  • With only the forName0 change reverted: FAIL: …; false; true, the findClass count.
  • zipfile, filestat, waitwake, monitorwake and jitwake still pass.

ABCL, cold: (compile 'make-adder) → (funcall (make-adder 3) 4) returns 7, a compiled pair of closures over one counter returns 2, and defclass/defgeneric/defmethod work with the compiler loaded.

ABCL warm image: the warmup calls compile on closures again. The image prints its prompt and quits in 0.1 s and runs all of the above correctly. It's 740 MB and builds in about 8.5 minutes. docs/ABCL.md and docs/IMAGES.md are updated, and the closure limitation is gone from both.

make check-regression couldn't run locally (no DejaGnu), so CI's gate is the full check.

Still open

  • Same-named classes in two loaders: the global by-name table, and %compile-method finding classes by name.
  • Field.get(null) returning null for statics of custom-loader classes.

🤖 Generated with Claude Code

OpenLDK resolved the classes a method refers to, and a class's superclass
and interfaces, by name in its own tables and on the classpath, never by
asking the Java class loader that defined the class.  A loader that
defines classes only when findClass is asked, as ABCL's MemoryClassLoader
does, never got asked: the JIT substituted an empty stub, and the code
then called an undefined OPENLDK::%clinit-.  This is why ABCL could not
COMPILE a function containing a closure.

%resolve-class looks in the defining loader's tables and its parents',
then the global tables and classpath as before, and finally calls the
user-defined loader's loadClass.  The JIT's resolution sites and
superclass/interface resolution go through it.  Java code run by loadClass
doesn't see the outer compile's *context*, a per-thread set stops
resolution cycles, and a compile claim now records its thread, so a
thread asking for a method it is already compiling gets an error instead
of waiting on itself.

Class.forName with a user loader now calls loadClass instead of findClass,
as the JVM does, so an already-defined class isn't handed to findClass
again; with initialize=true it initializes the class the loader returned.

Also JDK 25 names that loadClass's findClass path needed:
jdk/internal/perf/Perf.registerNatives, and DirectByteBuffer(long, long)
in Perf.createLong.

aaa/lazyloader.java covers lazily defined classes, a superclass only the
loader can find, and forName through loadClass.  The ABCL warm image's
warmup calls COMPILE on closures again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@atgreen
atgreen merged commit aa0d596 into master Oct 20, 2026
2 checks passed
@atgreen
atgreen deleted the lazy-class-resolution branch October 20, 2026 14:24
@atgreen atgreen mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant