1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 2001 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
12 /***********************************************************************/
18 #include <sys/resource.h>
20 static char sig_alt_stack
[SIGSTKSZ
];
21 static char * system_stack_top
;
23 #if defined(TARGET_i386) && defined(SYS_linux_elf)
24 static void segv_handler(int signo
, struct sigcontext sc
)
26 char * fault_addr
= (char *) sc
.cr2
;
28 static void segv_handler(int signo
, siginfo_t
* info
, void * context
)
30 char * fault_addr
= (char *) info
->si_addr
;
34 if (getrlimit(RLIMIT_STACK
, &limit
) == 0 &&
35 ((long) fault_addr
& (sizeof(long) - 1)) == 0 &&
36 fault_addr
< system_stack_top
&&
37 fault_addr
>= system_stack_top
- limit
.rlim_cur
- 0x2000) {
44 int main(int argc
, char ** argv
)
49 stk
.ss_sp
= sig_alt_stack
;
50 stk
.ss_size
= SIGSTKSZ
;
52 #if defined(TARGET_i386) && defined(SYS_linux_elf)
53 act
.sa_handler
= (void (*)(int)) segv_handler
;
54 act
.sa_flags
= SA_ONSTACK
| SA_NODEFER
;
56 act
.sa_sigaction
= segv_handler
;
57 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
| SA_NODEFER
;
59 sigemptyset(&act
.sa_mask
);
60 system_stack_top
= (char *) &act
;
61 if (sigaltstack(&stk
, NULL
) != 0) { perror("sigaltstack"); return 2; }
62 if (sigaction(SIGSEGV
, &act
, NULL
) != 0) { perror("sigaction"); return 2; }
63 /* We used to trigger a stack overflow at this point to test whether
64 the code above works, but this causes problems with POSIX threads
65 on some BSD systems. So, instead, we just test that all this
66 code compiles, indicating that the required syscalls are there. */