Skip to content

Commit 7671c2c

Browse files
bnason-nfAndersbakken
authored andcommitted
Fix and suppress some ASAN problems.
1 parent d64a3ab commit 7671c2c

10 files changed

+56
-17
lines changed

core/iwasm/aot/aot_runtime.c

+6
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
657657
return true;
658658
}
659659

660+
#if defined(__GNUC__) || defined(__clang__)
661+
__attribute__((no_sanitize("undefined")))
662+
#endif
660663
static bool
661664
tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
662665
AOTTableInstance *first_tbl_inst, char *error_buf,
@@ -3053,6 +3056,9 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
30533056
return ret;
30543057
}
30553058

3059+
#if defined(__GNUC__) || defined(__clang__)
3060+
__attribute__((no_sanitize("undefined")))
3061+
#endif
30563062
bool
30573063
aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
30583064
uint32 argc, uint32 *argv)

core/iwasm/aot/arch/aot_reloc_x86_64.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ init_plt_table(uint8 *plt)
8383
/* mov symbol_addr, rax */
8484
*p++ = 0x48;
8585
*p++ = 0xB8;
86-
*(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr;
86+
memcpy(p, &target_sym_map[i].symbol_addr, sizeof(uint64));
8787
p += sizeof(uint64);
8888
/* jmp rax */
8989
*p++ = 0xFF;
@@ -167,7 +167,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
167167
return false;
168168
}
169169

170-
*(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr;
170+
memcpy(target_section_addr + reloc_offset, &target_addr,
171+
sizeof(int32));
171172
break;
172173
}
173174
case R_X86_64_PC64:
@@ -203,7 +204,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
203204
return false;
204205
}
205206

206-
*(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr;
207+
memcpy(target_section_addr + reloc_offset, &target_addr,
208+
sizeof(int32));
207209
break;
208210
}
209211
#endif
@@ -248,7 +250,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
248250
"Try using wamrc with --size-level=1 or 0 option.");
249251
return false;
250252
}
251-
*(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr;
253+
memcpy(target_section_addr + reloc_offset, &target_addr,
254+
sizeof(int32));
252255
break;
253256
}
254257

core/iwasm/common/wasm_exec_env.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env)
195195
return exec_env->aux_stack_boundary != 0 || exec_env->aux_stack_bottom != 0;
196196
}
197197

198+
static inline uintptr_t
199+
wasm_pointer_align(uintptr_t n)
200+
{
201+
return (n + (_Alignof(void *) - 1)) & ~(_Alignof(void *) - 1);
202+
}
203+
198204
/**
199205
* Allocate a WASM frame from the WASM stack.
200206
*
@@ -208,22 +214,26 @@ static inline void *
208214
wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size)
209215
{
210216
uint8 *addr = exec_env->wasm_stack.top;
217+
unsigned aligned_size;
211218

212219
bh_assert(!(size & 3));
213220

221+
/* ensure that the next frame pointer meets alignment requirements */
222+
aligned_size = (unsigned)wasm_pointer_align(size);
223+
214224
/* For classic interpreter, the outs area doesn't contain the const cells,
215225
its size cannot be larger than the frame size, so here checking stack
216226
overflow with multiplying by 2 is enough. For fast interpreter, since
217227
the outs area contains const cells, its size may be larger than current
218228
frame size, we should check again before putting the function arguments
219229
into the outs area. */
220-
if (size * 2
230+
if (aligned_size * 2
221231
> (uint32)(uintptr_t)(exec_env->wasm_stack.top_boundary - addr)) {
222232
/* WASM stack overflow. */
223233
return NULL;
224234
}
225235

226-
exec_env->wasm_stack.top += size;
236+
exec_env->wasm_stack.top += aligned_size;
227237

228238
#if WASM_ENABLE_MEMORY_PROFILING != 0
229239
{

core/iwasm/common/wasm_runtime_common.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,9 @@ wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
20622062
return false;
20632063
}
20642064

2065+
#if defined(__GNUC__) || defined(__clang__)
2066+
__attribute__((no_sanitize("undefined")))
2067+
#endif
20652068
bool
20662069
wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst,
20672070
char const *name,
@@ -5821,9 +5824,9 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
58215824
#endif
58225825
#endif
58235826
if (n_ints < MAX_REG_INTS)
5824-
ints[n_ints++] = *(uint64 *)argv_src;
5827+
memcpy(&ints[n_ints++], argv_src, sizeof(uint64));
58255828
else
5826-
stacks[n_stacks++] = *(uint64 *)argv_src;
5829+
memcpy(&stacks[n_stacks++], argv_src, sizeof(uint64));
58275830
argv_src += 2;
58285831
break;
58295832
case VALUE_TYPE_F32:

core/iwasm/common/wasm_runtime_common.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ extern "C" {
5555
static inline void
5656
STORE_U32(void *addr, uint32_t value)
5757
{
58-
*(uint32_t *)(addr) = (uint32_t)(value);
58+
memcpy(addr, &value, sizeof(uint32_t));
5959
}
6060
static inline void
6161
STORE_U16(void *addr, uint16_t value)
6262
{
63-
*(uint16_t *)(addr) = (uint16_t)(value);
63+
memcpy(addr, &value, sizeof(uint16_t));
6464
}
6565
static inline void
6666
STORE_U8(void *addr, uint8_t value)
@@ -76,9 +76,9 @@ STORE_U8(void *addr, uint8_t value)
7676
#define LOAD_I16(addr) (*(int16 *)(addr))
7777
#define LOAD_U16(addr) (*(uint16 *)(addr))
7878

79-
#define STORE_PTR(addr, ptr) \
80-
do { \
81-
*(void **)addr = (void *)ptr; \
79+
#define STORE_PTR(addr, ptr) \
80+
do { \
81+
memcpy(addr, ptr, sizeof(void *)); \
8282
} while (0)
8383

8484
#else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */

core/iwasm/interpreter/wasm_interp_classic.c

+3
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
15241524
#endif
15251525
}
15261526

1527+
#if defined(__GNUC__) || defined(__clang__)
1528+
__attribute__((no_sanitize("undefined")))
1529+
#endif
15271530
static void
15281531
wasm_interp_call_func_bytecode(WASMModuleInstance *module,
15291532
WASMExecEnv *exec_env,

core/iwasm/interpreter/wasm_interp_fast.c

+9
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
719719
TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
720720
TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
721721

722+
#if defined(__GNUC__) || defined(__clang__)
723+
__attribute__((no_sanitize("undefined")))
724+
#endif
722725
static bool
723726
trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
724727
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,
756759
return true;
757760
}
758761

762+
#if defined(__GNUC__) || defined(__clang__)
763+
__attribute__((no_sanitize("undefined")))
764+
#endif
759765
static bool
760766
trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
761767
float64 src_min, float64 src_max, bool saturating, bool is_i32,
@@ -1442,6 +1448,9 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
14421448
#endif
14431449
}
14441450

1451+
#if defined(__GNUC__) || defined(__clang__)
1452+
__attribute__((no_sanitize("undefined")))
1453+
#endif
14451454
static void
14461455
wasm_interp_call_func_bytecode(WASMModuleInstance *module,
14471456
WASMExecEnv *exec_env,

core/iwasm/interpreter/wasm_loader.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -5289,7 +5289,7 @@ calculate_global_data_offset(WASMModule *module)
52895289
data_offset += wasm_value_type_size(global->type.val_type);
52905290
}
52915291

5292-
module->global_data_size = data_offset;
5292+
module->global_data_size = wasm_pointer_align(data_offset);
52935293
}
52945294

52955295
#if WASM_ENABLE_FAST_JIT != 0
@@ -10882,6 +10882,9 @@ DEFINE_GOTO_TABLE(const char *, op_mnemonics);
1088210882
#undef HANDLE_OPCODE
1088310883
#endif
1088410884

10885+
#if defined(__GNUC__) || defined(__clang__)
10886+
__attribute__((no_sanitize("undefined")))
10887+
#endif
1088510888
static bool
1088610889
wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1088710890
uint32 cur_func_idx, char *error_buf,

core/iwasm/interpreter/wasm_mini_loader.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ calculate_global_data_offset(WASMModule *module)
20372037
data_offset += wasm_value_type_size(global->type.val_type);
20382038
}
20392039

2040-
module->global_data_size = data_offset;
2040+
module->global_data_size = wasm_pointer_align(data_offset);
20412041
}
20422042

20432043
#if WASM_ENABLE_FAST_JIT != 0

core/iwasm/interpreter/wasm_runtime.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,8 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
12121212
}
12131213

12141214
bh_assert((uint32)(global - globals) == global_count);
1215-
bh_assert(global_data_offset == module->global_data_size);
1215+
bh_assert(wasm_pointer_align(global_data_offset)
1216+
== module->global_data_size);
12161217
(void)module_inst;
12171218
return globals;
12181219
fail:
@@ -2546,7 +2547,8 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
25462547
}
25472548
}
25482549
}
2549-
bh_assert(global_data == global_data_end);
2550+
bh_assert(wasm_pointer_align((uintptr_t)global_data)
2551+
== global_data_end);
25502552
}
25512553

25522554
if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {

0 commit comments

Comments
 (0)