unify host programs across targets
This commit is contained in:
13
nixos.just
13
nixos.just
@ -158,7 +158,7 @@ build module="__help" target="fail" mode="aot":
|
|||||||
just build-wasm-aot {{ module }}
|
just build-wasm-aot {{ module }}
|
||||||
just build-wasm-aot-array {{ 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-wasm-host {{ module }} {{ target }}
|
||||||
|
|
||||||
just build-system-startup {{ module }} {{ target }}
|
just build-system-startup {{ module }} {{ target }}
|
||||||
@ -168,7 +168,7 @@ build module="__help" target="fail" mode="aot":
|
|||||||
just build-wasm-module {{ module }}
|
just build-wasm-module {{ module }}
|
||||||
just build-wasm-interp-array {{ 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-wasm-host {{ module }} {{ target }}
|
||||||
|
|
||||||
just build-system-startup {{ module }} {{ target }}
|
just build-system-startup {{ module }} {{ target }}
|
||||||
@ -191,7 +191,14 @@ build module="__help" target="fail" mode="aot":
|
|||||||
[doc("Run binary")]
|
[doc("Run binary")]
|
||||||
[group("5: just do it")]
|
[group("5: just do it")]
|
||||||
run module:
|
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")]
|
[doc("Send binaries to mars")]
|
||||||
[group("5: just do it")]
|
[group("5: just do it")]
|
||||||
|
|||||||
@ -1,16 +1,20 @@
|
|||||||
#include "../lib.h"
|
#include "../lib.h"
|
||||||
|
|
||||||
|
#ifdef TARGET_LINUX
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
void fail_start_trace(void) {}
|
void fail_start_trace(void) {}
|
||||||
void fail_stop_trace(void) {}
|
void fail_stop_trace(void) {}
|
||||||
void fail_marker_positive(void) {}
|
void fail_marker_positive(void) {}
|
||||||
void fail_marker_negative(void) {}
|
void fail_marker_negative(void) {}
|
||||||
void fail_marker_detected(void) {}
|
void fail_marker_detected(void) {}
|
||||||
|
void print(const char *msg) { PRINT("[C] %s", msg); }
|
||||||
|
|
||||||
int wasm_module(void);
|
int wasm_module(void);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
MAIN {
|
||||||
int retval = wasm_module();
|
int retval = wasm_module();
|
||||||
printf("Function returned %d.\n", retval);
|
PRINT_SUCCESS("wasm_module returned %d.\n", retval);
|
||||||
return retval;
|
RET(retval);
|
||||||
}
|
}
|
||||||
@ -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(); }
|
|
||||||
@ -4,21 +4,57 @@
|
|||||||
#define INLINE __attribute__((always_inline)) inline
|
#define INLINE __attribute__((always_inline)) inline
|
||||||
#define NOINLINE __attribute__((noinline))
|
#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
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Mark start of injection
|
// Those functions are defined in the host program
|
||||||
void NOINLINE fail_start_trace(void);
|
void NOINLINE fail_start_trace(void); // Mark start of injection
|
||||||
// Mark end of injection
|
void NOINLINE fail_stop_trace(void); // Mark end of injection
|
||||||
void NOINLINE fail_stop_trace(void);
|
void NOINLINE fail_marker_positive(void); // Everything ok
|
||||||
|
void NOINLINE fail_marker_detected(void); // Everything ok
|
||||||
// everything ok: valid code and right values
|
void NOINLINE fail_marker_negative(void); // Invalid code
|
||||||
void NOINLINE fail_marker_positive(void);
|
void NOINLINE print(const char *msg);
|
||||||
// everything ok: valid code but wrong values
|
|
||||||
void NOINLINE fail_marker_negative(void);
|
|
||||||
// invalid code
|
|
||||||
void NOINLINE fail_marker_detected(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
139
targets/wasm-host/wasm_host.c
Normal file
139
targets/wasm-host/wasm_host.c
Normal 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);
|
||||||
|
}
|
||||||
@ -35,8 +35,11 @@ typedef int8_t sign_t;
|
|||||||
static enc_t cored_res;
|
static enc_t cored_res;
|
||||||
static enc_t sum_out[REPLICA_COUNT];
|
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) {
|
static INLINE enc_t apply(enc_t vc, sign_t bdyn) {
|
||||||
if (bdyn > SIG_MAX) {
|
if (bdyn > SIG_MAX) {
|
||||||
|
HOST_PRINT("signature overflow.\n");
|
||||||
fail_marker_detected();
|
fail_marker_detected();
|
||||||
}
|
}
|
||||||
return vc + bdyn;
|
return vc + bdyn;
|
||||||
@ -58,6 +61,7 @@ static sign_t cored_vote(void) {
|
|||||||
cored_res = apply(XC, (XC - ZC));
|
cored_res = apply(XC, (XC - ZC));
|
||||||
return SIG_s_XZ;
|
return SIG_s_XZ;
|
||||||
} else {
|
} else {
|
||||||
|
HOST_PRINT("all replicas differ.\n");
|
||||||
fail_marker_detected();
|
fail_marker_detected();
|
||||||
return 0;
|
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);
|
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;
|
XC = 0;
|
||||||
YC = 0;
|
YC = 0;
|
||||||
ZC = 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();
|
fail_start_trace();
|
||||||
|
|
||||||
sum<0, SIG_X>();
|
sum<0, SIG_X>();
|
||||||
@ -102,6 +102,7 @@ extern "C" int wasm_module(void) {
|
|||||||
vote_result_sig = SIG_Y;
|
vote_result_sig = SIG_Y;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
HOST_PRINT("unknown static_sig.\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,15 +111,18 @@ extern "C" int wasm_module(void) {
|
|||||||
|
|
||||||
/* Validate Vote result */
|
/* Validate Vote result */
|
||||||
if (!check(cored_res, THE_A, vote_result_sig)) {
|
if (!check(cored_res, THE_A, vote_result_sig)) {
|
||||||
|
HOST_PRINT("voted result invalid.\n");
|
||||||
fail_marker_detected();
|
fail_marker_detected();
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
plain_t res = decode(cored_res, THE_A, vote_result_sig);
|
plain_t res = decode(cored_res, THE_A, vote_result_sig);
|
||||||
if (res == 100) {
|
if (res == 100) {
|
||||||
|
HOST_PRINT("cored success.\n");
|
||||||
fail_marker_positive();
|
fail_marker_positive();
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
HOST_PRINT("undetected error.\n");
|
||||||
fail_marker_negative();
|
fail_marker_negative();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "../lib.h"
|
#include "../lib.h"
|
||||||
|
|
||||||
extern "C" int wasm_module(void) {
|
extern "C" EXPORT("wasm_module") int wasm_module(void) {
|
||||||
fail_start_trace();
|
fail_start_trace();
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
|
|||||||
20
wasm.just
20
wasm.just
@ -10,7 +10,7 @@ WASI_CFLAGS := "\
|
|||||||
-O0 \
|
-O0 \
|
||||||
-nostdlib \
|
-nostdlib \
|
||||||
-Wl,--no-entry \
|
-Wl,--no-entry \
|
||||||
-Wl,--export-all \
|
-Wl,--export=wasm_module \
|
||||||
-Wl,--no-gc-sections \
|
-Wl,--no-gc-sections \
|
||||||
-Wl,--initial-memory=65536 \
|
-Wl,--initial-memory=65536 \
|
||||||
-Wl,--export=__heap_base \
|
-Wl,--export=__heap_base \
|
||||||
@ -101,6 +101,7 @@ build-c-module module target="fail":
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
[private]
|
||||||
copy-c-module module:
|
copy-c-module module:
|
||||||
cp targets/wasm-module/{{ module }}.cpp {{ BUILD_DIR }}-{{ module }}/wasm-module.cpp
|
cp targets/wasm-module/{{ module }}.cpp {{ BUILD_DIR }}-{{ module }}/wasm-module.cpp
|
||||||
|
|
||||||
@ -158,8 +159,8 @@ LINUX_BAREMETAL_INCLUDES := f"\
|
|||||||
|
|
||||||
[doc("Insert the AOT array into the host program")]
|
[doc("Insert the AOT array into the host program")]
|
||||||
[group("2: build host")]
|
[group("2: build host")]
|
||||||
prepare-aot-host module target="fail":
|
prepare-aot-host module:
|
||||||
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
|
cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c
|
||||||
sed -i \
|
sed -i \
|
||||||
-e "s/__WASM_ARRAY_FILE__/wasm_aot_array.c/g" \
|
-e "s/__WASM_ARRAY_FILE__/wasm_aot_array.c/g" \
|
||||||
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_aot/g" \
|
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_aot/g" \
|
||||||
@ -168,8 +169,8 @@ prepare-aot-host module target="fail":
|
|||||||
|
|
||||||
[doc("Insert the WASM array into the host program")]
|
[doc("Insert the WASM array into the host program")]
|
||||||
[group("2: build host")]
|
[group("2: build host")]
|
||||||
prepare-interp-host module target="fail":
|
prepare-interp-host module:
|
||||||
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
|
cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c
|
||||||
sed -i \
|
sed -i \
|
||||||
-e "s/__WASM_ARRAY_FILE__/wasm_interp_array.c/g" \
|
-e "s/__WASM_ARRAY_FILE__/wasm_interp_array.c/g" \
|
||||||
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_wasm/g" \
|
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_wasm/g" \
|
||||||
@ -179,18 +180,21 @@ prepare-interp-host module target="fail":
|
|||||||
[private]
|
[private]
|
||||||
build-wasm-host-fail module:
|
build-wasm-host-fail module:
|
||||||
{{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \
|
{{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \
|
||||||
|
-DTARGET_FAIL \
|
||||||
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
||||||
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
||||||
|
|
||||||
[private]
|
[private]
|
||||||
build-wasm-host-linux module:
|
build-wasm-host-linux module:
|
||||||
{{ LINUX_CC }} {{ LINUX_CFLAGS }} {{ LINUX_INCLUDES }} \
|
{{ LINUX_CC }} {{ LINUX_CFLAGS }} {{ LINUX_INCLUDES }} \
|
||||||
|
-DTARGET_LINUX \
|
||||||
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
||||||
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
||||||
|
|
||||||
[private]
|
[private]
|
||||||
build-wasm-host-linux-baremetal module:
|
build-wasm-host-linux-baremetal module:
|
||||||
{{ CROSS_CC }} {{ LINUX_BAREMETAL_CFLAGS }} {{ LINUX_BAREMETAL_INCLUDES }} \
|
{{ CROSS_CC }} {{ LINUX_BAREMETAL_CFLAGS }} {{ LINUX_BAREMETAL_INCLUDES }} \
|
||||||
|
-DTARGET_LINUX_BAREMETAL \
|
||||||
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
|
||||||
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
-o {{ BUILD_DIR }}-{{ module }}/system.o
|
||||||
|
|
||||||
@ -212,13 +216,15 @@ build-wasm-host module target="fail":
|
|||||||
[private]
|
[private]
|
||||||
build-c-host-fail module:
|
build-c-host-fail module:
|
||||||
{{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \
|
{{ 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
|
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
|
||||||
|
|
||||||
[private]
|
[private]
|
||||||
build-c-host-linux module:
|
build-c-host-linux module:
|
||||||
{{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \
|
{{ 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
|
-o {{ BUILD_DIR }}-{{ module }}/c_host.o
|
||||||
|
|
||||||
[doc("Insert the C function into the host program (no WASM)")]
|
[doc("Insert the C function into the host program (no WASM)")]
|
||||||
|
|||||||
Reference in New Issue
Block a user