Skip to content

Commit

Permalink
Fix argument passing
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Herczeg [email protected]
  • Loading branch information
zherczeg committed Dec 10, 2024
1 parent 28d164d commit 13018e0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
33 changes: 16 additions & 17 deletions src/wasi/WASI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ void WASI::args_get(ExecutionState& state, Value* argv, Value* result, Instance*

TemporaryData<void*, 8> pointers(argc);

if (pointers.data() == nullptr) {
result[0] = Value(WasiErrNo::inval);
return;
}

char** data = reinterpret_cast<char**>(pointers.data());
result[0] = Value(static_cast<uint16_t>(uvwasi_args_get(WASI::g_uvwasi, data, uvArgBuf)));
uvwasi_errno_t error = uvwasi_args_get(WASI::g_uvwasi, data, uvArgBuf);

for (uvwasi_size_t i = 0; i < argc; i++) {
uvArgv[i] = data[i] - uvArgBuf;
if (error == WasiErrNo::success) {
char* buffer = reinterpret_cast<char*>(instance->memory(0)->buffer());
for (uvwasi_size_t i = 0; i < argc; i++) {
uvArgv[i] = data[i] - buffer;
}
}

result[0] = Value(error);
}

void WASI::args_sizes_get(ExecutionState& state, Value* argv, Value* result, Instance* instance)
Expand Down Expand Up @@ -293,18 +293,17 @@ void WASI::environ_get(ExecutionState& state, Value* argv, Value* result, Instan

TemporaryData<void*, 8> pointers(count);

if (pointers.data() == nullptr) {
result[0] = Value(WasiErrNo::inval);
return;
}

char** data = reinterpret_cast<char**>(pointers.data());
result[0] = Value(static_cast<uint16_t>(uvwasi_environ_get(WASI::g_uvwasi, data, uvEnvironBuf)));
uvwasi_errno_t error = uvwasi_environ_get(WASI::g_uvwasi, data, uvEnvironBuf);

char* buffer = reinterpret_cast<char*>(instance->memory(0)->buffer());
for (uvwasi_size_t i = 0; i < count; i++) {
uvEnviron[i] = data[i] - buffer;
if (error == WasiErrNo::success) {
char* buffer = reinterpret_cast<char*>(instance->memory(0)->buffer());
for (uvwasi_size_t i = 0; i < count; i++) {
uvEnviron[i] = data[i] - buffer;
}
}

result[0] = Value(error);
}

void WASI::random_get(ExecutionState& state, Value* argv, Value* result, Instance* instance)
Expand Down
44 changes: 32 additions & 12 deletions test/wasi/args.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
(func (export "check_args")(result i32)
(local $i i32)
(local $mismatched_char_count i32)
(local $argc i32)
(local $argv i32)
(local $argv_buf i32)
(local $argv_buf_size i32)
Expand All @@ -22,7 +21,6 @@

;; Memory[0] = args count
i32.const 0
local.tee $argc
;; Memory[4] = args size in characters
i32.const 4
local.tee $argv_buf_size
Expand All @@ -35,6 +33,19 @@
return
)
)

;; Memory[0] = args count
i32.const 0
i32.load
i32.const 4
i32.ne
(if
(then
i32.const -2
return
)
)

;; Memory[128] = argv[] (list of pointers to the strings)
i32.const 128
local.tee $argv
Expand All @@ -46,7 +57,7 @@
i32.ne
(if
(then
i32.const -2
i32.const -3
return
)
)
Expand All @@ -56,10 +67,22 @@
i32.const 4
i32.add
i32.load ;; &argv[1]
local.tee $i ;; start $i with the offset of the first char of argv[1]
local.get $argv_buf
i32.add ;; pointer to fist char in the buffer
local.set $value_addr
local.tee $value_addr
;; Memory[192] = argv_buf and 69 sizeof expected input, 192 - 69 = 123
i32.const 123
i32.sub
local.get $argv_buf_size
i32.load
i32.ne
(if
(then
i32.const -4
return
)
)

i32.const 69
local.set $i

(loop $for_each_char
;; *($expected_addr) != *($value_addr)
Expand Down Expand Up @@ -90,13 +113,10 @@

local.get $i
i32.const 1
i32.add
i32.sub
local.tee $i

;; $i < $argv_buf_size
local.get $argv_buf_size
i32.load
i32.lt_u
;; $i > 0
br_if $for_each_char
)
local.get $mismatched_char_count
Expand Down

0 comments on commit 13018e0

Please sign in to comment.