#include "../lib.h" #include "../wasm_export.h" #include "bh_platform.h" #include "wasm_aot_array.c" #ifdef TARGET_LINUX #include #include #endif // FAIL* instrumentation symbols void fail_start_trace(void) {} void fail_stop_trace(void) {} void fail_marker_positive(void) {} void fail_marker_negative(void) {} void fail_marker_detected(void) {} // Those functions will be called from WASM void host_fail_start_trace(wasm_exec_env_t exec_env) { fail_start_trace(); } void host_fail_stop_trace(wasm_exec_env_t exec_env) { fail_stop_trace(); } void host_fail_marker_positive(wasm_exec_env_t exec_env) { fail_marker_positive(); } void host_fail_marker_negative(wasm_exec_env_t exec_env) { fail_marker_negative(); } void host_fail_marker_detected(wasm_exec_env_t exec_env) { fail_marker_detected(); } void host_print(wasm_exec_env_t exec_env, const char *msg) { PRINT("[WASM] %s", msg); } #define STACK_SIZE (4 * 1024) #define HEAP_SIZE STACK_SIZE #define RUNTIME_POOL_SIZE 4 * STACK_SIZE MAIN { char error_buf[128]; // Step 1: Initialize WAMR Runtime static RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(RuntimeInitArgs)); static char global_heap_buf[RUNTIME_POOL_SIZE]; init_args.mem_alloc_type = Alloc_With_Pool; init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); init_args.max_thread_num = 1; if (!wasm_runtime_full_init(&init_args)) { PRINT_ERROR("wasm_runtime_full_init failed.\n"); goto error_cleanup; } // Step 2: Export Native Symbols static NativeSymbol native_symbols[] = { {"fail_start_trace", (void *)host_fail_start_trace, "()", NULL}, {"fail_stop_trace", (void *)host_fail_stop_trace, "()", NULL}, {"fail_marker_positive", (void *)host_fail_marker_positive, "()", NULL}, {"fail_marker_negative", (void *)host_fail_marker_negative, "()", NULL}, {"fail_marker_detected", (void *)host_fail_marker_detected, "()", NULL}, {"print", (void *)host_print, "(*)", NULL}, }; int count = sizeof(native_symbols) / sizeof(NativeSymbol); if (!wasm_runtime_register_natives("env", native_symbols, count)) { PRINT_ERROR("wasm_runtime_register_natives failed.\n"); goto error_cleanup; } // Step 3: Parse and Validate Module wasm_module_t module = wasm_runtime_load(build_sum0_base_wasm_module_aot, build_sum0_base_wasm_module_aot_len, error_buf, sizeof(error_buf)); if (!module) { PRINT_ERROR("wasm_runtime_load failed with \"%s\".\n", error_buf); goto error_cleanup; } // Step 4: Instantiate Module wasm_module_inst_t module_inst = wasm_runtime_instantiate( module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf)); if (!module_inst) { PRINT_ERROR("wasm_runtime_instantiate failed with \"%s\".\n", error_buf); goto error_cleanup; } // Step 5: Create Execution Environment wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(module_inst, STACK_SIZE); if (!exec_env) { PRINT_ERROR("wasm_runtime_create_exec_env failed.\n"); goto error_cleanup; } // Step 6: Find and Call Exported Function wasm_function_inst_t func = wasm_runtime_lookup_function(module_inst, "wasm_module"); if (!func) { PRINT_ERROR("wasm_runtime_lookup_function failed.\n"); goto error_cleanup; } // In case wasm_module accepts arguments, set them here uint32_t args[1]; if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) { const char *exception = wasm_runtime_get_exception(module_inst); PRINT_ERROR("wasm_runtime_call_wasm failed with \"%s\".\n", exception ? exception : "unknown"); goto error_cleanup; } PRINT_SUCCESS("wasm function execution finished.\n"); // In case wasm_module returns a value we can do sth with it uint32_t retval = args[0]; success_cleanup: wasm_runtime_destroy_exec_env(exec_env); wasm_runtime_deinstantiate(module_inst); wasm_runtime_unload(module); wasm_runtime_destroy(); RET(0); error_cleanup: if (exec_env) { wasm_runtime_destroy_exec_env(exec_env); } if (module_inst) { wasm_runtime_deinstantiate(module_inst); } if (module) { wasm_runtime_unload(module); } wasm_runtime_destroy(); RET(1); }