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:
Wenyong Huang
2024-03-12 11:38:50 +08:00
committed by GitHub
parent b6216a5f8a
commit 0ee5ffce85
46 changed files with 1006 additions and 754 deletions

View File

@ -48,28 +48,26 @@ 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) \
do { \
uint64 offset1 = (uint32)(start); \
if (disable_bounds_checks \
|| offset1 + bytes <= (uint64)get_linear_mem_size()) \
/* App heap space is not valid space for \
bulk memory operation */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
do { \
uint64 offset1 = (uint32)(start); \
if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
/* App heap space is not valid space for \
bulk memory operation */ \
maddr = memory->memory_data + offset1; \
else \
goto out_of_bounds; \
} while (0)
#else
#define CHECK_MEMORY_OVERFLOW(bytes) \
@ -1211,8 +1209,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),
@ -1235,13 +1233,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 */
@ -1253,8 +1249,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);
}
@ -1374,7 +1370,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;
@ -4086,18 +4082,19 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
{
uint32 aux_stack_top;
uint64 aux_stack_top;
read_leb_uint32(frame_ip, frame_ip_end, global_idx);
bh_assert(global_idx < module->e->global_count);
global = globals + global_idx;
global_addr = get_global_addr(global_data, global);
aux_stack_top = *(uint32 *)(frame_sp - 1);
if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
/* TODO: Memory64 the data type depends on mem idx type */
aux_stack_top = (uint64)(*(uint32 *)(frame_sp - 1));
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;
@ -4106,8 +4103,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
frame_sp--;
#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;
}
@ -5491,7 +5489,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
#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:
@ -5511,7 +5510,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