Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libexecinfo execinfo: Test stack traces through nu...



details:   https://anonhg.NetBSD.org/src/rev/4a73e377c5bf
branches:  trunk
changeset: 368663:4a73e377c5bf
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Jul 25 10:38:17 2022 +0000

description:
execinfo: Test stack traces through null pointer jump too.

diffstat:

 tests/lib/libexecinfo/t_sig_backtrace.c |  70 +++++++++++++++++++++++++++-----
 1 files changed, 59 insertions(+), 11 deletions(-)

diffs (136 lines):

diff -r 2f3b2c3803b5 -r 4a73e377c5bf tests/lib/libexecinfo/t_sig_backtrace.c
--- a/tests/lib/libexecinfo/t_sig_backtrace.c   Mon Jul 25 08:29:14 2022 +0000
+++ b/tests/lib/libexecinfo/t_sig_backtrace.c   Mon Jul 25 10:38:17 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_sig_backtrace.c,v 1.2 2021/11/23 23:29:55 thorpej Exp $      */
+/*     $NetBSD: t_sig_backtrace.c,v 1.3 2022/07/25 10:38:17 riastradh Exp $    */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_sig_backtrace.c,v 1.2 2021/11/23 23:29:55 thorpej Exp $");
+__RCSID("$NetBSD: t_sig_backtrace.c,v 1.3 2022/07/25 10:38:17 riastradh Exp $");
 
 #include <sys/mman.h>
 #include <execinfo.h>
@@ -48,8 +48,10 @@
 stack_t sig_stack;
 
 char *foo;
+char *(*bar)(void);
 
-static int the_loop(int);
+static int the_loop_deref(int);
+static int the_loop_jump(int);
 
 #ifdef NOINLINE_HACK
 volatile int noinline;
@@ -59,7 +61,7 @@
 func1(int i)
 {
        if (i > 100) {
-               return the_loop(i);
+               return the_loop_deref(i);
        }
        return i + 1;
 }
@@ -81,7 +83,7 @@
 }
 
 static int __noinline
-the_loop(int i)
+the_loop_deref(int i)
 {
        while (*foo != 0) {
                i = func3(i);
@@ -97,6 +99,23 @@
        return i;
 }
 
+static int __noinline
+the_loop_jump(int i)
+{
+       while ((*bar)() != 0) {
+               i = func3(i);
+               i = func1(i);
+               i = func2(i);
+       }
+
+#ifdef NOINLINE_HACK
+       if (noinline)
+               vfork();
+#endif
+
+       return i;
+}
+
 jmp_buf env;
 
 static void
@@ -155,14 +174,14 @@
        longjmp(env, 1);
 }
 
-ATF_TC(sig_backtrace);
-ATF_TC_HEAD(sig_backtrace, tc)
+ATF_TC(sig_backtrace_deref);
+ATF_TC_HEAD(sig_backtrace_deref, tc)
 {
        atf_tc_set_md_var(tc, "descr",
-           "Test backtrace(3) across signal handlers");
+           "Test backtrace(3) across signal handlers, null pointer deref");
 }
 
-ATF_TC_BODY(sig_backtrace, tc)
+ATF_TC_BODY(sig_backtrace_deref, tc)
 {
        sig_stack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
            MAP_ANON | MAP_STACK, -1, 0);
@@ -179,13 +198,42 @@
        ATF_REQUIRE(sigaction(SIGSEGV, &sa, NULL) == 0);
 
        if (setjmp(env) == 0) {
-               printf("%d\n", the_loop(0));
+               printf("%d\n", the_loop_deref(0));
+       }
+}
+
+ATF_TC(sig_backtrace_jump);
+ATF_TC_HEAD(sig_backtrace_jump, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Test backtrace(3) across signal handlers, null pointer jump");
+}
+
+ATF_TC_BODY(sig_backtrace_jump, 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_jump(0));
        }
 }
 
 ATF_TP_ADD_TCS(tp)
 {
-       ATF_TP_ADD_TC(tp, sig_backtrace);
+       ATF_TP_ADD_TC(tp, sig_backtrace_deref);
+       ATF_TP_ADD_TC(tp, sig_backtrace_jump);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index