|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Midokura Japan KK. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + */ |
| 5 | + |
| 6 | +#include "wasm_export.h" |
| 7 | +#include "bh_read_file.h" |
| 8 | + |
| 9 | +uint32_t |
| 10 | +host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx, |
| 11 | + uint32_t x, uint32_t stack); |
| 12 | +uint32_t |
| 13 | +host_consume_stack(wasm_exec_env_t exec_env, uint32_t stack); |
| 14 | + |
| 15 | +extern unsigned int nest; |
| 16 | + |
| 17 | +static NativeSymbol native_symbols[] = { |
| 18 | + { "host_consume_stack_and_call_indirect", |
| 19 | + host_consume_stack_and_call_indirect, "(iii)i", NULL }, |
| 20 | + { "host_consume_stack", host_consume_stack, "(i)i", NULL }, |
| 21 | +}; |
| 22 | + |
| 23 | +struct record { |
| 24 | + bool failed; |
| 25 | + bool leaked; |
| 26 | + char exception[128]; /* EXCEPTION_BUF_LEN */ |
| 27 | +}; |
| 28 | + |
| 29 | +void |
| 30 | +print_record(unsigned int start, unsigned int end, const struct record *rec) |
| 31 | +{ |
| 32 | + printf("%5u - %5u | %6s | %6s | %s\n", start, end, |
| 33 | + rec->failed ? "failed" : "ok", rec->leaked ? "leaked" : "ok", |
| 34 | + rec->exception); |
| 35 | +} |
| 36 | + |
| 37 | +int |
| 38 | +main(int argc, char **argv) |
| 39 | +{ |
| 40 | + char *buffer; |
| 41 | + char error_buf[128]; |
| 42 | + |
| 43 | + if (argc != 2) { |
| 44 | + return 2; |
| 45 | + } |
| 46 | + char *module_path = argv[1]; |
| 47 | + |
| 48 | + wasm_module_t module = NULL; |
| 49 | + uint32 buf_size; |
| 50 | + uint32 stack_size = 4096; |
| 51 | + /* |
| 52 | + * disable app heap. |
| 53 | + * - we use wasi |
| 54 | + * - https://github.com/bytecodealliance/wasm-micro-runtime/issues/2275 |
| 55 | + */ |
| 56 | + uint32 heap_size = 0; |
| 57 | + |
| 58 | + RuntimeInitArgs init_args; |
| 59 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 60 | + init_args.mem_alloc_type = Alloc_With_System_Allocator; |
| 61 | + init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol); |
| 62 | + init_args.native_module_name = "env"; |
| 63 | + init_args.native_symbols = native_symbols; |
| 64 | + if (!wasm_runtime_full_init(&init_args)) { |
| 65 | + printf("wasm_runtime_full_init failed.\n"); |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + buffer = bh_read_file_to_buffer(module_path, &buf_size); |
| 70 | + if (!buffer) { |
| 71 | + printf("bh_read_file_to_buffer failed\n"); |
| 72 | + goto fail; |
| 73 | + } |
| 74 | + |
| 75 | + module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf, |
| 76 | + sizeof(error_buf)); |
| 77 | + if (!module) { |
| 78 | + printf("wasm_runtime_load failed: %s\n", error_buf); |
| 79 | + goto fail; |
| 80 | + } |
| 81 | + |
| 82 | + /* header */ |
| 83 | + printf(" stack size | fail? | leak? | exception\n"); |
| 84 | + printf("-------------------------------------------------------------------" |
| 85 | + "--------\n"); |
| 86 | + |
| 87 | + unsigned int stack; |
| 88 | + unsigned int prevstack; |
| 89 | + unsigned int stack_range_start = 0; |
| 90 | + unsigned int stack_range_end = 4096 * 6; |
| 91 | + unsigned int step = 16; |
| 92 | + struct record rec0; |
| 93 | + struct record rec1; |
| 94 | + struct record *rec = &rec0; |
| 95 | + struct record *prevrec = &rec1; |
| 96 | + bool have_prevrec = false; |
| 97 | + for (stack = stack_range_start; stack < stack_range_end; stack += step) { |
| 98 | + wasm_module_inst_t module_inst = NULL; |
| 99 | + wasm_exec_env_t exec_env = NULL; |
| 100 | + bool failed = true; |
| 101 | + const char *exception = NULL; |
| 102 | + nest = 0; |
| 103 | + |
| 104 | + module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, |
| 105 | + error_buf, sizeof(error_buf)); |
| 106 | + if (!module_inst) { |
| 107 | + printf("wasm_runtime_instantiate failed: %s\n", error_buf); |
| 108 | + goto fail2; |
| 109 | + } |
| 110 | + |
| 111 | + exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); |
| 112 | + if (!exec_env) { |
| 113 | + printf("wasm_runtime_create_exec_env failed\n"); |
| 114 | + goto fail2; |
| 115 | + } |
| 116 | + |
| 117 | + const char *funcname = "test"; |
| 118 | + wasm_function_inst_t func = |
| 119 | + wasm_runtime_lookup_function(module_inst, funcname); |
| 120 | + if (!func) { |
| 121 | + printf("wasm_runtime_lookup_function failed for %s\n", funcname); |
| 122 | + goto fail2; |
| 123 | + } |
| 124 | + |
| 125 | + /* note: the function type is (ii)i */ |
| 126 | + uint32_t wasm_argv[] = { |
| 127 | + stack, |
| 128 | + 30, |
| 129 | + }; |
| 130 | + uint32_t wasm_argc = 2; |
| 131 | + if (!wasm_runtime_call_wasm(exec_env, func, wasm_argc, wasm_argv)) { |
| 132 | + exception = wasm_runtime_get_exception(module_inst); |
| 133 | + goto fail2; |
| 134 | + } |
| 135 | + failed = false; |
| 136 | + fail2: |
| 137 | + /* |
| 138 | + * note: non-zero "nest" here demonstrates resource leak on longjmp |
| 139 | + * from signal handler. |
| 140 | + * cf. |
| 141 | + * https://github.com/bytecodealliance/wasm-micro-runtime/issues/3320 |
| 142 | + */ |
| 143 | + memset(rec, 0, sizeof(*rec)); |
| 144 | + rec->failed = failed; |
| 145 | + rec->leaked = nest != 0; |
| 146 | + strncpy(rec->exception, exception ? exception : "", |
| 147 | + sizeof(rec->exception)); |
| 148 | + if (have_prevrec && memcmp(prevrec, rec, sizeof(*rec))) { |
| 149 | + print_record(prevstack, stack, prevrec); |
| 150 | + have_prevrec = false; |
| 151 | + } |
| 152 | + if (!have_prevrec) { |
| 153 | + prevstack = stack; |
| 154 | + struct record *tmp = prevrec; |
| 155 | + prevrec = rec; |
| 156 | + rec = tmp; |
| 157 | + have_prevrec = true; |
| 158 | + } |
| 159 | + if (exec_env) { |
| 160 | + wasm_runtime_destroy_exec_env(exec_env); |
| 161 | + } |
| 162 | + if (module_inst) { |
| 163 | + wasm_runtime_deinstantiate(module_inst); |
| 164 | + } |
| 165 | + } |
| 166 | + if (have_prevrec) { |
| 167 | + print_record(prevstack, stack, prevrec); |
| 168 | + } |
| 169 | + |
| 170 | +fail: |
| 171 | + if (module) { |
| 172 | + wasm_runtime_unload(module); |
| 173 | + } |
| 174 | + if (buffer) { |
| 175 | + BH_FREE(buffer); |
| 176 | + } |
| 177 | + wasm_runtime_destroy(); |
| 178 | +} |
0 commit comments