Xuanwo opened a new pull request, #8948:
URL: https://github.com/apache/opendal/pull/8948

   # Which issue does this PR close?
   
   N/A
   
   # Rationale for this change
   
   This PR fixes four soundness, resource-management, and metadata correctness 
bugs in `bindings/java`:
   
   1. **Unchecked JNI casts in `bindings/java/src/convert.rs`**: 
`jmap_to_hashmap` converted Java `Map` keys and values with `unsafe { 
JString::from_raw(...) }` without verifying that entries are non-null 
`java.lang.String` instances. Passing a raw or unchecked `Map` containing 
`null` or non-`String` entries triggers undefined behavior / JVM crashes (or 
fatal aborts under `-Xcheck:jni`). `read_string_field` also used an unnecessary 
`unsafe { JString::from_raw(...) }` cast.
   2. **Unbounded JNI local references in `OpenDAL.loadEnabledServices`**: 
`intern_load_enabled_services` in `bindings/java/src/utility.rs` allocated a 
`JString` local reference for every enabled service (47+ services) inside a 
loop without releasing each reference, exceeding the JNI local reference 
guarantee (16) and triggering HotSpot `-Xcheck:jni` warnings.
   3. **Stale hardcoded service list in `OpenDAL.enabledServices()`**: 
`intern_load_enabled_services` maintained a hardcoded `HashSet` of scheme 
constants that drifted from `bindings/java/Cargo.toml` and omitted enabled 
services (`gcs-grpc`, `goosefs`, `minio`, `r2`).
   4. **Native `blocking::Writer` leak on buffer OOM in 
`OperatorOutputStream`**: `OperatorOutputStream`'s constructor allocated the 
native `blocking::Writer` before allocating `this.bytes = new byte[maxBytes]`. 
If `new byte[maxBytes]` throws `OutOfMemoryError`, the native writer handle is 
never closed and leaks (keeping its runtime reference alive).
   
   # What changes are included in this PR?
   
   - `bindings/java/src/convert.rs`: Replace unchecked `unsafe { 
JString::from_raw(...) }` casts in `jmap_to_hashmap` with explicit null checks 
and `env.cast_local::<JString>(...)` returning `ErrorKind::ConfigInvalid` on 
`null` or non-`String` map keys/values, and replace the unchecked cast in 
`read_string_field` with `env.cast_local::<JString>(...)`.
   - `bindings/java/src/utility.rs`: Query 
`opendal::OperatorRegistry::get().schemes()` after calling 
`opendal::init_default_registry()` so `OpenDAL.enabledServices()` stays 
synchronized with the enabled service features, and wrap each loop-created 
`JString` in `.auto()` so local references are released per iteration.
   - 
`bindings/java/src/main/java/org/apache/opendal/OperatorOutputStream.java`: 
Validate `operator != null` and allocate `this.bytes = new byte[maxBytes]` 
before constructing the native `Writer`.
   - Tests (`JniMapValidationTest`, `UtilityTest`, 
`OperatorInputOutputStreamTest`, `ExecutorShutdownTest`): Add JUnit coverage 
for invalid/null map keys and values, `enabledServices()` completeness and 
`-Xcheck:jni` cleanliness, and `OperatorOutputStream` buffer allocation OOM 
without leaking native handles or creating objects.
   
   # Are there any user-facing changes?
   
   - Passing a `Map` with `null` or non-`String` keys/values into Java APIs 
that accept `Map<String, String>` now throws `OpenDALException` with 
`Code.ConfigInvalid` instead of risking a JVM crash.
   - `OpenDAL.enabledServices()` now reports all services registered by the 
compiled native library (including `gcs-grpc`, `goosefs`, `minio`, and `r2`).
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi
   - Model: Gemini 4 Neon
   - Effort: high
   - Role: Investigated the four JNI safety, local reference, service-registry, 
and resource-leak issues in `bindings/java`, implemented the Rust and Java 
fixes, added JUnit regression tests, and verified locally with formatting, 
clippy, spotless, and `-Xcheck:jni` test runs on Linux x86_64.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to