bytecodealliance / wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
Apache License 2.0
4.78k stars 604 forks source link

While executing the given testcase by using absoulte or relative paths, the result are different #2633

Open luxinyi0105 opened 10 months ago

luxinyi0105 commented 10 months ago

Describe the bug

Recently, I discovered an interesting phenomenon. When I use wamr to execute the given testcase, using the absolute or relative paths of the testcase in the terminal can result in different execution results.

Actual behavior & Expected behavior

The executing results of using relative or absolute paths to execute the same testcase with wamr are shown in the figure below. And the results of using other runtime tools, such as wasmtime and wasmedge to execute the same testcase are also shown in the figure below.

截图 2023-10-11 18-48-55

Versions and Environment

Tools version: iwasm 1.2.3 Operating system: Ubuntu 22.04.1 Architecture: x86_64

Additional context

The given testcase was simply mutated a wasm file, which was obtained by compiling C program generated with Csmith using Emscripten Compiler (Emcc).

The original C program is c_file.c, the compilation results with Emscripten is wasm_file.wasm, and its wat format is wat_file.wat.

We mutated the wat file to change all its i32/i64.lt_s with i32/i64.gt_s, and change its i32.rem_u in line 12382 with i32.mul

The result after mutation is mutated_file.wat, and its wasm format is mutated_file.wasm.

wenyongh commented 10 months ago

Hi, the difference is caused by that the result of wasi API args_get is used to participate the calculation of the output result, the args_get's results of using absolute path and using relative path are different in WAMR, so we got different output results. If you apply the below patch, then you can got the same result.

diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c
index 8faa5e44..a4059f8e 100644
--- a/core/iwasm/common/wasm_runtime_common.c
+++ b/core/iwasm/common/wasm_runtime_common.c
@@ -2912,6 +2912,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
     char *path, resolved_path[PATH_MAX];
     uint32 i;

+    static char *my_argv_buf = "mutated_file.wasm";
+    argv[0] = my_argv_buf;
+

Per my understanding, absolute path and relative path are really different command line arguments, WAMR's handling should be reasonable, and the wasi document doesn't require that we need to convert the argument in the handling of args_get. I tested the cases with wasmer, the running results are the same with WAMR.