Skip to content

Add JNI-style global references and complete GC roots - #182

Merged
dlunch merged 1 commit into
mainfrom
agent/jni-global-references
Jul 18, 2026
Merged

Add JNI-style global references and complete GC roots#182
dlunch merged 1 commit into
mainfrom
agent/jni-global-references

Conversation

@dlunch

@dlunch dlunch commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • add a typed, RAII-managed GlobalRef<T> backed by independent JVM global-reference entries
  • keep object arguments and field, static, array, method, and exception results alive as frame-local references
  • use a global reference while Thread.start() waits for its host spawn callback to attach
  • release bootstrap-only local references after JVM initialization

Root cause

Thread.start() captured its Java Thread instance in a Rust callback, but the JVM garbage collector could not see Rust-owned references. If collection ran after the caller frame returned and before the spawned task attached, the thread object could be collected. The same local-reference gap existed for object values entering a frame through several JVM APIs.

Validation

  • cargo test --workspace
  • cargo test -p jvm --test test_garbage_collection
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • git diff --check

Copilot AI review requested due to automatic review settings July 18, 2026 03:13
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.09091% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.11%. Comparing base (258cd4e) to head (7617a5a).

Files with missing lines Patch % Lines
java_runtime/src/classes/java/lang/thread.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #182      +/-   ##
==========================================
+ Coverage   87.03%   87.11%   +0.08%     
==========================================
  Files         198      199       +1     
  Lines       17846    17947     +101     
==========================================
+ Hits        15532    15635     +103     
+ Misses       2314     2312       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the JVM’s GC root tracking to cover Rust-owned references by introducing JNI-style global references and by treating certain API results/arguments as frame-local roots, addressing a real lifetime gap around Thread.start() and other object flows across native/Java boundaries.

Changes:

  • Add RAII-managed GlobalRef<T> backed by a global-reference table that the GC scans as a root set.
  • Record object arguments and returned object/exception results as frame-local references so they remain GC-visible across API boundaries.
  • Update Thread.start() to hold a global reference until the spawned callback attaches; add targeted GC regression tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test_utils/src/lib.rs Adds a queued-spawn mode to deterministically delay spawn callbacks in tests.
jvm/tests/test_garbage_collection.rs Adds tests covering global refs, frame-local roots for various APIs, and Thread.start() lifetime.
jvm/src/thread.rs Updates Java-frame creation to seed GC-visible locals from call arguments.
jvm/src/lib.rs Wires in the new global_ref module and exports GlobalRef.
jvm/src/jvm.rs Implements global refs, clears bootstrap locals post-init, and roots returned object/exception values + certain API results.
jvm/src/global_ref.rs Introduces GlobalReferences + RAII GlobalRef<T> with Drop cleanup.
jvm/src/garbage_collector.rs Treats global references as additional GC roots.
java_runtime/src/classes/java/lang/thread.rs Uses GlobalRef<Thread> to keep the thread instance alive until the spawn callback runs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread jvm/src/thread.rs
@dlunch
dlunch merged commit e557673 into main Jul 18, 2026
11 checks passed
@dlunch
dlunch deleted the agent/jni-global-references branch July 18, 2026 05:02
Jun025 added a commit to Jun025/RustJava that referenced this pull request Jul 31, 2026
Judged the two remaining remote branches on the fork:

- dependabot/cargo/tracing-attributes-0.1.31: deleted. PR #4 (fa92ef9)
  removed the tracing-attributes direct dependency outright, so the
  branch patches a Cargo.toml line that no longer exists.
- wie-ktf-hardening: preserved. 8 of its 12 commits are already in
  upstream/main via squash merges (dlunch#174 dlunch#175 dlunch#176 dlunch#177 dlunch#180 dlunch#182);
  git cherry missed this because origin/main trails upstream/main by
  20 commits. 4 commits carry residual value.

No code changes.

Co-authored-by: jun0 <junyoung.choi.a@miraeasset.com>
Co-authored-by: Claude <noreply@anthropic.com>
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.

2 participants