Skip to content

Commit a23fa9f

Browse files
authored
Implement memory64 for classic interpreter (#3266)
Adding a new cmake flag (cache variable) `WAMR_BUILD_MEMORY64` to enable the memory64 feature, it can only be enabled on the 64-bit platform/target and can only use software boundary check. And when it is enabled, it can support both i32 and i64 linear memory types. The main modifications are: - wasm loader & mini-loader: loading and bytecode validating process - wasm runtime: memory instantiating process - classic-interpreter: wasm code executing process - Support memory64 memory in related runtime APIs - Modify main function type check when it's memory64 wasm file - Modify `wasm_runtime_invoke_native` and `wasm_runtime_invoke_native_raw` to handle registered native function pointer argument when memory64 is enabled - memory64 classic-interpreter spec test in `test_wamr.sh` and in CI Currently, it supports memory64 memory wasm file that uses core spec (including bulk memory proposal) opcodes and threads opcodes. ps. #3091 #3240 #3260
1 parent 6b0b5de commit a23fa9f

File tree

22 files changed

+1083
-341
lines changed

22 files changed

+1083
-341
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ env:
6565
WASI_TEST_OPTIONS: "-s wasi_certification -w"
6666
WAMR_COMPILER_TEST_OPTIONS: "-s wamr_compiler -S -b -P"
6767
GC_TEST_OPTIONS: "-s spec -G -b -P"
68+
MEMORY64_TEST_OPTIONS: "-s spec -W -b -P"
6869

6970
jobs:
7071
build_llvm_libraries_on_ubuntu_2204:
@@ -144,6 +145,7 @@ jobs:
144145
"-DWAMR_BUILD_SIMD=1",
145146
"-DWAMR_BUILD_TAIL_CALL=1",
146147
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
148+
"-DWAMR_BUILD_MEMORY64=1",
147149
]
148150
os: [ubuntu-22.04]
149151
platform: [android, linux]
@@ -202,6 +204,21 @@ jobs:
202204
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
203205
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
204206
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
207+
# Memory64 only on CLASSIC INTERP mode, and only on 64-bit platform
208+
- make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
209+
platform: android
210+
- make_options_run_mode: $AOT_BUILD_OPTIONS
211+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
212+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
213+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
214+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
215+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
216+
- make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
217+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
218+
- make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
219+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
220+
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
221+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
205222
# Fast-JIT and Multi-Tier-JIT mode don't support android
206223
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
207224
platform: android
@@ -503,6 +520,7 @@ jobs:
503520
$THREADS_TEST_OPTIONS,
504521
$WASI_TEST_OPTIONS,
505522
$GC_TEST_OPTIONS,
523+
$MEMORY64_TEST_OPTIONS,
506524
]
507525
wasi_sdk_release:
508526
[
@@ -541,19 +559,30 @@ jobs:
541559
test_option: $GC_TEST_OPTIONS
542560
- running_mode: "multi-tier-jit"
543561
test_option: $GC_TEST_OPTIONS
562+
# aot, fast-interp, fast-jit, llvm-jit, multi-tier-jit don't support Memory64
563+
- running_mode: "aot"
564+
test_option: $MEMORY64_TEST_OPTIONS
565+
- running_mode: "fast-interp"
566+
test_option: $MEMORY64_TEST_OPTIONS
567+
- running_mode: "fast-jit"
568+
test_option: $MEMORY64_TEST_OPTIONS
569+
- running_mode: "jit"
570+
test_option: $MEMORY64_TEST_OPTIONS
571+
- running_mode: "multi-tier-jit"
572+
test_option: $MEMORY64_TEST_OPTIONS
544573
steps:
545574
- name: checkout
546575
uses: actions/checkout@v4
547576

548577
- name: Set-up OCaml
549578
uses: ocaml/setup-ocaml@v2
550-
if: matrix.test_option == '$GC_TEST_OPTIONS'
579+
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
551580
with:
552581
ocaml-compiler: 4.13
553582

554583
- name: Set-up Ocamlbuild
555-
if: matrix.test_option == '$GC_TEST_OPTIONS'
556-
run: opam install ocamlbuild dune
584+
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
585+
run: opam install ocamlbuild dune menhir
557586

558587
- name: download and install wasi-sdk
559588
if: matrix.test_option == '$WASI_TEST_OPTIONS'
@@ -617,13 +646,13 @@ jobs:
617646

618647
- name: run tests
619648
timeout-minutes: 30
620-
if: matrix.test_option != '$GC_TEST_OPTIONS'
649+
if: matrix.test_option != '$GC_TEST_OPTIONS' && matrix.test_option != '$MEMORY64_TEST_OPTIONS'
621650
run: ./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }}
622651
working-directory: ./tests/wamr-test-suites
623652

624-
- name: run gc tests
653+
- name: run gc or memory64 tests
625654
timeout-minutes: 20
626-
if: matrix.test_option == '$GC_TEST_OPTIONS'
655+
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
627656
run: |
628657
eval $(opam env)
629658
./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }}

.github/workflows/nightly_run.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ jobs:
130130
"-DWAMR_BUILD_SIMD=1",
131131
"-DWAMR_BUILD_TAIL_CALL=1",
132132
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
133+
"-DWAMR_BUILD_MEMORY64=1",
133134
]
134135
os: [ubuntu-20.04]
135136
platform: [android, linux]
@@ -188,6 +189,21 @@ jobs:
188189
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
189190
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
190191
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
192+
# Memory64 only on CLASSIC INTERP mode, and only on 64-bit platform
193+
- make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
194+
platform: android
195+
- make_options_run_mode: $AOT_BUILD_OPTIONS
196+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
197+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
198+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
199+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
200+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
201+
- make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
202+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
203+
- make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
204+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
205+
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
206+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
191207
# Fast-JIT and Multi-Tier-JIT mode don't support android
192208
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
193209
platform: android
@@ -271,6 +287,7 @@ jobs:
271287
"-DWAMR_BUILD_SIMD=1",
272288
"-DWAMR_BUILD_TAIL_CALL=1",
273289
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
290+
"-DWAMR_BUILD_MEMORY64=1",
274291
]
275292
exclude:
276293
# uncompatiable feature and platform
@@ -299,6 +316,11 @@ jobs:
299316
# MINI_LOADER only on INTERP mode
300317
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
301318
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
319+
# Memory64 only on CLASSIC INTERP mode
320+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
321+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
322+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
323+
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
302324
steps:
303325
- name: checkout
304326
uses: actions/checkout@v3

build-scripts/config_common.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ if (WAMR_BUILD_SHARED_MEMORY EQUAL 1)
248248
else ()
249249
add_definitions (-DWASM_ENABLE_SHARED_MEMORY=0)
250250
endif ()
251+
if (WAMR_BUILD_MEMORY64 EQUAL 1)
252+
# if native is 32-bit or cross-compiled to 32-bit
253+
if (NOT WAMR_BUILD_TARGET MATCHES ".*64.*")
254+
message (FATAL_ERROR "-- Memory64 is only available on the 64-bit platform/target")
255+
endif()
256+
add_definitions (-DWASM_ENABLE_MEMORY64=1)
257+
set (WAMR_DISABLE_HW_BOUND_CHECK 1)
258+
message (" Memory64 memory enabled")
259+
endif ()
251260
if (WAMR_BUILD_THREAD_MGR EQUAL 1)
252261
message (" Thread manager enabled")
253262
endif ()

core/config.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
#else
416416
#define DEFAULT_WASM_STACK_SIZE (12 * 1024)
417417
#endif
418-
/* Min auxilliary stack size of each wasm thread */
418+
/* Min auxiliary stack size of each wasm thread */
419419
#define WASM_THREAD_AUX_STACK_SIZE_MIN (256)
420420

421421
/* Default/min native stack size of each app thread */
@@ -564,7 +564,7 @@
564564
#endif
565565

566566
/* Support registering quick AOT/JIT function entries of some func types
567-
to speedup the calling process of invoking the AOT/JIT functions of
567+
to speed up the calling process of invoking the AOT/JIT functions of
568568
these types from the host embedder */
569569
#ifndef WASM_ENABLE_QUICK_AOT_ENTRY
570570
#define WASM_ENABLE_QUICK_AOT_ENTRY 1
@@ -578,6 +578,11 @@
578578
#define WASM_ENABLE_AOT_INTRINSICS 1
579579
#endif
580580

