Commit 15c555b3 authored by David Gibson's avatar David Gibson Committed by Dan Good

altstack: Restore alternate signal stack state

altstack relies on catching a SIGSEGV caused when overrunning the stack.
This means that the SEGV handler itself can't use the already overflowed
stack, and so we use sigaltstack() to assign the signal handler a different
stack.  On completion, altstack() clears the alternate signal stack.

However, it's possible that the calling program could be using
sigaltstack() for its own reasons, so it's more correct to restore the
sigaltstack() state to that from the beginning of the altstack() call.
This patch implements this behaviour.
Signed-off-by: default avatarDavid Gibson <david@gibson.dropbear.id.au>
parent 2da0271f
......@@ -71,6 +71,7 @@ int altstack(rlim_t max, void *(*fn)(void *), void *arg, void **out)
struct rlimit rl_save;
struct sigaction sa_save;
int errno_save;
stack_t ss_save;
assert(max > 0 && fn);
#define ok(x, y) ({ long __r = (long) (x); if (__r == -1) { bang(#x); if (y) goto out; } __r; })
......@@ -98,7 +99,7 @@ int altstack(rlim_t max, void *(*fn)(void *), void *arg, void **out)
stack_t ss = { .ss_sp = sigstk, .ss_size = sizeof(sigstk) };
struct sigaction sa = { .sa_handler = segvjmp, .sa_flags = SA_NODEFER|SA_RESETHAND|SA_ONSTACK };
ok(sigaltstack(&ss, 0), 1);
ok(sigaltstack(&ss, &ss_save), 1);
undo++;
sigemptyset(&sa.sa_mask);
......@@ -131,7 +132,7 @@ out:
case 4:
ok(sigaction(SIGSEGV, &sa_save, 0), 0);
case 3:
ok(sigaltstack(&(stack_t) { .ss_flags = SS_DISABLE }, 0), 0);
ok(sigaltstack(&ss_save, 0), 0);
case 2:
ok(munmap(m, max), 0);
case 1:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment