Skip to content

Commit

Permalink
Fix and suppress some ASAN problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnason-nf authored and Andersbakken committed Sep 18, 2024
1 parent d64a3ab commit a28083e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 25 deletions.
4 changes: 2 additions & 2 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
return true;
}

static bool
static __attribute__((no_sanitize("undefined"))) bool
tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
AOTTableInstance *first_tbl_inst, char *error_buf,
uint32 error_buf_size)
Expand Down Expand Up @@ -3053,7 +3053,7 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
return ret;
}

bool
__attribute__((no_sanitize("undefined"))) bool
aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
uint32 argc, uint32 *argv)
{
Expand Down
11 changes: 7 additions & 4 deletions core/iwasm/aot/arch/aot_reloc_x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 12 additions & 2 deletions core/iwasm/common/wasm_exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 = 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
{
Expand Down
6 changes: 3 additions & 3 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
return false;
}

bool
bool __attribute__((no_sanitize("undefined")))
wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst,
char const *name,
wasm_table_inst_t *table_inst)
Expand Down Expand Up @@ -5821,9 +5821,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:
Expand Down
10 changes: 5 additions & 5 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
#endif
}

static void
static __attribute__((no_sanitize("undefined"))) void
wasm_interp_call_func_bytecode(WASMModuleInstance *module,
WASMExecEnv *exec_env,
WASMFunctionInstance *cur_func,
Expand Down
6 changes: 3 additions & 3 deletions core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ 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)

static bool
static __attribute__((no_sanitize("undefined"))) bool
trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
float32 src_min, float32 src_max, bool saturating, bool is_i32,
bool is_sign)
Expand Down Expand Up @@ -756,7 +756,7 @@ trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
return true;
}

static bool
static __attribute__((no_sanitize("undefined"))) bool
trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
float64 src_min, float64 src_max, bool saturating, bool is_i32,
bool is_sign)
Expand Down Expand Up @@ -1442,7 +1442,7 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
#endif
}

static void
static __attribute__((no_sanitize("undefined"))) void
wasm_interp_call_func_bytecode(WASMModuleInstance *module,
WASMExecEnv *exec_env,
WASMFunctionInstance *cur_func,
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -10882,7 +10882,7 @@ DEFINE_GOTO_TABLE(const char *, op_mnemonics);
#undef HANDLE_OPCODE
#endif

static bool
static __attribute__((no_sanitize("undefined"))) bool
wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
uint32 cur_func_idx, char *error_buf,
uint32 error_buf_size)
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit a28083e

Please sign in to comment.