Fix fast-jit accessing shared memory's fields issue (#2841)

For shared memory, runtime should get the memories pointer from
module_inst first, then get memory instance from memories array,
and then get the fields of the memory instance.
This commit is contained in:
TianlongLiang
2023-12-01 17:41:24 +08:00
committed by GitHub
parent 162a977006
commit 3d0342fbc8
4 changed files with 358 additions and 92 deletions

View File

@ -137,6 +137,7 @@ check_and_seek(JitCompContext *cc, JitReg addr, uint32 offset, uint32 bytes)
{
JitReg memory_boundary = 0, offset1;
#ifndef OS_ENABLE_HW_BOUND_CHECK
JitReg cur_page_count;
/* the default memory */
uint32 mem_idx = 0;
#endif
@ -146,16 +147,10 @@ check_and_seek(JitCompContext *cc, JitReg addr, uint32 offset, uint32 bytes)
/* 1. shortcut if the memory size is 0 */
if (cc->cur_wasm_module->memories != NULL
&& 0 == cc->cur_wasm_module->memories[mem_idx].init_page_count) {
JitReg module_inst, cur_page_count;
uint32 cur_page_count_offset =
(uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
+ (uint32)offsetof(WASMMemoryInstance, cur_page_count);
cur_page_count = get_cur_page_count_reg(cc->jit_frame, mem_idx);
/* if (cur_mem_page_count == 0) goto EXCEPTION */
module_inst = get_module_inst_reg(cc->jit_frame);
cur_page_count = jit_cc_new_reg_I32(cc);
GEN_INSN(LDI32, cur_page_count, module_inst,
NEW_CONST(I32, cur_page_count_offset));
GEN_INSN(CMP, cc->cmp_reg, cur_page_count, NEW_CONST(I32, 0));
if (!jit_emit_exception(cc, EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
JIT_OP_BEQ, cc->cmp_reg, NULL)) {
@ -580,15 +575,9 @@ fail:
bool
jit_compile_op_memory_size(JitCompContext *cc, uint32 mem_idx)
{
JitReg module_inst, cur_page_count;
uint32 cur_page_count_offset =
(uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
+ (uint32)offsetof(WASMMemoryInstance, cur_page_count);
JitReg cur_page_count;
module_inst = get_module_inst_reg(cc->jit_frame);
cur_page_count = jit_cc_new_reg_I32(cc);
GEN_INSN(LDI32, cur_page_count, module_inst,
NEW_CONST(I32, cur_page_count_offset));
cur_page_count = get_cur_page_count_reg(cc->jit_frame, mem_idx);
PUSH_I32(cur_page_count);
@ -600,18 +589,11 @@ fail:
bool
jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx)
{
JitReg module_inst, grow_res, res;
JitReg grow_res, res;
JitReg prev_page_count, inc_page_count, args[2];
/* Get current page count */
uint32 cur_page_count_offset =
(uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
+ (uint32)offsetof(WASMMemoryInstance, cur_page_count);
module_inst = get_module_inst_reg(cc->jit_frame);
prev_page_count = jit_cc_new_reg_I32(cc);
GEN_INSN(LDI32, prev_page_count, module_inst,
NEW_CONST(I32, cur_page_count_offset));
/* Get current page count as prev_page_count */
prev_page_count = get_cur_page_count_reg(cc->jit_frame, mem_idx);
/* Call wasm_enlarge_memory */
POP_I32(inc_page_count);