Skip to content

Commit 0ee5ffc

Browse files
authoredMar 12, 2024
Refactor APIs and data structures as preliminary work for Memory64 (bytecodealliance#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
1 parent b6216a5 commit 0ee5ffc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1007
-755
lines changed
 

‎core/iwasm/aot/aot_loader.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -2423,13 +2423,22 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end,
24232423
}
24242424

24252425
read_uint32(p, p_end, module->aux_data_end_global_index);
2426-
read_uint32(p, p_end, module->aux_data_end);
2426+
read_uint64(p, p_end, module->aux_data_end);
24272427
read_uint32(p, p_end, module->aux_heap_base_global_index);
2428-
read_uint32(p, p_end, module->aux_heap_base);
2428+
read_uint64(p, p_end, module->aux_heap_base);
24292429
read_uint32(p, p_end, module->aux_stack_top_global_index);
2430-
read_uint32(p, p_end, module->aux_stack_bottom);
2430+
read_uint64(p, p_end, module->aux_stack_bottom);
24312431
read_uint32(p, p_end, module->aux_stack_size);
24322432

2433+
if (module->aux_data_end >= MAX_LINEAR_MEMORY_SIZE
2434+
|| module->aux_heap_base >= MAX_LINEAR_MEMORY_SIZE
2435+
|| module->aux_stack_bottom >= MAX_LINEAR_MEMORY_SIZE) {
2436+
set_error_buf(
2437+
error_buf, error_buf_size,
2438+
"invalid range of aux_date_end/aux_heap_base/aux_stack_bottom");
2439+
return false;
2440+
}
2441+
24332442
if (!load_object_data_sections_info(&p, p_end, module,
24342443
is_load_from_file_buf, error_buf,
24352444
error_buf_size))

‎core/iwasm/aot/aot_runtime.c

+73-55
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bh_static_assert(offsetof(AOTModuleInstance, cur_exception)
5050
bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
5151
== 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64));
5252

53-
bh_static_assert(sizeof(AOTMemoryInstance) == 104);
53+
bh_static_assert(sizeof(AOTMemoryInstance) == 112);
5454
bh_static_assert(offsetof(AOTTableInstance, elems) == 24);
5555

5656
bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
@@ -792,9 +792,10 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
792792
uint32 max_page_count =
793793
wasm_runtime_get_max_mem(max_memory_pages, memory->mem_init_page_count,
794794
memory->mem_max_page_count);
795-
uint32 inc_page_count, aux_heap_base, global_idx;
795+
uint32 inc_page_count, global_idx;
796796
uint32 bytes_of_last_page, bytes_to_page_end;
797-
uint32 heap_offset = num_bytes_per_page * init_page_count;
797+
uint64 aux_heap_base,
798+
heap_offset = (uint64)num_bytes_per_page * init_page_count;
798799
uint64 memory_data_size, max_memory_data_size;
799800
uint8 *p = NULL, *global_addr;
800801

@@ -819,6 +820,15 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
819820
heap_size = 0;
820821
}
821822

823+
/* If initial memory is the largest size allowed, disallowing insert host
824+
* managed heap */
825+
if (heap_size > 0 && heap_offset == MAX_LINEAR_MEMORY_SIZE) {
826+
set_error_buf(error_buf, error_buf_size,
827+
"failed to insert app heap into linear memory, "
828+
"try using `--heap-size=0` option");
829+
return NULL;
830+
}
831+
822832
if (init_page_count == max_page_count && init_page_count == 1) {
823833
/* If only one page and at most one page, we just append
824834
the app heap to the end of linear memory, enlarge the
@@ -842,7 +852,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
842852
}
843853
else if (module->aux_heap_base_global_index != (uint32)-1
844854
&& module->aux_heap_base
845-
< num_bytes_per_page * init_page_count) {
855+
< (uint64)num_bytes_per_page * init_page_count) {
846856
/* Insert app heap before __heap_base */
847857
aux_heap_base = module->aux_heap_base;
848858
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
@@ -869,15 +879,15 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
869879
- module->import_global_count;
870880
global_addr = module_inst->global_data
871881
+ module->globals[global_idx].data_offset;
872-
*(uint32 *)global_addr = aux_heap_base;
882+
*(uint32 *)global_addr = (uint32)aux_heap_base;
873883
LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
874884
}
875885
else {
876886
/* Insert app heap before new page */
877887
inc_page_count =
878888
(heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
879-
heap_offset = num_bytes_per_page * init_page_count;
880-
heap_size = num_bytes_per_page * inc_page_count;
889+
heap_offset = (uint64)num_bytes_per_page * init_page_count;
890+
heap_size = (uint64)num_bytes_per_page * inc_page_count;
881891
if (heap_size > 0)
882892
heap_size -= 1 * BH_KB;
883893
}
@@ -889,19 +899,9 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
889899
"try using `--heap-size=0` option");
890900
return NULL;
891901
}
892-
else if (init_page_count == DEFAULT_MAX_PAGES) {
893-
num_bytes_per_page = UINT32_MAX;
894-
init_page_count = max_page_count = 1;
895-
}
896902
if (max_page_count > DEFAULT_MAX_PAGES)
897903
max_page_count = DEFAULT_MAX_PAGES;
898904
}
899-
else { /* heap_size == 0 */
900-
if (init_page_count == DEFAULT_MAX_PAGES) {
901-
num_bytes_per_page = UINT32_MAX;
902-
init_page_count = max_page_count = 1;
903-
}
904-
}
905905

