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