From 5c524ff7eb1e0964478f147dfdbd0e5b6d083470 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 27 Mar 2022 11:10:24 +0000 Subject: [PATCH] Fix fd_seek for boundary parameters --- src/wasi_snapshot_preview1.rs | 4 +- tests/run-make/seek/Makefile | 12 ++++++ tests/run-make/seek/fixtures/file1.txt | 2 + tests/run-make/seek/main.c | 59 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/run-make/seek/Makefile create mode 100644 tests/run-make/seek/fixtures/file1.txt create mode 100644 tests/run-make/seek/main.c diff --git a/src/wasi_snapshot_preview1.rs b/src/wasi_snapshot_preview1.rs index 7e0a48b..fa0ce03 100644 --- a/src/wasi_snapshot_preview1.rs +++ b/src/wasi_snapshot_preview1.rs @@ -488,11 +488,11 @@ pub(crate) unsafe fn fd_seek( ) -> Result { let fd = fs.get_backing_fd(fd)?; fn compute_new_offset(base: usize, offset: Filedelta) -> Result { - let new_offset = if offset > 0 { + let new_offset = if offset >= 0 { base + (offset as usize) } else { let neg_offset = (-offset) as usize; - if neg_offset < base { + if neg_offset <= base { base - neg_offset } else { return Err(wasi::ERRNO_INVAL.into()); diff --git a/tests/run-make/seek/Makefile b/tests/run-make/seek/Makefile new file mode 100644 index 0000000..3881910 --- /dev/null +++ b/tests/run-make/seek/Makefile @@ -0,0 +1,12 @@ +-include ../tools.mk + +objs = $(TMPDIR)/main.c.o + +check: $(objs) + $(CC) $(LDFLAGS) $(objs) $(LIB_WASI_VFS) -o $(TMPDIR)/main.wasm + wasmtime --mapdir /fixtures::./fixtures $(TMPDIR)/main.wasm + $(WASI_VFS_CLI) pack $(TMPDIR)/main.wasm --mapdir /fixtures::./fixtures -o $(TMPDIR)/main.packed.wasm + $(WASI_RUN) $(TMPDIR)/main.packed.wasm + +clean: + rm -rf $(PROG) $(objs) diff --git a/tests/run-make/seek/fixtures/file1.txt b/tests/run-make/seek/fixtures/file1.txt new file mode 100644 index 0000000..1c89bfb --- /dev/null +++ b/tests/run-make/seek/fixtures/file1.txt @@ -0,0 +1,2 @@ +file1.1 +file1.2 diff --git a/tests/run-make/seek/main.c b/tests/run-make/seek/main.c new file mode 100644 index 0000000..492f7a3 --- /dev/null +++ b/tests/run-make/seek/main.c @@ -0,0 +1,59 @@ +#include "../check.h" +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int err; + int file1 = open("/fixtures/file1.txt", O_RDONLY); + assert(file1 != -1); + + struct stat st; + err = fstat(file1, &st); + assert(err == 0); + + char buffer[st.st_size]; + + __wasi_filesize_t off; + + // reset the cursor + assert(__wasi_fd_seek(file1, 0, SEEK_SET, &off) == 0); + assert(off == 0); + + // keep the initial cursor position + assert(__wasi_fd_seek(file1, 0, SEEK_CUR, &off) == 0); + assert(off == 0); + assert(read(file1, buffer, st.st_size) == st.st_size); + + assert(__wasi_fd_seek(file1, 2, SEEK_SET, &off) == 0); + assert(off == 2); + assert(read(file1, buffer, st.st_size) == st.st_size - 2); + + assert(__wasi_fd_seek(file1, st.st_size, SEEK_SET, &off) == 0); + assert(off == st.st_size); + assert(read(file1, buffer, 1) == 0); + + assert(__wasi_fd_seek(file1, st.st_size + 1, SEEK_SET, &off) == 0); + assert(err == 0); + assert(read(file1, buffer, 1) == 0); + + // reset the cursor + assert(__wasi_fd_seek(file1, 0, SEEK_SET, &off) == 0); + + assert(__wasi_fd_seek(file1, 0, SEEK_END, &off) == 0); + assert(err == 0); + assert(read(file1, buffer, 1) == 0); + + assert(__wasi_fd_seek(file1, -1, SEEK_END, &off) == 0); + assert(err == 0); + assert(read(file1, buffer, 1) == 1); + + assert(__wasi_fd_seek(file1, -st.st_size, SEEK_END, &off) == 0); + assert(err == 0); + assert(off == 0); + assert(read(file1, buffer, st.st_size) == st.st_size); + + return 0; +}