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