do fail interactions inside wasm module + unify host modules + fix cored module

This commit is contained in:
2026-04-13 23:03:54 +02:00
parent fe6c2f5b99
commit 550ce0b079
13 changed files with 283 additions and 370 deletions

View File

@ -1,18 +1,33 @@
#include "../lib.h"
#include "../wasm_export.h"
#include "bh_platform.h"
#include "wasm_export.h"
#include "__WASM_ARRAY_FILE__"
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) {}
void fail_start_trace_wasm(wasm_exec_env_t exec_env) { fail_start_trace(); }
void fail_stop_trace_wasm(wasm_exec_env_t exec_env) { fail_stop_trace(); }
void fail_marker_positive_wasm(wasm_exec_env_t exec_env) {
fail_marker_positive();
}
void fail_marker_negative_wasm(wasm_exec_env_t exec_env) {
fail_marker_negative();
}
void fail_marker_detected_wasm(wasm_exec_env_t exec_env) {
fail_marker_detected();
}
#define STACK_SIZE (4 * 1024)
#define HEAP_SIZE STACK_SIZE
#define RUNTIME_POOL_SIZE (4 * STACK_SIZE)
#define RUNTIME_POOL_SIZE 4 * STACK_SIZE
int main(int argc, char *argv[]) {
char error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_exec_env_t exec_env;
/* initialize the wasm runtime */
static char global_heap_buf[RUNTIME_POOL_SIZE];
@ -27,48 +42,56 @@ int main(int argc, char *argv[]) {
return 1;
}
/* parse the WASM file from buffer and create a WASM module */
module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__, error_buf,
sizeof(error_buf));
// Export fail symbols as native functions, so they can be "called" from wasm
static NativeSymbol native_symbols[] = {
{"fail_start_trace", fail_start_trace_wasm, "()"},
{"fail_stop_trace", fail_stop_trace_wasm, "()"},
{"fail_marker_positive", fail_marker_positive_wasm, "()"},
{"fail_marker_negative", fail_marker_negative_wasm, "()"},
{"fail_marker_detected", fail_marker_detected_wasm, "()"},
};
int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
if (!wasm_runtime_register_natives("env", native_symbols, n_native_symbols)) {
return 1;
}
/* parse the WASM file from buffer and create a WASM module */
wasm_module_t module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__,
error_buf, sizeof(error_buf));
if (!module) {
return 1;
}
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, STACK_SIZE, HEAP_SIZE,
error_buf, sizeof(error_buf));
wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf));
if (!module_inst) {
return 1;
}
/* lookup a WASM function by its name, the function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst, "wasm_module");
// Single exec_env for all functions (probably'd need a single one per
// thread?)
wasm_exec_env_t exec_env =
wasm_runtime_create_exec_env(module_inst, STACK_SIZE);
uint32_t args[1];
/* create an execution environment to execute arbitrary WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst, STACK_SIZE);
/* arguments are always transferred in 32-bit element */
uint32_t args[1] = {0};
/* call an arbitrary WASM function */
// Call wasm function
wasm_function_inst_t func =
wasm_runtime_lookup_function(module_inst, "wasm_module");
if (!func) {
return 1;
}
if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) {
/* function wasn't called correctly */
return 1;
}
/* the return value is stored in args[0] */
uint32_t result = args[0];
// In case wasm_module returns a value
uint32_t retval = args[0];
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
if (result == 100) {
return 0;
} else {
return 1;
}
return 0;
}