]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/config/auto-aux/stackov.c
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / config / auto-aux / stackov.c
1 /***********************************************************************/
2 /*                                                                     */
3 /*                           Objective Caml                            */
4 /*                                                                     */
5 /*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
6 /*                                                                     */
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.  */
11 /*                                                                     */
12 /***********************************************************************/
13
14 /* $Id: stackov.c 8768 2008-01-11 16:13:18Z doligez $ */
15
16 #include <stdio.h>
17 #include <signal.h>
18 #include <sys/resource.h>
19
20 static char sig_alt_stack[SIGSTKSZ];
21 static char * system_stack_top;
22
23 #if defined(TARGET_i386) && defined(SYS_linux_elf)
24 static void segv_handler(int signo, struct sigcontext sc)
25 {
26   char * fault_addr = (char *) sc.cr2;
27 #else
28 static void segv_handler(int signo, siginfo_t * info, void * context)
29 {
30   char * fault_addr = (char *) info->si_addr;
31 #endif
32   struct rlimit limit;
33
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) {
38     _exit(0);
39   } else {
40     _exit(4);
41   }
42 }
43
44 int main(int argc, char ** argv)
45 {
46   stack_t stk;
47   struct sigaction act;
48
49   stk.ss_sp = sig_alt_stack;
50   stk.ss_size = SIGSTKSZ;
51   stk.ss_flags = 0;
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;
55 #else
56   act.sa_sigaction = segv_handler;
57   act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER;
58 #endif  
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. */
67   return 0;
68 }