Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/lib/libexecinfo Add a test case for backtrace(3) acros...
details: https://anonhg.NetBSD.org/src/rev/8b02ff7179b8
branches: trunk
changeset: 1026309:8b02ff7179b8
user: thorpej <thorpej%NetBSD.org@localhost>
date: Thu Nov 18 15:03:19 2021 +0000
description:
Add a test case for backtrace(3) across a signal handler.
diffstat:
distrib/sets/lists/debug/mi | 3 +-
distrib/sets/lists/tests/mi | 3 +-
tests/lib/libexecinfo/Makefile | 3 +-
tests/lib/libexecinfo/t_sig_backtrace.c | 182 ++++++++++++++++++++++++++++++++
4 files changed, 188 insertions(+), 3 deletions(-)
diffs (236 lines):
diff -r f121aa60d2b0 -r 8b02ff7179b8 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi Thu Nov 18 04:33:20 2021 +0000
+++ b/distrib/sets/lists/debug/mi Thu Nov 18 15:03:19 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.367 2021/10/23 18:46:26 thorpej Exp $
+# $NetBSD: mi,v 1.368 2021/11/18 15:03:19 thorpej Exp $
./etc/mtree/set.debug comp-sys-root
./usr/lib comp-sys-usr compatdir
./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile
@@ -2265,6 +2265,7 @@
./usr/libdata/debug/usr/tests/lib/libdes/t_des.debug tests-lib-debug debug,atf,compattestfile
./usr/libdata/debug/usr/tests/lib/libevent/h_event.debug tests-lib-debug debug,atf,compattestfile
./usr/libdata/debug/usr/tests/lib/libexecinfo/t_backtrace.debug tests-lib-debug debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/lib/libexecinfo/t_sig_backtrace.debug tests-lib-debug debug,atf,compattestfile
./usr/libdata/debug/usr/tests/lib/liblutok/c_gate_test.debug tests-lutok-debug debug,atf,kyua,compattestfile
./usr/libdata/debug/usr/tests/lib/liblutok/debug_test.debug tests-lutok-debug debug,atf,kyua,compattestfile
./usr/libdata/debug/usr/tests/lib/liblutok/exceptions_test.debug tests-lutok-debug debug,atf,kyua,compattestfile
diff -r f121aa60d2b0 -r 8b02ff7179b8 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi Thu Nov 18 04:33:20 2021 +0000
+++ b/distrib/sets/lists/tests/mi Thu Nov 18 15:03:19 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1165 2021/11/17 04:33:26 kre Exp $
+# $NetBSD: mi,v 1.1166 2021/11/18 15:03:19 thorpej Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -3776,6 +3776,7 @@
./usr/tests/lib/libexecinfo/Atffile tests-lib-tests compattestfile,atf
./usr/tests/lib/libexecinfo/Kyuafile tests-lib-tests compattestfile,atf,kyua
./usr/tests/lib/libexecinfo/t_backtrace tests-lib-tests compattestfile,atf
+./usr/tests/lib/libexecinfo/t_sig_backtrace tests-lib-tests compattestfile,atf
./usr/tests/lib/libi386 tests-lib-tests compattestfile,atf
./usr/tests/lib/libi386/Atffile tests-lib-tests compattestfile,atf
./usr/tests/lib/libi386/Kyuafile tests-lib-tests compattestfile,atf,kyua
diff -r f121aa60d2b0 -r 8b02ff7179b8 tests/lib/libexecinfo/Makefile
--- a/tests/lib/libexecinfo/Makefile Thu Nov 18 04:33:20 2021 +0000
+++ b/tests/lib/libexecinfo/Makefile Thu Nov 18 15:03:19 2021 +0000
@@ -1,10 +1,11 @@
-# $NetBSD: Makefile,v 1.6 2015/01/31 20:55:43 nakayama Exp $
+# $NetBSD: Makefile,v 1.7 2021/11/18 15:03:19 thorpej Exp $
.include <bsd.own.mk>
TESTSDIR= ${TESTSBASE}/lib/libexecinfo
TESTS_C+= t_backtrace
+TESTS_C+= t_sig_backtrace
STRIPFLAG=
LDADD+= -lexecinfo -lelf
diff -r f121aa60d2b0 -r 8b02ff7179b8 tests/lib/libexecinfo/t_sig_backtrace.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libexecinfo/t_sig_backtrace.c Thu Nov 18 15:03:19 2021 +0000
@@ -0,0 +1,182 @@
+/* $NetBSD: t_sig_backtrace.c,v 1.1 2021/11/18 15:03:19 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_sig_backtrace.c,v 1.1 2021/11/18 15:03:19 thorpej Exp $");
+
+#include <sys/mman.h>
+#include <execinfo.h>
+#include <setjmp.h>
+#include <stdbool.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+stack_t sig_stack;
+
+char *foo;
+
+static int the_loop(int);
+
+static int __noinline
+func1(int i)
+{
+ if (i > 100) {
+ return the_loop(i);
+ }
+ return i + 1;
+}
+
+static int __noinline
+func2(int i)
+{
+ return func1(i) << 1;
+}
+
+static int __noinline
+func3(int i)
+{
+ if (func1(i) < 10) {
+ return func2(i);
+ } else {
+ return func1(i);
+ }
+}
+
+static int __noinline
+the_loop(int i)
+{
+ while (*foo != 0) {
+ i = func3(i);
+ i = func1(i);
+ i = func2(i);
+ }
+
+ return i;
+}
+
+jmp_buf env;
+
+static void
+handler(int s)
+{
+ printf("signal: %d\n", s);
+
+ void *array[10];
+ size_t size = backtrace(array, 10);
+ ATF_REQUIRE(size != 0);
+
+ printf("Backtrace %zd stack frames.\n", size);
+ backtrace_symbols_fd(array, size, STDOUT_FILENO);
+
+ char **strings = backtrace_symbols_fmt(array, size, "%n");
+ bool found_handler = false;
+ bool found_sigtramp = false;
+ bool found_the_loop = false;
+ bool found_main = false;
+ size_t i;
+
+ /*
+ * We must find the symbols in the following order:
+ *
+ * handler -> __sigtramp_siginfo_* -> the_loop -> main
+ */
+ for (i = 0; i < size; i++) {
+ if (!found_handler &&
+ strcmp(strings[i], "handler") == 0) {
+ found_handler = true;
+ continue;
+ }
+ if (found_handler && !found_sigtramp &&
+ strncmp(strings[i], "__sigtramp_siginfo_",
+ strlen("__sigtramp_siginfo_")) == 0) {
+ found_sigtramp = true;
+ continue;
+ }
+ if (found_sigtramp && !found_the_loop &&
+ strcmp(strings[i], "the_loop") == 0) {
+ found_the_loop = true;
+ continue;
+ }
+ if (found_the_loop && !found_main &&
+ strcmp(strings[i], "atf_tp_main") == 0) {
+ found_main = true;
+ break;
+ }
+ }
+
+ ATF_REQUIRE(found_handler);
+ ATF_REQUIRE(found_sigtramp);
+ ATF_REQUIRE(found_the_loop);
+ ATF_REQUIRE(found_main);
+
+ longjmp(env, 1);
+}
+
+ATF_TC(sig_backtrace);
+ATF_TC_HEAD(sig_backtrace, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test backtrace(3) across signal handlers");
+}
+
+ATF_TC_BODY(sig_backtrace, tc)
+{
+ sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_STACK, -1, 0);
+ ATF_REQUIRE(sig_stack.ss_sp != MAP_FAILED);
+
+ sig_stack.ss_size = SIGSTKSZ;
+ sig_stack.ss_flags = 0;
+ ATF_REQUIRE(sigaltstack(&sig_stack, NULL) == 0);
+
+ struct sigaction sa = {
+ .sa_handler = handler,
+ .sa_flags = SA_ONSTACK,
+ };
+ ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0);
+
+ if (setjmp(env) == 0) {
+ printf("%d\n", the_loop(0));
+ }
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, sig_backtrace);
+
+ return atf_no_error();
+}
Home |
Main Index |
Thread Index |
Old Index