1/* Verify that the clone wrapper properly aligns the child stack.
2 Copyright (C) 2021-2024 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 <sched.h>
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/wait.h>
25#include <unistd.h>
26#include <libc-pointer-arith.h>
27#include <tst-stack-align.h>
28#include <stackinfo.h>
29#include <support/xunistd.h>
30#include <support/check.h>
31
32static int
33check_stack_alignment (void *arg)
34{
35 bool ok = true;
36
37 puts (s: "in f");
38
39 if (TEST_STACK_ALIGN ())
40 ok = false;
41
42 return ok ? 0 : 1;
43}
44
45static int
46do_test (void)
47{
48 puts (s: "in do_test");
49
50 if (TEST_STACK_ALIGN ())
51 FAIL_EXIT1 ("stack isn't aligned\n");
52
53# define STACK_SIZE (128 * 1024)
54
55 char st[STACK_SIZE + 1];
56 /* NB: Align child stack to 1 byte. */
57 char *stack = PTR_ALIGN_UP (&st[0], 2) + 1;
58
59#if _STACK_GROWS_DOWN
60 pid_t p = clone (fn: check_stack_alignment, child_stack: stack + STACK_SIZE, flags: 0, arg: 0);
61#elif _STACK_GROWS_UP
62 pid_t p = clone (check_stack_alignment, stack, 0, 0);
63#else
64# error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
65#endif
66
67 /* Clone must not fail. */
68 TEST_VERIFY_EXIT (p != -1);
69
70 int e;
71 xwaitpid (p, status: &e, __WCLONE);
72 if (!WIFEXITED (e))
73 {
74 if (WIFSIGNALED (e))
75 printf (format: "died from signal %s\n", strsignal (WTERMSIG (e)));
76 FAIL_EXIT1 ("process did not terminate correctly");
77 }
78
79 if (WEXITSTATUS (e) != 0)
80 FAIL_EXIT1 ("exit code %d", WEXITSTATUS (e));
81
82 return 0;
83}
84
85#include <support/test-driver.c>
86

source code of glibc/sysdeps/unix/sysv/linux/tst-misalign-clone.c