Skip to content

Commit

Permalink
Fix issues found by GC and Fast JIT, refine some codes (bytecodeallia…
Browse files Browse the repository at this point in the history
…nce#1055)

Fix handle OP_TABLE_COPY issue
Fix loader handle OP_BLOCK/IF/LOOP issue if type_index is larger than 256
Fix loader handle OP_GET_GLOBAL, allow to change to GET_GLOBAL_64 for
aot compiler similiar to handling OP_SET_GLOBAL
Refine loader handle OP_GET/SET/TEE_LOCAL, disable changing opcode when
source debugging is enabled, so as no need to record the change of opcode
Refine wasm_interp_interp_frame_size to reduce the wasm operand stack usage

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
  • Loading branch information
wenyongh authored Mar 24, 2022
1 parent b6e5206 commit 7262aeb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 59 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 @@ -2819,8 +2819,8 @@ aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
dst_tbl_inst = aot_get_table_inst(module_inst, dst_tbl_idx);
bh_assert(dst_tbl_inst);

if ((uint64)src_offset + length > dst_tbl_inst->cur_size
|| (uint64)dst_offset + length > src_tbl_inst->cur_size) {
if ((uint64)dst_offset + length > dst_tbl_inst->cur_size
|| (uint64)src_offset + length > src_tbl_inst->cur_size) {
aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS);
return;
}
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/compilation/aot_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
break;

case WASM_OP_GET_GLOBAL:
case WASM_OP_GET_GLOBAL_64:
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
if (!aot_compile_op_get_global(comp_ctx, func_ctx, global_idx))
return false;
Expand Down
10 changes: 8 additions & 2 deletions core/iwasm/interpreter/wasm_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ typedef struct WASMInterpFrame {
static inline unsigned
wasm_interp_interp_frame_size(unsigned all_cell_num)
{
return align_uint((uint32)offsetof(WASMInterpFrame, lp) + all_cell_num * 5,
4);
unsigned frame_size;

#if WASM_ENABLE_FAST_INTERP == 0
frame_size = (uint32)offsetof(WASMInterpFrame, lp) + all_cell_num * 4;
#else
frame_size = (uint32)offsetof(WASMInterpFrame, operand) + all_cell_num * 4;
#endif
return align_uint(frame_size, 4);
}

void
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3067,8 +3067,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
s = (uint32)POP_I32();
d = (uint32)POP_I32();

if (s + n > dst_tbl_inst->cur_size
|| d + n > src_tbl_inst->cur_size) {
if (d + n > dst_tbl_inst->cur_size
|| s + n > src_tbl_inst->cur_size) {
wasm_set_exception(module,
"out of bounds table access");
goto got_exception;
Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2984,8 +2984,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
s = (uint32)POP_I32();
d = (uint32)POP_I32();

if (s + n > dst_tbl_inst->cur_size
|| d + n > src_tbl_inst->cur_size) {
if (d + n > dst_tbl_inst->cur_size
|| s + n > src_tbl_inst->cur_size) {
wasm_set_exception(module,
"out of bounds table access");
goto got_exception;
Expand Down
58 changes: 12 additions & 46 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -6414,6 +6414,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
uint8 value_type;
BlockType block_type;

p_org = p - 1;
value_type = read_uint8(p);
if (is_byte_a_type(value_type)) {
/* If the first byte is one of these special values:
Expand Down Expand Up @@ -6441,9 +6442,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
* the block quickly.
*/
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p - 2, *(p - 2));
record_fast_op(module, p_org, *p_org);
#endif
*(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
*p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
#endif
}

Expand Down Expand Up @@ -7309,33 +7310,22 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
operand_offset = local_offset;
PUSH_OFFSET_TYPE(local_type);
#else
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
&& (WASM_ENABLE_DEBUG_INTERP == 0)
if (local_offset < 0x80) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = EXT_OP_GET_LOCAL_FAST;
if (is_32bit_type(local_type)) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)local_offset;
}
else {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)(local_offset | 0x80);
}
while (p_org < p) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = WASM_OP_NOP;
}
}
#endif
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
break;
}

Expand Down Expand Up @@ -7384,33 +7374,22 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
POP_OFFSET_TYPE(local_type);
}
#else
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
&& (WASM_ENABLE_DEBUG_INTERP == 0)
if (local_offset < 0x80) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = EXT_OP_SET_LOCAL_FAST;
if (is_32bit_type(local_type)) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)local_offset;
}
else {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)(local_offset | 0x80);
}
while (p_org < p) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = WASM_OP_NOP;
}
}
#endif
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
break;
}

Expand Down Expand Up @@ -7455,33 +7434,22 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
*(loader_ctx->frame_offset
- wasm_value_type_cell_num(local_type)));
#else
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
&& (WASM_ENABLE_DEBUG_INTERP == 0)
if (local_offset < 0x80) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = EXT_OP_TEE_LOCAL_FAST;
if (is_32bit_type(local_type)) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)local_offset;
}
else {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = (uint8)(local_offset | 0x80);
}
while (p_org < p) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org++ = WASM_OP_NOP;
}
}
#endif
#endif
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
break;
}

Expand All @@ -7505,15 +7473,13 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
PUSH_TYPE(global_type);

#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
#if WASM_ENABLE_DEBUG_INTERP != 0
record_fast_op(module, p_org, *p_org);
#endif
*p_org = WASM_OP_GET_GLOBAL_64;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
Expand Down
7 changes: 2 additions & 5 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -4813,6 +4813,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
uint8 value_type;
BlockType block_type;

p_org = p - 1;
value_type = read_uint8(p);
if (is_byte_a_type(value_type)) {
/* If the first byte is one of these special values:
Expand All @@ -4835,7 +4836,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
* to new extended opcode so that interpreter can resolve
* the block quickly.
*/
*(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
*p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
#endif
}

Expand Down Expand Up @@ -5744,12 +5745,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
PUSH_TYPE(global_type);

#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (global_type == VALUE_TYPE_I64
|| global_type == VALUE_TYPE_F64) {
*p_org = WASM_OP_GET_GLOBAL_64;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (is_64bit_type(global_type)) {
skip_label();
Expand Down Expand Up @@ -5789,15 +5788,13 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
POP_TYPE(global_type);

#if WASM_ENABLE_FAST_INTERP == 0
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
if (is_64bit_type(global_type)) {
*p_org = WASM_OP_SET_GLOBAL_64;
}
else if (module->aux_stack_size > 0
&& global_idx == module->aux_stack_top_global_index) {
*p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
}
#endif
#else /* else of WASM_ENABLE_FAST_INTERP */
if (is_64bit_type(global_type)) {
skip_label();
Expand Down

0 comments on commit 7262aeb

Please sign in to comment.