-
Notifications
You must be signed in to change notification settings - Fork 739
Add runtime API for shared heap destroy and related test cases #4730
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
Draft
TianlongLiang
wants to merge
3
commits into
bytecodealliance:main
Choose a base branch
from
TianlongLiang:dev/shared_heap_destroy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−65
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,6 +148,25 @@ static void | |
| wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, | ||
| uint64 map_size); | ||
|
|
||
| static void | ||
| destroy_shared_heap_node(WASMSharedHeap *heap) | ||
| { | ||
| uint64 map_size; | ||
|
|
||
| if (heap->heap_handle) { | ||
| mem_allocator_destroy(heap->heap_handle); | ||
| wasm_runtime_free(heap->heap_handle); | ||
| #ifndef OS_ENABLE_HW_BOUND_CHECK | ||
| map_size = heap->size; | ||
| #else | ||
| map_size = 8 * (uint64)BH_GB; | ||
| #endif | ||
| wasm_munmap_linear_memory(heap->base_addr, heap->size, map_size); | ||
| } | ||
|
|
||
| wasm_runtime_free(heap); | ||
| } | ||
|
|
||
| static void * | ||
| runtime_malloc(uint64 size) | ||
| { | ||
|
|
@@ -339,6 +358,95 @@ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain) | |
| return cur; | ||
| } | ||
|
|
||
| /* Destroy and recycle a shared heap(or head of shared heap chain), return | ||
| * next node in the shared heap chain through new_head */ | ||
| static bool | ||
| wasm_runtime_destroy_shared_heap_head(WASMSharedHeap *head, | ||
| WASMSharedHeap **new_head) | ||
| { | ||
| WASMSharedHeap *cur = NULL, *prev = NULL, *next_head = NULL; | ||
| bool head_found = false, is_chain_head = true; | ||
|
|
||
| if (!head) { | ||
| LOG_WARNING("Invalid shared heap to destroy."); | ||
| return false; | ||
| } | ||
|
|
||
| *new_head = NULL; | ||
|
|
||
| os_mutex_lock(&shared_heap_list_lock); | ||
| if (head->attached_count != 0) { | ||
| LOG_WARNING("To destroy shared heap, it needs to be detached first."); | ||
| os_mutex_unlock(&shared_heap_list_lock); | ||
| return false; | ||
| } | ||
|
|
||
| for (cur = shared_heap_list; cur; cur = cur->next) { | ||
| if (cur == head) | ||
| head_found = true; | ||
| if (cur->chain_next == head) | ||
| is_chain_head = false; | ||
| } | ||
| if (!head_found) { | ||
| LOG_WARNING("Shared heap %p isn't tracked by runtime.", head); | ||
| os_mutex_unlock(&shared_heap_list_lock); | ||
| return false; | ||
| } | ||
| if (!is_chain_head) { | ||
| LOG_WARNING("Shared heap %p isn't the head of a shared heap chain.", | ||
| head); | ||
| os_mutex_unlock(&shared_heap_list_lock); | ||
| return false; | ||
| } | ||
|
|
||
| next_head = head->chain_next; | ||
| if (head == shared_heap_list) { | ||
| shared_heap_list = head->next; | ||
| destroy_shared_heap_node(head); | ||
| } | ||
| else { | ||
| prev = shared_heap_list; | ||
| cur = shared_heap_list->next; | ||
| while (prev) { | ||
| if (head == cur) { | ||
| prev->next = head->next; | ||
| destroy_shared_heap_node(cur); | ||
| break; | ||
| } | ||
| prev = prev->next; | ||
| cur = cur->next; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that the process of looking for But the current implementation is definitely functional. It is acceptable to keep it as is. |
||
| } | ||
|
|
||
| os_mutex_unlock(&shared_heap_list_lock); | ||
|
|
||
| LOG_VERBOSE("Destroyed shared heap %p", head); | ||
|
|
||
| *new_head = next_head; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool | ||
| wasm_runtime_destroy_shared_heap(WASMSharedHeap *head, bool entire_chain, | ||
| WASMSharedHeap **new_head) | ||
| { | ||
| WASMSharedHeap *next_head = NULL; | ||
|
|
||
| do { | ||
| if (!wasm_runtime_destroy_shared_heap_head(head, &next_head)) { | ||
| return false; | ||
| } | ||
| head = next_head; | ||
| } while (entire_chain && head); | ||
|
|
||
| if (new_head) { | ||
| *new_head = next_head; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static uint8 * | ||
| get_last_used_shared_heap_base_addr_adj(WASMModuleInstanceCommon *module_inst) | ||
| { | ||
|
|
@@ -819,7 +927,6 @@ destroy_shared_heaps() | |
| { | ||
| WASMSharedHeap *heap; | ||
| WASMSharedHeap *cur; | ||
| uint64 map_size; | ||
|
|
||
| os_mutex_lock(&shared_heap_list_lock); | ||
| heap = shared_heap_list; | ||
|
|
@@ -829,17 +936,7 @@ destroy_shared_heaps() | |
| while (heap) { | ||
| cur = heap; | ||
| heap = heap->next; | ||
| if (cur->heap_handle) { | ||
| mem_allocator_destroy(cur->heap_handle); | ||
| wasm_runtime_free(cur->heap_handle); | ||
| #ifndef OS_ENABLE_HW_BOUND_CHECK | ||
| map_size = cur->size; | ||
| #else | ||
| map_size = 8 * (uint64)BH_GB; | ||
| #endif | ||
| wasm_munmap_linear_memory(cur->base_addr, cur->size, map_size); | ||
| } | ||
| wasm_runtime_free(cur); | ||
| destroy_shared_heap_node(cur); | ||
| } | ||
| os_mutex_destroy(&shared_heap_list_lock); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose there are two chains, A-B-C and B-C, and both need to be
wasm_runtime_destroy_shared_heap()entire_chain.First, destroy the first chain, which is A-B-C. When deleting up to C, it is discovered that C is not the head, and then the condition
if (!is_chain_head)will fail and return.