1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <setjmp.h>
20 #include <stdlib.h>
21
22 #include "SignalUtils.h"
23
TEST(setjmp,setjmp_smoke)24 TEST(setjmp, setjmp_smoke) {
25 int value;
26 jmp_buf jb;
27 if ((value = setjmp(jb)) == 0) {
28 longjmp(jb, 123);
29 FAIL(); // Unreachable.
30 } else {
31 ASSERT_EQ(123, value);
32 }
33 }
34
TEST(setjmp,_setjmp_smoke)35 TEST(setjmp, _setjmp_smoke) {
36 int value;
37 jmp_buf jb;
38 if ((value = _setjmp(jb)) == 0) {
39 _longjmp(jb, 456);
40 FAIL(); // Unreachable.
41 } else {
42 ASSERT_EQ(456, value);
43 }
44 }
45
TEST(setjmp,sigsetjmp_0_smoke)46 TEST(setjmp, sigsetjmp_0_smoke) {
47 int value;
48 sigjmp_buf jb;
49 if ((value = sigsetjmp(jb, 0)) == 0) {
50 siglongjmp(jb, 789);
51 FAIL(); // Unreachable.
52 } else {
53 ASSERT_EQ(789, value);
54 }
55 }
56
TEST(setjmp,sigsetjmp_1_smoke)57 TEST(setjmp, sigsetjmp_1_smoke) {
58 int value;
59 sigjmp_buf jb;
60 if ((value = sigsetjmp(jb, 0)) == 0) {
61 siglongjmp(jb, 0xabc);
62 FAIL(); // Unreachable.
63 } else {
64 ASSERT_EQ(0xabc, value);
65 }
66 }
67
68 // Two distinct signal sets.
69 struct SigSets {
SigSetsSigSets70 SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
71 }
72
MakeSigSetSigSets73 static sigset64_t MakeSigSet(int offset) {
74 sigset64_t ss;
75 sigemptyset64(&ss);
76 sigaddset64(&ss, SIGUSR1 + offset);
77 // TIMER_SIGNAL.
78 sigaddset64(&ss, __SIGRTMIN);
79 sigaddset64(&ss, SIGRTMIN + offset);
80 return ss;
81 }
82
83 sigset64_t one;
84 sigset64_t two;
85 };
86
AssertSigmaskEquals(const sigset64_t & expected)87 void AssertSigmaskEquals(const sigset64_t& expected) {
88 sigset64_t actual;
89 sigprocmask64(SIG_SETMASK, nullptr, &actual);
90 size_t end = sizeof(expected) * 8;
91 for (size_t i = 1; i <= end; ++i) {
92 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
93 }
94 }
95
TEST(setjmp,_setjmp_signal_mask)96 TEST(setjmp, _setjmp_signal_mask) {
97 SignalMaskRestorer smr;
98
99 // _setjmp/_longjmp do not save/restore the signal mask.
100 SigSets ss;
101 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
102 jmp_buf jb;
103 if (_setjmp(jb) == 0) {
104 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
105 _longjmp(jb, 1);
106 FAIL(); // Unreachable.
107 } else {
108 AssertSigmaskEquals(ss.two);
109 }
110 }
111
TEST(setjmp,setjmp_signal_mask)112 TEST(setjmp, setjmp_signal_mask) {
113 SignalMaskRestorer smr;
114
115 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
116 // This is a BSD versus System V historical accident. POSIX leaves the
117 // behavior unspecified, so any code that cares needs to use sigsetjmp.
118 SigSets ss;
119 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
120 jmp_buf jb;
121 if (setjmp(jb) == 0) {
122 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
123 longjmp(jb, 1);
124 FAIL(); // Unreachable.
125 } else {
126 #if defined(__BIONIC__)
127 // bionic behaves like BSD and does save/restore the signal mask.
128 AssertSigmaskEquals(ss.one);
129 #else
130 // glibc behaves like System V and doesn't save/restore the signal mask.
131 AssertSigmaskEquals(ss.two);
132 #endif
133 }
134 }
135
TEST(setjmp,sigsetjmp_0_signal_mask)136 TEST(setjmp, sigsetjmp_0_signal_mask) {
137 SignalMaskRestorer smr;
138
139 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
140 SigSets ss;
141 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
142 sigjmp_buf sjb;
143 if (sigsetjmp(sjb, 0) == 0) {
144 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
145 siglongjmp(sjb, 1);
146 FAIL(); // Unreachable.
147 } else {
148 AssertSigmaskEquals(ss.two);
149 }
150 }
151
TEST(setjmp,sigsetjmp_1_signal_mask)152 TEST(setjmp, sigsetjmp_1_signal_mask) {
153 SignalMaskRestorer smr;
154
155 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
156 SigSets ss;
157 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
158 sigjmp_buf sjb;
159 if (sigsetjmp(sjb, 1) == 0) {
160 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
161 siglongjmp(sjb, 1);
162 FAIL(); // Unreachable.
163 } else {
164 AssertSigmaskEquals(ss.one);
165 }
166 }
167
168 #if defined(__aarch64__)
169 #define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
170 #define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
171 #define SET_FREGS \
172 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
173 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
174 #define CLEAR_FREGS \
175 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
176 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
177 #define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
178 #define CHECK_FREGS \
179 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
180 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
181 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
182 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
183 #elif defined(__arm__)
184 #define SET_FREG(n, v) \
185 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
186 #define SET_FREGS \
187 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
188 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
189 #define CLEAR_FREGS \
190 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
191 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
192 #define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
193 #define CHECK_FREGS \
194 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
195 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
196 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
197 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
198 #else
199 /* The other architectures don't save/restore fp registers. */
200 #define SET_FREGS
201 #define CLEAR_FREGS
202 #define CHECK_FREGS
203 #endif
204
TEST(setjmp,setjmp_fp_registers)205 TEST(setjmp, setjmp_fp_registers) {
206 int value;
207 jmp_buf jb;
208 SET_FREGS;
209 if ((value = setjmp(jb)) == 0) {
210 CLEAR_FREGS;
211 longjmp(jb, 123);
212 FAIL(); // Unreachable.
213 } else {
214 ASSERT_EQ(123, value);
215 CHECK_FREGS;
216 }
217 }
218
219 #if defined(__arm__)
220 #define __JB_SIGFLAG 0
221 #elif defined(__aarch64__)
222 #define __JB_SIGFLAG 0
223 #elif defined(__i386__)
224 #define __JB_SIGFLAG 8
225 #elif defined(__x86_64)
226 #define __JB_SIGFLAG 8
227 #endif
228
TEST(setjmp,setjmp_cookie)229 TEST(setjmp, setjmp_cookie) {
230 jmp_buf jb;
231 int value = setjmp(jb);
232 ASSERT_EQ(0, value);
233
234 long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
235
236 // Make sure there's actually a cookie.
237 EXPECT_NE(0, *sigflag & ~1);
238
239 // Wipe it out
240 *sigflag &= 1;
241 EXPECT_DEATH(longjmp(jb, 0), "");
242 }
243
TEST(setjmp,setjmp_cookie_checksum)244 TEST(setjmp, setjmp_cookie_checksum) {
245 jmp_buf jb;
246 int value = setjmp(jb);
247
248 if (value == 0) {
249 // Flip a bit.
250 reinterpret_cast<long*>(jb)[1] ^= 1;
251
252 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
253 } else {
254 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
255 }
256 }
257
call_longjmp(jmp_buf buf)258 __attribute__((noinline)) void call_longjmp(jmp_buf buf) {
259 longjmp(buf, 123);
260 }
261
TEST(setjmp,setjmp_stack)262 TEST(setjmp, setjmp_stack) {
263 jmp_buf buf;
264 int value = setjmp(buf);
265 if (value == 0) call_longjmp(buf);
266 EXPECT_EQ(123, value);
267 }
268