Skip to content

Commit

Permalink
Fix filedelta type in fd_seek
Browse files Browse the repository at this point in the history
Also add test.

Signed-off-by: Ádám Kulcsár <[email protected]>
  • Loading branch information
kulcsaradam committed Jan 20, 2025
1 parent acf00db commit 4109620
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wasi/WASI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void WASI::fd_prestat_dir_name(ExecutionState& state, Value* argv, Value* result
void WASI::fd_seek(ExecutionState& state, Value* argv, Value* result, Instance* instance)
{
uint32_t fd = argv[0].asI32();
uint64_t fileDelta = argv[1].asI32();
int64_t fileDelta = argv[1].asI64();
uint32_t whence = argv[2].asI32();
uvwasi_filesize_t* file_size = reinterpret_cast<uvwasi_filesize_t*>(get_memory_pointer(instance, argv[3], sizeof(uvwasi_filesize_t)));

Expand Down
82 changes: 82 additions & 0 deletions test/wasi/fd_seek.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(module
(import "wasi_snapshot_preview1" "path_open" (func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_write" (func $wasi_fd_write (param i32 i32 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_read" (func $wasi_fd_read (param i32 i32 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_seek" (func $wasi_fd_seek (param i32 i64 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32)))


(memory 1)

(export "memory" (memory 0))
(export "read_from_file_with_seek" (func $read_from_file_with_seek))
(data (i32.const 300) "./text.txt")

(; This test searches for the file 'text.txt' in the first opened directory
and reads 40 characters out of it.
It will only read from the file if the directories were mapped correctly ;)

(func $read_from_file_with_seek (param i64 i32)

i32.const 3 ;; Directory file descriptior, by default 3 is the first opened directory
i32.const 1 ;;lookupflags: directory
i32.const 300 ;; Offset of file name in memory
i32.const 19 ;; Length of file name
i32.const 0 ;; oflags: none
i64.const 4102 ;; rights: path_open, fd_read, fd_seek
i64.const 4102 ;; rights_inheriting: path_open, fd_read
i32.const 0 ;; fdflags: none
i32.const 0 ;; Offset to store at the opened file descriptor in memory
call $path_open

i32.eqz ;; fail if file could not be opened
(if
(then)
(else
i32.const 1
call $proc_exit
)
)

(i32.store (i32.const 104) (i32.const 6))
(i32.store (i32.const 100) (i32.const 0))
(i32.store (i32.const 200) (i32.const 500))

(call $wasi_fd_seek
(i32.const 0)
(i32.load)
(local.get 0)
(local.get 1)
(i32.const 1000)
)
drop

(call $wasi_fd_read
(i32.const 0)
(i32.load) ;; opened file descriptor

(i32.const 100) ;; store content at this location
(i32.const 1) ;; make it into a single buffer
(i32.const 200) ;; store number of read characters to this location
)
drop

(call $wasi_fd_write
(i32.const 1) ;;file descriptor
(i32.const 100) ;;offset of str offset
(i32.const 1) ;;iovec length
(i32.const 200) ;;result offset
)
drop
)
)

(assert_return (invoke "read_from_file_with_seek" (i64.const 6) (i32.const 0) ))
(assert_return (invoke "read_from_file_with_seek" (i64.const -6) (i32.const 0) ))

(assert_return (invoke "read_from_file_with_seek" (i64.const 6) (i32.const 1) ))
(assert_return (invoke "read_from_file_with_seek" (i64.const -6) (i32.const 1) ))

(assert_return (invoke "read_from_file_with_seek" (i64.const -12) (i32.const 2) ))
(assert_return (invoke "read_from_file_with_seek" (i64.const -6) (i32.const 2) ))

2 changes: 2 additions & 0 deletions test/wasi/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello
World

0 comments on commit 4109620

Please sign in to comment.