Xuanwo opened a new pull request, #8744: URL: https://github.com/apache/opendal/pull/8744
# Which issue does this PR close? Closes #8743. # Rationale for this change An audit of `bindings/haskell/` found that almost every Rust allocation crossing the FFI boundary either leaked or was released by the wrong allocator: - `FFIResult::ok` boxed every payload into `data_ptr`, and nothing ever freed that box — leaked on every successful call (`ByteSlice`, `bool`, `Metadata`, `*mut Lister`, `*mut Writer`, `*const c_char`, plus a `Box<*mut T>` wrapper for handles). - `error_message` (`CString::into_raw`) leaked on every error path; Haskell only `peekCString`ed it. - Haskell's `parseCString`/`parseString`/`parseFFIMetadata` called C `free()` on Rust `CString`s — allocator mismatch (UB). - `blocking_write`/`writer_write` built `Vec::from_raw_parts` out of the caller's Haskell buffer; only `mem::forget` prevented a double-free. - `free_byteslice` rebuilt `Vec::from_raw_parts(ptr, len, len)` with `capacity == len`, which does not match the original allocation when the source `Vec` was over-capacity, and is UB for `len == 0`. - `CString::new(...).unwrap()` in `FFIResult::err*`, `operator_info`, and `HsLogger::log` would panic across FFI (process abort) on interior NUL bytes; `od::blocking::Operator::new(...).unwrap()` had the same problem; null `path`/`scheme` pointers hit UB in `CStr::from_ptr`. # What changes are included in this PR? - `result.rs`: `FFIResult` gains an explicit ownership contract documented on the type, `ok_with_ptr` (data_ptr *is* the resource) and `none()` constructors, a `Default` for out-param init, and NUL-safe error construction via `into_raw_c_string`. - `types.rs`: `ByteSlice` now wraps `Box<[u8]>` so the recorded `len` always equals the allocation length. New exports `free_byteslice` (buffer + box), `free_bool`, `free_string`, `free_metadata` (all fields + box). `leak_str` replaced by `into_raw_c_string` which escapes interior NULs. Removed the dead, unsound `From<&mut ByteSlice> for Vec<u8>`. - `lib.rs`: `blocking_list`/`blocking_scan`/`blocking_writer`/`blocking_writer_append` pass the boxed `Lister`/`Writer` directly as `data_ptr` (freed by the existing `free_lister`/`free_writer` finalizers); `lister_next`/`operator_info` pass the `CString` as `data_ptr`; `blocking_write`/`writer_write` copy borrowed input via `slice::from_raw_parts(..).to_vec()`; null `scheme`/`path`/`keys`/`values` inputs return `FFIResult::err`; `blocking::Operator::new` errors are reported instead of unwrapped. - `OpenDAL.hs`/`FFI.hs`: every error path uses a `parseError` helper that frees `error_message` via Rust `free_string`; success paths release `free_byteslice`/`free_bool`/`free_metadata`/`free_string` under `finally`, and `ForeignPtr` creation uses `onException` so a thrown exception cannot orphan the handle; no Rust allocation is ever freed with C `free()` any more. - Tests: 17 new Rust unit tests covering free/round-trip/empty/null/error paths end-to-end through the exported symbols against the memory service; a new `ResourceTest` Haskell suite (7 cases) hammering repeated reads, error paths, empty reads, stats, lister exhaustion, writer close, and `performGC` finalizers — mismatched allocators or leaks surface as crashes/heap corruption there. # Are there any user-facing changes? No public Haskell or Rust API changes. The exported C symbols `free_byteslice` (signature changed to take the `ByteSlice*` out pointer) and `lister_next`/`operator_info`/`blocking_list`/`blocking_scan`/`blocking_writer` result layouts changed, but these are internal to this binding crate — the cabal package and the Rust `cdylib` are versioned and built together. User-visible behavior is unchanged except that leaked memory is now freed. # Breaking changes # AI Usage Statement - Harness: pi coding agent (T3 Code thread, delegated implementation subagent). - Model: devin/swe-2. - Effort: max reasoning. - Role: AI performed the FFI ownership audit, implemented both the Rust and Haskell changes, wrote the new tests, and verified them. Assumption for review: `ByteSlice`'s payload is always produced by `ByteSlice::from_vec` (now `Box<[u8]>`), which is true for all `FFIResult<ByteSlice>` producers in this crate. # Verification - `cargo clippy --all-targets -- -D warnings` — clean. - `cargo fmt --check` — clean. - `cargo test` — 23/23 pass (17 new FFI cleanup tests + 6 pre-existing/new result+types tests). - `cabal test` in Docker (`ubuntu-24.04`, ghcup GHC 9.4.8 + cabal, same as `ci_bindings_haskell.yml`) — all 36 tests pass, including 7 new `ResourceTest` cases. - `cabal build exe:getting-started` + direct run of the linked binary — prints expected output (`read 13 bytes`, `size = 13 bytes`), matching the CI step's tolerated register failure. -- 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]
