public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Noah Goldstein <goldstein.w.n@gmail.com>,
	"H.J. Lu" <hjl.tools@gmail.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 1/2] Add a test for longjmp from user context
Date: Tue, 19 Dec 2023 11:06:53 -0300	[thread overview]
Message-ID: <1ddd62b3-19c3-426e-990f-159811b9e64c@linaro.org> (raw)
In-Reply-To: <CAFUsyfK3dWR2sBGZUGa73QNriyOxqghucc0go_-EvWAVHdmCFQ@mail.gmail.com>



On 14/12/23 21:04, Noah Goldstein wrote:
> On Thu, Dec 14, 2023 at 4:43 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> On Thu, Dec 14, 2023 at 2:39 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>>>
>>> On Thu, Dec 14, 2023 at 1:37 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>>>>
>>>> Verify that longjmp works correctly after setcontext is called to switch
>>>> to a user context.
>>>> ---
>>>>  stdlib/Makefile           |  1 +
>>>>  stdlib/tst-setcontext10.c | 87 +++++++++++++++++++++++++++++++++++++++
>>>>  2 files changed, 88 insertions(+)
>>>>  create mode 100644 stdlib/tst-setcontext10.c
>>>>
>>>> diff --git a/stdlib/Makefile b/stdlib/Makefile
>>>> index 0b154e57c5..8c6249aab4 100644
>>>> --- a/stdlib/Makefile
>>>> +++ b/stdlib/Makefile
>>>> @@ -234,6 +234,7 @@ tests := \
>>>>    tst-setcontext7 \
>>>>    tst-setcontext8 \
>>>>    tst-setcontext9 \
>>>> +  tst-setcontext10 \
>>>>    tst-strfmon_l \
>>>>    tst-strfrom \
>>>>    tst-strfrom-locale \
>>>> diff --git a/stdlib/tst-setcontext10.c b/stdlib/tst-setcontext10.c
>>>> new file mode 100644
>>>> index 0000000000..2926753cb1
>>>> --- /dev/null
>>>> +++ b/stdlib/tst-setcontext10.c
>>>> @@ -0,0 +1,87 @@
>>>> +/* Check longjmp from user context to main 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
>>>> +   <https://www.gnu.org/licenses/>.  */
>>>> +
>>>> +#include <stdio.h>
>>>> +#include <stdlib.h>
>>>> +#include <setjmp.h>
>>>> +#include <ucontext.h>
>>>> +#include <unistd.h>
>>>> +
>>>> +static jmp_buf jmpbuf;
>>>> +static ucontext_t ctx;
>>>> +
>>>> +static void f2 (void);
>>>> +
>>>> +static void
>>>> +__attribute__ ((noinline, noclone))
>>>> +f1 (void)
>>>> +{
>>>> +  printf ("start f1\n");
>>>> +  f2 ();
>>>> +}
>>>> +
>>>> +static void
>>>> +__attribute__ ((noinline, noclone))
>>>> +f2 (void)
>>>> +{
>>>> +  printf ("start f2\n");
>>>> +  if (setcontext (&ctx) != 0)
>>>> +    {
>>>> +      printf ("%s: setcontext: %m\n", __FUNCTION__);
>>>> +      exit (EXIT_FAILURE);
>>>> +    }
>>>> +}
>>>> +
>>>> +static void
>>>> +f3 (void)
>>>> +{
>>>> +  printf ("start f3\n");
>>>> +  longjmp (jmpbuf, 1);
>>>> +}
>>>> +
>>>> +static int
>>>> +__attribute__ ((noinline, noclone))
>>>> +do_test_1 (void)
>>>> +{
>>>> +  char st1[32768];
>>>> +
>>>> +  if (setjmp (jmpbuf) != 0)
>>>> +    return 0;
>>>> +
>>>> +  puts ("making contexts");
>>>> +  if (getcontext (&ctx) != 0)
>>>> +    {
>>>> +      printf ("%s: getcontext: %m\n", __FUNCTION__);
>>>> +      exit (EXIT_FAILURE);
>>>> +    }
>>>> +  ctx.uc_stack.ss_sp = st1;
>>>> +  ctx.uc_stack.ss_size = sizeof st1;
>>>> +  ctx.uc_link = NULL;
>>>> +  makecontext (&ctx, (void (*) (void)) f3, 0);
>>>> +  f1 ();
>>>> +  puts ("FAIL: returned from f1 ()");
>>>> +  exit (EXIT_FAILURE);
>>>> +}
>>>> +
>>>> +static int
>>>> +do_test (void)
>>>> +{
>>>> +  return do_test_1 ();
>>>> +}
>>>> +
>>>> +#include <support/test-driver.c>
>>>> --
>>>> 2.43.0
>>>>
>>>
>>> What is the success path here?
>>
>> When longjmp is called from the user context, do_test_1
>> returns 0:
>>
>> if (setjmp (jmpbuf) != 0)
>>   return 0;
>>
>> --
>> H.J.
> 
> LGTM.
> 
> Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

I am seeing failures with gcc version 13.2.1 / binutils 2.41.0.20231122 with
glibc configured with --enable-stack-protector=all --enable-tunables=yes 
--enable-bind-now=yes --enable-profile=yes --enable-fortify-source=2 
--enable-hardcoded-path-in-tests --enable-cet (the hardware does not support
CET):

$ make test t=stdlib/tst-setcontext10
[...]
*** longjmp causes uninitialized stack frame ***: terminated
make[2]: Leaving directory '/home/azanella/Projects/glibc/glibc-git-master/stdlib'
FAIL: stdlib/tst-setcontext10
original exit status 1
making contexts
start f1
start f2
start f3
Didn't expect signal from child: got `Aborted'

  reply	other threads:[~2023-12-19 14:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 19:37 [PATCH 0/2] Add tests for longjmp with user contexts H.J. Lu
2023-12-14 19:37 ` [PATCH 1/2] Add a test for longjmp from user context H.J. Lu
2023-12-14 22:39   ` Noah Goldstein
2023-12-14 22:42     ` H.J. Lu
2023-12-15  0:04       ` Noah Goldstein
2023-12-19 14:06         ` Adhemerval Zanella Netto [this message]
2023-12-19 18:03           ` H.J. Lu
2023-12-14 19:37 ` [PATCH 2/2] Add a test for setjmp/longjmp within " H.J. Lu
2023-12-15  0:05   ` Noah Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1ddd62b3-19c3-426e-990f-159811b9e64c@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=goldstein.w.n@gmail.com \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).