Skip to content

Keep classes of the same name in different class loaders apart - #27

Merged
atgreen merged 3 commits into
masterfrom
loader-isolation
Oct 20, 2026
Merged

atgreen merged 3 commits into
masterfrom
loader-isolation

Conversation

@atgreen

@atgreen atgreen commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Builds on #25, which it includes as its first commit (86a10ed). Merge #25 first, and this PR's diff shrinks to 789a386.

Java identifies a class by its name and its defining loader. Several OpenLDK paths used the name alone, so a second loader's class of the same name could replace, or be mistaken for, the first. Two sibling loaders each defining HidXX, on master plus #25:

FAIL: one1 one2 one3 one4 distinct=false    (JVM: one1 two101 one2 one3)

The second loader got the first loader's class.

What was wrong, and the fix

The plan in the #25 discussion named three layers. Tracing the test turned up five places that went by name:

  1. Shared packages. User loaders reused their parent's Lisp package, so a second loader's defclass redefined the first loader's class in place. Each user loader now gets its own package (OPENLDK.L<n>), and *ldk-loaders-by-package* maps a package back to its loader.
  2. The boot loader's tables are the global tables. Those hold every loader's classes, so delegating to the boot loader found unrelated loaders' classes. A second loader's defineClass then reused the first loader's java.lang.Class and overwrote its classLoader. Delegation to the boot loader (%lookup-class-with-delegation), classload and findBootstrapClass no longer return a class a user loader defined. classload goes on to the classpath instead, so an app class named like a user loader's class still loads.
  3. Package by name. These found the package from the class name: getClass(), reflective construction (newInstance0 via %make-java-instance), both allocateInstances, static field storage, isInstance, isAssignableFrom and static-method reflection. They now go through the class's loader (%java-class-package, ldk-class-package). class-package now defaults to the loader of the class being compiled, which covers the JIT's call sites.
  4. Stubs and claims by name. Method stubs called %compile-method with the class name, so the wrong class could be compiled, and compile claims were keyed by name, so a same-named class's method could be marked done and never compile. Stubs now pass the <class> (the name under AOT, whose output is Lisp source). A user loader's claim keys carry the loader.
  5. Child-first loaders. A loader that overrides loadClass is now asked before the classpath (%overrides-load-class-p, cached per class), so it can supply its own class of a name the classpath also has. Parent-first loaders keep the cheaper order.

docs/CLASSLOADER.md was an early design transcript; it now describes how loaders actually work.

Tests

New, using lazyloader's same-length renaming trick:

  • aaa/siblingloaders.java: two loaders each define a class of the same name from different bytecode, and each keeps its own static state, static and instance methods. JVM OK; before FAIL: one1 one2 one3 one4 distinct=false; after OK.
  • aaa/childfirst.java: a child-first loader shadows a classpath class, and its class's reference resolves to the shadow while the app's own class still gets the classpath version. JVM OK; before FAIL: parent parent; after OK.

Still passing locally: lazyloader, zipfile, filestat, jitwake, waitwake, monitorwake. javacl builds with no warmup errors and compiles Hello.java.

Cost of one package per loader

ABCL creates a loader for every compile, so I compared it cold against #25's build. The script ran 30 closure compiles in a loop plus other compiles and CLOS:

#25 this PR
Whole run 601 s 481 s
30 compiles (ABCL's time) 27.0 s 24.4 s
Peak memory 2.52 GB 2.54 GB

Results are identical. No measurable cost; the speedup is probably run-to-run noise.

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

🤖 Generated with Claude Code

atgreen and others added 3 commits October 20, 2026 08:03
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]>
Java identifies a class by name and defining loader, but several OpenLDK
paths identified it by name alone, so a second loader's class of the same
name could replace, or be mistaken for, the first:

- User loaders shared their parent's Lisp package, so a second loader's
  DEFCLASS redefined the first loader's class in place.  Each user loader
  now gets its own package (OPENLDK.L<n>); *ldk-loaders-by-package* maps
  it back to the loader.
- The boot loader's tables are the global tables, which hold every
  loader's classes, so delegating to the boot loader found unrelated
  loaders' classes: a second loader's defineClass reused the first's
  java.lang.Class and overwrote its classLoader.  Delegation to the boot
  loader, classload and findBootstrapClass no longer return a class a user
  loader defined.
- getClass(), reflective construction, allocateInstance, static field
  storage, isInstance, isAssignableFrom and static-method reflection found
  the package by class name; they now go through the class's loader.
  class-package defaults to the loader of the class being compiled.
- Method stubs called %compile-method with the class name, and compile
  claims were keyed by name; stubs now pass the <class>, and a user
  loader's claims carry the loader.

A loader that overrides loadClass (child-first) is now asked before the
classpath, so it can supply its own class of a name the classpath has.

New aaa/siblingloaders.java and aaa/childfirst.java.  docs/CLASSLOADER.md,
an early design transcript, now describes the current model.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It was not a class loader bug: Field.get never initialized the declaring
class, which #26 fixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@atgreen

atgreen commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner Author

Added 570b9c6: drops the "reflective static field reads" known issue from docs/ABCL.md. It wasn't a class loader bug; #26 fixes it (Field.get never initialized the declaring class). It's done here rather than in #26 because this branch already has #25's version of that list, so there's no conflict. It's best merged after #26.

@atgreen
atgreen merged commit 2e5e6db into master Oct 20, 2026
2 checks passed
@atgreen
atgreen deleted the loader-isolation branch October 20, 2026 14:25
atgreen added a commit that referenced this pull request Oct 20, 2026
PRs #19-#21 and #23-#27 added aaa/jitwake, monitorwake, waitwake,
zipfile, filestat, lazyloader, reflstatic, siblingloaders and childfirst.
check-regression.sh only fails on lost passes and new failures, so a new
test is not enforced until it is in the baseline.  Core suites now 528
expected passes, up from 501; no test changed state.

Generated with make check-regression at 2e5e6db; the Running lines keep
the existing baseline's paths.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
atgreen added a commit that referenced this pull request Oct 21, 2026
Fix Clojure protocols on records (a #27 regression), and an unloaded ClassCastException
@atgreen atgreen mentioned this pull request Oct 21, 2026
Passw pushed a commit to Passw/atgreen-openldk that referenced this pull request Oct 23, 2026
emit-<class> names a class's superclass and interfaces with
class-symbol-for-reference, which only searched the defining loader's
parent chain.  A class from a loader outside that chain -- Clojure makes
a DynamicClassLoader per evaluation, so a defprotocol's interface and a
defrecord implementing it come from sibling loaders -- was missed, a
fresh symbol was interned in the new class's package, and the interface
was then dropped from its supers.  The record was no instance of the
protocol's interface: (instance? user.Area (->Sq 5)) was false and
calling the protocol method failed.  This came in with atgreen#27, when user
loaders stopped sharing a package.

class-symbol-for-reference now falls back to %resolve-class, which asks
the defining loader's loadClass, and names the class in its own package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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