Skip to content

Commit

Permalink
[3.15] backport #10333 (#10431)
Browse files Browse the repository at this point in the history
* test: demonstrate overflow with sendfile (#10334)

See #10333

Signed-off-by: Etienne Millon <[email protected]>

* fix: use `Long_val` for sendfile() parameters to fix file copying in docker (#10333)

* fix: sendfile() in docker

Signed-off-by: Haoxiang Fei <[email protected]>

* use ssize_t for return value

Co-authored-by: Etienne Millon <[email protected]>
Signed-off-by: Haoxiang Fei <[email protected]>

* update test

Signed-off-by: Etienne Millon <[email protected]>

* Add changelog

Signed-off-by: Etienne Millon <[email protected]>

---------

Signed-off-by: Haoxiang Fei <[email protected]>
Signed-off-by: Haoxiang Fei <[email protected]>
Signed-off-by: Etienne Millon <[email protected]>
Co-authored-by: Etienne Millon <[email protected]>
Co-authored-by: Etienne Millon <[email protected]>

* fix formatting (#10356)

Signed-off-by: Etienne Millon <[email protected]>

* move changelog entry to the right place (#10375)

Signed-off-by: Etienne Millon <[email protected]>

---------

Signed-off-by: Etienne Millon <[email protected]>
Signed-off-by: Haoxiang Fei <[email protected]>
Signed-off-by: Haoxiang Fei <[email protected]>
Co-authored-by: Haoxiang Fei <[email protected]>
  • Loading branch information
emillon and tonyfettes authored Apr 17, 2024
1 parent 95f208c commit f434ff8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/changes/10333.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- fix overflow in sendfile stubs (copy of large files could fail or end with
truncated files) (#10333, @tonyfettes)
6 changes: 3 additions & 3 deletions otherlibs/stdune/src/copyfile_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ CAMLprim value stdune_copyfile(value v_from, value v_to) {
caml_failwith("copyfile: only on macos");
}

static int dune_sendfile(int in, int out, int length) {
int ret;
static int dune_sendfile(int in, int out, size_t length) {
ssize_t ret;
while (length > 0) {
ret = sendfile(out, in, NULL, length);
if (ret < 0) {
Expand All @@ -90,7 +90,7 @@ CAMLprim value stdune_sendfile(value v_in, value v_out, value v_size) {
caml_release_runtime_system();
/* TODO Use copy_file_range once we have a good mechanism to test for its
* existence */
int ret = dune_sendfile(FD_val(v_in), FD_val(v_out), Int_val(v_size));
int ret = dune_sendfile(FD_val(v_in), FD_val(v_out), Long_val(v_size));
caml_acquire_runtime_system();
if (ret < 0) {
uerror("sendfile", Nothing);
Expand Down
8 changes: 8 additions & 0 deletions test/blackbox-tests/test-cases/dune
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
(applies_to fdo)
(enabled_if false))

; This test copies a large file, which is too slow to be enabled all the time.
; The source file is actually sparse, but sendfile seems to do a full copy
; nevertheless.

(cram
(applies_to sendfile-large-file)
(enabled_if false))

; This test is flaky

(cram
Expand Down
22 changes: 22 additions & 0 deletions test/blackbox-tests/test-cases/sendfile-large-file.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
We create a large file and check that it is copied completely.

$ cat > dune-project << EOF
> (lang dune 1.0)
> EOF

$ cat > create.ml << EOF
> let () = Unix.truncate "file.dat" 0x1_00_00_00_03
> EOF
$ touch file.dat
$ ocaml unix.cma create.ml
$ rm create.ml

$ dune build file.dat

$ dune_cmd stat size file.dat
4294967299

$ dune_cmd stat size _build/default/file.dat
4294967299

(3 indicates that the file size is taken modulo 2**32)

0 comments on commit f434ff8

Please sign in to comment.