xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/clone/clone07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2003.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2012 Wanlong Gao <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test for a libc bug where exiting child function by returning from
11*49cdfc7eSAndroid Build Coastguard Worker  * it caused SIGSEGV.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "clone_platform.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static void *child_stack;
22*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void * arg LTP_ATTRIBUTE_UNUSED)23*49cdfc7eSAndroid Build Coastguard Worker static int do_child(void *arg LTP_ATTRIBUTE_UNUSED)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_clone(void)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_clone(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	int status;
31*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid = 0;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PID_SILENT(ltp_clone(SIGCHLD, do_child, NULL, CHILD_STACK_SIZE,
34*49cdfc7eSAndroid Build Coastguard Worker 				child_stack));
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
37*49cdfc7eSAndroid Build Coastguard Worker 		return;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, &status, 0);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Using return in child will not cause abnormal exit");
43*49cdfc7eSAndroid Build Coastguard Worker 		return;
44*49cdfc7eSAndroid Build Coastguard Worker 	}
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Use of return in child caused SIGSEGV");
48*49cdfc7eSAndroid Build Coastguard Worker 		return;
49*49cdfc7eSAndroid Build Coastguard Worker 	}
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL, "Using return 0 in child the child %s", tst_strstatus(status));
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
55*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_clone,
56*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
57*49cdfc7eSAndroid Build Coastguard Worker 		{&child_stack, .size = CHILD_STACK_SIZE},
58*49cdfc7eSAndroid Build Coastguard Worker 		{},
59*49cdfc7eSAndroid Build Coastguard Worker 	},
60*49cdfc7eSAndroid Build Coastguard Worker };
61