summaryrefslogtreecommitdiff
path: root/tests/Gtest-resume-sig.c
blob: 18ec65da713440befd35d2d7a147c4651af4b2a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003-2004 Hewlett-Packard Co
	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

/*  Verify that unw_resume() restores the signal mask at proper time.  */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "compiler.h"

#include <libunwind.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#ifdef HAVE_IA64INTRIN_H
# include <ia64intrin.h>
#endif

#define panic(args...)						\
	do { fprintf (stderr, args); ++nerrors; } while (0)

int verbose;
int nerrors;
int got_usr1, got_usr2;
char *sigusr1_sp;

uintptr_t
get_bsp (void)
{
#if UNW_TARGET_IA64
# ifdef __INTEL_COMPILER
  return __getReg (_IA64_REG_AR_BSP);
# else
  return (uintptr_t) __builtin_ia64_bsp ();
# endif
#else
  return 0;
#endif
}

#ifdef TEST_WITH_SIGINFO
void
handler (int sig,
         siginfo_t *si UNUSED,
         void *ucontext UNUSED)
#else
void
handler (int sig)
#endif
{
  unw_word_t ip;
  sigset_t mask;
  unw_context_t uc;
  unw_cursor_t c;
  char foo;
  int ret;
  // The test rely on SIGUSR2 mask to be cleared when the handler returns.
  // For local context from the signal handler, there doesn't seem to be a way
  // currently to set it so just clear the whole struct to make sure the signal mask is cleared.
  // This should probably be fixed to avoid signal mask being set to random values
  // by `unw_resume` if the context was not pre-zeroed.,
  // Using the signal ucontext direction should also work automatically but currently doesn't
  // on ARM/AArch64 (or any other archs that doesn't have a proper sigreturn implementation)
  memset(&uc, 0x0, sizeof(uc));

#if UNW_TARGET_IA64
  if (verbose)
    printf ("bsp = %llx\n", (unsigned long long) get_bsp ());
#endif

  if (verbose)
    printf ("got signal %d\n", sig);

  if (sig == SIGUSR1)
    {
      ++got_usr1;
      sigusr1_sp = &foo;

      sigemptyset (&mask);
      sigaddset (&mask, SIGUSR2);
      sigprocmask (SIG_BLOCK, &mask, NULL);
      kill (getpid (), SIGUSR2);	/* pend SIGUSR2 */

      signal (SIGUSR1, SIG_IGN);

      if ((ret = unw_getcontext (&uc)) < 0)
	panic ("unw_getcontext() failed: ret=%d\n", ret);
      if ((ret = unw_init_local (&c, &uc)) < 0)
	panic ("unw_init_local() failed: ret=%d\n", ret);

      if ((ret = unw_step (&c)) < 0)		/* step to signal trampoline */
	panic ("unw_step(1) failed: ret=%d\n", ret);

      if ((ret = unw_step (&c)) < 0)		/* step to kill() */
	panic ("unw_step(2) failed: ret=%d\n", ret);

#if defined(UNW_TARGET_TILEGX)
      if ((ret = unw_step (&c)) < 0)		/* step to signal trampoline */
	panic ("unw_step(2) failed: ret=%d\n", ret);
#endif

      if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0)
	panic ("unw_get_reg(IP) failed: ret=%d\n", ret);
      if (verbose)
	printf ("resuming at 0x%lx, with SIGUSR2 pending\n",
		(unsigned long) ip);
      unw_resume (&c);
    }
  else if (sig == SIGUSR2)
    {
      ++got_usr2;
      if (got_usr1)
	{
	  if (verbose)
	    printf ("OK: stack still at %p\n", &foo);
	}
      signal (SIGUSR2, SIG_IGN);
    }
  else
    panic ("Got unexpected signal %d\n", sig);
}

int
main (int argc, char **argv UNUSED)
{
  struct sigaction sa;
  float d = 1.0;
  int n = 0;

  if (argc > 1)
    verbose = 1;

  memset (&sa, 0, sizeof(sa));
#ifdef TEST_WITH_SIGINFO
  sa.sa_sigaction = handler;
  sa.sa_flags = SA_SIGINFO;
#else
  sa.sa_handler = handler;
#endif

  if (sigaction (SIGUSR1, &sa, NULL) != 0 ||
      sigaction (SIGUSR2, &sa, NULL) != 0)
    {
      fprintf (stderr, "sigaction() failed: %s\n", strerror (errno));
      return -1;
    }

  /* Use the FPU a bit; otherwise we get spurious errors should the
     signal handler need to use the FPU for any reason.  This seems to
     happen on x86-64.  */
  while (d > 0.0)
    {
      d /= 2.0;
      ++n;
    }
  if (n > 9999)
    return -1;	/* can't happen, but don't tell the compiler... */

  if (verbose)
    printf ("sending SIGUSR1\n");
  kill (getpid (), SIGUSR1);

  if (!got_usr2)
    panic ("failed to get SIGUSR2\n");

  if (nerrors)
    {
      fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
      exit (-1);
    }

  if (verbose)
    printf ("SUCCESS\n");
  return 0;
}