1 /*
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
22 #include "ScopedSignalHandler.h"
23
TEST(setjmp,setjmp_smoke)24 TEST(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
TEST(setjmp,_setjmp_smoke)35 TEST(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
TEST(setjmp,sigsetjmp_0_smoke)46 TEST(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
TEST(setjmp,sigsetjmp_1_smoke)57 TEST(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
68 // Two distinct signal sets.
69 struct SigSets {
SigSetsSigSets70 SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
71 }
72
MakeSigSetSigSets73 static sigset64_t MakeSigSet(int offset) {
74 sigset64_t ss;
75 sigemptyset64(&ss);
76 sigaddset64(&ss, SIGUSR1 + offset);
77 sigaddset64(&ss, SIGRTMIN + offset);
78 return ss;
79 }
80
81 sigset64_t one;
82 sigset64_t two;
83 };
84
AssertSigmaskEquals(const sigset64_t & expected)85 void AssertSigmaskEquals(const sigset64_t& expected) {
86 sigset64_t actual;
87 sigprocmask64(SIG_SETMASK, NULL, &actual);
88 size_t end = sizeof(expected) * 8;
89 for (size_t i = 1; i <= end; ++i) {
90 EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
91 }
92 }
93
TEST(setjmp,_setjmp_signal_mask)94 TEST(setjmp, _setjmp_signal_mask) {
95 SignalMaskRestorer smr;
96
97 // _setjmp/_longjmp do not save/restore the signal mask.
98 SigSets ss;
99 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
100 jmp_buf jb;
101 if (_setjmp(jb) == 0) {
102 sigprocmask64(SIG_SETMASK, &ss.two, NULL);
103 _longjmp(jb, 1);
104 FAIL(); // Unreachable.
105 } else {
106 AssertSigmaskEquals(ss.two);
107 }
108 }
109
TEST(setjmp,setjmp_signal_mask)110 TEST(setjmp, setjmp_signal_mask) {
111 SignalMaskRestorer smr;
112
113 // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
114 // This is a BSD versus System V historical accident. POSIX leaves the
115 // behavior unspecified, so any code that cares needs to use sigsetjmp.
116 SigSets ss;
117 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
118 jmp_buf jb;
119 if (setjmp(jb) == 0) {
120 sigprocmask64(SIG_SETMASK, &ss.two, NULL);
121 longjmp(jb, 1);
122 FAIL(); // Unreachable.
123 } else {
124 #if defined(__BIONIC__)
125 // bionic behaves like BSD and does save/restore the signal mask.
126 AssertSigmaskEquals(ss.one);
127 #else
128 // glibc behaves like System V and doesn't save/restore the signal mask.
129 AssertSigmaskEquals(ss.two);
130 #endif
131 }
132 }
133
TEST(setjmp,sigsetjmp_0_signal_mask)134 TEST(setjmp, sigsetjmp_0_signal_mask) {
135 SignalMaskRestorer smr;
136
137 // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
138 SigSets ss;
139 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
140 sigjmp_buf sjb;
141 if (sigsetjmp(sjb, 0) == 0) {
142 sigprocmask64(SIG_SETMASK, &ss.two, NULL);
143 siglongjmp(sjb, 1);
144 FAIL(); // Unreachable.
145 } else {
146 AssertSigmaskEquals(ss.two);
147 }
148 }
149
TEST(setjmp,sigsetjmp_1_signal_mask)150 TEST(setjmp, sigsetjmp_1_signal_mask) {
151 SignalMaskRestorer smr;
152
153 // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
154 SigSets ss;
155 sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
156 sigjmp_buf sjb;
157 if (sigsetjmp(sjb, 1) == 0) {
158 sigprocmask64(SIG_SETMASK, &ss.two, NULL);
159 siglongjmp(sjb, 1);
160 FAIL(); // Unreachable.
161 } else {
162 AssertSigmaskEquals(ss.one);
163 }
164 }
165
166 #if defined(__aarch64__)
167 #define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
168 #define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
169 #define SET_FREGS \
170 SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
171 SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
172 #define CLEAR_FREGS \
173 CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
174 CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
175 #define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
176 #define CHECK_FREGS \
177 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
178 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
179 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
180 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
181 #elif defined(__arm__)
182 #define SET_FREG(n, v) \
183 ({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
184 #define SET_FREGS \
185 SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
186 SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
187 #define CLEAR_FREGS \
188 SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
189 SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
190 #define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
191 #define CHECK_FREGS \
192 EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
193 EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
194 EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
195 EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
196 #else
197 /* The other architectures don't save/restore fp registers. */
198 #define SET_FREGS
199 #define CLEAR_FREGS
200 #define CHECK_FREGS
201 #endif
202
TEST(setjmp,setjmp_fp_registers)203 TEST(setjmp, setjmp_fp_registers) {
204 int value;
205 jmp_buf jb;
206 SET_FREGS;
207 if ((value = setjmp(jb)) == 0) {
208 CLEAR_FREGS;
209 longjmp(jb, 123);
210 FAIL(); // Unreachable.
211 } else {
212 ASSERT_EQ(123, value);
213 CHECK_FREGS;
214 }
215 }
216
217 #if defined(__arm__)
218 #define __JB_SIGFLAG 0
219 #elif defined(__aarch64__)
220 #define __JB_SIGFLAG 0
221 #elif defined(__i386__)
222 #define __JB_SIGFLAG 8
223 #elif defined(__x86_64)
224 #define __JB_SIGFLAG 8
225 #elif defined(__mips__) && defined(__LP64__)
226 #define __JB_SIGFLAG 1
227 #elif defined(__mips__)
228 #define __JB_SIGFLAG 2
229 #endif
230
TEST(setjmp,setjmp_cookie)231 TEST(setjmp, setjmp_cookie) {
232 jmp_buf jb;
233 int value = setjmp(jb);
234 ASSERT_EQ(0, value);
235
236 #if defined(__mips__) && !defined(__LP64__)
237 // round address to 8-byte boundry
238 uintptr_t jb_aligned = reinterpret_cast<uintptr_t>(jb) & ~7L;
239 long* sigflag = reinterpret_cast<long*>(jb_aligned) + __JB_SIGFLAG;
240 #else
241 long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
242 #endif
243
244 // Make sure there's actually a cookie.
245 EXPECT_NE(0, *sigflag & ~1);
246
247 // Wipe it out
248 *sigflag &= 1;
249 EXPECT_DEATH(longjmp(jb, 0), "");
250 }
251
TEST(setjmp,setjmp_cookie_checksum)252 TEST(setjmp, setjmp_cookie_checksum) {
253 jmp_buf jb;
254 int value = setjmp(jb);
255
256 if (value == 0) {
257 // Flip a bit.
258 reinterpret_cast<long*>(jb)[1] ^= 1;
259
260 EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
261 } else {
262 fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
263 }
264 }
265