From c4defb887759a849e2176dd63900c147cb450352 Mon Sep 17 00:00:00 2001 From: dongsheng28849455 <68947925+dongsheng28849455@users.noreply.github.com> Date: Thu, 24 Nov 2022 12:48:31 +0800 Subject: [PATCH 1/8] Update xtensa LLVM version to 15.x (#1741) Fix the issue that xtensa .aot size cannot be over 256K. --- build-scripts/build_llvm.py | 2 +- core/iwasm/aot/arch/aot_reloc_xtensa.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build-scripts/build_llvm.py b/build-scripts/build_llvm.py index 4e37e390b4..bc0daf1f7d 100755 --- a/build-scripts/build_llvm.py +++ b/build-scripts/build_llvm.py @@ -208,7 +208,7 @@ def main(): }, "xtensa": { "repo": "https://github.com/espressif/llvm-project.git", - "branch": "xtensa_release_13.0.0", + "branch": "xtensa_release_15.x", }, "default": { "repo": "https://github.com/llvm/llvm-project.git", diff --git a/core/iwasm/aot/arch/aot_reloc_xtensa.c b/core/iwasm/aot/arch/aot_reloc_xtensa.c index 91766831d5..731ae6395c 100644 --- a/core/iwasm/aot/arch/aot_reloc_xtensa.c +++ b/core/iwasm/aot/arch/aot_reloc_xtensa.c @@ -259,7 +259,9 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, if (relative_offset < -256 * BH_KB || relative_offset > -4) { set_error_buf(error_buf, error_buf_size, "AOT module load failed: " - "target address out of range."); + "target address out of range.\n" + "Try using `wamrc --size-level=0` to generate " + ".literal island."); return false; } From 8dc9d6dc4f8f5592190f0fc6f441ad39d2aca2b1 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 24 Nov 2022 15:50:05 +0800 Subject: [PATCH 2/8] Enlarge the default wasm operand stack size to 64KB (#1746) Enlarge the default wasm operand stack size to 64KB since the original default size 16KB is a little small, and the operand stack overflow exception is often thrown when running wasm apps. --- product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp | 6 +++--- product-mini/platforms/posix/main.c | 4 ++-- product-mini/platforms/windows/main.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) 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 779dd24802..95a6869c58 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp @@ -217,7 +217,7 @@ print_help() " than main\n"); printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n" " level with more log\n"); - printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n"); + printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n"); printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n"); printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n" " that runs commands in the form of `FUNC ARG...`\n"); @@ -606,7 +606,7 @@ main(int argc, char *argv[]) const char *func_name = NULL; uint8_t *wasm_file_buf = NULL; uint32_t wasm_file_size; - uint32_t stack_size = 16 * 1024, heap_size = 16 * 1024; + uint32_t stack_size = 64 * 1024, heap_size = 16 * 1024; void *wasm_module = NULL; void *wasm_module_inst = NULL; char error_buf[128] = { 0 }; @@ -825,7 +825,7 @@ wamr_pal_init(const struct wamr_pal_attr *args) int wamr_pal_create_process(struct wamr_pal_create_process_args *args) { - uint32_t stack_size = 16 * 1024, heap_size = 16 * 1024; + uint32_t stack_size = 64 * 1024, heap_size = 16 * 1024; int log_verbose_level = 2; bool is_repl_mode = false; const char *dir_list[8] = { NULL }; diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 72f1e29bc0..e58bbf3d83 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -32,7 +32,7 @@ print_help() printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n" " level with more log\n"); #endif - printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n"); + printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n"); printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n"); #if WASM_ENABLE_FAST_JIT != 0 printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n"); @@ -341,7 +341,7 @@ main(int argc, char *argv[]) const char *func_name = NULL; uint8 *wasm_file_buf = NULL; uint32 wasm_file_size; - uint32 stack_size = 16 * 1024, heap_size = 16 * 1024; + uint32 stack_size = 64 * 1024, heap_size = 16 * 1024; #if WASM_ENABLE_FAST_JIT != 0 uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE; #endif diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 30d59f2f73..1042763d97 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -27,7 +27,7 @@ print_help() printf(" -v=n Set log verbose level (0 to 5, default is 2) larger\n" " level with more log\n"); #endif - printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n"); + printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n"); printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n"); printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n" " that runs commands in the form of `FUNC ARG...`\n"); @@ -227,7 +227,7 @@ main(int argc, char *argv[]) const char *func_name = NULL; uint8 *wasm_file_buf = NULL; uint32 wasm_file_size; - uint32 stack_size = 16 * 1024, heap_size = 16 * 1024; + uint32 stack_size = 64 * 1024, heap_size = 16 * 1024; wasm_module_t wasm_module = NULL; wasm_module_inst_t wasm_module_inst = NULL; RuntimeInitArgs init_args; From 1032aac60b351cc5e99a92ffda8159a5b43c5bf9 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 24 Nov 2022 21:26:18 +0900 Subject: [PATCH 3/8] Add wasm_runtime_get_wasi_exit_code (#1748) Refer to https://github.com/bytecodealliance/wasm-micro-runtime/issues/1738 --- core/iwasm/common/wasm_runtime_common.c | 20 ++++++++---- core/iwasm/common/wasm_runtime_common.h | 10 +++++- core/iwasm/include/wasm_export.h | 12 +++++++ .../libc-uvwasi/libc_uvwasi_wrapper.c | 20 ++++++++++-- .../libraries/libc-wasi/libc_wasi_wrapper.c | 3 ++ product-mini/platforms/posix/main.c | 31 ++++++++++++++----- product-mini/platforms/windows/main.c | 31 ++++++++++++++----- 7 files changed, 101 insertions(+), 26 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 0cbaaf5c43..a6e552e9e0 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2675,17 +2675,18 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, char *argv[], uint32 argc, int stdinfd, int stdoutfd, int stderrfd, char *error_buf, uint32 error_buf_size) { - uvwasi_t *uvwasi = NULL; + WASIContext *ctx; + uvwasi_t *uvwasi; uvwasi_options_t init_options; const char **envp = NULL; uint64 total_size; uint32 i; bool ret = false; - uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf, - error_buf_size); - if (!uvwasi) + ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size); + if (!ctx) return false; + uvwasi = &ctx->uvwasi; /* Setup the initialization options */ uvwasi_options_init(&init_options); @@ -2733,7 +2734,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, goto fail; } - wasm_runtime_set_wasi_ctx(module_inst, uvwasi); + wasm_runtime_set_wasi_ctx(module_inst, ctx); ret = true; @@ -2863,12 +2864,19 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst) WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst); if (wasi_ctx) { - uvwasi_destroy(wasi_ctx); + uvwasi_destroy(&wasi_ctx->uvwasi); wasm_runtime_free(wasi_ctx); } } #endif +uint32_t +wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst) +{ + WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst); + return wasi_ctx->exit_code; +} + WASIContext * wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm) { diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 418d4352f9..bcfa05cb70 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -369,9 +369,13 @@ typedef struct WASIContext { char **argv_list; char *env_buf; char **env_list; + uint32_t exit_code; } WASIContext; #else -typedef uvwasi_t WASIContext; +typedef struct WASIContext { + uvwasi_t uvwasi; + uint32_t exit_code; +} WASIContext; #endif #endif @@ -768,6 +772,10 @@ wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst); WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst); +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst); + bool wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, const char *dir_list[], uint32 dir_count, diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index a8aa161120..182c5753e5 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -450,6 +450,18 @@ wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst); WASM_RUNTIME_API_EXTERN wasm_function_inst_t wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst); +/** + * Get WASI exit code. + * + * After a WASI command completed its execution, an embedder can + * call this function to get its exit code. (that is, the value given + * to proc_exit.) + * + * @param module_inst the module instance + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst); + /** * Lookup an exported function in the WASM module instance. * diff --git a/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c b/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c index 187693ea96..504ff7f93e 100644 --- a/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c +++ b/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c @@ -11,9 +11,6 @@ #define get_module_inst(exec_env) \ wasm_runtime_get_module_inst(exec_env) -#define get_wasi_ctx(module_inst) \ - wasm_runtime_get_wasi_ctx(module_inst) - #define validate_app_addr(offset, size) \ wasm_runtime_validate_app_addr(module_inst, offset, size) @@ -72,9 +69,24 @@ typedef struct iovec_app { uint32 buf_len; } iovec_app_t; +typedef struct WASIContext { + uvwasi_t uvwasi; + uint32_t exit_code; +} WASIContext; + void * wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst); +static uvwasi_t * +get_wasi_ctx(wasm_module_inst_t module_inst) +{ + WASIContext *ctx = wasm_runtime_get_wasi_ctx(module_inst); + if (ctx == NULL) { + return NULL; + } + return &ctx->uvwasi; +} + static wasi_errno_t wasi_args_get(wasm_exec_env_t exec_env, uint32 *argv_offsets, char *argv_buf) { @@ -924,10 +936,12 @@ static void wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval) { wasm_module_inst_t module_inst = get_module_inst(exec_env); + WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst); /* Here throwing exception is just to let wasm app exit, the upper layer should clear the exception and return as normal */ wasm_runtime_set_exception(module_inst, "wasi proc exit"); + wasi_ctx->exit_code = rval; } static wasi_errno_t diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index d9eea05518..a6a448b509 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -57,6 +57,7 @@ typedef struct WASIContext { char **argv_list; char *env_buf; char **env_list; + uint32_t exit_code; } * wasi_ctx_t; wasi_ctx_t @@ -980,10 +981,12 @@ static void wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval) { wasm_module_inst_t module_inst = get_module_inst(exec_env); + wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); /* Here throwing exception is just to let wasm app exit, the upper layer should clear the exception and return as normal */ wasm_runtime_set_exception(module_inst, "wasi proc exit"); + wasi_ctx->exit_code = rval; } static wasi_errno_t diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index e58bbf3d83..147e73866e 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -86,7 +86,7 @@ app_instance_main(wasm_module_inst_t module_inst) wasm_application_execute_main(module_inst, app_argc, app_argv); if ((exception = wasm_runtime_get_exception(module_inst))) printf("%s\n", exception); - return NULL; + return exception; } static void * @@ -96,7 +96,7 @@ app_instance_func(wasm_module_inst_t module_inst, const char *func_name) app_argv + 1); /* The result of wasm function or exception info was output inside wasm_application_execute_func(), here we don't output them again. */ - return NULL; + return wasm_runtime_get_exception(module_inst); } /** @@ -643,14 +643,29 @@ main(int argc, char *argv[]) } #endif - if (is_repl_mode) + ret = 0; + if (is_repl_mode) { app_instance_repl(wasm_module_inst); - else if (func_name) - app_instance_func(wasm_module_inst, func_name); - else - app_instance_main(wasm_module_inst); + } + else if (func_name) { + if (app_instance_func(wasm_module_inst, func_name)) { + /* got an exception */ + ret = 1; + } + } + else { + if (app_instance_main(wasm_module_inst)) { + /* got an exception */ + ret = 1; + } + } - ret = 0; +#if WASM_ENABLE_LIBC_WASI != 0 + if (ret == 0) { + /* propagate wasi exit code. */ + ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst); + } +#endif #if WASM_ENABLE_DEBUG_INTERP != 0 fail4: diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 1042763d97..8b1d6057e2 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -63,7 +63,7 @@ app_instance_main(wasm_module_inst_t module_inst) wasm_application_execute_main(module_inst, app_argc, app_argv); if ((exception = wasm_runtime_get_exception(module_inst))) printf("%s\n", exception); - return NULL; + return exception; } static void * @@ -73,7 +73,7 @@ app_instance_func(wasm_module_inst_t module_inst, const char *func_name) app_argv + 1); /* The result of wasm function or exception info was output inside wasm_application_execute_func(), here we don't output them again. */ - return NULL; + return wasm_runtime_get_exception(module_inst); } /** @@ -451,14 +451,29 @@ main(int argc, char *argv[]) } #endif - if (is_repl_mode) + ret = 0; + if (is_repl_mode) { app_instance_repl(wasm_module_inst); - else if (func_name) - app_instance_func(wasm_module_inst, func_name); - else - app_instance_main(wasm_module_inst); + } + else if (func_name) { + if (app_instance_func(wasm_module_inst, func_name)) { + /* got an exception */ + ret = 1; + } + } + else { + if (app_instance_main(wasm_module_inst)) { + /* got an exception */ + ret = 1; + } + } - ret = 0; +#if WASM_ENABLE_LIBC_WASI != 0 + if (ret == 0) { + /* propagate wasi exit code. */ + ret = wasm_runtime_get_wasi_exit_code(wasm_module_inst); + } +#endif #if WASM_ENABLE_DEBUG_INTERP != 0 fail4: From 29b76dd27542d88a369a060bc631c38e3db89949 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 24 Nov 2022 21:48:50 +0800 Subject: [PATCH 4/8] Create module hash for each module in SGX lib-rats (#1745) Current SGX lib-rats wasm module hash is stored in a global buffer, which may be overwritten if there are multiple wasm module loadings. We move the module hash into the enclave module to resolve the issue. And rename the SGX_IPFS macro/variable in Makefile and Enclave.edl to make the code more consistent. And refine the sgx-ra sample document. --- core/iwasm/common/wasm_runtime_common.c | 6 ++ core/iwasm/common/wasm_runtime_common.h | 4 ++ core/iwasm/include/wasm_export.h | 21 ++++++ .../libraries/lib-rats/lib_rats_common.h | 11 +++- .../libraries/lib-rats/lib_rats_wrapper.c | 6 +- .../libraries/lib-rats/lib_rats_wrapper.h | 3 +- .../platforms/linux-sgx/CMakeLists.txt | 8 +-- .../linux-sgx/enclave-sample/App/App.cpp | 2 +- .../enclave-sample/Enclave/Enclave.cpp | 65 ++++++++++++++++++- .../enclave-sample/Enclave/Enclave.edl | 4 +- .../linux-sgx/enclave-sample/Makefile | 4 +- samples/sgx-ra/README.md | 23 ++++--- 12 files changed, 131 insertions(+), 26 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index a6e552e9e0..fbcdfcd009 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1160,6 +1160,12 @@ wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst) wasm_runtime_deinstantiate_internal(module_inst, false); } +WASMModuleCommon * +wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst) +{ + return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module; +} + WASMExecEnv * wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst, uint32 stack_size) diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index bcfa05cb70..cc311ba730 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -488,6 +488,10 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, WASM_RUNTIME_API_EXTERN void wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst); +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN WASMModuleCommon * +wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst); + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst, diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 182c5753e5..cc173af92d 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -357,6 +357,17 @@ wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot, WASM_RUNTIME_API_EXTERN void wasm_runtime_unload(wasm_module_t module); +/** + * Get the module hash of a WASM module, currently only available on + * linux-sgx platform when the remote attestation feature is enabled + * + * @param module the WASM module to retrieve + * + * @return the module hash of the WASM module + */ +char * +wasm_runtime_get_module_hash(wasm_module_t module); + /** * Set WASI parameters. * @@ -444,6 +455,16 @@ wasm_runtime_instantiate(const wasm_module_t module, WASM_RUNTIME_API_EXTERN void wasm_runtime_deinstantiate(wasm_module_inst_t module_inst); +/** + * Get WASM module from WASM module instance + * + * @param module_inst the WASM module instance to retrieve + * + * @return the WASM module + */ +WASM_RUNTIME_API_EXTERN wasm_module_t +wasm_runtime_get_module(wasm_module_inst_t module_inst); + WASM_RUNTIME_API_EXTERN bool wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst); diff --git a/core/iwasm/libraries/lib-rats/lib_rats_common.h b/core/iwasm/libraries/lib-rats/lib_rats_common.h index 223f2fc3fc..929e105f05 100644 --- a/core/iwasm/libraries/lib-rats/lib_rats_common.h +++ b/core/iwasm/libraries/lib-rats/lib_rats_common.h @@ -11,9 +11,14 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define SGX_QUOTE_MAX_SIZE 8192 #define SGX_USER_DATA_SIZE 64 #define SGX_MEASUREMENT_SIZE 32 + /* clang-format off */ typedef struct rats_sgx_evidence { uint8_t quote[SGX_QUOTE_MAX_SIZE]; /* The quote of the Enclave */ @@ -28,4 +33,8 @@ typedef struct rats_sgx_evidence { } rats_sgx_evidence_t; /* clang-format on */ -#endif \ No newline at end of file +#ifdef __cplusplus +} +#endif + +#endif diff --git a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c index 81250d95ff..59d61f4c82 100644 --- a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c +++ b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c @@ -16,13 +16,13 @@ #include "bh_common.h" #include "lib_rats_common.h" -extern char wasm_module_hash[SHA256_DIGEST_LENGTH]; - static int librats_collect_wrapper(wasm_exec_env_t exec_env, char **evidence_json, const char *buffer, uint32_t buffer_size) { wasm_module_inst_t module_inst = get_module_inst(exec_env); + wasm_module_t module = wasm_runtime_get_module(module_inst); + char *wasm_module_hash = wasm_runtime_get_module_hash(module); char *json, *str_ret; uint32_t str_ret_offset; @@ -112,4 +112,4 @@ get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis) { *p_lib_rats_apis = native_symbols_lib_rats; return sizeof(native_symbols_lib_rats) / sizeof(NativeSymbol); -} \ No newline at end of file +} diff --git a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h index 294dca7749..e334983e9a 100644 --- a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h +++ b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h @@ -10,6 +10,7 @@ #include #include + #include "lib_rats_common.h" #ifdef __cplusplus @@ -44,4 +45,4 @@ librats_parse_evidence(const char *evidence_json, uint32_t json_size, } #endif -#endif \ No newline at end of file +#endif diff --git a/product-mini/platforms/linux-sgx/CMakeLists.txt b/product-mini/platforms/linux-sgx/CMakeLists.txt index cfd3f4b63c..306f634e77 100644 --- a/product-mini/platforms/linux-sgx/CMakeLists.txt +++ b/product-mini/platforms/linux-sgx/CMakeLists.txt @@ -140,14 +140,14 @@ endif() if (WAMR_BUILD_SGX_IPFS EQUAL 1) execute_process( - COMMAND bash -c "sed -i -E 's/^#define SGX_IPFS 0/#define SGX_IPFS 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" - COMMAND bash -c "sed -i -E 's/^SGX_IPFS = 0/SGX_IPFS = 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" + COMMAND bash -c "sed -i -E 's/^#define WASM_ENABLE_SGX_IPFS 0/#define WASM_ENABLE_SGX_IPFS 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" + COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_SGX_IPFS = 0/WAMR_BUILD_SGX_IPFS = 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" OUTPUT_VARIABLE cmdOutput ) else() execute_process( - COMMAND bash -c "sed -i -E 's/^#define SGX_IPFS 1/#define SGX_IPFS 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" - COMMAND bash -c "sed -i -E 's/^SGX_IPFS = 1/SGX_IPFS = 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" + COMMAND bash -c "sed -i -E 's/^#define WASM_ENABLE_SGX_IPFS 1/#define WASM_ENABLE_SGX_IPFS 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" + COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_SGX_IPFS = 1/WAMR_BUILD_SGX_IPFS = 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" OUTPUT_VARIABLE cmdOutput ) endif() 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 95a6869c58..0a6f24a9da 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp @@ -103,7 +103,7 @@ enclave_init(sgx_enclave_id_t *p_eid) <= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) { /* compose the token path */ strncpy(token_path, home_dir, MAX_PATH); - strncat(token_path, "/", strlen("/")); + strncat(token_path, "/", strlen("/") + 1); strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1); } else { 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 51d0341785..7cb9c3f472 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -14,8 +14,6 @@ #if WASM_ENABLE_LIB_RATS != 0 #include - -char wasm_module_hash[SHA256_DIGEST_LENGTH]; #endif extern "C" { @@ -68,8 +66,17 @@ typedef struct EnclaveModule { uint32 wasi_argc; bool is_xip_file; uint32 total_size_mapped; +#if WASM_ENABLE_LIB_RATS != 0 + char module_hash[SHA256_DIGEST_LENGTH]; + struct EnclaveModule *next; +#endif } EnclaveModule; +#if WASM_ENABLE_LIB_RATS != 0 +static EnclaveModule *enclave_module_list = NULL; +static korp_mutex enclave_module_list_lock = OS_THREAD_MUTEX_INITIALIZER; +#endif + #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0 static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 }; #endif @@ -250,10 +257,17 @@ handle_cmd_load_module(uint64 *args, uint32 argc) *(EnclaveModule **)args_org = enclave_module; #if WASM_ENABLE_LIB_RATS != 0 + /* Calculate the module hash */ SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, wasm_file, wasm_file_size); - SHA256_Final((unsigned char *)wasm_module_hash, &sha256); + SHA256_Final((unsigned char *)enclave_module->module_hash, &sha256); + + /* Insert enclave module to enclave module list */ + os_mutex_lock(&enclave_module_list_lock); + enclave_module->next = enclave_module_list; + enclave_module_list = enclave_module; + os_mutex_unlock(&enclave_module_list_lock); #endif LOG_VERBOSE("Load module success.\n"); @@ -267,6 +281,28 @@ handle_cmd_unload_module(uint64 *args, uint32 argc) bh_assert(argc == 1); +#if WASM_ENABLE_LIB_RATS != 0 + /* Remove enclave module from enclave module list */ + os_mutex_lock(&enclave_module_list_lock); + + EnclaveModule *node_prev = NULL; + EnclaveModule *node = enclave_module_list; + + while (node && node != enclave_module) { + node_prev = node; + node = node->next; + } + bh_assert(node == enclave_module); + + if (!node_prev) + enclave_module_list = node->next; + else + node_prev->next = node->next; + + os_mutex_unlock(&enclave_module_list_lock); +#endif + + /* Destroy enclave module resources */ if (enclave_module->wasi_arg_buf) wasm_runtime_free(enclave_module->wasi_arg_buf); @@ -279,6 +315,29 @@ handle_cmd_unload_module(uint64 *args, uint32 argc) LOG_VERBOSE("Unload module success.\n"); } +#if WASM_ENABLE_LIB_RATS != 0 +char * +wasm_runtime_get_module_hash(wasm_module_t module) +{ + EnclaveModule *enclave_module; + char *module_hash = NULL; + + os_mutex_lock(&enclave_module_list_lock); + + enclave_module = enclave_module_list; + while (enclave_module) { + if (enclave_module->module == module) { + module_hash = enclave_module->module_hash; + break; + } + enclave_module = enclave_module->next; + } + os_mutex_unlock(&enclave_module_list_lock); + + return module_hash; +} +#endif + static void handle_cmd_instantiate_module(uint64 *args, uint32 argc) { 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 a0de16969f..0de4c6404a 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl @@ -3,8 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#define WASM_ENABLE_SGX_IPFS 0 #define WASM_ENABLE_LIB_RATS 0 -#define SGX_IPFS 0 enclave { from "sgx_tstdc.edl" import *; @@ -14,7 +14,7 @@ enclave { from "rats.edl" import *; from "sgx_tsgxssl.edl" import *; #endif -#if SGX_IPFS != 0 +#if WASM_ENABLE_SGX_IPFS != 0 from "sgx_tprotected_fs.edl" import *; #endif diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Makefile b/product-mini/platforms/linux-sgx/enclave-sample/Makefile index 87bf1f8013..b598aad549 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Makefile +++ b/product-mini/platforms/linux-sgx/enclave-sample/Makefile @@ -11,7 +11,7 @@ SGX_DEBUG ?= 0 SPEC_TEST ?= 0 # These variables are automatically set by CMakeLists.txt -SGX_IPFS = 0 +WAMR_BUILD_SGX_IPFS = 0 WAMR_BUILD_LIB_RATS = 0 WAMR_BUILD_GLOBAL_HEAP_POOL = 0 WAMR_BUILD_GLOBAL_HEAP_SIZE = 10485760 @@ -112,7 +112,7 @@ else Service_Library_Name := sgx_tservice endif -ifeq ($(SGX_IPFS), 1) +ifeq ($(WAMR_BUILD_SGX_IPFS), 1) Intel_Ipfs_Trusted_Flag = -lsgx_tprotected_fs App_Link_Flags += -lsgx_uprotected_fs endif diff --git a/samples/sgx-ra/README.md b/samples/sgx-ra/README.md index a374d11379..39a2f2d9ca 100644 --- a/samples/sgx-ra/README.md +++ b/samples/sgx-ra/README.md @@ -15,33 +15,38 @@ Before starting, we need to download and install [SGX SDK](https://download.01.o The following commands are an example of the SGX environment installation on Ubuntu 18.04. ``` shell +# Set your platform, you can get the platforms list on +# https://download.01.org/intel-sgx/latest/linux-latest/distro $ cd $HOME -$ # Set your platform, you can get the platforms list on -$ # https://download.01.org/intel-sgx/latest/linux-latest/distro $ SGX_PLATFORM=ubuntu18.04-server $ SGX_SDK_VERSION=2.17.100.3 $ SGX_DRIVER_VERSION=1.41 -$ # install the dependencies + +# install the dependencies $ sudo apt-get update $ sudo apt-get install -y dkms -$ # install SGX Driver + +# install SGX Driver $ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PLATFORM/sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin $ chmod +x sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin $ sudo ./sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin -$ # install SGX SDK + +# install SGX SDK $ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PLATFORM/sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin $ chmod +x sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin $ sudo ./sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin -$ # install SGX DCAP Library + +# install SGX DCAP Library $ echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu bionic main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list > /dev/null $ wget -O - https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add - $ sudo apt-get update $ sudo apt-get install -y libsgx-uae-service libsgx-dcap-default-qpl-dev libsgx-dcap-ql-dev libsgx-dcap-quote-verify-dev -$ # install SGX SSL Library + +# install SGX SSL Library $ git clone https://github.com/intel/linux-sgx.git $ cd linux-sgx && make preparation $ sudo cp external/toolset/{current_distr}/* /usr/local/bin -$ # Verify that the paths are correctly set +$ # Verify that the paths are correctly set $ which ar as ld objcopy objdump ranlib $ cd ../ $ git clone https://github.com/intel/intel-sgx-ssl.git @@ -195,4 +200,4 @@ The sample will print the evidence in JSON and the message: *Evidence is trusted - [Intel SGX Software Installation Guide For Linux OS](https://download.01.org/intel-sgx/latest/dcap-latest/linux/docs/Intel_SGX_SW_Installation_Guide_for_Linux.pdf) - [Intel Software Guard Extensions (IntelĀ® SGX) Data Center Attestation Primitives: Library API ](https://download.01.org/intel-sgx/latest/dcap-latest/linux/docs/Intel_SGX_ECDSA_QuoteLibReference_DCAP_API.pdf) -- [Remote Attestation for Multi-Package Platforms using Intel SGX Datacenter Attestation Primitives (DCAP)](https://download.01.org/intel-sgx/latest/dcap-latest/linux/docs/Intel_SGX_DCAP_Multipackage_SW.pdf) \ No newline at end of file +- [Remote Attestation for Multi-Package Platforms using Intel SGX Datacenter Attestation Primitives (DCAP)](https://download.01.org/intel-sgx/latest/dcap-latest/linux/docs/Intel_SGX_DCAP_Multipackage_SW.pdf) From eaedceca2f4886df6ac55169907f2c3c98b060df Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Fri, 25 Nov 2022 08:33:44 +0800 Subject: [PATCH 5/8] Add bh_print_proc_mem() to dump memory info of current process (#1734) Only support Posix platforms currently, read memory consumption info from file "/proc/self/status". --- core/iwasm/common/wasm_c_api.c | 37 ++++++++++---- core/shared/platform/alios/alios_platform.c | 6 +++ .../common/freertos/freertos_malloc.c | 6 +++ .../platform/common/posix/posix_malloc.c | 48 +++++++++++++++++++ core/shared/platform/esp-idf/espidf_malloc.c | 6 +++ .../platform/include/platform_api_extension.h | 13 +++++ core/shared/platform/linux-sgx/sgx_platform.c | 6 +++ core/shared/platform/nuttx/nuttx_platform.c | 6 +++ core/shared/platform/riot/riot_platform.c | 6 +++ core/shared/platform/rt-thread/rtt_platform.c | 6 +++ core/shared/platform/windows/win_malloc.c | 6 +++ core/shared/platform/zephyr/zephyr_platform.c | 6 +++ core/shared/utils/bh_log.c | 26 ++++++++++ core/shared/utils/bh_log.h | 8 ++++ 14 files changed, 178 insertions(+), 8 deletions(-) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 8f28ec4cc6..5d286434e8 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -258,6 +258,12 @@ WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal) WASM_DEFINE_VEC_OWN(store, wasm_store_delete) WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete) +#ifndef NDEBUG +#define WASM_C_DUMP_PROC_MEM() LOG_PROC_MEM() +#else +#define WASM_C_DUMP_PROC_MEM() (void)0 +#endif + /* Runtime Environment */ own wasm_config_t * wasm_config_new(void) @@ -307,6 +313,14 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts) RuntimeInitArgs init_args = { 0 }; init_args.mem_alloc_type = type; +#ifndef NDEBUG + bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE); +#else + bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING); +#endif + + WASM_C_DUMP_PROC_MEM(); + if (type == Alloc_With_Pool) { if (!opts) { return NULL; @@ -337,14 +351,6 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts) goto failed; } -#ifndef NDEBUG - /*DEBUG*/ - bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE); -#else - /*VERBOSE*/ - bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING); -#endif - /* create wasm_engine_t */ if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) { goto failed; @@ -358,6 +364,8 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts) engine->ref_count = 1; + WASM_C_DUMP_PROC_MEM(); + RETURN_OBJ(engine, wasm_engine_delete_internal) } @@ -442,6 +450,8 @@ wasm_store_new(wasm_engine_t *engine) { wasm_store_t *store = NULL; + WASM_C_DUMP_PROC_MEM(); + if (!engine || singleton_engine != engine) { return NULL; } @@ -474,6 +484,8 @@ wasm_store_new(wasm_engine_t *engine) goto failed; } + WASM_C_DUMP_PROC_MEM(); + return store; failed: wasm_store_delete(store); @@ -1903,6 +1915,8 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) bh_assert(singleton_engine); + WASM_C_DUMP_PROC_MEM(); + if (!store || !binary || binary->size == 0 || binary->size > UINT32_MAX) goto quit; @@ -1958,6 +1972,9 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) goto destroy_lock; module_ex->ref_count = 1; + + WASM_C_DUMP_PROC_MEM(); + return module_ext_to_module(module_ex); destroy_lock: @@ -4453,6 +4470,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, return NULL; } + WASM_C_DUMP_PROC_MEM(); + instance = malloc_internal(sizeof(wasm_instance_t)); if (!instance) { goto failed; @@ -4595,6 +4614,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, goto failed; } + WASM_C_DUMP_PROC_MEM(); + return instance; failed: diff --git a/core/shared/platform/alios/alios_platform.c b/core/shared/platform/alios/alios_platform.c index 6418587373..c9f5f17e64 100644 --- a/core/shared/platform/alios/alios_platform.c +++ b/core/shared/platform/alios/alios_platform.c @@ -40,6 +40,12 @@ void os_free(void *ptr) {} +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + void * os_mmap(void *hint, size_t size, int prot, int flags) { diff --git a/core/shared/platform/common/freertos/freertos_malloc.c b/core/shared/platform/common/freertos/freertos_malloc.c index 19cf4d5f1d..e47a8cce16 100644 --- a/core/shared/platform/common/freertos/freertos_malloc.c +++ b/core/shared/platform/common/freertos/freertos_malloc.c @@ -20,3 +20,9 @@ os_realloc(void *ptr, unsigned size) void os_free(void *ptr) {} + +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} diff --git a/core/shared/platform/common/posix/posix_malloc.c b/core/shared/platform/common/posix/posix_malloc.c index 660d1baae6..912998ee07 100644 --- a/core/shared/platform/common/posix/posix_malloc.c +++ b/core/shared/platform/common/posix/posix_malloc.c @@ -22,3 +22,51 @@ os_free(void *ptr) { free(ptr); } + +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + int ret = -1; + FILE *f; + char line[128] = { 0 }; + unsigned int out_idx = 0; + + if (!out || !size) + goto quit; + + f = fopen("/proc/self/status", "r"); + if (!f) { + perror("fopen failed: "); + goto quit; + } + + memset(out, 0, size); + + while (fgets(line, sizeof(line), f)) { +#if WASM_ENABLE_MEMORY_PROFILING != 0 + if (strncmp(line, "Vm", 2) == 0 || strncmp(line, "Rss", 3) == 0) { +#else + if (strncmp(line, "VmRSS", 5) == 0 + || strncmp(line, "RssAnon", 7) == 0) { +#endif + size_t line_len = strlen(line); + if (line_len >= size - 1 - out_idx) + goto close_file; + + /* copying without null-terminated byte */ + memcpy(out + out_idx, line, line_len); + out_idx += line_len; + } + } + + if (ferror(f)) { + perror("fgets failed: "); + goto close_file; + } + + ret = 0; +close_file: + fclose(f); +quit: + return ret; +} \ No newline at end of file diff --git a/core/shared/platform/esp-idf/espidf_malloc.c b/core/shared/platform/esp-idf/espidf_malloc.c index 8fde7c8d34..08ec883058 100644 --- a/core/shared/platform/esp-idf/espidf_malloc.c +++ b/core/shared/platform/esp-idf/espidf_malloc.c @@ -76,3 +76,9 @@ os_free(void *ptr) free(mem_origin); } } + +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index 23f6443732..42baad74f7 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -977,6 +977,19 @@ os_socket_set_broadcast(bh_socket_t socket, bool is_enabled); int os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled); +/** + * Dump memory information of the current process + * It may have variant implementations in different platforms + * + * @param out the output buffer. It is for sure the return content + * is a c-string which ends up with '\0' + * @param size the size of the output buffer + * + * @return 0 if success, -1 otherwise + */ +int +os_dumps_proc_mem_info(char *out, unsigned int size); + #ifdef __cplusplus } #endif diff --git a/core/shared/platform/linux-sgx/sgx_platform.c b/core/shared/platform/linux-sgx/sgx_platform.c index c0b423d522..b40eaf79c4 100644 --- a/core/shared/platform/linux-sgx/sgx_platform.c +++ b/core/shared/platform/linux-sgx/sgx_platform.c @@ -51,6 +51,12 @@ os_free(void *ptr) free(ptr); } +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + int putchar(int c) { diff --git a/core/shared/platform/nuttx/nuttx_platform.c b/core/shared/platform/nuttx/nuttx_platform.c index 8b8d6d85ae..28188420ed 100644 --- a/core/shared/platform/nuttx/nuttx_platform.c +++ b/core/shared/platform/nuttx/nuttx_platform.c @@ -38,6 +38,12 @@ os_free(void *ptr) free(ptr); } +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + void * os_mmap(void *hint, size_t size, int prot, int flags) { diff --git a/core/shared/platform/riot/riot_platform.c b/core/shared/platform/riot/riot_platform.c index 82a8463957..a0c38e8c94 100644 --- a/core/shared/platform/riot/riot_platform.c +++ b/core/shared/platform/riot/riot_platform.c @@ -43,6 +43,12 @@ os_free(void *ptr) free(ptr); } +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + void * os_mmap(void *hint, size_t size, int prot, int flags) { diff --git a/core/shared/platform/rt-thread/rtt_platform.c b/core/shared/platform/rt-thread/rtt_platform.c index 13eb439863..4685e1ea31 100644 --- a/core/shared/platform/rt-thread/rtt_platform.c +++ b/core/shared/platform/rt-thread/rtt_platform.c @@ -88,6 +88,12 @@ os_free(void *ptr) } } +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2]; int diff --git a/core/shared/platform/windows/win_malloc.c b/core/shared/platform/windows/win_malloc.c index 660d1baae6..56aaf9c7b3 100644 --- a/core/shared/platform/windows/win_malloc.c +++ b/core/shared/platform/windows/win_malloc.c @@ -22,3 +22,9 @@ os_free(void *ptr) { free(ptr); } + +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_platform.c b/core/shared/platform/zephyr/zephyr_platform.c index 55ad84f420..b4f2e5ec74 100644 --- a/core/shared/platform/zephyr/zephyr_platform.c +++ b/core/shared/platform/zephyr/zephyr_platform.c @@ -85,6 +85,12 @@ void os_free(void *ptr) {} +int +os_dumps_proc_mem_info(char *out, unsigned int size) +{ + return -1; +} + #if 0 struct out_context { int count; diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index 3e5e95ea96..5e3a5b0f44 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -79,3 +79,29 @@ bh_print_time(const char *prompt) last_time_ms = curr_time_ms; } + +void +bh_print_proc_mem(const char *prompt) +{ + char buf[1024] = { 0 }; + + if (log_verbose_level < BH_LOG_LEVEL_DEBUG) + return; + + if (os_dumps_proc_mem_info(buf, sizeof(buf)) != 0) + return; + + os_printf("%s\n", prompt); + os_printf("===== memory usage =====\n"); + os_printf("%s", buf); + os_printf("==========\n"); + return; +} + +void +bh_log_proc_mem(const char *function, uint32 line) +{ + char prompt[128] = { 0 }; + snprintf(prompt, sizeof(prompt), "[MEM] %s(...) L%u", function, line); + return bh_print_proc_mem(prompt); +} \ No newline at end of file diff --git a/core/shared/utils/bh_log.h b/core/shared/utils/bh_log.h index 1578028b7b..e0bc61da26 100644 --- a/core/shared/utils/bh_log.h +++ b/core/shared/utils/bh_log.h @@ -73,6 +73,14 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...); void bh_print_time(const char *prompt); +void +bh_print_proc_mem(const char *prompt); + +void +bh_log_proc_mem(const char *function, uint32 line); + +#define LOG_PROC_MEM(...) bh_log_proc_mem(__FUNCTION__, __LINE__) + #ifdef __cplusplus } #endif From 0456043d8d31bd830c4751140aa28d90e430b0e8 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 25 Nov 2022 09:20:38 +0800 Subject: [PATCH 6/8] Fix missing intrinsics for risc-v which were reported by spec test (#1750) --- .github/workflows/compilation_on_nuttx.yml | 8 +-- core/iwasm/aot/arch/aot_reloc_riscv.c | 60 +++++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compilation_on_nuttx.yml b/.github/workflows/compilation_on_nuttx.yml index bb49551d15..15758a32b7 100644 --- a/.github/workflows/compilation_on_nuttx.yml +++ b/.github/workflows/compilation_on_nuttx.yml @@ -91,7 +91,10 @@ jobs: - name: Install RISC-V Compilers if: contains(matrix.nuttx_board_config, 'risc-v') - run: sudo apt install -y gcc-riscv64-unknown-elf + run: | + curl -L https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz > riscv.tar.gz + tar xvf riscv.tar.gz + echo "$PWD/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin" >> $GITHUB_PATH - name: Checkout NuttX uses: actions/checkout@v3 @@ -113,9 +116,8 @@ jobs: - name: Enable WAMR for NuttX run: | - find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_CR=y\n${{ matrix.wamr_config_option }}' + find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_LF=y\n${{ matrix.wamr_config_option }}' find nuttx/boards/sim -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n' - find nuttx/boards/risc-v -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n' - name: Build run: | diff --git a/core/iwasm/aot/arch/aot_reloc_riscv.c b/core/iwasm/aot/arch/aot_reloc_riscv.c index 7d7002e654..0d24876e59 100644 --- a/core/iwasm/aot/arch/aot_reloc_riscv.c +++ b/core/iwasm/aot/arch/aot_reloc_riscv.c @@ -16,47 +16,105 @@ #define RV_OPCODE_SW 0x23 /* clang-format off */ +void __adddf3(); +void __addsf3(); void __divdi3(); void __divsi3(); +void __divsf3(); +void __eqsf2(); +void __eqdf2(); +void __extendsfdf2(); void __fixdfdi(); +void __fixdfsi(); void __fixsfdi(); +void __fixsfsi(); void __fixunsdfdi(); +void __fixunsdfsi(); void __fixunssfdi(); +void __fixunssfsi(); void __floatdidf(); void __floatdisf(); +void __floatsisf(); +void __floatsidf(); void __floatundidf(); void __floatundisf(); +void __floatunsisf(); +void __floatunsidf(); +void __gedf2(); +void __gesf2(); +void __gtsf2(); +void __ledf2(); +void __lesf2(); void __moddi3(); void __modsi3(); +void __muldf3(); void __muldi3(); +void __mulsf3(); void __mulsi3(); +void __nedf2(); +void __nesf2(); +void __subdf3(); +void __subsf3(); +void __truncdfsf2(); void __udivdi3(); void __udivsi3(); void __umoddi3(); void __umodsi3(); +void __unorddf2(); +void __unordsf2(); /* clang-format on */ static SymbolMap target_sym_map[] = { /* clang-format off */ REG_COMMON_SYMBOLS +#ifndef __riscv_flen + REG_SYM(__adddf3), + REG_SYM(__addsf3), + REG_SYM(__divsf3), + REG_SYM(__gedf2), + REG_SYM(__gesf2), + REG_SYM(__gtsf2), + REG_SYM(__ledf2), + REG_SYM(__lesf2), + REG_SYM(__muldf3), + REG_SYM(__nedf2), + REG_SYM(__nesf2), + REG_SYM(__eqsf2), + REG_SYM(__eqdf2), + REG_SYM(__extendsfdf2), + REG_SYM(__fixunsdfdi), + REG_SYM(__fixunsdfsi), + REG_SYM(__fixunssfsi), + REG_SYM(__subdf3), + REG_SYM(__subsf3), + REG_SYM(__truncdfsf2), + REG_SYM(__unorddf2), + REG_SYM(__unordsf2), +#endif REG_SYM(__divdi3), REG_SYM(__divsi3), #if __riscv_xlen == 32 REG_SYM(__fixdfdi), + REG_SYM(__fixdfsi), REG_SYM(__fixsfdi), + REG_SYM(__fixsfsi), #endif - REG_SYM(__fixunsdfdi), REG_SYM(__fixunssfdi), #if __riscv_xlen == 32 REG_SYM(__floatdidf), REG_SYM(__floatdisf), + REG_SYM(__floatsisf), + REG_SYM(__floatsidf), REG_SYM(__floatundidf), REG_SYM(__floatundisf), + REG_SYM(__floatunsisf), + REG_SYM(__floatunsidf), #endif REG_SYM(__moddi3), REG_SYM(__modsi3), REG_SYM(__muldi3), #if __riscv_xlen == 32 + REG_SYM(__mulsf3), REG_SYM(__mulsi3), #endif REG_SYM(__udivdi3), From 96570cca22c2e356007c2b0235aad3191cf1c884 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 25 Nov 2022 11:26:08 +0800 Subject: [PATCH 7/8] Remove unused LLVM JIT wapper functions (#1747) Only create the necessary wrapper functions for LLVM JIT --- core/iwasm/compilation/aot_llvm.c | 12 +++++++++++- core/iwasm/interpreter/wasm_loader.c | 2 +- core/iwasm/interpreter/wasm_mini_loader.c | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 7b55eeaa50..b6e8b1164a 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -54,6 +54,7 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module, char func_name[48]; uint64 size; uint32 i, j = 0, param_count = (uint64)aot_func_type->param_count; + uint32 backend_thread_num, compile_thread_num; /* exec env as first parameter */ param_count++; @@ -119,7 +120,16 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module, if (p_func_type) *p_func_type = func_type; - if (comp_ctx->is_jit_mode) { + backend_thread_num = WASM_ORC_JIT_BACKEND_THREAD_NUM; + compile_thread_num = WASM_ORC_JIT_COMPILE_THREAD_NUM; + + /* Add the jit wrapper function with simple prototype, so that we + can easily call it to trigger its compilation and let LLVM JIT + compile the actual jit functions by adding them into the function + list in the PartitionFunction callback */ + if (comp_ctx->is_jit_mode + && (func_index % (backend_thread_num * compile_thread_num) + < backend_thread_num)) { func_type_wrapper = LLVMFunctionType(VOID_TYPE, NULL, 0, false); if (!func_type_wrapper) { aot_set_last_error("create LLVM function type failed."); diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index efd3fb8591..a74780d9d5 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -2955,7 +2955,7 @@ orcjit_thread_callback(void *arg) LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name); if (error != LLVMErrorSuccess) { char *err_msg = LLVMGetErrorMessage(error); - os_printf("failed to compile orc jit function: %s", err_msg); + os_printf("failed to compile orc jit function: %s\n", err_msg); LLVMDisposeErrorMessage(err_msg); continue; } diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 0f37c99833..f009ca875c 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1930,8 +1930,8 @@ compile_llvm_jit_functions(WASMModule *module, char *error_buf, if (error != LLVMErrorSuccess) { char *err_msg = LLVMGetErrorMessage(error); char buf[128]; - snprintf(buf, sizeof(buf), "failed to compile orc jit function: %s", - err_msg); + snprintf(buf, sizeof(buf), + "failed to compile orc jit function: %s\n", err_msg); set_error_buf(error_buf, error_buf_size, buf); LLVMDisposeErrorMessage(err_msg); return false; From 1465901f6fd73ecae2dfab6bd295f55887146020 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 25 Nov 2022 09:45:19 +0000 Subject: [PATCH 8/8] Create trap for error message when wasm_instance_new fails (#1751) Create trap for error message when wasm_instance_new fails: - Similar to [this PR](https://github.com/bytecodealliance/wasm-micro-runtime/pull/1526), but create a wasm_trap_t to output the error msg instead of adding error_buf to the API. - Trap will need to be deleted by the caller but is not a breaking change as it is only created if trap is not NULL. - Add error messages for all failure cases here, try to make them accurate but welcome feedback for improvements. Signed-off-by: Andrew Chambers --- core/iwasm/common/wasm_c_api.c | 43 +++++++++++++++++++++++++++------ core/iwasm/include/wasm_c_api.h | 4 +-- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 5d286434e8..7a24b17a41 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -4444,25 +4444,25 @@ aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot, wasm_instance_t * wasm_instance_new(wasm_store_t *store, const wasm_module_t *module, - const wasm_extern_vec_t *imports, own wasm_trap_t **traps) + const wasm_extern_vec_t *imports, own wasm_trap_t **trap) { - return wasm_instance_new_with_args(store, module, imports, traps, + return wasm_instance_new_with_args(store, module, imports, trap, KILOBYTE(32), KILOBYTE(32)); } wasm_instance_t * wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, const wasm_extern_vec_t *imports, - own wasm_trap_t **traps, const uint32 stack_size, + own wasm_trap_t **trap, const uint32 stack_size, const uint32 heap_size) { - char error_buf[128] = { 0 }; + char sub_error_buf[128] = { 0 }; + char error_buf[256] = { 0 }; uint32 import_count = 0; bool import_count_verified = false; wasm_instance_t *instance = NULL; uint32 i = 0; bool processed = false; - (void)traps; bh_assert(singleton_engine); @@ -4474,6 +4474,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, instance = malloc_internal(sizeof(wasm_instance_t)); if (!instance) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to malloc instance"); goto failed; } @@ -4490,6 +4492,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, /* make sure a complete import list */ if ((int32)import_count < 0 || import_count != actual_link_import_count) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to validate imports"); goto failed; } } @@ -4508,6 +4512,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, import_count = aot_link(instance, MODULE_AOT(module), (wasm_extern_t **)imports->data); if ((int32)import_count < 0) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to validate imports"); goto failed; } } @@ -4520,18 +4526,21 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, * also leads to below branch */ if (!import_count_verified) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to verify import count"); goto failed; } } instance->inst_comm_rt = wasm_runtime_instantiate( - *module, stack_size, heap_size, error_buf, sizeof(error_buf)); + *module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf)); if (!instance->inst_comm_rt) { - LOG_ERROR(error_buf); goto failed; } if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to create exec env singleton"); goto failed; } @@ -4556,6 +4565,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, instance->inst_comm_rt; break; default: + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Unknown import kind"); goto failed; } } @@ -4572,6 +4583,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, if (!interp_process_export(store, (WASMModuleInstance *)instance->inst_comm_rt, instance->exports)) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Interpreter failed to process exports"); goto failed; } @@ -4594,6 +4607,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, if (!aot_process_export(store, (AOTModuleInstance *)instance->inst_comm_rt, instance->exports)) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "AOT failed to process exports"); goto failed; } @@ -4606,11 +4621,15 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, * leads to below branch */ if (!processed) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Incorrect filetype and compilation flags"); goto failed; } /* add it to a watching list in store */ if (!bh_vector_append((Vector *)store->instances, &instance)) { + snprintf(sub_error_buf, sizeof(sub_error_buf), + "Failed to add to store instances"); goto failed; } @@ -4619,7 +4638,15 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, return instance; failed: - LOG_DEBUG("%s failed", __FUNCTION__); + snprintf(error_buf, sizeof(error_buf), "%s failed: %s", __FUNCTION__, + sub_error_buf); + if (trap != NULL) { + wasm_message_t message = { 0 }; + wasm_name_new_from_string(&message, error_buf); + *trap = wasm_trap_new(store, &message); + wasm_byte_vec_delete(&message); + } + LOG_DEBUG(error_buf); wasm_instance_delete_internal(instance); return NULL; } diff --git a/core/iwasm/include/wasm_c_api.h b/core/iwasm/include/wasm_c_api.h index 39ee01a2d5..8900978957 100644 --- a/core/iwasm/include/wasm_c_api.h +++ b/core/iwasm/include/wasm_c_api.h @@ -592,13 +592,13 @@ WASM_DECLARE_REF(instance) WASM_API_EXTERN own wasm_instance_t* wasm_instance_new( wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports, - own wasm_trap_t** + own wasm_trap_t** trap ); // please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args( wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports, - own wasm_trap_t**, const uint32_t stack_size, const uint32_t heap_size + own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size ); WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);