From nobody Mon Jan 09 18:35:21 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NrN0n5TxXz2pF9Q; Mon, 9 Jan 2023 18:35:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NrN0n4qd6z4KXS; Mon, 9 Jan 2023 18:35:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673289321; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=juleKHkhRtmk2ESs9S6ADy8BUjnImqkfvKHFhuHxb8I=; b=JvBJRDlkD77rC55inKI8XdO6AQinzjHUKlvmuoDlY3WDX5d/4QLSSxN1aVxB/0llMg2zlA /sSTGYd6nCMPbVPW5qtc+js5LjPszb+rLRIXOtnGndFnw7yLvzQn9J8Q9cWJEC5f9Yk0DY 2BvphnxiI0H1P2xuvUGmTMp6HUk8M6jatVMmbdjlpJVf2koCQ6zVSvx3MTDNQFxUZ01TXV a2zXJ4+DrqGostqLUjus7oTXWT0XLmh1//I/Q6zNjN8tze52D/T9SHvhLMd5pxGi0SYNnE UFq/p/Cy2Qaxe4iY3NGkZry/9Cq9agFlUWB52L1YeoXCqS1ePSvyz0uPiWTjOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673289321; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=juleKHkhRtmk2ESs9S6ADy8BUjnImqkfvKHFhuHxb8I=; b=cm65Z6YrI0eUGttK/4eLiYkchz58GDMp8kZM/d8e1lnk2FQCKtso1+dREpBP2U53vVBHNQ 212or0I67otYGCFZuKTpUqWeZ7WFKms41On/5wI3OQxebyz0pjrqLrWwrax3lHwCsyp35i Xm2K/zfP7IVPSFb0xtcmZXWtdTzHzhI8N5WjDA7bYtwNaH4QtQJPBkaz94Llkv5sAFg2n2 z9kx1gfydOun5Tvz2pZhdJWdKi6113Fqv0T4P2U8cYxG+2hJpJ07ML8juS68qxTNj/d+Bp M9GWDhhVIw93BakdKDnWkPFZ2gfVXYyZNiPfATKPF/EW1rwhbyOFp0oMCZKImg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673289321; a=rsa-sha256; cv=none; b=KAqcwBIXaT8R94SoqjtNn19ikXmDdLGtlUipbQqlqPLVV2QtyVpqlkSdjuHP70c0Kb12WW aD+aiFbp0RwpZ02f/ESQiu7eAgKwsRpPzv3OLYGz96AZ0y5AlpCjmgFH2GmhZXWQCeKzRO HN9xNYB0JzZM71KnCEEE//+XzxTWBhZmhMFqOYGMnYCO/LClVkaOIO01eoSCYbI3TQWL+B DNn55c4dBn3qWnzUijVCtpem9XuhRaOVNYW+0ZpWeeW3hXANMVRAdsPOkAtUlgLDMJQpcq 9UYchkkI8q/SjAtKRL3BHc1HNeDqXIQTt/2zkcXO1VVhU/t11jAOrWtsBIbSNA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NrN0n3YYjzf15; Mon, 9 Jan 2023 18:35:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 309IZLM7039261; Mon, 9 Jan 2023 18:35:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 309IZLck039260; Mon, 9 Jan 2023 18:35:21 GMT (envelope-from git) Date: Mon, 9 Jan 2023 18:35:21 GMT Message-Id: <202301091835.309IZLck039260@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: 9fb118bebced - main - libc: Fix longjmp/_longjmp(buf, 0) for AArch64 and RISC-V List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9fb118bebced1452a46756a13be0161021b10905 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=9fb118bebced1452a46756a13be0161021b10905 commit 9fb118bebced1452a46756a13be0161021b10905 Author: Jessica Clarke AuthorDate: 2023-01-09 18:34:43 +0000 Commit: Jessica Clarke CommitDate: 2023-01-09 18:34:43 +0000 libc: Fix longjmp/_longjmp(buf, 0) for AArch64 and RISC-V These architectures fail to handle this special case, and will cause the corresponding setjmp/_setjmp to return 0 rather than 1. Fix this and add regression tests (also committed upstream). PR: 268684 Reviewed by: arichardson, jhb MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D29363 --- contrib/netbsd-tests/lib/libc/setjmp/t_setjmp.c | 50 ++++++++++++++++++++++--- lib/libc/aarch64/gen/_setjmp.S | 3 +- lib/libc/aarch64/gen/setjmp.S | 3 +- lib/libc/riscv/gen/_setjmp.S | 3 ++ lib/libc/riscv/gen/setjmp.S | 3 ++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/contrib/netbsd-tests/lib/libc/setjmp/t_setjmp.c b/contrib/netbsd-tests/lib/libc/setjmp/t_setjmp.c index 1f0f1ed5ea89..1b1baa584468 100644 --- a/contrib/netbsd-tests/lib/libc/setjmp/t_setjmp.c +++ b/contrib/netbsd-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 #include #include +#include #include #include #include @@ -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(); } diff --git a/lib/libc/aarch64/gen/_setjmp.S b/lib/libc/aarch64/gen/_setjmp.S index 49bf4df4f524..94a58d774f2b 100644 --- a/lib/libc/aarch64/gen/_setjmp.S +++ b/lib/libc/aarch64/gen/_setjmp.S @@ -91,7 +91,8 @@ ENTRY(_longjmp) #endif /* Load the return value */ - mov x0, x1 + cmp x1, #0 + csinc x0, x1, xzr, ne ret botch: diff --git a/lib/libc/aarch64/gen/setjmp.S b/lib/libc/aarch64/gen/setjmp.S index b302594ff549..e6cdba801e19 100644 --- a/lib/libc/aarch64/gen/setjmp.S +++ b/lib/libc/aarch64/gen/setjmp.S @@ -113,7 +113,8 @@ ENTRY(longjmp) ldp d14, d15, [x0] /* Load the return value */ - mov x0, x1 + cmp x1, #0 + csinc x0, x1, xzr, ne ret botch: diff --git a/lib/libc/riscv/gen/_setjmp.S b/lib/libc/riscv/gen/_setjmp.S index ded6705ef7ee..94b4e90b6f42 100644 --- a/lib/libc/riscv/gen/_setjmp.S +++ b/lib/libc/riscv/gen/_setjmp.S @@ -131,6 +131,9 @@ ENTRY(_longjmp) /* Load the return value */ mv a0, a1 + bnez a1, 1f + li a0, 1 +1: ret botch: diff --git a/lib/libc/riscv/gen/setjmp.S b/lib/libc/riscv/gen/setjmp.S index c0458e907ce0..1d5b4d5fc0ca 100644 --- a/lib/libc/riscv/gen/setjmp.S +++ b/lib/libc/riscv/gen/setjmp.S @@ -161,6 +161,9 @@ ENTRY(longjmp) /* Load the return value */ mv a0, a1 + bnez a1, 1f + li a0, 1 +1: ret botch: