1 /* $NetBSD: t_sig_backtrace.c,v 1.2 2021/11/23 23:29:55 thorpej 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.2 2021/11/23 23:29:55 thorpej 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 52 static int the_loop(int); 53 54 #ifdef NOINLINE_HACK 55 volatile int noinline; 56 #endif 57 58 static int __noinline 59 func1(int i) 60 { 61 if (i > 100) { 62 return the_loop(i); 63 } 64 return i + 1; 65 } 66 67 static int __noinline 68 func2(int i) 69 { 70 return func1(i) << 1; 71 } 72 73 static int __noinline 74 func3(int i) 75 { 76 if (func1(i) < 10) { 77 return func2(i); 78 } else { 79 return func1(i); 80 } 81 } 82 83 static int __noinline 84 the_loop(int i) 85 { 86 while (*foo != 0) { 87 i = func3(i); 88 i = func1(i); 89 i = func2(i); 90 } 91 92 #ifdef NOINLINE_HACK 93 if (noinline) 94 vfork(); 95 #endif 96 97 return i; 98 } 99 100 jmp_buf env; 101 102 static void 103 handler(int s) 104 { 105 printf("signal: %d\n", s); 106 107 void *array[10]; 108 size_t size = backtrace(array, 10); 109 ATF_REQUIRE(size != 0); 110 111 printf("Backtrace %zd stack frames.\n", size); 112 backtrace_symbols_fd(array, size, STDOUT_FILENO); 113 114 char **strings = backtrace_symbols_fmt(array, size, "%n"); 115 bool found_handler = false; 116 bool found_sigtramp = false; 117 bool found_the_loop = false; 118 bool found_main = false; 119 size_t i; 120 121 /* 122 * We must find the symbols in the following order: 123 * 124 * handler -> __sigtramp_siginfo_* -> the_loop -> main 125 */ 126 for (i = 0; i < size; i++) { 127 if (!found_handler && 128 strcmp(strings[i], "handler") == 0) { 129 found_handler = true; 130 continue; 131 } 132 if (found_handler && !found_sigtramp && 133 strncmp(strings[i], "__sigtramp_siginfo_", 134 strlen("__sigtramp_siginfo_")) == 0) { 135 found_sigtramp = true; 136 continue; 137 } 138 if (found_sigtramp && !found_the_loop && 139 strcmp(strings[i], "the_loop") == 0) { 140 found_the_loop = true; 141 continue; 142 } 143 if (found_the_loop && !found_main && 144 strcmp(strings[i], "atf_tp_main") == 0) { 145 found_main = true; 146 break; 147 } 148 } 149 150 ATF_REQUIRE(found_handler); 151 ATF_REQUIRE(found_sigtramp); 152 ATF_REQUIRE(found_the_loop); 153 ATF_REQUIRE(found_main); 154 155 longjmp(env, 1); 156 } 157 158 ATF_TC(sig_backtrace); 159 ATF_TC_HEAD(sig_backtrace, tc) 160 { 161 atf_tc_set_md_var(tc, "descr", 162 "Test backtrace(3) across signal handlers"); 163 } 164 165 ATF_TC_BODY(sig_backtrace, tc) 166 { 167 sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, 168 MAP_ANON | MAP_STACK, -1, 0); 169 ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED); 170 171 sig_stack.ss_size = SIGSTKSZ; 172 sig_stack.ss_flags = 0; 173 ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0); 174 175 struct sigaction sa = { 176 .sa_handler = handler, 177 .sa_flags = SA_ONSTACK, 178 }; 179 ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0); 180 181 if (setjmp(env) == 0) { 182 printf("%d\n", the_loop(0)); 183 } 184 } 185 186 ATF_TP_ADD_TCS(tp) 187 { 188 ATF_TP_ADD_TC(tp, sig_backtrace); 189 190 return atf_no_error(); 191 } 192