diff --git a/.gitignore b/.gitignore index 99f1a502e4..fef0c762df 100644 --- a/.gitignore +++ b/.gitignore @@ -21,8 +21,6 @@ wamr-sdk/out/ wamr-sdk/runtime/build_runtime_sdk/ test-tools/host-tool/bin/ product-mini/app-samples/hello-world/test.wasm -product-mini/platforms/linux-sgx/enclave-sample/App/ -product-mini/platforms/linux-sgx/enclave-sample/Enclave/ product-mini/platforms/linux-sgx/enclave-sample/iwasm build_out @@ -39,4 +37,12 @@ tests/benchmarks/coremark/coremark* samples/workload/include/** !samples/workload/include/.gitkeep -# core/iwasm/libraries/wasi-threads \ No newline at end of file +# core/iwasm/libraries/wasi-threads + +*_u.c +*_u.h +*_t.c +*_t.h +*.o +product-mini/app-samples/hello-world/iwasm +product-mini/app-samples/hello-world/test_wasm.h diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index 314938d60f..62e85ad6fd 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -66,6 +66,7 @@ void wasm_shared_memory_destroy() { bh_hash_map_destroy(wait_map); + wait_map = NULL; os_mutex_destroy(&shared_memory_list_lock); } diff --git a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp index 2b5300ff36..e3ac7d3864 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp @@ -281,34 +281,12 @@ split_string(char *str, int *count) return res; } -typedef enum EcallCmd { - CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */ - CMD_LOAD_MODULE, /* wasm_runtime_load() */ - CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */ - CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */ - CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */ - CMD_CALL_WASM, /* wasm_runtime_call_wasm */ - CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */ - CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */ - CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */ - CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */ - CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */ - CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */ - CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */ - CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */ - CMD_GET_VERSION, /* wasm_runtime_get_version() */ -#if WASM_ENABLE_STATIC_PGO != 0 - CMD_GET_PGO_PROF_BUF_SIZE, /* wasm_runtime_get_pro_prof_data_size() */ - CMD_DUMP_PGO_PROF_BUF_DATA, /* wasm_runtime_dump_pgo_prof_data_to_buf() */ -#endif -} EcallCmd; - static void -app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc, - char **app_argv); +app_instance_func(uint16_t wasm_module_inst, const char *func_name, + int app_argc, char **app_argv); static void * -app_instance_repl(void *module_inst, int app_argc, char **app_argv) +app_instance_repl(uint16_t module_inst_idx, int app_argc, char **app_argv) { char *cmd = NULL; size_t len = 0; @@ -332,7 +310,7 @@ app_instance_repl(void *module_inst, int app_argc, char **app_argv) break; } if (app_argc != 0) { - app_instance_func(module_inst, app_argv[0], app_argc - 1, + app_instance_func(module_inst_idx, app_argv[0], app_argc - 1, app_argv + 1); } free(app_argv); @@ -361,15 +339,11 @@ validate_env_str(char *env) static bool set_log_verbose_level(int log_verbose_level) { - uint64_t ecall_args[1]; - /* Set log verbose level */ if (log_verbose_level != 2) { - ecall_args[0] = log_verbose_level; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL, - (uint8_t *)ecall_args, sizeof(uint64_t))) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_set_log_level(g_eid, log_verbose_level)) { + printf("Call ecall_handle_cmd_set_log_level() failed.\n"); return false; } } @@ -379,16 +353,14 @@ set_log_verbose_level(int log_verbose_level) static bool init_runtime(uint32_t max_thread_num) { - uint64_t ecall_args[1]; + bool ecall_ret = false; - ecall_args[0] = max_thread_num; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args, - sizeof(ecall_args))) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_init_runtime(g_eid, &ecall_ret, max_thread_num)) { + printf("Call ecall_handle_cmd_init_runtime() failed.\n"); return false; } - if (!(bool)ecall_args[0]) { + if (!ecall_ret) { printf("Init runtime environment failed.\n"); return false; } @@ -398,230 +370,168 @@ init_runtime(uint32_t max_thread_num) static void destroy_runtime() { - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, NULL, 0)) { - printf("Call ecall_handle_command() failed.\n"); + if (SGX_SUCCESS != ecall_handle_cmd_destroy_runtime(g_eid)) { + printf("Call ecall_handle_cmd_destroy_runtime() failed.\n"); } } -static void * +static bool load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, char *error_buf, - uint32_t error_buf_size) + uint32_t error_buf_size, uint16_t *enclave_module_idx) { - uint64_t ecall_args[4]; - - ecall_args[0] = (uint64_t)(uintptr_t)wasm_file_buf; - ecall_args[1] = wasm_file_size; - ecall_args[2] = (uint64_t)(uintptr_t)error_buf; - ecall_args[3] = error_buf_size; - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_LOAD_MODULE, (uint8_t *)ecall_args, - sizeof(uint64_t) * 4)) { - printf("Call ecall_handle_command() failed.\n"); - return NULL; + bool ecall_ret = false; + + if ((SGX_SUCCESS + != ecall_handle_cmd_load_module( + g_eid, &ecall_ret, (char *)wasm_file_buf, wasm_file_size, + error_buf, error_buf_size, enclave_module_idx)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_load_module() failed.\n"); + return false; } - return (void *)(uintptr_t)ecall_args[0]; + return true; } static void -unload_module(void *wasm_module) +unload_module(uint16_t wasm_module_idx) { - uint64_t ecall_args[1]; + bool ecall_ret = false; - ecall_args[0] = (uint64_t)(uintptr_t)wasm_module; - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, (uint8_t *)ecall_args, - sizeof(uint64_t))) { - printf("Call ecall_handle_command() failed.\n"); + if ((SGX_SUCCESS + != ecall_handle_cmd_unload_module(g_eid, &ecall_ret, wasm_module_idx)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_unload_module() failed.\n"); } } -static void * -instantiate_module(void *wasm_module, uint32_t stack_size, uint32_t heap_size, - char *error_buf, uint32_t error_buf_size) +static bool +instantiate_module(uint16_t wasm_module_idx, uint32_t stack_size, + uint32_t heap_size, char *error_buf, uint32_t error_buf_size, + uint16_t *wasm_module_inst_idx) { - uint64_t ecall_args[5]; - - ecall_args[0] = (uint64_t)(uintptr_t)wasm_module; - ecall_args[1] = stack_size; - ecall_args[2] = heap_size; - ecall_args[3] = (uint64_t)(uintptr_t)error_buf; - ecall_args[4] = error_buf_size; - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE, - (uint8_t *)ecall_args, sizeof(uint64_t) * 5)) { - printf("Call ecall_handle_command() failed.\n"); - return NULL; + bool ecall_ret = false; + + if ((SGX_SUCCESS + != ecall_handle_cmd_instantiate_module( + g_eid, &ecall_ret, wasm_module_idx, stack_size, heap_size, + error_buf, error_buf_size, wasm_module_inst_idx)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_instantiate_module() failed.\n"); + return false; } - return (void *)(uintptr_t)ecall_args[0]; + return true; } static void -deinstantiate_module(void *wasm_module_inst) +deinstantiate_module(uint16_t wasm_module_inst_idx) { - uint64_t ecall_args[1]; + bool ecall_ret = false; - ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE, - (uint8_t *)ecall_args, sizeof(uint64_t))) { - printf("Call ecall_handle_command() failed.\n"); + if ((SGX_SUCCESS + != ecall_handle_cmd_deinstantiate_module(g_eid, &ecall_ret, + wasm_module_inst_idx)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_deinstantiate_module() failed.\n"); } } static bool -get_exception(void *wasm_module_inst, char *exception, uint32_t exception_size) +get_exception(uint16_t wasm_module_inst_idx, char *exception, + uint32_t exception_size) { - uint64_t ecall_args[3]; + bool ecall_ret = false; - ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst; - ecall_args[1] = (uint64_t)(uintptr_t)exception; - ecall_args[2] = exception_size; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_GET_EXCEPTION, (uint8_t *)ecall_args, - sizeof(uint64_t) * 3)) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_get_exception(g_eid, &ecall_ret, + wasm_module_inst_idx, exception, + exception_size)) { + printf("Call ecall_handle_cmd_get_exception() failed.\n"); + return false; } - return (bool)ecall_args[0]; + return ecall_ret; } static void -app_instance_main(void *wasm_module_inst, int app_argc, char **app_argv) +app_instance_main(uint16_t wasm_module_inst_idx, int app_argc, char **app_argv) { char exception[128]; - uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; - int i, size; - - if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { - if (!(ecall_args = - (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 2)))) { - printf("Allocate memory failed.\n"); - return; - } - } + bool ecall_ret = false; - ecall_args[0] = (uintptr_t)wasm_module_inst; - ecall_args[1] = app_argc; - for (i = 0; i < app_argc; i++) { - ecall_args[i + 2] = (uintptr_t)app_argv[i]; + if ((SGX_SUCCESS + != ecall_handle_cmd_exec_app_main( + g_eid, &ecall_ret, wasm_module_inst_idx, app_argv, app_argc)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_exec_app_main() failed.\n"); } - size = (uint32_t)sizeof(uint64_t) * (app_argc + 2); - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, (uint8_t *)ecall_args, - size)) { - printf("Call ecall_handle_command() failed.\n"); - } - - if (get_exception(wasm_module_inst, exception, sizeof(exception))) { + if (get_exception(wasm_module_inst_idx, exception, sizeof(exception))) { printf("%s\n", exception); } - - if (ecall_args != ecall_args_buf) { - free(ecall_args); - } } static void -app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc, - char **app_argv) +app_instance_func(uint16_t wasm_module_inst_idx, const char *func_name, + int app_argc, char **app_argv) { - uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf; - int i, size; - - if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) { - if (!(ecall_args = - (uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 3)))) { - printf("Allocate memory failed.\n"); - return; - } - } + bool ecall_ret = false; - ecall_args[0] = (uintptr_t)wasm_module_inst; - ecall_args[1] = (uintptr_t)func_name; - ecall_args[2] = (uintptr_t)app_argc; - for (i = 0; i < app_argc; i++) { - ecall_args[i + 3] = (uintptr_t)app_argv[i]; - } - - size = (uint32_t)sizeof(uint64_t) * (app_argc + 3); - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, (uint8_t *)ecall_args, - size)) { - printf("Call ecall_handle_command() failed.\n"); - } - - if (ecall_args != ecall_args_buf) { - free(ecall_args); + if ((SGX_SUCCESS + != ecall_handle_cmd_exec_app_func(g_eid, &ecall_ret, + wasm_module_inst_idx, func_name, + app_argv, app_argc)) + or (ecall_ret == false)) { + printf("Call ecall_handle_cmd_exec_app_func() failed.\n"); } } static bool -set_wasi_args(void *wasm_module, const char **dir_list, uint32_t dir_list_size, - const char **env_list, uint32_t env_list_size, int stdinfd, - int stdoutfd, int stderrfd, char **argv, uint32_t argc, - const char **addr_pool, uint32_t addr_pool_size) +set_wasi_args(uint16_t wasm_module_idx, const char **dir_list, + uint32_t dir_list_size, const char **env_list, + uint32_t env_list_size, int stdinfd, int stdoutfd, int stderrfd, + char **argv, uint32_t argc, const char **addr_pool, + uint32_t addr_pool_size) { - uint64_t ecall_args[12]; - - ecall_args[0] = (uint64_t)(uintptr_t)wasm_module; - ecall_args[1] = (uint64_t)(uintptr_t)dir_list; - ecall_args[2] = dir_list_size; - ecall_args[3] = (uint64_t)(uintptr_t)env_list; - ecall_args[4] = env_list_size; - ecall_args[5] = stdinfd; - ecall_args[6] = stdoutfd; - ecall_args[7] = stderrfd; - ecall_args[8] = (uint64_t)(uintptr_t)argv; - ecall_args[9] = argc; - ecall_args[10] = (uint64_t)(uintptr_t)addr_pool; - ecall_args[11] = addr_pool_size; + bool ecall_ret = false; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, (uint8_t *)ecall_args, - sizeof(uint64_t) * 12)) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_set_wasi_args( + g_eid, &ecall_ret, wasm_module_idx, dir_list, dir_list_size, + env_list, env_list_size, stdinfd, stdoutfd, stderrfd, argv, argc, + addr_pool, addr_pool_size)) { + printf("Call ecall_handle_cmd_set_wasi_args() failed.\n"); } - return (bool)ecall_args[0]; + return ecall_ret; } static void get_version(uint64_t *major, uint64_t *minor, uint64_t *patch) { - uint64_t ecall_args[3] = { 0 }; - if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_GET_VERSION, (uint8_t *)ecall_args, - sizeof(ecall_args))) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_get_version(g_eid, (uint32_t *)major, + (uint32_t *)minor, (uint32_t *)patch)) { + printf("Call ecall_handle_cmd_get_version() failed.\n"); return; } - - *major = ecall_args[0]; - *minor = ecall_args[1]; - *patch = ecall_args[2]; } #if WASM_ENABLE_STATIC_PGO != 0 static void -dump_pgo_prof_data(void *module_inst, const char *path) +dump_pgo_prof_data(uint16_t module_inst_idx, const char *path) { char *buf; uint32_t len; FILE *file; - uint64_t ecall_args[1]; - ecall_args[0] = (uint64_t)(uintptr_t)module_inst; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_GET_PGO_PROF_BUF_SIZE, - (uint8_t *)ecall_args, sizeof(ecall_args))) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_get_pgo_prof_buf_size(g_eid, &len, + module_inst_idx)) { + printf("Call ecall_handle_cmd_get_pgo_prof_buf_size() failed.\n"); return; } - if (!(len = ecall_args[0])) { + if (!len) { printf("failed to get LLVM PGO profile data size\n"); return; } @@ -631,19 +541,14 @@ dump_pgo_prof_data(void *module_inst, const char *path) return; } - uint64_t ecall_args_2[3]; - ecall_args_2[0] = (uint64_t)(uintptr_t)module_inst; - ecall_args_2[1] = (uint64_t)(uintptr_t)buf; - ecall_args_2[2] = len; if (SGX_SUCCESS - != ecall_handle_command(g_eid, CMD_DUMP_PGO_PROF_BUF_DATA, - (uint8_t *)ecall_args_2, - sizeof(ecall_args_2))) { - printf("Call ecall_handle_command() failed.\n"); + != ecall_handle_cmd_get_pgo_prof_buf_data(g_eid, &len, module_inst_idx, + buf, len)) { + printf("Call ecall_handle_cmd_get_pgo_prof_buf_data() failed.\n"); free(buf); return; } - if (!(len = ecall_args_2[0])) { + if (!len) { printf("failed to dump LLVM PGO profile data\n"); free(buf); return; @@ -672,8 +577,8 @@ main(int argc, char *argv[]) uint8_t *wasm_file_buf = NULL; uint32_t wasm_file_size; uint32_t stack_size = 64 * 1024, heap_size = 16 * 1024; - void *wasm_module = NULL; - void *wasm_module_inst = NULL; + uint16_t wasm_module_idx = 0; + uint16_t wasm_module_inst_idx = 0; char error_buf[128] = { 0 }; int log_verbose_level = 2; bool is_repl_mode = false; @@ -825,14 +730,14 @@ main(int argc, char *argv[]) } /* Load module */ - if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, error_buf, - sizeof(error_buf)))) { + if (!load_module(wasm_file_buf, wasm_file_size, error_buf, + sizeof(error_buf), &wasm_module_idx)) { printf("%s\n", error_buf); goto fail2; } /* Set wasi arguments */ - if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list, + if (!set_wasi_args(wasm_module_idx, dir_list, dir_list_size, env_list, env_list_size, 0, 1, 2, argv, argc, addr_pool, addr_pool_size)) { printf("%s\n", "set wasi arguments failed.\n"); @@ -840,19 +745,18 @@ main(int argc, char *argv[]) } /* Instantiate module */ - if (!(wasm_module_inst = - instantiate_module(wasm_module, stack_size, heap_size, error_buf, - sizeof(error_buf)))) { + if (!instantiate_module(wasm_module_idx, stack_size, heap_size, error_buf, + sizeof(error_buf), &wasm_module_inst_idx)) { printf("%s\n", error_buf); goto fail3; } if (is_repl_mode) - app_instance_repl(wasm_module_inst, argc, argv); + app_instance_repl(wasm_module_inst_idx, argc, argv); else if (func_name) - app_instance_func(wasm_module_inst, func_name, argc - 1, argv + 1); + app_instance_func(wasm_module_inst_idx, func_name, argc - 1, argv + 1); else - app_instance_main(wasm_module_inst, argc, argv); + app_instance_main(wasm_module_inst_idx, argc, argv); #if WASM_ENABLE_STATIC_PGO != 0 if (gen_prof_file) @@ -862,11 +766,11 @@ main(int argc, char *argv[]) ret = 0; /* Deinstantiate module */ - deinstantiate_module(wasm_module_inst); + deinstantiate_module(wasm_module_inst_idx); fail3: /* Unload module */ - unload_module(wasm_module); + unload_module(wasm_module_idx); fail2: /* Free the file buffer */ @@ -911,7 +815,7 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) uint32_t addr_pool_size = 0; uint32_t max_thread_num = 4; char *wasm_files[16]; - void *wasm_module_inst[16]; + uint16_t wasm_module_inst_idx[16]; int stdinfd = -1; int stdoutfd = -1; int stderrfd = -1; @@ -960,7 +864,7 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) for (i = 0; i < len; ++i) { uint8_t *wasm_file_buf = NULL; uint32_t wasm_file_size; - void *wasm_module = NULL; + uint16_t wasm_module_idx; char error_buf[128] = { 0 }; /* Load WASM byte buffer from WASM bin file */ @@ -972,8 +876,8 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) } /* Load module */ - if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, - error_buf, sizeof(error_buf)))) { + if (!load_module(wasm_file_buf, wasm_file_size, error_buf, + sizeof(error_buf), &wasm_module_idx)) { printf("%s\n", error_buf); free(wasm_file_buf); destroy_runtime(); @@ -981,32 +885,32 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args) } /* Set wasi arguments */ - if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list, + if (!set_wasi_args(wasm_module_idx, dir_list, dir_list_size, env_list, env_list_size, stdinfd, stdoutfd, stderrfd, argv, argc, addr_pool, addr_pool_size)) { printf("%s\n", "set wasi arguments failed.\n"); - unload_module(wasm_module); + unload_module(wasm_module_idx); free(wasm_file_buf); destroy_runtime(); return -1; } /* Instantiate module */ - if (!(wasm_module_inst[i] = - instantiate_module(wasm_module, stack_size, heap_size, - error_buf, sizeof(error_buf)))) { + if (!instantiate_module(wasm_module_idx, stack_size, heap_size, + error_buf, sizeof(error_buf), + &(wasm_module_inst_idx[i]))) { printf("%s\n", error_buf); - unload_module(wasm_module); + unload_module(wasm_module_idx); free(wasm_file_buf); destroy_runtime(); return -1; } - app_instance_main(wasm_module_inst[i], argc, argv); + app_instance_main(wasm_module_inst_idx[i], argc, argv); /* Deinstantiate module */ - deinstantiate_module(wasm_module_inst[i]); - unload_module(wasm_module); + deinstantiate_module(wasm_module_inst_idx[i]); + unload_module(wasm_module_idx); free(wasm_file_buf); } diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index 9ed17e1c83..49234e1c5f 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Enclave_t.h" #include "wasm_export.h" @@ -33,28 +34,6 @@ enclave_print(const char *message) } } -typedef enum EcallCmd { - CMD_INIT_RUNTIME = 0, /* wasm_runtime_init/full_init() */ - CMD_LOAD_MODULE, /* wasm_runtime_load() */ - CMD_INSTANTIATE_MODULE, /* wasm_runtime_instantiate() */ - CMD_LOOKUP_FUNCTION, /* wasm_runtime_lookup_function() */ - CMD_CREATE_EXEC_ENV, /* wasm_runtime_create_exec_env() */ - CMD_CALL_WASM, /* wasm_runtime_call_wasm */ - CMD_EXEC_APP_FUNC, /* wasm_application_execute_func() */ - CMD_EXEC_APP_MAIN, /* wasm_application_execute_main() */ - CMD_GET_EXCEPTION, /* wasm_runtime_get_exception() */ - CMD_DEINSTANTIATE_MODULE, /* wasm_runtime_deinstantiate() */ - CMD_UNLOAD_MODULE, /* wasm_runtime_unload() */ - CMD_DESTROY_RUNTIME, /* wasm_runtime_destroy() */ - CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */ - CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */ - CMD_GET_VERSION, /* wasm_runtime_get_version() */ -#if WASM_ENABLE_STATIC_PGO != 0 - CMD_GET_PGO_PROF_BUF_SIZE, /* wasm_runtime_get_pro_prof_data_size() */ - CMD_DUMP_PGO_PROF_BUF_DATA, /* wasm_runtime_dump_pgo_prof_data_to_buf() */ -#endif -} EcallCmd; - typedef struct EnclaveModule { wasm_module_t module; uint8 *wasm_file; @@ -85,6 +64,111 @@ static korp_mutex enclave_module_list_lock = OS_THREAD_MUTEX_INITIALIZER; static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 }; #endif +class PointerManager +{ + public: + bool add(void *ptr, uint16_t *idx_output) + { + bool success = false; + pthread_rwlock_wrlock(&mRwlock); + for (uint16_t i = mCurrentIdx; i < std::numeric_limits::max(); + i++) { + if (mMap.count(i) == 0) { + mMap[i] = ptr; + *idx_output = i; + success = true; + mCurrentIdx = i + 1; + break; + } + } + pthread_rwlock_unlock(&mRwlock); + return success; + } + + void *get(uint16_t idx) + { + void *ptr = nullptr; + pthread_rwlock_rdlock(&mRwlock); + if (mMap.count(idx)) { + ptr = mMap[idx]; + } + pthread_rwlock_unlock(&mRwlock); + return ptr; + } + + bool remove(uint16_t idx) + { + bool success = false; + pthread_rwlock_wrlock(&mRwlock); + if (mMap.erase(idx) == 1) { + success = true; + if (idx < mCurrentIdx) { + mCurrentIdx = idx; + } + } + pthread_rwlock_unlock(&mRwlock); + return success; + } + + private: + std::unordered_map mMap; + uint16_t mCurrentIdx = 0; + pthread_rwlock_t mRwlock = PTHREAD_RWLOCK_INITIALIZER; +}; +PointerManager gEnclaveModuleMgr, gWasmModuleInstMgr; + +/// @brief Deep copy an array of char string +/// @param SrcCStrArray Source array of char string +/// @param DstCStrArray Dest. array of char string, if null, then +/// automatically malloced +/// @param Length Array length. If 0, cause fail +/// @return \p DstCStrArray. If failed, return null, char string elements will +/// be auto free-ed, if \p DstCStrArray is feed with null, it will be auto +/// free-ed before return, but if \p DstCStrArray is input with not null, it's +/// caller duty to free \p DstCStrArray. If success, user need to free all char +/// string elements before free this \p DstCStrArray. +char ** +DeepCopyCStrArray(char **SrcCStrArray, char **DstCStrArray, size_t Length) +{ + if ((SrcCStrArray == nullptr) or (Length == 0)) { + return nullptr; + } + bool DstIsNull = false; + if (DstCStrArray == nullptr) { + DstIsNull = true; + size_t AllocSize = Length * sizeof(char *); + if ((AllocSize <= Length /* Int Overflow */) + or (AllocSize + > std::numeric_limits::max() /* Too large */) + or (!(DstCStrArray = (char **)wasm_runtime_malloc( + AllocSize)) /* Malloc Fail */)) { + return nullptr; + } + } + + for (size_t i = 0; i < Length; i++) { + size_t AllocSize = (strlen(SrcCStrArray[i]) + 1) * sizeof(char); + if ((AllocSize > std::numeric_limits::max() /* Too large */) + or (!(DstCStrArray[i] = (char *)wasm_runtime_malloc( + AllocSize)) /* Malloc Fail */)) { + for (size_t j = 0; j < i; j++) { + wasm_runtime_free(DstCStrArray[j]); + DstCStrArray[j] = nullptr; + } + if (DstIsNull) { + // DstCStrArray is allocated by myself, free it + wasm_runtime_free(DstCStrArray); + DstCStrArray = nullptr; + } + return nullptr; + } + + strcpy(DstCStrArray[i], SrcCStrArray[i]); + } + + return DstCStrArray; +} + static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { @@ -92,18 +176,14 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) snprintf(error_buf, error_buf_size, "%s", string); } -static void -handle_cmd_init_runtime(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_init_runtime(uint32_t max_thread_num) { - uint32 max_thread_num; + bool ret = false; RuntimeInitArgs init_args; - bh_assert(argc == 1); - os_set_print_function(enclave_print); - max_thread_num = (uint32)args[0]; - memset(&init_args, 0, sizeof(RuntimeInitArgs)); init_args.max_thread_num = max_thread_num; @@ -118,17 +198,18 @@ handle_cmd_init_runtime(uint64 *args, uint32 argc) /* initialize runtime environment */ if (!wasm_runtime_full_init(&init_args)) { LOG_ERROR("Init runtime environment failed.\n"); - args[0] = false; - return; + ret = false; + goto exit; } - args[0] = true; - + ret = true; LOG_VERBOSE("Init runtime environment success.\n"); +exit: + return ret; } -static void -handle_cmd_destroy_runtime() +void +ecall_handle_cmd_destroy_runtime() { wasm_runtime_destroy(); @@ -201,18 +282,19 @@ is_xip_file(const uint8 *buf, uint32 size) return false; } -static void -handle_cmd_load_module(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_load_module(char *wasm_file, uint32_t wasm_file_size, + char *error_buf, uint32_t error_buf_size, + uint16_t *enclave_module_idx) { - uint64 *args_org = args; - char *wasm_file = *(char **)args++; - uint32 wasm_file_size = *(uint32 *)args++; - char *error_buf = *(char **)args++; - uint32 error_buf_size = *(uint32 *)args++; + bool ret = false; uint64 total_size = sizeof(EnclaveModule) + (uint64)wasm_file_size; EnclaveModule *enclave_module; - bh_assert(argc == 4); + if (wasm_file == nullptr or enclave_module_idx == nullptr) { + ret = false; + goto exit; + } if (!is_xip_file((uint8 *)wasm_file, wasm_file_size)) { if (total_size >= UINT32_MAX @@ -221,8 +303,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc) set_error_buf(error_buf, error_buf_size, "WASM module load failed: " "allocate memory failed."); - *(void **)args_org = NULL; - return; + ret = false; + goto exit; } memset(enclave_module, 0, (uint32)total_size); } @@ -235,8 +317,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc) NULL, (uint32)total_size, map_prot, map_flags))) { set_error_buf(error_buf, error_buf_size, "WASM module load failed: mmap memory failed."); - *(void **)args_org = NULL; - return; + ret = false; + goto exit; } memset(enclave_module, 0, (uint32)total_size); enclave_module->is_xip_file = true; @@ -254,11 +336,19 @@ handle_cmd_load_module(uint64 *args, uint32 argc) wasm_runtime_free(enclave_module); else os_munmap(enclave_module, (uint32)total_size); - *(void **)args_org = NULL; - return; + ret = false; + goto exit; } - *(EnclaveModule **)args_org = enclave_module; + if (!gEnclaveModuleMgr.add(enclave_module, enclave_module_idx)) { + wasm_runtime_unload(enclave_module->module); + if (!enclave_module->is_xip_file) + wasm_runtime_free(enclave_module); + else + os_munmap(enclave_module, (uint32)total_size); + ret = false; + goto exit; + } #if WASM_ENABLE_LIB_RATS != 0 /* Calculate the module hash */ @@ -275,14 +365,22 @@ handle_cmd_load_module(uint64 *args, uint32 argc) #endif LOG_VERBOSE("Load module success.\n"); + ret = true; +exit: + return ret; } -static void -handle_cmd_unload_module(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_unload_module(uint16_t enclave_module_idx) { - EnclaveModule *enclave_module = *(EnclaveModule **)args++; + bool ret = false; + EnclaveModule *enclave_module = + (EnclaveModule *)gEnclaveModuleMgr.get(enclave_module_idx); - bh_assert(argc == 1); + if (enclave_module == nullptr) { + ret = false; + goto exit; + } #if WASM_ENABLE_LIB_RATS != 0 /* Remove enclave module from enclave module list */ @@ -315,7 +413,14 @@ handle_cmd_unload_module(uint64 *args, uint32 argc) else os_munmap(enclave_module, enclave_module->total_size_mapped); + if (!gEnclaveModuleMgr.remove(enclave_module_idx)) { + ret = false; + goto exit; + } LOG_VERBOSE("Unload module success.\n"); + ret = true; +exit: + return ret; } #if WASM_ENABLE_LIB_RATS != 0 @@ -341,73 +446,108 @@ wasm_runtime_get_module_hash(wasm_module_t module) } #endif -static void -handle_cmd_instantiate_module(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_instantiate_module(uint16_t enclave_module_idx, + uint32_t stack_size, uint32_t heap_size, + char *error_buf, uint32_t error_buf_size, + uint16_t *wasm_module_inst_idx) { - uint64 *args_org = args; - EnclaveModule *enclave_module = *(EnclaveModule **)args++; - uint32 stack_size = *(uint32 *)args++; - uint32 heap_size = *(uint32 *)args++; - char *error_buf = *(char **)args++; - uint32 error_buf_size = *(uint32 *)args++; + bool ret = false; + EnclaveModule *enclave_module = nullptr; wasm_module_inst_t module_inst; - bh_assert(argc == 5); + if ((wasm_module_inst_idx == nullptr) + or !(enclave_module = + (EnclaveModule *)gEnclaveModuleMgr.get(enclave_module_idx))) { + ret = false; + goto exit; + } if (!(module_inst = wasm_runtime_instantiate(enclave_module->module, stack_size, heap_size, error_buf, error_buf_size))) { - *(void **)args_org = NULL; - return; + ret = false; + goto exit; } - *(wasm_module_inst_t *)args_org = module_inst; + if (!gWasmModuleInstMgr.add(module_inst, wasm_module_inst_idx)) { + ret = false; + wasm_runtime_deinstantiate(module_inst); + goto exit; + } LOG_VERBOSE("Instantiate module success.\n"); + ret = true; +exit: + return ret; } -static void -handle_cmd_deinstantiate_module(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_deinstantiate_module(uint16_t wasm_module_inst_idx) { - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++; + bool ret = false; + wasm_module_inst_t module_inst = + (wasm_module_inst_t)gWasmModuleInstMgr.get(wasm_module_inst_idx); - bh_assert(argc == 1); + if (module_inst == nullptr) { + ret = false; + goto exit; + } wasm_runtime_deinstantiate(module_inst); + if (!gWasmModuleInstMgr.remove(wasm_module_inst_idx)) { + ret = false; + goto exit; + } LOG_VERBOSE("Deinstantiate module success.\n"); + ret = true; +exit: + return ret; } -static void -handle_cmd_get_exception(uint64 *args, uint32 argc) +bool +ecall_handle_cmd_get_exception(uint16_t wasm_module_inst_idx, char *exception, + uint32_t exception_size) { - uint64 *args_org = args; - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++; - char *exception = *(char **)args++; - uint32 exception_size = *(uint32 *)args++; + bool ret = false; + wasm_module_inst_t module_inst = nullptr; const char *exception1; - bh_assert(argc == 3); + if ((exception == nullptr) + or (!(module_inst = (wasm_module_inst_t)gWasmModuleInstMgr.get( + wasm_module_inst_idx)))) { + ret = false; + goto exit; + } if ((exception1 = wasm_runtime_get_exception(module_inst))) { snprintf(exception, exception_size, "%s", exception1); - args_org[0] = true; + ret = true; } else { - args_org[0] = false; + ret = false; } +exit: + return ret; } -static void -handle_cmd_exec_app_main(uint64 *args, int32 argc) +bool +ecall_handle_cmd_exec_app_main(uint16_t wasm_module_inst_idx, char **u_app_argv, + uint32_t app_argc) { - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++; - uint32 app_argc = *(uint32 *)args++; + bool ret = false; + wasm_module_inst_t module_inst = nullptr; char **app_argv = NULL; uint64 total_size; int32 i; - bh_assert(argc >= 3); + if ((u_app_argv == nullptr) or (app_argc == 0) + or (!(module_inst = (wasm_module_inst_t)gWasmModuleInstMgr.get( + wasm_module_inst_idx)))) { + ret = false; + goto exit; + } bh_assert(app_argc >= 1); total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2); @@ -415,78 +555,115 @@ handle_cmd_exec_app_main(uint64 *args, int32 argc) if (total_size >= UINT32_MAX || !(app_argv = (char **)wasm_runtime_malloc(total_size))) { wasm_runtime_set_exception(module_inst, "allocate memory failed."); - return; + ret = false; + goto exit; } - for (i = 0; i < app_argc; i++) { - app_argv[i] = (char *)(uintptr_t)args[i]; + if (!DeepCopyCStrArray(u_app_argv, app_argv, app_argc)) { + wasm_runtime_free(app_argv); + app_argv = nullptr; + ret = false; + goto exit; } wasm_application_execute_main(module_inst, app_argc - 1, app_argv + 1); + for (uint32 i = 0; i < app_argc; i++) { + wasm_runtime_free(app_argv[i]); + app_argv[i] = nullptr; + } wasm_runtime_free(app_argv); + app_argv = nullptr; + ret = true; +exit: + return ret; } -static void -handle_cmd_exec_app_func(uint64 *args, int32 argc) +bool +ecall_handle_cmd_exec_app_func(uint16_t wasm_module_inst_idx, + const char *func_name, char **u_app_argv, + uint32_t app_argc) { - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++; - char *func_name = *(char **)args++; - uint32 app_argc = *(uint32 *)args++; + bool ret = false; + wasm_module_inst_t module_inst = nullptr; char **app_argv = NULL; uint64 total_size; - int32 i, func_name_len = strlen(func_name); + int32 i, func_name_len; - bh_assert(argc == app_argc + 3); + if ((func_name == nullptr) or ((u_app_argv == nullptr) xor (app_argc == 0)) + or (!(module_inst = (wasm_module_inst_t)gWasmModuleInstMgr.get( + wasm_module_inst_idx)))) { + ret = false; + goto exit; + } + func_name_len = strlen(func_name); total_size = sizeof(char *) * (app_argc > 2 ? (uint64)app_argc : 2); if (total_size >= UINT32_MAX || !(app_argv = (char **)wasm_runtime_malloc(total_size))) { wasm_runtime_set_exception(module_inst, "allocate memory failed."); - return; + ret = false; + goto exit; } - for (i = 0; i < app_argc; i++) { - app_argv[i] = (char *)(uintptr_t)args[i]; + /* if app_argc is 0, no need deep copy */ + if (app_argc != 0 and !DeepCopyCStrArray(u_app_argv, app_argv, app_argc)) { + wasm_runtime_free(app_argv); + app_argv = nullptr; + ret = false; + goto exit; } wasm_application_execute_func(module_inst, func_name, app_argc, app_argv); - wasm_runtime_free(app_argv); + /* if app_argc is 0, no deep copy need to free */ + for (int32 i = 0; i < app_argc; i++) { + wasm_runtime_free(app_argv[i]); + app_argv[i] = nullptr; + } + if (app_argv) { + wasm_runtime_free(app_argv); + app_argv = nullptr; + } + ret = true; +exit: + return ret; } -static void -handle_cmd_set_log_level(uint64 *args, uint32 argc) +void +ecall_handle_cmd_set_log_level(int log_level) { #if WASM_ENABLE_LOG != 0 - LOG_VERBOSE("Set log verbose level to %d.\n", (int)args[0]); - bh_log_set_verbose_level((int)args[0]); + LOG_VERBOSE("Set log verbose level to %d.\n", log_level); + bh_log_set_verbose_level(log_level); #endif } #ifndef SGX_DISABLE_WASI -static void -handle_cmd_set_wasi_args(uint64 *args, int32 argc) +bool +ecall_handle_cmd_set_wasi_args(uint16_t enclave_module_idx, + const char **dir_list, uint32_t dir_list_size, + const char **env_list, uint32_t env_list_size, + int stdinfd, int stdoutfd, int stderrfd, + char **wasi_argv, uint32_t wasi_argc, + const char **addr_pool_list, + uint32_t addr_pool_list_size) { - uint64 *args_org = args; - EnclaveModule *enclave_module = *(EnclaveModule **)args++; - char **dir_list = *(char ***)args++; - uint32 dir_list_size = *(uint32 *)args++; - char **env_list = *(char ***)args++; - uint32 env_list_size = *(uint32 *)args++; - int stdinfd = *(int *)args++; - int stdoutfd = *(int *)args++; - int stderrfd = *(int *)args++; - char **wasi_argv = *(char ***)args++; + bool ret = false; + EnclaveModule *enclave_module = nullptr; char *p, *p1; - uint32 wasi_argc = *(uint32 *)args++; - char **addr_pool_list = *(char ***)args++; - uint32 addr_pool_list_size = *(uint32 *)args++; uint64 total_size = 0; int32 i, str_len; - bh_assert(argc == 10); + if ((!dir_list and dir_list_size != 0) or (!env_list and env_list_size != 0) + or (!wasi_argv and wasi_argc != 0) + or (!addr_pool_list and addr_pool_list_size != 0) + or !(enclave_module = + (EnclaveModule *)gEnclaveModuleMgr.get(enclave_module_idx))) { + ret = false; + goto exit; + } total_size += sizeof(char *) * (uint64)dir_list_size + sizeof(char *) * (uint64)env_list_size @@ -512,8 +689,8 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc) if (total_size >= UINT32_MAX || !(enclave_module->wasi_arg_buf = p = (char *)wasm_runtime_malloc((uint32)total_size))) { - *args_org = false; - return; + ret = false; + goto exit; } p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size @@ -579,125 +756,88 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc) (const char **)enclave_module->wasi_addr_pool_list, addr_pool_list_size); - *args_org = true; + ret = true; +exit: + return ret; } #else -static void -handle_cmd_set_wasi_args(uint64 *args, int32 argc) +bool +ecall_handle_cmd_set_wasi_args(uint16_t enclave_module_idx, + const char **dir_list, uint32_t dir_list_size, + const char **env_list, uint32_t env_list_size, + int stdinfd, int stdoutfd, int stderrfd, + char **wasi_argv, uint32_t wasi_argc, + const char **addr_pool_list, + uint32_t addr_pool_list_size) { - *args = true; + return true; } #endif /* end of SGX_DISABLE_WASI */ -static void -handle_cmd_get_version(uint64 *args, uint32 argc) +void +ecall_handle_cmd_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch) { - uint32 major, minor, patch; - bh_assert(argc == 3); - - wasm_runtime_get_version(&major, &minor, &patch); - args[0] = major; - args[1] = minor; - args[2] = patch; + if (major and minor and patch) { + wasm_runtime_get_version(major, minor, patch); + } } #if WASM_ENABLE_STATIC_PGO != 0 -static void -handle_cmd_get_pgo_prof_buf_size(uint64 *args, int32 argc) +uint32_t +ecall_handle_cmd_get_pgo_prof_buf_size(uint16_t wasm_module_inst_idx) { - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args; + wasm_module_inst_t module_inst = nullptr; uint32 buf_len; - bh_assert(argc == 1); + if ((module_inst = + (wasm_module_inst_t)gWasmModuleInstMgr.get(wasm_module_inst_idx)) + == nullptr) { + return 0; + } buf_len = wasm_runtime_get_pgo_prof_data_size(module_inst); - args[0] = buf_len; + return buf_len; } -static void -handle_cmd_get_pro_prof_buf_data(uint64 *args, int32 argc) +uint32_t +ecall_handle_cmd_get_pgo_prof_buf_data(uint16_t wasm_module_inst_idx, char *buf, + uint32_t len) { - uint64 *args_org = args; - wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++; - char *buf = *(char **)args++; - uint32 len = *(uint32 *)args++; + wasm_module_inst_t module_inst = nullptr; uint32 bytes_dumped; - bh_assert(argc == 3); + if ((buf == nullptr) + or !(module_inst = (wasm_module_inst_t)gWasmModuleInstMgr.get( + wasm_module_inst_idx))) { + return 0; + } bytes_dumped = wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len); - args_org[0] = bytes_dumped; + return bytes_dumped; } -#endif - -void -ecall_handle_command(unsigned cmd, unsigned char *cmd_buf, - unsigned cmd_buf_size) +#else +uint32_t +ecall_handle_cmd_get_pgo_prof_buf_size(uint16_t wasm_module_inst_idx) { - uint64 *args = (uint64 *)cmd_buf; - uint32 argc = cmd_buf_size / sizeof(uint64); + return 0; +} - switch (cmd) { - case CMD_INIT_RUNTIME: - handle_cmd_init_runtime(args, argc); - break; - case CMD_LOAD_MODULE: - handle_cmd_load_module(args, argc); - break; - case CMD_SET_WASI_ARGS: - handle_cmd_set_wasi_args(args, argc); - break; - case CMD_INSTANTIATE_MODULE: - handle_cmd_instantiate_module(args, argc); - break; - case CMD_LOOKUP_FUNCTION: - break; - case CMD_CREATE_EXEC_ENV: - break; - case CMD_CALL_WASM: - break; - case CMD_EXEC_APP_FUNC: - handle_cmd_exec_app_func(args, argc); - break; - case CMD_EXEC_APP_MAIN: - handle_cmd_exec_app_main(args, argc); - break; - case CMD_GET_EXCEPTION: - handle_cmd_get_exception(args, argc); - break; - case CMD_DEINSTANTIATE_MODULE: - handle_cmd_deinstantiate_module(args, argc); - break; - case CMD_UNLOAD_MODULE: - handle_cmd_unload_module(args, argc); - break; - case CMD_DESTROY_RUNTIME: - handle_cmd_destroy_runtime(); - break; - case CMD_SET_LOG_LEVEL: - handle_cmd_set_log_level(args, argc); - break; - case CMD_GET_VERSION: - handle_cmd_get_version(args, argc); - break; -#if WASM_ENABLE_STATIC_PGO != 0 - case CMD_GET_PGO_PROF_BUF_SIZE: - handle_cmd_get_pgo_prof_buf_size(args, argc); - break; - case CMD_DUMP_PGO_PROF_BUF_DATA: - handle_cmd_get_pro_prof_buf_data(args, argc); - break; -#endif - default: - LOG_ERROR("Unknown command %d\n", cmd); - break; - } +uint32_t +ecall_handle_cmd_get_pgo_prof_buf_data(uint16_t wasm_module_inst_idx, char *buf, + uint32_t len) +{ + return 0; } +#endif void ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) { + if (wasm_file_buf == nullptr) { + return; + } + wasm_module_t wasm_module = NULL; wasm_module_inst_t wasm_module_inst = NULL; RuntimeInitArgs init_args; diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl index 0de4c6404a..0dcb6c5545 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl @@ -20,11 +20,71 @@ enclave { trusted { /* define ECALLs here. */ - public void ecall_handle_command(unsigned cmd, - [in, out, size=cmd_buf_size]uint8_t *cmd_buf, - unsigned cmd_buf_size); - public void ecall_iwasm_main([user_check]uint8_t *wasm_file_buf, - uint32_t wasm_file_size); + public bool ecall_handle_cmd_init_runtime(uint32_t max_thread_num); + public void ecall_handle_cmd_destroy_runtime(); + public bool ecall_handle_cmd_load_module( + [in, size=wasm_file_size] char *wasm_file, + uint32_t wasm_file_size, + [out, size=error_buf_size] char *error_buf, + uint32_t error_buf_size, + [out] uint16_t *enclave_module_idx + ); + public bool ecall_handle_cmd_unload_module(uint16_t enclave_module_idx); + public bool ecall_handle_cmd_instantiate_module( + uint16_t enclave_module_idx, + uint32_t stack_size, + uint32_t heap_size, + [out, size=error_buf_size] char *error_buf, + uint32_t error_buf_size, + [out] uint16_t *module_inst_idx + ); + public bool ecall_handle_cmd_deinstantiate_module(uint16_t wasm_module_inst_idx); + public bool ecall_handle_cmd_get_exception( + uint16_t wasm_module_inst_idx, + [out, size=exception_size] char *exception, + uint32_t exception_size + ); + public bool ecall_handle_cmd_exec_app_main( + uint16_t wasm_module_inst_idx, + [in, count=app_argc] char **u_app_argv, + uint32_t app_argc + ); + public bool ecall_handle_cmd_exec_app_func( + uint16_t wasm_module_inst_idx, + [in, string] const char *func_name, + [in, count=app_argc] char **u_app_argv, + uint32_t app_argc + ); + public void ecall_handle_cmd_set_log_level(int log_level); + public bool ecall_handle_cmd_set_wasi_args( + uint16_t enclave_module_idx, + [in, count=dir_list_size] const char **dir_list, + uint32_t dir_list_size, + [in, count=env_list_size] const char **env_list, + uint32_t env_list_size, + int stdinfd, + int stdoutfd, + int stderrfd, + [in, count=wasi_argc] char **wasi_argv, + uint32_t wasi_argc, + [in, count=addr_pool_list_size] const char **addr_pool_list, + uint32_t addr_pool_list_size + ); + public void ecall_handle_cmd_get_version( + [out] uint32_t *major, + [out] uint32_t *minor, + [out] uint32_t *patch + ); + public uint32_t ecall_handle_cmd_get_pgo_prof_buf_size(uint16_t wasm_module_inst_idx); + public uint32_t ecall_handle_cmd_get_pgo_prof_buf_data( + uint16_t wasm_module_inst_idx, + [out, size=len] char *buf, + uint32_t len + ); + public void ecall_iwasm_main( + [in, size=wasm_file_size] uint8_t *wasm_file_buf, + uint32_t wasm_file_size + ); }; untrusted { diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Makefile b/product-mini/platforms/linux-sgx/enclave-sample/Makefile index 4025456216..95ffdafe35 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Makefile +++ b/product-mini/platforms/linux-sgx/enclave-sample/Makefile @@ -129,6 +129,7 @@ Enclave_Include_Paths := -IEnclave -I$(WAMR_ROOT)/core/iwasm/include \ -I$(WAMR_ROOT)/core/shared/platform/linux-sgx \ -I$(SGX_SDK)/include \ -I$(SGX_SDK)/include/tlibc \ + -I$(SGX_SDK)/include/libcxx \ -I$(SGX_SDK)/include/stlport ifeq ($(WAMR_BUILD_LIB_RATS), 1) @@ -208,6 +209,7 @@ ifeq ($(WAMR_BUILD_LIB_RATS), 1) @echo "librats build success" endif +App/Enclave_u.h: App/Enclave_u.c App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path) @echo "GEN => $@" @@ -216,6 +218,7 @@ App/Enclave_u.o: App/Enclave_u.c @$(CC) $(App_C_Flags) -c $< -o $@ @echo "CC <= $<" +App/%.o: App/Enclave_u.h App/%.o: App/%.cpp @$(CXX) $(App_Cpp_Flags) -c $< -o $@ @echo "CXX <= $<" @@ -230,6 +233,7 @@ $(App_Name): App/Enclave_u.o $(App_Cpp_Objects) libvmlib_untrusted.a ######## Enclave Objects ######## +Enclave/Enclave_t.h: Enclave/Enclave_t.c Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats @cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path) @echo "GEN => $@" @@ -238,6 +242,7 @@ Enclave/Enclave_t.o: Enclave/Enclave_t.c @$(CC) $(Enclave_C_Flags) -c $< -o $@ @echo "CC <= $<" +Enclave/%.o: Enclave/Enclave_t.h Enclave/%.o: Enclave/%.cpp @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@ @echo "CXX <= $<"