blob: 0de0a01ba9e7991c17cd91c71da5888256b62cb2 [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
Yi Kong11f696a2024-05-15 02:30:44 +0900177#if defined(__arm__)
178#define SET_FREG(n, v) asm volatile("vmov.f64 d"#n ", #"#v : : : "d"#n)
179#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
180#define CLEAR_FREG(n) asm volatile("vmov.i64 d"#n ", #0x0" : : : "d"#n)
181#elif defined(__aarch64__)
Elliott Hughes87dd5032015-01-26 20:52:34 -0800182#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
Yi Kong11f696a2024-05-15 02:30:44 +0900183#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
Elliott Hughes87dd5032015-01-26 20:52:34 -0800184#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
Yi Kong11f696a2024-05-15 02:30:44 +0900185#endif
186
187#if defined(__arm__) || defined(__aarch64__)
Elliott Hughes87dd5032015-01-26 20:52:34 -0800188#define SET_FREGS \
189 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
190 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
191#define CLEAR_FREGS \
192 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
193 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
Elliott Hughes87dd5032015-01-26 20:52:34 -0800194#define CHECK_FREGS \
195 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
196 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
197 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
198 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
199#else
200/* The other architectures don't save/restore fp registers. */
201#define SET_FREGS
202#define CLEAR_FREGS
203#define CHECK_FREGS
204#endif
205
206TEST(setjmp, setjmp_fp_registers) {
207 int value;
208 jmp_buf jb;
209 SET_FREGS;
210 if ((value = setjmp(jb)) == 0) {
211 CLEAR_FREGS;
212 longjmp(jb, 123);
213 FAIL(); // Unreachable.
214 } else {
215 ASSERT_EQ(123, value);
216 CHECK_FREGS;
217 }
218}
Josh Gao7fda8d22015-09-10 15:40:24 -0700219
220#if defined(__arm__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000221#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700222#elif defined(__aarch64__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000223#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700224#elif defined(__i386__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000225#define JB_SIGFLAG_OFFSET 8
226#elif defined(__riscv)
227#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700228#elif defined(__x86_64)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000229#define JB_SIGFLAG_OFFSET 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700230#endif
231
Elliott Hughese657eb42021-02-18 17:11:56 -0800232TEST_F(setjmp_DeathTest, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700233 jmp_buf jb;
234 int value = setjmp(jb);
235 ASSERT_EQ(0, value);
236
Elliott Hughese1905ed2022-10-17 23:23:36 +0000237 long* sigflag = reinterpret_cast<long*>(jb) + JB_SIGFLAG_OFFSET;
Josh Gao7fda8d22015-09-10 15:40:24 -0700238
239 // Make sure there's actually a cookie.
240 EXPECT_NE(0, *sigflag & ~1);
241
242 // Wipe it out
243 *sigflag &= 1;
244 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700245}
Josh Gaoa4c69132016-03-02 19:03:17 -0800246
Elliott Hughese657eb42021-02-18 17:11:56 -0800247TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
Josh Gaoa4c69132016-03-02 19:03:17 -0800248 jmp_buf jb;
249 int value = setjmp(jb);
250
251 if (value == 0) {
252 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100253 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800254
255 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
256 } else {
257 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
258 }
259}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800260
261__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
262 longjmp(buf, 123);
263}
264
265TEST(setjmp, setjmp_stack) {
266 jmp_buf buf;
267 int value = setjmp(buf);
268 if (value == 0) call_longjmp(buf);
269 EXPECT_EQ(123, value);
270}
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700271
272TEST(setjmp, bug_152210274) {
273 // Ensure that we never have a mangled value in the stack pointer.
274#if defined(__BIONIC__)
275 struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
276 ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
277
278 constexpr size_t kNumThreads = 20;
279
280 // Start a bunch of threads calling setjmp/longjmp.
281 auto jumper = [](void* arg) -> void* {
282 sigset_t set;
283 sigemptyset(&set);
284 sigaddset(&set, SIGPROF);
285 pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
286
287 jmp_buf buf;
288 for (size_t count = 0; count < 100000; ++count) {
289 if (setjmp(buf) != 0) {
290 perror("setjmp");
291 abort();
292 }
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700293 // This will never be true, but the compiler doesn't know that, so the
294 // setjmp won't be removed by DCE. With HWASan/MTE this also acts as a
295 // kind of enforcement that the threads are done before leaving the test.
296 if (*static_cast<size_t*>(arg) != 123) longjmp(buf, 1);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700297 }
298 return nullptr;
299 };
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700300 pthread_t threads[kNumThreads];
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700301 pid_t tids[kNumThreads] = {};
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700302 size_t var = 123;
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700303 for (size_t i = 0; i < kNumThreads; ++i) {
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700304 ASSERT_EQ(0, pthread_create(&threads[i], nullptr, jumper, &var));
305 tids[i] = pthread_gettid_np(threads[i]);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700306 }
307
308 // Start the interrupter thread.
309 auto interrupter = [](void* arg) -> void* {
310 pid_t* tids = static_cast<pid_t*>(arg);
311 for (size_t count = 0; count < 1000; ++count) {
312 for (size_t i = 0; i < kNumThreads; i++) {
313 if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
314 perror("tgkill failed");
315 abort();
316 }
317 }
318 usleep(100);
319 }
320 return nullptr;
321 };
322 pthread_t t;
323 ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
324 pthread_join(t, nullptr);
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700325 for (size_t i = 0; i < kNumThreads; i++) {
326 pthread_join(threads[i], nullptr);
327 }
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700328#else
Elliott Hughes14ab3532021-04-08 20:59:50 -0700329 GTEST_SKIP() << "tests uses functions not in glibc";
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700330#endif
331}