Home | History | Annotate | Line # | Download | only in libexecinfo
t_backtrace_sandbox.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: t_backtrace_sandbox.c,v 1.2 2025/01/27 17:02:50 riastradh Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2025 Kyle Evans <kevans (at) FreeBSD.org>
      5  1.1  christos  *
      6  1.1  christos  * SPDX-License-Identifier: BSD-2-Clause
      7  1.1  christos  */
      8  1.1  christos #include <sys/cdefs.h>
      9  1.2  riastrad __RCSID("$NetBSD: t_backtrace_sandbox.c,v 1.2 2025/01/27 17:02:50 riastradh Exp $");
     10  1.1  christos 
     11  1.1  christos #include <sys/param.h>
     12  1.1  christos #ifdef __FreeBSD__
     13  1.1  christos #include <sys/capsicum.h>
     14  1.1  christos #define __arraycount(a) nitems(a)
     15  1.1  christos #endif
     16  1.1  christos 
     17  1.1  christos #include <execinfo.h>
     18  1.1  christos #include <signal.h>
     19  1.1  christos #include <stdio.h>
     20  1.1  christos #include <string.h>
     21  1.1  christos #include <unistd.h>
     22  1.1  christos 
     23  1.1  christos #include <atf-c.h>
     24  1.1  christos 
     25  1.1  christos #define	BT_FUNCTIONS		10
     26  1.1  christos 
     27  1.2  riastrad ATF_TC(backtrace_sandbox);
     28  1.1  christos ATF_TC_HEAD(backtrace_sandbox, tc)
     29  1.1  christos {
     30  1.1  christos         atf_tc_set_md_var(tc, "descr",
     31  1.1  christos 	    "Test backtrace_sandbox_init(3) in sandbox");
     32  1.1  christos #ifndef __FreeBSD__
     33  1.1  christos 	atf_tc_set_md_var(tc, "require.user", "root");
     34  1.1  christos #endif
     35  1.1  christos }
     36  1.1  christos 
     37  1.1  christos ATF_TC_BODY(backtrace_sandbox, tc)
     38  1.1  christos {
     39  1.1  christos 	void *addr[BT_FUNCTIONS];
     40  1.1  christos 	char **syms;
     41  1.1  christos 	size_t frames;
     42  1.1  christos 
     43  1.1  christos 	frames = backtrace(addr, __arraycount(addr));
     44  1.1  christos 	ATF_REQUIRE(frames > 0);
     45  1.1  christos 
     46  1.1  christos 	syms = backtrace_symbols_fmt(addr, frames, "%n");
     47  1.1  christos 	ATF_REQUIRE(strcmp(syms[0], "atfu_backtrace_sandbox_body") == 0);
     48  1.1  christos 
     49  1.1  christos 	backtrace_sandbox_init();
     50  1.1  christos #ifdef __FreeBSD__
     51  1.1  christos 	cap_enter();
     52  1.1  christos #else
     53  1.1  christos 	ATF_REQUIRE(chroot("/tmp") == 0);
     54  1.1  christos #endif
     55  1.1  christos 
     56  1.1  christos 	syms = backtrace_symbols_fmt(addr, frames, "%n");
     57  1.1  christos 	ATF_REQUIRE(strcmp(syms[0], "atfu_backtrace_sandbox_body") == 0);
     58  1.1  christos }
     59  1.1  christos 
     60  1.1  christos ATF_TP_ADD_TCS(tp)
     61  1.1  christos {
     62  1.1  christos 
     63  1.1  christos 	ATF_TP_ADD_TC(tp, backtrace_sandbox);
     64  1.1  christos 
     65  1.1  christos 	return atf_no_error();
     66  1.1  christos }
     67