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,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);

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;
}

View File

@ -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 <stdio.h>
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;
}
}

File diff suppressed because it is too large Load Diff