Debian Codegraph @

src/kj/exception-test.c++

raw
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include "exception.h"
23 #include "debug.h"
24 #include <kj/compat/gtest.h>
25 #include <stdexcept>
26 #include <stdint.h>
27
28 namespace kj {
29 namespace _ { // private
30 namespace {
31
32 TEST(Exception, TrimSourceFilename) {
33 #if _WIN32
34 EXPECT_TRUE(trimSourceFilename(__FILE__) == "kj/exception-test.c++" ||
35 trimSourceFilename(__FILE__) == "kj\\exception-test.c++");
36 #else
37 EXPECT_EQ(trimSourceFilename(__FILE__), "kj/exception-test.c++");
38 #endif
39 }
40
41 TEST(Exception, RunCatchingExceptions) {
42 bool recovered = false;
43 Maybe<Exception> e = kj::runCatchingExceptions([&]() {
44 KJ_FAIL_ASSERT("foo") {
45 break;
46 }
47 recovered = true;
48 });
49
50 #if KJ_NO_EXCEPTIONS
51 EXPECT_TRUE(recovered);
52 #else
53 EXPECT_FALSE(recovered);
54 #endif
55
56 KJ_IF_MAYBE(ex, e) {
57 EXPECT_EQ("foo", ex->getDescription());
58 } else {
59 ADD_FAILURE() << "Expected exception";
60 }
61 }
62
63 #if !KJ_NO_EXCEPTIONS
64 TEST(Exception, RunCatchingExceptionsStdException) {
65 Maybe<Exception> e = kj::runCatchingExceptions([&]() {
66 throw std::logic_error("foo");
67 });
68
69 KJ_IF_MAYBE(ex, e) {
70 EXPECT_EQ("std::exception: foo", ex->getDescription());
71 } else {
72 ADD_FAILURE() << "Expected exception";
73 }
74 }
75
76 TEST(Exception, RunCatchingExceptionsOtherException) {
77 Maybe<Exception> e = kj::runCatchingExceptions([&]() {
78 throw 123;
79 });
80
81 KJ_IF_MAYBE(ex, e) {
82 #if __GNUC__ && !KJ_NO_RTTI
83 EXPECT_EQ("unknown non-KJ exception of type: int", ex->getDescription());
84 #else
85 EXPECT_EQ("unknown non-KJ exception", ex->getDescription());
86 #endif
87 } else {
88 ADD_FAILURE() << "Expected exception";
89 }
90 }
91 #endif
92
93 #if !KJ_NO_EXCEPTIONS
94 // We skip this test when exceptions are disabled because making it no-exceptions-safe defeats
95 // the purpose of the test: recoverable exceptions won't throw inside a destructor in the first
96 // place.
97
98 class ThrowingDestructor: public UnwindDetector {
99 public:
100 ~ThrowingDestructor() noexcept(false) {
101 catchExceptionsIfUnwinding([]() {
102 KJ_FAIL_ASSERT("this is a test, not a real bug");
103 });
104 }
105 };
106
107 TEST(Exception, UnwindDetector) {
108 // If no other exception is happening, ThrowingDestructor's destructor throws one.
109 Maybe<Exception> e = kj::runCatchingExceptions([&]() {
110 ThrowingDestructor t;
111 });
112
113 KJ_IF_MAYBE(ex, e) {
114 EXPECT_EQ("this is a test, not a real bug", ex->getDescription());
115 } else {
116 ADD_FAILURE() << "Expected exception";
117 }
118
119 // If another exception is happening, ThrowingDestructor's destructor's exception is squelched.
120 e = kj::runCatchingExceptions([&]() {
121 ThrowingDestructor t;
122 KJ_FAIL_ASSERT("baz") {
123 break;
124 }
125 });
126
127 KJ_IF_MAYBE(ex, e) {
128 EXPECT_EQ("baz", ex->getDescription());
129 } else {
130 ADD_FAILURE() << "Expected exception";
131 }
132 }
133 #endif
134
135 #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) || \
136 KJ_HAS_COMPILER_FEATURE(address_sanitizer) || \
137 defined(__SANITIZE_ADDRESS__)
138 // The implementation skips this check in these cases.
139 #else
140 #if !__MINGW32__ // Inexplicably crashes when exception is thrown from constructor.
141 TEST(Exception, ExceptionCallbackMustBeOnStack) {
142 KJ_EXPECT_THROW_MESSAGE("must be allocated on the stack", new ExceptionCallback);
143 }
144 #endif
145 #endif // !__MINGW32__
146
147 #if !KJ_NO_EXCEPTIONS
148 TEST(Exception, ScopeSuccessFail) {
149 bool success = false;
150 bool failure = false;
151
152 {
153 KJ_ON_SCOPE_SUCCESS(success = true);
154 KJ_ON_SCOPE_FAILURE(failure = true);
155
156 EXPECT_FALSE(success);
157 EXPECT_FALSE(failure);
158 }
159
160 EXPECT_TRUE(success);
161 EXPECT_FALSE(failure);
162
163 success = false;
164 failure = false;
165
166 try {
167 KJ_ON_SCOPE_SUCCESS(success = true);
168 KJ_ON_SCOPE_FAILURE(failure = true);
169
170 EXPECT_FALSE(success);
171 EXPECT_FALSE(failure);
172
173 throw 1;
174 } catch (int) {}
175
176 EXPECT_FALSE(success);
177 EXPECT_TRUE(failure);
178 }
179 #endif
180
181 #if __GNUG__ || defined(__clang__)
182 kj::String testStackTrace() __attribute__((noinline));
183 #elif _MSC_VER
184 __declspec(noinline) kj::String testStackTrace();
185 #endif
186
187 kj::String testStackTrace() {
188 // getStackTrace() normally skips its immediate caller, so we wrap it in another layer.
189 return getStackTrace();
190 }
191
192 KJ_TEST("getStackTrace() returns correct line number, not line + 1") {
193 // Backtraces normally produce the return address of each stack frame, but that's usually the
194 // address immediately after the one that made the call. As a result, it used to be that stack
195 // traces often pointed to the line after the one that made a call, which was confusing. This
196 // checks that this bug is fixed.
197 //
198 // This is not a very robust test, because:
199 // 1) Since symbolic stack traces are not available in many situations (e.g. release builds
200 // lacking debug symbols, systems where addr2line isn't present, etc.), we only check that
201 // the stack trace does *not* contain the *wrong* value, rather than checking that it does
202 // contain the right one.
203 // 2) This test only detects the problem if the call instruction to testStackTrace() is the
204 // *last* instruction attributed to its line of code. Whether or not this is true seems to be
205 // dependent on obscure compiler behavior. For example, below, it could only be the case if
206 // RVO is applied -- but in my testing, RVO does seem to be applied here. I tried several
207 // variations involving passing via an output parameter or a global variable rather than
208 // returning, but found some variations detected the problem and others didn't, essentially
209 // at random.
210
211 auto trace = testStackTrace();
212 auto wrong = kj::str("exception-test.c++:", __LINE__);
213
214 KJ_ASSERT(strstr(trace.cStr(), wrong.cStr()) == nullptr, trace, wrong);
215 }
216
217 #if !KJ_NO_EXCEPTIONS
218 KJ_TEST("InFlightExceptionIterator works") {
219 bool caught = false;
220 try {
221 KJ_DEFER({
222 try {
223 KJ_FAIL_ASSERT("bar");
224 } catch (const kj::Exception& e) {
225 InFlightExceptionIterator iter;
226 KJ_IF_MAYBE(e2, iter.next()) {
227 KJ_EXPECT(e2 == &e, e2->getDescription());
228 } else {
229 KJ_FAIL_EXPECT("missing first exception");
230 }
231
232 KJ_IF_MAYBE(e2, iter.next()) {
233 KJ_EXPECT(e2->getDescription() == "foo", e2->getDescription());
234 } else {
235 KJ_FAIL_EXPECT("missing second exception");
236 }
237
238 KJ_EXPECT(iter.next() == nullptr, "more than two exceptions");
239
240 caught = true;
241 }
242 });
243 KJ_FAIL_ASSERT("foo");
244 } catch (const kj::Exception& e) {
245 // expected
246 }
247
248 KJ_EXPECT(caught);
249 }
250 #endif
251
252 KJ_TEST("computeRelativeTrace") {
253 auto testCase = [](uint expectedPrefix,
254 ArrayPtr<const uintptr_t> trace, ArrayPtr<const uintptr_t> relativeTo) {
255 auto tracePtr = KJ_MAP(x, trace) { return (void*)x; };
256 auto relativeToPtr = KJ_MAP(x, relativeTo) { return (void*)x; };
257
258 auto result = computeRelativeTrace(tracePtr, relativeToPtr);
259 KJ_EXPECT(result.begin() == tracePtr.begin());
260
261 KJ_EXPECT(result.size() == expectedPrefix, trace, relativeTo, result);
262 };
263
264 testCase(8,
265 {1, 2, 3, 4, 5, 6, 7, 8},
266 {8, 7, 6, 5, 4, 3, 2, 1});
267
268 testCase(5,
269 {1, 2, 3, 4, 5, 6, 7, 8},
270 {8, 7, 6, 5, 5, 6, 7, 8});
271
272 testCase(5,
273 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
274 {8, 7, 6, 5, 5, 6, 7, 8});
275
276 testCase(5,
277 {1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8},
278 {8, 7, 6, 5, 5, 6, 7, 8});
279
280 testCase(9,
281 {1, 2, 3, 4, 5, 6, 7, 8, 5, 5, 6, 7, 8},
282 {8, 7, 6, 5, 5, 6, 7, 8});
283
284 testCase(5,
285 {1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 6, 7, 8},
286 {8, 7, 6, 5, 5, 6, 7, 8});
287
288 testCase(5,
289 {1, 2, 3, 4, 5, 6, 7, 8},
290 {8, 7, 6, 5, 5, 6, 7, 8, 7, 8});
291
292 testCase(5,
293 {1, 2, 3, 4, 5, 6, 7, 8},
294 {8, 7, 6, 5, 6, 7, 8, 7, 8});
295 }
296
297 } // namespace
298 } // namespace _ (private)
299 } // namespace kj