906906
LOG_VERBOSE("Memory instantiate:");
907907
LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
@@ -911,7 +911,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
911911
LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
912912

913913
max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
914-
bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
914+
bh_assert(max_memory_data_size <= MAX_LINEAR_MEMORY_SIZE);
915915
(void)max_memory_data_size;
916916

917917
if (wasm_allocate_linear_memory(&p, is_shared_memory, num_bytes_per_page,
@@ -927,11 +927,11 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
927927
memory_inst->num_bytes_per_page = num_bytes_per_page;
928928
memory_inst->cur_page_count = init_page_count;
929929
memory_inst->max_page_count = max_page_count;
930-
memory_inst->memory_data_size = (uint32)memory_data_size;
930+
memory_inst->memory_data_size = memory_data_size;
931931

932932
/* Init memory info */
933933
memory_inst->memory_data = p;
934-
memory_inst->memory_data_end = p + (uint32)memory_data_size;
934+
memory_inst->memory_data_end = p + memory_data_size;
935935

936936
/* Initialize heap info */
937937
memory_inst->heap_data = p + heap_offset;
@@ -1098,7 +1098,7 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
10981098

10991099
if (memory_inst->memory_data) {
11001100
bh_memcpy_s((uint8 *)memory_inst->memory_data + base_offset,
1101-
memory_inst->memory_data_size - base_offset,
1101+
(uint32)memory_inst->memory_data_size - base_offset,
11021102
data_seg->bytes, length);
11031103
}
11041104
}
@@ -2472,23 +2472,26 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
24722472
return ret;
24732473
}
24742474

