alternate stacks for signal handlers

Henry Cejtin henry@sourcelight.com
Thu, 27 Jul 2000 01:44:24 -0500


I  don't  see  how,  given  the  Linux  kernel's cheap test to see if you are
already using the alternate stack, that you can ever view esp  as  a  general
register.   I  just did a test, with a bit of tweaked assembler code, and the
kernel does just what it appears to do: If a signal  comes  in  and  the  esp
register  happens  to  point  in  the  range of locations where you said your
alternate stack was, then the stack pointer is not changed, it is  just  used
as  is.   This `small' chance that esp happens to point into that region WILL
happen eventually, and you won't be able to duplicate the failure.  I  REALLY
hate that kind of bug.

Ah,  here  is a really grotesque hack.  If the size you decide you need for a
signal stack is N, then you allocate 2*N space and in the call to sigaltstack
you say that the alternate stack is
    start of space + N
and  N bytes long.  If a signal comes in and you were NOT using the alternate
stack, but the esp register happens to be in  this  range,  then  the  kernel
won't  bother  changing it and you will start to use it, but since you have N
bytes below (below because the stack grows on Intel chips by decreasing) that
you can safely write in, you still have your N bytes at least of stack.

Of  course  you also need to allocate some dead space so that if you overflow
the signal stack you  will  die  instead  of  silently  corrupting  yourself.
Again,  since  the  Intel stack grows by decreasing, you have to put the dead
page before the stark of the 2*N bytes.

I did a quick check of SML/NJ to see what they do, and I am  confused.   They
don't  ever  seem  to  even call sigaction, but they must be doing something.
I'll investigate more later.  I'm quite curious what they do.