From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id 65941387542A for ; Fri, 15 Dec 2023 00:05:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65941387542A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 65941387542A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702598721; cv=none; b=YIkEw8PQ/F/H+7KrHrLUFNoAQ4Ml1xDF3RLrZ3dNDZqdeRc+NRz3P5I7t2XUvVjcJvjEK5RLSieemaFN1q9YqBkOXGgmziKZp44NGTgMfR4L7wp3DHdqim6iPqTZvdaKn7bjZEscqgdi1ewlSUYieepGCpkq712GnSpUr7snRFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702598721; c=relaxed/simple; bh=JnXlLWasP/JR61SNyR/CRpJ6AHr3ssPh9+r7bskeKPg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=TgIuXV+4nm9yNjlKWvYmE1KxYTmiIKDYRYidRF2wn3+c2wyHkz/p0xoEcev0Gl4D49qZ1qpohpT9LBwsq6s84MINMd60ThlFbqdCKFP93XvbLGGch5KRh+EI8Y7HG1m5DtViJndIDyHICHs08DSVzJDVAPcFNPXNQmbqGUjdF+U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-20308664c13so86939fac.3 for ; Thu, 14 Dec 2023 16:05:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702598718; x=1703203518; darn=sourceware.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=y45sOMxJ7BPJ9qF/Wnc12SwhxjudfvWv0UjenHVajp0=; b=Yrgw6PWOEABEkESy/Qfg0qvhRKeYeDz2yjFA3dXEXNcRzPLvhLxczfoy/FtMMMIp4r t0DInUgpG49DszFtzXXdY/Wzr+qBHZckdceFiubJuNFgXm0S/j4KG4r5skX3LEfhJw8z XYbcwDZ5F+adFpcQE7rCje+rhM55xdCnd/xFp3D8UKoF9kueCWsKiqYu9ixRMOvB/T/K mAMVyj1mamvN1dCtOy8gmb542MFBZ+kDTphB67HxaXOHpiGV26uQzwzdLdxTS6GLHFZP iFgudwqtU7mOUegOuXTx7cQJsV7WUt2VSqIbII+mRwdWzb+/LZe6EjoonDfTV/UP5T6I Ntjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702598718; x=1703203518; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y45sOMxJ7BPJ9qF/Wnc12SwhxjudfvWv0UjenHVajp0=; b=VucUTM7SEog9QS0SmHbcFd/QwsOV75ldOZ4D+mFm0M3x+4y+nLG0mj3iz9NpDwJQSn 6O9dxEYV+TbIPxHAWPKj4TbROkqSIz+uMHqAM+3aH/wqjuj1Ye5olewOWG3wIQE3hApi 4vWkiPkeYh6jXFwOUxHXDRCpkYRPmu2ITghFOSiw651EXVTJsCX0XizP4eR24oHFoyQy zMV0VBGpexKObels/sLCGLQDK0xXplKQ/ZiKAToeo+Mp7KJ8AhdRjlogbQ83VkxgRvDA x/grxESd+Rykp1bGokCZldzuqK6yALZE2CMqRLenwOgfl+phmWrtryWlwlOJO/HrUClT A7zQ== X-Gm-Message-State: AOJu0YykBF0ziIj54WhixGWkPnePxSGWRU0licsKVoaBnP5nqS9mRfLy DL9fFAK1JBDCPxPGIPjAGTA4D2UoZTs3QmnUwRg= X-Google-Smtp-Source: AGHT+IFG8S0MngvJD6jwZwO376gfm+GNqDRZDMaw74LFPEXIfbOqX2L1KYLchGa3n8qqt+Mr2qT9tdI5kWdX4IvQEqM= X-Received: by 2002:a05:6871:798a:b0:1fb:34dc:5cfe with SMTP id pb10-20020a056871798a00b001fb34dc5cfemr12044272oac.56.1702598718169; Thu, 14 Dec 2023 16:05:18 -0800 (PST) MIME-Version: 1.0 References: <20231214193703.238374-1-hjl.tools@gmail.com> <20231214193703.238374-3-hjl.tools@gmail.com> In-Reply-To: <20231214193703.238374-3-hjl.tools@gmail.com> From: Noah Goldstein Date: Thu, 14 Dec 2023 18:05:06 -0600 Message-ID: Subject: Re: [PATCH 2/2] Add a test for setjmp/longjmp within user context To: "H.J. Lu" Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Dec 14, 2023 at 1:37=E2=80=AFPM H.J. Lu wrote= : > > Verify that setjmp/longjmp works correctly within a user context. > --- > stdlib/Makefile | 1 + > stdlib/tst-setcontext11.c | 178 ++++++++++++++++++++++++++++++++++++++ > 2 files changed, 179 insertions(+) > create mode 100644 stdlib/tst-setcontext11.c > > diff --git a/stdlib/Makefile b/stdlib/Makefile > index 8c6249aab4..0b5ef699a2 100644 > --- a/stdlib/Makefile > +++ b/stdlib/Makefile > @@ -235,6 +235,7 @@ tests :=3D \ > tst-setcontext8 \ > tst-setcontext9 \ > tst-setcontext10 \ > + tst-setcontext11 \ > tst-strfmon_l \ > tst-strfrom \ > tst-strfrom-locale \ > diff --git a/stdlib/tst-setcontext11.c b/stdlib/tst-setcontext11.c > new file mode 100644 > index 0000000000..5f5df5b81b > --- /dev/null > +++ b/stdlib/tst-setcontext11.c > @@ -0,0 +1,178 @@ > +/* Check setjmp/longjmp within user context. > + Copyright (C) 2023 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static ucontext_t ctx[3]; > +static jmp_buf jmpbuf; > + > +static int was_in_f1; > +static int was_in_f2; > +static int longjmp_called; > + > +static char st2[32768]; > + > +static void > +f1 (int a0, int a1, int a2, int a3) > +{ > + printf ("start f1(a0=3D%x,a1=3D%x,a2=3D%x,a3=3D%x)\n", a0, a1, a2, a3)= ; > + > + if (a0 !=3D 1 || a1 !=3D 2 || a2 !=3D 3 || a3 !=3D -4) > + { > + puts ("arg mismatch"); > + exit (EXIT_FAILURE); > + } > + > + if (swapcontext (&ctx[1], &ctx[2]) !=3D 0) > + { > + printf ("%s: swapcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + puts ("finish f1"); > + was_in_f1 =3D 1; > +} > + > +static void > +__attribute__ ((noinline, noclone)) > +call_longjmp (void) > +{ > + longjmp_called =3D 1; > + longjmp (jmpbuf, 1); > +} > + > +static void > +f2 (void) > +{ > + if (!longjmp_called) > + { > + if (setjmp (jmpbuf) =3D=3D 0) > + call_longjmp (); > + } > + > + puts ("start f2"); > + if (swapcontext (&ctx[2], &ctx[1]) !=3D 0) > + { > + printf ("%s: swapcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + puts ("finish f2"); > + was_in_f2 =3D 1; > +} > + > +volatile int global; > +static int back_in_main; > + > +static void > +check_called (void) > +{ > + if (back_in_main =3D=3D 0) > + { > + puts ("program did not reach main again"); > + _exit (EXIT_FAILURE); > + } > +} > + > +static int > +do_test (void) > +{ > + atexit (check_called); > + > + char st1[32768]; > + > + puts ("making contexts"); > + if (getcontext (&ctx[1]) !=3D 0) > + { > + if (errno =3D=3D ENOSYS) > + { > + back_in_main =3D 1; > + exit (EXIT_SUCCESS); > + } > + > + printf ("%s: getcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + > + /* Play some tricks with this context. */ > + if (++global =3D=3D 1) > + if (setcontext (&ctx[1]) !=3D 0) > + { > + printf ("%s: setcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + if (global !=3D 2) > + { > + printf ("%s: 'global' not incremented twice\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + > + ctx[1].uc_stack.ss_sp =3D st1; > + ctx[1].uc_stack.ss_size =3D sizeof st1; > + ctx[1].uc_link =3D &ctx[0]; > + { > + ucontext_t tempctx =3D ctx[1]; > + makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4); > + > + /* Without this check, a stub makecontext can make us spin forever. = */ > + if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) =3D=3D 0) > + { > + puts ("makecontext was a no-op, presuming not implemented"); > + return 0; > + } > + } > + > + if (getcontext (&ctx[2]) !=3D 0) > + { > + printf ("%s: second getcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + ctx[2].uc_stack.ss_sp =3D st2; > + ctx[2].uc_stack.ss_size =3D sizeof st2; > + ctx[2].uc_link =3D &ctx[1]; > + makecontext (&ctx[2], f2, 0); > + > + puts ("swapping contexts"); > + if (swapcontext (&ctx[0], &ctx[2]) !=3D 0) > + { > + printf ("%s: swapcontext: %m\n", __FUNCTION__); > + exit (EXIT_FAILURE); > + } > + puts ("back at main program"); > + back_in_main =3D 1; > + > + if (was_in_f1 =3D=3D 0) > + { > + puts ("didn't reach f1"); > + exit (EXIT_FAILURE); > + } > + if (was_in_f2 =3D=3D 0) > + { > + puts ("didn't reach f2"); > + exit (EXIT_FAILURE); > + } > + > + puts ("test succeeded"); > + return 0; > +} > + > +#include > -- > 2.43.0 > LGTM. Reviewed-by: Noah Goldstein