This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-ocaml-write-uaf-and-reader-eof in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 382d57ed4f4358726645ef9b7460decbe0afaadf Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 00:16:15 2026 +0100 fix(bindings/ocaml): copy write buffers and allow short reader reads Operator.write and Writer.write passed GC-managed OCaml bytes to core as a 'static slice. Core may retain the buffer after the call, so mutating or collecting the OCaml bytes corrupted or invalidated stored data. Copy the bytes before handing them to core. Reader.pread used an exact bounded range, so a buffer larger than the remaining bytes failed the completeness check, and the range end could overflow. Read through an unbounded range instead, which stops at EOF. Make create_dir return the bool its OCaml signature promises. --- bindings/ocaml/lib/operator.mli | 3 +- bindings/ocaml/src/operator/mod.rs | 14 ++++---- bindings/ocaml/src/operator/reader.rs | 12 ++++--- bindings/ocaml/src/operator/writer.rs | 6 ++-- bindings/ocaml/test/test.ml | 63 ++++++++++++++++++++++++++++++++++- 5 files changed, 82 insertions(+), 16 deletions(-) diff --git a/bindings/ocaml/lib/operator.mli b/bindings/ocaml/lib/operator.mli index 8522cceb39..b1cb9fcb04 100644 --- a/bindings/ocaml/lib/operator.mli +++ b/bindings/ocaml/lib/operator.mli @@ -178,7 +178,8 @@ val create_dir : Example: {[ match create_dir op "data/subdir/" with - | Ok () -> print_endline "Directory created" + | Ok true -> print_endline "Directory created" + | Ok false -> print_endline "Directory was not created" | Error err -> printf "Error: %s\n" err ]} *) diff --git a/bindings/ocaml/src/operator/mod.rs b/bindings/ocaml/src/operator/mod.rs index e67eba26fc..e2ac6ec973 100644 --- a/bindings/ocaml/src/operator/mod.rs +++ b/bindings/ocaml/src/operator/mod.rs @@ -83,8 +83,8 @@ pub fn blocking_is_exist(operator: &mut Operator, path: String) -> Result<bool, #[ocaml::func] #[ocaml::sig("operator -> string -> (bool, string) Result.t ")] -pub fn blocking_create_dir(operator: &mut Operator, path: String) -> Result<(), String> { - map_res_error(operator.0.create_dir(path.as_str())) +pub fn blocking_create_dir(operator: &mut Operator, path: String) -> Result<bool, String> { + map_res_error(operator.0.create_dir(path.as_str())).map(|()| true) } #[ocaml::func] @@ -104,12 +104,10 @@ pub fn blocking_reader( #[ocaml::func] #[ocaml::sig("operator -> string -> bytes -> (unit, string) Result.t ")] -pub fn blocking_write( - operator: &mut Operator, - path: String, - bs: &'static [u8], -) -> Result<(), String> { - map_res_error(operator.0.write(path.as_str(), bs).map(|_| ())) +pub fn blocking_write(operator: &mut Operator, path: String, bs: &[u8]) -> Result<(), String> { + // Core may retain the buffer after this call, but `bs` points into the OCaml + // heap and can be mutated or moved by the GC. Hand over an owned copy. + map_res_error(operator.0.write(path.as_str(), bs.to_vec()).map(|_| ())) } #[ocaml::func] diff --git a/bindings/ocaml/src/operator/reader.rs b/bindings/ocaml/src/operator/reader.rs index 2f069148d5..6a95c7cad6 100644 --- a/bindings/ocaml/src/operator/reader.rs +++ b/bindings/ocaml/src/operator/reader.rs @@ -15,12 +15,16 @@ // specific language governing permissions and limitations // under the License. +use std::io::Read; + use super::*; #[ocaml::func] #[ocaml::sig("reader -> bytes -> int64 -> (int, string) Result.t ")] -pub fn reader_pread(reader: &mut Reader, mut buf: &mut [u8], offset: u64) -> Result<usize, String> { - let size = buf.len(); - let n = map_res_error(reader.0.read_into(&mut buf, offset..offset + size as u64))?; - Ok(n) +pub fn reader_pread(reader: &mut Reader, buf: &mut [u8], offset: u64) -> Result<usize, String> { + // An unbounded range stops at EOF and returns a short read instead of failing + // the bounded-range completeness check when the buffer exceeds the remaining + // bytes. It also avoids computing `offset + buf.len()`, which can overflow. + let mut reader = map_res_error(reader.0.clone().into_std_read(offset..))?; + reader.read(buf).map_err(|err| err.to_string()) } diff --git a/bindings/ocaml/src/operator/writer.rs b/bindings/ocaml/src/operator/writer.rs index e78793bc1b..6ab3193ffb 100644 --- a/bindings/ocaml/src/operator/writer.rs +++ b/bindings/ocaml/src/operator/writer.rs @@ -19,8 +19,10 @@ use super::*; #[ocaml::func] #[ocaml::sig("writer -> bytes -> (unit, string) Result.t ")] -pub fn writer_write(writer: &mut Writer, bs: &'static [u8]) -> Result<(), String> { - map_res_error(writer.0.write(bs)) +pub fn writer_write(writer: &mut Writer, bs: &[u8]) -> Result<(), String> { + // Core may retain the buffer after this call, but `bs` points into the OCaml + // heap and can be mutated or moved by the GC. Hand over an owned copy. + map_res_error(writer.0.write(bs.to_vec())) } #[ocaml::func] diff --git a/bindings/ocaml/test/test.ml b/bindings/ocaml/test/test.ml index 909fe9915c..5d07c1f8b6 100644 --- a/bindings/ocaml/test/test.ml +++ b/bindings/ocaml/test/test.ml @@ -28,13 +28,20 @@ let new_test_block_operator test_ctxt : operator = let cfgs = [ ("root", bracket_tmpdir test_ctxt) ] in test_check_result (Operator.new_operator "fs" cfgs) +let new_test_memory_operator () : operator = + test_check_result (Operator.new_operator "memory" []) + +let read_string bo path = + test_check_result (Operator.read bo path) + |> Array.to_seq |> Bytes.of_seq |> Bytes.to_string + (** Basic operator creation tests *) let test_new_block_operator _ = ignore new_test_block_operator (** Test directory creation and recursive removal *) let test_create_dir_and_remove_all test_ctxt = let bo = new_test_block_operator test_ctxt in - ignore (test_check_result (Operator.create_dir bo "/testdir/")); + assert_equal true (test_check_result (Operator.create_dir bo "/testdir/")); ignore (test_check_result (Operator.write bo "/testdir/foo" (Bytes.of_string "bar"))); @@ -65,6 +72,32 @@ let test_operator_reader test_ctxt = assert_equal 5 i; assert_equal "world" (Bytes.to_string data) +(** Test that a read buffer larger than the remaining bytes returns the tail *) +let test_reader_pread_short_read test_ctxt = + let bo = new_test_block_operator test_ctxt in + ignore + (test_check_result + (Operator.write bo "tempfile" (Bytes.of_string "helloworld"))); + let reader = Operator.reader bo "tempfile" |> test_check_result in + let data = Bytes.make 64 '\000' in + let n = Operator.Reader.pread reader data 5L |> test_check_result in + assert_equal 5 n; + assert_equal "world" (Bytes.sub_string data 0 n) + +(** Test that reads at or beyond EOF return zero without failing *) +let test_reader_pread_eof test_ctxt = + let bo = new_test_block_operator test_ctxt in + ignore + (test_check_result + (Operator.write bo "tempfile" (Bytes.of_string "helloworld"))); + let reader = Operator.reader bo "tempfile" |> test_check_result in + let data = Bytes.make 8 'x' in + let n = Operator.Reader.pread reader data 10L |> test_check_result in + assert_equal 0 n; + assert_equal "xxxxxxxx" (Bytes.to_string data); + let n = Operator.Reader.pread reader data 100L |> test_check_result in + assert_equal 0 n + (** Test writer functionality *) let test_operator_writer test_ctxt = let bo = new_test_block_operator test_ctxt in @@ -82,6 +115,30 @@ let test_operator_writer test_ctxt = assert_equal "Hello World!" (data |> Array.to_seq |> Bytes.of_seq |> Bytes.to_string) +(** Test that write copies caller-owned bytes instead of retaining them *) +let test_write_copies_caller_bytes _ = + let bo = new_test_memory_operator () in + let data = Bytes.of_string "original" in + ignore (test_check_result (Operator.write bo "copy" data)); + Bytes.set data 0 'X'; + Gc.compact (); + assert_equal "original" (read_string bo "copy") + +(** Test that each writer chunk is copied before it is buffered *) +let test_writer_copies_caller_bytes _ = + let bo = new_test_memory_operator () in + let writer = Operator.writer bo "copy" |> test_check_result in + let first = Bytes.of_string "Hello " in + let second = Bytes.of_string "World!" in + ignore (test_check_result (Operator.Writer.write writer first)); + Bytes.set first 0 'J'; + Gc.compact (); + ignore (test_check_result (Operator.Writer.write writer second)); + Bytes.set second 0 'X'; + Gc.compact (); + ignore (test_check_result (Operator.Writer.close writer)); + assert_equal "Hello World!" (read_string bo "copy") + (** Test metadata functionality *) let test_operator_stat test_ctxt = let bo = new_test_block_operator test_ctxt in @@ -368,7 +425,11 @@ let suite = "test_create_dir_and_remove_all" >:: test_create_dir_and_remove_all; "test_block_write_and_read" >:: test_block_write_and_read; "test_operator_reader" >:: test_operator_reader; + "test_reader_pread_short_read" >:: test_reader_pread_short_read; + "test_reader_pread_eof" >:: test_reader_pread_eof; "test_operator_writer" >:: test_operator_writer; + "test_write_copies_caller_bytes" >:: test_write_copies_caller_bytes; + "test_writer_copies_caller_bytes" >:: test_writer_copies_caller_bytes; "test_operator_stat" >:: test_operator_stat; "test_list" >:: test_list; "test_lister" >:: test_lister;
