]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/tst-setcontext10.c
Add a test for longjmp from user context
[thirdparty/glibc.git] / stdlib / tst-setcontext10.c
CommitLineData
08bc191f
L
1/* Check longjmp from user context to main context.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <setjmp.h>
22#include <ucontext.h>
23#include <unistd.h>
24
25static jmp_buf jmpbuf;
26static ucontext_t ctx;
27
28static void f2 (void);
29
30static void
31__attribute__ ((noinline, noclone))
32f1 (void)
33{
34 printf ("start f1\n");
35 f2 ();
36}
37
38static void
39__attribute__ ((noinline, noclone))
40f2 (void)
41{
42 printf ("start f2\n");
43 if (setcontext (&ctx) != 0)
44 {
45 printf ("%s: setcontext: %m\n", __FUNCTION__);
46 exit (EXIT_FAILURE);
47 }
48}
49
50static void
51f3 (void)
52{
53 printf ("start f3\n");
54 longjmp (jmpbuf, 1);
55}
56
57static int
58__attribute__ ((noinline, noclone))
59do_test_1 (void)
60{
61 char st1[32768];
62
63 if (setjmp (jmpbuf) != 0)
64 return 0;
65
66 puts ("making contexts");
67 if (getcontext (&ctx) != 0)
68 {
69 printf ("%s: getcontext: %m\n", __FUNCTION__);
70 exit (EXIT_FAILURE);
71 }
72 ctx.uc_stack.ss_sp = st1;
73 ctx.uc_stack.ss_size = sizeof st1;
74 ctx.uc_link = NULL;
75 makecontext (&ctx, (void (*) (void)) f3, 0);
76 f1 ();
77 puts ("FAIL: returned from f1 ()");
78 exit (EXIT_FAILURE);
79}
80
81static int
82do_test (void)
83{
84 return do_test_1 ();
85}
86
87#include <support/test-driver.c>