xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/clone/clone08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Oracle and/or its affiliates. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Fujitsu Ltd.
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
9*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "clone_platform.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/futex.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static pid_t ptid, ctid, tgid;
21*49cdfc7eSAndroid Build Coastguard Worker static void *child_stack;
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_parent(int t);
24*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_parent(void *);
25*49cdfc7eSAndroid Build Coastguard Worker static pid_t parent_ppid;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_tid(int t);
28*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_child_settid(void *);
29*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_parent_settid(void *);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_thread(int t);
33*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_thread(void *);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker /*
36*49cdfc7eSAndroid Build Coastguard Worker  * Children cloned with CLONE_VM should avoid using any functions that
37*49cdfc7eSAndroid Build Coastguard Worker  * might require dl_runtime_resolve, because they share thread-local
38*49cdfc7eSAndroid Build Coastguard Worker  * storage with parent. If both try to resolve symbols at same time you
39*49cdfc7eSAndroid Build Coastguard Worker  * can crash, likely at _dl_x86_64_restore_sse().
40*49cdfc7eSAndroid Build Coastguard Worker  * See this thread for relevant discussion:
41*49cdfc7eSAndroid Build Coastguard Worker  * http://www.mail-archive.com/[email protected]/msg01944.html
42*49cdfc7eSAndroid Build Coastguard Worker  */
43*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
44*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
45*49cdfc7eSAndroid Build Coastguard Worker 	int flags;
46*49cdfc7eSAndroid Build Coastguard Worker 	void (*testfunc)(int);
47*49cdfc7eSAndroid Build Coastguard Worker 	int (*do_child)(void *);
48*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
49*49cdfc7eSAndroid Build Coastguard Worker 	{"CLONE_PARENT", CLONE_PARENT | SIGCHLD,
50*49cdfc7eSAndroid Build Coastguard Worker 	 test_clone_parent, child_clone_parent},
51*49cdfc7eSAndroid Build Coastguard Worker 	{"CLONE_CHILD_SETTID", CLONE_CHILD_SETTID | SIGCHLD,
52*49cdfc7eSAndroid Build Coastguard Worker 	 test_clone_tid, child_clone_child_settid},
53*49cdfc7eSAndroid Build Coastguard Worker 	{"CLONE_PARENT_SETTID", CLONE_PARENT_SETTID | CLONE_VM | SIGCHLD,
54*49cdfc7eSAndroid Build Coastguard Worker 	 test_clone_tid, child_clone_parent_settid},
55*49cdfc7eSAndroid Build Coastguard Worker 	{"CLONE_THREAD", CLONE_THREAD | CLONE_SIGHAND | CLONE_VM |
56*49cdfc7eSAndroid Build Coastguard Worker 	 CLONE_CHILD_CLEARTID | SIGCHLD,
57*49cdfc7eSAndroid Build Coastguard Worker 	 test_clone_thread, child_clone_thread},
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker 
do_test(unsigned int i)60*49cdfc7eSAndroid Build Coastguard Worker static void do_test(unsigned int i)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "running %s", test_cases[i].name);
63*49cdfc7eSAndroid Build Coastguard Worker 	test_cases[i].testfunc(i);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)66*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker 	child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)71*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	free(child_stack);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
clone_child(const struct test_case * t)76*49cdfc7eSAndroid Build Coastguard Worker static long clone_child(const struct test_case *t)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	TEST(ltp_clone7(t->flags, t->do_child, NULL, CHILD_STACK_SIZE,
79*49cdfc7eSAndroid Build Coastguard Worker 		child_stack, &ptid, NULL, &ctid));
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TTERRNO == ENOSYS)
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "clone does not support 7 args");
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "%s clone() failed", t->name);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	return TST_RET;
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker 
test_clone_parent(int t)90*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_parent(int t)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	child = SAFE_FORK();
95*49cdfc7eSAndroid Build Coastguard Worker 	if (!child) {
96*49cdfc7eSAndroid Build Coastguard Worker 		parent_ppid = getppid();
97*49cdfc7eSAndroid Build Coastguard Worker 		clone_child(&test_cases[t]);
98*49cdfc7eSAndroid Build Coastguard Worker 		_exit(0);
99*49cdfc7eSAndroid Build Coastguard Worker 	}
100*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker 
child_clone_parent(void * arg LTP_ATTRIBUTE_UNUSED)103*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_parent(void *arg LTP_ATTRIBUTE_UNUSED)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker 	if (parent_ppid == getppid()) {
106*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "clone and forked child has the same parent");
107*49cdfc7eSAndroid Build Coastguard Worker 	} else {
108*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "getppid != parent_ppid (%d != %d)",
109*49cdfc7eSAndroid Build Coastguard Worker 			parent_ppid, getppid());
110*49cdfc7eSAndroid Build Coastguard Worker 	}
111*49cdfc7eSAndroid Build Coastguard Worker 	tst_syscall(__NR_exit, 0);
112*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker 
test_clone_tid(int t)115*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_tid(int t)
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker 	clone_child(&test_cases[t]);
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
child_clone_child_settid(void * arg LTP_ATTRIBUTE_UNUSED)121*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_child_settid(void *arg LTP_ATTRIBUTE_UNUSED)
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker 	if (ctid == tst_syscall(__NR_getpid))
124*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "clone() correctly set ctid");
125*49cdfc7eSAndroid Build Coastguard Worker 	else
126*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "ctid != getpid() (%d != %d)", ctid, getpid());
127*49cdfc7eSAndroid Build Coastguard Worker 	tst_syscall(__NR_exit, 0);
128*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
129*49cdfc7eSAndroid Build Coastguard Worker }
130*49cdfc7eSAndroid Build Coastguard Worker 
child_clone_parent_settid(void * arg LTP_ATTRIBUTE_UNUSED)131*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_parent_settid(void *arg LTP_ATTRIBUTE_UNUSED)
132*49cdfc7eSAndroid Build Coastguard Worker {
133*49cdfc7eSAndroid Build Coastguard Worker 	if (ptid == tst_syscall(__NR_getpid))
134*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "clone() correctly set ptid");
135*49cdfc7eSAndroid Build Coastguard Worker 	else
136*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "ptid != getpid() (%d != %d)", ptid, getpid());
137*49cdfc7eSAndroid Build Coastguard Worker 	tst_syscall(__NR_exit, 0);
138*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
test_clone_thread(int t)141*49cdfc7eSAndroid Build Coastguard Worker static void test_clone_thread(int t)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	child = SAFE_FORK();
146*49cdfc7eSAndroid Build Coastguard Worker 	if (!child) {
147*49cdfc7eSAndroid Build Coastguard Worker 		struct timespec timeout = { 5 /* sec */, 0 };
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 		tgid = tst_syscall(__NR_getpid);
150*49cdfc7eSAndroid Build Coastguard Worker 		ctid = -1;
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 		clone_child(&test_cases[t]);
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 		if (syscall(SYS_futex, &ctid, FUTEX_WAIT, -1, &timeout)) {
155*49cdfc7eSAndroid Build Coastguard Worker 			/*
156*49cdfc7eSAndroid Build Coastguard Worker 			 * futex here is racing with clone() above.
157*49cdfc7eSAndroid Build Coastguard Worker 			 * Which means we can get EWOULDBLOCK if
158*49cdfc7eSAndroid Build Coastguard Worker 			 * ctid has been already changed by clone()
159*49cdfc7eSAndroid Build Coastguard Worker 			 * before we make the call. As long as ctid
160*49cdfc7eSAndroid Build Coastguard Worker 			 * changes we should not report error when
161*49cdfc7eSAndroid Build Coastguard Worker 			 * futex returns EWOULDBLOCK.
162*49cdfc7eSAndroid Build Coastguard Worker 			 */
163*49cdfc7eSAndroid Build Coastguard Worker 			if (errno != EWOULDBLOCK || ctid == -1) {
164*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL | TERRNO,
165*49cdfc7eSAndroid Build Coastguard Worker 					"futex failed, ctid: %d", ctid);
166*49cdfc7eSAndroid Build Coastguard Worker 				_exit(0);
167*49cdfc7eSAndroid Build Coastguard Worker 			}
168*49cdfc7eSAndroid Build Coastguard Worker 		}
169*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "futex exit on ctid change, ctid: %d", ctid);
170*49cdfc7eSAndroid Build Coastguard Worker 		_exit(0);
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker 
child_clone_thread(void * arg LTP_ATTRIBUTE_UNUSED)176*49cdfc7eSAndroid Build Coastguard Worker static int child_clone_thread(void *arg LTP_ATTRIBUTE_UNUSED)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker 	if (tgid == tst_syscall(__NR_getpid))
179*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "clone has the same thread id");
180*49cdfc7eSAndroid Build Coastguard Worker 	else
181*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "clone's thread id not equal parent's id");
182*49cdfc7eSAndroid Build Coastguard Worker 	tst_syscall(__NR_exit, 0);
183*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
184*49cdfc7eSAndroid Build Coastguard Worker }
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
187*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_cases),
188*49cdfc7eSAndroid Build Coastguard Worker 	.test = do_test,
189*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
190*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
191*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1
192*49cdfc7eSAndroid Build Coastguard Worker };
193