localedata: su_ID: make lang_name agree with CLDR
[glibc.git] / sysdeps / unix / sysv / linux / x86 / tst-cet-setcontext-1.c
blob388931f5f38c8bf1dfce05f5fab35ab23fa9f0d4
1 /* Check getcontext and setcontext on the context from makecontext
2 with shadow stack.
3 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <ucontext.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <stdatomic.h>
27 #include <x86intrin.h>
29 static ucontext_t ctx[5];
30 static atomic_int done;
32 static void
33 __attribute__((noinline, noclone))
34 f2 (void)
36 printf ("start f2\n");
37 done++;
38 if (setcontext (&ctx[2]) != 0)
40 printf ("%s: setcontext: %m\n", __FUNCTION__);
41 exit (EXIT_FAILURE);
45 static void
46 f1 (void)
48 printf ("start f1\n");
49 if (getcontext (&ctx[2]) != 0)
51 printf ("%s: getcontext: %m\n", __FUNCTION__);
52 exit (EXIT_FAILURE);
54 if (done)
55 exit (EXIT_SUCCESS);
56 f2 ();
59 static int
60 do_test (void)
62 char st1[32768];
63 puts ("making contexts");
64 if (getcontext (&ctx[0]) != 0)
66 printf ("%s: getcontext: %m\n", __FUNCTION__);
67 exit (EXIT_FAILURE);
69 if (getcontext (&ctx[1]) != 0)
71 printf ("%s: getcontext: %m\n", __FUNCTION__);
72 exit (EXIT_FAILURE);
75 ctx[3].uc_stack.ss_sp = st1;
76 ctx[3].uc_stack.ss_size = sizeof st1;
77 ctx[3].uc_link = &ctx[0];
78 makecontext (&ctx[3], (void (*) (void)) f1, 0);
80 ctx[1].uc_stack.ss_sp = st1;
81 ctx[1].uc_stack.ss_size = sizeof st1;
82 ctx[1].uc_link = &ctx[0];
83 makecontext (&ctx[1], (void (*) (void)) f1, 0);
85 ctx[4].uc_stack.ss_sp = st1;
86 ctx[4].uc_stack.ss_size = sizeof st1;
87 ctx[4].uc_link = &ctx[0];
88 makecontext (&ctx[4], (void (*) (void)) f1, 0);
90 /* NB: When shadow stack is enabled, makecontext calls map_shadow_stack
91 to allocate a new shadow stack which can be unmapped. The base
92 address and size of the new shadow stack are returned in __ssp[1]
93 and __ssp[2]. makecontext is called for CTX1, CTX3 and CTX4. But
94 only CTX1 is used. New shadow stacks are allocated in the order
95 of CTX3, CTX1, CTX4. It is very likely that CTX1's shadow stack is
96 placed between CTX3 and CTX4. We munmap CTX3's and CTX4's shadow
97 stacks to create gaps above and below CTX1's shadow stack. We check
98 that setcontext CTX1 works correctly in this case. */
99 if (_get_ssp () != 0)
101 if (ctx[3].__ssp[1] != 0
102 && munmap ((void *) (uintptr_t) ctx[3].__ssp[1],
103 (size_t) ctx[3].__ssp[2]) != 0)
105 printf ("%s: munmap: %m\n", __FUNCTION__);
106 exit (EXIT_FAILURE);
109 if (ctx[4].__ssp[1] != 0
110 && munmap ((void *) (uintptr_t) ctx[4].__ssp[1],
111 (size_t) ctx[4].__ssp[2]) != 0)
113 printf ("%s: munmap: %m\n", __FUNCTION__);
114 exit (EXIT_FAILURE);
118 if (setcontext (&ctx[1]) != 0)
120 printf ("%s: setcontext: %m\n", __FUNCTION__);
121 exit (EXIT_FAILURE);
123 exit (EXIT_FAILURE);
126 #include <support/test-driver.c>