Skip to content

Commit 5547924

Browse files
authored
Refine codes and fix several issues (#882)
Refine some codes in wasm loader Add -Wshadow to gcc compile flags and fix some variable shadowed issues Fix function parameter/return types not checked issue Fix fast-interp loader reserve_block_ret() not handle V128 return type issue Fix mini loader load_table_segment_section() failed issue Add detailed comments for argc argument in wasm_runtime_call_wasm()
1 parent 915b26b commit 5547924

File tree

13 files changed

+273
-289
lines changed

13 files changed

+273
-289
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,14 +1454,14 @@ load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
14541454
/* Now code points to an ELF object, we pull it down to .text section */
14551455
uint64 offset;
14561456
uint64 size;
1457-
char *buf = module->code;
1458-
module->elf_hdr = buf;
1459-
if (!get_text_section(buf, &offset, &size)) {
1457+
char *code_buf = module->code;
1458+
module->elf_hdr = code_buf;
1459+
if (!get_text_section(code_buf, &offset, &size)) {
14601460
set_error_buf(error_buf, error_buf_size,
14611461
"get text section of ELF failed");
14621462
return false;
14631463
}
1464-
module->code = buf + offset;
1464+
module->code = code_buf + offset;
14651465
module->code_size -= (uint32)offset;
14661466
}
14671467
#endif

core/iwasm/aot/aot_runtime.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ table_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
216216

217217
/* fill table with element segment content */
218218
for (i = 0; i < module->table_init_data_count; i++) {
219-
AOTTableInstance *tbl_inst;
220-
221219
table_seg = module->table_init_data_list[i];
222220

223221
#if WASM_ENABLE_REF_TYPES != 0
@@ -1404,6 +1402,16 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
14041402
uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
14051403
bool ret;
14061404

1405+
if (argc < func_type->param_cell_num) {
1406+
char buf[128];
1407+
snprintf(buf, sizeof(buf),
1408+
"invalid argument count %u, must be no smaller than %u", argc,
1409+
func_type->param_cell_num);
1410+
aot_set_exception(module_inst, buf);
1411+
return false;
1412+
}
1413+
argc = func_type->param_cell_num;
1414+
14071415
/* set thread handle and stack boundary */
14081416
wasm_exec_env_set_thread_info(exec_env);
14091417

core/iwasm/common/wasm_shared_memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ destroy_wait_info(void *wait_info)
283283
}
284284

285285
static void
286-
release_wait_info(HashMap *wait_map, AtomicWaitInfo *wait_info, void *address)
286+
release_wait_info(HashMap *wait_map_, AtomicWaitInfo *wait_info, void *address)
287287
{
288288
if (wait_info->wait_list->len == 0) {
289-
bh_hash_map_remove(wait_map, address, NULL, NULL);
289+
bh_hash_map_remove(wait_map_, address, NULL, NULL);
290290
destroy_wait_info(wait_info);
291291
}
292292
}

core/iwasm/compilation/aot_compiler.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,35 +311,35 @@ check_type_compatible(uint8 src_type, uint8 dst_type)
311311
if (!(func_type = \
312312
LLVMFunctionType(ret_type, param_types, argc, false))) { \
313313
aot_set_last_error("llvm add function type failed."); \
314-
return false; \
314+
goto fail; \
315315
} \
316316
if (comp_ctx->is_jit_mode) { \
317317
/* JIT mode, call the function directly */ \
318318
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
319319
aot_set_last_error("llvm add pointer type failed."); \
320-
return false; \
320+
goto fail; \
321321
} \
322322
if (!(value = I64_CONST((uint64)(uintptr_t)name)) \
323323
|| !(func = LLVMConstIntToPtr(value, func_ptr_type))) { \
324324
aot_set_last_error("create LLVM value failed."); \
325-
return false; \
325+
goto fail; \
326326
} \
327327
} \
328328
else if (comp_ctx->is_indirect_mode) { \
329329
int32 func_index; \
330330
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
331331
aot_set_last_error("create LLVM function type failed."); \
332-
return false; \
332+
goto fail; \
333333
} \
334334
\
335335
func_index = aot_get_native_symbol_index(comp_ctx, #name); \
336336
if (func_index < 0) { \
337-
return false; \
337+
goto fail; \
338338
} \
339339
if (!(func = aot_get_func_from_table( \
340340
comp_ctx, func_ctx->native_symbol, func_ptr_type, \
341341
func_index))) { \
342-
return false; \
342+
goto fail; \
343343
} \
344344
} \
345345
else { \
@@ -349,7 +349,7 @@ check_type_compatible(uint8 src_type, uint8 dst_type)
349349
&& !(func = LLVMAddFunction(comp_ctx->module, func_name, \
350350
func_type))) { \
351351
aot_set_last_error("llvm add function failed."); \
352-
return false; \
352+
goto fail; \
353353
} \
354354
} \
355355
} while (0)

