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

Open
wants to merge 28 commits into
base: dev/shared_heap
Choose a base branch
from

Conversation

TianlongLiang
Copy link
Collaborator

For AOT:

  • support preallocated shared heap and shared heap chain

For shared heap tests and samples:

  • enable AOT in sample and unit test
  • add more shared heap unit test cases, and enable shared heap unit test suite on X86_32 platform

…and update shared heap unit tests and sample
@lum1n0us lum1n0us linked an issue May 18, 2025 that may be closed by this pull request
@TianlongLiang
Copy link
Collaborator Author

The CI issue is due to the update of LLVM, which will be resolved after rebasing the dev branch

@TianlongLiang TianlongLiang marked this pull request as ready for review May 20, 2025 08:10
if (bytes == 0) {
bytes = 1;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, a large bytes can cause several troubles. Like underflow because shared_heap_end - bytes

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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, we should check all fields with DefPointer here to expose any potential AOT issues as early as possible. If agree, add next and heap_handle.

@@ -77,6 +77,23 @@ extern "C" {
#undef DUMP_MODULE
#endif

#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SHARED_HEAP != 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro definition can be moved to aot_llvm.c, and only one version should be kept.

uint32
get_module_inst_extra_offset(AOTCompContext *comp_ctx);

#define BUILD_FIELD_PTR(ptr, offset, field, name) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field is not used.

uint32
get_module_inst_extra_offset(AOTCompContext *comp_ctx);

#define BUILD_FIELD_PTR(ptr, offset, field, name) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a better name, like BUILD_LOAD_FIELD?

LLVMBasicBlockRef check_succ, LLVMValueRef maddr_phi,
LLVMValueRef start_offset, LLVMValueRef max_addr,
LLVMValueRef mem_base_addr, LLVMValueRef bytes, uint32 bytes_u32,
bool is_memory64, bool is_target_64bit, bool bulk_memory, bool enable_segue)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need comments to explain what each variable stands for.

LLVMBasicBlockRef check_succ, LLVMValueRef maddr_phi,
LLVMValueRef start_offset, LLVMValueRef max_addr,
LLVMValueRef mem_base_addr, LLVMValueRef bytes, uint32 bytes_u32,
bool is_memory64, bool is_target_64bit, bool bulk_memory, bool enable_segue)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maynot a good idea to cover two types of memory overflow check.

if (is_memory64 && comp_ctx->enable_bound_check) {
/* 1.1 offset + addr can overflow when it's memory64
* 2.1 Or when it's on 32-bit platform */
if (is_memory64 || !is_target_64bit) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss the condition && comp_ctx->enable_bound_check)?

BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp");
BUILD_ICMP(LLVMIntNE, max_addr, is_target_64bit ? I64_ZERO : I32_ZERO,
cmp1, "cmp1");
BUILD_OP(And, cmp, cmp1, cmp, "overflow");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought L1448 can detect overflow, so why are the two below, L1449 and L1451 required?

* otherwise it will be UINT32_MAX/UINT64_MAX */
if (!(func_ctx->shared_heap_head_start_off = LLVMBuildSelect(
comp_ctx->builder, cmp, shared_heap_head_start_off_minus_one,
shared_heap_head_start_off, "head_start_off"))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment is correct, we can use a const here without obtaining shared_heap_head_start_off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Shared heap enhancements
2 participants