Skip to content

bpo-29884: faulthandler: Restore the old sigaltstack during teardown #777

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

Merged
merged 1 commit into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,7 @@ Artur Zaprzala
Mike Zarnstorff
Yury V. Zaytsev
Siebren van der Zee
Christophe Zeitouny
Nickolai Zeldovich
Yuxiao Zeng
Uwe Zessin
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ Extension Modules
Library
-------

- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
Patch by Christophe Zeitouny.

- bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.

- bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords
Expand Down
17 changes: 16 additions & 1 deletion Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static const size_t faulthandler_nsignals = \

#ifdef HAVE_SIGALTSTACK
static stack_t stack;
static stack_t old_stack;
#endif


Expand Down Expand Up @@ -1317,7 +1318,7 @@ int _PyFaulthandler_Init(void)
stack.ss_size = SIGSTKSZ;
stack.ss_sp = PyMem_Malloc(stack.ss_size);
if (stack.ss_sp != NULL) {
err = sigaltstack(&stack, NULL);
err = sigaltstack(&stack, &old_stack);
if (err) {
PyMem_Free(stack.ss_sp);
stack.ss_sp = NULL;
Expand Down Expand Up @@ -1373,6 +1374,20 @@ void _PyFaulthandler_Fini(void)
faulthandler_disable();
#ifdef HAVE_SIGALTSTACK
if (stack.ss_sp != NULL) {
/* Fetch the current alt stack */
stack_t current_stack;
if (sigaltstack(NULL, &current_stack) == 0) {
if (current_stack.ss_sp == stack.ss_sp) {
/* The current alt stack is the one that we installed.
It is safe to restore the old stack that we found when
we installed ours */
sigaltstack(&old_stack, NULL);
} else {
/* Someone switched to a different alt stack and didn't
restore ours when they were done (if they're done).
There's not much we can do in this unlikely case */
}
}
PyMem_Free(stack.ss_sp);
stack.ss_sp = NULL;
}
Expand Down