1/* Test for proper error/errno handling in clone.
2 Copyright (C) 2006-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/* BZ #2386, BZ #31402 */
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <sched.h>
25#include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
26#include <support/check.h>
27
28volatile unsigned v = 0xdeadbeef;
29
30int child_fn(void *arg)
31{
32 puts (s: "FAIL: in child_fn(); should not be here");
33 exit(1);
34}
35
36static int
37__attribute__((noinline))
38do_clone (int (*fn)(void *), void *stack)
39{
40 int result;
41 unsigned int a = v;
42 unsigned int b = v;
43 unsigned int c = v;
44 unsigned int d = v;
45 unsigned int e = v;
46 unsigned int f = v;
47 unsigned int g = v;
48 unsigned int h = v;
49 unsigned int i = v;
50 unsigned int j = v;
51 unsigned int k = v;
52 unsigned int l = v;
53 unsigned int m = v;
54 unsigned int n = v;
55 unsigned int o = v;
56
57 result = clone (fn: fn, child_stack: stack, flags: 0, NULL);
58
59 /* Check that clone does not clobber call-saved registers. */
60 TEST_VERIFY (a == v && b == v && c == v && d == v && e == v && f == v
61 && g == v && h == v && i == v && j == v && k == v && l == v
62 && m == v && n == v && o == v);
63
64 return result;
65}
66
67static void
68__attribute__((noinline))
69do_test_single (int (*fn)(void *), void *stack)
70{
71 printf (format: "%s (fn=%p, stack=%p)\n", __FUNCTION__, fn, stack);
72 errno = 0;
73
74 int result = do_clone (fn, stack);
75
76 TEST_COMPARE (errno, EINVAL);
77 TEST_COMPARE (result, -1);
78}
79
80static int
81do_test (void)
82{
83 char st[128 * 1024] __attribute__ ((aligned));
84 void *stack = NULL;
85#if _STACK_GROWS_DOWN
86 stack = st + sizeof (st);
87#elif _STACK_GROWS_UP
88 stack = st;
89#else
90# error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
91#endif
92
93 do_test_single (fn: child_fn, NULL);
94 do_test_single (NULL, stack);
95 do_test_single (NULL, NULL);
96
97 return 0;
98}
99
100#include <support/test-driver.c>
101

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