• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2019 Brock York <twunknown AT gmail.com>
3 
4 This file is part of libunwind.
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <execinfo.h>
30 #include <sys/types.h>
31 #include <sys/ucontext.h>
32 #include <unistd.h>
33 
34 #ifdef HAVE_SYS_PTRACE_H
35 #include <sys/ptrace.h>
36 #endif
37 
38 #define UNW_LOCAL_ONLY
39 #include <libunwind.h>
40 
41 /*
42  * unwind in the signal handler checking the backtrace is correct
43  * after a bad jump.
44  */
handle_sigsegv(int signal,siginfo_t * info,void * ucontext)45 void handle_sigsegv(int signal, siginfo_t *info, void *ucontext)
46 {
47   /*
48    * 0 = success
49    * !0 = general failure
50    * 77 = test skipped
51    * 99 = complete failure
52    */
53   int test_status = 0;
54   unw_cursor_t cursor; unw_context_t uc;
55   unw_word_t ip, sp, offset;
56   char name[1000];
57   int found_signal_frame = 0;
58   int i = 0;
59   char *names[] = {
60     "",
61     "main",
62   };
63   int names_count = sizeof(names) / sizeof(*names);
64 
65   unw_getcontext(&uc);
66   unw_init_local(&cursor, &uc);
67 
68   while (unw_step(&cursor) > 0 && !test_status)
69     {
70       if (unw_is_signal_frame(&cursor))
71         {
72           found_signal_frame = 1;
73         }
74       if (found_signal_frame)
75         {
76           unw_get_reg(&cursor, UNW_REG_IP, &ip);
77           unw_get_reg(&cursor, UNW_REG_SP, &sp);
78           memset(name, 0, sizeof(char) * 1000);
79           unw_get_proc_name(&cursor, name, sizeof(char) * 1000, &offset);
80           printf("ip = %lx, sp = %lx offset = %lx name = %s\n", (long) ip, (long) sp, (long) offset, name);
81           if (i < names_count)
82             {
83               if (strcmp(names[i], name) != 0)
84                 {
85                   test_status = 1;
86                   printf("frame %s doesn't match expected frame %s\n", name, names[i]);
87                 }
88               else
89                 {
90                   i += 1;
91                 }
92             }
93         }
94     }
95 
96   if (i != names_count) //Make sure we found all the frames!
97     {
98       printf("Failed to find all frames i:%d != names_count:%d\n", i, names_count);
99       test_status = 1;
100     }
101 
102   /*return test_status to test harness*/
103   exit(test_status);
104 }
105 
106 void (*invalid_function)() = (void*)1;
107 
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110   struct sigaction sa;
111   memset(&sa, 0, sizeof(sa));
112   sa.sa_sigaction = handle_sigsegv;
113   sa.sa_flags = SA_SIGINFO;
114   sigaction(SIGSEGV, &sa, NULL);
115 
116   invalid_function();
117 
118   /*
119    * 99 is the hard error exit status for automake tests:
120    * https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html#Scripts_002dbased-Testsuites
121    * If we dont end up in the signal handler something went horribly wrong.
122    */
123   return 99;
124 }
125