Skip to content

Commit 669d7f1

Browse files
committed
fix(wasi-threads): release stack context memory before exit
1 parent ef6183b commit 669d7f1

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

core/shared/platform/common/posix/posix_thread.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ os_create_stack_context(korp_tid handle)
7777
return BH_MALLOC(sizeof(sigjmp_buf));
7878
}
7979

80+
static void
81+
free_stack_context_list(void *stack_context_list)
82+
{
83+
bh_list *context_list = (bh_list *)stack_context_list;
84+
85+
sigjmp_buf *context = bh_list_first_elem(context_list);
86+
while (context) {
87+
sigjmp_buf *next = bh_list_elem_next(context);
88+
BH_FREE(context);
89+
context = next;
90+
};
91+
92+
BH_FREE(context_list);
93+
}
94+
8095
bool
8196
os_stack_contexts_init()
8297
{
@@ -96,7 +111,7 @@ os_stack_contexts_init()
96111

97112
stack_context_handler.contexts = bh_hash_map_create(
98113
32, false, (HashFunc)thread_handle_hash,
99-
(KeyEqualFunc)thread_handle_equal, NULL, wasm_runtime_free);
114+
(KeyEqualFunc)thread_handle_equal, NULL, free_stack_context_list);
100115
if (stack_context_handler.contexts == NULL) {
101116
LOG_ERROR("failed to init stack contexts");
102117
return false;
@@ -106,11 +121,16 @@ os_stack_contexts_init()
106121
}
107122

108123
void
109-
os_stack_contexts_deinit()
124+
os_stack_contexts_destroy()
110125
{
111-
os_mutex_destroy(&stack_context_handler.lock);
126+
os_mutex_lock(&stack_context_handler.lock);
127+
if (!bh_hash_map_destroy(stack_context_handler.contexts))
128+
LOG_ERROR("failed to destroy stack context map");
129+
stack_context_handler.contexts = NULL;
130+
os_mutex_unlock(&stack_context_handler.lock);
112131

113-
// TODO(eloparco): Release memory
132+
if (os_mutex_destroy(&stack_context_handler.lock) != BHT_OK)
133+
LOG_ERROR("failed to destroy stack contexts");
114134
}
115135

116136
void
@@ -125,6 +145,7 @@ os_remove_stack_context(korp_tid handle, sigjmp_buf *context)
125145
assert(context_list);
126146
int ret = bh_list_remove(context_list, context);
127147
assert(ret == BH_LIST_SUCCESS);
148+
BH_FREE(context);
128149

129150
os_mutex_unlock(&stack_context_handler.lock);
130151
}
@@ -143,30 +164,36 @@ os_is_returning_from_signal(korp_tid handle, sigjmp_buf *context)
143164
if (!context_list) {
144165
context_list = BH_MALLOC(sizeof(bh_list));
145166
if (!context_list) {
146-
os_mutex_unlock(&stack_context_handler.lock);
147-
return NULL;
167+
LOG_ERROR("failed to allocate stack context list");
168+
goto unlock;
148169
}
170+
149171
int ret_list = bh_list_init(context_list);
150172
if (ret_list != BH_LIST_SUCCESS) {
151-
BH_FREE(context_list);
152-
os_mutex_unlock(&stack_context_handler.lock);
153-
return NULL;
173+
LOG_ERROR("failed to initialize stack context list");
174+
goto free_list_and_unlock;
154175
}
155176

156177
bool ret_map =
157178
bh_hash_map_insert(stack_context_handler.contexts,
158179
(void *)(uintptr_t)handle, context_list);
159180
if (!ret_map) {
160-
BH_FREE(context_list);
161-
os_mutex_unlock(&stack_context_handler.lock);
162-
return NULL;
181+
LOG_ERROR("failed to insert stack context list into map");
182+
goto free_list_and_unlock;
163183
}
164184
}
165185

166186
/* Add stack context to stack context list */
167187
int bh_res = bh_list_insert(context_list, context);
168-
assert(bh_res == BH_LIST_SUCCESS);
188+
if (bh_res != BH_LIST_SUCCESS) {
189+
LOG_ERROR("failed to insert stack context into list");
190+
goto free_list_and_unlock;
191+
}
192+
goto unlock;
169193

194+
free_list_and_unlock:
195+
BH_FREE(context_list);
196+
unlock:
170197
os_mutex_unlock(&stack_context_handler.lock);
171198
return false;
172199
}

core/shared/platform/include/platform_api_extension.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ os_stack_contexts_init();
315315
*
316316
*/
317317
void
318-
os_stack_contexts_deinit();
318+
os_stack_contexts_destroy();
319319

320320
/**
321321
* @brief Creates and returns an empty stack context for the current thread

product-mini/platforms/posix/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ main(int argc, char *argv[])
695695
/* unload the native libraries */
696696
unregister_and_unload_native_libs(native_handle_count, native_handle_list);
697697
#endif
698+
699+
os_stack_contexts_destroy();
698700

699701
/* destroy runtime environment */
700702
wasm_runtime_destroy();

0 commit comments

Comments
 (0)