Skip to content

Shared heap enhancement for AOT and update tests and samples #4226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9747e00
shared heap enhancement: modify memory check for aot_check_memory_ove…
TianlongLiang Apr 9, 2025
b0f0741
first draft of shared heap enhancement in AOT
TianlongLiang Apr 11, 2025
20cd6af
fix some compilation issue
TianlongLiang Apr 14, 2025
2f16e41
use alloca for func ctx shared heap cache value
TianlongLiang Apr 15, 2025
b58c75c
use correct alloca for func ctx shared heap cache value
TianlongLiang Apr 15, 2025
5de0ca3
enable shared heap chain aot test and bug fix
TianlongLiang Apr 17, 2025
106732b
Fix a missing argument on 32bits platform, still has the shared heap …
TianlongLiang Apr 17, 2025
eaf2020
Fix shared heap chain iteration problem on 32bits platform
TianlongLiang Apr 18, 2025
ae9d5ea
fix AOT bulk memory bounds checks compliation issue
TianlongLiang Apr 21, 2025
86fbb8e
fix AOT bulk memory bounds checks on 64 bits platform
TianlongLiang Apr 23, 2025
463b961
refactor aot memory check
TianlongLiang Apr 24, 2025
bc8dcd7
refactor AOT bulk memory bounds checks
TianlongLiang Apr 24, 2025
b5839c1
update AOT bulk memory bounds checks
TianlongLiang Apr 24, 2025
63de113
update AOT bulk memory bounds checks
TianlongLiang Apr 24, 2025
9dff0fd
update AOT bulk memory bounds check
TianlongLiang Apr 24, 2025
386dac3
fix typo
TianlongLiang Apr 24, 2025
f960e14
add more unit test for shared heap
TianlongLiang Apr 25, 2025
671c70a
finished organizing unit test for shared heap and enable x86_32 for s…
TianlongLiang Apr 28, 2025
d332af5
format
TianlongLiang Apr 28, 2025
e1432e4
cover a corner case for bulk memory overflow check
TianlongLiang Apr 29, 2025
ecfb609
cover a corner case for bulk memory overflow check
TianlongLiang Apr 29, 2025
5702a61
some update
TianlongLiang Apr 30, 2025
7ddc5f8
try func call to replace shared heap chain traverse
TianlongLiang May 9, 2025
7cdd02e
fix compilation error in JIT and potentially load nullptr
TianlongLiang May 15, 2025
b0415c0
add option for wamrc to enable single shared heap/multi shared heap, …
TianlongLiang May 16, 2025
a8f61a5
some refactor
TianlongLiang May 20, 2025
6405cc2
format
TianlongLiang May 20, 2025
3288983
shared heap 32 bit platform unit test
TianlongLiang May 28, 2025
8f49f86
cr suggestions: 1. check potiential underflow 2. refactor and use sep…
TianlongLiang Jun 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/iwasm/aot/aot_reloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ typedef struct {
#define REG_STRINGREF_SYM()
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
#define REG_SHARED_HEAP_SYM() \
REG_SYM(wasm_runtime_update_last_used_shared_heap),
#else
#define REG_SHARED_HEAP_SYM()
#endif

#define REG_COMMON_SYMBOLS \
REG_SYM(aot_set_exception_with_id), \
REG_SYM(aot_invoke_native), \
Expand Down Expand Up @@ -218,6 +225,7 @@ typedef struct {
REG_LLVM_PGO_SYM() \
REG_GC_SYM() \
REG_STRINGREF_SYM() \
REG_SHARED_HEAP_SYM() \

#define CHECK_RELOC_OFFSET(data_size) do { \
if (!check_reloc_offset(target_section_size, \
Expand Down
10 changes: 10 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_start_off) == 16);
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_end_off) == 24);
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap) == 32);

bh_static_assert(offsetof(WASMSharedHeap, next) == 0);
bh_static_assert(offsetof(WASMSharedHeap, chain_next) == 8);
bh_static_assert(offsetof(WASMSharedHeap, heap_handle) == 16);
bh_static_assert(offsetof(WASMSharedHeap, base_addr) == 24);
bh_static_assert(offsetof(WASMSharedHeap, size) == 32);
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem64) == 40);
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem32) == 48);

bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);

bh_static_assert(sizeof(wasm_val_t) == 16);
Expand Down Expand Up @@ -1991,6 +1999,8 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
#else
extra->shared_heap_start_off.u32[0] = UINT32_MAX;
#endif
/* After shared heap chain, will early stop if shared heap is NULL */
extra->shared_heap = NULL;

#if WASM_ENABLE_PERF_PROFILING != 0
total_size = sizeof(AOTFuncPerfProfInfo)
Expand Down
15 changes: 10 additions & 5 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
(uint64)get_last_used_shared_heap_start_offset(module_inst);
shared_heap_end = (uint64)get_last_used_shared_heap_end_offset(module_inst);
if (app_offset >= shared_heap_start
&& app_offset <= shared_heap_end - bytes + 1) {
&& app_offset <= shared_heap_end - bytes + 1
&& bytes - 1 <= shared_heap_end) {
return true;
}

Expand All @@ -624,7 +625,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
if (app_offset < shared_heap_start
|| app_offset > shared_heap_end - bytes + 1) {
|| app_offset > shared_heap_end - bytes + 1
|| bytes - 1 > shared_heap_end) {
goto fail;
}

Expand All @@ -635,7 +637,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
shared_heap_end = shared_heap_start - 1 + cur->size;
if (app_offset >= shared_heap_start
&& app_offset <= shared_heap_end - bytes + 1) {
&& app_offset <= shared_heap_end - bytes + 1
&& bytes - 1 <= shared_heap_end) {
update_last_used_shared_heap(module_inst, cur, is_memory64);
return true;
}
Expand Down Expand Up @@ -1227,7 +1230,9 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
#if WASM_ENABLE_SHARED_HEAP != 0
if (is_native_addr_in_shared_heap(module_inst_comm,
memory_inst->is_memory64, addr, 1)) {
return addr - get_last_used_shared_heap_base_addr_adj(module_inst_comm);
return (uint64)(uintptr_t)(addr
- get_last_used_shared_heap_base_addr_adj(
module_inst_comm));
}
#endif

Expand Down Expand Up @@ -1349,7 +1354,7 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
(WASMModuleInstanceCommon *)module_inst);
shared_heap_end_off = get_last_used_shared_heap_end_offset(
(WASMModuleInstanceCommon *)module_inst);
native_addr = shared_heap_base_addr_adj + app_buf_addr;
native_addr = shared_heap_base_addr_adj + (uintptr_t)app_buf_addr;

/* The whole string must be in the shared heap */
str = (const char *)native_addr;
Expand Down
37 changes: 37 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -7883,3 +7883,40 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)

return true;
}

#if WASM_ENABLE_SHARED_HEAP != 0
bool
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
uintptr_t app_offset, size_t bytes,
uintptr_t *shared_heap_start_off_p,
uintptr_t *shared_heap_end_off_p,
uint8 **shared_heap_base_addr_adj_p,
bool is_memory64)
{
WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
uint64 shared_heap_start, shared_heap_end;

if (bytes == 0) {
bytes = 1;
}

/* Find the exact shared heap that app addr is in, and update last used
* shared heap info in func context */
for (cur = heap; cur; cur = cur->chain_next) {
shared_heap_start =
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
shared_heap_end = shared_heap_start - 1 + cur->size;
if (app_offset >= shared_heap_start
&& app_offset <= shared_heap_end - bytes + 1
&& bytes - 1 <= shared_heap_end) {
*shared_heap_start_off_p = (uintptr_t)shared_heap_start;
*shared_heap_end_off_p = (uintptr_t)shared_heap_end;
*shared_heap_base_addr_adj_p =
cur->base_addr - (uintptr_t)shared_heap_start;
return true;
}
}

return false;
}
#endif
10 changes: 10 additions & 0 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,16 @@ void
wasm_runtime_set_linux_perf(bool flag);
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
bool
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
uintptr_t app_offset, size_t bytes,
uintptr_t *shared_heap_start_off_p,
uintptr_t *shared_heap_end_off_p,
uint8 **shared_heap_base_addr_adj_p,
bool is_memory64);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading