Skip to content

Improve native GC lifetimes and member resolution - #227

Merged
dlunch merged 3 commits into
mainfrom
improve/native-gc-and-member-resolution
Oct 25, 2026
Merged

dlunch merged 3 commits into
mainfrom
improve/native-gc-and-member-resolution

Conversation

@dlunch

@dlunch dlunch commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Garbage collection

  • Add optional consumer-provided ExternalRoots to Jvm::new.
  • Make push_native_frame(args) return a scoped NativeFrame guard that roots object arguments and releases local references on drop; remove manual Jvm::pop_frame.
  • Track reachability by object identity, retain active monitor owners and waiters, and keep synchronized method arguments and results rooted across suspension.
  • Return collection failures through anyhow::Result without sweeping after root discovery or tracing fails; translate Java GC entry-point failures to InternalError.
  • Extend GC tests and existing synchronization fixtures to cover external roots, frame lifetimes, cyclic references, and collection during monitor operations.

Member resolution

  • Check the requested static or instance kind first, preserving declaring-class lookup order and incompatible-kind errors.

Verification

  • cargo fmt --all
  • cargo clippy --workspace --all-targets --locked --offline -- -D warnings
  • cargo clippy --target wasm32-unknown-unknown --locked --offline -- -D warnings
  • cargo test --workspace --locked --offline --quiet: 581 passed, 2 ignored.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 23:38
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T00:35:47.252497Z b0fac61 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.79%. Comparing base (00a2734) to head (b0fac61).

Files with missing lines Patch % Lines
jvm/src/jvm.rs 91.76% 7 Missing ⚠️
rustjava-runtime/src/classes/java/lang/runtime.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #227      +/-   ##
==========================================
+ Coverage   87.78%   87.79%   +0.01%     
==========================================
  Files         311      311              
  Lines       47092    47155      +63     
==========================================
+ Hits        41338    41401      +63     
  Misses       5754     5754              

☔ 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.

🔵 Needs a closer look

It reworks core GC reachability, monitor concurrency, and synchronized-method execution ordering—high-risk areas where subtle lifetime or race regressions warrant final human review despite passing tests.

No open issues 🎉

What changed in this PR

This PR hardens the native JVM's garbage collector against collecting host-visible references and tightens member resolution. On the GC side it adds a consumer-provided ExternalRoots trait plus scoped NativeFrame guards so that host references, synchronized-method arguments/results, and active monitor objects stay rooted across suspension points; reachability is now tracked by object identity (fixing cyclic-reference handling) and collection failures surface through anyhow::Result instead of panicking, with Java System.gc()/Runtime.gc() translating failures to InternalError. On the resolution side, field/method lookup now tries the caller's requested static/instance kind first while preserving JVMS declaring-class order and incompatible-kind errors.

Changes:

  • GC roots now include ExternalRoots, active monitors (Monitor::is_active), and NativeFrame-scoped host references; collect_garbage/determine_garbage return anyhow::Result and key reachability on ClassInstance::identity().
  • Synchronized-method execution acquires the monitor after pushing the Java frame and uses pop_frame_with_result, keeping arguments/results rooted while waiting for the monitor; MonitorWait tracks active_waits and releases it on Drop.
  • resolve_field/resolve_method gained an expected_static preference; broad test/fixture additions cover external roots, frame lifetimes, cycles, and GC during monitor operations.
File Description
jvm/​src/​external_roots.rs New ExternalRoots trait for consumer-provided root discovery.
jvm/​src/​jvm.rs NativeFrame guard, native_frame/pop_frame_with_result, external-root + monitor rooting, expected_static resolution, reordered synchronized-method flow.
jvm/​src/​garbage_collector.rs Identity-based reachability and anyhow::Result error propagation instead of unwrap.
jvm/​src/​monitor.rs active_waits counter, is_active, MonitorWait Drop, and &mut listener await.
jvm/​src/​lib.rs Re-exports ExternalRoots and NativeFrame.
rustjava-runtime/​src/​classes/​java/​lang/​{system,runtime}.rs Translate GC failure to InternalError.
jvm/​tests/​test_garbage_collection.rs, jvm/​tests/​test_string.rs New/updated GC tests (external roots, frames, monitors, cycles).
test-data/​src/​{MonitorSemantics,ThreadInterruption}.java Add System.gc() to exercise collection during monitor operations.
test-utils/​src/​lib.rs, src/​lib.rs, rustjava-runtime/​tests/​.../​test_class.rs Re-export get_bootstrap_class_loader and pass new Jvm::new argument.

Review effort: Balanced

@dlunch
dlunch merged commit 367c6db into main Oct 25, 2026
12 checks passed
@dlunch
dlunch deleted the improve/native-gc-and-member-resolution branch October 25, 2026 00:44
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