forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst-except.cc
83 lines (70 loc) · 1.96 KB
/
tst-except.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include <osv/debug.hh>
#include <exception>
#include <setjmp.h>
#include <memory>
int tests = 0, fails = 0;
static void report(bool ok, const char* msg)
{
++tests;
fails += !ok;
debug("%s: %s\n", (ok ? "PASS" : "FAIL"), msg);
}
jmp_buf env;
static bool saw_unhandled = false;
void myterminate()
{
debug("caught unhandled exception\n");
saw_unhandled = true;
longjmp(env, 1);
}
void function_that_throws()
{
throw 0;
}
void function_with_landing_point()
{
std::shared_ptr<int> ptr = std::make_shared<int>(7);
function_that_throws();
}
void test_unwind_resume()
{
try {
function_with_landing_point();
} catch (int x) {
report(true, "_Unwind_Resume");
}
}
int main(int ac, char** av)
{
// Test simple throw of an integer.
try {
throw 1;
report (0, "don't continue after throw");
} catch (int e) {
report (e == 1, "catch 1");
}
test_unwind_resume();
// Test that unhandled exceptions work and indeed call the termination
// function as set by std::set_terminate(). Unfortunately, this test is
// very messy, as the gcc exception handling code makes very sure an
// unhandled exception aborts the system - after calling the termination
// handler, if for some reason it didn't abort, it calls abort(), and
// even catches further exceptions and aborts. So we can only escape the
// termination handler with an ugly logjmp...
auto old = std::set_terminate(myterminate);
if (setjmp(env)) {
// Second return. Success
report(saw_unhandled, "unhandled exception\n");
} else {
throw 1;
report(false, "unhandled execption\n");
}
std::set_terminate(old);
debug("SUMMARY: %d tests, %d failures\n", tests, fails);
}