Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and suppress some ASAN problems. #3798

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
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 = (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
{
Expand Down
7 changes: 5 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
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
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion 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,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,
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
Loading