Module Name:    src
Committed By:   riastradh
Date:           Mon Feb 19 12:29:48 UTC 2024

Modified Files:
        src/tests/lib/libc/setjmp: t_sigstack.c

Log Message:
longjmp(3): Test signal mask vs stack restore with siglongjmp too.

PR lib/57946


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/lib/libc/setjmp/t_sigstack.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/setjmp/t_sigstack.c
diff -u src/tests/lib/libc/setjmp/t_sigstack.c:1.2 src/tests/lib/libc/setjmp/t_sigstack.c:1.3
--- src/tests/lib/libc/setjmp/t_sigstack.c:1.2	Mon Feb 19 04:33:21 2024
+++ src/tests/lib/libc/setjmp/t_sigstack.c	Mon Feb 19 12:29:48 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_sigstack.c,v 1.2 2024/02/19 04:33:21 riastradh Exp $	*/
+/*	$NetBSD: t_sigstack.c,v 1.3 2024/02/19 12:29:48 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2024 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_sigstack.c,v 1.2 2024/02/19 04:33:21 riastradh Exp $");
+__RCSID("$NetBSD: t_sigstack.c,v 1.3 2024/02/19 12:29:48 riastradh Exp $");
 
 #include <setjmp.h>
 #include <signal.h>
@@ -39,7 +39,10 @@ __RCSID("$NetBSD: t_sigstack.c,v 1.2 202
 
 struct sigaltstack ss;
 jmp_buf jmp;
+sigjmp_buf sigjmp;
 unsigned nentries;
+const char *bailname;
+void (*bailfn)(void) __dead;
 
 static void
 on_sigusr1(int signo, siginfo_t *si, void *ctx)
@@ -81,10 +84,10 @@ on_sigusr1(int signo, siginfo_t *si, voi
 #endif
 	ATF_REQUIRE_MSG((sp < ss.ss_sp ||
 		sp > (void *)((char *)ss.ss_sp + ss.ss_size)),
-	    "longjmp failed to restore stack before allowing signal --"
+	    "%s failed to restore stack before allowing signal --"
 	    " interrupted stack pointer %p lies in sigaltstack"
 	    " [%p, %p), size 0x%zx",
-	    sp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
+	    bailname, sp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
 
 	/*
 	 * First time through, we want to test whether longjmp restores
@@ -100,19 +103,17 @@ on_sigusr1(int signo, siginfo_t *si, voi
 	/*
 	 * Jump back to the original context.
 	 */
-	longjmp(jmp, 1);
+	(*bailfn)();
 }
 
-ATF_TC(setjmp);
-ATF_TC_HEAD(setjmp, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Test longjmp restores stack first, then signal mask");
-}
-ATF_TC_BODY(setjmp, tc)
+static void
+go(const char *name, void (*fn)(void) __dead)
 {
 	struct sigaction sa;
 
+	bailname = name;
+	bailfn = fn;
+
 	/*
 	 * Allocate a stack for the signal handler to run in, and
 	 * configure the system to use it.
@@ -145,6 +146,33 @@ ATF_TC_BODY(setjmp, tc)
 	RL(sigaction(SIGUSR1, &sa, NULL));
 
 	/*
+	 * Raise the signal to enter the signal handler the first time.
+	 */
+	RL(raise(SIGUSR1));
+
+	/*
+	 * If we ever reach this point, something went seriously wrong.
+	 */
+	atf_tc_fail("unreachable");
+}
+
+static void __dead
+bail_longjmp(void)
+{
+
+	longjmp(jmp, 1);
+}
+
+ATF_TC(setjmp);
+ATF_TC_HEAD(setjmp, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test longjmp restores stack first, then signal mask");
+}
+ATF_TC_BODY(setjmp, tc)
+{
+
+	/*
 	 * Set up a return point for the signal handler: when the
 	 * signal handler does longjmp(jmp, 1), it comes flying out of
 	 * here.
@@ -153,21 +181,46 @@ ATF_TC_BODY(setjmp, tc)
 		return;
 
 	/*
-	 * Raise the signal to enter the signal handler the first time.
+	 * Run the test with longjmp.
 	 */
-	RL(raise(SIGUSR1));
+	go("longjmp", &bail_longjmp);
+}
+
+static void __dead
+bail_siglongjmp(void)
+{
+
+	siglongjmp(sigjmp, 1);
+}
+
+ATF_TC(sigsetjmp);
+ATF_TC_HEAD(sigsetjmp, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test siglongjmp restores stack first, then signal mask");
+}
+ATF_TC_BODY(sigsetjmp, tc)
+{
 
 	/*
-	 * If we ever reach this point, something went seriously wrong.
+	 * Set up a return point for the signal handler: when the
+	 * signal handler does siglongjmp(sigjmp, 1), it comes flying
+	 * out of here.
 	 */
-	atf_tc_fail("unreachable");
+	if (sigsetjmp(sigjmp, /*savesigmask*/1) == 1)
+		return;
+
+	/*
+	 * Run the test with siglongjmp.
+	 */
+	go("siglongjmp", &bail_siglongjmp);
 }
 
 ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, setjmp);
-//	ATF_TP_ADD_TC(tp, sigsetjmp);
+	ATF_TP_ADD_TC(tp, sigsetjmp);
 
 	return atf_no_error();
 }

Reply via email to