Compare commits

...

4 Commits

93 changed files with 482 additions and 353 deletions

View File

@ -90,6 +90,8 @@ client module:
-Wf,--server-port={{ FAIL_SERVER_PORT }} \
-Wf,--state-dir={{ BUILD_DIR }}-{{ module }}/state \
-Wf,--trap \
-Wf,--catch-write-textsegment \
-Wf,--catch-outerspace \
-Wf,--timeout=500000 \
-Wf,--ok-marker=fail_marker_positive \
-Wf,--fail-marker=fail_marker_negative \

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<PROJECT>
<PROJECT_DATA_XML_NAME NAME="DISPLAY_DATA">
<SAVE_STATE>
<ARRAY NAME="EXPANDED_PATHS" TYPE="string">
<A VALUE="2026-04-13_23-40_cored_aot_fail:" />
</ARRAY>
<STATE NAME="SHOW_TABLE" TYPE="boolean" VALUE="false" />
</SAVE_STATE>
</PROJECT_DATA_XML_NAME>
<TOOL_MANAGER ACTIVE_WORKSPACE="Workspace">
<WORKSPACE NAME="Workspace" ACTIVE="true" />
</TOOL_MANAGER>
</PROJECT>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<PROJECT>
<PROJECT_DATA_XML_NAME NAME="DISPLAY_DATA">
<SAVE_STATE>
<ARRAY NAME="EXPANDED_PATHS" TYPE="string">
<A VALUE="2026-04-13_23-43_cored_interp_fail:" />
</ARRAY>
<STATE NAME="SHOW_TABLE" TYPE="boolean" VALUE="false" />
</SAVE_STATE>
</PROJECT_DATA_XML_NAME>
<TOOL_MANAGER ACTIVE_WORKSPACE="Workspace">
<WORKSPACE NAME="Workspace" ACTIVE="true" />
</TOOL_MANAGER>
</PROJECT>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/grub/boot/system.elf (Stored with Git LFS) Executable file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/markers.csv (Stored with Git LFS) Normal file

Binary file not shown.
1 version https://git-lfs.github.com/spec/v1
2 oid sha256:b36738a8507659463bca3b9bb7f6501da76fc5bcf793dd9d79e84ee920395532
3 size 2592

