From 09c41cf831d47b28a0000154b62bbe66e8326b74 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 18 May 2020 14:48:55 -0700 Subject: [PATCH 1/3] Add some optional defines to disable wasm2c checks --- src/prebuilt/wasm2c.include.c | 10 +++++++++- src/prebuilt/wasm2c.include.h | 2 +- src/wasm2c.c.tmpl | 10 +++++++++- src/wasm2c.h.tmpl | 2 +- wasm2c/README.md | 11 +++++++++++ wasm2c/examples/fac/fac.c | 2 +- wasm2c/examples/fac/fac.h | 2 +- 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/prebuilt/wasm2c.include.c b/src/prebuilt/wasm2c.include.c index d7b1df23a..cfee519a9 100644 --- a/src/prebuilt/wasm2c.include.c +++ b/src/prebuilt/wasm2c.include.c @@ -1,6 +1,6 @@ /* Generated from 'wasm2c.c.tmpl' by wasm2c_tmpl.py, do not edit! */ const char SECTION_NAME(includes)[] = -"/* Automically generated by wasm2c */\n" +"/* Automatically generated by wasm2c */\n" "#include \n" "#include \n" ; @@ -11,9 +11,13 @@ const char SECTION_NAME(declarations)[] = "\n" "#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)\n" "\n" +"#ifndef WASM_RT_NO_STACK_DEPTH_CHECKS\n" "#define FUNC_PROLOGUE \\\n" " if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \\\n" " TRAP(EXHAUSTION)\n" +"#else\n" +"#define FUNC_PROLOGUE\n" +"#endif\n" "\n" "#define FUNC_EPILOGUE --wasm_rt_call_stack_depth\n" "\n" @@ -25,8 +29,12 @@ const char SECTION_NAME(declarations)[] = " ? ((t)table.data[x].func)(__VA_ARGS__) \\\n" " : TRAP(CALL_INDIRECT))\n" "\n" +"#ifndef WASM_RT_NO_MEMORY_CHECKS\n" "#define MEMCHECK(mem, a, t) \\\n" " if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB)\n" +"#else\n" +"#define MEMCHECK(mem, a, t)\n" +"#endif\n" "\n" "#define DEFINE_LOAD(name, t1, t2, t3) \\\n" " static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \\\n" diff --git a/src/prebuilt/wasm2c.include.h b/src/prebuilt/wasm2c.include.h index 4ba247516..55f856863 100644 --- a/src/prebuilt/wasm2c.include.h +++ b/src/prebuilt/wasm2c.include.h @@ -1,6 +1,6 @@ /* Generated from 'wasm2c.h.tmpl' by wasm2c_tmpl.py, do not edit! */ const char SECTION_NAME(top)[] = -"/* Automically generated by wasm2c */\n" +"/* Automatically generated by wasm2c */\n" "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n" diff --git a/src/wasm2c.c.tmpl b/src/wasm2c.c.tmpl index 4465cf9a0..c170f3b92 100644 --- a/src/wasm2c.c.tmpl +++ b/src/wasm2c.c.tmpl @@ -1,5 +1,5 @@ %%includes -/* Automically generated by wasm2c */ +/* Automatically generated by wasm2c */ #include #include %%declarations @@ -8,9 +8,13 @@ #define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0) +#ifndef WASM_RT_NO_STACK_DEPTH_CHECKS #define FUNC_PROLOGUE \ if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \ TRAP(EXHAUSTION) +#else +#define FUNC_PROLOGUE +#endif #define FUNC_EPILOGUE --wasm_rt_call_stack_depth @@ -22,8 +26,12 @@ ? ((t)table.data[x].func)(__VA_ARGS__) \ : TRAP(CALL_INDIRECT)) +#ifndef WASM_RT_NO_MEMORY_CHECKS #define MEMCHECK(mem, a, t) \ if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB) +#else +#define MEMCHECK(mem, a, t) +#endif #define DEFINE_LOAD(name, t1, t2, t3) \ static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ diff --git a/src/wasm2c.h.tmpl b/src/wasm2c.h.tmpl index 8478d81be..e029401ac 100644 --- a/src/wasm2c.h.tmpl +++ b/src/wasm2c.h.tmpl @@ -1,5 +1,5 @@ %%top -/* Automically generated by wasm2c */ +/* Automatically generated by wasm2c */ #ifdef __cplusplus extern "C" { #endif diff --git a/wasm2c/README.md b/wasm2c/README.md index 305ec01ee..2efcc36f1 100644 --- a/wasm2c/README.md +++ b/wasm2c/README.md @@ -436,3 +436,14 @@ $ wat-desugar fac-flat.wat --fold -o fac-folded.wat The formatting is different and the variable and function names are gone, but the structure is the same. + +## Optionally disabling checks + +By default wasm2c output will check all the things that WebAssembly VMs would, +like memory accesses being in bounds. You can optionally disable some of those +things, by defining the following things when compiling the C code: + +* `WASM_RT_NO_MEMORY_CHECKS`: Prevents checks from being emitted for memory + accesses being in bounds. +* `WASM_RT_NO_STACK_DEPTH_CHECKS`: Prevents checks from being emitted for + exhausting the stack. diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index acd8ed91f..bf5314064 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -1,4 +1,4 @@ -/* Automically generated by wasm2c */ +/* Automatically generated by wasm2c */ #include #include diff --git a/wasm2c/examples/fac/fac.h b/wasm2c/examples/fac/fac.h index 5e78f33bf..877c75d2c 100644 --- a/wasm2c/examples/fac/fac.h +++ b/wasm2c/examples/fac/fac.h @@ -1,6 +1,6 @@ #ifndef FAC_H_GENERATED_ #define FAC_H_GENERATED_ -/* Automically generated by wasm2c */ +/* Automatically generated by wasm2c */ #ifdef __cplusplus extern "C" { #endif From d003a9a554915374e0967e5a0ca8428ad9b801c1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 19 May 2020 13:40:12 -0700 Subject: [PATCH 2/3] eplilogue too --- src/prebuilt/wasm2c.include.c | 4 ++++ src/wasm2c.c.tmpl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/prebuilt/wasm2c.include.c b/src/prebuilt/wasm2c.include.c index cfee519a9..e410e0dcd 100644 --- a/src/prebuilt/wasm2c.include.c +++ b/src/prebuilt/wasm2c.include.c @@ -19,7 +19,11 @@ const char SECTION_NAME(declarations)[] = "#define FUNC_PROLOGUE\n" "#endif\n" "\n" +"#ifndef WASM_RT_NO_STACK_DEPTH_CHECKS\n" "#define FUNC_EPILOGUE --wasm_rt_call_stack_depth\n" +"#else\n" +"#define FUNC_EPILOGUE\n" +"#endif\n" "\n" "#define UNREACHABLE TRAP(UNREACHABLE)\n" "\n" diff --git a/src/wasm2c.c.tmpl b/src/wasm2c.c.tmpl index c170f3b92..87bca220e 100644 --- a/src/wasm2c.c.tmpl +++ b/src/wasm2c.c.tmpl @@ -16,7 +16,11 @@ #define FUNC_PROLOGUE #endif +#ifndef WASM_RT_NO_STACK_DEPTH_CHECKS #define FUNC_EPILOGUE --wasm_rt_call_stack_depth +#else +#define FUNC_EPILOGUE +#endif #define UNREACHABLE TRAP(UNREACHABLE) From 26e078e65514da7209445b8a6cd65598a663b160 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 19 May 2020 13:41:52 -0700 Subject: [PATCH 3/3] rename --- src/prebuilt/wasm2c.include.c | 2 +- src/wasm2c.c.tmpl | 2 +- wasm2c/README.md | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/prebuilt/wasm2c.include.c b/src/prebuilt/wasm2c.include.c index e410e0dcd..708a25993 100644 --- a/src/prebuilt/wasm2c.include.c +++ b/src/prebuilt/wasm2c.include.c @@ -33,7 +33,7 @@ const char SECTION_NAME(declarations)[] = " ? ((t)table.data[x].func)(__VA_ARGS__) \\\n" " : TRAP(CALL_INDIRECT))\n" "\n" -"#ifndef WASM_RT_NO_MEMORY_CHECKS\n" +"#ifndef WASM_RT_NO_MEMORY_BOUNDS_CHECKS\n" "#define MEMCHECK(mem, a, t) \\\n" " if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB)\n" "#else\n" diff --git a/src/wasm2c.c.tmpl b/src/wasm2c.c.tmpl index 87bca220e..215cf845a 100644 --- a/src/wasm2c.c.tmpl +++ b/src/wasm2c.c.tmpl @@ -30,7 +30,7 @@ ? ((t)table.data[x].func)(__VA_ARGS__) \ : TRAP(CALL_INDIRECT)) -#ifndef WASM_RT_NO_MEMORY_CHECKS +#ifndef WASM_RT_NO_MEMORY_BOUNDS_CHECKS #define MEMCHECK(mem, a, t) \ if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB) #else diff --git a/wasm2c/README.md b/wasm2c/README.md index 2efcc36f1..34b400dfe 100644 --- a/wasm2c/README.md +++ b/wasm2c/README.md @@ -443,7 +443,7 @@ By default wasm2c output will check all the things that WebAssembly VMs would, like memory accesses being in bounds. You can optionally disable some of those things, by defining the following things when compiling the C code: -* `WASM_RT_NO_MEMORY_CHECKS`: Prevents checks from being emitted for memory - accesses being in bounds. +* `WASM_RT_NO_MEMORY_BOUNDS_CHECKS`: Prevents checks from being emitted for + memory accesses being in bounds. * `WASM_RT_NO_STACK_DEPTH_CHECKS`: Prevents checks from being emitted for exhausting the stack.