Draft wamr modifications + host program structure sections

This commit is contained in:
2026-09-11 17:03:16 +02:00
parent 0d4c0fb3c9
commit 63531014d6
24 changed files with 526 additions and 190 deletions
@@ -0,0 +1,15 @@
// Define FAIL* marker 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) {}
// Forward declare workload
int wasm_module(void);
// Execute
MAIN {
int retval = wasm_module();
RET(retval);
}
@@ -0,0 +1,18 @@
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
// If using Alloc_With_Allocator:
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)wamr_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)wamr_realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)wamr_free;
// If using Alloc_With_Pool instead:
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)) {
goto error_cleanup;
}
@@ -0,0 +1,23 @@
// Error handling is omitted
char error_buf[128];
wasm_module_t module = wasm_runtime_load(
module_array, module_array_length, error_buf, sizeof(error_buf));
wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf));
wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(
module_inst, STACK_SIZE);
wasm_function_inst_t func = wasm_runtime_lookup_function(
module_inst, "wasm_module");
// Arguments can be supplied to the function
// by passing a pointer to an args buffer.
// No arguments are set here, but a single
// slot is reserved for the return value.
uint32_t argv[1];
uint32_t argc = 0;
wasm_runtime_call_wasm(exec_env, func, argc, argv);
uint32_t retval = argv[0];
@@ -0,0 +1,12 @@
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},
};
int count = sizeof(native_symbols) / sizeof(NativeSymbol);
if (!wasm_runtime_register_natives("env", native_symbols, count)) {
goto error_cleanup;
}
@@ -0,0 +1,23 @@
// Define FAIL* marker 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 can be called from within Wasm after registration
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();
}
@@ -0,0 +1,18 @@
extern "C" EXPORT("wasm_module") int wasm_module(void) {
// Workload initialization
int x = 0;
fail_start_trace();
// Workload execution
x = x + 1;
fail_stop_trace();
// Success condition
if (x == 1) {
fail_marker_positive();
return 0;
} else {
fail_marker_negative();
return 1;
}
}