Skip to content

Commit b11dbcb

Browse files
authored
thread mgr: Free aux stack only when it was allocated (#3282)
When thread manager is enabled, the aux stack of exec_env may be allocated by wasm_cluster_allocate_aux_stack or disabled by setting aux_stack_bottom as UINTPTR_MAX directly. For the latter, no need to free it. And fix an issue when paring `--gc-heap-size=n` argument for iwasm, and fix a variable shadowed warning in fast-jit.
1 parent 4ef724b commit b11dbcb

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

core/iwasm/common/wasm_exec_env.h

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ typedef struct WASMExecEnv {
117117

118118
/* whether current thread is detached */
119119
bool thread_is_detached;
120+
121+
/* whether the aux stack is allocated */
122+
bool is_aux_stack_allocated;
120123
#endif
121124

122125
#if WASM_ENABLE_GC != 0

core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -9293,8 +9293,8 @@ jit_codegen_init()
92939293
imm.setValue(INT32_MAX);
92949294
a.jne(imm);
92959295

9296-
char *stream = (char *)a.code()->sectionById(0)->buffer().data()
9297-
+ a.code()->sectionById(0)->buffer().size();
9296+
char *stream_old = (char *)a.code()->sectionById(0)->buffer().data()
9297+
+ a.code()->sectionById(0)->buffer().size();
92989298

92999299
/* If yes, call jit_set_exception_with_id to throw exception,
93009300
and then set eax to JIT_INTERP_ACTION_THROWN, and jump to
@@ -9319,7 +9319,7 @@ jit_codegen_init()
93199319
/* Patch the offset of jne instruction */
93209320
char *stream_new = (char *)a.code()->sectionById(0)->buffer().data()
93219321
+ a.code()->sectionById(0)->buffer().size();
9322-
*(int32 *)(stream - 4) = (int32)(stream_new - stream);
9322+
*(int32 *)(stream_old - 4) = (int32)(stream_new - stream_old);
93239323
}
93249324

93259325
/* Load compiled func ptr and call it */

core/iwasm/libraries/thread-mgr/thread_manager.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
558558
aux_stack_size)) {
559559
goto fail3;
560560
}
561+
new_exec_env->is_aux_stack_allocated = true;
561562

562563
/* Inherit suspend_flags of parent thread */
563564
new_exec_env->suspend_flags.flags =
@@ -603,7 +604,9 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
603604
exec_env_tls = exec_env;
604605
}
605606

606-
/* Free aux stack space */
607+
/* Free aux stack space which was allocated in
608+
wasm_cluster_spawn_exec_env */
609+
bh_assert(exec_env_tls->is_aux_stack_allocated);
607610
wasm_cluster_free_aux_stack(exec_env_tls,
608611
(uint64)exec_env->aux_stack_bottom);
609612

@@ -655,7 +658,9 @@ thread_manager_start_routine(void *arg)
655658
#endif
656659

657660
/* Free aux stack space */
658-
wasm_cluster_free_aux_stack(exec_env, (uint64)exec_env->aux_stack_bottom);
661+
if (exec_env->is_aux_stack_allocated)
662+
wasm_cluster_free_aux_stack(exec_env,
663+
(uint64)exec_env->aux_stack_bottom);
659664

660665
os_mutex_lock(&cluster_list_lock);
661666

@@ -723,11 +728,13 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
723728
aux_stack_size)) {
724729
goto fail2;
725730
}
731+
new_exec_env->is_aux_stack_allocated = true;
726732
}
727733
else {
728734
/* Disable aux stack */
729735
new_exec_env->aux_stack_boundary = 0;
730736
new_exec_env->aux_stack_bottom = UINTPTR_MAX;
737+
new_exec_env->is_aux_stack_allocated = false;
731738
}
732739

733740
/* Inherit suspend_flags of parent thread */
@@ -1049,7 +1056,9 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
10491056
#endif
10501057

10511058
/* Free aux stack space */
1052-
wasm_cluster_free_aux_stack(exec_env, (uint64)exec_env->aux_stack_bottom);
1059+
if (exec_env->is_aux_stack_allocated)
1060+
wasm_cluster_free_aux_stack(exec_env,
1061+
(uint64)exec_env->aux_stack_bottom);
10531062

10541063
/* App exit the thread, free the resources before exit native thread */
10551064

product-mini/platforms/posix/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ main(int argc, char *argv[])
675675
#endif
676676
#if WASM_ENABLE_GC != 0
677677
else if (!strncmp(argv[0], "--gc-heap-size=", 15)) {
678-
if (argv[0][21] == '\0')
678+
if (argv[0][15] == '\0')
679679
return print_help();
680680
gc_heap_size = atoi(argv[0] + 15);
681681
}

0 commit comments

Comments
 (0)