• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-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 /*  Verify that unw_resume() restores the signal mask at proper time.  */
25 
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 
30 #include "compiler.h"
31 
32 #include <libunwind.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39 
40 #ifdef HAVE_IA64INTRIN_H
41 # include <ia64intrin.h>
42 #endif
43 
44 #define panic(args...)						\
45 	do { fprintf (stderr, args); ++nerrors; } while (0)
46 
47 int verbose;
48 int nerrors;
49 int got_usr1, got_usr2;
50 char *sigusr1_sp;
51 
52 uintptr_t
get_bsp(void)53 get_bsp (void)
54 {
55 #if UNW_TARGET_IA64
56 # ifdef __INTEL_COMPILER
57   return __getReg (_IA64_REG_AR_BSP);
58 # else
59   return (uintptr_t) __builtin_ia64_bsp ();
60 # endif
61 #else
62   return 0;
63 #endif
64 }
65 
66 #ifdef TEST_WITH_SIGINFO
67 void
handler(int sig,siginfo_t * si UNUSED,void * ucontext UNUSED)68 handler (int sig,
69          siginfo_t *si UNUSED,
70          void *ucontext UNUSED)
71 #else
72 void
73 handler (int sig)
74 #endif
75 {
76   unw_word_t ip;
77   sigset_t mask;
78   unw_context_t uc;
79   unw_cursor_t c;
80   char foo;
81   int ret;
82   // The test rely on SIGUSR2 mask to be cleared when the handler returns.
83   // For local context from the signal handler, there doesn't seem to be a way
84   // currently to set it so just clear the whole struct to make sure the signal mask is cleared.
85   // This should probably be fixed to avoid signal mask being set to random values
86   // by `unw_resume` if the context was not pre-zeroed.,
87   // Using the signal ucontext direction should also work automatically but currently doesn't
88   // on ARM/AArch64 (or any other archs that doesn't have a proper sigreturn implementation)
89   memset(&uc, 0x0, sizeof(uc));
90 
91 #if UNW_TARGET_IA64
92   if (verbose)
93     printf ("bsp = %llx\n", (unsigned long long) get_bsp ());
94 #endif
95 
96   if (verbose)
97     printf ("got signal %d\n", sig);
98 
99   if (sig == SIGUSR1)
100     {
101       ++got_usr1;
102       sigusr1_sp = &foo;
103 
104       sigemptyset (&mask);
105       sigaddset (&mask, SIGUSR2);
106       sigprocmask (SIG_BLOCK, &mask, NULL);
107       kill (getpid (), SIGUSR2);	/* pend SIGUSR2 */
108 
109       signal (SIGUSR1, SIG_IGN);
110 
111       if ((ret = unw_getcontext (&uc)) < 0)
112 	panic ("unw_getcontext() failed: ret=%d\n", ret);
113       if ((ret = unw_init_local (&c, &uc)) < 0)
114 	panic ("unw_init_local() failed: ret=%d\n", ret);
115 
116       if ((ret = unw_step (&c)) < 0)		/* step to signal trampoline */
117 	panic ("unw_step(1) failed: ret=%d\n", ret);
118 
119       if ((ret = unw_step (&c)) < 0)		/* step to kill() */
120 	panic ("unw_step(2) failed: ret=%d\n", ret);
121 
122 #if defined(UNW_TARGET_TILEGX)
123       if ((ret = unw_step (&c)) < 0)		/* step to signal trampoline */
124 	panic ("unw_step(2) failed: ret=%d\n", ret);
125 #endif
126 
127       if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0)
128 	panic ("unw_get_reg(IP) failed: ret=%d\n", ret);
129       if (verbose)
130 	printf ("resuming at 0x%lx, with SIGUSR2 pending\n",
131 		(unsigned long) ip);
132       unw_resume (&c);
133     }
134   else if (sig == SIGUSR2)
135     {
136       ++got_usr2;
137       if (got_usr1)
138 	{
139 	  if (verbose)
140 	    printf ("OK: stack still at %p\n", &foo);
141 	}
142       signal (SIGUSR2, SIG_IGN);
143     }
144   else
145     panic ("Got unexpected signal %d\n", sig);
146 }
147 
148 int
main(int argc,char ** argv UNUSED)149 main (int argc, char **argv UNUSED)
150 {
151   struct sigaction sa;
152   float d = 1.0;
153   int n = 0;
154 
155   if (argc > 1)
156     verbose = 1;
157 
158   memset (&sa, 0, sizeof(sa));
159 #ifdef TEST_WITH_SIGINFO
160   sa.sa_sigaction = handler;
161   sa.sa_flags = SA_SIGINFO;
162 #else
163   sa.sa_handler = handler;
164 #endif
165 
166   if (sigaction (SIGUSR1, &sa, NULL) != 0 ||
167       sigaction (SIGUSR2, &sa, NULL) != 0)
168     {
169       fprintf (stderr, "sigaction() failed: %s\n", strerror (errno));
170       return -1;
171     }
172 
173   /* Use the FPU a bit; otherwise we get spurious errors should the
174      signal handler need to use the FPU for any reason.  This seems to
175      happen on x86-64.  */
176   while (d > 0.0)
177     {
178       d /= 2.0;
179       ++n;
180     }
181   if (n > 9999)
182     return -1;	/* can't happen, but don't tell the compiler... */
183 
184   if (verbose)
185     printf ("sending SIGUSR1\n");
186   kill (getpid (), SIGUSR1);
187 
188   if (!got_usr2)
189     panic ("failed to get SIGUSR2\n");
190 
191   if (nerrors)
192     {
193       fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
194       exit (-1);
195     }
196 
197   if (verbose)
198     printf ("SUCCESS\n");
199   return 0;
200 }
201