1 1.8 riastrad /* $NetBSD: t_sig_backtrace.c,v 1.8 2025/04/17 14:18:40 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.8 riastrad __RCSID("$NetBSD: t_sig_backtrace.c,v 1.8 2025/04/17 14:18:40 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.6 riastrad the_loop_deref(int i0) 87 1.1 thorpej { 88 1.6 riastrad volatile int i = i0; 89 1.6 riastrad 90 1.1 thorpej while (*foo != 0) { 91 1.1 thorpej i = func3(i); 92 1.1 thorpej i = func1(i); 93 1.1 thorpej i = func2(i); 94 1.1 thorpej } 95 1.1 thorpej 96 1.2 thorpej #ifdef NOINLINE_HACK 97 1.2 thorpej if (noinline) 98 1.2 thorpej vfork(); 99 1.2 thorpej #endif 100 1.2 thorpej 101 1.1 thorpej return i; 102 1.1 thorpej } 103 1.1 thorpej 104 1.3 riastrad static int __noinline 105 1.6 riastrad the_loop_jump(int i0) 106 1.3 riastrad { 107 1.6 riastrad volatile int i = i0; 108 1.6 riastrad 109 1.3 riastrad while ((*bar)() != 0) { 110 1.3 riastrad i = func3(i); 111 1.3 riastrad i = func1(i); 112 1.3 riastrad i = func2(i); 113 1.3 riastrad } 114 1.3 riastrad 115 1.3 riastrad #ifdef NOINLINE_HACK 116 1.3 riastrad if (noinline) 117 1.3 riastrad vfork(); 118 1.3 riastrad #endif 119 1.3 riastrad 120 1.3 riastrad return i; 121 1.3 riastrad } 122 1.3 riastrad 123 1.1 thorpej jmp_buf env; 124 1.1 thorpej 125 1.1 thorpej static void 126 1.1 thorpej handler(int s) 127 1.1 thorpej { 128 1.1 thorpej printf("signal: %d\n", s); 129 1.1 thorpej 130 1.1 thorpej void *array[10]; 131 1.1 thorpej size_t size = backtrace(array, 10); 132 1.1 thorpej ATF_REQUIRE(size != 0); 133 1.1 thorpej 134 1.1 thorpej printf("Backtrace %zd stack frames.\n", size); 135 1.7 riastrad fflush(stdout); 136 1.1 thorpej backtrace_symbols_fd(array, size, STDOUT_FILENO); 137 1.1 thorpej 138 1.1 thorpej char **strings = backtrace_symbols_fmt(array, size, "%n"); 139 1.1 thorpej bool found_handler = false; 140 1.1 thorpej bool found_sigtramp = false; 141 1.1 thorpej bool found_the_loop = false; 142 1.1 thorpej bool found_main = false; 143 1.1 thorpej size_t i; 144 1.1 thorpej 145 1.1 thorpej /* 146 1.1 thorpej * We must find the symbols in the following order: 147 1.1 thorpej * 148 1.1 thorpej * handler -> __sigtramp_siginfo_* -> the_loop -> main 149 1.1 thorpej */ 150 1.1 thorpej for (i = 0; i < size; i++) { 151 1.1 thorpej if (!found_handler && 152 1.1 thorpej strcmp(strings[i], "handler") == 0) { 153 1.1 thorpej found_handler = true; 154 1.1 thorpej continue; 155 1.1 thorpej } 156 1.1 thorpej if (found_handler && !found_sigtramp && 157 1.1 thorpej strncmp(strings[i], "__sigtramp_siginfo_", 158 1.1 thorpej strlen("__sigtramp_siginfo_")) == 0) { 159 1.1 thorpej found_sigtramp = true; 160 1.1 thorpej continue; 161 1.1 thorpej } 162 1.1 thorpej if (found_sigtramp && !found_the_loop && 163 1.4 riastrad strncmp(strings[i], "the_loop", strlen("the_loop")) == 0) { 164 1.1 thorpej found_the_loop = true; 165 1.1 thorpej continue; 166 1.1 thorpej } 167 1.1 thorpej if (found_the_loop && !found_main && 168 1.1 thorpej strcmp(strings[i], "atf_tp_main") == 0) { 169 1.1 thorpej found_main = true; 170 1.1 thorpej break; 171 1.1 thorpej } 172 1.1 thorpej } 173 1.1 thorpej 174 1.8 riastrad ATF_CHECK(found_handler); 175 1.8 riastrad ATF_CHECK(found_sigtramp); 176 1.8 riastrad ATF_CHECK(found_the_loop); 177 1.8 riastrad ATF_CHECK(found_main); 178 1.1 thorpej 179 1.1 thorpej longjmp(env, 1); 180 1.1 thorpej } 181 1.1 thorpej 182 1.3 riastrad ATF_TC(sig_backtrace_deref); 183 1.3 riastrad ATF_TC_HEAD(sig_backtrace_deref, tc) 184 1.3 riastrad { 185 1.3 riastrad atf_tc_set_md_var(tc, "descr", 186 1.3 riastrad "Test backtrace(3) across signal handlers, null pointer deref"); 187 1.3 riastrad } 188 1.3 riastrad 189 1.3 riastrad ATF_TC_BODY(sig_backtrace_deref, tc) 190 1.3 riastrad { 191 1.3 riastrad sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, 192 1.3 riastrad MAP_ANON | MAP_STACK, -1, 0); 193 1.3 riastrad ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED); 194 1.3 riastrad 195 1.3 riastrad sig_stack.ss_size = SIGSTKSZ; 196 1.3 riastrad sig_stack.ss_flags = 0; 197 1.3 riastrad ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0); 198 1.3 riastrad 199 1.3 riastrad struct sigaction sa = { 200 1.3 riastrad .sa_handler = handler, 201 1.3 riastrad .sa_flags = SA_ONSTACK, 202 1.3 riastrad }; 203 1.3 riastrad ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0); 204 1.3 riastrad 205 1.8 riastrad #ifdef __sparc__ /* 32 or 64 */ 206 1.8 riastrad atf_tc_expect_fail("PR port-sparc64/59313:" 207 1.8 riastrad " t_sig_backtrace tests are failing"); 208 1.8 riastrad #endif 209 1.8 riastrad 210 1.3 riastrad if (setjmp(env) == 0) { 211 1.3 riastrad printf("%d\n", the_loop_deref(0)); 212 1.3 riastrad } 213 1.3 riastrad } 214 1.3 riastrad 215 1.3 riastrad ATF_TC(sig_backtrace_jump); 216 1.3 riastrad ATF_TC_HEAD(sig_backtrace_jump, tc) 217 1.1 thorpej { 218 1.1 thorpej atf_tc_set_md_var(tc, "descr", 219 1.3 riastrad "Test backtrace(3) across signal handlers, null pointer jump"); 220 1.1 thorpej } 221 1.1 thorpej 222 1.3 riastrad ATF_TC_BODY(sig_backtrace_jump, tc) 223 1.1 thorpej { 224 1.1 thorpej sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, 225 1.1 thorpej MAP_ANON | MAP_STACK, -1, 0); 226 1.1 thorpej ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED); 227 1.1 thorpej 228 1.1 thorpej sig_stack.ss_size = SIGSTKSZ; 229 1.1 thorpej sig_stack.ss_flags = 0; 230 1.1 thorpej ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0); 231 1.1 thorpej 232 1.1 thorpej struct sigaction sa = { 233 1.1 thorpej .sa_handler = handler, 234 1.1 thorpej .sa_flags = SA_ONSTACK, 235 1.1 thorpej }; 236 1.1 thorpej ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0); 237 1.1 thorpej 238 1.5 riastrad atf_tc_expect_fail("PR lib/56940"); 239 1.5 riastrad 240 1.1 thorpej if (setjmp(env) == 0) { 241 1.3 riastrad printf("%d\n", the_loop_jump(0)); 242 1.1 thorpej } 243 1.1 thorpej } 244 1.1 thorpej 245 1.1 thorpej ATF_TP_ADD_TCS(tp) 246 1.1 thorpej { 247 1.3 riastrad ATF_TP_ADD_TC(tp, sig_backtrace_deref); 248 1.3 riastrad ATF_TP_ADD_TC(tp, sig_backtrace_jump); 249 1.1 thorpej 250 1.1 thorpej return atf_no_error(); 251 1.1 thorpej } 252