blob: 6ae8bfd3f8a081d33562d0459697679607201686 [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>
Florian Mayer4817ca62022-04-15 22:53:51 +000025#include <android-base/test_utils.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070026
Elliott Hughes71ba5892018-02-07 12:44:45 -080027#include "SignalUtils.h"
Elliott Hughes460130b2018-01-31 09:05:26 -080028
Elliott Hughes141b9172021-04-09 17:13:09 -070029using setjmp_DeathTest = SilentDeathTest;
Elliott Hughese657eb42021-02-18 17:11:56 -080030
Yongqin Liu9fea4092014-10-31 16:37:09 +080031TEST(setjmp, setjmp_smoke) {
32 int value;
33 jmp_buf jb;
34 if ((value = setjmp(jb)) == 0) {
35 longjmp(jb, 123);
36 FAIL(); // Unreachable.
37 } else {
38 ASSERT_EQ(123, value);
39 }
40}
41
42TEST(setjmp, _setjmp_smoke) {
43 int value;
44 jmp_buf jb;
45 if ((value = _setjmp(jb)) == 0) {
46 _longjmp(jb, 456);
47 FAIL(); // Unreachable.
48 } else {
49 ASSERT_EQ(456, value);
50 }
51}
52
53TEST(setjmp, sigsetjmp_0_smoke) {
54 int value;
55 sigjmp_buf jb;
56 if ((value = sigsetjmp(jb, 0)) == 0) {
57 siglongjmp(jb, 789);
58 FAIL(); // Unreachable.
59 } else {
60 ASSERT_EQ(789, value);
61 }
62}
63
64TEST(setjmp, sigsetjmp_1_smoke) {
65 int value;
66 sigjmp_buf jb;
67 if ((value = sigsetjmp(jb, 0)) == 0) {
68 siglongjmp(jb, 0xabc);
69 FAIL(); // Unreachable.
70 } else {
71 ASSERT_EQ(0xabc, value);
72 }
73}
74
Elliott Hughes460130b2018-01-31 09:05:26 -080075// Two distinct signal sets.
Elliott Hughes1510a1c2014-12-10 09:31:04 -080076struct SigSets {
77 SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080078 }
Elliott Hughes1510a1c2014-12-10 09:31:04 -080079
Elliott Hughes460130b2018-01-31 09:05:26 -080080 static sigset64_t MakeSigSet(int offset) {
81 sigset64_t ss;
82 sigemptyset64(&ss);
83 sigaddset64(&ss, SIGUSR1 + offset);
Colin Cross23b986c2022-10-19 15:03:59 -070084#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -070085 // TIMER_SIGNAL.
86 sigaddset64(&ss, __SIGRTMIN);
Colin Cross23b986c2022-10-19 15:03:59 -070087#endif
Elliott Hughes460130b2018-01-31 09:05:26 -080088 sigaddset64(&ss, SIGRTMIN + offset);
Elliott Hughes1510a1c2014-12-10 09:31:04 -080089 return ss;
90 }
91
Elliott Hughes460130b2018-01-31 09:05:26 -080092 sigset64_t one;
93 sigset64_t two;
Elliott Hughes1510a1c2014-12-10 09:31:04 -080094};
Yongqin Liu9fea4092014-10-31 16:37:09 +080095
Elliott Hughes460130b2018-01-31 09:05:26 -080096void AssertSigmaskEquals(const sigset64_t& expected) {
97 sigset64_t actual;
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 sigprocmask64(SIG_SETMASK, nullptr, &actual);
Elliott Hughes460130b2018-01-31 09:05:26 -080099 size_t end = sizeof(expected) * 8;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -0800100 for (size_t i = 1; i <= end; ++i) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800101 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -0800102 }
103}
104
Yongqin Liu9fea4092014-10-31 16:37:09 +0800105TEST(setjmp, _setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800106 SignalMaskRestorer smr;
107
Yongqin Liu9fea4092014-10-31 16:37:09 +0800108 // _setjmp/_longjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800109 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800110 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800111 jmp_buf jb;
112 if (_setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700113 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800114 _longjmp(jb, 1);
115 FAIL(); // Unreachable.
116 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800117 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800118 }
119}
120
121TEST(setjmp, setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800122 SignalMaskRestorer smr;
123
Yongqin Liu9fea4092014-10-31 16:37:09 +0800124 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
125 // This is a BSD versus System V historical accident. POSIX leaves the
126 // behavior unspecified, so any code that cares needs to use sigsetjmp.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800127 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800128 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800129 jmp_buf jb;
130 if (setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700131 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800132 longjmp(jb, 1);
133 FAIL(); // Unreachable.
134 } else {
Yongqin Liu9fea4092014-10-31 16:37:09 +0800135#if defined(__BIONIC__)
136 // bionic behaves like BSD and does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800137 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800138#else
139 // glibc behaves like System V and doesn't save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800140 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800141#endif
142 }
143}
144
145TEST(setjmp, sigsetjmp_0_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800146 SignalMaskRestorer smr;
147
Yongqin Liu9fea4092014-10-31 16:37:09 +0800148 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800149 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800150 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800151 sigjmp_buf sjb;
152 if (sigsetjmp(sjb, 0) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700153 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800154 siglongjmp(sjb, 1);
155 FAIL(); // Unreachable.
156 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800157 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800158 }
159}
160
161TEST(setjmp, sigsetjmp_1_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800162 SignalMaskRestorer smr;
163
Yongqin Liu9fea4092014-10-31 16:37:09 +0800164 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800165 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800166 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800167 sigjmp_buf sjb;
168 if (sigsetjmp(sjb, 1) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700169 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800170 siglongjmp(sjb, 1);
171 FAIL(); // Unreachable.
172 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800173 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800174 }
Yongqin Liu9fea4092014-10-31 16:37:09 +0800175}
Elliott Hughes87dd5032015-01-26 20:52:34 -0800176
177#if defined(__aarch64__)
178#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
179#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
180#define SET_FREGS \
181 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
182 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
183#define CLEAR_FREGS \
184 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
185 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
186#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
187#define CHECK_FREGS \
188 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
189 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
190 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
191 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
192#elif defined(__arm__)
193#define SET_FREG(n, v) \
194 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
195#define SET_FREGS \
196 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
197 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
198#define CLEAR_FREGS \
199 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
200 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
201#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
202#define CHECK_FREGS \
203 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
204 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
205 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
206 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
207#else
208/* The other architectures don't save/restore fp registers. */
209#define SET_FREGS
210#define CLEAR_FREGS
211#define CHECK_FREGS
212#endif
213
214TEST(setjmp, setjmp_fp_registers) {
215 int value;
216 jmp_buf jb;
217 SET_FREGS;
218 if ((value = setjmp(jb)) == 0) {
219 CLEAR_FREGS;
220 longjmp(jb, 123);
221 FAIL(); // Unreachable.
222 } else {
223 ASSERT_EQ(123, value);
224 CHECK_FREGS;
225 }
226}
Josh Gao7fda8d22015-09-10 15:40:24 -0700227
228#if defined(__arm__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000229#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700230#elif defined(__aarch64__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000231#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700232#elif defined(__i386__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000233#define JB_SIGFLAG_OFFSET 8
234#elif defined(__riscv)
235#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700236#elif defined(__x86_64)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000237#define JB_SIGFLAG_OFFSET 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700238#endif
239
Elliott Hughese657eb42021-02-18 17:11:56 -0800240TEST_F(setjmp_DeathTest, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700241 jmp_buf jb;
242 int value = setjmp(jb);
243 ASSERT_EQ(0, value);
244
Elliott Hughese1905ed2022-10-17 23:23:36 +0000245 long* sigflag = reinterpret_cast<long*>(jb) + JB_SIGFLAG_OFFSET;
Josh Gao7fda8d22015-09-10 15:40:24 -0700246
247 // Make sure there's actually a cookie.
248 EXPECT_NE(0, *sigflag & ~1);
249
250 // Wipe it out
251 *sigflag &= 1;
252 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700253}
Josh Gaoa4c69132016-03-02 19:03:17 -0800254
Elliott Hughese657eb42021-02-18 17:11:56 -0800255TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
Josh Gaoa4c69132016-03-02 19:03:17 -0800256 jmp_buf jb;
257 int value = setjmp(jb);
258
259 if (value == 0) {
260 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100261 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800262
263 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
264 } else {
265 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
266 }
267}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800268
269__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
270 longjmp(buf, 123);
271}
272
273TEST(setjmp, setjmp_stack) {
274 jmp_buf buf;
275 int value = setjmp(buf);
276 if (value == 0) call_longjmp(buf);
277 EXPECT_EQ(123, value);
278}
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700279
280TEST(setjmp, bug_152210274) {
281 // Ensure that we never have a mangled value in the stack pointer.
282#if defined(__BIONIC__)
283 struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
284 ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
285
286 constexpr size_t kNumThreads = 20;
287
288 // Start a bunch of threads calling setjmp/longjmp.
289 auto jumper = [](void* arg) -> void* {
290 sigset_t set;
291 sigemptyset(&set);
292 sigaddset(&set, SIGPROF);
293 pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
294
295 jmp_buf buf;
296 for (size_t count = 0; count < 100000; ++count) {
297 if (setjmp(buf) != 0) {
298 perror("setjmp");
299 abort();
300 }
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700301 // This will never be true, but the compiler doesn't know that, so the
302 // setjmp won't be removed by DCE. With HWASan/MTE this also acts as a
303 // kind of enforcement that the threads are done before leaving the test.
304 if (*static_cast<size_t*>(arg) != 123) longjmp(buf, 1);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700305 }
306 return nullptr;
307 };
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700308 pthread_t threads[kNumThreads];
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700309 pid_t tids[kNumThreads] = {};
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700310 size_t var = 123;
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700311 for (size_t i = 0; i < kNumThreads; ++i) {
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700312 ASSERT_EQ(0, pthread_create(&threads[i], nullptr, jumper, &var));
313 tids[i] = pthread_gettid_np(threads[i]);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700314 }
315
316 // Start the interrupter thread.
317 auto interrupter = [](void* arg) -> void* {
318 pid_t* tids = static_cast<pid_t*>(arg);
319 for (size_t count = 0; count < 1000; ++count) {
320 for (size_t i = 0; i < kNumThreads; i++) {
321 if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
322 perror("tgkill failed");
323 abort();
324 }
325 }
326 usleep(100);
327 }
328 return nullptr;
329 };
330 pthread_t t;
331 ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
332 pthread_join(t, nullptr);
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700333 for (size_t i = 0; i < kNumThreads; i++) {
334 pthread_join(threads[i], nullptr);
335 }
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700336#else
Elliott Hughes14ab3532021-04-08 20:59:50 -0700337 GTEST_SKIP() << "tests uses functions not in glibc";
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700338#endif
339}