Refactor APIs and data structures as preliminary work for Memory64 (#3209)
# Change the data type representing linear memory address from u32 to u64
## APIs signature changes
- (Export)wasm_runtime_module_malloc
- wasm_module_malloc
- wasm_module_malloc_internal
- aot_module_malloc
- aot_module_malloc_internal
- wasm_runtime_module_realloc
- wasm_module_realloc
- wasm_module_realloc_internal
- aot_module_realloc
- aot_module_realloc_internal
- (Export)wasm_runtime_module_free
- wasm_module_free
- wasm_module_free_internal
- aot_module_malloc
- aot_module_free_internal
- (Export)wasm_runtime_module_dup_data
- wasm_module_dup_data
- aot_module_dup_data
- (Export)wasm_runtime_validate_app_addr
- (Export)wasm_runtime_validate_app_str_addr
- (Export)wasm_runtime_validate_native_addr
- (Export)wasm_runtime_addr_app_to_native
- (Export)wasm_runtime_addr_native_to_app
- (Export)wasm_runtime_get_app_addr_range
- aot_set_aux_stack
- aot_get_aux_stack
- wasm_set_aux_stack
- wasm_get_aux_stack
- aot_check_app_addr_and_convert, wasm_check_app_addr_and_convert
and jit_check_app_addr_and_convert
- wasm_exec_env_set_aux_stack
- wasm_exec_env_get_aux_stack
- wasm_cluster_create_thread
- wasm_cluster_allocate_aux_stack
- wasm_cluster_free_aux_stack
## Data structure changes
- WASMModule and AOTModule
- field aux_data_end, aux_heap_base and aux_stack_bottom
- WASMExecEnv
- field aux_stack_boundary and aux_stack_bottom
- AOTCompData
- field aux_data_end, aux_heap_base and aux_stack_bottom
- WASMMemoryInstance(AOTMemoryInstance)
- field memory_data_size and change __padding to is_memory64
- WASMModuleInstMemConsumption
- field total_size and memories_size
- WASMDebugExecutionMemory
- field start_offset and current_pos
- WASMCluster
- field stack_tops
## Components that are affected by the APIs and data structure changes
- libc-builtin
- libc-emcc
- libc-uvwasi
- libc-wasi
- Python and Go Language Embedding
- Interpreter Debug engine
- Multi-thread: lib-pthread, wasi-threads and thread manager
This commit is contained in:
@ -39,16 +39,15 @@ typedef float64 CellType_F64;
|
||||
|
||||
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|
||||
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
|
||||
#define CHECK_MEMORY_OVERFLOW(bytes) \
|
||||
do { \
|
||||
uint64 offset1 = (uint64)offset + (uint64)addr; \
|
||||
if (disable_bounds_checks \
|
||||
|| offset1 + bytes <= (uint64)get_linear_mem_size()) \
|
||||
/* If offset1 is in valid range, maddr must also \
|
||||
be in valid range, no need to check it again. */ \
|
||||
maddr = memory->memory_data + offset1; \
|
||||
else \
|
||||
goto out_of_bounds; \
|
||||
#define CHECK_MEMORY_OVERFLOW(bytes) \
|
||||
do { \
|
||||
uint64 offset1 = (uint64)offset + (uint64)addr; \
|
||||
if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
|
||||
/* If offset1 is in valid range, maddr must also \
|
||||
be in valid range, no need to check it again. */ \
|
||||
maddr = memory->memory_data + offset1; \
|
||||
else \
|
||||
goto out_of_bounds; \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
|
||||
@ -1274,8 +1273,8 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
|
||||
uint8 *ip = prev_frame->ip;
|
||||
char buf[128];
|
||||
WASMExecEnv *sub_module_exec_env = NULL;
|
||||
uint32 aux_stack_origin_boundary = 0;
|
||||
uint32 aux_stack_origin_bottom = 0;
|
||||
uintptr_t aux_stack_origin_boundary = 0;
|
||||
uintptr_t aux_stack_origin_bottom = 0;
|
||||
|
||||
if (!sub_func_inst) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
@ -1298,13 +1297,11 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
|
||||
wasm_exec_env_set_module_inst(exec_env,
|
||||
(WASMModuleInstanceCommon *)sub_module_inst);
|
||||
/* - aux_stack_boundary */
|
||||
aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
|
||||
exec_env->aux_stack_boundary.boundary =
|
||||
sub_module_exec_env->aux_stack_boundary.boundary;
|
||||
aux_stack_origin_boundary = exec_env->aux_stack_boundary;
|
||||
exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
|
||||
/* - aux_stack_bottom */
|
||||
aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
|
||||
exec_env->aux_stack_bottom.bottom =
|
||||
sub_module_exec_env->aux_stack_bottom.bottom;
|
||||
aux_stack_origin_bottom = exec_env->aux_stack_bottom;
|
||||
exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
|
||||
|
||||
/* set ip NULL to make call_func_bytecode return after executing
|
||||
this function */
|
||||
@ -1316,8 +1313,8 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
|
||||
|
||||
/* restore ip and other replaced */
|
||||
prev_frame->ip = ip;
|
||||
exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
|
||||
exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
|
||||
exec_env->aux_stack_boundary = aux_stack_origin_boundary;
|
||||
exec_env->aux_stack_bottom = aux_stack_origin_bottom;
|
||||
wasm_exec_env_restore_module_inst(exec_env,
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
}
|
||||
@ -1444,7 +1441,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|
||||
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
|
||||
|| WASM_ENABLE_BULK_MEMORY != 0
|
||||
uint32 linear_mem_size = 0;
|
||||
uint64 linear_mem_size = 0;
|
||||
if (memory)
|
||||
#if WASM_ENABLE_THREAD_MGR == 0
|
||||
linear_mem_size = memory->memory_data_size;
|
||||
@ -3527,27 +3524,29 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
|
||||
HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
|
||||
{
|
||||
uint32 aux_stack_top;
|
||||
uint64 aux_stack_top;
|
||||
|
||||
global_idx = read_uint32(frame_ip);
|
||||
bh_assert(global_idx < module->e->global_count);
|
||||
global = globals + global_idx;
|
||||
global_addr = get_global_addr(global_data, global);
|
||||
aux_stack_top = frame_lp[GET_OFFSET()];
|
||||
if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
|
||||
/* TODO: Memory64 the data type depends on mem idx type */
|
||||
aux_stack_top = (uint64)frame_lp[GET_OFFSET()];
|
||||
if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
|
||||
wasm_set_exception(module, "wasm auxiliary stack overflow");
|
||||
goto got_exception;
|
||||
}
|
||||
if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
|
||||
if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
|
||||
wasm_set_exception(module,
|
||||
"wasm auxiliary stack underflow");
|
||||
goto got_exception;
|
||||
}
|
||||
*(int32 *)global_addr = aux_stack_top;
|
||||
*(int32 *)global_addr = (uint32)aux_stack_top;
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
if (module->module->aux_stack_top_global_index != (uint32)-1) {
|
||||
uint32 aux_stack_used = module->module->aux_stack_bottom
|
||||
- *(uint32 *)global_addr;
|
||||
uint32 aux_stack_used =
|
||||
(uint32)(module->module->aux_stack_bottom
|
||||
- *(uint32 *)global_addr);
|
||||
if (aux_stack_used > module->e->max_aux_stack_used)
|
||||
module->e->max_aux_stack_used = aux_stack_used;
|
||||
}
|
||||
@ -4968,8 +4967,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
#ifndef OS_ENABLE_HW_BOUND_CHECK
|
||||
CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
|
||||
#else
|
||||
if ((uint64)(uint32)addr + bytes
|
||||
> (uint64)linear_mem_size)
|
||||
if ((uint64)(uint32)addr + bytes > linear_mem_size)
|
||||
goto out_of_bounds;
|
||||
maddr = memory->memory_data + (uint32)addr;
|
||||
#endif
|
||||
@ -4987,7 +4985,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
if (offset + bytes > seg_len)
|
||||
goto out_of_bounds;
|
||||
|
||||
bh_memcpy_s(maddr, linear_mem_size - addr,
|
||||
bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
|
||||
data + offset, (uint32)bytes);
|
||||
break;
|
||||
}
|
||||
@ -5017,17 +5015,18 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
|
||||
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
|
||||
#else
|
||||
if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
|
||||
if ((uint64)(uint32)src + len > linear_mem_size)
|
||||
goto out_of_bounds;
|
||||
msrc = memory->memory_data + (uint32)src;
|
||||
|
||||
if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
|
||||
if ((uint64)(uint32)dst + len > linear_mem_size)
|
||||
goto out_of_bounds;
|
||||
mdst = memory->memory_data + (uint32)dst;
|
||||
#endif
|
||||
|
||||
/* allowing the destination and source to overlap */
|
||||
bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
|
||||
bh_memmove_s(mdst, (uint32)(linear_mem_size - dst),
|
||||
msrc, len);
|
||||
break;
|
||||
}
|
||||
case WASM_OP_MEMORY_FILL:
|
||||
@ -5046,7 +5045,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
#ifndef OS_ENABLE_HW_BOUND_CHECK
|
||||
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
|
||||
#else
|
||||
if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
|
||||
if ((uint64)(uint32)dst + len > linear_mem_size)
|
||||
goto out_of_bounds;
|
||||
mdst = memory->memory_data + (uint32)dst;
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user