From adfd5ed0c2b5c9788b8fda384e8ca0b3e77d27e9 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Fri, 24 Jun 2022 11:20:31 +0800 Subject: [PATCH] address PR comments --- core/iwasm/aot/aot_runtime.c | 25 ++++++--------------- core/iwasm/aot/aot_runtime.h | 6 ++--- core/iwasm/common/wasm_runtime_common.c | 24 ++++++++++++++++++++ core/iwasm/common/wasm_runtime_common.h | 22 ++++++++++++++++++ core/iwasm/include/wasm_export.h | 2 +- core/iwasm/interpreter/wasm_runtime.c | 30 +++++++++---------------- core/iwasm/interpreter/wasm_runtime.h | 6 ++--- 7 files changed, 71 insertions(+), 44 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index d4f0f2fdd..64024ce07 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -3063,24 +3063,13 @@ aot_create_call_stack(struct WASMExecEnv *exec_env) return true; } -#define PRINT_OR_DUMP() \ - do { \ - if (print) { \ - os_printf("%s", line_buf); \ - } \ - else if (buf) { \ - uint32 remain_len = len - total_len; \ - uint32 string_len = \ - snprintf(buf + total_len, remain_len, "%s", line_buf); \ - if (string_len >= remain_len) { \ - /* Buffer full */ \ - return len; \ - } \ - total_len += string_len; \ - } \ - else { \ - total_len += strlen(line_buf); \ - } \ +#define PRINT_OR_DUMP() \ + do { \ + total_len += \ + wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \ + if ((!print) && buf && (len == 0)) { \ + return total_len; \ + } \ } while (0) uint32 diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 076bd52fd..c3838b585 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -735,17 +735,17 @@ bool aot_create_call_stack(struct WASMExecEnv *exec_env); /** - * @brief dump aot call stack or get the size + * @brief Dump wasm call stack or get the size * * @param exec_env the execution environment - * @param print print to stdout + * @param print whether to print to stdout or not * @param buf buffer to store the dumped content * @param len length of the buffer * * @return when print is true, return the bytes printed out to stdout; when * print is false and buf is NULL, return the size required to store the * callstack content; when print is false and buf is not NULL, return the size - * dumped to the buffer + * dumped to the buffer, 0 means error and data in buf may be invalid */ uint32 aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 8a0957149..41ff732ff 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -4527,6 +4527,30 @@ wasm_externref_retain(uint32 externref_idx) #endif /* end of WASM_ENABLE_REF_TYPES */ #if WASM_ENABLE_DUMP_CALL_STACK != 0 +uint32 +wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print, + char **buf, uint32 *len) +{ + if (dump_or_print) { + return (uint32)os_printf("%s", line_buf); + } + else if (*buf) { + uint32 dump_len; + + dump_len = snprintf(*buf, *len, "%s", line_buf); + if (dump_len >= *len) { + dump_len = *len; + } + + *len = *len - dump_len; + *buf = *buf + dump_len; + return dump_len; + } + else { + return strlen(line_buf); + } +} + void wasm_runtime_dump_call_stack(WASMExecEnv *exec_env) { diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index a704678ea..24d9acd0b 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -804,6 +804,28 @@ void wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst); #endif /* end of WASM_ENABLE_REF_TYPES */ +#if WASM_ENABLE_DUMP_CALL_STACK != 0 +/** + * @brief Internal implementation for dumping or printing callstack line + * + * @note if dump_or_print is true, then print to stdout directly + * if dump_or_print is false, but *buf is NULL, then return the length of + * the line + * if dump_or_print is false, and *buf is not NULL, then dump content to the + * memory pointed by *buf, and adjust *buf and *len according to actual bytes + * dumped, and return the actual dumped length + * + * @param line_buf current line to dump or print + * @param dump_or_print whether to print to stdout or dump to buf + * @param buf [INOUT] pointer to the buffer + * @param len [INOUT] pointer to remaining length + * @return bytes printed to stdout or dumped to buf + */ +uint32 +wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print, + char **buf, uint32 *len); +#endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */ + /* Get module of the current exec_env */ WASMModuleCommon * wasm_exec_env_get_module(WASMExecEnv *exec_env); diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 1c720a545..ed7a865b8 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -1076,7 +1076,7 @@ wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env); * @param len length of the buffer * * @return bytes dumped to the buffer, including the terminating null - * byte ('\0'), 0 means error and buf may remain untouched + * byte ('\0'), 0 means error and data in buf may be invalid */ WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf, diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 2761dcfd7..deba92dca 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2562,7 +2562,10 @@ wasm_interp_create_call_stack(struct WASMExecEnv *exec_env) frame.func_name_wp = func_name; - bh_vector_append(module_inst->frames, &frame); + if (!bh_vector_append(module_inst->frames, &frame)) { + bh_vector_destroy(module_inst->frames); + return false; + } cur_frame = cur_frame->prev_frame; n++; @@ -2571,24 +2574,13 @@ wasm_interp_create_call_stack(struct WASMExecEnv *exec_env) return true; } -#define PRINT_OR_DUMP() \ - do { \ - if (print) { \ - os_printf("%s", line_buf); \ - } \ - else if (buf) { \ - uint32 remain_len = len - total_len; \ - uint32 string_len = \ - snprintf(buf + total_len, remain_len, "%s", line_buf); \ - if (string_len >= remain_len) { \ - /* Buffer full */ \ - return len; \ - } \ - total_len += string_len; \ - } \ - else { \ - total_len += strlen(line_buf); \ - } \ +#define PRINT_OR_DUMP() \ + do { \ + total_len += \ + wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \ + if ((!print) && buf && (len == 0)) { \ + return total_len; \ + } \ } while (0) uint32 diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index dcedb4c34..c780e6a11 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -436,17 +436,17 @@ bool wasm_interp_create_call_stack(struct WASMExecEnv *exec_env); /** - * @brief dump wasm call stack or get the size + * @brief Dump wasm call stack or get the size * * @param exec_env the execution environment - * @param print print to stdout + * @param print whether to print to stdout or not * @param buf buffer to store the dumped content * @param len length of the buffer * * @return when print is true, return the bytes printed out to stdout; when * print is false and buf is NULL, return the size required to store the * callstack content; when print is false and buf is not NULL, return the size - * dumped to the buffer + * dumped to the buffer, 0 means error and data in buf may be invalid */ uint32 wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,