xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/signal/signal06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015   Author: Oleg Nesterov <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *                      Modify: Li Wang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * you should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker  * Description:
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  *      save_xstate_sig()->drop_init_fpu() doesn't look right. setup_rt_frame()
22*49cdfc7eSAndroid Build Coastguard Worker  *      can fail after that, in this case the next setup_rt_frame() triggered
23*49cdfc7eSAndroid Build Coastguard Worker  *      by SIGSEGV won't save fpu simply because the old state was lost. This
24*49cdfc7eSAndroid Build Coastguard Worker  *      obviously mean that fpu won't be restored after sys_rt_sigreturn() from
25*49cdfc7eSAndroid Build Coastguard Worker  *      SIGSEGV handler.
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  *      These commits fix the issue on v3.17-rc3-3 stable kernel:
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  *      commit df24fb859a4e200d9324e2974229fbb7adf00aef
30*49cdfc7eSAndroid Build Coastguard Worker  * 		Author: Oleg Nesterov <[email protected]>
31*49cdfc7eSAndroid Build Coastguard Worker  * 		Date:   Tue Sep 2 19:57:17 2014 +0200
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  *      commit 66463db4fc5605d51c7bb81d009d5bf30a783a2c
34*49cdfc7eSAndroid Build Coastguard Worker  *              Author: Oleg Nesterov <[email protected]>
35*49cdfc7eSAndroid Build Coastguard Worker  *              Date:   Tue Sep 2 19:57:13 2014 +0200
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  * Reproduce:
38*49cdfc7eSAndroid Build Coastguard Worker  *	Test-case (needs -O2).
39*49cdfc7eSAndroid Build Coastguard Worker  */
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <assert.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
51*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
52*49cdfc7eSAndroid Build Coastguard Worker #include "pgsize_helpers.h"
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "signal06";
55*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 5;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker #if __x86_64__
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #define LOOPS 30000
60*49cdfc7eSAndroid Build Coastguard Worker #define VALUE 123.456
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker volatile double D;
63*49cdfc7eSAndroid Build Coastguard Worker volatile int FLAGE;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker char altstack[MAX_PAGE_SIZE * 10] __attribute__((aligned(MAX_PAGE_SIZE)));
66*49cdfc7eSAndroid Build Coastguard Worker 
test(void)67*49cdfc7eSAndroid Build Coastguard Worker void test(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	int loop = 0;
70*49cdfc7eSAndroid Build Coastguard Worker 	int pid = getpid();
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	D = VALUE;
73*49cdfc7eSAndroid Build Coastguard Worker 	while (D == VALUE && loop < LOOPS) {
74*49cdfc7eSAndroid Build Coastguard Worker 		/* sys_tkill(pid, SIGHUP); asm to avoid save/reload
75*49cdfc7eSAndroid Build Coastguard Worker 		 * fp regs around c call */
76*49cdfc7eSAndroid Build Coastguard Worker 		int unused;
77*49cdfc7eSAndroid Build Coastguard Worker 		asm volatile ("syscall" : "=a"(unused) : "a"(__NR_tkill), "D"(pid), "S"(SIGHUP) : "rcx", "r11");
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 		loop++;
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	FLAGE = 1;
83*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "loop = %d", loop);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	if (loop == LOOPS) {
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "%s call succeeded", TCID);
87*49cdfc7eSAndroid Build Coastguard Worker 	} else {
88*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Bug Reproduced!");
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_exit();
90*49cdfc7eSAndroid Build Coastguard Worker 	}
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
sigh(int sig LTP_ATTRIBUTE_UNUSED)93*49cdfc7eSAndroid Build Coastguard Worker void sigh(int sig LTP_ATTRIBUTE_UNUSED)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
tfunc(void * arg LTP_ATTRIBUTE_UNUSED)97*49cdfc7eSAndroid Build Coastguard Worker void *tfunc(void *arg LTP_ATTRIBUTE_UNUSED)
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker 	int i;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	for (i = -1; ; i *= -1) {
102*49cdfc7eSAndroid Build Coastguard Worker 		if (i == -1) {
103*49cdfc7eSAndroid Build Coastguard Worker 			TEST(mprotect(altstack, sizeof(altstack), PROT_READ));
104*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1)
105*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TBROK | TTERRNO, NULL, "mprotect failed");
106*49cdfc7eSAndroid Build Coastguard Worker 		}
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		TEST(mprotect(altstack, sizeof(altstack), PROT_READ | PROT_WRITE));
109*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1)
110*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TTERRNO, NULL, "mprotect failed");
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 		if (FLAGE == 1)
113*49cdfc7eSAndroid Build Coastguard Worker 			return NULL;
114*49cdfc7eSAndroid Build Coastguard Worker 	}
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)117*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
118*49cdfc7eSAndroid Build Coastguard Worker {
119*49cdfc7eSAndroid Build Coastguard Worker 	int i, lc;
120*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t pt;
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	stack_t st = {
125*49cdfc7eSAndroid Build Coastguard Worker 		.ss_sp = altstack,
126*49cdfc7eSAndroid Build Coastguard Worker 		.ss_size = sizeof(altstack),
127*49cdfc7eSAndroid Build Coastguard Worker 		.ss_flags = SS_ONSTACK,
128*49cdfc7eSAndroid Build Coastguard Worker 	};
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa = {
131*49cdfc7eSAndroid Build Coastguard Worker 		.sa_handler = sigh,
132*49cdfc7eSAndroid Build Coastguard Worker 	};
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	TEST(sigaction(SIGSEGV, &sa, NULL));
135*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1)
136*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TTERRNO, NULL,
137*49cdfc7eSAndroid Build Coastguard Worker 				"SIGSEGV signal setup failed");
138*49cdfc7eSAndroid Build Coastguard Worker 	sigaltstack(&st, NULL);
139*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_flags = SA_ONSTACK;
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	TEST(sigaction(SIGHUP, &sa, NULL));
142*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1)
143*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TTERRNO, NULL,
144*49cdfc7eSAndroid Build Coastguard Worker 				"SIGHUP signal setup failed");
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
147*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++) {
150*49cdfc7eSAndroid Build Coastguard Worker 			FLAGE = 0;
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 			TEST(pthread_create(&pt, NULL, tfunc, NULL));
153*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN)
154*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TBROK | TRERRNO, NULL,
155*49cdfc7eSAndroid Build Coastguard Worker 						"pthread_create failed");
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 			test();
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 			TEST(pthread_join(pt, NULL));
160*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN)
161*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TBROK | TRERRNO, NULL,
162*49cdfc7eSAndroid Build Coastguard Worker 						"pthread_join failed");
163*49cdfc7eSAndroid Build Coastguard Worker 		}
164*49cdfc7eSAndroid Build Coastguard Worker 	}
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker #else
main(void)170*49cdfc7eSAndroid Build Coastguard Worker int main(void)
171*49cdfc7eSAndroid Build Coastguard Worker {
172*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "Only test on x86_64.");
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker #endif
175