581+
/* Disable memory64 by default */
582+
#ifndef WASM_ENABLE_MEMORY64
583+
#define WASM_ENABLE_MEMORY64 0
584+
#endif
585+
581586
#ifndef WASM_TABLE_MAX_SIZE
582587
#define WASM_TABLE_MAX_SIZE 1024
583588
#endif

core/iwasm/aot/aot_runtime.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,10 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
914914
bh_assert(max_memory_data_size <= MAX_LINEAR_MEMORY_SIZE);
915915
(void)max_memory_data_size;
916916

917-
if (wasm_allocate_linear_memory(&p, is_shared_memory, num_bytes_per_page,
918-
init_page_count, max_page_count,
919-
&memory_data_size)
917+
/* TODO: memory64 uses is_memory64 flag */
918+
if (wasm_allocate_linear_memory(&p, is_shared_memory, false,
919+
num_bytes_per_page, init_page_count,
920+
max_page_count, &memory_data_size)
920921
!= BHT_OK) {
921922
set_error_buf(error_buf, error_buf_size,
922923
"allocate linear memory failed");

core/iwasm/common/wasm_application.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static union {
6161
* Implementation of wasm_application_execute_main()
6262
*/
6363
static bool
64-
check_main_func_type(const WASMFuncType *type)
64+
check_main_func_type(const WASMFuncType *type, bool is_memory64)
6565
{
6666
if (!(type->param_count == 0 || type->param_count == 2)
6767
|| type->result_count > 1) {
@@ -72,7 +72,8 @@ check_main_func_type(const WASMFuncType *type)
7272

7373
if (type->param_count == 2
7474
&& !(type->types[0] == VALUE_TYPE_I32
75-
&& type->types[1] == VALUE_TYPE_I32)) {
75+
&& type->types[1]
76+
== (is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32))) {
7677
LOG_ERROR(
7778
"WASM execute application failed: invalid main function type.\n");
7879
return false;
@@ -94,14 +95,18 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
9495
WASMFunctionInstanceCommon *func;
9596
WASMFuncType *func_type = NULL;
9697
WASMExecEnv *exec_env = NULL;
97-
uint32 argc1 = 0, argv1[2] = { 0 };
98+
uint32 argc1 = 0, argv1[3] = { 0 };
9899
uint32 total_argv_size = 0;
99100
uint64 total_size;
100101
uint64 argv_buf_offset = 0;
101102
int32 i;
102103
char *argv_buf, *p, *p_end;
103104
uint32 *argv_offsets, module_type;
104-
bool ret, is_import_func = true;
105+
bool ret, is_import_func = true, is_memory64 = false;
106+
#if WASM_ENABLE_MEMORY64 != 0
107+
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
108+
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
109+
#endif
105110

106111
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
107112
if (!exec_env) {
@@ -187,7 +192,7 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
187192
return false;
188193
}
189194

190-
if (!check_main_func_type(func_type)) {
195+
if (!check_main_func_type(func_type, is_memory64)) {
191196
wasm_runtime_set_exception(module_inst,
192197
"invalid function type of main function");
193198
return false;
@@ -218,11 +223,21 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
218223
p += strlen(argv[i]) + 1;
219224
}
220225

221-
argc1 = 2;
222226
argv1[0] = (uint32)argc;
223-
/* TODO: memory64 uint64 when the memory idx is i64 */
224-
argv1[1] =
225-
(uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
227+
#if WASM_ENABLE_MEMORY64 != 0
228+
if (is_memory64) {
229+
argc1 = 3;
230+
uint64 app_addr =
231+
wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
232+
PUT_I64_TO_ADDR(&argv[1], app_addr);
233+
}
234+
else
235+
#endif
236+
{
237+
argc1 = 2;
238+
argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst,
239+
argv_offsets);
240+
}
226241
}
227242

228243
ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1);

0 commit comments

Comments
 (0)