From fc3077b74d2550d59fcd86d098931782d2e6bf66 Mon Sep 17 00:00:00 2001 From: Georgii Rylov Date: Thu, 27 Feb 2025 14:32:17 +0000 Subject: [PATCH] address comments --- core/config.h | 4 ++++ core/iwasm/aot/aot_runtime.c | 25 +++++++++++++++---------- core/iwasm/aot/aot_runtime.h | 5 +++-- core/iwasm/common/wasm_runtime_common.c | 11 +++++++---- core/iwasm/common/wasm_runtime_common.h | 9 +++------ core/iwasm/include/wasm_export.h | 11 ++++++----- core/iwasm/interpreter/wasm_runtime.c | 6 ++++-- core/iwasm/interpreter/wasm_runtime.h | 5 +++-- 8 files changed, 45 insertions(+), 31 deletions(-) diff --git a/core/config.h b/core/config.h index fbbbf6771..d71aaca39 100644 --- a/core/config.h +++ b/core/config.h @@ -193,6 +193,10 @@ #error "Heap aux stack allocation must be enabled for WASI threads" #endif +#ifndef WAMR_ENABLE_COPY_CALLSTACK +#define WAMR_ENABLE_COPY_CALLSTACK 0 +#endif + #ifndef WASM_ENABLE_BASE_LIB #define WASM_ENABLE_BASE_LIB 0 #endif diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 19e71889d..b06b19af1 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -10,6 +10,7 @@ #include "../common/wasm_runtime_common.h" #include "../common/wasm_memory.h" #include "../interpreter/wasm_runtime.h" +#include #if WASM_ENABLE_SHARED_MEMORY != 0 #include "../common/wasm_shared_memory.h" #endif @@ -4105,8 +4106,8 @@ aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame) #if WAMR_ENABLE_COPY_CALLSTACK != 0 uint32 -aot_copy_callstack_tiny_frame(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, - const uint32 length, const uint32 skip_n) +aot_copy_callstack_tiny_frame(WASMExecEnv *exec_env, wasm_frame_t* buffer, + const uint32 length, const uint32 skip_n, char *error_buf, uint32 error_buf_size) { /* * Note for devs: please refrain from such modifications inside of @@ -4126,12 +4127,16 @@ aot_copy_callstack_tiny_frame(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, bool is_top_index_in_range = top_boundary >= top && top >= (bottom + sizeof(AOTTinyFrame)); if (!is_top_index_in_range) { - return count; + char* err_msg = "Top of the stack pointer is outside of the stack boundaries"; + strncpy(error_buf, err_msg, error_buf_size); + return 0; } bool is_top_aligned_with_bottom = (unsigned long)(top - bottom) % sizeof(AOTTinyFrame) == 0; if (!is_top_aligned_with_bottom) { - return count; + char* err_msg = "Top of the stack is not aligned with the bottom"; + strncpy(error_buf, err_msg, error_buf_size); + return 0; } AOTTinyFrame *frame = (AOTTinyFrame *)(top - sizeof(AOTTinyFrame)); @@ -4155,8 +4160,8 @@ aot_copy_callstack_tiny_frame(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, uint32 aot_copy_callstack_standard_frame(WASMExecEnv *exec_env, - wasm_frame_ptr_t buffer, const uint32 length, - const uint32 skip_n) + wasm_frame_t* buffer, const uint32 length, + const uint32 skip_n, char *error_buf, uint32_t error_buf_size) { /* * Note for devs: please refrain from such modifications inside of @@ -4203,8 +4208,8 @@ aot_copy_callstack_standard_frame(WASMExecEnv *exec_env, } uint32 -aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, - const uint32 length, const uint32 skip_n) +aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_t* buffer, + const uint32 length, const uint32 skip_n, char *error_buf, uint32_t error_buf_size) { /* * Note for devs: please refrain from such modifications inside of @@ -4217,10 +4222,10 @@ aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, */ if (!is_tiny_frame(exec_env)) { return aot_copy_callstack_standard_frame(exec_env, buffer, length, - skip_n); + skip_n, error_buf, error_buf_size); } else { - return aot_copy_callstack_tiny_frame(exec_env, buffer, length, skip_n); + return aot_copy_callstack_tiny_frame(exec_env, buffer, length, skip_n, error_buf, error_buf_size); } } #endif // WAMR_ENABLE_COPY_CALLSTACK diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index a338cb4ef..5be51c05a 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -779,8 +779,9 @@ aot_create_call_stack(struct WASMExecEnv *exec_env); #if WAMR_ENABLE_COPY_CALLSTACK != 0 uint32 -aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, - const uint32 length, const uint32 skip_n); +aot_copy_callstack(WASMExecEnv *exec_env, wasm_frame_t *buffer, + const uint32 length, const uint32 skip_n, char *error_buf, + uint32_t error_buf_size); #endif // WAMR_ENABLE_COPY_CALLSTACK /** diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 065bff1da..99f0522e9 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1742,8 +1742,9 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env) #if WAMR_ENABLE_COPY_CALLSTACK != 0 uint32 -wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_ptr_t buffer, - const uint32 length, const uint32 skip_n) +wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_t *buffer, + const uint32 length, const uint32 skip_n, char *error_buf, + uint32_t error_buf_size) { /* * Note for devs: please refrain from such modifications inside of @@ -1760,13 +1761,15 @@ wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_ptr_t buffer, #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - return wasm_interp_copy_callstack(exec_env, buffer, length, skip_n); + return wasm_interp_copy_callstack(exec_env, buffer, length, skip_n, + error_buf, error_buf_size); } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - return aot_copy_callstack(exec_env, buffer, length, skip_n); + return aot_copy_callstack(exec_env, buffer, length, skip_n, error_buf, + error_buf_size); } #endif #endif diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index f89c8b5ee..c6425af20 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -639,14 +639,11 @@ wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst, WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env); -#ifndef WAMR_ENABLE_COPY_CALLSTACK -#define WAMR_ENABLE_COPY_CALLSTACK 0 -#endif - #if WAMR_ENABLE_COPY_CALLSTACK != 0 WASM_RUNTIME_API_EXTERN uint32_t -wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_ptr_t buffer, - const uint32 length, const uint32 skip_n); +wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_t *buffer, + const uint32 length, const uint32 skip_n, char *error_buf, + uint32 error_buf_size); #endif // WAMR_ENABLE_COPY_CALLSTACK /* See wasm_export.h for description */ diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index cacc68e2a..e63ff0124 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -139,7 +139,7 @@ typedef struct wasm_frame_t { uint32_t *lp; } WASMCApiFrame; -typedef struct wasm_frame_t *wasm_frame_ptr_t; +typedef WASMCApiFrame wasm_frame_t; /* WASM section */ typedef struct wasm_section_t { @@ -896,16 +896,17 @@ wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env); * - exec_env->module_inst->module * * @param exec_env the execution environment that containes frames - * @param buffer the buffer of size equal length * sizeof(frame) to copy frames - * to + * @param buffer the buffer of size equal length * sizeof(wasm_frame_t) to copy + * frames to * @param length the number of frames to copy * @param skip_n the number of frames to skip from the top of the stack * * @return number of copied frames */ WASM_RUNTIME_API_EXTERN uint32_t -wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_ptr_t buffer, - const uint32_t length, const uint32_t skip_n); +wasm_copy_callstack(const wasm_exec_env_t exec_env, wasm_frame_t *buffer, + const uint32_t length, const uint32_t skip_n, + char *error_buf, uint32_t error_buf_size); /** * Get the singleton execution environment for the instance. diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 0f75eb9f9..38fc02292 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -5,6 +5,7 @@ #include "wasm_runtime.h" #include "wasm.h" +#include "wasm_c_api.h" #include "wasm_exec_env.h" #include "wasm_loader.h" #include "wasm_interp.h" @@ -4198,8 +4199,9 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, #if WAMR_ENABLE_COPY_CALLSTACK != 0 uint32 -wasm_interp_copy_callstack(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, - uint32 length, uint32 skip_n) +wasm_interp_copy_callstack(WASMExecEnv *exec_env, wasm_frame_t *buffer, + uint32 length, uint32 skip_n, char *error_buf, + uint32_t error_buf_size) { /* * Note for devs: please refrain from such modifications inside of diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 3747eb80f..8d38c8831 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -733,8 +733,9 @@ wasm_get_table_inst(const WASMModuleInstance *module_inst, uint32 tbl_idx) #if WAMR_ENABLE_COPY_CALLSTACK != 0 uint32 -wasm_interp_copy_callstack(WASMExecEnv *exec_env, wasm_frame_ptr_t buffer, - uint32 length, uint32 skip_n); +wasm_interp_copy_callstack(WASMExecEnv *exec_env, wasm_frame_t *buffer, + uint32 length, uint32 skip_n, char *error_buf, + uint32_t error_buf_size); #endif // WAMR_ENABLE_COPY_CALLSTACK bool