1 /* $OpenBSD: t_backtrace.c,v 1.2 2021/12/13 18:04:28 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 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/types.h> 33 34 #include <atf-c.h> 35 #include <string.h> 36 #include <stdbool.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <execinfo.h> 40 #include <unistd.h> 41 42 #ifndef __arraycount 43 #define __arraycount(a) (sizeof(a) / sizeof(a[0])) 44 #endif 45 46 void myfunc3(size_t ncalls); 47 void myfunc2(size_t ncalls); 48 void myfunc1(size_t origcalls, volatile size_t ncalls); 49 void myfunc(size_t ncalls); 50 51 static volatile int prevent_inline; 52 53 void 54 myfunc3(size_t ncalls) 55 { 56 static const struct { 57 const char *name; 58 bool is_optional; 59 } frames[] = { 60 { "myfunc", false }, 61 { "atf_body_backtrace_fmt_basic", false }, 62 { "atf_test", true }, 63 { "main", true }, 64 { "_start", true }, 65 }; 66 size_t j, nptrs, min_frames, max_frames; 67 void *buffer[ncalls + 10]; 68 char **strings; 69 70 min_frames = 0; 71 max_frames = 0; 72 for (j = 0; j < __arraycount(frames); ++j) { 73 if (!frames[j].is_optional) 74 ++min_frames; 75 ++max_frames; 76 } 77 nptrs = backtrace(buffer, __arraycount(buffer)); 78 ATF_REQUIRE(nptrs != (size_t)-1); 79 strings = backtrace_symbols_fmt(buffer, nptrs, "%n"); 80 81 ATF_CHECK(strings != NULL); 82 83 printf("got nptrs=%zu ncalls=%zu (min_frames: %zu, max_frames: %zu)\n", 84 nptrs, ncalls, min_frames, max_frames); 85 printf("backtrace is:\n"); 86 for (j = 0; j < nptrs; j++) { 87 printf("#%zu: %s\n", j, strings[j]); 88 } 89 90 ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames); 91 ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames); 92 ATF_CHECK_STREQ(strings[0], "myfunc3"); 93 ATF_CHECK_STREQ(strings[1], "myfunc2"); 94 95 for (j = 2; j < ncalls + 2; j++) 96 ATF_CHECK_STREQ(strings[j], "myfunc1"); 97 98 for (size_t i = 0; j < nptrs; i++, j++) { 99 if (frames[i].is_optional && 100 strcmp(strings[j], frames[i].name)) { 101 --i; 102 continue; 103 } 104 ATF_CHECK_STREQ(strings[j], frames[i].name); 105 } 106 107 free(strings); 108 109 if (prevent_inline) 110 vfork(); 111 } 112 113 void 114 myfunc2(size_t ncalls) 115 { 116 myfunc3(ncalls); 117 118 if (prevent_inline) 119 vfork(); 120 } 121 122 void 123 myfunc1(size_t origcalls, volatile size_t ncalls) 124 { 125 if (ncalls > 1) 126 myfunc1(origcalls, ncalls - 1); 127 else 128 myfunc2(origcalls); 129 130 if (prevent_inline) 131 vfork(); 132 } 133 134 void 135 myfunc(size_t ncalls) 136 { 137 myfunc1(ncalls, ncalls); 138 139 if (prevent_inline) 140 vfork(); 141 } 142 143 ATF_TC(backtrace_fmt_basic); 144 ATF_TC_HEAD(backtrace_fmt_basic, tc) 145 { 146 atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)"); 147 atf_tc_set_md_var(tc, "require.files", "/proc/self"); 148 } 149 150 ATF_TC_BODY(backtrace_fmt_basic, tc) 151 { 152 myfunc(12); 153 154 if (prevent_inline) 155 vfork(); 156 } 157 158 ATF_TP_ADD_TCS(tp) 159 { 160 161 ATF_TP_ADD_TC(tp, backtrace_fmt_basic); 162 163 return atf_no_error(); 164 } 165