BIN
injections/2026-04-13_23-40_cored_aot_fail/module_host.c (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/results.txt (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/startup.o (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/cmos (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/config (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/cpu0 (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/dma (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/floppy (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/gameport (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/hard_drive (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/ioapic (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/keyboard (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/memory (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/memory.ram (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/pc_system (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/pic (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/pit (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/serial (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/vga (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/vga.memory (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/state/virt_timer (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/syscalls.o (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/system.elf (Stored with Git LFS) Executable file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/system.iso (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/system.o (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/trace.pb (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/wasm_aot_array.c (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/wasm_module.aot (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-40_cored_aot_fail/wasm_module.wasm (Stored with Git LFS) Executable file

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/grub/boot/system.elf (Stored with Git LFS) Executable file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/markers.csv (Stored with Git LFS) Normal file

Binary file not shown.
1 version https://git-lfs.github.com/spec/v1
2 oid sha256:8e7185a83d29c2be7d5632f28f4f50c38636de80aeb0db9a13f6049964bf2ed4
3 size 5266

BIN
injections/2026-04-13_23-43_cored_interp_fail/module_host.c (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/results.txt (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/startup.o (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/syscalls.o (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/system.elf (Stored with Git LFS) Executable file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/system.iso (Stored with Git LFS) Normal file

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/system.o (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
injections/2026-04-13_23-43_cored_interp_fail/wasm_module.wasm (Stored with Git LFS) Executable file

Binary file not shown.

View File

@ -151,13 +151,14 @@ build module="__help" target="fail" mode="aot":
just clean {{ module }}
just create-build-dir {{ module }}
just copy-c-module {{ module }}
if [ "{{ mode }}" = "aot" ]; then
just build-wasm-module {{ module }}
just build-wasm-aot {{ module }}
just build-wasm-aot-array {{ module }}
just prepare-aot-host {{ module }} {{ target }}
just prepare-aot-host {{ module }}
just build-wasm-host {{ module }} {{ target }}
just build-system-startup {{ module }} {{ target }}
@ -167,7 +168,7 @@ build module="__help" target="fail" mode="aot":
just build-wasm-module {{ module }}
just build-wasm-interp-array {{ module }}
just prepare-interp-host {{ module }} {{ target }}
just prepare-interp-host {{ module }}
just build-wasm-host {{ module }} {{ target }}
just build-system-startup {{ module }} {{ target }}
@ -190,7 +191,14 @@ build module="__help" target="fail" mode="aot":
[doc("Run binary")]
[group("5: just do it")]
run module:
{{ BUILD_DIR }}-{{ module }}/system.elf
@echo "Running {{ module }}:"
@{{ BUILD_DIR }}-{{ module }}/system.elf
[arg("mode", pattern="c|aot|interp", help="Which WASM mode to use")]
[arg("target", pattern="fail|linux|linux-baremetal", help="Which platform to compile for")]
[doc("Perform all steps for a fail/linux/linux-bm build with aot/interp WASM")]
[group("5: just do it")]
build-run module="__help" target="fail" mode="aot": (build module target mode) (run module)
[doc("Send binaries to mars")]
[group("5: just do it")]

View File

@ -1,16 +1,20 @@
#include "../lib.h"
#ifdef TARGET_LINUX
#include <stdio.h>
#endif
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 print(const char *msg) { PRINT("[C] %s", msg); }
int wasm_module(void);
int main(int argc, char *argv[]) {
MAIN {
int retval = wasm_module();
printf("Function returned %d.\n", retval);
return retval;
PRINT_SUCCESS("wasm_module returned %d.\n", retval);
RET(retval);
}

View File

@ -1,11 +0,0 @@
#include "../lib.h"
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) {}
int wasm_module(void);
void os_main(void) { wasm_module(); }

View File

@ -4,21 +4,57 @@
#define INLINE __attribute__((always_inline)) inline
#define NOINLINE __attribute__((noinline))
#define EXPORT(fnct) __attribute__((export_name(fnct)))
#define IMPORT(fnct) __attribute__((import_module("env"), import_name(fnct)))
#if !defined(TARGET_FAIL) && !defined(TARGET_LINUX_BAREMETAL) && \
!defined(TARGET_LINUX)
// Set to linux while editing to prevent lsp errors
#define TARGET_LINUX
#endif
#ifdef TARGET_FAIL
#define MAIN void os_main(void)
#define PRINT(fmt, ...)
#define PRINT_ERROR(fmt, ...)
#define PRINT_SUCCESS(fmt, ...)
#define HOST_PRINT(msg)
#define RET(val) return
#endif
#ifdef TARGET_LINUX_BAREMETAL
#define MAIN int main(int argc, char *argv[])
#define PRINT(fmt, ...)
#define PRINT_ERROR(fmt, ...)
#define PRINT_SUCCESS(fmt, ...)
#define HOST_PRINT(msg)
#define RET(val) return
#endif
#ifdef TARGET_LINUX
#define MAIN int main(int argc, char *argv[])
#define PRINT(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__)
#define PRINT_ERROR(fmt, ...) \
fprintf(stderr, "[Error] "); \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define PRINT_SUCCESS(fmt, ...) \
fprintf(stdout, "[Success] "); \
fprintf(stdout, fmt, ##__VA_ARGS__)
#define HOST_PRINT(msg) print(msg)
#define RET(val) return val;
#endif
#ifdef __cplusplus
extern "C" {
#endif
// Mark start of injection
void NOINLINE fail_start_trace(void);
// Mark end of injection
void NOINLINE fail_stop_trace(void);
// everything ok: valid code and right values
void NOINLINE fail_marker_positive(void);
// everything ok: valid code but wrong values
void NOINLINE fail_marker_negative(void);
// invalid code
void NOINLINE fail_marker_detected(void);
// Those functions are defined in the host program
void NOINLINE fail_start_trace(void); // Mark start of injection
void NOINLINE fail_stop_trace(void); // Mark end of injection
void NOINLINE fail_marker_positive(void); // Everything ok
void NOINLINE fail_marker_detected(void); // Everything ok
void NOINLINE fail_marker_negative(void); // Invalid code
void NOINLINE print(const char *msg);
#ifdef __cplusplus
}

View File

@ -1,100 +0,0 @@
#include "../lib.h"
#include "../wasm_export.h"
#include "bh_platform.h"
#include "__WASM_ARRAY_FILE__"
// Symbols to interact with FAIL*
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.
// They are separated because they get passed the exec_env.
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
void os_main() {
char error_buf[128];
/* initialize the wasm runtime */
static char global_heap_buf[RUNTIME_POOL_SIZE];
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
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)) {
return;
}
// 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;
}
/* 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;
}
/* create an instance of the WASM module (WASM linear memory is ready) */
wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf));
if (!module_inst) {
return;
}
// 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];
// Call wasm function
wasm_function_inst_t func =
wasm_runtime_lookup_function(module_inst, "wasm_module");
if (!func) {
return;
}
if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) {
return;
}
// 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();
return;
}

View File

@ -1,97 +0,0 @@
#include "../lib.h"
#include "../wasm_export.h"
#include "bh_platform.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
int main(int argc, char *argv[]) {
char error_buf[128];
/* initialize the wasm runtime */
static char global_heap_buf[RUNTIME_POOL_SIZE];
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
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)) {
return 1;
}
// 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) */
wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf));
if (!module_inst) {
return 1;
}
// 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];
// 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)) {
return 1;
}
// 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();
return 0;
}

View File

@ -1,108 +0,0 @@
#include "../lib.h"
#include "../wasm_export.h"
#include "bh_platform.h"
#include "__WASM_ARRAY_FILE__"
#include <stdio.h>
void fail_start_trace(void) { printf("Start Trace Marker.\n"); }
void fail_stop_trace(void) { printf("Stop Trace Marker.\n"); }
void fail_marker_positive(void) { printf("Positive Marker.\n"); }
void fail_marker_negative(void) { printf("Negative Marker.\n"); }
void fail_marker_detected(void) { printf("Detected Marker.\n"); }
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
int main(int argc, char *argv[]) {
char error_buf[128];
/* initialize the wasm runtime */
static char global_heap_buf[RUNTIME_POOL_SIZE];
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
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)) {
return 1;
}
// 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)) {
printf("Failed to register native symbols.\n");
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) {
printf("Failed to parse module from WASM file: \"%s\"\n", error_buf);
return 1;
}
/* create an instance of the WASM module (WASM linear memory is ready) */
wasm_module_inst_t module_inst = wasm_runtime_instantiate(
module, STACK_SIZE, HEAP_SIZE, error_buf, sizeof(error_buf));
if (!module_inst) {
printf("Failed to instantiate WASM module: \"%s\"\n", error_buf);
return 1;
}
// 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);
if (!exec_env) {
printf("Failed to create WASM execution environment.\n");
return 1;
}
// Call wasm function
uint32_t args[1] = {0};
wasm_function_inst_t func =
wasm_runtime_lookup_function(module_inst, "wasm_module");
if (!func) {
printf("Failed to obtain WASM function instance.\n");
return 1;
}
printf("Calling WASM function.\n");
if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) {
printf("Failed to call WASM function.\n");
return 1;
}
// In case wasm_module returns a value
uint32_t retval = args[0];
printf("WASM function returned %d.\n", retval);
printf("Destroying WAMR objects.\n");
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
}

View File

@ -0,0 +1,139 @@
#include "../lib.h"
#include "../wasm_export.h"
#include "bh_platform.h"
#include "__WASM_ARRAY_FILE__"
#ifdef TARGET_LINUX
#include <stdio.h>
#include <string.h>
#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(__WASM_ARRAY__, __WASM_ARRAY_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);
}

View File

@ -35,8 +35,11 @@ typedef int8_t sign_t;
static enc_t cored_res;
static enc_t sum_out[REPLICA_COUNT];
// The prints here can't happen because they're disabled under test ¯\_(ツ)_/¯
static INLINE enc_t apply(enc_t vc, sign_t bdyn) {
if (bdyn > SIG_MAX) {
HOST_PRINT("signature overflow.\n");
fail_marker_detected();
}
return vc + bdyn;
@ -58,6 +61,7 @@ static sign_t cored_vote(void) {
cored_res = apply(XC, (XC - ZC));
return SIG_s_XZ;
} else {
HOST_PRINT("all replicas differ.\n");
fail_marker_detected();
return 0;
}
@ -72,15 +76,11 @@ template <const unsigned int N, const sign_t S> static INLINE void sum(void) {
sum_out[N] = encode(sum, THE_A, S);
}
extern "C" int wasm_module(void) {
extern "C" EXPORT("wasm_module") int wasm_module(void) {
XC = 0;
YC = 0;
ZC = 0;
// TODO: Start trace from WASM?
// - Simpler, as only a single wasm function has to be called for all
// modules
// - Doesn't inject wasm_runtime_call_wasm() setup
fail_start_trace();
sum<0, SIG_X>();
@ -102,6 +102,7 @@ extern "C" int wasm_module(void) {
vote_result_sig = SIG_Y;
break;
default:
HOST_PRINT("unknown static_sig.\n");
break;
}
@ -110,15 +111,18 @@ extern "C" int wasm_module(void) {
/* Validate Vote result */
if (!check(cored_res, THE_A, vote_result_sig)) {
HOST_PRINT("voted result invalid.\n");
fail_marker_detected();
return 2;
}
plain_t res = decode(cored_res, THE_A, vote_result_sig);
if (res == 100) {
HOST_PRINT("cored success.\n");
fail_marker_positive();
return 0;
} else {
HOST_PRINT("undetected error.\n");
fail_marker_negative();
return 1;
}

View File

@ -1,6 +1,6 @@
#include "../lib.h"
extern "C" int wasm_module(void) {
extern "C" EXPORT("wasm_module") int wasm_module(void) {
fail_start_trace();
int sum = 0;

View File

@ -10,7 +10,7 @@ WASI_CFLAGS := "\
-O0 \
-nostdlib \
-Wl,--no-entry \
-Wl,--export-all \
-Wl,--export=wasm_module \
-Wl,--no-gc-sections \
-Wl,--initial-memory=65536 \
-Wl,--export=__heap_base \
@ -101,6 +101,10 @@ build-c-module module target="fail":
exit 1
fi
[private]
copy-c-module module:
cp targets/wasm-module/{{ module }}.cpp {{ BUILD_DIR }}-{{ module }}/wasm-module.cpp
# =================================================================================================================== #
# Host program recipes
# =================================================================================================================== #
@ -155,8 +159,8 @@ LINUX_BAREMETAL_INCLUDES := f"\
[doc("Insert the AOT array into the host program")]
[group("2: build host")]
prepare-aot-host module target="fail":
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
prepare-aot-host module:
cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/wasm_aot_array.c/g" \
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_aot/g" \
@ -165,8 +169,8 @@ prepare-aot-host module target="fail":
[doc("Insert the WASM array into the host program")]
[group("2: build host")]
prepare-interp-host module target="fail":
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
prepare-interp-host module:
cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/wasm_interp_array.c/g" \
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_wasm/g" \
@ -176,18 +180,21 @@ prepare-interp-host module target="fail":
[private]
build-wasm-host-fail module:
{{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \
-DTARGET_FAIL \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[private]
build-wasm-host-linux module:
{{ LINUX_CC }} {{ LINUX_CFLAGS }} {{ LINUX_INCLUDES }} \
-DTARGET_LINUX \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[private]
build-wasm-host-linux-baremetal module:
{{ CROSS_CC }} {{ LINUX_BAREMETAL_CFLAGS }} {{ LINUX_BAREMETAL_INCLUDES }} \
-DTARGET_LINUX_BAREMETAL \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
@ -209,13 +216,15 @@ build-wasm-host module target="fail":
[private]
build-c-host-fail module:
{{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \
-c targets/c-host/fail.c \
-DTARGET_FAIL \
-c targets/c-host/c_host.c \
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
[private]
build-c-host-linux module:
{{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \
-c targets/c-host/linux.c \
-DTARGET_LINUX \
-c targets/c-host/c_host.c \
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
[doc("Insert the C function into the host program (no WASM)")]