1 /*        $NetBSD: t_sig_backtrace.c,v 1.8 2025/04/17 14:18:40 riastradh Exp $  */
2 
3 /*-
4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: t_sig_backtrace.c,v 1.8 2025/04/17 14:18:40 riastradh Exp $");
34 
35 #include <sys/mman.h>
36 #include <execinfo.h>
37 #include <setjmp.h>
38 #include <stdbool.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <atf-c.h>
47 
48 stack_t sig_stack;
49 
50 char *foo;
51 char *(*bar)(void);
52 
53 static int the_loop_deref(int);
54 static int the_loop_jump(int);
55 
56 #ifdef NOINLINE_HACK
57 volatile int noinline;
58 #endif
59 
60 static int __noinline
func1(int i)61 func1(int i)
62 {
63           if (i > 100) {
64                     return the_loop_deref(i);
65           }
66           return i + 1;
67 }
68 
69 static int __noinline
func2(int i)70 func2(int i)
71 {
72           return func1(i) << 1;
73 }
74 
75 static int __noinline
func3(int i)76 func3(int i)
77 {
78           if (func1(i) < 10) {
79                     return func2(i);
80           } else {
81                     return func1(i);
82           }
83 }
84 
85 static int __noinline
the_loop_deref(int i0)86 the_loop_deref(int i0)
87 {
88           volatile int i = i0;
89 
90           while (*foo != 0) {
91                     i = func3(i);
92                     i = func1(i);
93                     i = func2(i);
94           }
95 
96 #ifdef NOINLINE_HACK
97           if (noinline)
98                     vfork();
99 #endif
100 
101           return i;
102 }
103 
104 static int __noinline
the_loop_jump(int i0)105 the_loop_jump(int i0)
106 {
107           volatile int i = i0;
108 
109           while ((*bar)() != 0) {
110                     i = func3(i);
111                     i = func1(i);
112                     i = func2(i);
113           }
114 
115 #ifdef NOINLINE_HACK
116           if (noinline)
117                     vfork();
118 #endif
119 
120           return i;
121 }
122 
123 jmp_buf env;
124 
125 static void
handler(int s)126 handler(int s)
127 {
128           printf("signal: %d\n", s);
129 
130           void *array[10];
131           size_t size = backtrace(array, 10);
132           ATF_REQUIRE(size != 0);
133 
134           printf("Backtrace %zd stack frames.\n", size);
135           fflush(stdout);
136           backtrace_symbols_fd(array, size, STDOUT_FILENO);
137 
138           char **strings = backtrace_symbols_fmt(array, size, "%n");
139           bool found_handler = false;
140           bool found_sigtramp = false;
141           bool found_the_loop = false;
142           bool found_main = false;
143           size_t i;
144 
145           /*
146            * We must find the symbols in the following order:
147            *
148            * handler -> __sigtramp_siginfo_* -> the_loop -> main
149            */
150           for (i = 0; i < size; i++) {
151                     if (!found_handler &&
152                         strcmp(strings[i], "handler") == 0) {
153                               found_handler = true;
154                               continue;
155                     }
156                     if (found_handler && !found_sigtramp &&
157                         strncmp(strings[i], "__sigtramp_siginfo_",
158                                   strlen("__sigtramp_siginfo_")) == 0) {
159                               found_sigtramp = true;
160                               continue;
161                     }
162                     if (found_sigtramp && !found_the_loop &&
163                         strncmp(strings[i], "the_loop", strlen("the_loop")) == 0) {
164                               found_the_loop = true;
165                               continue;
166                     }
167                     if (found_the_loop && !found_main &&
168                         strcmp(strings[i], "atf_tp_main") == 0) {
169                               found_main = true;
170                               break;
171                     }
172           }
173 
174           ATF_CHECK(found_handler);
175           ATF_CHECK(found_sigtramp);
176           ATF_CHECK(found_the_loop);
177           ATF_CHECK(found_main);
178 
179           longjmp(env, 1);
180 }
181 
182 ATF_TC(sig_backtrace_deref);
ATF_TC_HEAD(sig_backtrace_deref,tc)183 ATF_TC_HEAD(sig_backtrace_deref, tc)
184 {
185           atf_tc_set_md_var(tc, "descr",
186               "Test backtrace(3) across signal handlers, null pointer deref");
187 }
188 
ATF_TC_BODY(sig_backtrace_deref,tc)189 ATF_TC_BODY(sig_backtrace_deref, tc)
190 {
191           sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
192               MAP_ANON | MAP_STACK, -1, 0);
193           ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED);
194 
195           sig_stack.ss_size = SIGSTKSZ;
196           sig_stack.ss_flags = 0;
197           ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0);
198 
199           struct sigaction sa = {
200                     .sa_handler = handler,
201                     .sa_flags = SA_ONSTACK,
202           };
203           ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0);
204 
205 #ifdef __sparc__              /* 32 or 64 */
206           atf_tc_expect_fail("PR port-sparc64/59313:"
207               " t_sig_backtrace tests are failing");
208 #endif
209 
210           if (setjmp(env) == 0) {
211                     printf("%d\n", the_loop_deref(0));
212           }
213 }
214 
215 ATF_TC(sig_backtrace_jump);
ATF_TC_HEAD(sig_backtrace_jump,tc)216 ATF_TC_HEAD(sig_backtrace_jump, tc)
217 {
218           atf_tc_set_md_var(tc, "descr",
219               "Test backtrace(3) across signal handlers, null pointer jump");
220 }
221 
ATF_TC_BODY(sig_backtrace_jump,tc)222 ATF_TC_BODY(sig_backtrace_jump, tc)
223 {
224           sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
225               MAP_ANON | MAP_STACK, -1, 0);
226           ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED);
227 
228           sig_stack.ss_size = SIGSTKSZ;
229           sig_stack.ss_flags = 0;
230           ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0);
231 
232           struct sigaction sa = {
233                     .sa_handler = handler,
234                     .sa_flags = SA_ONSTACK,
235           };
236           ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0);
237 
238           atf_tc_expect_fail("PR lib/56940");
239 
240           if (setjmp(env) == 0) {
241                     printf("%d\n", the_loop_jump(0));
242           }
243 }
244 
ATF_TP_ADD_TCS(tp)245 ATF_TP_ADD_TCS(tp)
246 {
247           ATF_TP_ADD_TC(tp, sig_backtrace_deref);
248           ATF_TP_ADD_TC(tp, sig_backtrace_jump);
249 
250           return atf_no_error();
251 }
252