Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some optional defines to disable wasm2c trap checks #1432

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/prebuilt/wasm2c.include.c
Original file line number Diff line number Diff line change
@@ -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 <math.h>\n"
"#include <string.h>\n"
;
Expand All @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove here too? Probably doesn't hurt anything, but it seems odd to decrement the value here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, not sure how I missed that, thanks...

"\n"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/prebuilt/wasm2c.include.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 9 additions & 1 deletion src/wasm2c.c.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%includes
/* Automically generated by wasm2c */
/* Automatically generated by wasm2c */
#include <math.h>
#include <string.h>
%%declarations
Expand All @@ -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

Expand All @@ -22,8 +26,12 @@
? ((t)table.data[x].func)(__VA_ARGS__) \
: TRAP(CALL_INDIRECT))

#ifndef WASM_RT_NO_MEMORY_CHECKS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe WASM_RT_NO_MEMORY_BOUNDS_CHECKS instead? Not sure what other memory checks we may have, but seems better to be specific.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good.

#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) { \
Expand Down
2 changes: 1 addition & 1 deletion src/wasm2c.h.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%top
/* Automically generated by wasm2c */
/* Automatically generated by wasm2c */
#ifdef __cplusplus
extern "C" {
#endif
Expand Down
11 changes: 11 additions & 0 deletions wasm2c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion wasm2c/examples/fac/fac.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Automically generated by wasm2c */
/* Automatically generated by wasm2c */
#include <math.h>
#include <string.h>

Expand Down
2 changes: 1 addition & 1 deletion wasm2c/examples/fac/fac.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down