2475-
uint32
2475+
uint64
24762476
aot_module_malloc_internal(AOTModuleInstance *module_inst,
2477-
WASMExecEnv *exec_env, uint32 size,
2477+
WASMExecEnv *exec_env, uint64 size,
24782478
void **p_native_addr)
24792479
{
24802480
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
24812481
AOTModule *module = (AOTModule *)module_inst->module;
24822482
uint8 *addr = NULL;
24832483
uint32 offset = 0;
24842484

2485+
/* TODO: Memory64 size check based on memory idx type */
2486+
bh_assert(size <= UINT32_MAX);
2487+
24852488
if (!memory_inst) {
24862489
aot_set_exception(module_inst, "uninitialized memory");
24872490
return 0;
24882491
}
24892492

24902493
if (memory_inst->heap_handle) {
2491-
addr = mem_allocator_malloc(memory_inst->heap_handle, size);
2494+
addr = mem_allocator_malloc(memory_inst->heap_handle, (uint32)size);
24922495
}
24932496
else if (module->malloc_func_index != (uint32)-1
24942497
&& module->free_func_index != (uint32)-1) {
@@ -2513,7 +2516,7 @@ aot_module_malloc_internal(AOTModuleInstance *module_inst,
25132516

25142517
if (!malloc_func
25152518
|| !execute_malloc_function(module_inst, exec_env, malloc_func,
2516-
retain_func, size, &offset)) {
2519+
retain_func, (uint32)size, &offset)) {
25172520
return 0;
25182521
}
25192522
addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL;
@@ -2532,17 +2535,21 @@ aot_module_malloc_internal(AOTModuleInstance *module_inst,
25322535
}
25332536
if (p_native_addr)
25342537
*p_native_addr = addr;
2535-
return (uint32)(addr - memory_inst->memory_data);
2538+
return (uint64)(addr - memory_inst->memory_data);
25362539
}
25372540

2538-
uint32
2541+
uint64
25392542
aot_module_realloc_internal(AOTModuleInstance *module_inst,
2540-
WASMExecEnv *exec_env, uint32 ptr, uint32 size,
2543+
WASMExecEnv *exec_env, uint64 ptr, uint64 size,
25412544
void **p_native_addr)
25422545
{
25432546
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
25442547
uint8 *addr = NULL;
25452548

2549+
/* TODO: Memory64 ptr and size check based on memory idx type */
2550+
bh_assert(ptr <= UINT32_MAX);
2551+
bh_assert(size <= UINT32_MAX);
2552+
25462553
if (!memory_inst) {
25472554
aot_set_exception(module_inst, "uninitialized memory");
25482555
return 0;
@@ -2551,7 +2558,8 @@ aot_module_realloc_internal(AOTModuleInstance *module_inst,
25512558
if (memory_inst->heap_handle) {
25522559
addr = mem_allocator_realloc(
25532560
memory_inst->heap_handle,
2554-
ptr ? memory_inst->memory_data + ptr : NULL, size);
2561+
(uint32)ptr ? memory_inst->memory_data + (uint32)ptr : NULL,
2562+
(uint32)size);
25552563
}
25562564

25572565
/* Only support realloc in WAMR's app heap */
@@ -2570,12 +2578,12 @@ aot_module_realloc_internal(AOTModuleInstance *module_inst,
25702578

25712579
if (p_native_addr)
25722580
*p_native_addr = addr;
2573-
return (uint32)(addr - memory_inst->memory_data);
2581+
return (uint64)(addr - memory_inst->memory_data);
25742582
}
25752583

25762584
void
25772585
aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
2578-
uint32 ptr)
2586+
uint64 ptr)
25792587
{
25802588
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
25812589
AOTModule *module = (AOTModule *)module_inst->module;
@@ -2584,8 +2592,11 @@ aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
25842592
return;
25852593
}
25862594

2595+
/* TODO: Memory64 ptr and size check based on memory idx type */
2596+
bh_assert(ptr <= UINT32_MAX);
2597+
25872598
if (ptr) {
2588-
uint8 *addr = memory_inst->memory_data + ptr;
2599+
uint8 *addr = memory_inst->memory_data + (uint32)ptr;
25892600
uint8 *memory_data_end;
25902601

25912602
/* memory->memory_data_end may be changed in memory grow */
@@ -2616,44 +2627,49 @@ aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
26162627
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
26172628

26182629
if (free_func)
2619-
execute_free_function(module_inst, exec_env, free_func, ptr);
2630+
execute_free_function(module_inst, exec_env, free_func,
2631+
(uint32)ptr);
26202632
}
26212633
}
26222634
}
26232635

2624-
uint32
2625-
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
2636+
uint64
2637+
aot_module_malloc(AOTModuleInstance *module_inst, uint64 size,
26262638
void **p_native_addr)
26272639
{
26282640
return aot_module_malloc_internal(module_inst, NULL, size, p_native_addr);
26292641
}
26302642

2631-
uint32
2632-
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
2643+
uint64
2644+
aot_module_realloc(AOTModuleInstance *module_inst, uint64 ptr, uint64 size,
26332645
void **p_native_addr)
26342646
{
26352647
return aot_module_realloc_internal(module_inst, NULL, ptr, size,
26362648
p_native_addr);
26372649
}
26382650

26392651
void
2640-
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
2652+
aot_module_free(AOTModuleInstance *module_inst, uint64 ptr)
26412653
{
26422654
aot_module_free_internal(module_inst, NULL, ptr);
26432655
}
26442656

