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
34 static jmp_buf jmpbuf
;
35 static ucontext_t ctx
;
37 static void f2 (void);
40 __attribute__ ((noinline
, noclone
))
43 printf ("start f1\n");
48 __attribute__ ((noinline
, noclone
))
51 printf ("start f2\n");
52 if (setcontext (&ctx
) != 0)
54 printf ("%s: setcontext: %m\n", __FUNCTION__
);
62 printf ("start f3\n");
67 __attribute__ ((noinline
, noclone
))
72 if (setjmp (jmpbuf
) != 0)
75 puts ("making contexts");
76 if (getcontext (&ctx
) != 0)
78 printf ("%s: getcontext: %m\n", __FUNCTION__
);
81 ctx
.uc_stack
.ss_sp
= st1
;
82 ctx
.uc_stack
.ss_size
= sizeof st1
;
84 makecontext (&ctx
, (void (*) (void)) f3
, 0);
86 puts ("FAIL: returned from f1 ()");
96 #include <support/test-driver.c>