From 87b259a40a324c410e1b82c2f944eae3e22c27a1 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Wed, 27 Apr 2022 20:02:38 +0800 Subject: [PATCH] Implement opcode memory.grow and fix zydis compile error (#1123) --- .../fast-jit/cg/x86-64/jit_codegen_x86_64.cpp | 4 +- core/iwasm/fast-jit/fe/jit_emit_memory.c | 43 ++++++++++++++++++- core/iwasm/fast-jit/fe/jit_emit_memory.h | 2 +- core/iwasm/fast-jit/fe/jit_emit_variable.c | 2 +- core/iwasm/fast-jit/iwasm_fast_jit.cmake | 1 + core/iwasm/fast-jit/jit_frontend.c | 2 +- 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp b/core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp index 44f14ff8d..89b12da00 100644 --- a/core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp +++ b/core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp @@ -4435,11 +4435,11 @@ lower_callnative(JitCompContext *cc, x86::Assembler &a, bh_list *jmp_info_list, case JIT_REG_KIND_I32: if (jit_reg_is_const(arg_reg)) { i32 = jit_cc_get_const_I32(cc, arg_reg); - imm.setValue(i32); + imm.setValue((int64)i32); a.mov(regs_arg[i], imm); } else { - a.mov(regs_arg[i], regs_i32[jit_reg_no(arg_reg)]); + a.movsxd(regs_arg[i], regs_i32[jit_reg_no(arg_reg)]); } break; case JIT_REG_KIND_I64: diff --git a/core/iwasm/fast-jit/fe/jit_emit_memory.c b/core/iwasm/fast-jit/fe/jit_emit_memory.c index a15caa9bb..2387649c4 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_memory.c +++ b/core/iwasm/fast-jit/fe/jit_emit_memory.c @@ -4,8 +4,10 @@ */ #include "jit_emit_memory.h" +#include "jit_emit_exception.h" #include "../jit_frontend.h" -#include "fe/jit_emit_exception.h" +#include "../jit_codegen.h" +#include "../../interpreter/wasm_runtime.h" static JitReg get_memory_boundary(JitCompContext *cc, uint32 mem_idx, uint32 bytes) @@ -409,8 +411,45 @@ jit_compile_op_memory_size(JitCompContext *cc) } bool -jit_compile_op_memory_grow(JitCompContext *cc) +jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx) { + JitReg delta, module_inst, grow_result, res, memory_inst, prev_page_count; + JitInsn *insn; + + /* WASMMemoryInstance->cur_page_count before enlarging */ + memory_inst = get_memory_inst_reg(cc->jit_frame, mem_idx); + prev_page_count = jit_cc_new_reg_I32(cc); + GEN_INSN(LDI32, prev_page_count, memory_inst, + NEW_CONST(I32, offsetof(WASMMemoryInstance, cur_page_count))); + + /* call wasm_enlarge_memory */ +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) + /* Set native_ret to x86::eax */ + grow_result = jit_codegen_get_hreg_by_name("eax"); +#else + grow_result = jit_cc_new_reg_I32(cc); +#endif + POP_I32(delta); + module_inst = get_module_inst_reg(cc->jit_frame); + insn = GEN_INSN(CALLNATIVE, grow_result, + NEW_CONST(PTR, (uintptr_t)wasm_enlarge_memory), 2); + if (insn) { + *(jit_insn_opndv(insn, 2)) = module_inst; + *(jit_insn_opndv(insn, 3)) = delta; + } + + /* check if enlarge memory success */ + res = jit_cc_new_reg_I32(cc); + GEN_INSN(CMP, cc->cmp_reg, grow_result, NEW_CONST(I32, 0)); + GEN_INSN(SELECTNE, res, cc->cmp_reg, prev_page_count, + NEW_CONST(I32, (int32)-1)); + PUSH_I32(res); + + /* ensure a refresh in next get_memory_XXX_reg */ + clear_memory_regs(cc->jit_frame); + + return true; +fail: return false; } diff --git a/core/iwasm/fast-jit/fe/jit_emit_memory.h b/core/iwasm/fast-jit/fe/jit_emit_memory.h index 442e08670..4f1d04cda 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_memory.h +++ b/core/iwasm/fast-jit/fe/jit_emit_memory.h @@ -47,7 +47,7 @@ bool jit_compile_op_memory_size(JitCompContext *cc); bool -jit_compile_op_memory_grow(JitCompContext *cc); +jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx); #if WASM_ENABLE_BULK_MEMORY != 0 bool diff --git a/core/iwasm/fast-jit/fe/jit_emit_variable.c b/core/iwasm/fast-jit/fe/jit_emit_variable.c index 0ef0b9419..5439beb60 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_variable.c +++ b/core/iwasm/fast-jit/fe/jit_emit_variable.c @@ -153,7 +153,7 @@ jit_compile_op_tee_local(JitCompContext *cc, uint32 local_idx) break; default: bh_assert(0); - break; + goto fail; } return true; diff --git a/core/iwasm/fast-jit/iwasm_fast_jit.cmake b/core/iwasm/fast-jit/iwasm_fast_jit.cmake index b7a6b2711..f02f7ea0b 100644 --- a/core/iwasm/fast-jit/iwasm_fast_jit.cmake +++ b/core/iwasm/fast-jit/iwasm_fast_jit.cmake @@ -58,6 +58,7 @@ if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") FetchContent_Declare( zydis GIT_REPOSITORY https://github.com/zyantific/zydis.git + GIT_TAG e14a07895136182a5b53e181eec3b1c6e0b434de ) FetchContent_GetProperties(zydis) if (NOT zydis_POPULATED) diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 8fe59b645..0a7c38b5d 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -1398,7 +1398,7 @@ jit_compile_func(JitCompContext *cc) case WASM_OP_MEMORY_GROW: read_leb_uint32(frame_ip, frame_ip_end, mem_idx); - if (!jit_compile_op_memory_grow(cc)) + if (!jit_compile_op_memory_grow(cc, mem_idx)) return false; break;