diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary-reader-ast.c | 41 | ||||
-rw-r--r-- | src/wasm-binary-reader-ast.h | 1 | ||||
-rw-r--r-- | src/wasm-binary-reader-interpreter.c | 52 | ||||
-rw-r--r-- | src/wasm-binary-reader-interpreter.h | 1 | ||||
-rw-r--r-- | src/wasm-common.c | 54 | ||||
-rw-r--r-- | src/wasm-common.h | 47 | ||||
-rw-r--r-- | src/wasm-interp-sq.c | 6 | ||||
-rw-r--r-- | src/wasm-interp.c | 9 | ||||
-rw-r--r-- | src/wasm-stack-allocator.c | 20 | ||||
-rw-r--r-- | src/wasm-wast.c | 5 |
10 files changed, 139 insertions, 97 deletions
diff --git a/src/wasm-binary-reader-ast.c b/src/wasm-binary-reader-ast.c index acbe2422..7ff795b2 100644 --- a/src/wasm-binary-reader-ast.c +++ b/src/wasm-binary-reader-ast.c @@ -26,6 +26,14 @@ #include "wasm-binary-reader.h" #include "wasm-common.h" +#define LOG 0 + +#if LOG +#define LOGF(...) fprintf(stderr, __VA_ARGS__) +#else +#define LOGF(...) (void)0 +#endif + #define CHECK_ALLOC_(ctx, cond) \ do { \ if (!(cond)) { \ @@ -64,9 +72,6 @@ } \ } while (0) -#define LOG 0 -#define UNKNOWN_OFFSET ((uint32_t)~0) - typedef struct WasmExprNode { WasmExpr* expr; uint32_t index; @@ -79,6 +84,7 @@ WASM_DEFINE_VECTOR(uint32, WasmUint32); typedef struct WasmContext { WasmAllocator* allocator; + WasmBinaryErrorHandler* error_handler; WasmModule* module; WasmFunc* current_func; @@ -104,7 +110,7 @@ static void on_error(uint32_t offset, const char* message, void* user_data); static void print_error(WasmContext* ctx, const char* format, ...) { WASM_SNPRINTF_ALLOCA(buffer, length, format); - on_error(UNKNOWN_OFFSET, buffer, ctx); + on_error(WASM_UNKNOWN_OFFSET, buffer, ctx); } static WasmResult dup_name(WasmContext* ctx, @@ -154,10 +160,11 @@ static uint32_t translate_depth(WasmContext* ctx, uint32_t depth) { } void on_error(uint32_t offset, const char* message, void* user_data) { - if (offset == UNKNOWN_OFFSET) - fprintf(stderr, "error: %s\n", message); - else - fprintf(stderr, "error: @0x%08x: %s\n", offset, message); + WasmContext* ctx = user_data; + if (ctx->error_handler->on_error) { + ctx->error_handler->on_error(offset, message, + ctx->error_handler->user_data); + } } static WasmResult begin_memory_section(void* user_data) { @@ -349,9 +356,7 @@ static WasmResult begin_function_body(uint32_t index, void* user_data) { WasmContext* ctx = user_data; assert(index < ctx->module->funcs.size); ctx->current_func = ctx->module->funcs.data[index]; -#if LOG - fprintf(stderr, "func %d\n", index); -#endif + LOGF("func %d\n", index); return WASM_OK; } @@ -391,9 +396,7 @@ static WasmResult reduce(WasmContext* ctx, WasmExpr* expr) { while (!done) { done = WASM_TRUE; if (ctx->expr_stack.size == 0) { -#if LOG - fprintf(stderr, "reduce: <- %u\n", expr->type); -#endif + LOGF("reduce: <- %u\n", expr->type); WasmExprPtr* expr_ptr = wasm_append_expr_ptr(ctx->allocator, &ctx->current_func->exprs); @@ -403,10 +406,8 @@ static WasmResult reduce(WasmContext* ctx, WasmExpr* expr) { WasmExprNode* top = &ctx->expr_stack.data[ctx->expr_stack.size - 1]; assert(top->index < top->total); -#if LOG - fprintf(stderr, "reduce: %d(%d/%d) <- %d\n", top->expr->type, top->index, + LOGF("reduce: %d(%d/%d) <- %d\n", top->expr->type, top->index, top->total, expr->type); -#endif switch (top->expr->type) { case WASM_EXPR_TYPE_BINARY: @@ -574,9 +575,7 @@ static WasmResult reduce(WasmContext* ctx, WasmExpr* expr) { static WasmResult shift(WasmContext* ctx, WasmExpr* expr, uint32_t count) { ctx->just_pushed_fake_depth = WASM_FALSE; if (count > 0) { -#if LOG - fprintf(stderr, "shift: %d %u\n", expr->type, count); -#endif + LOGF("shift: %d %u\n", expr->type, count); WasmExprNode* node = wasm_append_expr_node(ctx->allocator, &ctx->expr_stack); CHECK_ALLOC_NULL(ctx, node); @@ -1147,10 +1146,12 @@ WasmResult wasm_read_binary_ast(struct WasmAllocator* allocator, const void* data, size_t size, const WasmReadBinaryOptions* options, + WasmBinaryErrorHandler* error_handler, struct WasmModule* out_module) { WasmContext ctx; WASM_ZERO_MEMORY(ctx); ctx.allocator = allocator; + ctx.error_handler = error_handler; ctx.module = out_module; WasmBinaryReader reader; diff --git a/src/wasm-binary-reader-ast.h b/src/wasm-binary-reader-ast.h index dfe8c2b4..5b1f73d8 100644 --- a/src/wasm-binary-reader-ast.h +++ b/src/wasm-binary-reader-ast.h @@ -28,6 +28,7 @@ WasmResult wasm_read_binary_ast(struct WasmAllocator* allocator, const void* data, size_t size, const struct WasmReadBinaryOptions* options, + WasmBinaryErrorHandler*, struct WasmModule* out_module); WASM_EXTERN_C_END diff --git a/src/wasm-binary-reader-interpreter.c b/src/wasm-binary-reader-interpreter.c index cd8ab345..de18f6dd 100644 --- a/src/wasm-binary-reader-interpreter.c +++ b/src/wasm-binary-reader-interpreter.c @@ -27,6 +27,12 @@ #define LOG 0 +#if LOG +#define LOGF(...) fprintf(stderr, __VA_ARGS__) +#else +#define LOGF(...) (void)0 +#endif + #define INVALID_FUNC_INDEX ((uint32_t)~0) #define CHECK_ALLOC_(ctx, cond) \ @@ -156,6 +162,7 @@ WASM_DEFINE_ARRAY(interpreter_func, WasmInterpreterFunc); typedef struct WasmContext { WasmAllocator* allocator; + WasmBinaryErrorHandler* error_handler; WasmAllocator* memory_allocator; WasmInterpreterModule* module; WasmInterpreterFuncArray funcs; @@ -450,10 +457,8 @@ static WasmResult push_depth_with_offset(WasmContext* ctx, node->type = type; node->value_stack_size = ctx->value_stack_size; node->offset = offset; -#if LOG - fprintf(stderr, " (%d): push depth %" PRIzd ":%s\n", ctx->value_stack_size, - ctx->depth_stack.size - 1, s_type_names[type]); -#endif + LOGF(" (%d): push depth %" PRIzd ":%s\n", ctx->value_stack_size, + ctx->depth_stack.size - 1, s_type_names[type]); return WASM_OK; } @@ -462,10 +467,8 @@ static WasmResult push_depth(WasmContext* ctx, WasmType type) { } static void pop_depth(WasmContext* ctx) { -#if LOG - fprintf(stderr, " (%d): pop depth %" PRIzd "\n", ctx->value_stack_size, - ctx->depth_stack.size - 1); -#endif + LOGF(" (%d): pop depth %" PRIzd "\n", ctx->value_stack_size, + ctx->depth_stack.size - 1); assert(ctx->depth_stack.size > 0); ctx->depth_stack.size--; /* reduce the depth_fixups stack as well, but it may be smaller than @@ -502,10 +505,11 @@ static uint32_t translate_local_index(WasmContext* ctx, uint32_t local_index) { } void on_error(uint32_t offset, const char* message, void* user_data) { - if (offset == WASM_INVALID_OFFSET) - fprintf(stderr, "error: %s\n", message); - else - fprintf(stderr, "error: @0x%08x: %s\n", offset, message); + WasmContext* ctx = user_data; + if (ctx->error_handler->on_error) { + ctx->error_handler->on_error(offset, message, + ctx->error_handler->user_data); + } } static WasmResult on_memory_initial_size_pages(uint32_t pages, @@ -608,9 +612,7 @@ static WasmResult on_function_bodies_count(uint32_t count, void* user_data) { } static WasmResult begin_function_body(uint32_t index, void* user_data) { -#if LOG - fprintf(stderr, "*** func %d ***\n", index); -#endif + LOGF("*** func %d ***\n", index); WasmContext* ctx = user_data; WasmInterpreterFunc* func = get_func(ctx, index); WasmInterpreterFuncSignature* sig = get_signature(ctx, func->sig_index); @@ -696,11 +698,9 @@ static WasmResult reduce(WasmContext* ctx, WasmInterpreterExpr* expr) { done = WASM_TRUE; if (ctx->expr_stack.size == 0) { -#if LOG - fprintf(stderr, "%3" PRIzd "(%d): reduce: <- %s:%s\n", + LOGF("%3" PRIzd "(%d): reduce: <- %s:%s\n", ctx->expr_stack.size, ctx->value_stack_size, s_opcode_name[expr->opcode], s_type_names[expr->type]); -#endif /* discard all top-level values. The last one is the return value, which * we don't want to discard, but we won't know if this is the last @@ -713,15 +713,13 @@ static WasmResult reduce(WasmContext* ctx, WasmInterpreterExpr* expr) { WasmExprNode* top = &ctx->expr_stack.data[ctx->expr_stack.size - 1]; assert(top->index < top->total); -#if LOG - fprintf(stderr, "%3" PRIzd "(%d): reduce: %s(%d/%d) <- %s:%s\n", + LOGF("%3" PRIzd "(%d): reduce: %s(%d/%d) <- %s:%s\n", ctx->expr_stack.size, ctx->value_stack_size, s_opcode_name[top->expr.opcode], top->index, top->total, s_opcode_name[expr->opcode], s_type_names[expr->type]); -#endif #if LOG if (top->expr.opcode == WASM_OPCODE_BR) { - fprintf(stderr, " : br depth %u\n", top->expr.br.depth); + LOGF(" : br depth %u\n", top->expr.br.depth); } #endif @@ -1080,11 +1078,9 @@ static WasmResult shift(WasmContext* ctx, WasmInterpreterExpr* expr, uint32_t count) { assert(count > 0); -#if LOG - fprintf(stderr, "%3" PRIzd "(%d): shift: %s:%s %u\n", ctx->expr_stack.size, - ctx->value_stack_size, s_opcode_name[expr->opcode], - s_type_names[expr->type], count); -#endif + LOGF("%3" PRIzd "(%d): shift: %s:%s %u\n", ctx->expr_stack.size, + ctx->value_stack_size, s_opcode_name[expr->opcode], + s_type_names[expr->type], count); WasmExprNode* node = wasm_append_expr_node(ctx->allocator, &ctx->expr_stack); CHECK_ALLOC_NULL(ctx, node); node->expr = *expr; @@ -1578,10 +1574,12 @@ WasmResult wasm_read_binary_interpreter(WasmAllocator* allocator, const void* data, size_t size, const WasmReadBinaryOptions* options, + WasmBinaryErrorHandler* error_handler, WasmInterpreterModule* out_module) { WasmContext ctx; WASM_ZERO_MEMORY(ctx); ctx.allocator = allocator; + ctx.error_handler = error_handler; ctx.memory_allocator = memory_allocator; ctx.module = out_module; ctx.start_func_index = INVALID_FUNC_INDEX; diff --git a/src/wasm-binary-reader-interpreter.h b/src/wasm-binary-reader-interpreter.h index 384b6833..bd701b92 100644 --- a/src/wasm-binary-reader-interpreter.h +++ b/src/wasm-binary-reader-interpreter.h @@ -30,6 +30,7 @@ WasmResult wasm_read_binary_interpreter( const void* data, size_t size, const struct WasmReadBinaryOptions* options, + WasmBinaryErrorHandler*, struct WasmInterpreterModule* out_module); WASM_EXTERN_C_END diff --git a/src/wasm-common.c b/src/wasm-common.c index c311bcef..834ce59d 100644 --- a/src/wasm-common.c +++ b/src/wasm-common.c @@ -145,12 +145,12 @@ static void print_carets(FILE* out, fprintf(out, "%*s%.*s\n", (int)num_spaces, "", (int)num_carets, carets); } -static void print_error(FILE* out, - const WasmLocation* loc, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset) { +static void print_source_error(FILE* out, + const WasmLocation* loc, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset) { fprintf(out, "%s:%d:%d: %s\n", loc->filename, loc->line, loc->first_column, error); if (source_line && source_line_length > 0) { @@ -161,23 +161,33 @@ static void print_error(FILE* out, } } -void wasm_default_error_callback(const WasmLocation* loc, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset, - void* user_data) { - print_error(stderr, loc, error, source_line, source_line_length, - source_line_column_offset); +void wasm_default_source_error_callback(const WasmLocation* loc, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset, + void* user_data) { + print_source_error(stderr, loc, error, source_line, source_line_length, + source_line_column_offset); } -void wasm_default_assert_invalid_callback(const WasmLocation* loc, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset, - void* user_data) { +void wasm_default_assert_invalid_source_error_callback( + const WasmLocation* loc, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset, + void* user_data) { fprintf(stdout, "assert_invalid error:\n "); - print_error(stdout, loc, error, source_line, source_line_length, - source_line_column_offset); + print_source_error(stdout, loc, error, source_line, source_line_length, + source_line_column_offset); +} + +void wasm_default_binary_error_callback(uint32_t offset, + const char* error, + void* user_data) { + if (offset == WASM_UNKNOWN_OFFSET) + fprintf(stderr, "error: %s\n", error); + else + fprintf(stderr, "error: @0x%08x: %s\n", offset, error); } diff --git a/src/wasm-common.h b/src/wasm-common.h index 22c04a30..7af2ea4e 100644 --- a/src/wasm-common.h +++ b/src/wasm-common.h @@ -38,6 +38,7 @@ #define WASM_ZERO_MEMORY(var) memset((void*)&(var), 0, sizeof(var)) #define WASM_USE(x) (void)x +#define WASM_UNKNOWN_OFFSET ((uint32_t)~0) #define WASM_PAGE_SIZE 0x10000 /* 64k */ #define PRIstringslice "%.*s" @@ -107,10 +108,22 @@ typedef struct WasmSourceErrorHandler { } WasmSourceErrorHandler; #define WASM_SOURCE_ERROR_HANDLER_DEFAULT \ - { wasm_default_error_callback, 80, NULL } + { wasm_default_source_error_callback, 80, NULL } #define WASM_ASSERT_INVALID_SOURCE_ERROR_HANDLER_DEFAULT \ - { wasm_default_assert_invalid_callback, 80, NULL } + { wasm_default_assert_invalid_source_error_callback, 80, NULL } + +typedef void (*WasmBinaryErrorCallback)(uint32_t offset, + const char* error, + void* user_data); + +typedef struct WasmBinaryErrorHandler { + WasmBinaryErrorCallback on_error; + void* user_data; +} WasmBinaryErrorHandler; + +#define WASM_BINARY_ERROR_HANDLER_DEFAULT \ + { wasm_default_binary_error_callback, NULL } /* matches binary format, do not change */ enum { @@ -351,18 +364,24 @@ WasmResult wasm_read_file(struct WasmAllocator* allocator, const char* filename, void** out_data, size_t* out_size); -void wasm_default_error_callback(const WasmLocation*, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset, - void* user_data); -void wasm_default_assert_invalid_callback(const WasmLocation*, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset, - void* user_data); + +void wasm_default_source_error_callback(const WasmLocation*, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset, + void* user_data); +void wasm_default_assert_invalid_source_error_callback( + const WasmLocation*, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset, + void* user_data); +void wasm_default_binary_error_callback(uint32_t offset, + const char* error, + void* user_data); + WASM_EXTERN_C_END #endif /* WASM_COMMON_H_ */ diff --git a/src/wasm-interp-sq.c b/src/wasm-interp-sq.c index d93dd3b5..9635967f 100644 --- a/src/wasm-interp-sq.c +++ b/src/wasm-interp-sq.c @@ -49,6 +49,9 @@ static WasmInterpreterThreadOptions s_thread_options = static WasmBool s_trace; static WasmBool s_use_libc_allocator; +static WasmBinaryErrorHandler s_error_handler = + WASM_BINARY_ERROR_HANDLER_DEFAULT; + #define V(name, str) str, static const char* s_trap_strings[] = {FOREACH_INTERPRETER_RESULT(V)}; #undef V @@ -204,7 +207,8 @@ static WasmResult read_module(WasmAllocator* allocator, WasmAllocator* memory_allocator = &g_wasm_libc_allocator; WASM_ZERO_MEMORY(*out_module); return wasm_read_binary_interpreter(allocator, memory_allocator, data, size, - &s_read_binary_options, out_module); + &s_read_binary_options, &s_error_handler, + out_module); } static WasmResult init_thread(WasmAllocator* allocator, diff --git a/src/wasm-interp.c b/src/wasm-interp.c index 9b1e8b4a..07f28c54 100644 --- a/src/wasm-interp.c +++ b/src/wasm-interp.c @@ -43,6 +43,9 @@ static WasmBool s_spec; static WasmBool s_run_all_exports; static WasmBool s_use_libc_allocator; +static WasmBinaryErrorHandler s_error_handler = + WASM_BINARY_ERROR_HANDLER_DEFAULT; + #define NOPE WASM_OPTION_NO_ARGUMENT #define YEP WASM_OPTION_HAS_ARGUMENT @@ -377,9 +380,9 @@ static WasmResult read_module(WasmAllocator* allocator, if (WASM_SUCCEEDED(result)) { WasmAllocator* memory_allocator = &g_wasm_libc_allocator; WASM_ZERO_MEMORY(*out_module); - result = - wasm_read_binary_interpreter(allocator, memory_allocator, data, size, - &s_read_binary_options, out_module); + result = wasm_read_binary_interpreter(allocator, memory_allocator, data, + size, &s_read_binary_options, + &s_error_handler, out_module); if (WASM_SUCCEEDED(result)) { if (s_verbose) diff --git a/src/wasm-stack-allocator.c b/src/wasm-stack-allocator.c index 47817910..b3638b97 100644 --- a/src/wasm-stack-allocator.c +++ b/src/wasm-stack-allocator.c @@ -22,6 +22,12 @@ #define WASM_TRACE_ALLOCATOR 0 +#if WASM_TRACE_ALLOCATOR +#define TRACEF(...) fprintf(stderr, __VA_ARGS__) +#else +#define TRACEF(...) +#endif + #if WASM_STACK_ALLOCATOR_STATS #include <stdio.h> #endif /* WASM_STACK_ALLOCATOR_STATS */ @@ -126,8 +132,8 @@ static void* stack_alloc(WasmAllocator* allocator, #if WASM_TRACE_ALLOCATOR if (file) { - fprintf(stderr, "%s:%d: stack_alloc(%" PRIzd ", align=%" PRIzd ") => %p\n", - file, line, size, align, result); + TRACEF("%s:%d: stack_alloc(%" PRIzd ", align=%" PRIzd ") => %p\n", file, + line, size, align, result); } #endif /* WASM_TRACE_ALLOCATOR */ @@ -163,9 +169,8 @@ static void* stack_realloc(WasmAllocator* allocator, #if WASM_TRACE_ALLOCATOR if (file) { - fprintf(stderr, - "%s:%d: stack_realloc(%p, %" PRIzd ", align=%" PRIzd ") => %p\n", - file, line, p, size, align, result); + TRACEF("%s:%d: stack_realloc(%p, %" PRIzd ", align=%" PRIzd ") => %p\n", + file, line, p, size, align, result); } #endif /* WASM_TRACE_ALLOCATOR */ @@ -193,10 +198,7 @@ static void stack_free(WasmAllocator* allocator, if (!p) return; -#if WASM_TRACE_ALLOCATOR - fprintf(stderr, "%s:%d: stack_free(%p)\n", file, line, p); -#endif /* WASM_TRACE_ALLOCATOR */ - + TRACEF("%s:%d: stack_free(%p)\n", file, line, p); WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator; #if WASM_STACK_ALLOCATOR_STATS diff --git a/src/wasm-wast.c b/src/wasm-wast.c index d4686772..c47c709d 100644 --- a/src/wasm-wast.c +++ b/src/wasm-wast.c @@ -38,6 +38,9 @@ static WasmReadBinaryOptions s_read_binary_options = static WasmBool s_use_libc_allocator; static WasmBool s_generate_names; +static WasmBinaryErrorHandler s_error_handler = + WASM_BINARY_ERROR_HANDLER_DEFAULT; + #define NOPE WASM_OPTION_NO_ARGUMENT #define YEP WASM_OPTION_HAS_ARGUMENT @@ -143,7 +146,7 @@ int main(int argc, char** argv) { WasmModule module; WASM_ZERO_MEMORY(module); result = wasm_read_binary_ast(allocator, data, size, &s_read_binary_options, - &module); + &s_error_handler, &module); if (WASM_SUCCEEDED(result)) { if (s_generate_names) result = wasm_generate_names(allocator, &module); |