1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2004 Hewlett-Packard Co
3 	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23 
24 /* Test to verify that we can siglongjmp() into a frame whose register
25    window is not backed by valid memory.  */
26 
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <setjmp.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <sys/mman.h>
41 
42 #ifdef HAVE_IA64INTRIN_H
43 # include <ia64intrin.h>
44 #endif
45 
46 static sigjmp_buf env;
47 static int return_level;
48 static uintptr_t return_bsp;
49 static int verbose;
50 
51 uintptr_t
get_bsp(void)52 get_bsp (void)
53 {
54 #ifdef __INTEL_COMPILER
55   return __getReg (_IA64_REG_AR_BSP);
56 #else
57   return (uintptr_t) __builtin_ia64_bsp ();
58 #endif
59 }
60 
61 static void
sighandler(int signal,void * siginfo,void * sigcontext)62 sighandler (int signal, void *siginfo, void *sigcontext)
63 {
64   ucontext_t *uc = sigcontext;
65   int local = 0;
66 
67   if (verbose)
68     printf ("got signal, stack at %p, saved bsp=0x%lx\n",
69 	    &local, uc->uc_mcontext.sc_ar_bsp);
70   siglongjmp (env, 1);
71 }
72 
73 /* Direct call of doit () at the end of doit () would get optimized by GCC to
74    a branch.  */
75 static void doit (int n);
76 typedef void (*doit_type) (int);
77 static volatile doit_type doit_pointer = doit;
78 
79 static void
doit(int n)80 doit (int n)
81 {
82   uintptr_t guard_page_addr, bsp = get_bsp ();
83   void *ret;
84 
85   if (n == 0)
86     {
87       size_t page_size = getpagesize ();
88 
89       guard_page_addr = (bsp + page_size - 1) & -page_size;
90       if (verbose)
91 	printf ("guard_page_addr = 0x%lx\n", (unsigned long) guard_page_addr);
92       ret = mmap ((void *) guard_page_addr, page_size, PROT_NONE,
93 		  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
94       if (ret != (void *) guard_page_addr)
95 	{
96 	  if (ret == MAP_FAILED)
97 	    perror ("mmap");
98 	  else
99 	    fprintf (stderr, "mmap() returned %p, expected 0x%lx\n",
100 		     ret, guard_page_addr);
101 	  exit (EXIT_FAILURE);
102 	}
103     }
104 
105   if (sigsetjmp (env, 1))
106     {
107       return_level = n;
108       return_bsp = bsp;
109     }
110   else
111     (*doit_pointer) (n + 1);
112 }
113 
114 int
main(int argc,char ** argv)115 main (int argc, char **argv)
116 {
117   struct sigaction sa;
118   stack_t ss;
119 
120   if (argc > 1)
121     verbose = 1;
122 
123   ss.ss_sp = malloc (2 * SIGSTKSZ);
124   if (ss.ss_sp == NULL)
125     {
126       puts ("failed to allocate alternate stack");
127       return EXIT_FAILURE;
128     }
129   ss.ss_flags = 0;
130   ss.ss_size = 2 * SIGSTKSZ;
131   if (sigaltstack (&ss, NULL) < 0)
132     {
133       printf ("sigaltstack failed: %s\n", strerror (errno));
134       return EXIT_FAILURE;
135     }
136 
137   sa.sa_handler = (void (*) (int)) sighandler;
138   sigemptyset (&sa.sa_mask);
139   sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
140   if (sigaction (SIGSEGV, &sa, NULL) < 0)
141     {
142       printf ("sigaction failed: %s\n", strerror (errno));
143       exit (1);
144     }
145 
146   doit (0);
147 
148   if (verbose)
149     {
150       printf ("sigsetjmp returned at level %d bsp=0x%lx\n",
151 	      return_level, return_bsp);
152       puts ("Test succeeded!");
153     }
154   return EXIT_SUCCESS;
155 }
156