Line 0
Link Here
|
|
|
1 |
--- test/test_foreign_exceptions.cc.orig 2016-05-29 13:30:15 UTC |
2 |
+++ test/test_foreign_exceptions.cc |
3 |
@@ -0,0 +1,125 @@ |
4 |
+#include <cstdio> |
5 |
+#include <cstdlib> |
6 |
+#include "unwind.h" |
7 |
+ |
8 |
+#define EXCEPTION_CLASS(a,b,c,d,e,f,g,h) \ |
9 |
+ ((static_cast<uint64_t>(a) << 56) +\ |
10 |
+ (static_cast<uint64_t>(b) << 48) +\ |
11 |
+ (static_cast<uint64_t>(c) << 40) +\ |
12 |
+ (static_cast<uint64_t>(d) << 32) +\ |
13 |
+ (static_cast<uint64_t>(e) << 24) +\ |
14 |
+ (static_cast<uint64_t>(f) << 16) +\ |
15 |
+ (static_cast<uint64_t>(g) << 8) +\ |
16 |
+ (static_cast<uint64_t>(h))) |
17 |
+ |
18 |
+// using ld --wrap=_Unwind_RaiseException hook feature |
19 |
+extern "C" _Unwind_Reason_Code __real__Unwind_RaiseException (_Unwind_Exception *e); |
20 |
+extern "C" _Unwind_Reason_Code __wrap__Unwind_RaiseException (_Unwind_Exception *e); |
21 |
+ |
22 |
+extern "C" _Unwind_Reason_Code __wrap__Unwind_RaiseException (_Unwind_Exception *e) |
23 |
+{ |
24 |
+ // clobber exception class forcing libcxx own exceptions to be treated |
25 |
+ // as foreign exception within libcxx itself |
26 |
+ e->exception_class = EXCEPTION_CLASS('F','O','R','E','I','G','N','\0'); |
27 |
+ __real__Unwind_RaiseException(e); |
28 |
+} |
29 |
+ |
30 |
+_Unwind_Exception global_e; |
31 |
+ |
32 |
+enum test_status { |
33 |
+ PENDING, PASSED, FAILED |
34 |
+}; |
35 |
+ |
36 |
+const char test_status_str[][8] = { |
37 |
+ "PENDING", "PASSED", "FAILED" |
38 |
+}; |
39 |
+ |
40 |
+test_status test1_status = PENDING; |
41 |
+test_status test2_status = PENDING; |
42 |
+test_status test3_status = PENDING; |
43 |
+ |
44 |
+void test2_exception_cleanup(_Unwind_Reason_Code code, _Unwind_Exception *e) |
45 |
+{ |
46 |
+ fputs("(2) exception_cleanup called\n", stderr); |
47 |
+ if (e != &global_e) { |
48 |
+ fprintf(stderr, "(2) ERROR: unexpected ptr: expecting %p, got %p\n", &global_e, e); |
49 |
+ test2_status = FAILED; |
50 |
+ } |
51 |
+ if (test2_status == PENDING) |
52 |
+ test2_status = PASSED; |
53 |
+} |
54 |
+ |
55 |
+struct test3_exception |
56 |
+{ |
57 |
+ static int counter; |
58 |
+ ~test3_exception() |
59 |
+ { |
60 |
+ counter++; |
61 |
+ fputs("(3) exception dtor\n", stderr); |
62 |
+ } |
63 |
+}; |
64 |
+int test3_exception::counter = 0; |
65 |
+ |
66 |
+int main() |
67 |
+{ |
68 |
+ /////////////////////////////////////////////////////////////// |
69 |
+ fputs("(1) foreign exception, exception_cleanup=nullptr\n", stderr); |
70 |
+ try |
71 |
+ { |
72 |
+ global_e.exception_class = 0; |
73 |
+ global_e.exception_cleanup = 0; |
74 |
+ __real__Unwind_RaiseException(&global_e); |
75 |
+ } |
76 |
+ catch (...) |
77 |
+ { |
78 |
+ } |
79 |
+ test1_status = PASSED; |
80 |
+ fputs("(1) PASS\n", stderr); |
81 |
+ |
82 |
+ /////////////////////////////////////////////////////////////// |
83 |
+ fputs("(2) foreign exception, exception_cleanup present\n", stderr); |
84 |
+ try |
85 |
+ { |
86 |
+ global_e.exception_class = 0; |
87 |
+ global_e.exception_cleanup = test2_exception_cleanup; |
88 |
+ __real__Unwind_RaiseException(&global_e); |
89 |
+ } |
90 |
+ catch (...) |
91 |
+ { |
92 |
+ } |
93 |
+ fprintf(stderr, "(2) %s\n", test_status_str[test2_status]); |
94 |
+ |
95 |
+ /////////////////////////////////////////////////////////////// |
96 |
+ fputs("(3) C++ exception in foreign environment\n", stderr); |
97 |
+ int counter_expected; |
98 |
+ try |
99 |
+ { |
100 |
+ // throw was rigged such that the runtime treats C++ exceptions |
101 |
+ // as foreign ones |
102 |
+ throw test3_exception(); |
103 |
+ } |
104 |
+ catch (test3_exception&) |
105 |
+ { |
106 |
+ fputs("(3) ERROR: wrong catch\n", stderr); |
107 |
+ test3_status = FAILED; |
108 |
+ } |
109 |
+ catch (...) |
110 |
+ { |
111 |
+ fputs("(3) catch(...)\n", stderr); |
112 |
+ counter_expected = test3_exception::counter + 1; |
113 |
+ // one more dtor immediately after we leave catch |
114 |
+ } |
115 |
+ if (test3_status == PENDING && test3_exception::counter != counter_expected) { |
116 |
+ fputs("(3) ERROR: exception dtor didn't run\n", stderr); |
117 |
+ test3_status = FAILED; |
118 |
+ } |
119 |
+ if (test3_status == PENDING) |
120 |
+ test3_status = PASSED; |
121 |
+ fprintf(stderr, "(3) %s\n", test_status_str[test3_status]); |
122 |
+ |
123 |
+ /////////////////////////////////////////////////////////////// |
124 |
+ if (test1_status == PASSED && test2_status == PASSED && test3_status == PASSED) |
125 |
+ return EXIT_SUCCESS; |
126 |
+ else |
127 |
+ return EXIT_FAILURE; |
128 |
+} |