x86: sigaltstack problems

Sergei Trofimovich st at anti-virus.by
Fri Feb 15 07:40:14 UTC 2008


On Thu, 14 Feb 2008 11:40:21 -0700
Bert JW Regeer <xistence at 0x58.com> wrote:

> On Feb 14, 2008, at 08:46 , Sergei Trofimovich wrote:
> 
> > Attached file causes segfaults on freebsd 4,5,6
> > but keeps alive in linux.
> >
> > IANIAML, so please CC me explicitly.
> >
> > Thanks!
> 
> You did not attach any files.
> 
> Bert JW Regeer

Sorry, something stripped it out.

(copy of file is here -  http://rafb.net/p/OYjAUQ55.html)

The question is:
Is it okay the program segfaults?

I thought sigaltstack is the way not to mess our (possible invalid) stack.
IANIAML, so please CC me explicitly.

//////////////////////////////////////////////////////
//main.c:
//////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <sys/time.h>
#include <signal.h>
#include <unistd.h>

volatile int alarmed = 0;
void alarm_handler(int signo)
{
    alarmed = 1;
}

#define EMIT_ASM_CALL(aflag)                                \
    asm volatile(                                           \
        "nop \t\n"                                          \
        /* backup and mess esp */                           \
        "movl %%esp, %%ebp \t\n"                            \
        "xorl %%eax, %%eax \t\n"                            \
        "movl %%eax, %%esp \t\n"                            \
                                                            \
        "while_not_alarmed: \t\n"                           \
        "movl %0, %%eax \t\n"                               \
        "test %%eax, %%eax \t\n"                            \
                                                            \
        /* loop on volatile var */                          \
        "jz while_not_alarmed \t\n"                         \
                                                            \
        /* restore esp */                                   \
        "movl %%ebp, %%esp \t\n"                            \
        "nop \t\n"                                          \
        :                                                   \
        : "m"(aflag)                                        \
        : "%eax", "%ebp", "%esp","cc" /* we mess up EFLAGS */);

int main ()
{
    /* alternate stack not to segfault on signal arrival */
    stack_t ss;
    ss.ss_sp = malloc(SIGSTKSZ);
    if (ss.ss_sp == NULL) exit (1);
    ss.ss_size = SIGSTKSZ;
    ss.ss_flags = 0;
    if (sigaltstack(&ss, NULL) == -1) exit (2);


    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sigfillset(&sa.sa_mask);
    sa.sa_handler = alarm_handler;
    // we DO alternate stack on signal arrival
    sa.sa_flags   = SA_ONSTACK;
    sigaction(SIGALRM, &sa, NULL);

    alarm (1);

    // loop on volatile var
    EMIT_ASM_CALL(alarmed);

    printf ("caught alarm signal\n");
    return 0;
}


More information about the freebsd-hackers mailing list