core/iwasm/compilation/aot_emit_function.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
345345
}
346346

347347
return true;
348+
fail:
349+
return false;
348350
}
349351
#endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \
350352
|| (WASM_ENABLE_PERF_PROFILING != 0) */

core/iwasm/include/wasm_export.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,10 @@ wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
477477
* @param exec_env the execution environment to call the function,
478478
* which must be created from wasm_create_exec_env()
479479
* @param function the function to call
480-
* @param argc the number of arguments
480+
* @param argc total cell number that the function parameters occupy,
481+
* a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
482+
* occupies one cell, i64/f64 argument occupies two cells, note that
483+
* it might be different from the parameter number of the function
481484
* @param argv the arguments. If the function has return value,
482485
* the first (or first two in case 64-bit return value) element of
483486
* argv stores the return value of the called WASM function after this

core/iwasm/interpreter/wasm.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ wasm_value_type_size(uint8 value_type)
520520
case VALUE_TYPE_V128:
521521
return sizeof(int64) * 2;
522522
#endif
523+
case VALUE_TYPE_VOID:
524+
return 0;
523525
default:
524526
bh_assert(0);
525527
}
@@ -529,25 +531,7 @@ wasm_value_type_size(uint8 value_type)
529531
inline static uint16
530532
wasm_value_type_cell_num(uint8 value_type)
531533
{
532-
if (value_type == VALUE_TYPE_VOID)
533-
return 0;
534-
else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
535-
#if WASM_ENABLE_REF_TYPES != 0
536-
|| value_type == VALUE_TYPE_FUNCREF
537-
|| value_type == VALUE_TYPE_EXTERNREF
538-
#endif
539-
)
540-
return 1;
541-
else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
542-
return 2;
543-
#if WASM_ENABLE_SIMD != 0
544-
else if (value_type == VALUE_TYPE_V128)
545-
return 4;
546-
#endif
547-
else {
548-
bh_assert(0);
549-
}
550-
return 0;
534+
return wasm_value_type_size(value_type) / 4;
551535
}
552536

553537
inline static uint32

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,19 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
256256
--frame_csp; \
257257
} while (0)
258258

259-
#define POP_CSP_N(n) \
260-
do { \
261-
uint32 *frame_sp_old = frame_sp; \
262-
uint32 cell_num = 0; \
263-
POP_CSP_CHECK_OVERFLOW(n + 1); \
264-
frame_csp -= n; \
265-
frame_ip = (frame_csp - 1)->target_addr; \
266-
/* copy arity values of block */ \
267-
frame_sp = (frame_csp - 1)->frame_sp; \
268-
cell_num = (frame_csp - 1)->cell_num; \
269-
word_copy(frame_sp, frame_sp_old - cell_num, cell_num); \
270-
frame_sp += cell_num; \
259+
#define POP_CSP_N(n) \
260+
do { \
261+
uint32 *frame_sp_old = frame_sp; \
262+
uint32 cell_num_to_copy; \
263+
POP_CSP_CHECK_OVERFLOW(n + 1); \
264+
frame_csp -= n; \
265+
frame_ip = (frame_csp - 1)->target_addr; \
266+
/* copy arity values of block */ \
267+
frame_sp = (frame_csp - 1)->frame_sp; \
268+
cell_num_to_copy = (frame_csp - 1)->cell_num; \
269+
word_copy(frame_sp, frame_sp_old - cell_num_to_copy, \
270+
cell_num_to_copy); \
271+
frame_sp += cell_num_to_copy; \
271272
} while (0)
272273

273274
/* Pop the given number of elements from the given frame's stack. */
@@ -367,11 +368,11 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
367368
PUSH_##src_op_type(cval); \
368369
} while (0)
369370

370-
#define DEF_OP_EQZ(src_op_type) \
371-
do { \
372-
int32 val; \
373-
val = POP_##src_op_type() == 0; \
374-
PUSH_I32(val); \
371+
#define DEF_OP_EQZ(src_op_type) \
372+
do { \
373+
int32 pop_val; \
374+
pop_val = POP_##src_op_type() == 0; \
375+
PUSH_I32(pop_val); \
375376
} while (0)
376377

377378
#define DEF_OP_CMP(src_type, src_op_type, cond) \
@@ -434,9 +435,9 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
434435

435436
#define DEF_OP_MATH(src_type, src_op_type, method) \
436437
do { \
437-
src_type val; \
438-
val = POP_##src_op_type(); \
439-
PUSH_##src_op_type(method(val)); \
438+
src_type src_val; \
439+
src_val = POP_##src_op_type(); \
440+
PUSH_##src_op_type(method(src_val)); \
440441
} while (0)
441442

442443
#define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
@@ -1384,22 +1385,22 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
13841385

13851386
HANDLE_OP(WASM_OP_TABLE_SET)
13861387
{
1387-
uint32 tbl_idx, elem_idx, val;
1388+
uint32 tbl_idx, elem_idx, elem_val;
13881389
WASMTableInstance *tbl_inst;
13891390

13901391
read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
13911392
bh_assert(tbl_idx < module->table_count);
13921393

13931394
tbl_inst = wasm_get_table_inst(module, tbl_idx);
13941395

1395-
val = POP_I32();
1396+
elem_val = POP_I32();
13961397
elem_idx = POP_I32();
13971398
if (elem_idx >= tbl_inst->cur_size) {
13981399
wasm_set_exception(module, "out of bounds table access");
13991400
goto got_exception;
14001401
}
14011402

1402-
((uint32 *)(tbl_inst->base_addr))[elem_idx] = val;
1403+
((uint32 *)(tbl_inst->base_addr))[elem_idx] = elem_val;
14031404
HANDLE_OP_END();
14041405
}
14051406

@@ -1414,9 +1415,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
14141415

14151416
HANDLE_OP(WASM_OP_REF_IS_NULL)
14161417
{
1417-
uint32 val;
1418-
val = POP_I32();
1419-
PUSH_I32(val == NULL_REF ? 1 : 0);
1418+
uint32 ref_val;
1419+
ref_val = POP_I32();
1420+
PUSH_I32(ref_val == NULL_REF ? 1 : 0);
14201421
HANDLE_OP_END();
14211422
}
14221423

@@ -2955,16 +2956,16 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
29552956
case WASM_OP_MEMORY_FILL:
29562957
{
29572958
uint32 dst, len;
2958-
uint8 val, *mdst;
2959+
uint8 fill_val, *mdst;
29592960
frame_ip++;
29602961

29612962
len = POP_I32();
2962-
val = POP_I32();
2963+
fill_val = POP_I32();
29632964
dst = POP_I32();
29642965

29652966
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
29662967

2967-
memset(mdst, val, len);
2968+
memset(mdst, fill_val, len);
29682969

29692970
break;
29702971
}
@@ -3119,7 +3120,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
31193120
}
31203121
case WASM_OP_TABLE_FILL:
31213122
{
3122-
uint32 tbl_idx, n, val, i;
3123+
uint32 tbl_idx, n, fill_val;
31233124
WASMTableInstance *tbl_inst;
31243125

31253126
read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
@@ -3128,7 +3129,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
31283129
tbl_inst = wasm_get_table_inst(module, tbl_idx);
31293130

31303131
n = POP_I32();
3131-
val = POP_I32();
3132+
fill_val = POP_I32();
31323133
i = POP_I32();
31333134

31343135
/* TODO: what if the element is not passive? */
@@ -3142,7 +3143,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
31423143
}
31433144

31443145
for (; n != 0; i++, n--) {
3145-
((uint32 *)(tbl_inst->base_addr))[i] = val;
3146+
((uint32 *)(tbl_inst->base_addr))[i] = fill_val;
31463147
}
31473148

31483149
break;
@@ -3167,15 +3168,16 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
31673168
switch (opcode) {
31683169
case WASM_OP_ATOMIC_NOTIFY:
31693170
{
3170-
uint32 count, ret;
3171+
uint32 notify_count, ret;
31713172

3172-
count = POP_I32();
3173+
notify_count = POP_I32();
31733174
addr = POP_I32();
31743175
CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
31753176
CHECK_ATOMIC_MEMORY_ACCESS();
31763177

31773178
ret = wasm_runtime_atomic_notify(
3178-
(WASMModuleInstanceCommon *)module, maddr, count);
3179+
(WASMModuleInstanceCommon *)module, maddr,
3180+
notify_count);
31793181
bh_assert((int32)ret >= 0);
31803182

31813183
PUSH_I32(ret);
@@ -3184,7 +3186,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
31843186
case WASM_OP_ATOMIC_WAIT32:
31853187
{
31863188
uint64 timeout;
3187-
uint32 expect, addr, ret;
3189+
uint32 expect, ret;
31883190

31893191
timeout = POP_I64();
31903192
expect = POP_I32();
@@ -3708,13 +3710,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
37083710
frame here. */
37093711
unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
37103712

3711-
if (argc != function->param_cell_num) {
3713+
if (argc < function->param_cell_num) {
37123714
char buf[128];
3713-
snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d",
3714-
argc, function->param_cell_num);
3715+
snprintf(buf, sizeof(buf),
3716+
"invalid argument count %u, must be no smaller than %u", argc,
3717+
function->param_cell_num);
37153718
wasm_set_exception(module_inst, buf);
37163719
return;
37173720
}
3721+
argc = function->param_cell_num;
37183722

37193723
if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
37203724
wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,

0 commit comments

Comments
 (0)