blob: 946928517144fd588b6246e727f3f0084239840a [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
Elliott Hughes86968d92024-05-21 21:25:50 +0000177#if defined(__arm__) || defined(__aarch64__)
178// arm and arm64 have the same callee save fp registers (8-15),
179// but use different instructions for accessing them.
Yi Kong11f696a2024-05-15 02:30:44 +0900180#if defined(__arm__)
181#define SET_FREG(n, v) asm volatile("vmov.f64 d"#n ", #"#v : : : "d"#n)
182#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
183#define CLEAR_FREG(n) asm volatile("vmov.i64 d"#n ", #0x0" : : : "d"#n)
184#elif defined(__aarch64__)
Elliott Hughes87dd5032015-01-26 20:52:34 -0800185#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
Yi Kong11f696a2024-05-15 02:30:44 +0900186#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
Elliott Hughes87dd5032015-01-26 20:52:34 -0800187#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
Yi Kong11f696a2024-05-15 02:30:44 +0900188#endif
Elliott Hughes87dd5032015-01-26 20:52:34 -0800189#define SET_FREGS \
190 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
Elliott Hughes86968d92024-05-21 21:25:50 +0000191 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0)
Elliott Hughes87dd5032015-01-26 20:52:34 -0800192#define CLEAR_FREGS \
193 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
Elliott Hughes86968d92024-05-21 21:25:50 +0000194 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15)
Elliott Hughes87dd5032015-01-26 20:52:34 -0800195#define CHECK_FREGS \
Elliott Hughes86968d92024-05-21 21:25:50 +0000196 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
197 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
198 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
199 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15))
200
201#elif defined(__riscv)
202// riscv64 has callee save registers fs0-fs11.
203// TODO: use Zfa to get 1.0 rather than the one_p trick.
204#define SET_FREGS \
205 double one = 1, *one_p = &one; \
206 asm volatile("fmv.d.x fs0, zero ; fld fs1, (%0) ; \
207 fadd.d fs2, fs1, fs1 ; fadd.d fs3, fs2, fs1 ; \
208 fadd.d fs4, fs3, fs1 ; fadd.d fs5, fs4, fs1 ; \
209 fadd.d fs6, fs5, fs1 ; fadd.d fs7, fs6, fs1 ; \
210 fadd.d fs8, fs7, fs1 ; fadd.d fs9, fs8, fs1 ; \
211 fadd.d fs10, fs9, fs1 ; fadd.d fs11, fs10, fs1" \
212 : \
213 : "r"(one_p) \
214 : "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", \
215 "fs6", "fs7", "fs8", "fs9", "fs10", "fs11")
216#define CLEAR_FREGS \
217 asm volatile("fmv.d.x fs0, zero ; fmv.d.x fs1, zero ; \
218 fmv.d.x fs2, zero ; fmv.d.x fs3, zero ; \
219 fmv.d.x fs4, zero ; fmv.d.x fs5, zero ; \
220 fmv.d.x fs6, zero ; fmv.d.x fs7, zero ; \
221 fmv.d.x fs8, zero ; fmv.d.x fs9, zero ; \
222 fmv.d.x fs10, zero ; fmv.d.x fs11, zero" \
223 : : : "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", \
224 "fs6", "fs7", "fs8", "fs9", "fs10", "fs11")
225#define GET_FREG(n) ({ double _r; asm volatile("fmv.d %0, fs"#n : "=f"(_r) : :); _r; })
226#define CHECK_FREGS \
227 EXPECT_EQ(0.0, GET_FREG(0)); EXPECT_EQ(1.0, GET_FREG(1)); \
228 EXPECT_EQ(2.0, GET_FREG(2)); EXPECT_EQ(3.0, GET_FREG(3)); \
229 EXPECT_EQ(4.0, GET_FREG(4)); EXPECT_EQ(5.0, GET_FREG(5)); \
230 EXPECT_EQ(6.0, GET_FREG(6)); EXPECT_EQ(7.0, GET_FREG(7)); \
231 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
232 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11))
233
Elliott Hughes87dd5032015-01-26 20:52:34 -0800234#else
Elliott Hughes86968d92024-05-21 21:25:50 +0000235// x86 and x86-64 don't save/restore fp registers.
Elliott Hughes87dd5032015-01-26 20:52:34 -0800236#define SET_FREGS
237#define CLEAR_FREGS
238#define CHECK_FREGS
239#endif
240
241TEST(setjmp, setjmp_fp_registers) {
242 int value;
243 jmp_buf jb;
244 SET_FREGS;
245 if ((value = setjmp(jb)) == 0) {
246 CLEAR_FREGS;
247 longjmp(jb, 123);
248 FAIL(); // Unreachable.
249 } else {
250 ASSERT_EQ(123, value);
251 CHECK_FREGS;
252 }
253}
Josh Gao7fda8d22015-09-10 15:40:24 -0700254
255#if defined(__arm__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000256#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700257#elif defined(__aarch64__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000258#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700259#elif defined(__i386__)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000260#define JB_SIGFLAG_OFFSET 8
261#elif defined(__riscv)
262#define JB_SIGFLAG_OFFSET 0
Josh Gao7fda8d22015-09-10 15:40:24 -0700263#elif defined(__x86_64)
Elliott Hughese1905ed2022-10-17 23:23:36 +0000264#define JB_SIGFLAG_OFFSET 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700265#endif
266
Elliott Hughese657eb42021-02-18 17:11:56 -0800267TEST_F(setjmp_DeathTest, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700268 jmp_buf jb;
269 int value = setjmp(jb);
270 ASSERT_EQ(0, value);
271
Elliott Hughese1905ed2022-10-17 23:23:36 +0000272 long* sigflag = reinterpret_cast<long*>(jb) + JB_SIGFLAG_OFFSET;
Josh Gao7fda8d22015-09-10 15:40:24 -0700273
274 // Make sure there's actually a cookie.
275 EXPECT_NE(0, *sigflag & ~1);
276
277 // Wipe it out
278 *sigflag &= 1;
279 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700280}
Josh Gaoa4c69132016-03-02 19:03:17 -0800281
Elliott Hughese657eb42021-02-18 17:11:56 -0800282TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
Josh Gaoa4c69132016-03-02 19:03:17 -0800283 jmp_buf jb;
284 int value = setjmp(jb);
285
286 if (value == 0) {
287 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100288 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800289
290 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
291 } else {
292 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
293 }
294}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800295
296__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
297 longjmp(buf, 123);
298}
299
300TEST(setjmp, setjmp_stack) {
301 jmp_buf buf;
302 int value = setjmp(buf);
303 if (value == 0) call_longjmp(buf);
304 EXPECT_EQ(123, value);
305}
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700306
307TEST(setjmp, bug_152210274) {
308 // Ensure that we never have a mangled value in the stack pointer.
309#if defined(__BIONIC__)
310 struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
311 ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
312
313 constexpr size_t kNumThreads = 20;
314
315 // Start a bunch of threads calling setjmp/longjmp.
316 auto jumper = [](void* arg) -> void* {
317 sigset_t set;
318 sigemptyset(&set);
319 sigaddset(&set, SIGPROF);
320 pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
321
322 jmp_buf buf;
323 for (size_t count = 0; count < 100000; ++count) {
324 if (setjmp(buf) != 0) {
325 perror("setjmp");
326 abort();
327 }
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700328 // This will never be true, but the compiler doesn't know that, so the
329 // setjmp won't be removed by DCE. With HWASan/MTE this also acts as a
330 // kind of enforcement that the threads are done before leaving the test.
331 if (*static_cast<size_t*>(arg) != 123) longjmp(buf, 1);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700332 }
333 return nullptr;
334 };
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700335 pthread_t threads[kNumThreads];
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700336 pid_t tids[kNumThreads] = {};
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700337 size_t var = 123;
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700338 for (size_t i = 0; i < kNumThreads; ++i) {
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700339 ASSERT_EQ(0, pthread_create(&threads[i], nullptr, jumper, &var));
340 tids[i] = pthread_gettid_np(threads[i]);
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700341 }
342
343 // Start the interrupter thread.
344 auto interrupter = [](void* arg) -> void* {
345 pid_t* tids = static_cast<pid_t*>(arg);
346 for (size_t count = 0; count < 1000; ++count) {
347 for (size_t i = 0; i < kNumThreads; i++) {
348 if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
349 perror("tgkill failed");
350 abort();
351 }
352 }
353 usleep(100);
354 }
355 return nullptr;
356 };
357 pthread_t t;
358 ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
359 pthread_join(t, nullptr);
Peter Collingbourne25a7c3f2023-03-21 22:20:22 -0700360 for (size_t i = 0; i < kNumThreads; i++) {
361 pthread_join(threads[i], nullptr);
362 }
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700363#else
Elliott Hughes14ab3532021-04-08 20:59:50 -0700364 GTEST_SKIP() << "tests uses functions not in glibc";
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700365#endif
366}