fix(sdk/jni): guard finalizeScannerPairing against panics across FFI#435
Open
dsmfa10 wants to merge 1 commit into
Open
fix(sdk/jni): guard finalizeScannerPairing against panics across FFI#435dsmfa10 wants to merge 1 commit into
dsmfa10 wants to merge 1 commit into
Conversation
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Every JNI export wraps its body in `jni_catch_unwind_*` so a Rust panic cannot unwind across the `extern "system"` boundary (which is UB and aborts the JVM). `finalizeScannerPairing` was the lone exception: it called `env.get_string(..)` and `rt.block_on(..)` directly, so a panic in the orchestrator/runtime would cross the boundary. Wrap the body in `jni_catch_unwind_jboolean` (returns JNI_FALSE / 0 on panic), matching the other exports. NOTE: `jni` is `#[cfg(all(target_os = "android", feature = "jni"))]`, not built by the host toolchain; rustfmt will normalize the body indentation on an Android build. Verify there. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
623c00c to
c1aeaad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A Rust panic must never unwind across an
extern "system"(FFI) boundary — doingso is undefined behavior and aborts the JVM. The module's safety contract states
every JNI export wraps its body in a
jni_catch_unwind_*helper. Every export inble_events.rsandunified_protobuf_bridge.rsfollows this — exceptJava_..._finalizeScannerPairing, which calledenv.get_string(..)andrt.block_on(orchestrator.finalize_scanner_pairing_by_address(..))directly:A panic in the orchestrator/runtime/JNI would cross the FFI boundary.
Fix
Wrap the body in
jni_catch_unwind_jboolean(returnsJNI_FALSE/0 on panic),mirroring the established pattern used by the other exports:
Verification
Cannot
cargo checkhere (android+jni cfg-gated). The wrapper is the same helperAssertUnwindSafe(move || ...)form used by ~15 sibling exports in the samefile; the captured params are
Copyraw JNI pointers (UnwindSafe). Reviewershould build the Android target — rustfmt will also normalize the wrapped
body's indentation there.
CI gates & coverage
Full verification for this PR runs in CI — Rust (
cargo fmt --check,clippy -D warnings, workspace tests), Frontend, Android Unit Tests,Coverage, SPDX headers, CodeQL (see the PR's Checks tab). The local
check noted above is a subset; the broader mandated gates (full workspace test
suite, codegen/scan, Android, Frontend) run in CI, not locally — none is
silently skipped.