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