Implement Memory64 support for AOT (#3362)
Refer to: https://github.com/bytecodealliance/wasm-micro-runtime/pull/3266 https://github.com/bytecodealliance/wasm-micro-runtime/issues/3091
This commit is contained in:
@ -500,8 +500,10 @@ typedef struct WASMTable {
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
typedef uint64 mem_offset_t;
|
||||
#define PR_MEM_OFFSET PRIu64
|
||||
#else
|
||||
typedef uint32 mem_offset_t;
|
||||
#define PR_MEM_OFFSET PRIu32
|
||||
#endif
|
||||
|
||||
typedef struct WASMMemory {
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include "wasm.h"
|
||||
#include "wasm_opcode.h"
|
||||
#include "wasm_runtime.h"
|
||||
#include "wasm_loader_common.h"
|
||||
#include "../common/wasm_native.h"
|
||||
#include "../common/wasm_memory.h"
|
||||
#if WASM_ENABLE_GC != 0
|
||||
@ -2755,43 +2756,6 @@ check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
check_memory_flag(const uint8 mem_flag, char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
/* Check whether certain features indicated by mem_flag are enabled in
|
||||
* runtime */
|
||||
if (mem_flag > MAX_PAGE_COUNT_FLAG) {
|
||||
#if WASM_ENABLE_SHARED_MEMORY == 0
|
||||
if (mem_flag & SHARED_MEMORY_FLAG) {
|
||||
LOG_VERBOSE("shared memory flag was found, please enable shared "
|
||||
"memory, lib-pthread or lib-wasi-threads");
|
||||
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_MEMORY64 == 0
|
||||
if (mem_flag & MEMORY64_FLAG) {
|
||||
LOG_VERBOSE("memory64 flag was found, please enable memory64");
|
||||
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
|
||||
set_error_buf(error_buf, error_buf_size, "invalid limits flags");
|
||||
return false;
|
||||
}
|
||||
else if ((mem_flag & SHARED_MEMORY_FLAG)
|
||||
&& !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"shared memory must have maximum");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *parent_module, const char *sub_module_name,
|
||||
@ -2824,7 +2788,7 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!check_memory_flag(mem_flag, error_buf, error_buf_size)) {
|
||||
if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -3226,7 +3190,8 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!check_memory_flag(memory->flags, error_buf, error_buf_size)) {
|
||||
if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
|
||||
false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -14762,9 +14727,9 @@ re_scan:
|
||||
goto fail;
|
||||
}
|
||||
|
||||
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
||||
read_leb_mem_offset(p, p_end, mem_offset); /* offset */
|
||||
|
||||
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
|
||||
POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
|
||||
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
||||
func->has_memory_operations = true;
|
||||
#endif
|
||||
@ -14781,10 +14746,10 @@ re_scan:
|
||||
goto fail;
|
||||
}
|
||||
|
||||
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
||||
read_leb_mem_offset(p, p_end, mem_offset); /* offset */
|
||||
|
||||
POP_V128();
|
||||
POP_I32();
|
||||
POP_MEM_OFFSET();
|
||||
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
||||
func->has_memory_operations = true;
|
||||
#endif
|
||||
@ -14999,7 +14964,7 @@ re_scan:
|
||||
goto fail;
|
||||
}
|
||||
|
||||
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
||||
read_leb_mem_offset(p, p_end, mem_offset); /* offset */
|
||||
|
||||
CHECK_BUF(p, p_end, 1);
|
||||
lane = read_uint8(p);
|
||||
@ -15009,7 +14974,7 @@ re_scan:
|
||||
}
|
||||
|
||||
POP_V128();
|
||||
POP_I32();
|
||||
POP_MEM_OFFSET();
|
||||
if (opcode1 < SIMD_v128_store8_lane) {
|
||||
PUSH_V128();
|
||||
}
|
||||
@ -15030,9 +14995,9 @@ re_scan:
|
||||
goto fail;
|
||||
}
|
||||
|
||||
read_leb_uint32(p, p_end, mem_offset); /* offset */
|
||||
read_leb_mem_offset(p, p_end, mem_offset); /* offset */
|
||||
|
||||
POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
|
||||
POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
|
||||
#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
|
||||
func->has_memory_operations = true;
|
||||
#endif
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#include "wasm_runtime.h"
|
||||
#include "../common/wasm_native.h"
|
||||
#include "../common/wasm_memory.h"
|
||||
#include "wasm_loader_common.h"
|
||||
#if WASM_ENABLE_FAST_JIT != 0
|
||||
#include "../fast-jit/jit_compiler.h"
|
||||
#include "../fast-jit/jit_codecache.h"
|
||||
@ -714,38 +715,6 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
check_memory_flag(const uint8 mem_flag)
|
||||
{
|
||||
/* Check whether certain features indicated by mem_flag are enabled in
|
||||
* runtime */
|
||||
if (mem_flag > MAX_PAGE_COUNT_FLAG) {
|
||||
#if WASM_ENABLE_SHARED_MEMORY == 0
|
||||
if (mem_flag & SHARED_MEMORY_FLAG) {
|
||||
LOG_VERBOSE("shared memory flag was found, please enable shared "
|
||||
"memory, lib-pthread or lib-wasi-threads");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_MEMORY64 == 0
|
||||
if (mem_flag & MEMORY64_FLAG) {
|
||||
LOG_VERBOSE("memory64 flag was found, please enable memory64");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
|
||||
return false;
|
||||
}
|
||||
else if ((mem_flag & SHARED_MEMORY_FLAG)
|
||||
&& !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *parent_module, const char *sub_module_name,
|
||||
@ -766,7 +735,9 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
uint32 declare_max_page_count = 0;
|
||||
|
||||
read_leb_uint32(p, p_end, mem_flag);
|
||||
bh_assert(check_memory_flag(mem_flag));
|
||||
if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_APP_FRAMEWORK == 0
|
||||
is_memory64 = mem_flag & MEMORY64_FLAG;
|
||||
@ -796,7 +767,6 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
|
||||
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
|
||||
|
||||
*p_buf = p;
|
||||
(void)check_memory_flag;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -891,7 +861,10 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
|
||||
read_leb_uint32(p, p_end, memory->flags);
|
||||
bh_assert(p - p_org <= 1);
|
||||
(void)p_org;
|
||||
bh_assert(check_memory_flag(memory->flags));
|
||||
if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
|
||||
false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_APP_FRAMEWORK == 0
|
||||
is_memory64 = memory->flags & MEMORY64_FLAG;
|
||||
@ -916,7 +889,6 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
|
||||
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
|
||||
|
||||
*p_buf = p;
|
||||
(void)check_memory_flag;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -1408,19 +1408,23 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[3], argc;
|
||||
union {
|
||||
uint32 u32[3];
|
||||
uint64 u64;
|
||||
} argv;
|
||||
uint32 argc;
|
||||
bool ret;
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
bool is_memory64 = module_inst->memories[0]->is_memory64;
|
||||
if (is_memory64) {
|
||||
argc = 2;
|
||||
PUT_I64_TO_ADDR(&argv[0], size);
|
||||
PUT_I64_TO_ADDR(&argv.u64, size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
argc = 1;
|
||||
argv[0] = (uint32)size;
|
||||
argv.u32[0] = (uint32)size;
|
||||
}
|
||||
|
||||
/* if __retain is exported, then this module is compiled by
|
||||
@ -1431,7 +1435,7 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
/* the malloc function from assemblyscript is:
|
||||
function __new(size: usize, id: u32)
|
||||
id = 0 means this is an ArrayBuffer object */
|
||||
argv[argc] = 0;
|
||||
argv.u32[argc] = 0;
|
||||
argc++;
|
||||
}
|
||||
|
||||
@ -1472,10 +1476,10 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
}
|
||||
}
|
||||
|
||||
ret = wasm_call_function(exec_env, malloc_func, argc, argv);
|
||||
ret = wasm_call_function(exec_env, malloc_func, argc, argv.u32);
|
||||
|
||||
if (retain_func && ret)
|
||||
ret = wasm_call_function(exec_env, retain_func, 1, argv);
|
||||
ret = wasm_call_function(exec_env, retain_func, 1, argv.u32);
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
@ -1487,11 +1491,11 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
if (ret) {
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (is_memory64)
|
||||
*p_result = GET_I64_FROM_ADDR(&argv[0]);
|
||||
*p_result = GET_I64_FROM_ADDR(&argv.u64);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
*p_result = argv[0];
|
||||
*p_result = argv.u32[0];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@ -1506,18 +1510,22 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2], argc;
|
||||
union {
|
||||
uint32 u32[2];
|
||||
uint64 u64;
|
||||
} argv;
|
||||
uint32 argc;
|
||||
bool ret;
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (module_inst->memories[0]->is_memory64) {
|
||||
PUT_I64_TO_ADDR(&argv[0], offset);
|
||||
PUT_I64_TO_ADDR(&argv.u64, offset);
|
||||
argc = 2;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
argv[0] = (uint32)offset;
|
||||
argv.u32[0] = (uint32)offset;
|
||||
argc = 1;
|
||||
}
|
||||
|
||||
@ -1558,7 +1566,7 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
}
|
||||
}
|
||||
|
||||
ret = wasm_call_function(exec_env, free_func, argc, argv);
|
||||
ret = wasm_call_function(exec_env, free_func, argc, argv.u32);
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
@ -4176,7 +4184,7 @@ fail:
|
||||
#if WASM_ENABLE_BULK_MEMORY != 0
|
||||
bool
|
||||
llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
|
||||
uint32 offset, uint32 len, uint32 dst)
|
||||
uint32 offset, uint32 len, size_t dst)
|
||||
{
|
||||
WASMMemoryInstance *memory_inst;
|
||||
WASMModule *module;
|
||||
@ -4211,7 +4219,7 @@ llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
|
||||
(WASMModuleInstanceCommon *)module_inst, (uint64)dst);
|
||||
|
||||
SHARED_MEMORY_LOCK(memory_inst);
|
||||
bh_memcpy_s(maddr, (uint32)(memory_inst->memory_data_size - dst),
|
||||
bh_memcpy_s(maddr, CLAMP_U64_TO_U32(memory_inst->memory_data_size - dst),
|
||||
data + offset, len);
|
||||
SHARED_MEMORY_UNLOCK(memory_inst);
|
||||
return true;
|
||||
|
||||
@ -760,7 +760,7 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
|
||||
#if WASM_ENABLE_BULK_MEMORY != 0
|
||||
bool
|
||||
llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
|
||||
uint32 offset, uint32 len, uint32 dst);
|
||||
uint32 offset, uint32 len, size_t dst);
|
||||
|
||||
bool
|
||||
llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index);
|
||||
|
||||
Reference in New Issue
Block a user