diff --git a/src/wasi/WASI.cpp b/src/wasi/WASI.cpp index 7c04d543b..735673a29 100644 --- a/src/wasi/WASI.cpp +++ b/src/wasi/WASI.cpp @@ -118,17 +118,17 @@ void WASI::args_get(ExecutionState& state, Value* argv, Value* result, Instance* TemporaryData pointers(argc); - if (pointers.data() == nullptr) { - result[0] = Value(WasiErrNo::inval); - return; - } - char** data = reinterpret_cast(pointers.data()); - result[0] = Value(static_cast(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(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) @@ -293,18 +293,17 @@ void WASI::environ_get(ExecutionState& state, Value* argv, Value* result, Instan TemporaryData pointers(count); - if (pointers.data() == nullptr) { - result[0] = Value(WasiErrNo::inval); - return; - } - char** data = reinterpret_cast(pointers.data()); - result[0] = Value(static_cast(uvwasi_environ_get(WASI::g_uvwasi, data, uvEnvironBuf))); + uvwasi_errno_t error = uvwasi_environ_get(WASI::g_uvwasi, data, uvEnvironBuf); - char* buffer = reinterpret_cast(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(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) diff --git a/test/wasi/args.wast b/test/wasi/args.wast index 2efd65ddc..ade9477ed 100644 --- a/test/wasi/args.wast +++ b/test/wasi/args.wast @@ -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) @@ -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 @@ -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 @@ -46,7 +57,7 @@ i32.ne (if (then - i32.const -2 + i32.const -3 return ) ) @@ -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) @@ -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