1 /* Verify longjmp fortify checking does not reject signal stacks.
3 Test case mostly written by Paolo Bonzini <pbonzini@redhat.com>. */
10 #include <sys/types.h>
12 #include <sys/resource.h>
15 static int do_test (void);
16 #define TEST_FUNCTION do_test ()
17 #include "../test-skeleton.c"
19 static jmp_buf mainloop
;
20 static sigset_t mainsigset
;
21 static volatile sig_atomic_t pass
;
24 write_indented (const char *str
)
26 for (int i
= 0; i
< pass
; ++i
)
32 stackoverflow_handler (int sig
)
35 /* Sanity check to keep test from looping forever (in case the longjmp
36 chk code is slightly broken). */
38 sigaltstack (NULL
, &altstack
);
39 write_indented ("in signal handler\n");
40 if (altstack
.ss_flags
& SS_ONSTACK
)
41 write_indented ("on alternate stack\n");
42 siglongjmp (mainloop
, pass
);
47 recurse_1 (int n
, volatile int *p
)
50 *recurse_1 (n
+ 1, p
) += n
;
59 return *recurse_1 (n
, &sum
);
66 char mystack
[SIGSTKSZ
];
68 struct sigaction action
;
70 /* Before starting the endless recursion, try to be friendly to the user's
71 machine. On some Linux 2.2.x systems, there is no stack limit for user
72 processes at all. We don't want to kill such systems. */
74 rl
.rlim_cur
= rl
.rlim_max
= 0x100000; /* 1 MB */
75 setrlimit (RLIMIT_STACK
, &rl
);
76 /* Install the alternate stack. */
77 altstack
.ss_sp
= mystack
;
78 altstack
.ss_size
= sizeof (mystack
);
79 altstack
.ss_flags
= 0; /* no SS_DISABLE */
80 if (sigaltstack (&altstack
, NULL
) < 0)
82 puts ("first sigaltstack failed");
85 /* Install the SIGSEGV handler. */
86 sigemptyset (&action
.sa_mask
);
87 action
.sa_handler
= &stackoverflow_handler
;
88 action
.sa_flags
= SA_ONSTACK
;
89 sigaction (SIGSEGV
, &action
, (struct sigaction
*) NULL
);
90 sigaction (SIGBUS
, &action
, (struct sigaction
*) NULL
);
92 /* Save the current signal mask. */
93 sigemptyset (&emptyset
);
94 sigprocmask (SIG_BLOCK
, &emptyset
, &mainsigset
);
96 /* Provoke two stack overflows in a row. */
97 if (sigsetjmp (mainloop
, 1) != 0)
100 printf ("%*sout of signal handler\n", pass
, "");
105 sigaltstack (NULL
, &altstack
);
106 if (altstack
.ss_flags
& SS_ONSTACK
)
107 printf ("%*son alternate stack\n", pass
, "");
109 printf ("%*snot on alternate stack\n", pass
, "");
114 puts ("recurse call returned");
118 altstack
.ss_flags
|= SS_DISABLE
;
119 if (sigaltstack (&altstack
, NULL
) == -1)
120 printf ("disabling alternate stack failed\n");
122 printf ("disabling alternate stack succeeded \n");
124 /* Restore the signal handlers, in case we trigger a crash after the
126 signal (SIGBUS
, SIG_DFL
);
127 signal (SIGSEGV
, SIG_DFL
);