2645-
uint32
2657+
uint64
26462658
aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
2647-
uint32 size)
2659+
uint64 size)
26482660
{
26492661
char *buffer;
2650-
uint32 buffer_offset =
2651-
aot_module_malloc(module_inst, size, (void **)&buffer);
2662+
uint64 buffer_offset;
2663+
2664+
/* TODO: Memory64 size check based on memory idx type */
2665+
bh_assert(size <= UINT32_MAX);
2666+
2667+
buffer_offset = aot_module_malloc(module_inst, size, (void **)&buffer);
26522668

26532669
if (buffer_offset != 0) {
26542670
buffer = wasm_runtime_addr_app_to_native(
26552671
(WASMModuleInstanceCommon *)module_inst, buffer_offset);
2656-
bh_memcpy_s(buffer, size, src, size);
2672+
bh_memcpy_s(buffer, (uint32)size, src, (uint32)size);
26572673
}
26582674
return buffer_offset;
26592675
}
@@ -2935,7 +2951,7 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
29352951

29362952
bool
29372953
aot_check_app_addr_and_convert(AOTModuleInstance *module_inst, bool is_str,
2938-
uint32 app_buf_addr, uint32 app_buf_size,
2954+
uint64 app_buf_addr, uint64 app_buf_size,
29392955
void **p_native_addr)
29402956
{
29412957
bool ret;
@@ -2999,7 +3015,7 @@ aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
29993015
}
30003016

30013017
if (!wasm_runtime_validate_app_addr((WASMModuleInstanceCommon *)module_inst,
3002-
dst, len))
3018+
(uint64)dst, (uint64)len))
30033019
return false;
30043020

30053021
if ((uint64)offset + (uint64)len > seg_len) {
@@ -3008,10 +3024,11 @@ aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset,
30083024
}
30093025

30103026
maddr = wasm_runtime_addr_app_to_native(
3011-
(WASMModuleInstanceCommon *)module_inst, dst);
3027+
(WASMModuleInstanceCommon *)module_inst, (uint64)dst);
30123028

30133029
SHARED_MEMORY_LOCK(memory_inst);
3014-
bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len);
3030+
bh_memcpy_s(maddr, (uint32)(memory_inst->memory_data_size - dst),
3031+
data + offset, len);
30153032
SHARED_MEMORY_UNLOCK(memory_inst);
30163033
return true;
30173034
}
@@ -3030,14 +3047,14 @@ aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index)
30303047

30313048
#if WASM_ENABLE_THREAD_MGR != 0
30323049
bool
3033-
aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
3050+
aot_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset, uint32 size)
30343051
{
30353052
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
30363053
AOTModule *module = (AOTModule *)module_inst->module;
30373054

30383055
uint32 stack_top_idx = module->aux_stack_top_global_index;
3039-
uint32 data_end = module->aux_data_end;
3040-
uint32 stack_bottom = module->aux_stack_bottom;
3056+
uint64 data_end = module->aux_data_end;
3057+
uint64 stack_bottom = module->aux_stack_bottom;
30413058
bool is_stack_before_data = stack_bottom < data_end ? true : false;
30423059

30433060
/* Check the aux stack space, currently we don't allocate space in heap */
@@ -3050,27 +3067,28 @@ aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
30503067
set the initial value for the global */
30513068
uint32 global_offset = module->globals[stack_top_idx].data_offset;
30523069
uint8 *global_addr = module_inst->global_data + global_offset;
3053-
*(int32 *)global_addr = start_offset;
3070+
/* TODO: Memory64 the type i32/i64 depends on memory idx type*/
3071+
*(int32 *)global_addr = (uint32)start_offset;
30543072

30553073
/* The aux stack boundary is a constant value,
30563074
set the value to exec_env */
3057-
exec_env->aux_stack_boundary.boundary = start_offset - size;
3058-
exec_env->aux_stack_bottom.bottom = start_offset;
3075+
exec_env->aux_stack_boundary = (uintptr_t)start_offset - size;
3076+
exec_env->aux_stack_bottom = (uintptr_t)start_offset;
30593077
return true;
30603078
}
30613079

30623080
return false;
30633081
}
30643082

30653083
bool
3066-
aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
3084+
aot_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset, uint32 *size)
30673085
{
30683086
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
30693087
AOTModule *module = (AOTModule *)module_inst->module;
30703088

30713089
/* The aux stack information is resolved in loader
30723090
and store in module */
3073-
uint32 stack_bottom = module->aux_stack_bottom;
3091+
uint64 stack_bottom = module->aux_stack_bottom;
30743092
uint32 total_aux_stack_size = module->aux_stack_size;
30753093

30763094
if (stack_bottom != 0 && total_aux_stack_size != 0) {

0 commit comments

Comments
 (0)
Please sign in to comment.