blob: 472aa205b823c05f77bb685036e8fd895bfd486b [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);
Josh Gaobaf20fc2018-10-08 17:28:07 -070084 // TIMER_SIGNAL.
85 sigaddset64(&ss, __SIGRTMIN);
Elliott Hughes460130b2018-01-31 09:05:26 -080086 sigaddset64(&ss, SIGRTMIN + offset);
Elliott Hughes1510a1c2014-12-10 09:31:04 -080087 return ss;
88 }
89
Elliott Hughes460130b2018-01-31 09:05:26 -080090 sigset64_t one;
91 sigset64_t two;
Elliott Hughes1510a1c2014-12-10 09:31:04 -080092};
Yongqin Liu9fea4092014-10-31 16:37:09 +080093
Elliott Hughes460130b2018-01-31 09:05:26 -080094void AssertSigmaskEquals(const sigset64_t& expected) {
95 sigset64_t actual;
Yi Kong32bc0fc2018-08-02 17:31:13 -070096 sigprocmask64(SIG_SETMASK, nullptr, &actual);
Elliott Hughes460130b2018-01-31 09:05:26 -080097 size_t end = sizeof(expected) * 8;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080098 for (size_t i = 1; i <= end; ++i) {
Elliott Hughes460130b2018-01-31 09:05:26 -080099 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -0800100 }
101}
102
Yongqin Liu9fea4092014-10-31 16:37:09 +0800103TEST(setjmp, _setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800104 SignalMaskRestorer smr;
105
Yongqin Liu9fea4092014-10-31 16:37:09 +0800106 // _setjmp/_longjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800107 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800108 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800109 jmp_buf jb;
110 if (_setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700111 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800112 _longjmp(jb, 1);
113 FAIL(); // Unreachable.
114 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800115 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800116 }
117}
118
119TEST(setjmp, setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800120 SignalMaskRestorer smr;
121
Yongqin Liu9fea4092014-10-31 16:37:09 +0800122 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
123 // This is a BSD versus System V historical accident. POSIX leaves the
124 // behavior unspecified, so any code that cares needs to use sigsetjmp.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800125 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800126 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800127 jmp_buf jb;
128 if (setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700129 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800130 longjmp(jb, 1);
131 FAIL(); // Unreachable.
132 } else {
Yongqin Liu9fea4092014-10-31 16:37:09 +0800133#if defined(__BIONIC__)
134 // bionic behaves like BSD and does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800135 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800136#else
137 // glibc behaves like System V and doesn't save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800138 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800139#endif
140 }
141}
142
143TEST(setjmp, sigsetjmp_0_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800144 SignalMaskRestorer smr;
145
Yongqin Liu9fea4092014-10-31 16:37:09 +0800146 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800147 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800148 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800149 sigjmp_buf sjb;
150 if (sigsetjmp(sjb, 0) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700151 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800152 siglongjmp(sjb, 1);
153 FAIL(); // Unreachable.
154 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800155 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800156 }
157}
158
159TEST(setjmp, sigsetjmp_1_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800160 SignalMaskRestorer smr;
161
Yongqin Liu9fea4092014-10-31 16:37:09 +0800162 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800163 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800164 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800165 sigjmp_buf sjb;
166 if (sigsetjmp(sjb, 1) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700167 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800168 siglongjmp(sjb, 1);
169 FAIL(); // Unreachable.
170 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800171 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800172 }
Yongqin Liu9fea4092014-10-31 16:37:09 +0800173}
Elliott Hughes87dd5032015-01-26 20:52:34 -0800174
175#if defined(__aarch64__)
176#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
177#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
178#define SET_FREGS \
179 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
180 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
181#define CLEAR_FREGS \
182 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
183 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
184#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
185#define CHECK_FREGS \
186 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
187 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
188 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
189 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
190#elif defined(__arm__)
191#define SET_FREG(n, v) \
192 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
193#define SET_FREGS \
194 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
195 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
196#define CLEAR_FREGS \
197 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
198 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
199#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
200#define CHECK_FREGS \
201 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
202 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
203 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
204 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
205#else
206/* The other architectures don't save/restore fp registers. */
207#define SET_FREGS
208#define CLEAR_FREGS
209#define CHECK_FREGS
210#endif
211
212TEST(setjmp, setjmp_fp_registers) {
213 int value;
214 jmp_buf jb;
215 SET_FREGS;
216 if ((value = setjmp(jb)) == 0) {
217 CLEAR_FREGS;
218 longjmp(jb, 123);
219 FAIL(); // Unreachable.
220 } else {
221 ASSERT_EQ(123, value);
222 CHECK_FREGS;
223 }
224}
Josh Gao7fda8d22015-09-10 15:40:24 -0700225
226#if defined(__arm__)
227#define __JB_SIGFLAG 0
228#elif defined(__aarch64__)
229#define __JB_SIGFLAG 0
230#elif defined(__i386__)
Elliott Hughes460130b2018-01-31 09:05:26 -0800231#define __JB_SIGFLAG 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700232#elif defined(__x86_64)
233#define __JB_SIGFLAG 8
234#endif
235
Elliott Hughese657eb42021-02-18 17:11:56 -0800236TEST_F(setjmp_DeathTest, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700237 jmp_buf jb;
238 int value = setjmp(jb);
239 ASSERT_EQ(0, value);
240
241 long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
242
243 // Make sure there's actually a cookie.
244 EXPECT_NE(0, *sigflag & ~1);
245
246 // Wipe it out
247 *sigflag &= 1;
248 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700249}
Josh Gaoa4c69132016-03-02 19:03:17 -0800250
Elliott Hughese657eb42021-02-18 17:11:56 -0800251TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
Josh Gaoa4c69132016-03-02 19:03:17 -0800252 jmp_buf jb;
253 int value = setjmp(jb);
254
255 if (value == 0) {
256 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100257 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800258
259 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
260 } else {
261 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
262 }
263}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800264
265__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
266 longjmp(buf, 123);
267}
268
269TEST(setjmp, setjmp_stack) {
270 jmp_buf buf;
271 int value = setjmp(buf);
272 if (value == 0) call_longjmp(buf);
273 EXPECT_EQ(123, value);
274}
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700275
276TEST(setjmp, bug_152210274) {
Florian Mayer4817ca62022-04-15 22:53:51 +0000277 SKIP_WITH_HWASAN; // b/227390656
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700278 // Ensure that we never have a mangled value in the stack pointer.
279#if defined(__BIONIC__)
280 struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
281 ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
282
283 constexpr size_t kNumThreads = 20;
284
285 // Start a bunch of threads calling setjmp/longjmp.
286 auto jumper = [](void* arg) -> void* {
287 sigset_t set;
288 sigemptyset(&set);
289 sigaddset(&set, SIGPROF);
290 pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
291
292 jmp_buf buf;
293 for (size_t count = 0; count < 100000; ++count) {
294 if (setjmp(buf) != 0) {
295 perror("setjmp");
296 abort();
297 }
298 if (*static_cast<pid_t*>(arg) == 100) longjmp(buf, 1);
299 }
300 return nullptr;
301 };
302 pid_t tids[kNumThreads] = {};
303 for (size_t i = 0; i < kNumThreads; ++i) {
304 pthread_t t;
305 ASSERT_EQ(0, pthread_create(&t, nullptr, jumper, &tids[i]));
306 tids[i] = pthread_gettid_np(t);
307 }
308
309 // Start the interrupter thread.
310 auto interrupter = [](void* arg) -> void* {
311 pid_t* tids = static_cast<pid_t*>(arg);
312 for (size_t count = 0; count < 1000; ++count) {
313 for (size_t i = 0; i < kNumThreads; i++) {
314 if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
315 perror("tgkill failed");
316 abort();
317 }
318 }
319 usleep(100);
320 }
321 return nullptr;
322 };
323 pthread_t t;
324 ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
325 pthread_join(t, nullptr);
326#else
Elliott Hughes14ab3532021-04-08 20:59:50 -0700327 GTEST_SKIP() << "tests uses functions not in glibc";
Elliott Hughesc0d41db2021-04-02 18:02:38 -0700328#endif
329}