fix: contain Sapling FFI panics and preserve caller state

FE
2d ago
+2160-185

Unexpected Rust panics currently terminate a JVM linked to the release library. Add an internal unwind boundary to the Sapling/ZIP32 APIs used by JNI, with staged outputs and contexts so a caught panic cannot leave partially committed state.

  • Switch release to panic = 'unwind' and reject builds configured with panic = 'abort'.
  • Separate ordinary Rust implementations from 32 recoverable exports. Catch panics inside the exported function, before crossing C/JNI, and expose a read-and-clear thread-local status.
  • Compute outputs and mutable context updates in owned local values; commit only on success. Ordinary false returns and panics preserve caller state.
  • Add eleven checked replacements for void operations. Preserve fail-closed legacy void symbols, so older callers cannot mistake a failed operation for success. JNI must migrate to checked calls and inspect boundary status after every operation.
  • Retain atomic parameter publication. An unexpected panic while loading poisons the store and requires restart; startup must not proceed. Expected initialization failures remain retryable.
  • Provide opt-in test-only injection, including Bellman CPU-pool/scoped-worker panics and panic-payload destructor failure. Normal artifacts contain no injection export.

This PR includes 0e03e8e (the previously implemented atomic initializer) as a prerequisite, then the panic-boundary commit. It targets release_vm_zksnarks_4.1. Independent randomness, Merkle, and verification-state PRs are not modified; release integration must combine them and keep expected-error handling inside the ordinary Rust implementation. Sprout and Equihash bodies are unchanged.

Validation on macOS x86_64 / JDK 8:

  • Rust unit tests: 20 passed, 1 fixture-dependent test ignored by default; initialization is exercised by the JNI suites below.
  • Instrumented release JNI: 38 Java tests passed. Entry injection covers all 31 JNI methods. Post-computation injection covers real Spend/Output proving and verification, unchanged outputs, same-context retry, and final binding signatures compared with a fresh verification context.
  • All 3 panic scenarios also passed with java-tron's production parameter files/hashes.
  • Uninstrumented release JNI: 35 tests passed; Rust and JNI symbol checks confirm no injection exports.
  • Forced abort build fails with the intended compile-time diagnostic.
  • Both SDK CMake configurations generate successfully and isolate test artifacts; test-library installation is disabled. Local JNI builds linked the release Rust static library and an existing libsodium archive.

Companion JNI integration: https://github.com/tronprotocol/zksnark-java-sdk/pull/23 (required). The four packaged platform binaries are not rebuilt here. This contains unwinding Rust panics; invalid pointers, segfaults, OOM aborts, fatal panic hooks, and double panics during unwinding remain outside the guarantee. Details: librustzcash/FFI_PANIC_BOUNDARY.md.