1 /* Test case mostly written by Paolo Bonzini <pbonzini@redhat.com>. */
9 #include <sys/resource.h>
12 static jmp_buf mainloop
;
13 static sigset_t mainsigset
;
18 stackoverflow_handler (int sig
)
22 sigaltstack (NULL
, &altstack
);
23 /* Using printf is not really kosher in signal handlers but we know
25 printf ("%*sin signal handler\n", pass
, "");
26 if (altstack
.ss_flags
& SS_ONSTACK
)
27 printf ("%*son alternate stack\n", pass
, "");
28 siglongjmp (mainloop
, pass
);
33 recurse_1 (int n
, volatile int *p
)
36 *recurse_1 (n
+ 1, p
) += n
;
45 return *recurse_1 (n
, &sum
);
52 char mystack
[SIGSTKSZ
];
54 struct sigaction action
;
56 /* Before starting the endless recursion, try to be friendly to the user's
57 machine. On some Linux 2.2.x systems, there is no stack limit for user
58 processes at all. We don't want to kill such systems. */
60 rl
.rlim_cur
= rl
.rlim_max
= 0x100000; /* 1 MB */
61 setrlimit (RLIMIT_STACK
, &rl
);
62 /* Install the alternate stack. */
63 altstack
.ss_sp
= mystack
;
64 altstack
.ss_size
= sizeof (mystack
);
65 altstack
.ss_flags
= 0; /* no SS_DISABLE */
66 if (sigaltstack (&altstack
, NULL
) < 0)
68 puts ("first sigaltstack failed");
71 /* Install the SIGSEGV handler. */
72 sigemptyset (&action
.sa_mask
);
73 action
.sa_handler
= &stackoverflow_handler
;
74 action
.sa_flags
= SA_ONSTACK
;
75 sigaction (SIGSEGV
, &action
, (struct sigaction
*) NULL
);
76 sigaction (SIGBUS
, &action
, (struct sigaction
*) NULL
);
78 /* Save the current signal mask. */
79 sigemptyset (&emptyset
);
80 sigprocmask (SIG_BLOCK
, &emptyset
, &mainsigset
);
82 /* Provoke two stack overflows in a row. */
83 if (sigsetjmp (mainloop
, 1) != 0)
86 printf ("%*sout of signal handler\n", pass
, "");
91 sigaltstack (NULL
, &altstack
);
92 if (altstack
.ss_flags
& SS_ONSTACK
)
93 printf ("%*son alternate stack\n", pass
, "");
95 printf ("%*snot on alternate stack\n", pass
, "");
100 puts ("recurse call returned");
104 altstack
.ss_flags
|= SS_DISABLE
;
105 if (sigaltstack (&altstack
, NULL
) == -1)
106 printf ("disabling alternate stack failed\n");
108 printf ("disabling alternate stack succeeded \n");
113 #define TEST_FUNCTION do_test ()
114 #include "../test-skeleton.c"