debug: Improve mqueue.h fortify warnings with clang
[glibc.git] / stdlib / tst-setcontext10.c
blob7267f5efe3b10f7f743f05c0f4c3d226898f3ce9
1 /* Check longjmp from user context to main context.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* When _FORTIFY_SOURCE is defined to 2, ____longjmp_chk is called,
20 instead of longjmp. ____longjmp_chk compares the relative stack
21 values to decide if it is called from a stack frame which called
22 setjmp. If not, ____longjmp_chk assumes that an alternate signal
23 stack is used. Since comparing the relative stack values isn't
24 reliable with user context, when there is no signal, ____longjmp_chk
25 will fail. Undefine _FORTIFY_SOURCE to avoid ____longjmp_chk. */
26 #undef _FORTIFY_SOURCE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <setjmp.h>
31 #include <ucontext.h>
32 #include <unistd.h>
34 static jmp_buf jmpbuf;
35 static ucontext_t ctx;
37 static void f2 (void);
39 static void
40 __attribute__ ((noinline, noclone))
41 f1 (void)
43 printf ("start f1\n");
44 f2 ();
47 static void
48 __attribute__ ((noinline, noclone))
49 f2 (void)
51 printf ("start f2\n");
52 if (setcontext (&ctx) != 0)
54 printf ("%s: setcontext: %m\n", __FUNCTION__);
55 exit (EXIT_FAILURE);
59 static void
60 f3 (void)
62 printf ("start f3\n");
63 longjmp (jmpbuf, 1);
66 static int
67 __attribute__ ((noinline, noclone))
68 do_test_1 (void)
70 char st1[32768];
72 if (setjmp (jmpbuf) != 0)
73 return 0;
75 puts ("making contexts");
76 if (getcontext (&ctx) != 0)
78 printf ("%s: getcontext: %m\n", __FUNCTION__);
79 exit (EXIT_FAILURE);
81 ctx.uc_stack.ss_sp = st1;
82 ctx.uc_stack.ss_size = sizeof st1;
83 ctx.uc_link = NULL;
84 makecontext (&ctx, (void (*) (void)) f3, 0);
85 f1 ();
86 puts ("FAIL: returned from f1 ()");
87 exit (EXIT_FAILURE);
90 static int
91 do_test (void)
93 return do_test_1 ();
96 #include <support/test-driver.c>