From 7671c2ce4a02416ed7f45b68570e1ed0b8c2b11c Mon Sep 17 00:00:00 2001 From: Benbuck Nason Date: Tue, 2 Jul 2024 16:33:42 -0700 Subject: [PATCH] Fix and suppress some ASAN problems. --- core/iwasm/aot/aot_runtime.c | 6 ++++++ core/iwasm/aot/arch/aot_reloc_x86_64.c | 11 +++++++---- core/iwasm/common/wasm_exec_env.h | 14 ++++++++++++-- core/iwasm/common/wasm_runtime_common.c | 7 +++++-- core/iwasm/common/wasm_runtime_common.h | 10 +++++----- core/iwasm/interpreter/wasm_interp_classic.c | 3 +++ core/iwasm/interpreter/wasm_interp_fast.c | 9 +++++++++ core/iwasm/interpreter/wasm_loader.c | 5 ++++- core/iwasm/interpreter/wasm_mini_loader.c | 2 +- core/iwasm/interpreter/wasm_runtime.c | 6 ++++-- 10 files changed, 56 insertions(+), 17 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 13664ca0ef..69fd007670 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -657,6 +657,9 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, return true; } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static bool tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, AOTTableInstance *first_tbl_inst, char *error_buf, @@ -3053,6 +3056,9 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, return ret; } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif bool aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx, uint32 argc, uint32 *argv) diff --git a/core/iwasm/aot/arch/aot_reloc_x86_64.c b/core/iwasm/aot/arch/aot_reloc_x86_64.c index fe18d79c63..b7cf395faa 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_64.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_64.c @@ -83,7 +83,7 @@ init_plt_table(uint8 *plt) /* mov symbol_addr, rax */ *p++ = 0x48; *p++ = 0xB8; - *(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; + memcpy(p, &target_sym_map[i].symbol_addr, sizeof(uint64)); p += sizeof(uint64); /* jmp rax */ *p++ = 0xFF; @@ -167,7 +167,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, return false; } - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; + memcpy(target_section_addr + reloc_offset, &target_addr, + sizeof(int32)); break; } case R_X86_64_PC64: @@ -203,7 +204,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, return false; } - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; + memcpy(target_section_addr + reloc_offset, &target_addr, + sizeof(int32)); break; } #endif @@ -248,7 +250,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, "Try using wamrc with --size-level=1 or 0 option."); return false; } - *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; + memcpy(target_section_addr + reloc_offset, &target_addr, + sizeof(int32)); break; } diff --git a/core/iwasm/common/wasm_exec_env.h b/core/iwasm/common/wasm_exec_env.h index 53d2487555..243ffe6692 100644 --- a/core/iwasm/common/wasm_exec_env.h +++ b/core/iwasm/common/wasm_exec_env.h @@ -195,6 +195,12 @@ wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env) return exec_env->aux_stack_boundary != 0 || exec_env->aux_stack_bottom != 0; } +static inline uintptr_t +wasm_pointer_align(uintptr_t n) +{ + return (n + (_Alignof(void *) - 1)) & ~(_Alignof(void *) - 1); +} + /** * Allocate a WASM frame from the WASM stack. * @@ -208,22 +214,26 @@ static inline void * wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size) { uint8 *addr = exec_env->wasm_stack.top; + unsigned aligned_size; bh_assert(!(size & 3)); + /* ensure that the next frame pointer meets alignment requirements */ + aligned_size = (unsigned)wasm_pointer_align(size); + /* For classic interpreter, the outs area doesn't contain the const cells, its size cannot be larger than the frame size, so here checking stack overflow with multiplying by 2 is enough. For fast interpreter, since the outs area contains const cells, its size may be larger than current frame size, we should check again before putting the function arguments into the outs area. */ - if (size * 2 + if (aligned_size * 2 > (uint32)(uintptr_t)(exec_env->wasm_stack.top_boundary - addr)) { /* WASM stack overflow. */ return NULL; } - exec_env->wasm_stack.top += size; + exec_env->wasm_stack.top += aligned_size; #if WASM_ENABLE_MEMORY_PROFILING != 0 { diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 314dc7ddb1..771bac116d 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2062,6 +2062,9 @@ wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst, return false; } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif bool wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst, char const *name, @@ -5821,9 +5824,9 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, #endif #endif if (n_ints < MAX_REG_INTS) - ints[n_ints++] = *(uint64 *)argv_src; + memcpy(&ints[n_ints++], argv_src, sizeof(uint64)); else - stacks[n_stacks++] = *(uint64 *)argv_src; + memcpy(&stacks[n_stacks++], argv_src, sizeof(uint64)); argv_src += 2; break; case VALUE_TYPE_F32: diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index fb2c79408d..1cae9e9884 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -55,12 +55,12 @@ extern "C" { static inline void STORE_U32(void *addr, uint32_t value) { - *(uint32_t *)(addr) = (uint32_t)(value); + memcpy(addr, &value, sizeof(uint32_t)); } static inline void STORE_U16(void *addr, uint16_t value) { - *(uint16_t *)(addr) = (uint16_t)(value); + memcpy(addr, &value, sizeof(uint16_t)); } static inline void STORE_U8(void *addr, uint8_t value) @@ -76,9 +76,9 @@ STORE_U8(void *addr, uint8_t value) #define LOAD_I16(addr) (*(int16 *)(addr)) #define LOAD_U16(addr) (*(uint16 *)(addr)) -#define STORE_PTR(addr, ptr) \ - do { \ - *(void **)addr = (void *)ptr; \ +#define STORE_PTR(addr, ptr) \ + do { \ + memcpy(addr, ptr, sizeof(void *)); \ } while (0) #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 67f8c2d455..28b27758e9 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1524,6 +1524,9 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global) #endif } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static void wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMExecEnv *exec_env, diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 1d7ca8f908..b62a73afcc 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -719,6 +719,9 @@ TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64) TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32) TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64) +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static bool trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp, float32 src_min, float32 src_max, bool saturating, bool is_i32, @@ -756,6 +759,9 @@ trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp, return true; } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static bool trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp, float64 src_min, float64 src_max, bool saturating, bool is_i32, @@ -1442,6 +1448,9 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global) #endif } +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static void wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMExecEnv *exec_env, diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 3a21b1fc6b..378fbd995d 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -5289,7 +5289,7 @@ calculate_global_data_offset(WASMModule *module) data_offset += wasm_value_type_size(global->type.val_type); } - module->global_data_size = data_offset; + module->global_data_size = wasm_pointer_align(data_offset); } #if WASM_ENABLE_FAST_JIT != 0 @@ -10882,6 +10882,9 @@ DEFINE_GOTO_TABLE(const char *, op_mnemonics); #undef HANDLE_OPCODE #endif +#if defined(__GNUC__) || defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif static bool wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, uint32 cur_func_idx, char *error_buf, diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 968eaf0096..1950689afa 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -2037,7 +2037,7 @@ calculate_global_data_offset(WASMModule *module) data_offset += wasm_value_type_size(global->type.val_type); } - module->global_data_size = data_offset; + module->global_data_size = wasm_pointer_align(data_offset); } #if WASM_ENABLE_FAST_JIT != 0 diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index e8f4c749e0..ede99cfbd0 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1212,7 +1212,8 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst, } bh_assert((uint32)(global - globals) == global_count); - bh_assert(global_data_offset == module->global_data_size); + bh_assert(wasm_pointer_align(global_data_offset) + == module->global_data_size); (void)module_inst; return globals; fail: @@ -2546,7 +2547,8 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, } } } - bh_assert(global_data == global_data_end); + bh_assert(wasm_pointer_align((uintptr_t)global_data) + == global_data_end); } if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {