Skip to content

Commit 1329e1d

Browse files
authoredAug 21, 2024
Add support for multi-memory proposal in classic interpreter (bytecodealliance#3742)
Implement multi-memory for classic-interpreter. Support core spec (and bulk memory) opcodes now, and will support atomic opcodes, and add multi-memory export APIs in the future. PS: Multi-memory spec test patched a lot for linking test to adapt for multi-module implementation.
1 parent f4383a9 commit 1329e1d

22 files changed

+1658
-262
lines changed
 

‎.github/workflows/compilation_on_android_ubuntu.yml

+36-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ env:
6868
WAMR_COMPILER_TEST_OPTIONS: "-s wamr_compiler -S -b -P"
6969
GC_TEST_OPTIONS: "-s spec -G -b -P"
7070
MEMORY64_TEST_OPTIONS: "-s spec -W -b -P"
71+
MULTI_MEMORY_TEST_OPTIONS: "-s spec -E -b -P"
7172

7273
jobs:
7374
build_llvm_libraries_on_ubuntu_2204:
@@ -148,6 +149,7 @@ jobs:
148149
"-DWAMR_BUILD_TAIL_CALL=1",
149150
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
150151
"-DWAMR_BUILD_MEMORY64=1",
152+
"-DWAMR_BUILD_MULTI_MEMORY=1",
151153
]
152154
os: [ubuntu-22.04]
153155
platform: [android, linux]
@@ -206,11 +208,9 @@ jobs:
206208
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
207209
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
208210
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
209-
# Memory64 only on CLASSIC INTERP mode, and only on 64-bit platform
211+
# Memory64 only on CLASSIC INTERP and AOT mode, and only on 64-bit platform
210212
- make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
211213
platform: android
212-
- make_options_run_mode: $AOT_BUILD_OPTIONS
213-
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
214214
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
215215
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
216216
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
@@ -221,6 +221,21 @@ jobs:
221221
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
222222
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
223223
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
224+
# Multi memory only on CLASSIC INTERP mode, and only on 64-bit platform
225+
- make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
226+
platform: android
227+
- make_options_run_mode: $AOT_BUILD_OPTIONS
228+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
229+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
230+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
231+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
232+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
233+
- make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
234+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
235+
- make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
236+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
237+
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
238+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
224239
# Fast-JIT and Multi-Tier-JIT mode don't support android
225240
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
226241
platform: android
@@ -593,6 +608,7 @@ jobs:
593608
$WASI_TEST_OPTIONS,
594609
$GC_TEST_OPTIONS,
595610
$MEMORY64_TEST_OPTIONS,
611+
$MULTI_MEMORY_TEST_OPTIONS,
596612
]
597613
wasi_sdk_release:
598614
[
@@ -640,18 +656,30 @@ jobs:
640656
test_option: $MEMORY64_TEST_OPTIONS
641657
- running_mode: "multi-tier-jit"
642658
test_option: $MEMORY64_TEST_OPTIONS
659+
# aot, fast-interp, fast-jit, llvm-jit, multi-tier-jit don't support Multi Memory
660+
- running_mode: "aot"
661+
test_option: $MULTI_MEMORY_TEST_OPTIONS
662+
- running_mode: "fast-interp"
663+
test_option: $MULTI_MEMORY_TEST_OPTIONS
664+
- running_mode: "fast-jit"
665+
test_option: $MULTI_MEMORY_TEST_OPTIONS
666+
- running_mode: "jit"
667+
test_option: $MULTI_MEMORY_TEST_OPTIONS
668+
- running_mode: "multi-tier-jit"
669+
test_option: $MULTI_MEMORY_TEST_OPTIONS
670+
643671
steps:
644672
- name: checkout
645673
uses: actions/checkout@v4
646674

647675
- name: Set-up OCaml
648676
uses: ocaml/setup-ocaml@v3
649-
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
677+
if: matrix.test_option == '$GC_TEST_OPTIONS'
650678
with:
651679
ocaml-compiler: 4.13
652680

653681
- name: Set-up Ocamlbuild
654-
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
682+
if: matrix.test_option == '$GC_TEST_OPTIONS'
655683
run: opam install ocamlbuild dune menhir
656684

657685
- name: download and install wasi-sdk
@@ -717,13 +745,13 @@ jobs:
717745

718746
- name: run tests
719747
timeout-minutes: 30
720-
if: matrix.test_option != '$GC_TEST_OPTIONS' && matrix.test_option != '$MEMORY64_TEST_OPTIONS'
748+
if: matrix.test_option != '$GC_TEST_OPTIONS'
721749
run: ./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }}
722750
working-directory: ./tests/wamr-test-suites
723751

