Skip to content

Commit da31c07

Browse files
authored
posix_thread.c: Restore old signal alternate stack before thread exit (#3693)
Some host environment may also create an signal alternate stack and access it after the wasm runtime exits, the runtime should backup the stack info and restore it before thread exits. The issue was found in golang: ``` signal 23 received on thread with on signal stack fatal error: non-Go code disabled signaltstack ```
1 parent 1362a30 commit da31c07

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ static os_thread_local_attribute bool thread_signal_inited = false;
504504
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
505505
/* The signal alternate stack base addr */
506506
static os_thread_local_attribute uint8 *sigalt_stack_base_addr;
507+
/* The previous signal alternate stack */
508+
static os_thread_local_attribute stack_t prev_sigalt_stack;
507509

508510
/*
509511
* ASAN is not designed to work with custom stack unwind or other low-level
@@ -683,7 +685,9 @@ os_thread_signal_init(os_signal_handler handler)
683685
sigalt_stack_info.ss_sp = map_addr;
684686
sigalt_stack_info.ss_size = map_size;
685687
sigalt_stack_info.ss_flags = 0;
686-
if (sigaltstack(&sigalt_stack_info, NULL) != 0) {
688+
memset(&prev_sigalt_stack, 0, sizeof(stack_t));
689+
/* Set signal alternate stack and save the previous one */
690+
if (sigaltstack(&sigalt_stack_info, &prev_sigalt_stack) != 0) {
687691
os_printf("Failed to init signal alternate stack\n");
688692
goto fail2;
689693
}
@@ -729,19 +733,12 @@ os_thread_signal_init(os_signal_handler handler)
729733
void
730734
os_thread_signal_destroy()
731735
{
732-
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
733-
stack_t sigalt_stack_info;
734-
#endif
735-
736736
if (!thread_signal_inited)
737737
return;
738738

739739
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
740-
/* Disable signal alternate stack */
741-
memset(&sigalt_stack_info, 0, sizeof(stack_t));
742-
sigalt_stack_info.ss_flags = SS_DISABLE;
743-
sigalt_stack_info.ss_size = SIG_ALT_STACK_SIZE;
744-
sigaltstack(&sigalt_stack_info, NULL);
740+
/* Restore the previous signal alternate stack */
741+
sigaltstack(&prev_sigalt_stack, NULL);
745742

746743
os_munmap(sigalt_stack_base_addr, SIG_ALT_STACK_SIZE);
747744

0 commit comments

Comments
 (0)