blob: ee126eb1148c2ac4ff0acec30b85d8636a8c209f [file] [log] [blame]
Yongqin Liu9fea4092014-10-31 16:37:09 +08001/*
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>
Elliott Hughesc0d41db2021-04-02 18:02:38 -070021#include <sys/syscall.h>
22#include <unistd.h>
Yongqin Liu9fea4092014-10-31 16:37:09 +080023
Elliott Hughes141b9172021-04-09 17:13:09 -070024#include <android-base/silent_death_test.h>
25
Elliott Hughes71ba5892018-02-07 12:44:45 -080026#include "SignalUtils.h"
Elliott Hughes460130b2018-01-31 09:05:26 -080027
Elliott Hughes141b9172021-04-09 17:13:09 -070028using setjmp_DeathTest = SilentDeathTest;
Elliott Hughese657eb42021-02-18 17:11:56 -080029
Yongqin Liu9fea4092014-10-31 16:37:09 +080030TEST(setjmp, setjmp_smoke) {
31 int value;
32 jmp_buf jb;
33 if ((value = setjmp(jb)) == 0) {
34 longjmp(jb, 123);
35 FAIL(); // Unreachable.
36 } else {
37 ASSERT_EQ(123, value);
38 }
39}
40
41TEST(setjmp, _setjmp_smoke) {
42 int value;
43 jmp_buf jb;
44 if ((value = _setjmp(jb)) == 0) {
45 _longjmp(jb, 456);
46 FAIL(); // Unreachable.
47 } else {
48 ASSERT_EQ(456, value);
49 }
50}
51
52TEST(setjmp, sigsetjmp_0_smoke) {
53 int value;
54 sigjmp_buf jb;
55 if ((value = sigsetjmp(jb, 0)) == 0) {
56 siglongjmp(jb, 789);
57 FAIL(); // Unreachable.
58 } else {
59 ASSERT_EQ(789, value);
60 }
61}
62
63TEST(setjmp, sigsetjmp_1_smoke) {
64 int value;
65 sigjmp_buf jb;
66 if ((value = sigsetjmp(jb, 0)) == 0) {
67 siglongjmp(jb, 0xabc);
68 FAIL(); // Unreachable.
69 } else {
70 ASSERT_EQ(0xabc, value);
71 }
72}
73
Elliott Hughes460130b2018-01-31 09:05:26 -080074// Two distinct signal sets.
Elliott Hughes1510a1c2014-12-10 09:31:04 -080075struct SigSets {
76 SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080077 }
Elliott Hughes1510a1c2014-12-10 09:31:04 -080078
Elliott Hughes460130b2018-01-31 09:05:26 -080079 static sigset64_t MakeSigSet(int offset) {
80 sigset64_t ss;
81 sigemptyset64(&ss);
82 sigaddset64(&ss, SIGUSR1 + offset);
Josh Gaobaf20fc2018-10-08 17:28:07 -070083 // TIMER_SIGNAL.
84 sigaddset64(&ss, __SIGRTMIN);
Elliott Hughes460130b2018-01-31 09:05:26 -080085 sigaddset64(&ss, SIGRTMIN + offset);
Elliott Hughes1510a1c2014-12-10 09:31:04 -080086 return ss;
87 }
88
Elliott Hughes460130b2018-01-31 09:05:26 -080089 sigset64_t one;
90 sigset64_t two;
Elliott Hughes1510a1c2014-12-10 09:31:04 -080091};
Yongqin Liu9fea4092014-10-31 16:37:09 +080092
Elliott Hughes460130b2018-01-31 09:05:26 -080093void AssertSigmaskEquals(const sigset64_t& expected) {
94 sigset64_t actual;
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 sigprocmask64(SIG_SETMASK, nullptr, &actual);
Elliott Hughes460130b2018-01-31 09:05:26 -080096 size_t end = sizeof(expected) * 8;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080097 for (size_t i = 1; i <= end; ++i) {
Elliott Hughes460130b2018-01-31 09:05:26 -080098 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080099 }
100}
101
Yongqin Liu9fea4092014-10-31 16:37:09 +0800102TEST(setjmp, _setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800103 SignalMaskRestorer smr;
104
Yongqin Liu9fea4092014-10-31 16:37:09 +0800105 // _setjmp/_longjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800106 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800107 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800108 jmp_buf jb;
109 if (_setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700110 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800111 _longjmp(jb, 1);
112 FAIL(); // Unreachable.
113 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800114 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800115 }
116}
117
118TEST(setjmp, setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800119 SignalMaskRestorer smr;
120
Yongqin Liu9fea4092014-10-31 16:37:09 +0800121 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
122 // This is a BSD versus System V historical accident. POSIX leaves the
123 // behavior unspecified, so any code that cares needs to use sigsetjmp.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800124 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800125 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800126 jmp_buf jb;
127 if (setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800129 longjmp(jb, 1);
130 FAIL(); // Unreachable.
131 } else {
Yongqin Liu9fea4092014-10-31 16:37:09 +0800132#if defined(__BIONIC__)
133 // bionic behaves like BSD and does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800134 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800135#else
136 // glibc behaves like System V and doesn't save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800137 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800138#endif
139 }
140}
141
142TEST(setjmp, sigsetjmp_0_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800143 SignalMaskRestorer smr;
144
Yongqin Liu9fea4092014-10-31 16:37:09 +0800145 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800146 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800147 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800148 sigjmp_buf sjb;
149 if (sigsetjmp(sjb, 0) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700150 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800151 siglongjmp(sjb, 1);
152 FAIL(); // Unreachable.
153 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800154 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800155 }
156}
157
158TEST(setjmp, sigsetjmp_1_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800159 SignalMaskRestorer smr;
160
Yongqin Liu9fea4092014-10-31 16:37:09 +0800161 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800162 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800163 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800164 sigjmp_buf sjb;
165 if (sigsetjmp(sjb, 1) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700166 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800167 siglongjmp(sjb, 1);
168 FAIL(); // Unreachable.
169 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800170 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800171 }
Yongqin Liu9fea4092014-10-31 16:37:09 +0800172}
Elliott Hughes87dd5032015-01-26 20:52:34 -0800173
174#if defined(__aarch64__)
175#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
176#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
177#define SET_FREGS \
178 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
179 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
180#define CLEAR_FREGS \
181 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
182 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
183#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
184#define CHECK_FREGS \
185 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
186 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
187 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
188 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
189#elif defined(__arm__)
190#define SET_FREG(n, v) \
191 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
192#define SET_FREGS \
193 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
194 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
195#define CLEAR_FREGS \
196 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
197 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
198#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
199#define CHECK_FREGS \
200 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
201 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
202 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
203 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
204#else
205/* The other architectures don't save/restore fp registers. */
206#define SET_FREGS
207#define CLEAR_FREGS
208#define CHECK_FREGS
209#endif
210
211TEST(setjmp, setjmp_fp_registers) {
212 int value;
213 jmp_buf jb;
214 SET_FREGS;
215 if ((value = setjmp(jb)) == 0) {
216 CLEAR_FREGS;
217 longjmp(jb, 123);
218 FAIL(); // Unreachable.
219 } else {
220 ASSERT_EQ(123, value);
221 CHECK_FREGS;
222 }
223}
Josh Gao7fda8d22015-09-10 15:40:24 -0700224
225#if defined(__arm__)
226#define __JB_SIGFLAG 0
227#elif defined(__aarch64__)
228#define __JB_SIGFLAG 0
229#elif defined(__i386__)
Elliott Hughes460130b2018-01-31 09:05:26 -0800230#define __JB_SIGFLAG 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700231#elif defined(__x86_64)
232#define __JB_SIGFLAG 8
233#endif
234
Elliott Hughese657eb42021-02-18 17:11:56 -0800235TEST_F(setjmp_DeathTest, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700236 jmp_buf jb;
237 int value = setjmp(jb);
238 ASSERT_EQ(0, value);
239
240 long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
241
242 // Make sure there's actually a cookie.
243 EXPECT_NE(0, *sigflag & ~1);
244
245 // Wipe it out
246 *sigflag &= 1;
247 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700248}
Josh Gaoa4c69132016-03-02 19:03:17 -0800249
Elliott Hughese657eb42021-02-18 17:11:56 -0800250TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
Josh Gaoa4c69132016-03-02 19:03:17 -0800251 jmp_buf jb;
252 int value = setjmp(jb);
253
254 if (value == 0) {
255 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100256 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800257
258 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
259 } else {
260 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
261 }
262}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800263
264__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
265 longjmp(buf, 123);
266}
267
268TEST(setjmp, setjmp_stack) {
269 jmp_buf buf;
270 int value = setjmp(buf);
271 if (value == 0) call_longjmp(buf);
272 EXPECT_EQ(123, value);
273}
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700274
275TEST(setjmp, bug_152210274) {
276 // Ensure that we never have a mangled value in the stack pointer.
277#if defined(__BIONIC__)
278 struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
279 ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
280
281 constexpr size_t kNumThreads = 20;
282
283 // Start a bunch of threads calling setjmp/longjmp.
284 auto jumper = [](void* arg) -> void* {
285 sigset_t set;
286 sigemptyset(&set);
287 sigaddset(&set, SIGPROF);
288 pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
289
290 jmp_buf buf;
291 for (size_t count = 0; count < 100000; ++count) {
292 if (setjmp(buf) != 0) {
293 perror("setjmp");
294 abort();
295 }
296 if (*static_cast<pid_t*>(arg) == 100) longjmp(buf, 1);
297 }
298 return nullptr;
299 };
300 pid_t tids[kNumThreads] = {};
301 for (size_t i = 0; i < kNumThreads; ++i) {
302 pthread_t t;
303 ASSERT_EQ(0, pthread_create(&t, nullptr, jumper, &tids[i]));
304 tids[i] = pthread_gettid_np(t);
305 }
306
307 // Start the interrupter thread.
308 auto interrupter = [](void* arg) -> void* {
309 pid_t* tids = static_cast<pid_t*>(arg);
310 for (size_t count = 0; count < 1000; ++count) {
311 for (size_t i = 0; i < kNumThreads; i++) {
312 if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
313 perror("tgkill failed");
314 abort();
315 }
316 }
317 usleep(100);
318 }
319 return nullptr;
320 };
321 pthread_t t;
322 ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
323 pthread_join(t, nullptr);
324#else
Elliott Hughes14ab3532021-04-08 20:59:50 -0700325 GTEST_SKIP() << "tests uses functions not in glibc";
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700326#endif
327}