diff --git a/targets/c-host/fail.c b/targets/c-host/fail.c index b48fbaf..a8da95e 100644 --- a/targets/c-host/fail.c +++ b/targets/c-host/fail.c @@ -1,5 +1,4 @@ #include "../lib.h" -#include "../lib_cored.h" void fail_start_trace(void) {} void fail_stop_trace(void) {} @@ -7,47 +6,6 @@ void fail_marker_positive(void) {} void fail_marker_negative(void) {} void fail_marker_detected(void) {} -void init_outputs(plain_t x_c, plain_t y_c, plain_t z_c); int wasm_module(void); -enc_t get_cored_res(void); -void os_main(void) { - init_outputs(100, 142, 100); - - fail_start_trace(); - sign_t static_sig = wasm_module(); - fail_stop_trace(); - - sign_t vote_result_sig; - switch (static_sig) { - case SIG_s_XYZ: - case SIG_s_XY: - case SIG_s_XZ: - vote_result_sig = SIG_X; - break; - case SIG_s_YZ: - vote_result_sig = SIG_Y; - break; - default: - break; - } - - // Inversely apply constant program flow signature. - enc_t cored_res = get_cored_res(); - cored_res -= static_sig; - - /* Validate Vote result */ - if (!check(cored_res, THE_A, vote_result_sig)) { - fail_marker_detected(); - return; - } - - plain_t res = decode(cored_res, THE_A, vote_result_sig); - if (res == 100) { - fail_marker_positive(); - return; - } else { - fail_marker_negative(); - return; - } -} +void os_main(void) { wasm_module(); } diff --git a/targets/c-host/linux.c b/targets/c-host/linux.c index f035b0a..bb6cee8 100644 --- a/targets/c-host/linux.c +++ b/targets/c-host/linux.c @@ -1,56 +1,16 @@ -#include "../lib_cored.h" +#include "../lib.h" #include -void init_outputs(plain_t x_c, plain_t y_c, plain_t z_c); -int wasm_module(void); -enc_t get_cored_res(void); - +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); + int main(int argc, char *argv[]) { - init_outputs(100, 142, 100); - sign_t static_sig = wasm_module(); - - sign_t vote_result_sig; - switch (static_sig) { - case SIG_s_XYZ: - case SIG_s_XY: - case SIG_s_XZ: - vote_result_sig = SIG_X; - break; - case SIG_s_YZ: - vote_result_sig = SIG_Y; - break; - default: - // printf("ERROR: Unknown static_sig %d\n", static_sig); - break; - } - - // Inversely apply constant program flow signature. - enc_t cored_res = get_cored_res(); - printf("Inversely apply: cored_res=%d static_sig=%d\n", cored_res, - static_sig); - cored_res -= static_sig; - - /* Validate Vote result */ - printf( - "Validating vote result: cored_res=%d THE_A=%d vote_result_sig = % d\n ", - cored_res, THE_A, vote_result_sig); - if (!check(cored_res, THE_A, vote_result_sig)) { - printf("Validation failed :(\n"); - // fail_marker_detected(); - return 1; - } - - plain_t res = decode(cored_res, THE_A, vote_result_sig); - // printf("Decoded result: %d (%f)\n", res, q_to_float(res)); - if (res == 100) { - // fail_marker_positive(); - printf("Marker Positive\n"); - return 0; - } else { - printf("Marker Negative\n"); - // fail_marker_negative(); - return 1; - } + int retval = wasm_module(); + printf("Function returned %d.\n", retval); + return retval; } diff --git a/targets/lib.h b/targets/lib.h index 0ab4162..1182766 100644 --- a/targets/lib.h +++ b/targets/lib.h @@ -4,6 +4,10 @@ #define INLINE __attribute__((always_inline)) inline #define NOINLINE __attribute__((noinline)) +#ifdef __cplusplus +extern "C" { +#endif + // Mark start of injection void NOINLINE fail_start_trace(void); // Mark end of injection @@ -16,4 +20,8 @@ void NOINLINE fail_marker_negative(void); // invalid code void NOINLINE fail_marker_detected(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/targets/lib_cored.h b/targets/lib_cored.h deleted file mode 100644 index 8074fa9..0000000 --- a/targets/lib_cored.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _include_lib_cored_h -#define _include_lib_cored_h - -#include - -#define SENSORS_COUNT 3 -#define FILTER_COUNT 3 - -typedef uint16_t enc_t; -typedef uint8_t plain_t; -typedef int8_t sign_t; - -#define check(vc, A, B) (((vc - B) % A) == 0) -#define encode(v, A, B) ((((plain_t)v) * ((sign_t)A)) + ((sign_t)B)) -#define decode(vc, A, B) ((vc - B) / A) - -#define equals(vc1, vc2, B1, B2) ((vc1 - vc2) == (B1 - B2)) - -#define THE_A 110 - -// CONSTANT Replica results signatures -#define SIG_X 32 -#define SIG_Y 23 -#define SIG_Z 67 -#define SIG_MAX SIG_Z - -// CONSTANT Voter Result Signatures -#define SIG_s_XYZ ((SIG_X - SIG_Y) + (SIG_X - SIG_Z)) -#define SIG_s_XY (SIG_X - SIG_Y) -#define SIG_s_YZ (SIG_Y - SIG_Z) -#define SIG_s_XZ (SIG_X - SIG_Z) - -#define XC filter_out[0] -#define YC filter_out[1] -#define ZC filter_out[2] - -#endif diff --git a/targets/wasm-host/fail.c b/targets/wasm-host/fail.c index e8358c6..33fb75d 100644 --- a/targets/wasm-host/fail.c +++ b/targets/wasm-host/fail.c @@ -1,22 +1,29 @@ #include "../lib.h" -#include "../lib_cored.h" - +#include "../wasm_export.h" #include "bh_platform.h" -#include "wasm_export.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) {} -void fail_start_trace_wasm(wasm_exec_env_t exec_env) {} -void fail_stop_trace_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_positive_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_negative_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_detected_wasm(wasm_exec_env_t exec_env) {} +// 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 @@ -38,6 +45,7 @@ void os_main() { 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, "()"}, @@ -53,7 +61,6 @@ void os_main() { /* 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; } @@ -61,7 +68,6 @@ void os_main() { /* 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; } @@ -70,81 +76,20 @@ void os_main() { // thread?) wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(module_inst, STACK_SIZE); - uint32_t args[FILTER_COUNT]; + uint32_t args[1]; - // Place encoded input values for cored voter. - // They'll be broken by FAIL* on read, so I don't need to inject this (?) - // fail_start_trace(); - wasm_function_inst_t init_func = - wasm_runtime_lookup_function(module_inst, "init_outputs"); - args[0] = 100; - args[1] = 142; - args[2] = 100; - if (!wasm_runtime_call_wasm(exec_env, init_func, 3, args)) { - return; - } - - // CoRed vote + // Call wasm function wasm_function_inst_t func = wasm_runtime_lookup_function(module_inst, "wasm_module"); - args[0] = 0; - args[1] = 0; - args[2] = 0; - fail_start_trace(); + if (!func) { + return; + } if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) { return; } - fail_stop_trace(); - sign_t static_sig = args[0]; - // Obtain dynamic signature produced by CoRed - wasm_function_inst_t cored_res_func = - wasm_runtime_lookup_function(module_inst, "get_cored_res"); - args[0] = 0; - args[1] = 0; - args[2] = 0; - if (!wasm_runtime_call_wasm(exec_env, cored_res_func, 0, args)) { - return; - } - enc_t cored_res = args[0]; - - sign_t vote_result_sig; - switch (static_sig) { - case SIG_s_XYZ: - case SIG_s_XY: - case SIG_s_XZ: - vote_result_sig = SIG_X; - break; - case SIG_s_YZ: - vote_result_sig = SIG_Y; - break; - default: - // printf("ERROR: Unknown static_sig %d\n", static_sig); - break; - } - - // Inversely apply constant program flow signature. - // printf("Inversely apply: cored_res=%d static_sig=%d\n", cored_res, - // static_sig); - cored_res -= static_sig; - - /* Validate Vote result */ - // printf( - // "Validating vote result: cored_res=%d THE_A=%d vote_result_sig = % d\n - // ", cored_res, THE_A, vote_result_sig); - if (!check(cored_res, THE_A, vote_result_sig)) { - // printf("Validation failed :(\n"); - fail_marker_detected(); - return; - } - - plain_t res = decode(cored_res, THE_A, vote_result_sig); - // printf("Decoded result: %d (%f)\n", res, q_to_float(res)); - if (res == 100) { - fail_marker_positive(); - } else { - fail_marker_negative(); - } + // In case wasm_module returns a value + uint32_t retval = args[0]; wasm_runtime_destroy_exec_env(exec_env); wasm_runtime_deinstantiate(module_inst); diff --git a/targets/wasm-host/linux-baremetal.c b/targets/wasm-host/linux-baremetal.c index 38d4b11..006a970 100644 --- a/targets/wasm-host/linux-baremetal.c +++ b/targets/wasm-host/linux-baremetal.c @@ -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; } diff --git a/targets/wasm-host/linux.c b/targets/wasm-host/linux.c index 4e3a6d4..ee551ab 100644 --- a/targets/wasm-host/linux.c +++ b/targets/wasm-host/linux.c @@ -1,16 +1,27 @@ -#include "../lib_cored.h" - +#include "../lib.h" +#include "../wasm_export.h" #include "bh_platform.h" -#include "wasm_export.h" #include "__WASM_ARRAY_FILE__" #include -void fail_start_trace_wasm(wasm_exec_env_t exec_env) {} -void fail_stop_trace_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_positive_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_negative_wasm(wasm_exec_env_t exec_env) {} -void fail_marker_detected_wasm(wasm_exec_env_t exec_env) {} +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 @@ -24,7 +35,6 @@ int main(int argc, char *argv[]) { static RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(RuntimeInitArgs)); - // init_args.mem_alloc_type = Alloc_With_System_Allocator; 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); @@ -33,6 +43,7 @@ int main(int argc, char *argv[]) { 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, "()"}, @@ -42,106 +53,56 @@ int main(int argc, char *argv[]) { }; 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("Load failed: %s\n", error_buf); + 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("Inst failed: %s\n", error_buf); + 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); - uint32_t args[FILTER_COUNT]; - - wasm_function_inst_t init_func = - wasm_runtime_lookup_function(module_inst, "init_outputs"); - args[0] = 100; - args[1] = 142; - args[2] = 100; - if (!wasm_runtime_call_wasm(exec_env, init_func, 3, args)) { - printf("Failed to call init_outputs\n"); + if (!exec_env) { + printf("Failed to create WASM execution environment.\n"); return 1; } - // CoRed vote + // Call wasm function + uint32_t args[1] = {0}; wasm_function_inst_t func = wasm_runtime_lookup_function(module_inst, "wasm_module"); - args[0] = 0; - args[1] = 0; - args[2] = 0; + 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_module\n"); + printf("Failed to call WASM function.\n"); return 1; } - sign_t static_sig = args[0]; - // Obtain dynamic signature produced by CoRed - wasm_function_inst_t cored_res_func = - wasm_runtime_lookup_function(module_inst, "get_cored_res"); - args[0] = 0; - args[1] = 0; - args[2] = 0; - if (!wasm_runtime_call_wasm(exec_env, cored_res_func, 0, args)) { - printf("Failed to call get_cored_res\n"); - return 1; - } - enc_t cored_res = args[0]; + // 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(); - - sign_t vote_result_sig; - switch (static_sig) { - case SIG_s_XYZ: - case SIG_s_XY: - case SIG_s_XZ: - vote_result_sig = SIG_X; - break; - case SIG_s_YZ: - vote_result_sig = SIG_Y; - break; - default: - printf("ERROR: Unknown static_sig %d\n", static_sig); - break; - } - - // Inversely apply constant program flow signature. - printf("Inversely apply: cored_res=%d static_sig=%d\n", cored_res, - static_sig); - cored_res -= static_sig; - - /* Validate Vote result */ - printf( - "Validating vote result: cored_res=%d THE_A=%d vote_result_sig = % d\n ", - cored_res, THE_A, vote_result_sig); - if (!check(cored_res, THE_A, vote_result_sig)) { - printf("Validation failed :(\n"); - return 1; - } - - plain_t res = decode(cored_res, THE_A, vote_result_sig); - // printf("Decoded result: %d (%f)\n", res, q_to_float(res)); - if (res == 100) { - printf("Marker Positive\n"); - return 0; - } else { - printf("Marker Negative\n"); - return 1; - } } diff --git a/targets/wasm-module/cored.c b/targets/wasm-module/cored.c deleted file mode 100644 index cc81a48..0000000 --- a/targets/wasm-module/cored.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "../lib.h" -#include "../lib_cored.h" - -static enc_t cored_res; -static enc_t filter_out[FILTER_COUNT]; - -void init_outputs(plain_t x_c, plain_t y_c, plain_t z_c) { - /* Replicated Filtersteps */ - XC = encode(x_c, THE_A, SIG_X); - YC = encode(y_c, THE_A, SIG_Y); - ZC = encode(z_c, THE_A, SIG_Z); -} - -enc_t INLINE apply(enc_t vc, sign_t bdyn) { - if (bdyn > SIG_MAX) { - fail_marker_detected(); - } - return vc + bdyn; -} - -sign_t wasm_module(void) { - if (equals(XC, YC, SIG_X, SIG_Y)) { - if (equals(XC, ZC, SIG_X, SIG_Z)) { - cored_res = apply(XC, (XC - YC) + (XC - ZC)); - return SIG_s_XYZ; - } else { - cored_res = apply(XC, (XC - YC)); - return SIG_s_XY; - } - } else if (equals(YC, ZC, SIG_Y, SIG_Z)) { - cored_res = apply(YC, (YC - ZC)); - return SIG_s_YZ; - } else if (equals(XC, ZC, SIG_X, SIG_Z)) { - cored_res = apply(XC, (XC - ZC)); - return SIG_s_XZ; - } else { - fail_marker_detected(); - return 0; - } -} - -enc_t get_cored_res() { return cored_res; } -// enc_t get_xc() { return XC; } -// enc_t get_yc() { return YC; } -// enc_t get_zc() { return ZC; } diff --git a/targets/wasm-module/cored.cpp b/targets/wasm-module/cored.cpp new file mode 100644 index 0000000..703026b --- /dev/null +++ b/targets/wasm-module/cored.cpp @@ -0,0 +1,125 @@ +#include + +#include "../lib.h" + +typedef uint16_t enc_t; +typedef uint8_t plain_t; +typedef int8_t sign_t; + +#define REPLICA_COUNT 3 + +#define check(vc, A, B) (((vc - B) % A) == 0) +#define encode(v, A, B) ((((plain_t)v) * ((sign_t)A)) + ((sign_t)B)) +#define decode(vc, A, B) ((vc - B) / A) + +#define equals(vc1, vc2, B1, B2) ((vc1 - vc2) == (B1 - B2)) + +#define THE_A 110 + +// CONSTANT Replica results signatures +#define SIG_X 32 +#define SIG_Y 23 +#define SIG_Z 67 +#define SIG_MAX SIG_Z + +// CONSTANT Voter Result Signatures +#define SIG_s_XYZ ((SIG_X - SIG_Y) + (SIG_X - SIG_Z)) +#define SIG_s_XY (SIG_X - SIG_Y) +#define SIG_s_YZ (SIG_Y - SIG_Z) +#define SIG_s_XZ (SIG_X - SIG_Z) + +#define XC sum_out[0] +#define YC sum_out[1] +#define ZC sum_out[2] + +static enc_t cored_res; +static enc_t sum_out[REPLICA_COUNT]; + +static INLINE enc_t apply(enc_t vc, sign_t bdyn) { + if (bdyn > SIG_MAX) { + fail_marker_detected(); + } + return vc + bdyn; +} + +static sign_t cored_vote(void) { + if (equals(XC, YC, SIG_X, SIG_Y)) { + if (equals(XC, ZC, SIG_X, SIG_Z)) { + cored_res = apply(XC, (XC - YC) + (XC - ZC)); + return SIG_s_XYZ; + } else { + cored_res = apply(XC, (XC - YC)); + return SIG_s_XY; + } + } else if (equals(YC, ZC, SIG_Y, SIG_Z)) { + cored_res = apply(YC, (YC - ZC)); + return SIG_s_YZ; + } else if (equals(XC, ZC, SIG_X, SIG_Z)) { + cored_res = apply(XC, (XC - ZC)); + return SIG_s_XZ; + } else { + fail_marker_detected(); + return 0; + } +} + +template static INLINE void sum(void) { + int sum = 0; + for (int i = 0; i < 100; ++i) { + sum += 1; + } + + sum_out[N] = encode(sum, THE_A, S); +} + +extern "C" 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>(); + sum<1, SIG_Y>(); + sum<2, SIG_Z>(); + + sign_t static_sig = cored_vote(); + + fail_stop_trace(); + + sign_t vote_result_sig; + switch (static_sig) { + case SIG_s_XYZ: + case SIG_s_XY: + case SIG_s_XZ: + vote_result_sig = SIG_X; + break; + case SIG_s_YZ: + vote_result_sig = SIG_Y; + break; + default: + break; + } + + // Inversely apply constant program flow signature. + cored_res -= static_sig; + + /* Validate Vote result */ + if (!check(cored_res, THE_A, vote_result_sig)) { + fail_marker_detected(); + return 2; + } + + plain_t res = decode(cored_res, THE_A, vote_result_sig); + if (res == 100) { + fail_marker_positive(); + return 0; + } else { + fail_marker_negative(); + return 1; + } +} diff --git a/targets/wasm-module/sum.c b/targets/wasm-module/sum.c deleted file mode 100644 index 121fb2b..0000000 --- a/targets/wasm-module/sum.c +++ /dev/null @@ -1,7 +0,0 @@ -int wasm_module(void) { - int sum = 0; - for (int i = 0; i < 100; ++i) { - sum += 1; - } - return sum; -} diff --git a/targets/wasm-module/sum.cpp b/targets/wasm-module/sum.cpp new file mode 100644 index 0000000..cf882c7 --- /dev/null +++ b/targets/wasm-module/sum.cpp @@ -0,0 +1,20 @@ +#include "../lib.h" + +extern "C" int wasm_module(void) { + fail_start_trace(); + + int sum = 0; + for (int i = 0; i < 100; ++i) { + sum += 1; + } + + fail_stop_trace(); + + if (sum == 100) { + fail_marker_positive(); + return 0; + } else { + fail_marker_negative(); + return 1; + } +} diff --git a/targets/wasm-host/wasm_export.h b/targets/wasm_export.h similarity index 100% rename from targets/wasm-host/wasm_export.h rename to targets/wasm_export.h diff --git a/wasm.just b/wasm.just index afadf78..9cdc234 100644 --- a/wasm.just +++ b/wasm.just @@ -23,6 +23,7 @@ CROSS_CFLAGS_NOWASM := "\ -ffunction-sections \ -fdata-sections \ -ffreestanding \ +-fpermissive \ -ggdb3 \ " CROSS_LDFLAGS_NOWASM := "\ @@ -39,6 +40,7 @@ LINUX_CFLAGS_NOWASM := "\ -m32 \ -ffunction-sections \ -fdata-sections \ +-fpermissive \ -ggdb3 \ " LINUX_LDFLAGS_NOWASM := "\ @@ -57,7 +59,7 @@ XXD := "xxd" [doc("C -> WASM: Compile a C function to a WASM module using WASI-SDK")] [group("1: build module")] build-wasm-module module: - {{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.c -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm + {{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.cpp -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm [doc("WASM -> AOT: Compile a WASM module ahead-of-time using WAMR")] [group("1: build module")] @@ -77,13 +79,13 @@ build-wasm-interp-array module: [private] build-c-module-fail module: {{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \ - -c targets/wasm-module/{{ module }}.c \ + -c targets/wasm-module/{{ module }}.cpp \ -o {{ BUILD_DIR }}-{{ module }}/c_module.o [private] build-c-module-linux module: {{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \ - -c targets/wasm-module/{{ module }}.c \ + -c targets/wasm-module/{{ module }}.cpp \ -o {{ BUILD_DIR }}-{{ module }}/c_module.o [doc("C -> Object: Compile a C function (no WASM)")]