Skip to content

Initialize a class before reflective access to its static fields - #26

Merged
atgreen merged 1 commit into
masterfrom
reflect-static-clinit
Oct 20, 2026
Merged

atgreen merged 1 commit into
masterfrom
reflect-static-clinit

Conversation

@atgreen

@atgreen atgreen commented Oct 20, 2026

Copy link
Copy Markdown
Owner

JDK 25 routes Field.get/Field.set through a method-handle accessor, which OpenLDK can't run, so OpenLDK replaces both with a direct slot access (src/native/reflection.lisp). That replacement never initialized the declaring class, while the JVM always runs <clinit> before reflective access to a static.

So a static field read by reflection before anything else touched its class came back with its default value. A plain app class:

JVM:     MSG=set by <clinit> N=42
OpenLDK: MSG=null N=0

Field.set had the mirror problem: the written value was overwritten when <clinit> ran later.

This was listed in #25 and docs/ABCL.md as a custom-class-loader bug. It isn't one: the repro just read the field before anything else had initialized the class.

Fix: %field-slot-storage, which both overrides use, now initializes the declaring class (%clinit) before returning a static field's storage.

Test: new aaa/reflstatic.java reads two statics by reflection first, and writes one with Field.set before its class is initialized.

  • JVM: OK
  • before: FAIL: null 0 1 1
  • after: OK

make check-regression couldn't run locally (no DejaGnu), so CI's gate is the full check. Once #25 merges, the "reflective static field reads" known issue in docs/ABCL.md can go; I'll remove it in the follow-up loader PR.

🤖 Generated with Claude Code

OpenLDK replaces Field.get/set with a direct slot access, which never
initialized the declaring class.  A static field read by reflection before
anything else touched its class came back with its default (null, 0), and
a value written by Field.set was overwritten when <clinit> ran later.
Initialize the declaring class first, as the JVM does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
atgreen added a commit that referenced this pull request Oct 20, 2026
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 merged commit fb2d137 into master Oct 20, 2026
2 checks passed
@atgreen
atgreen deleted the reflect-static-clinit branch October 20, 2026 14:25
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