Xuanwo opened a new issue, #8743: URL: https://github.com/apache/opendal/issues/8743
## Summary Auditing `bindings/haskell/` found several native resource bugs at the Rust↔Haskell FFI boundary. Every bug below is verifiable from the source; most leak on the hot path or free memory with the wrong allocator. ## Leaks of Rust allocations - **Every `FFIResult<T>::ok(data)` box leaks.** `ok()` wraps the payload in `Box::into_raw(Box::new(data))` but no Haskell or Rust code ever frees that box: `blocking_read` leaks `Box<ByteSlice>`, `blocking_is_exist` leaks `Box<bool>`, `blocking_stat`/`writer_close` leak `Box<Metadata>`, `lister_next`/`operator_info` leak `Box<*const c_char>`, and `blocking_list`/`blocking_scan`/`blocking_writer(_append)` leak a `Box<*mut T>` wrapper around the handle. - **Every error path leaks `error_message`.** `FFIResult::err*` sets `error_message` from `CString::into_raw`; Haskell only does `peekCString` (copy) and never frees it. ## Frees with the wrong allocator (UB) - `parseCString`/`parseString`/`parseFFIMetadata` in `OpenDAL.hs` call Haskell `free()` (libc `free`) on Rust `CString::into_raw` pointers: metadata fields, `lister_next` results, `operator_info` results. Rust allocations must be released by Rust code. - `blocking_write`/`writer_write` run `Vec::from_raw_parts(bytes as *mut u8, len, len)` on the **caller's** Haskell buffer. The `mem::forget` prevents a double-free, but the `Vec` still claims ownership of foreign memory (any later drop is UB) — it is only used for a `.clone()`. - `free_byteslice` rebuilds `Vec::from_raw_parts(ptr, len, len)`, but `ByteSlice::from_vec` keeps the original `Vec` capacity, which can exceed `len` (e.g. `Buffer::to_vec`); reconstructing with the wrong capacity is UB when dropped. For `len == 0` with a null or non-allocated pointer this is also UB. ## Panics across the FFI boundary (process abort) - `CString::new(..).unwrap()` in `FFIResult::err`, `FFIResult::err_with_source`, `operator_info` and `HsLogger::log` panics on error messages or paths containing interior NUL bytes. - `od::blocking::Operator::new(operator).unwrap()` in `via_map_ffi`. - `CStr::from_ptr` on null `scheme`/`path`/config key pointers is UB (Haskell never passes null, but the exported symbols accept it silently). ## Fix approach Make every allocation produced by Rust be freed by an exported Rust function: - `FFIResult<T>` conventions documented: handle results pass the boxed handle directly as `data_ptr` (no wrapper box); string results pass the `CString` pointer; inline values (`ByteSlice`, `Metadata`, `bool`) get a matching `free_*` export that releases payload + box. - New exports: `free_byteslice` (frees `Box<ByteSlice>` + buffer, rebuilt as `Box<[u8]>` so the allocation layout always matches), `free_bool`, `free_string`, `free_metadata`. - Haskell side releases resources with `finally`/`onException` so frees run even when an exception is thrown; error paths use a `parseError` helper that frees `error_message`. - Borrowed input buffers are copied with `slice::from_raw_parts(..).to_vec()` instead of fake `Vec` ownership; interior NUL bytes are escaped instead of panicking; null path/scheme inputs return `FFIResult::err`. - Added Rust unit tests for free/round-trip/empty/null/error paths and a Haskell `ResourceTest` suite (repeated ops, error paths, empty reads, lister exhaustion, `performGC` finalizer checks). Reported by OpenDAL maintenance tooling (AI-assisted). A PR implementing the above follows. -- 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]
