fix(JNI): resolve JNI code safety issues in native layer

FE
4d ago
+958-670

Summary

This PR fixes critical JNI code safety issues in the native C++ layer.

Changes

LibrustzcashJNIImpl.cpp (25 functions fixed)

  • Replace unused <iostream> with <cstring>
  • Fix null check ordering (move BEFORE function calls)
  • Add proper error cleanup with JNI_ABORT on error paths
  • Use modern C++ casts (reinterpret_cast, static_cast, const_cast)
  • Replace NULL with nullptr
  • Fix string length API (strlen vs GetStringLength)

LibsodiumJNIImpl.cpp (8 functions fixed)

  • Replace unused <iostream> with <cstring> and <new>
  • Use new (std::nothrow) to prevent unhandled exceptions
  • Add comprehensive null pointer checks and error cleanup
  • Modern C++ style casts

CMakeLists.txt

  • Add macOS-specific libc++ include path for Clang
  • Support architecture-specific install destinations

README.md

  • Update build instructions

Array contracts and platform validation

Validate fixed buffer widths and caller-supplied lengths before invoking Rust or libsodium. Fixed-width inputs are copied by region, variable-length inputs use read-only JNI leases, and outputs are copied back only for the specified prefix after success. Multiple outputs that require distinct storage are checked before calling the native function. Authentication failure leaves both the caller's plaintext buffer and reported length intact.

All 34 SDK tests passed on Linux and macOS, on both x86_64 and arm64. The four bundled libraries come from this fork build with Rust submodule 45f8049. Each artifact was checked against its platform and the 39 JNI exports, then checked byte-for-byte in the Maven JAR.

This PR and #19 are independently based on master and both replace the four bundled libraries. After the first merges, rebuild the remaining PR's libraries from the combined source and rerun both regression suites.

Both SDK builds currently pin Rust submodule 45f8049. To ship proof-failure recovery alongside these SDK changes, update the submodule after librustzcash #12 merges and rebuild all four libraries before releasing the SDK.