1#include <assert.h>
2#include <setjmp.h>
3#include <signal.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7// Regression test for bug 91162:  if a client had a SEGV signal handler,
8// and jumped to a bogus address, Valgrind would abort.  With the fix,
9// the following test runs to completion correctly.
10
11static jmp_buf myjmpbuf;
12
13static
14void SIGSEGV_handler(int signum)
15{
16   __builtin_longjmp(myjmpbuf, 1);
17}
18
19int main(void)
20{
21   struct sigaction sigsegv_new, sigsegv_saved;
22   int res;
23
24   /* Install own SIGSEGV handler */
25   sigsegv_new.sa_handler  = SIGSEGV_handler;
26   sigsegv_new.sa_flags    = 0;
27#if !defined(__APPLE__)
28   sigsegv_new.sa_restorer = NULL;
29#endif
30   res = sigemptyset( &sigsegv_new.sa_mask );
31   assert(res == 0);
32
33   res = sigaction( SIGSEGV, &sigsegv_new, &sigsegv_saved );
34   assert(res == 0);
35
36   if (__builtin_setjmp(myjmpbuf) == 0) {
37      // Jump to zero; will cause seg fault
38#if defined(__powerpc64__)
39      unsigned long int fn[3];
40      fn[0] = 0;
41      fn[1] = 0;
42      fn[2] = 0;
43#else
44      void (*fn)(void) = 0;
45#endif
46      ((void(*)(void)) fn) ();
47      fprintf(stderr, "Got here??\n");
48   } else  {
49      fprintf(stderr, "Signal caught, as expected\n");
50   }
51
52   return 0;
53}
54
55