* [PATCH 0/2] Add tests for longjmp with user contexts
@ 2023-12-14 19:37 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 19:37 ` [PATCH 2/2] Add a test for setjmp/longjmp within " H.J. Lu
0 siblings, 2 replies; 9+ messages in thread
From: H.J. Lu @ 2023-12-14 19:37 UTC (permalink / raw)
To: libc-alpha
Verify that longjmp works correctly from user context to main context
and within a user context.
H.J. Lu (2):
Add a test for longjmp from user context
Add a test for setjmp/longjmp within user context
stdlib/Makefile | 2 +
stdlib/tst-setcontext10.c | 87 +++++++++++++++++++
stdlib/tst-setcontext11.c | 178 ++++++++++++++++++++++++++++++++++++++
3 files changed, 267 insertions(+)
create mode 100644 stdlib/tst-setcontext10.c
create mode 100644 stdlib/tst-setcontext11.c
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/2] Add a test for longjmp from user context
2023-12-14 19:37 [PATCH 0/2] Add tests for longjmp with user contexts H.J. Lu
@ 2023-12-14 19:37 ` H.J. Lu
2023-12-14 22:39 ` Noah Goldstein
2023-12-14 19:37 ` [PATCH 2/2] Add a test for setjmp/longjmp within " H.J. Lu
1 sibling, 1 reply; 9+ messages in thread
From: H.J. Lu @ 2023-12-14 19:37 UTC (permalink / raw)
To: libc-alpha
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/2] Add a test for setjmp/longjmp within user context
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 19:37 ` H.J. Lu
2023-12-15 0:05 ` Noah Goldstein
1 sibling, 1 reply; 9+ messages in thread
From: H.J. Lu @ 2023-12-14 19:37 UTC (permalink / raw)
To: libc-alpha
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 := \
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
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <setjmp.h>
+#include <ucontext.h>
+#include <unistd.h>
+
+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=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
+
+ if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
+ {
+ puts ("arg mismatch");
+ exit (EXIT_FAILURE);
+ }
+
+ if (swapcontext (&ctx[1], &ctx[2]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+ puts ("finish f1");
+ was_in_f1 = 1;
+}
+
+static void
+__attribute__ ((noinline, noclone))
+call_longjmp (void)
+{
+ longjmp_called = 1;
+ longjmp (jmpbuf, 1);
+}
+
+static void
+f2 (void)
+{
+ if (!longjmp_called)
+ {
+ if (setjmp (jmpbuf) == 0)
+ call_longjmp ();
+ }
+
+ puts ("start f2");
+ if (swapcontext (&ctx[2], &ctx[1]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+ puts ("finish f2");
+ was_in_f2 = 1;
+}
+
+volatile int global;
+static int back_in_main;
+
+static void
+check_called (void)
+{
+ if (back_in_main == 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]) != 0)
+ {
+ if (errno == ENOSYS)
+ {
+ back_in_main = 1;
+ exit (EXIT_SUCCESS);
+ }
+
+ printf ("%s: getcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+
+ /* Play some tricks with this context. */
+ if (++global == 1)
+ if (setcontext (&ctx[1]) != 0)
+ {
+ printf ("%s: setcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+ if (global != 2)
+ {
+ printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+
+ ctx[1].uc_stack.ss_sp = st1;
+ ctx[1].uc_stack.ss_size = sizeof st1;
+ ctx[1].uc_link = &ctx[0];
+ {
+ ucontext_t tempctx = 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]) == 0)
+ {
+ puts ("makecontext was a no-op, presuming not implemented");
+ return 0;
+ }
+ }
+
+ if (getcontext (&ctx[2]) != 0)
+ {
+ printf ("%s: second getcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+ ctx[2].uc_stack.ss_sp = st2;
+ ctx[2].uc_stack.ss_size = sizeof st2;
+ ctx[2].uc_link = &ctx[1];
+ makecontext (&ctx[2], f2, 0);
+
+ puts ("swapping contexts");
+ if (swapcontext (&ctx[0], &ctx[2]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (EXIT_FAILURE);
+ }
+ puts ("back at main program");
+ back_in_main = 1;
+
+ if (was_in_f1 == 0)
+ {
+ puts ("didn't reach f1");
+ exit (EXIT_FAILURE);
+ }
+ if (was_in_f2 == 0)
+ {
+ puts ("didn't reach f2");
+ exit (EXIT_FAILURE);
+ }
+
+ puts ("test succeeded");
+ return 0;
+}
+
+#include <support/test-driver.c>
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] Add a test for longjmp from user context
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
0 siblings, 1 reply; 9+ messages in thread
From: Noah Goldstein @ 2023-12-14 22:39 UTC (permalink / raw)
To: H.J. Lu; +Cc: libc-alpha
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?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] Add a test for longjmp from user context
2023-12-14 22:39 ` Noah Goldstein
@ 2023-12-14 22:42 ` H.J. Lu
2023-12-15 0:04 ` Noah Goldstein
0 siblings, 1 reply; 9+ messages in thread
From: H.J. Lu @ 2023-12-14 22:42 UTC (permalink / raw)
To: Noah Goldstein; +Cc: libc-alpha
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.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] Add a test for longjmp from user context
2023-12-14 22:42 ` H.J. Lu
@ 2023-12-15 0:04 ` Noah Goldstein
2023-12-19 14:06 ` Adhemerval Zanella Netto
0 siblings, 1 reply; 9+ messages in thread
From: Noah Goldstein @ 2023-12-15 0:04 UTC (permalink / raw)
To: H.J. Lu; +Cc: libc-alpha
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>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/2] Add a test for setjmp/longjmp within user context
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
0 siblings, 0 replies; 9+ messages in thread
From: Noah Goldstein @ 2023-12-15 0:05 UTC (permalink / raw)
To: H.J. Lu; +Cc: libc-alpha
On Thu, Dec 14, 2023 at 1:37 PM H.J. Lu <hjl.tools@gmail.com> 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 := \
> 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
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <errno.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <setjmp.h>
> +#include <ucontext.h>
> +#include <unistd.h>
> +
> +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=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
> +
> + if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
> + {
> + puts ("arg mismatch");
> + exit (EXIT_FAILURE);
> + }
> +
> + if (swapcontext (&ctx[1], &ctx[2]) != 0)
> + {
> + printf ("%s: swapcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> + puts ("finish f1");
> + was_in_f1 = 1;
> +}
> +
> +static void
> +__attribute__ ((noinline, noclone))
> +call_longjmp (void)
> +{
> + longjmp_called = 1;
> + longjmp (jmpbuf, 1);
> +}
> +
> +static void
> +f2 (void)
> +{
> + if (!longjmp_called)
> + {
> + if (setjmp (jmpbuf) == 0)
> + call_longjmp ();
> + }
> +
> + puts ("start f2");
> + if (swapcontext (&ctx[2], &ctx[1]) != 0)
> + {
> + printf ("%s: swapcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> + puts ("finish f2");
> + was_in_f2 = 1;
> +}
> +
> +volatile int global;
> +static int back_in_main;
> +
> +static void
> +check_called (void)
> +{
> + if (back_in_main == 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]) != 0)
> + {
> + if (errno == ENOSYS)
> + {
> + back_in_main = 1;
> + exit (EXIT_SUCCESS);
> + }
> +
> + printf ("%s: getcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> +
> + /* Play some tricks with this context. */
> + if (++global == 1)
> + if (setcontext (&ctx[1]) != 0)
> + {
> + printf ("%s: setcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> + if (global != 2)
> + {
> + printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> +
> + ctx[1].uc_stack.ss_sp = st1;
> + ctx[1].uc_stack.ss_size = sizeof st1;
> + ctx[1].uc_link = &ctx[0];
> + {
> + ucontext_t tempctx = 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]) == 0)
> + {
> + puts ("makecontext was a no-op, presuming not implemented");
> + return 0;
> + }
> + }
> +
> + if (getcontext (&ctx[2]) != 0)
> + {
> + printf ("%s: second getcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> + ctx[2].uc_stack.ss_sp = st2;
> + ctx[2].uc_stack.ss_size = sizeof st2;
> + ctx[2].uc_link = &ctx[1];
> + makecontext (&ctx[2], f2, 0);
> +
> + puts ("swapping contexts");
> + if (swapcontext (&ctx[0], &ctx[2]) != 0)
> + {
> + printf ("%s: swapcontext: %m\n", __FUNCTION__);
> + exit (EXIT_FAILURE);
> + }
> + puts ("back at main program");
> + back_in_main = 1;
> +
> + if (was_in_f1 == 0)
> + {
> + puts ("didn't reach f1");
> + exit (EXIT_FAILURE);
> + }
> + if (was_in_f2 == 0)
> + {
> + puts ("didn't reach f2");
> + exit (EXIT_FAILURE);
> + }
> +
> + puts ("test succeeded");
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
> --
> 2.43.0
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] Add a test for longjmp from user context
2023-12-15 0:04 ` Noah Goldstein
@ 2023-12-19 14:06 ` Adhemerval Zanella Netto
2023-12-19 18:03 ` H.J. Lu
0 siblings, 1 reply; 9+ messages in thread
From: Adhemerval Zanella Netto @ 2023-12-19 14:06 UTC (permalink / raw)
To: Noah Goldstein, H.J. Lu; +Cc: libc-alpha
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'
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/2] Add a test for longjmp from user context
2023-12-19 14:06 ` Adhemerval Zanella Netto
@ 2023-12-19 18:03 ` H.J. Lu
0 siblings, 0 replies; 9+ messages in thread
From: H.J. Lu @ 2023-12-19 18:03 UTC (permalink / raw)
To: Adhemerval Zanella Netto; +Cc: Noah Goldstein, libc-alpha
On Tue, Dec 19, 2023 at 6:06 AM Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> 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'
-Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=2 doesn't
work with user context. I submitted a patch to undefine _FORTIFY_SOURCE
--
H.J.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-12-19 18:04 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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
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).