|
| 1 | +#include <cassert> |
1 | 2 | #include <cstring> |
2 | 3 | #include <cstdio> |
| 4 | +#include <vector> |
3 | 5 |
|
4 | 6 | #include "cxxabi.h" |
5 | | - |
| 7 | +#include "private_typeinfo.h" |
6 | 8 | #include "cxa_exception.h" |
7 | 9 | #include "include/atomic_support.h" |
8 | 10 |
|
| 11 | +extern "C" void setThrew(uintptr_t threw, int value); |
| 12 | +extern "C" void setTempRet0(uint32_t value); |
| 13 | + |
9 | 14 | namespace __cxxabiv1 { |
10 | 15 |
|
11 | 16 | extern "C" { |
@@ -67,46 +72,126 @@ struct __catch_info { |
67 | 72 | void* adjustedPtr; |
68 | 73 | }; |
69 | 74 |
|
70 | | -static __cxa_exception* get_exception_info(__catch_info* info) { |
71 | | - return (__cxa_exception*)info->basePtr; |
| 75 | +static __cxa_exception* get_cxa_exception(__catch_info* info) { |
| 76 | + return cxa_exception_from_thrown_object(info->basePtr); |
| 77 | +} |
| 78 | + |
| 79 | +static int is_pointer_type(std::type_info* type) { |
| 80 | + return !!dynamic_cast<__pointer_type_info*>(type); |
72 | 81 | } |
73 | 82 |
|
74 | 83 | // Get pointer which is expected to be received by catch clause in C++ code. It may be adjusted |
75 | 84 | // when the pointer is casted to some of the exception object base classes (e.g. when virtual |
76 | 85 | // inheritance is used). When a pointer is thrown this method should return the thrown pointer |
77 | 86 | // itself. |
78 | | -static void* get_thrown_object(__catch_info* info) { |
79 | | - bool isPointer = ___cxa_is_pointer_type(get_exception_info(info)->exceptionType); |
| 87 | +static void* get_exception_ptr(__catch_info* info) { |
| 88 | + bool isPointer = is_pointer_type(get_cxa_exception(info)->exceptionType); |
80 | 89 | if (isPointer) { |
81 | | - return *this.basePtr; |
| 90 | + return info->basePtr; |
82 | 91 | } |
83 | | - void* adjusted = get_adjusted_ptr(info); |
84 | | - if (adjusted) { |
85 | | - return adjusted; |
| 92 | + if (info->adjustedPtr) { |
| 93 | + return &info->adjustedPtr; |
86 | 94 | } |
87 | | - return info.basePtr; |
| 95 | + return &info->basePtr; |
88 | 96 | } |
89 | 97 |
|
90 | 98 | static int uncaughtExceptionCount; |
91 | 99 | static std::vector<__catch_info*> exceptionCaught; |
92 | 100 |
|
93 | | -void* __cxa_begin_catch(void* unwind_arg) _NOEXCEPT |
| 101 | +void* __cxa_begin_catch(void* unwind_arg) _NOEXCEPT { |
94 | 102 | __catch_info* info = (__catch_info*)unwind_arg; |
95 | | - __cxa_exception* ex = get_exception_info(info); |
96 | | - if (!ex->caught()) { |
| 103 | + __cxa_exception* ex = get_cxa_exception(info); |
| 104 | + if (!ex->caught) { |
97 | 105 | ex->caught = true; |
98 | 106 | uncaughtExceptionCount--; |
99 | 107 | } |
100 | | - info->rethrown = false; |
| 108 | + ex->rethrown = false; |
101 | 109 | exceptionCaught.push_back(info); |
102 | 110 | #if 0 |
103 | 111 | err('cxa_begin_catch ' + [ptr, 'stack', exceptionCaught]); |
104 | 112 | #endif |
105 | 113 | __cxa_increment_exception_refcount(info->basePtr); |
106 | | - return get_thrown_object(info); |
| 114 | + return get_exception_ptr(info); |
107 | 115 | } |
108 | | -#endif |
109 | 116 |
|
| 117 | +static void* exceptionLast; |
| 118 | + |
| 119 | +// We're done with a catch. Now, we can run the destructor if there is one |
| 120 | +// and free the exception. Note that if the dynCall on the destructor fails |
| 121 | +// due to calling apply on undefined, that means that the destructor is |
| 122 | +// an invalid index into the FUNCTION_TABLE, so something has gone wrong. |
| 123 | +void __cxa_end_catch() { |
| 124 | + // Clear state flag. |
| 125 | + setThrew(0, 0); |
| 126 | + assert(exceptionCaught.size() > 0); |
| 127 | + // Call destructor if one is registered then clear it. |
| 128 | + __catch_info* catchInfo = exceptionCaught.back(); |
| 129 | + exceptionCaught.pop_back(); |
| 130 | + |
| 131 | + //err('cxa_end_catch popped ' + [catchInfo, exceptionLast, 'stack', exceptionCaught]); |
| 132 | + __cxa_decrement_exception_refcount(catchInfo->basePtr); |
| 133 | + ::free(catchInfo); |
| 134 | + exceptionLast = 0; // XXX in decRef? |
110 | 135 | } |
111 | 136 |
|
| 137 | +int __cxa_can_catch(std::type_info* catchType, std::type_info* excpType, void **thrown); |
| 138 | + |
| 139 | +// Finds a suitable catch clause for when an exception is thrown. |
| 140 | +// In normal compilers, this functionality is handled by the C++ |
| 141 | +// 'personality' routine. This is passed a fairly complex structure |
| 142 | +// relating to the context of the exception and makes judgements |
| 143 | +// about how to handle it. Some of it is about matching a suitable |
| 144 | +// catch clause, and some of it is about unwinding. We already handle |
| 145 | +// unwinding using 'if' blocks around each function, so the remaining |
| 146 | +// functionality boils down to picking a suitable 'catch' block. |
| 147 | +// We'll do that here, instead, to keep things simpler. |
| 148 | +void* __cxa_find_matching_catch_v(int count, ...) { |
| 149 | + void* thrown = exceptionLast; |
| 150 | + if (!thrown) { |
| 151 | + // just pass through the null ptr |
| 152 | + setTempRet0(0); |
| 153 | + return 0; |
| 154 | + } |
| 155 | + __cxa_exception* info = cxa_exception_from_thrown_object(thrown); |
| 156 | + std::type_info* thrownType = info->exceptionType; |
| 157 | + __catch_info CatchInfo{thrown, 0} |
| 158 | + if (!thrownType) { |
| 159 | + // just pass through the thrown ptr |
| 160 | + setTempRet0(0); |
| 161 | + return catchInfo.basePtr; |
| 162 | + } |
| 163 | + |
| 164 | + // can_catch receives a **, add indirection |
| 165 | + //out("can_catch on " + [thrown]); |
| 166 | + void* exceptionThrown = 0; |
| 167 | + // The different catch blocks are denoted by different types. |
| 168 | + // Due to inheritance, those types may not precisely match the |
| 169 | + // type of the thrown object. Find one which matches, and |
| 170 | + // return the type of the catch block which should be called. |
| 171 | + va_list ap; |
| 172 | + va_start(ap, count); |
| 173 | + for (int i = 0; i < count; i++) { |
| 174 | + std::type_info* caughtType = va_arg(va_list ap, std::type_info*); |
| 175 | + if (caughtType === 0 || caughtType === thrownType) { |
| 176 | + // Catch all clause matched or exactly the same type is caught |
| 177 | + break; |
| 178 | + } |
| 179 | + if (__cxa_can_catch(caughtType, thrownType, &exceptionThrown) { |
| 180 | + if (thrown !== exceptionThrown) { |
| 181 | + catchInfo.set_adjusted_ptr(exceptionThrown); |
| 182 | + } |
| 183 | + //out(" can_catch found " + [adjusted, caughtType]); |
| 184 | + setTempRet0(caughtType); |
| 185 | + return makeStructuralReturn(catchInfo.basePtr); |
| 186 | + } |
| 187 | + } |
| 188 | + va_end(ap); |
| 189 | + setTempRet0(thrownType); |
| 190 | + return catchInfo.basePtr; |
| 191 | +}, |
| 192 | + |
| 193 | +#endif // __USING_EMSCRIPTEN_EXCEPTIONS__ |
| 194 | + |
| 195 | +} // extern "C" |
| 196 | + |
112 | 197 | } // abi |
0 commit comments