blob: 44d8af142789ccfb7bce6c968e2291d5de9b9d0e [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>
21
Elliott Hughes71ba5892018-02-07 12:44:45 -080022#include "SignalUtils.h"
Elliott Hughes460130b2018-01-31 09:05:26 -080023
Yongqin Liu9fea4092014-10-31 16:37:09 +080024TEST(setjmp, setjmp_smoke) {
25 int value;
26 jmp_buf jb;
27 if ((value = setjmp(jb)) == 0) {
28 longjmp(jb, 123);
29 FAIL(); // Unreachable.
30 } else {
31 ASSERT_EQ(123, value);
32 }
33}
34
35TEST(setjmp, _setjmp_smoke) {
36 int value;
37 jmp_buf jb;
38 if ((value = _setjmp(jb)) == 0) {
39 _longjmp(jb, 456);
40 FAIL(); // Unreachable.
41 } else {
42 ASSERT_EQ(456, value);
43 }
44}
45
46TEST(setjmp, sigsetjmp_0_smoke) {
47 int value;
48 sigjmp_buf jb;
49 if ((value = sigsetjmp(jb, 0)) == 0) {
50 siglongjmp(jb, 789);
51 FAIL(); // Unreachable.
52 } else {
53 ASSERT_EQ(789, value);
54 }
55}
56
57TEST(setjmp, sigsetjmp_1_smoke) {
58 int value;
59 sigjmp_buf jb;
60 if ((value = sigsetjmp(jb, 0)) == 0) {
61 siglongjmp(jb, 0xabc);
62 FAIL(); // Unreachable.
63 } else {
64 ASSERT_EQ(0xabc, value);
65 }
66}
67
Elliott Hughes460130b2018-01-31 09:05:26 -080068// Two distinct signal sets.
Elliott Hughes1510a1c2014-12-10 09:31:04 -080069struct SigSets {
70 SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080071 }
Elliott Hughes1510a1c2014-12-10 09:31:04 -080072
Elliott Hughes460130b2018-01-31 09:05:26 -080073 static sigset64_t MakeSigSet(int offset) {
74 sigset64_t ss;
75 sigemptyset64(&ss);
76 sigaddset64(&ss, SIGUSR1 + offset);
Josh Gaobaf20fc2018-10-08 17:28:07 -070077 // TIMER_SIGNAL.
78 sigaddset64(&ss, __SIGRTMIN);
Elliott Hughes460130b2018-01-31 09:05:26 -080079 sigaddset64(&ss, SIGRTMIN + offset);
Elliott Hughes1510a1c2014-12-10 09:31:04 -080080 return ss;
81 }
82
Elliott Hughes460130b2018-01-31 09:05:26 -080083 sigset64_t one;
84 sigset64_t two;
Elliott Hughes1510a1c2014-12-10 09:31:04 -080085};
Yongqin Liu9fea4092014-10-31 16:37:09 +080086
Elliott Hughes460130b2018-01-31 09:05:26 -080087void AssertSigmaskEquals(const sigset64_t& expected) {
88 sigset64_t actual;
Yi Kong32bc0fc2018-08-02 17:31:13 -070089 sigprocmask64(SIG_SETMASK, nullptr, &actual);
Elliott Hughes460130b2018-01-31 09:05:26 -080090 size_t end = sizeof(expected) * 8;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080091 for (size_t i = 1; i <= end; ++i) {
Elliott Hughes460130b2018-01-31 09:05:26 -080092 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
Elliott Hughes1c0c0ed2014-12-05 22:24:49 -080093 }
94}
95
Yongqin Liu9fea4092014-10-31 16:37:09 +080096TEST(setjmp, _setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -080097 SignalMaskRestorer smr;
98
Yongqin Liu9fea4092014-10-31 16:37:09 +080099 // _setjmp/_longjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800100 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800101 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800102 jmp_buf jb;
103 if (_setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800105 _longjmp(jb, 1);
106 FAIL(); // Unreachable.
107 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800108 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800109 }
110}
111
112TEST(setjmp, setjmp_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800113 SignalMaskRestorer smr;
114
Yongqin Liu9fea4092014-10-31 16:37:09 +0800115 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
116 // This is a BSD versus System V historical accident. POSIX leaves the
117 // behavior unspecified, so any code that cares needs to use sigsetjmp.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800118 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800119 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800120 jmp_buf jb;
121 if (setjmp(jb) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700122 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800123 longjmp(jb, 1);
124 FAIL(); // Unreachable.
125 } else {
Yongqin Liu9fea4092014-10-31 16:37:09 +0800126#if defined(__BIONIC__)
127 // bionic behaves like BSD and does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800128 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800129#else
130 // glibc behaves like System V and doesn't save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800131 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800132#endif
133 }
134}
135
136TEST(setjmp, sigsetjmp_0_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800137 SignalMaskRestorer smr;
138
Yongqin Liu9fea4092014-10-31 16:37:09 +0800139 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800140 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800141 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800142 sigjmp_buf sjb;
143 if (sigsetjmp(sjb, 0) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700144 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800145 siglongjmp(sjb, 1);
146 FAIL(); // Unreachable.
147 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800148 AssertSigmaskEquals(ss.two);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800149 }
150}
151
152TEST(setjmp, sigsetjmp_1_signal_mask) {
Elliott Hughes460130b2018-01-31 09:05:26 -0800153 SignalMaskRestorer smr;
154
Yongqin Liu9fea4092014-10-31 16:37:09 +0800155 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800156 SigSets ss;
Elliott Hughes460130b2018-01-31 09:05:26 -0800157 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800158 sigjmp_buf sjb;
159 if (sigsetjmp(sjb, 1) == 0) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700160 sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800161 siglongjmp(sjb, 1);
162 FAIL(); // Unreachable.
163 } else {
Elliott Hughes1510a1c2014-12-10 09:31:04 -0800164 AssertSigmaskEquals(ss.one);
Yongqin Liu9fea4092014-10-31 16:37:09 +0800165 }
Yongqin Liu9fea4092014-10-31 16:37:09 +0800166}
Elliott Hughes87dd5032015-01-26 20:52:34 -0800167
168#if defined(__aarch64__)
169#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
170#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
171#define SET_FREGS \
172 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
173 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
174#define CLEAR_FREGS \
175 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
176 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
177#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
178#define CHECK_FREGS \
179 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
180 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
181 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
182 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
183#elif defined(__arm__)
184#define SET_FREG(n, v) \
185 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
186#define SET_FREGS \
187 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
188 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
189#define CLEAR_FREGS \
190 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
191 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
192#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
193#define CHECK_FREGS \
194 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
195 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
196 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
197 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
198#else
199/* The other architectures don't save/restore fp registers. */
200#define SET_FREGS
201#define CLEAR_FREGS
202#define CHECK_FREGS
203#endif
204
205TEST(setjmp, setjmp_fp_registers) {
206 int value;
207 jmp_buf jb;
208 SET_FREGS;
209 if ((value = setjmp(jb)) == 0) {
210 CLEAR_FREGS;
211 longjmp(jb, 123);
212 FAIL(); // Unreachable.
213 } else {
214 ASSERT_EQ(123, value);
215 CHECK_FREGS;
216 }
217}
Josh Gao7fda8d22015-09-10 15:40:24 -0700218
219#if defined(__arm__)
220#define __JB_SIGFLAG 0
221#elif defined(__aarch64__)
222#define __JB_SIGFLAG 0
223#elif defined(__i386__)
Elliott Hughes460130b2018-01-31 09:05:26 -0800224#define __JB_SIGFLAG 8
Josh Gao7fda8d22015-09-10 15:40:24 -0700225#elif defined(__x86_64)
226#define __JB_SIGFLAG 8
Nikola Veljkovic1b519c02015-10-06 18:39:49 +0200227#elif defined(__mips__) && defined(__LP64__)
228#define __JB_SIGFLAG 1
229#elif defined(__mips__)
230#define __JB_SIGFLAG 2
Josh Gao7fda8d22015-09-10 15:40:24 -0700231#endif
232
233TEST(setjmp, setjmp_cookie) {
Josh Gao7fda8d22015-09-10 15:40:24 -0700234 jmp_buf jb;
235 int value = setjmp(jb);
236 ASSERT_EQ(0, value);
237
Nikola Veljkovic1b519c02015-10-06 18:39:49 +0200238#if defined(__mips__) && !defined(__LP64__)
239 // round address to 8-byte boundry
240 uintptr_t jb_aligned = reinterpret_cast<uintptr_t>(jb) & ~7L;
241 long* sigflag = reinterpret_cast<long*>(jb_aligned) + __JB_SIGFLAG;
242#else
Josh Gao7fda8d22015-09-10 15:40:24 -0700243 long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
Nikola Veljkovic1b519c02015-10-06 18:39:49 +0200244#endif
Josh Gao7fda8d22015-09-10 15:40:24 -0700245
246 // Make sure there's actually a cookie.
247 EXPECT_NE(0, *sigflag & ~1);
248
249 // Wipe it out
250 *sigflag &= 1;
251 EXPECT_DEATH(longjmp(jb, 0), "");
Josh Gao7fda8d22015-09-10 15:40:24 -0700252}
Josh Gaoa4c69132016-03-02 19:03:17 -0800253
254TEST(setjmp, setjmp_cookie_checksum) {
255 jmp_buf jb;
256 int value = setjmp(jb);
257
258 if (value == 0) {
259 // Flip a bit.
Predrag Blagojevic32995902016-03-16 15:49:12 +0100260 reinterpret_cast<long*>(jb)[1] ^= 1;
Josh Gaoa4c69132016-03-02 19:03:17 -0800261
262 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
263 } else {
264 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
265 }
266}
Peter Collingbourne734beec2018-11-14 12:41:41 -0800267
268__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
269 longjmp(buf, 123);
270}
271
272TEST(setjmp, setjmp_stack) {
273 jmp_buf buf;
274 int value = setjmp(buf);
275 if (value == 0) call_longjmp(buf);
276 EXPECT_EQ(123, value);
277}