724-
- name: run gc or memory64 tests
752+
- name: run gc tests
725753
timeout-minutes: 20
726-
if: matrix.test_option == '$GC_TEST_OPTIONS' || matrix.test_option == '$MEMORY64_TEST_OPTIONS'
754+
if: matrix.test_option == '$GC_TEST_OPTIONS'
727755
run: |
728756
eval $(opam env)
729757
./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }}

‎.github/workflows/nightly_run.yml

+23-3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ jobs:
132132
"-DWAMR_BUILD_TAIL_CALL=1",
133133
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
134134
"-DWAMR_BUILD_MEMORY64=1",
135+
"-DWAMR_BUILD_MULTI_MEMORY=1",
135136
]
136137
os: [ubuntu-20.04]
137138
platform: [android, linux]
@@ -190,11 +191,9 @@ jobs:
190191
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
191192
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
192193
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
193-
# Memory64 only on CLASSIC INTERP mode, and only on 64-bit platform
194+
# Memory64 only on CLASSIC INTERP and AOT mode, and only on 64-bit platform
194195
- make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
195196
platform: android
196-
- make_options_run_mode: $AOT_BUILD_OPTIONS
197-
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
198197
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
199198
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
200199
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
@@ -205,6 +204,21 @@ jobs:
205204
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
206205
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
207206
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
207+
# Multi memory 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_MULTI_MEMORY=1"
212+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
213+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
214+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
215+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
216+
- make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
217+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
218+
- make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
219+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
220+
- make_options_run_mode: $MULTI_TIER_JIT_BUILD_OPTIONS
221+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
208222
# Fast-JIT and Multi-Tier-JIT mode don't support android
209223
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
210224
platform: android
@@ -289,6 +303,7 @@ jobs:
289303
"-DWAMR_BUILD_TAIL_CALL=1",
290304
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
291305
"-DWAMR_BUILD_MEMORY64=1",
306+
"-DWAMR_BUILD_MULTI_MEMORY=1",
292307
]
293308
exclude:
294309
# incompatible feature and platform
@@ -322,6 +337,11 @@ jobs:
322337
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
323338
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
324339
make_options_feature: "-DWAMR_BUILD_MEMORY64=1"
340+
# Memory64 only on CLASSIC INTERP mode
341+
- make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
342+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
343+
- make_options_run_mode: $FAST_JIT_BUILD_OPTIONS
344+
make_options_feature: "-DWAMR_BUILD_MULTI_MEMORY=1"
325345
steps:
326346
- name: Install dependencies
327347
run: |

‎build-scripts/config_common.cmake

+5
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ if (WAMR_BUILD_MEMORY64 EQUAL 1)
265265
set (WAMR_DISABLE_HW_BOUND_CHECK 1)
266266
message (" Memory64 memory enabled")
267267
endif ()
268+
if (WAMR_BUILD_MULTI_MEMORY EQUAL 1)
269+
add_definitions (-DWASM_ENABLE_MULTI_MEMORY=1)
270+
message (" Multi memory enabled")
271+
set (WAMR_BUILD_DEBUG_INTERP 0)
272+
endif ()
268273
if (WAMR_BUILD_THREAD_MGR EQUAL 1)
269274
message (" Thread manager enabled")
270275
endif ()

‎core/config.h

+5
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@
664664
#define WASM_ENABLE_MEMORY64 0
665665
#endif
666666

667+
/* Disable multi-memory by default */
668+
#ifndef WASM_ENABLE_MULTI_MEMORY
669+
#define WASM_ENABLE_MULTI_MEMORY 0
670+
#endif
671+
667672
#ifndef WASM_TABLE_MAX_SIZE
668673
#define WASM_TABLE_MAX_SIZE 1024
669674
#endif

‎core/iwasm/aot/aot_runtime.c

+7
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,13 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
28172817
return wasm_enlarge_memory(module_inst, inc_page_count);
28182818
}
28192819

2820+
bool
2821+
aot_enlarge_memory_with_idx(AOTModuleInstance *module_inst,
2822+
uint32 inc_page_count, uint32 memidx)
2823+
{
2824+
return wasm_enlarge_memory_with_idx(module_inst, inc_page_count, memidx);
2825+
}
2826+
28202827
bool
28212828
aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
28222829
uint32 *argv)

‎core/iwasm/aot/aot_runtime.h

+4
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
605605
bool
606606
aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count);
607607

608+
bool
609+
aot_enlarge_memory_with_idx(AOTModuleInstance *module_inst,
610+
uint32 inc_page_count, uint32 memidx);
611+
608612
/**
609613
* Invoke native function from aot code
610614
*/

‎core/iwasm/common/wasm_memory.c

+36-2
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,16 @@ wasm_get_default_memory(WASMModuleInstance *module_inst)
670670
return NULL;
671671
}
672672

673+
WASMMemoryInstance *
674+
wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
675+
{
676+
bh_assert(index < module_inst->memory_count);
677+
if (module_inst->memories)
678+
return module_inst->memories[index];
679+
else
680+
return NULL;
681+
}
682+
673683
void
674684
wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
675685
uint64 memory_data_size)
@@ -747,9 +757,14 @@ wasm_mmap_linear_memory(uint64_t map_size, uint64 commit_size)
747757
}
748758

749759
bool
750-
wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
760+
wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count,
761+
uint32 memidx)
751762
{
763+
#if WASM_ENABLE_MULTI_MEMORY != 0
764+
WASMMemoryInstance *memory = wasm_get_memory_with_idx(module, memidx);
765+
#else
752766
WASMMemoryInstance *memory = wasm_get_default_memory(module);
767+
#endif
753768
uint8 *memory_data_old, *memory_data_new, *heap_data_old;
754769
uint32 num_bytes_per_page, heap_size;
755770
uint32 cur_page_count, max_page_count, total_page_count;
@@ -960,7 +975,7 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
960975
if (module->memory_count > 0)
961976
shared_memory_lock(module->memories[0]);
962977
#endif
963-
ret = wasm_enlarge_memory_internal(module, inc_page_count);
978+
ret = wasm_enlarge_memory_internal(module, inc_page_count, 0);
964979
#if WASM_ENABLE_SHARED_MEMORY != 0
965980
if (module->memory_count > 0)
966981
shared_memory_unlock(module->memories[0]);
@@ -969,6 +984,25 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
969984
return ret;
970985
}
971986

987+
bool
988+
wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
989+
uint32 memidx)
990+
{
991+
bool ret = false;
992+
993+
#if WASM_ENABLE_SHARED_MEMORY != 0
994+
if (memidx < module->memory_count)
995+
shared_memory_lock(module->memories[memidx]);
996+
#endif
997+
ret = wasm_enlarge_memory_internal(module, inc_page_count, memidx);
998+
#if WASM_ENABLE_SHARED_MEMORY != 0
999+
if (memidx < module->memory_count)
1000+
shared_memory_unlock(module->memories[memidx]);
1001+
#endif
1002+
1003+
return ret;
1004+
}
1005+
9721006
void
9731007
wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
9741008
{

‎core/iwasm/common/wasm_runtime_common.c

+26-24
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,36 @@ static RunningMode runtime_running_mode = Mode_Default;
181181
of signal handler */
182182
static os_thread_local_attribute WASMExecEnv *exec_env_tls = NULL;
183183

184+
static bool
185+
is_sig_addr_in_guard_pages(void *sig_addr, WASMModuleInstance *module_inst)
186+
{
187+
WASMMemoryInstance *memory_inst;
188+
uint8 *mapped_mem_start_addr = NULL;
189+
uint8 *mapped_mem_end_addr = NULL;
190+
uint32 i;
191+
192+
for (i = 0; i < module_inst->memory_count; ++i) {
193+
/* To be compatible with multi memory, get the ith memory instance */
194+
memory_inst = wasm_get_memory_with_idx(module_inst, i);
195+
mapped_mem_start_addr = memory_inst->memory_data;
196+
mapped_mem_end_addr = memory_inst->memory_data + 8 * (uint64)BH_GB;
197+
if (mapped_mem_start_addr <= (uint8 *)sig_addr
198+
&& (uint8 *)sig_addr < mapped_mem_end_addr) {
199+
/* The address which causes segmentation fault is inside
200+
the memory instance's guard regions */
201+
return true;
202+
}
203+
}
204+
205+
return false;
206+
}
207+
184208
#ifndef BH_PLATFORM_WINDOWS
185209
static void
186210
runtime_signal_handler(void *sig_addr)
187211
{
188212
WASMModuleInstance *module_inst;
189-
WASMMemoryInstance *memory_inst;
190213
WASMJmpBuf *jmpbuf_node;
191-
uint8 *mapped_mem_start_addr = NULL;
192-
uint8 *mapped_mem_end_addr = NULL;
193214
uint32 page_size = os_getpagesize();
194215
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
195216
uint8 *stack_min_addr;
@@ -201,23 +222,13 @@ runtime_signal_handler(void *sig_addr)
201222
&& (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
202223
/* Get mapped mem info of current instance */
203224
module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
204-
/* Get the default memory instance */
205-
memory_inst = wasm_get_default_memory(module_inst);
206-
if (memory_inst) {
207-
mapped_mem_start_addr = memory_inst->memory_data;
208-
mapped_mem_end_addr = memory_inst->memory_data + 8 * (uint64)BH_GB;
209-
}
210225

211226
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
212227
/* Get stack info of current thread */
213228
stack_min_addr = os_thread_get_stack_boundary();
214229
#endif
215230

216-
if (memory_inst
217-
&& (mapped_mem_start_addr <= (uint8 *)sig_addr
218-
&& (uint8 *)sig_addr < mapped_mem_end_addr)) {
219-
/* The address which causes segmentation fault is inside
220-
the memory instance's guard regions */
231+
if (is_sig_addr_in_guard_pages(sig_addr, module_inst)) {
221232
wasm_set_exception(module_inst, "out of bounds memory access");
222233
os_longjmp(jmpbuf_node->jmpbuf, 1);
223234
}
@@ -340,16 +351,7 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
340351
&& (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
341352
module_inst = (WASMModuleInstance *)exec_env_tls->module_inst;
342353
if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
343-
/* Get the default memory instance */
344-
memory_inst = wasm_get_default_memory(module_inst);
345-
if (memory_inst) {
346-
mapped_mem_start_addr = memory_inst->memory_data;
347-
mapped_mem_end_addr =
348-
memory_inst->memory_data + 8 * (uint64)BH_GB;
349-
}
350-
351-
if (memory_inst && mapped_mem_start_addr <= (uint8 *)sig_addr
352-
&& (uint8 *)sig_addr < mapped_mem_end_addr) {
354+
if (is_sig_addr_in_guard_pages(sig_addr, module_inst)) {
353355
/* The address which causes segmentation fault is inside
354356
the memory instance's guard regions.
355357
Set exception and let the wasm func continue to run, when

‎core/iwasm/compilation/aot.c

+1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
540540
/* TODO: create import memories */
541541

542542
/* Allocate memory for memory array, reserve one AOTMemory space at least */
543+
/* TODO: multi-memory */
543544
if (!comp_data->memory_count)
544545
comp_data->memory_count = 1;
545546

‎core/iwasm/compilation/aot_emit_memory.c

+1
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
895895

896896
POP_PAGE_COUNT(delta);
897897

898+
/* TODO: multi-memory aot_enlarge_memory_with_idx() */
898899
/* Function type of aot_enlarge_memory() */
899900
param_types[0] = INT8_PTR_TYPE;
900901
param_types[1] = I32_TYPE;

‎core/iwasm/fast-jit/fe/jit_emit_memory.c

+1
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ jit_compile_op_memory_grow(JitCompContext *cc, uint32 mem_idx)
602602
args[0] = get_module_inst_reg(cc->jit_frame);
603603
args[1] = inc_page_count;
604604

605+
/* TODO: multi-memory wasm_enlarge_memory_with_idx() */
605606
if (!jit_emit_callnative(cc, wasm_enlarge_memory, grow_res, args, 2)) {
606607
goto fail;
607608
}

0 commit comments

Comments
 (0)