NetBSD Problem Report #56066

From www@netbsd.org  Sun Mar 21 16:19:55 2021
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 31DBF1A9217
	for <gnats-bugs@gnats.NetBSD.org>; Sun, 21 Mar 2021 16:19:55 +0000 (UTC)
Message-Id: <20210321161953.76F261A921F@mollari.NetBSD.org>
Date: Sun, 21 Mar 2021 16:19:53 +0000 (UTC)
From: jrtc27@FreeBSD.org
Reply-To: jrtc27@FreeBSD.org
To: gnats-bugs@NetBSD.org
Subject: [PATCH] Add tests for calling longjmp/_longjmp with a value of 0
X-Send-Pr-Version: www-1.0

>Number:         56066
>Category:       misc
>Synopsis:       [PATCH] Add tests for calling longjmp/_longjmp with a value of 0
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    misc-bug-people
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Mar 21 16:20:00 +0000 2021
>Closed-Date:    Sun Mar 28 22:46:06 +0000 2021
>Last-Modified:  Sun Mar 28 22:46:06 +0000 2021
>Originator:     Jessica Clarke
>Release:        9.99.81
>Organization:
>Environment:
N/A
>Description:
No tests currently exist in t_setjmp.c for calling longjmp/_longjmp with a value of 0, and some existing FreeBSD architectures fail to handle this case.
>How-To-Repeat:
N/A
>Fix:
From dbb1587e32367e675417ad88cb5725e84562f128 Mon Sep 17 00:00:00 2001
From: Jessica Clarke <jrtc27@FreeBSD.org>
Date: Sun, 21 Mar 2021 16:13:35 +0000
Subject: [PATCH] Add tests for calling longjmp/_longjmp with a value of 0

This is an edge case that needs special handling, and was missed for
several FreeBSD architectures.
---
 tests/lib/libc/setjmp/t_setjmp.c | 50 ++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)

diff --git a/tests/lib/libc/setjmp/t_setjmp.c b/tests/lib/libc/setjmp/t_setjmp.c
index 1f0f1ed5ea89..1b1baa584468 100644
--- a/tests/lib/libc/setjmp/t_setjmp.c
+++ b/tests/lib/libc/setjmp/t_setjmp.c
@@ -70,6 +70,7 @@ __RCSID("$NetBSD: t_setjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $");
 #include <errno.h>
 #include <setjmp.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -83,6 +84,8 @@ __RCSID("$NetBSD: t_setjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $");
 #define TEST_U_SETJMP 1
 #define TEST_SIGSETJMP_SAVE 2
 #define TEST_SIGSETJMP_NOSAVE 3
+#define TEST_LONGJMP_ZERO 4
+#define TEST_U_LONGJMP_ZERO 5

 static int expectsignal;

@@ -101,12 +104,16 @@ h_check(int test)
 	sigjmp_buf sjb;
 	sigset_t ss;
 	int i, x;
+	volatile bool did_longjmp;

 	i = getpid();
+	did_longjmp = false;

-	if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE)
+	if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE ||
+	    test == TEST_LONGJMP_ZERO)
 		expectsignal = 0;
-	else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE)
+	else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE ||
+	    test == TEST_U_LONGJMP_ZERO)
 		expectsignal = 1;
 	else
 		atf_tc_fail("unknown test");
@@ -119,26 +126,37 @@ h_check(int test)
 	REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
 	REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);

-	if (test == TEST_SETJMP)
+	if (test == TEST_SETJMP || test == TEST_LONGJMP_ZERO)
 		x = setjmp(jb);
-	else if (test == TEST_U_SETJMP)
+	else if (test == TEST_U_SETJMP || test == TEST_U_LONGJMP_ZERO)
 		x = _setjmp(jb);
 	else 
 		x = sigsetjmp(sjb, !expectsignal);

 	if (x != 0) {
-		ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
+		if (test == TEST_LONGJMP_ZERO || test == TEST_U_LONGJMP_ZERO)
+			ATF_REQUIRE_MSG(x == 1, "setjmp returned wrong value");
+		else
+			ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
+
 		kill(i, SIGABRT);
 		ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
 		atf_tc_pass();
+	} else if (did_longjmp) {
+		atf_tc_fail("setjmp returned zero after longjmp");
 	}

 	REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);

+	did_longjmp = true;
 	if (test == TEST_SETJMP)
 		longjmp(jb, i);
+	else if (test == TEST_LONGJMP_ZERO)
+		longjmp(jb, 0);
 	else if (test == TEST_U_SETJMP)
 		_longjmp(jb, i);
+	else if (test == TEST_U_LONGJMP_ZERO)
+		_longjmp(jb, 0);
 	else 
 		siglongjmp(sjb, i);

@@ -185,12 +203,34 @@ ATF_TC_BODY(sigsetjmp_nosave, tc)
 	h_check(TEST_SIGSETJMP_NOSAVE);
 }

+ATF_TC(longjmp_zero);
+ATF_TC_HEAD(longjmp_zero, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Checks longjmp(3) with a zero value");
+}
+ATF_TC_BODY(longjmp_zero, tc)
+{
+	h_check(TEST_LONGJMP_ZERO);
+}
+
+ATF_TC(_longjmp_zero);
+ATF_TC_HEAD(_longjmp_zero, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Checks _longjmp(3) with a zero value");
+}
+ATF_TC_BODY(_longjmp_zero, tc)
+{
+	h_check(TEST_U_LONGJMP_ZERO);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, setjmp);
 	ATF_TP_ADD_TC(tp, _setjmp);
 	ATF_TP_ADD_TC(tp, sigsetjmp_save);
 	ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
+	ATF_TP_ADD_TC(tp, longjmp_zero);
+	ATF_TP_ADD_TC(tp, _longjmp_zero);

 	return atf_no_error();
 }
-- 
2.31.0

>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56066 CVS commit: src/tests/lib/libc/setjmp
Date: Sun, 21 Mar 2021 12:36:32 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Sun Mar 21 16:36:32 UTC 2021

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

 Log Message:
 PR/56066: Jessica Clarke: Add tests for calling {_,}longjmp with a zero value.


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

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

State-Changed-From-To: open->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sun, 28 Mar 2021 22:46:06 +0000
State-Changed-Why:
Christos committed it.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.