1 /* Check stack alignment provided by makecontext.
2 Copyright (C) 2018-2023 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/>. */
22 #include <support/check.h>
23 #include <support/namespace.h>
24 #include <support/xunistd.h>
28 /* Used for error reporting. */
29 static const char *context;
31 /* Check that ADDRESS is aligned to ALIGNMENT bytes, behind a compiler
33 __attribute__ ((noinline, noclone, weak))
35 check_align (void *address, size_t alignment)
37 uintptr_t uaddress = (uintptr_t) address;
38 if ((uaddress % alignment) != 0)
40 support_record_failure ();
41 printf ("error: %s: object at address %p is not aligned to %zu bytes\n",
42 context, address, alignment);
46 /* Various alignment checking functions. */
48 __attribute__ ((noinline, noclone, weak))
50 check_align_int (void)
53 check_align (&a, __alignof__ (a));
56 __attribute__ ((noinline, noclone, weak))
58 check_align_long (void)
61 check_align (&a, __alignof__ (a));
64 __attribute__ ((noinline, noclone, weak))
66 check_align_long_long (void)
69 check_align (&a, __alignof__ (a));
72 __attribute__ ((noinline, noclone, weak))
74 check_align_double (void)
77 check_align (&a, __alignof__ (a));
80 __attribute__ ((noinline, noclone, weak))
84 int a __attribute__ ((aligned (4)));
88 __attribute__ ((noinline, noclone, weak))
92 double a __attribute__ ((aligned (8)));
96 __attribute__ ((noinline, noclone, weak))
102 double x0 __attribute__ ((aligned (16)));
105 check_align (&a, 16);
108 __attribute__ ((noinline, noclone, weak))
110 check_align_32 (void)
114 double x0 __attribute__ ((aligned (32)));
119 check_align (&a, 32);
122 /* Call all the alignment checking functions. */
123 __attribute__ ((noinline, noclone, weak))
125 check_alignments (void)
129 check_align_long_long ();
130 check_align_double ();
137 /* Callback functions for makecontext and their invokers (to be used
138 with support_isolate_in_subprocess). */
140 static ucontext_t ucp;
145 context = "callback_0";
147 context = "after return from callback_0";
151 invoke_callback_0 (void *closure)
153 makecontext (&ucp, (void *) callback_0, 0);
154 if (setcontext (&ucp) != 0)
155 FAIL_EXIT1 ("setcontext");
156 FAIL_EXIT1 ("setcontext returned");
160 callback_1 (int arg1)
162 context = "callback_1";
164 TEST_COMPARE (arg1, 101);
165 context = "after return from callback_1";
169 invoke_callback_1 (void *closure)
171 makecontext (&ucp, (void *) callback_1, 1, 101);
172 if (setcontext (&ucp) != 0)
173 FAIL_EXIT1 ("setcontext");
174 FAIL_EXIT1 ("setcontext returned");
178 callback_2 (int arg1, int arg2)
180 context = "callback_2";
182 TEST_COMPARE (arg1, 201);
183 TEST_COMPARE (arg2, 202);
184 context = "after return from callback_2";
188 invoke_callback_2 (void *closure)
190 makecontext (&ucp, (void *) callback_2, 2, 201, 202);
191 if (setcontext (&ucp) != 0)
192 FAIL_EXIT1 ("setcontext");
193 FAIL_EXIT1 ("setcontext returned");
197 callback_3 (int arg1, int arg2, int arg3)
199 context = "callback_3";
201 TEST_COMPARE (arg1, 301);
202 TEST_COMPARE (arg2, 302);
203 TEST_COMPARE (arg3, 303);
204 context = "after return from callback_3";
208 invoke_callback_3 (void *closure)
210 makecontext (&ucp, (void *) callback_3, 3, 301, 302, 303);
211 if (setcontext (&ucp) != 0)
212 FAIL_EXIT1 ("setcontext");
213 FAIL_EXIT1 ("setcontext returned");
219 context = "direct call";
222 atexit (check_alignments);
224 if (getcontext (&ucp) != 0)
225 FAIL_UNSUPPORTED ("getcontext");
228 ucp.uc_stack.ss_size = 512 * 1024;
229 ucp.uc_stack.ss_sp = xmmap (NULL, ucp.uc_stack.ss_size,
230 PROT_READ | PROT_WRITE,
231 MAP_PRIVATE | MAP_ANONYMOUS, -1);
233 support_isolate_in_subprocess (invoke_callback_0, NULL);
234 support_isolate_in_subprocess (invoke_callback_1, NULL);
235 support_isolate_in_subprocess (invoke_callback_2, NULL);
236 support_isolate_in_subprocess (invoke_callback_3, NULL);
241 #include <support/test-driver.c>