annotate libcxxabi/src/cxa_personality.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===------------------------- cxa_exception.cpp --------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //
anatofuz
parents:
diff changeset
8 // This file implements the "Exception Handling APIs"
anatofuz
parents:
diff changeset
9 // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
anatofuz
parents:
diff changeset
10 // http://www.intel.com/design/itanium/downloads/245358.htm
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include <assert.h>
anatofuz
parents:
diff changeset
15 #include <stdlib.h>
anatofuz
parents:
diff changeset
16 #include <string.h>
anatofuz
parents:
diff changeset
17 #include <typeinfo>
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 #include "__cxxabi_config.h"
anatofuz
parents:
diff changeset
20 #include "cxa_exception.h"
anatofuz
parents:
diff changeset
21 #include "cxa_handlers.h"
anatofuz
parents:
diff changeset
22 #include "private_typeinfo.h"
anatofuz
parents:
diff changeset
23 #include "unwind.h"
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
anatofuz
parents:
diff changeset
26 #include <windows.h>
anatofuz
parents:
diff changeset
27 #include <winnt.h>
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 extern "C" EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD,
anatofuz
parents:
diff changeset
30 void *, PCONTEXT,
anatofuz
parents:
diff changeset
31 PDISPATCHER_CONTEXT,
anatofuz
parents:
diff changeset
32 _Unwind_Personality_Fn);
anatofuz
parents:
diff changeset
33 #endif
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 /*
anatofuz
parents:
diff changeset
36 Exception Header Layout:
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 +---------------------------+-----------------------------+---------------+
anatofuz
parents:
diff changeset
39 | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
anatofuz
parents:
diff changeset
40 +---------------------------+-----------------------------+---------------+
anatofuz
parents:
diff changeset
41 ^
anatofuz
parents:
diff changeset
42 |
anatofuz
parents:
diff changeset
43 +-------------------------------------------------------+
anatofuz
parents:
diff changeset
44 |
anatofuz
parents:
diff changeset
45 +---------------------------+-----------------------------+
anatofuz
parents:
diff changeset
46 | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
anatofuz
parents:
diff changeset
47 +---------------------------+-----------------------------+
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 Exception Handling Table Layout:
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 +-----------------+--------+
anatofuz
parents:
diff changeset
52 | lpStartEncoding | (char) |
anatofuz
parents:
diff changeset
53 +---------+-------+--------+---------------+-----------------------+
anatofuz
parents:
diff changeset
54 | lpStart | (encoded with lpStartEncoding) | defaults to funcStart |
anatofuz
parents:
diff changeset
55 +---------+-----+--------+-----------------+---------------+-------+
anatofuz
parents:
diff changeset
56 | ttypeEncoding | (char) | Encoding of the type_info table |
anatofuz
parents:
diff changeset
57 +---------------+-+------+----+----------------------------+----------------+
anatofuz
parents:
diff changeset
58 | classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null |
anatofuz
parents:
diff changeset
59 +-----------------++--------+-+----------------------------+----------------+
anatofuz
parents:
diff changeset
60 | callSiteEncoding | (char) | Encoding for Call Site Table |
anatofuz
parents:
diff changeset
61 +------------------+--+-----+-----+------------------------+--------------------------+
anatofuz
parents:
diff changeset
62 | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table |
anatofuz
parents:
diff changeset
63 +---------------------+-----------+---------------------------------------------------+
anatofuz
parents:
diff changeset
64 #ifndef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
65 +---------------------+-----------+------------------------------------------------+
anatofuz
parents:
diff changeset
66 | Beginning of Call Site Table The current ip lies within the |
anatofuz
parents:
diff changeset
67 | ... (start, length) range of one of these |
anatofuz
parents:
diff changeset
68 | call sites. There may be action needed. |
anatofuz
parents:
diff changeset
69 | +-------------+---------------------------------+------------------------------+ |
anatofuz
parents:
diff changeset
70 | | start | (encoded with callSiteEncoding) | offset relative to funcStart | |
anatofuz
parents:
diff changeset
71 | | length | (encoded with callSiteEncoding) | length of code fragment | |
anatofuz
parents:
diff changeset
72 | | landingPad | (encoded with callSiteEncoding) | offset relative to lpStart | |
anatofuz
parents:
diff changeset
73 | | actionEntry | (ULEB128) | Action Table Index 1-based | |
anatofuz
parents:
diff changeset
74 | | | | actionEntry == 0 -> cleanup | |
anatofuz
parents:
diff changeset
75 | +-------------+---------------------------------+------------------------------+ |
anatofuz
parents:
diff changeset
76 | ... |
anatofuz
parents:
diff changeset
77 +----------------------------------------------------------------------------------+
anatofuz
parents:
diff changeset
78 #else // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
79 +---------------------+-----------+------------------------------------------------+
anatofuz
parents:
diff changeset
80 | Beginning of Call Site Table The current ip is a 1-based index into |
anatofuz
parents:
diff changeset
81 | ... this table. Or it is -1 meaning no |
anatofuz
parents:
diff changeset
82 | action is needed. Or it is 0 meaning |
anatofuz
parents:
diff changeset
83 | terminate. |
anatofuz
parents:
diff changeset
84 | +-------------+---------------------------------+------------------------------+ |
anatofuz
parents:
diff changeset
85 | | landingPad | (ULEB128) | offset relative to lpStart | |
anatofuz
parents:
diff changeset
86 | | actionEntry | (ULEB128) | Action Table Index 1-based | |
anatofuz
parents:
diff changeset
87 | | | | actionEntry == 0 -> cleanup | |
anatofuz
parents:
diff changeset
88 | +-------------+---------------------------------+------------------------------+ |
anatofuz
parents:
diff changeset
89 | ... |
anatofuz
parents:
diff changeset
90 +----------------------------------------------------------------------------------+
anatofuz
parents:
diff changeset
91 #endif // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
92 +---------------------------------------------------------------------+
anatofuz
parents:
diff changeset
93 | Beginning of Action Table ttypeIndex == 0 : cleanup |
anatofuz
parents:
diff changeset
94 | ... ttypeIndex > 0 : catch |
anatofuz
parents:
diff changeset
95 | ttypeIndex < 0 : exception spec |
anatofuz
parents:
diff changeset
96 | +--------------+-----------+--------------------------------------+ |
anatofuz
parents:
diff changeset
97 | | ttypeIndex | (SLEB128) | Index into type_info Table (1-based) | |
anatofuz
parents:
diff changeset
98 | | actionOffset | (SLEB128) | Offset into next Action Table entry | |
anatofuz
parents:
diff changeset
99 | +--------------+-----------+--------------------------------------+ |
anatofuz
parents:
diff changeset
100 | ... |
anatofuz
parents:
diff changeset
101 +---------------------------------------------------------------------+-----------------+
anatofuz
parents:
diff changeset
102 | type_info Table, but classInfoOffset does *not* point here! |
anatofuz
parents:
diff changeset
103 | +----------------+------------------------------------------------+-----------------+ |
anatofuz
parents:
diff changeset
104 | | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | |
anatofuz
parents:
diff changeset
105 | +----------------+------------------------------------------------+-----------------+ |
anatofuz
parents:
diff changeset
106 | ... |
anatofuz
parents:
diff changeset
107 | +----------------+------------------------------------------------+-----------------+ |
anatofuz
parents:
diff changeset
108 | | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | |
anatofuz
parents:
diff changeset
109 | +----------------+------------------------------------------------+-----------------+ |
anatofuz
parents:
diff changeset
110 | +---------------------------------------+-----------+------------------------------+ |
anatofuz
parents:
diff changeset
111 | | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! | |
anatofuz
parents:
diff changeset
112 | | ... | (ULEB128) | | |
anatofuz
parents:
diff changeset
113 | | Mth ttypeIndex for 1st exception spec | (ULEB128) | | |
anatofuz
parents:
diff changeset
114 | | 0 | (ULEB128) | | |
anatofuz
parents:
diff changeset
115 | +---------------------------------------+------------------------------------------+ |
anatofuz
parents:
diff changeset
116 | ... |
anatofuz
parents:
diff changeset
117 | +---------------------------------------+------------------------------------------+ |
anatofuz
parents:
diff changeset
118 | | 0 | (ULEB128) | throw() | |
anatofuz
parents:
diff changeset
119 | +---------------------------------------+------------------------------------------+ |
anatofuz
parents:
diff changeset
120 | ... |
anatofuz
parents:
diff changeset
121 | +---------------------------------------+------------------------------------------+ |
anatofuz
parents:
diff changeset
122 | | 1st ttypeIndex for Nth exception spec | (ULEB128) | | |
anatofuz
parents:
diff changeset
123 | | ... | (ULEB128) | | |
anatofuz
parents:
diff changeset
124 | | Mth ttypeIndex for Nth exception spec | (ULEB128) | | |
anatofuz
parents:
diff changeset
125 | | 0 | (ULEB128) | | |
anatofuz
parents:
diff changeset
126 | +---------------------------------------+------------------------------------------+ |
anatofuz
parents:
diff changeset
127 +---------------------------------------------------------------------------------------+
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 Notes:
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 * ttypeIndex in the Action Table, and in the exception spec table, is an index,
anatofuz
parents:
diff changeset
132 not a byte count, if positive. It is a negative index offset of
anatofuz
parents:
diff changeset
133 classInfoOffset and the sizeof entry depends on ttypeEncoding.
anatofuz
parents:
diff changeset
134 But if ttypeIndex is negative, it is a positive 1-based byte offset into the
anatofuz
parents:
diff changeset
135 type_info Table.
anatofuz
parents:
diff changeset
136 And if ttypeIndex is zero, it refers to a catch (...).
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 * landingPad can be 0, this implies there is nothing to be done.
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 * landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done
anatofuz
parents:
diff changeset
141 @landingPad.
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 * A cleanup can also be found under landingPad != 0 and actionEntry != 0 in
anatofuz
parents:
diff changeset
144 the Action Table with ttypeIndex == 0.
anatofuz
parents:
diff changeset
145 */
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 namespace __cxxabiv1
anatofuz
parents:
diff changeset
148 {
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 namespace
anatofuz
parents:
diff changeset
151 {
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 template <class AsType>
anatofuz
parents:
diff changeset
154 uintptr_t readPointerHelper(const uint8_t*& p) {
anatofuz
parents:
diff changeset
155 AsType value;
anatofuz
parents:
diff changeset
156 memcpy(&value, p, sizeof(AsType));
anatofuz
parents:
diff changeset
157 p += sizeof(AsType);
anatofuz
parents:
diff changeset
158 return static_cast<uintptr_t>(value);
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160
anatofuz
parents:
diff changeset
161 } // end namespace
anatofuz
parents:
diff changeset
162
anatofuz
parents:
diff changeset
163 extern "C"
anatofuz
parents:
diff changeset
164 {
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 // private API
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 // Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 // DWARF Constants
anatofuz
parents:
diff changeset
171 enum
anatofuz
parents:
diff changeset
172 {
anatofuz
parents:
diff changeset
173 DW_EH_PE_absptr = 0x00,
anatofuz
parents:
diff changeset
174 DW_EH_PE_uleb128 = 0x01,
anatofuz
parents:
diff changeset
175 DW_EH_PE_udata2 = 0x02,
anatofuz
parents:
diff changeset
176 DW_EH_PE_udata4 = 0x03,
anatofuz
parents:
diff changeset
177 DW_EH_PE_udata8 = 0x04,
anatofuz
parents:
diff changeset
178 DW_EH_PE_sleb128 = 0x09,
anatofuz
parents:
diff changeset
179 DW_EH_PE_sdata2 = 0x0A,
anatofuz
parents:
diff changeset
180 DW_EH_PE_sdata4 = 0x0B,
anatofuz
parents:
diff changeset
181 DW_EH_PE_sdata8 = 0x0C,
anatofuz
parents:
diff changeset
182 DW_EH_PE_pcrel = 0x10,
anatofuz
parents:
diff changeset
183 DW_EH_PE_textrel = 0x20,
anatofuz
parents:
diff changeset
184 DW_EH_PE_datarel = 0x30,
anatofuz
parents:
diff changeset
185 DW_EH_PE_funcrel = 0x40,
anatofuz
parents:
diff changeset
186 DW_EH_PE_aligned = 0x50,
anatofuz
parents:
diff changeset
187 DW_EH_PE_indirect = 0x80,
anatofuz
parents:
diff changeset
188 DW_EH_PE_omit = 0xFF
anatofuz
parents:
diff changeset
189 };
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 /// Read a uleb128 encoded value and advance pointer
anatofuz
parents:
diff changeset
192 /// See Variable Length Data Appendix C in:
anatofuz
parents:
diff changeset
193 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
anatofuz
parents:
diff changeset
194 /// @param data reference variable holding memory pointer to decode from
anatofuz
parents:
diff changeset
195 /// @returns decoded value
anatofuz
parents:
diff changeset
196 static
anatofuz
parents:
diff changeset
197 uintptr_t
anatofuz
parents:
diff changeset
198 readULEB128(const uint8_t** data)
anatofuz
parents:
diff changeset
199 {
anatofuz
parents:
diff changeset
200 uintptr_t result = 0;
anatofuz
parents:
diff changeset
201 uintptr_t shift = 0;
anatofuz
parents:
diff changeset
202 unsigned char byte;
anatofuz
parents:
diff changeset
203 const uint8_t *p = *data;
anatofuz
parents:
diff changeset
204 do
anatofuz
parents:
diff changeset
205 {
anatofuz
parents:
diff changeset
206 byte = *p++;
anatofuz
parents:
diff changeset
207 result |= static_cast<uintptr_t>(byte & 0x7F) << shift;
anatofuz
parents:
diff changeset
208 shift += 7;
anatofuz
parents:
diff changeset
209 } while (byte & 0x80);
anatofuz
parents:
diff changeset
210 *data = p;
anatofuz
parents:
diff changeset
211 return result;
anatofuz
parents:
diff changeset
212 }
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 /// Read a sleb128 encoded value and advance pointer
anatofuz
parents:
diff changeset
215 /// See Variable Length Data Appendix C in:
anatofuz
parents:
diff changeset
216 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
anatofuz
parents:
diff changeset
217 /// @param data reference variable holding memory pointer to decode from
anatofuz
parents:
diff changeset
218 /// @returns decoded value
anatofuz
parents:
diff changeset
219 static
anatofuz
parents:
diff changeset
220 intptr_t
anatofuz
parents:
diff changeset
221 readSLEB128(const uint8_t** data)
anatofuz
parents:
diff changeset
222 {
anatofuz
parents:
diff changeset
223 uintptr_t result = 0;
anatofuz
parents:
diff changeset
224 uintptr_t shift = 0;
anatofuz
parents:
diff changeset
225 unsigned char byte;
anatofuz
parents:
diff changeset
226 const uint8_t *p = *data;
anatofuz
parents:
diff changeset
227 do
anatofuz
parents:
diff changeset
228 {
anatofuz
parents:
diff changeset
229 byte = *p++;
anatofuz
parents:
diff changeset
230 result |= static_cast<uintptr_t>(byte & 0x7F) << shift;
anatofuz
parents:
diff changeset
231 shift += 7;
anatofuz
parents:
diff changeset
232 } while (byte & 0x80);
anatofuz
parents:
diff changeset
233 *data = p;
anatofuz
parents:
diff changeset
234 if ((byte & 0x40) && (shift < (sizeof(result) << 3)))
anatofuz
parents:
diff changeset
235 result |= static_cast<uintptr_t>(~0) << shift;
anatofuz
parents:
diff changeset
236 return static_cast<intptr_t>(result);
anatofuz
parents:
diff changeset
237 }
anatofuz
parents:
diff changeset
238
anatofuz
parents:
diff changeset
239 /// Read a pointer encoded value and advance pointer
anatofuz
parents:
diff changeset
240 /// See Variable Length Data in:
anatofuz
parents:
diff changeset
241 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
anatofuz
parents:
diff changeset
242 /// @param data reference variable holding memory pointer to decode from
anatofuz
parents:
diff changeset
243 /// @param encoding dwarf encoding type
anatofuz
parents:
diff changeset
244 /// @returns decoded value
anatofuz
parents:
diff changeset
245 static
anatofuz
parents:
diff changeset
246 uintptr_t
anatofuz
parents:
diff changeset
247 readEncodedPointer(const uint8_t** data, uint8_t encoding)
anatofuz
parents:
diff changeset
248 {
anatofuz
parents:
diff changeset
249 uintptr_t result = 0;
anatofuz
parents:
diff changeset
250 if (encoding == DW_EH_PE_omit)
anatofuz
parents:
diff changeset
251 return result;
anatofuz
parents:
diff changeset
252 const uint8_t* p = *data;
anatofuz
parents:
diff changeset
253 // first get value
anatofuz
parents:
diff changeset
254 switch (encoding & 0x0F)
anatofuz
parents:
diff changeset
255 {
anatofuz
parents:
diff changeset
256 case DW_EH_PE_absptr:
anatofuz
parents:
diff changeset
257 result = readPointerHelper<uintptr_t>(p);
anatofuz
parents:
diff changeset
258 break;
anatofuz
parents:
diff changeset
259 case DW_EH_PE_uleb128:
anatofuz
parents:
diff changeset
260 result = readULEB128(&p);
anatofuz
parents:
diff changeset
261 break;
anatofuz
parents:
diff changeset
262 case DW_EH_PE_sleb128:
anatofuz
parents:
diff changeset
263 result = static_cast<uintptr_t>(readSLEB128(&p));
anatofuz
parents:
diff changeset
264 break;
anatofuz
parents:
diff changeset
265 case DW_EH_PE_udata2:
anatofuz
parents:
diff changeset
266 result = readPointerHelper<uint16_t>(p);
anatofuz
parents:
diff changeset
267 break;
anatofuz
parents:
diff changeset
268 case DW_EH_PE_udata4:
anatofuz
parents:
diff changeset
269 result = readPointerHelper<uint32_t>(p);
anatofuz
parents:
diff changeset
270 break;
anatofuz
parents:
diff changeset
271 case DW_EH_PE_udata8:
anatofuz
parents:
diff changeset
272 result = readPointerHelper<uint64_t>(p);
anatofuz
parents:
diff changeset
273 break;
anatofuz
parents:
diff changeset
274 case DW_EH_PE_sdata2:
anatofuz
parents:
diff changeset
275 result = readPointerHelper<int16_t>(p);
anatofuz
parents:
diff changeset
276 break;
anatofuz
parents:
diff changeset
277 case DW_EH_PE_sdata4:
anatofuz
parents:
diff changeset
278 result = readPointerHelper<int32_t>(p);
anatofuz
parents:
diff changeset
279 break;
anatofuz
parents:
diff changeset
280 case DW_EH_PE_sdata8:
anatofuz
parents:
diff changeset
281 result = readPointerHelper<int64_t>(p);
anatofuz
parents:
diff changeset
282 break;
anatofuz
parents:
diff changeset
283 default:
anatofuz
parents:
diff changeset
284 // not supported
anatofuz
parents:
diff changeset
285 abort();
anatofuz
parents:
diff changeset
286 break;
anatofuz
parents:
diff changeset
287 }
anatofuz
parents:
diff changeset
288 // then add relative offset
anatofuz
parents:
diff changeset
289 switch (encoding & 0x70)
anatofuz
parents:
diff changeset
290 {
anatofuz
parents:
diff changeset
291 case DW_EH_PE_absptr:
anatofuz
parents:
diff changeset
292 // do nothing
anatofuz
parents:
diff changeset
293 break;
anatofuz
parents:
diff changeset
294 case DW_EH_PE_pcrel:
anatofuz
parents:
diff changeset
295 if (result)
anatofuz
parents:
diff changeset
296 result += (uintptr_t)(*data);
anatofuz
parents:
diff changeset
297 break;
anatofuz
parents:
diff changeset
298 case DW_EH_PE_textrel:
anatofuz
parents:
diff changeset
299 case DW_EH_PE_datarel:
anatofuz
parents:
diff changeset
300 case DW_EH_PE_funcrel:
anatofuz
parents:
diff changeset
301 case DW_EH_PE_aligned:
anatofuz
parents:
diff changeset
302 default:
anatofuz
parents:
diff changeset
303 // not supported
anatofuz
parents:
diff changeset
304 abort();
anatofuz
parents:
diff changeset
305 break;
anatofuz
parents:
diff changeset
306 }
anatofuz
parents:
diff changeset
307 // then apply indirection
anatofuz
parents:
diff changeset
308 if (result && (encoding & DW_EH_PE_indirect))
anatofuz
parents:
diff changeset
309 result = *((uintptr_t*)result);
anatofuz
parents:
diff changeset
310 *data = p;
anatofuz
parents:
diff changeset
311 return result;
anatofuz
parents:
diff changeset
312 }
anatofuz
parents:
diff changeset
313
anatofuz
parents:
diff changeset
314 static
anatofuz
parents:
diff changeset
315 void
anatofuz
parents:
diff changeset
316 call_terminate(bool native_exception, _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
317 {
anatofuz
parents:
diff changeset
318 __cxa_begin_catch(unwind_exception);
anatofuz
parents:
diff changeset
319 if (native_exception)
anatofuz
parents:
diff changeset
320 {
anatofuz
parents:
diff changeset
321 // Use the stored terminate_handler if possible
anatofuz
parents:
diff changeset
322 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
323 std::__terminate(exception_header->terminateHandler);
anatofuz
parents:
diff changeset
324 }
anatofuz
parents:
diff changeset
325 std::terminate();
anatofuz
parents:
diff changeset
326 }
anatofuz
parents:
diff changeset
327
anatofuz
parents:
diff changeset
328 #if defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
329 static const void* read_target2_value(const void* ptr)
anatofuz
parents:
diff changeset
330 {
anatofuz
parents:
diff changeset
331 uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr);
anatofuz
parents:
diff changeset
332 if (!offset)
anatofuz
parents:
diff changeset
333 return 0;
anatofuz
parents:
diff changeset
334 // "ARM EABI provides a TARGET2 relocation to describe these typeinfo
anatofuz
parents:
diff changeset
335 // pointers. The reason being it allows their precise semantics to be
anatofuz
parents:
diff changeset
336 // deferred to the linker. For bare-metal they turn into absolute
anatofuz
parents:
diff changeset
337 // relocations. For linux they turn into GOT-REL relocations."
anatofuz
parents:
diff changeset
338 // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html
anatofuz
parents:
diff changeset
339 #if defined(LIBCXXABI_BAREMETAL)
anatofuz
parents:
diff changeset
340 return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) +
anatofuz
parents:
diff changeset
341 offset);
anatofuz
parents:
diff changeset
342 #else
anatofuz
parents:
diff changeset
343 return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) +
anatofuz
parents:
diff changeset
344 offset);
anatofuz
parents:
diff changeset
345 #endif
anatofuz
parents:
diff changeset
346 }
anatofuz
parents:
diff changeset
347
anatofuz
parents:
diff changeset
348 static const __shim_type_info*
anatofuz
parents:
diff changeset
349 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,
anatofuz
parents:
diff changeset
350 uint8_t ttypeEncoding, bool native_exception,
anatofuz
parents:
diff changeset
351 _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
352 {
anatofuz
parents:
diff changeset
353 if (classInfo == 0)
anatofuz
parents:
diff changeset
354 {
anatofuz
parents:
diff changeset
355 // this should not happen. Indicates corrupted eh_table.
anatofuz
parents:
diff changeset
356 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
357 }
anatofuz
parents:
diff changeset
358
anatofuz
parents:
diff changeset
359 assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6
anatofuz
parents:
diff changeset
360 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal
anatofuz
parents:
diff changeset
361 (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux
anatofuz
parents:
diff changeset
362 "Unexpected TTypeEncoding");
anatofuz
parents:
diff changeset
363 (void)ttypeEncoding;
anatofuz
parents:
diff changeset
364
anatofuz
parents:
diff changeset
365 const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t);
anatofuz
parents:
diff changeset
366 return reinterpret_cast<const __shim_type_info *>(
anatofuz
parents:
diff changeset
367 read_target2_value(ttypePtr));
anatofuz
parents:
diff changeset
368 }
anatofuz
parents:
diff changeset
369 #else // !defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
370 static
anatofuz
parents:
diff changeset
371 const __shim_type_info*
anatofuz
parents:
diff changeset
372 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,
anatofuz
parents:
diff changeset
373 uint8_t ttypeEncoding, bool native_exception,
anatofuz
parents:
diff changeset
374 _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
375 {
anatofuz
parents:
diff changeset
376 if (classInfo == 0)
anatofuz
parents:
diff changeset
377 {
anatofuz
parents:
diff changeset
378 // this should not happen. Indicates corrupted eh_table.
anatofuz
parents:
diff changeset
379 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
380 }
anatofuz
parents:
diff changeset
381 switch (ttypeEncoding & 0x0F)
anatofuz
parents:
diff changeset
382 {
anatofuz
parents:
diff changeset
383 case DW_EH_PE_absptr:
anatofuz
parents:
diff changeset
384 ttypeIndex *= sizeof(void*);
anatofuz
parents:
diff changeset
385 break;
anatofuz
parents:
diff changeset
386 case DW_EH_PE_udata2:
anatofuz
parents:
diff changeset
387 case DW_EH_PE_sdata2:
anatofuz
parents:
diff changeset
388 ttypeIndex *= 2;
anatofuz
parents:
diff changeset
389 break;
anatofuz
parents:
diff changeset
390 case DW_EH_PE_udata4:
anatofuz
parents:
diff changeset
391 case DW_EH_PE_sdata4:
anatofuz
parents:
diff changeset
392 ttypeIndex *= 4;
anatofuz
parents:
diff changeset
393 break;
anatofuz
parents:
diff changeset
394 case DW_EH_PE_udata8:
anatofuz
parents:
diff changeset
395 case DW_EH_PE_sdata8:
anatofuz
parents:
diff changeset
396 ttypeIndex *= 8;
anatofuz
parents:
diff changeset
397 break;
anatofuz
parents:
diff changeset
398 default:
anatofuz
parents:
diff changeset
399 // this should not happen. Indicates corrupted eh_table.
anatofuz
parents:
diff changeset
400 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
401 }
anatofuz
parents:
diff changeset
402 classInfo -= ttypeIndex;
anatofuz
parents:
diff changeset
403 return (const __shim_type_info*)readEncodedPointer(&classInfo, ttypeEncoding);
anatofuz
parents:
diff changeset
404 }
anatofuz
parents:
diff changeset
405 #endif // !defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
406
anatofuz
parents:
diff changeset
407 /*
anatofuz
parents:
diff changeset
408 This is checking a thrown exception type, excpType, against a possibly empty
anatofuz
parents:
diff changeset
409 list of catchType's which make up an exception spec.
anatofuz
parents:
diff changeset
410
anatofuz
parents:
diff changeset
411 An exception spec acts like a catch handler, but in reverse. This "catch
anatofuz
parents:
diff changeset
412 handler" will catch an excpType if and only if none of the catchType's in
anatofuz
parents:
diff changeset
413 the list will catch a excpType. If any catchType in the list can catch an
anatofuz
parents:
diff changeset
414 excpType, then this exception spec does not catch the excpType.
anatofuz
parents:
diff changeset
415 */
anatofuz
parents:
diff changeset
416 #if defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
417 static
anatofuz
parents:
diff changeset
418 bool
anatofuz
parents:
diff changeset
419 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,
anatofuz
parents:
diff changeset
420 uint8_t ttypeEncoding, const __shim_type_info* excpType,
anatofuz
parents:
diff changeset
421 void* adjustedPtr, _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
422 {
anatofuz
parents:
diff changeset
423 if (classInfo == 0)
anatofuz
parents:
diff changeset
424 {
anatofuz
parents:
diff changeset
425 // this should not happen. Indicates corrupted eh_table.
anatofuz
parents:
diff changeset
426 call_terminate(false, unwind_exception);
anatofuz
parents:
diff changeset
427 }
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 assert(((ttypeEncoding == DW_EH_PE_absptr) || // LLVM or GCC 4.6
anatofuz
parents:
diff changeset
430 (ttypeEncoding == DW_EH_PE_pcrel) || // GCC 4.7 baremetal
anatofuz
parents:
diff changeset
431 (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) && // GCC 4.7 linux
anatofuz
parents:
diff changeset
432 "Unexpected TTypeEncoding");
anatofuz
parents:
diff changeset
433 (void)ttypeEncoding;
anatofuz
parents:
diff changeset
434
anatofuz
parents:
diff changeset
435 // specIndex is negative of 1-based byte offset into classInfo;
anatofuz
parents:
diff changeset
436 specIndex = -specIndex;
anatofuz
parents:
diff changeset
437 --specIndex;
anatofuz
parents:
diff changeset
438 const void** temp = reinterpret_cast<const void**>(
anatofuz
parents:
diff changeset
439 reinterpret_cast<uintptr_t>(classInfo) +
anatofuz
parents:
diff changeset
440 static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t));
anatofuz
parents:
diff changeset
441 // If any type in the spec list can catch excpType, return false, else return true
anatofuz
parents:
diff changeset
442 // adjustments to adjustedPtr are ignored.
anatofuz
parents:
diff changeset
443 while (true)
anatofuz
parents:
diff changeset
444 {
anatofuz
parents:
diff changeset
445 // ARM EHABI exception specification table (filter table) consists of
anatofuz
parents:
diff changeset
446 // several pointers which will directly point to the type info object
anatofuz
parents:
diff changeset
447 // (instead of ttypeIndex). The table will be terminated with 0.
anatofuz
parents:
diff changeset
448 const void** ttypePtr = temp++;
anatofuz
parents:
diff changeset
449 if (*ttypePtr == 0)
anatofuz
parents:
diff changeset
450 break;
anatofuz
parents:
diff changeset
451 // We can get the __shim_type_info simply by performing a
anatofuz
parents:
diff changeset
452 // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info.
anatofuz
parents:
diff changeset
453 const __shim_type_info* catchType =
anatofuz
parents:
diff changeset
454 static_cast<const __shim_type_info*>(read_target2_value(ttypePtr));
anatofuz
parents:
diff changeset
455 void* tempPtr = adjustedPtr;
anatofuz
parents:
diff changeset
456 if (catchType->can_catch(excpType, tempPtr))
anatofuz
parents:
diff changeset
457 return false;
anatofuz
parents:
diff changeset
458 }
anatofuz
parents:
diff changeset
459 return true;
anatofuz
parents:
diff changeset
460 }
anatofuz
parents:
diff changeset
461 #else
anatofuz
parents:
diff changeset
462 static
anatofuz
parents:
diff changeset
463 bool
anatofuz
parents:
diff changeset
464 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,
anatofuz
parents:
diff changeset
465 uint8_t ttypeEncoding, const __shim_type_info* excpType,
anatofuz
parents:
diff changeset
466 void* adjustedPtr, _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
467 {
anatofuz
parents:
diff changeset
468 if (classInfo == 0)
anatofuz
parents:
diff changeset
469 {
anatofuz
parents:
diff changeset
470 // this should not happen. Indicates corrupted eh_table.
anatofuz
parents:
diff changeset
471 call_terminate(false, unwind_exception);
anatofuz
parents:
diff changeset
472 }
anatofuz
parents:
diff changeset
473 // specIndex is negative of 1-based byte offset into classInfo;
anatofuz
parents:
diff changeset
474 specIndex = -specIndex;
anatofuz
parents:
diff changeset
475 --specIndex;
anatofuz
parents:
diff changeset
476 const uint8_t* temp = classInfo + specIndex;
anatofuz
parents:
diff changeset
477 // If any type in the spec list can catch excpType, return false, else return true
anatofuz
parents:
diff changeset
478 // adjustments to adjustedPtr are ignored.
anatofuz
parents:
diff changeset
479 while (true)
anatofuz
parents:
diff changeset
480 {
anatofuz
parents:
diff changeset
481 uint64_t ttypeIndex = readULEB128(&temp);
anatofuz
parents:
diff changeset
482 if (ttypeIndex == 0)
anatofuz
parents:
diff changeset
483 break;
anatofuz
parents:
diff changeset
484 const __shim_type_info* catchType = get_shim_type_info(ttypeIndex,
anatofuz
parents:
diff changeset
485 classInfo,
anatofuz
parents:
diff changeset
486 ttypeEncoding,
anatofuz
parents:
diff changeset
487 true,
anatofuz
parents:
diff changeset
488 unwind_exception);
anatofuz
parents:
diff changeset
489 void* tempPtr = adjustedPtr;
anatofuz
parents:
diff changeset
490 if (catchType->can_catch(excpType, tempPtr))
anatofuz
parents:
diff changeset
491 return false;
anatofuz
parents:
diff changeset
492 }
anatofuz
parents:
diff changeset
493 return true;
anatofuz
parents:
diff changeset
494 }
anatofuz
parents:
diff changeset
495 #endif
anatofuz
parents:
diff changeset
496
anatofuz
parents:
diff changeset
497 static
anatofuz
parents:
diff changeset
498 void*
anatofuz
parents:
diff changeset
499 get_thrown_object_ptr(_Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
500 {
anatofuz
parents:
diff changeset
501 // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1
anatofuz
parents:
diff changeset
502 // Regardless, this library is prohibited from touching a foreign exception
anatofuz
parents:
diff changeset
503 void* adjustedPtr = unwind_exception + 1;
anatofuz
parents:
diff changeset
504 if (__getExceptionClass(unwind_exception) == kOurDependentExceptionClass)
anatofuz
parents:
diff changeset
505 adjustedPtr = ((__cxa_dependent_exception*)adjustedPtr - 1)->primaryException;
anatofuz
parents:
diff changeset
506 return adjustedPtr;
anatofuz
parents:
diff changeset
507 }
anatofuz
parents:
diff changeset
508
anatofuz
parents:
diff changeset
509 namespace
anatofuz
parents:
diff changeset
510 {
anatofuz
parents:
diff changeset
511
anatofuz
parents:
diff changeset
512 struct scan_results
anatofuz
parents:
diff changeset
513 {
anatofuz
parents:
diff changeset
514 int64_t ttypeIndex; // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup
anatofuz
parents:
diff changeset
515 const uint8_t* actionRecord; // Currently unused. Retained to ease future maintenance.
anatofuz
parents:
diff changeset
516 const uint8_t* languageSpecificData; // Needed only for __cxa_call_unexpected
anatofuz
parents:
diff changeset
517 uintptr_t landingPad; // null -> nothing found, else something found
anatofuz
parents:
diff changeset
518 void* adjustedPtr; // Used in cxa_exception.cpp
anatofuz
parents:
diff changeset
519 _Unwind_Reason_Code reason; // One of _URC_FATAL_PHASE1_ERROR,
anatofuz
parents:
diff changeset
520 // _URC_FATAL_PHASE2_ERROR,
anatofuz
parents:
diff changeset
521 // _URC_CONTINUE_UNWIND,
anatofuz
parents:
diff changeset
522 // _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
523 };
anatofuz
parents:
diff changeset
524
anatofuz
parents:
diff changeset
525 } // unnamed namespace
anatofuz
parents:
diff changeset
526
anatofuz
parents:
diff changeset
527 static
anatofuz
parents:
diff changeset
528 void
anatofuz
parents:
diff changeset
529 set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
anatofuz
parents:
diff changeset
530 const scan_results& results)
anatofuz
parents:
diff changeset
531 {
anatofuz
parents:
diff changeset
532 #if defined(__USING_SJLJ_EXCEPTIONS__)
anatofuz
parents:
diff changeset
533 #define __builtin_eh_return_data_regno(regno) regno
anatofuz
parents:
diff changeset
534 #endif
anatofuz
parents:
diff changeset
535 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
anatofuz
parents:
diff changeset
536 reinterpret_cast<uintptr_t>(unwind_exception));
anatofuz
parents:
diff changeset
537 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1),
anatofuz
parents:
diff changeset
538 static_cast<uintptr_t>(results.ttypeIndex));
anatofuz
parents:
diff changeset
539 _Unwind_SetIP(context, results.landingPad);
anatofuz
parents:
diff changeset
540 }
anatofuz
parents:
diff changeset
541
anatofuz
parents:
diff changeset
542 /*
anatofuz
parents:
diff changeset
543 There are 3 types of scans needed:
anatofuz
parents:
diff changeset
544
anatofuz
parents:
diff changeset
545 1. Scan for handler with native or foreign exception. If handler found,
anatofuz
parents:
diff changeset
546 save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
anatofuz
parents:
diff changeset
547 May also report an error on invalid input.
anatofuz
parents:
diff changeset
548 May terminate for invalid exception table.
anatofuz
parents:
diff changeset
549 _UA_SEARCH_PHASE
anatofuz
parents:
diff changeset
550
anatofuz
parents:
diff changeset
551 2. Scan for handler with foreign exception. Must return _URC_HANDLER_FOUND,
anatofuz
parents:
diff changeset
552 or call terminate.
anatofuz
parents:
diff changeset
553 _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception
anatofuz
parents:
diff changeset
554
anatofuz
parents:
diff changeset
555 3. Scan for cleanups. If a handler is found and this isn't forced unwind,
anatofuz
parents:
diff changeset
556 then terminate, otherwise ignore the handler and keep looking for cleanup.
anatofuz
parents:
diff changeset
557 If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
anatofuz
parents:
diff changeset
558 May also report an error on invalid input.
anatofuz
parents:
diff changeset
559 May terminate for invalid exception table.
anatofuz
parents:
diff changeset
560 _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME
anatofuz
parents:
diff changeset
561 */
anatofuz
parents:
diff changeset
562
anatofuz
parents:
diff changeset
563 static void scan_eh_tab(scan_results &results, _Unwind_Action actions,
anatofuz
parents:
diff changeset
564 bool native_exception,
anatofuz
parents:
diff changeset
565 _Unwind_Exception *unwind_exception,
anatofuz
parents:
diff changeset
566 _Unwind_Context *context) {
anatofuz
parents:
diff changeset
567 // Initialize results to found nothing but an error
anatofuz
parents:
diff changeset
568 results.ttypeIndex = 0;
anatofuz
parents:
diff changeset
569 results.actionRecord = 0;
anatofuz
parents:
diff changeset
570 results.languageSpecificData = 0;
anatofuz
parents:
diff changeset
571 results.landingPad = 0;
anatofuz
parents:
diff changeset
572 results.adjustedPtr = 0;
anatofuz
parents:
diff changeset
573 results.reason = _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
574 // Check for consistent actions
anatofuz
parents:
diff changeset
575 if (actions & _UA_SEARCH_PHASE)
anatofuz
parents:
diff changeset
576 {
anatofuz
parents:
diff changeset
577 // Do Phase 1
anatofuz
parents:
diff changeset
578 if (actions & (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME | _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
579 {
anatofuz
parents:
diff changeset
580 // None of these flags should be set during Phase 1
anatofuz
parents:
diff changeset
581 // Client error
anatofuz
parents:
diff changeset
582 results.reason = _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
583 return;
anatofuz
parents:
diff changeset
584 }
anatofuz
parents:
diff changeset
585 }
anatofuz
parents:
diff changeset
586 else if (actions & _UA_CLEANUP_PHASE)
anatofuz
parents:
diff changeset
587 {
anatofuz
parents:
diff changeset
588 if ((actions & _UA_HANDLER_FRAME) && (actions & _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
589 {
anatofuz
parents:
diff changeset
590 // _UA_HANDLER_FRAME should only be set if phase 1 found a handler.
anatofuz
parents:
diff changeset
591 // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened.
anatofuz
parents:
diff changeset
592 // Client error
anatofuz
parents:
diff changeset
593 results.reason = _URC_FATAL_PHASE2_ERROR;
anatofuz
parents:
diff changeset
594 return;
anatofuz
parents:
diff changeset
595 }
anatofuz
parents:
diff changeset
596 }
anatofuz
parents:
diff changeset
597 else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set
anatofuz
parents:
diff changeset
598 {
anatofuz
parents:
diff changeset
599 // One of these should be set.
anatofuz
parents:
diff changeset
600 // Client error
anatofuz
parents:
diff changeset
601 results.reason = _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
602 return;
anatofuz
parents:
diff changeset
603 }
anatofuz
parents:
diff changeset
604 // Start scan by getting exception table address
anatofuz
parents:
diff changeset
605 const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context);
anatofuz
parents:
diff changeset
606 if (lsda == 0)
anatofuz
parents:
diff changeset
607 {
anatofuz
parents:
diff changeset
608 // There is no exception table
anatofuz
parents:
diff changeset
609 results.reason = _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
610 return;
anatofuz
parents:
diff changeset
611 }
anatofuz
parents:
diff changeset
612 results.languageSpecificData = lsda;
anatofuz
parents:
diff changeset
613 // Get the current instruction pointer and offset it before next
anatofuz
parents:
diff changeset
614 // instruction in the current frame which threw the exception.
anatofuz
parents:
diff changeset
615 uintptr_t ip = _Unwind_GetIP(context) - 1;
anatofuz
parents:
diff changeset
616 // Get beginning current frame's code (as defined by the
anatofuz
parents:
diff changeset
617 // emitted dwarf code)
anatofuz
parents:
diff changeset
618 uintptr_t funcStart = _Unwind_GetRegionStart(context);
anatofuz
parents:
diff changeset
619 #ifdef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
620 if (ip == uintptr_t(-1))
anatofuz
parents:
diff changeset
621 {
anatofuz
parents:
diff changeset
622 // no action
anatofuz
parents:
diff changeset
623 results.reason = _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
624 return;
anatofuz
parents:
diff changeset
625 }
anatofuz
parents:
diff changeset
626 else if (ip == 0)
anatofuz
parents:
diff changeset
627 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
628 // ip is 1-based index into call site table
anatofuz
parents:
diff changeset
629 #else // !__USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
630 uintptr_t ipOffset = ip - funcStart;
anatofuz
parents:
diff changeset
631 #endif // !defined(_USING_SLJL_EXCEPTIONS__)
anatofuz
parents:
diff changeset
632 const uint8_t* classInfo = NULL;
anatofuz
parents:
diff changeset
633 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
anatofuz
parents:
diff changeset
634 // dwarf emission
anatofuz
parents:
diff changeset
635 // Parse LSDA header.
anatofuz
parents:
diff changeset
636 uint8_t lpStartEncoding = *lsda++;
anatofuz
parents:
diff changeset
637 const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding);
anatofuz
parents:
diff changeset
638 if (lpStart == 0)
anatofuz
parents:
diff changeset
639 lpStart = (const uint8_t*)funcStart;
anatofuz
parents:
diff changeset
640 uint8_t ttypeEncoding = *lsda++;
anatofuz
parents:
diff changeset
641 if (ttypeEncoding != DW_EH_PE_omit)
anatofuz
parents:
diff changeset
642 {
anatofuz
parents:
diff changeset
643 // Calculate type info locations in emitted dwarf code which
anatofuz
parents:
diff changeset
644 // were flagged by type info arguments to llvm.eh.selector
anatofuz
parents:
diff changeset
645 // intrinsic
anatofuz
parents:
diff changeset
646 uintptr_t classInfoOffset = readULEB128(&lsda);
anatofuz
parents:
diff changeset
647 classInfo = lsda + classInfoOffset;
anatofuz
parents:
diff changeset
648 }
anatofuz
parents:
diff changeset
649 // Walk call-site table looking for range that
anatofuz
parents:
diff changeset
650 // includes current PC.
anatofuz
parents:
diff changeset
651 uint8_t callSiteEncoding = *lsda++;
anatofuz
parents:
diff changeset
652 #ifdef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
653 (void)callSiteEncoding; // When using SjLj exceptions, callSiteEncoding is never used
anatofuz
parents:
diff changeset
654 #endif
anatofuz
parents:
diff changeset
655 uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda));
anatofuz
parents:
diff changeset
656 const uint8_t* callSiteTableStart = lsda;
anatofuz
parents:
diff changeset
657 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
anatofuz
parents:
diff changeset
658 const uint8_t* actionTableStart = callSiteTableEnd;
anatofuz
parents:
diff changeset
659 const uint8_t* callSitePtr = callSiteTableStart;
anatofuz
parents:
diff changeset
660 while (callSitePtr < callSiteTableEnd)
anatofuz
parents:
diff changeset
661 {
anatofuz
parents:
diff changeset
662 // There is one entry per call site.
anatofuz
parents:
diff changeset
663 #ifndef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
664 // The call sites are non-overlapping in [start, start+length)
anatofuz
parents:
diff changeset
665 // The call sites are ordered in increasing value of start
anatofuz
parents:
diff changeset
666 uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding);
anatofuz
parents:
diff changeset
667 uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding);
anatofuz
parents:
diff changeset
668 uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding);
anatofuz
parents:
diff changeset
669 uintptr_t actionEntry = readULEB128(&callSitePtr);
anatofuz
parents:
diff changeset
670 if ((start <= ipOffset) && (ipOffset < (start + length)))
anatofuz
parents:
diff changeset
671 #else // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
672 // ip is 1-based index into this table
anatofuz
parents:
diff changeset
673 uintptr_t landingPad = readULEB128(&callSitePtr);
anatofuz
parents:
diff changeset
674 uintptr_t actionEntry = readULEB128(&callSitePtr);
anatofuz
parents:
diff changeset
675 if (--ip == 0)
anatofuz
parents:
diff changeset
676 #endif // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
677 {
anatofuz
parents:
diff changeset
678 // Found the call site containing ip.
anatofuz
parents:
diff changeset
679 #ifndef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
680 if (landingPad == 0)
anatofuz
parents:
diff changeset
681 {
anatofuz
parents:
diff changeset
682 // No handler here
anatofuz
parents:
diff changeset
683 results.reason = _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
684 return;
anatofuz
parents:
diff changeset
685 }
anatofuz
parents:
diff changeset
686 landingPad = (uintptr_t)lpStart + landingPad;
anatofuz
parents:
diff changeset
687 #else // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
688 ++landingPad;
anatofuz
parents:
diff changeset
689 #endif // __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
690 if (actionEntry == 0)
anatofuz
parents:
diff changeset
691 {
anatofuz
parents:
diff changeset
692 // Found a cleanup
anatofuz
parents:
diff changeset
693 // If this is a type 1 or type 2 search, there are no handlers
anatofuz
parents:
diff changeset
694 // If this is a type 3 search, you want to install the cleanup.
anatofuz
parents:
diff changeset
695 if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME))
anatofuz
parents:
diff changeset
696 {
anatofuz
parents:
diff changeset
697 results.ttypeIndex = 0; // Redundant but clarifying
anatofuz
parents:
diff changeset
698 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
699 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
700 return;
anatofuz
parents:
diff changeset
701 }
anatofuz
parents:
diff changeset
702 // No handler here
anatofuz
parents:
diff changeset
703 results.reason = _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
704 return;
anatofuz
parents:
diff changeset
705 }
anatofuz
parents:
diff changeset
706 // Convert 1-based byte offset into
anatofuz
parents:
diff changeset
707 const uint8_t* action = actionTableStart + (actionEntry - 1);
anatofuz
parents:
diff changeset
708 // Scan action entries until you find a matching handler, cleanup, or the end of action list
anatofuz
parents:
diff changeset
709 while (true)
anatofuz
parents:
diff changeset
710 {
anatofuz
parents:
diff changeset
711 const uint8_t* actionRecord = action;
anatofuz
parents:
diff changeset
712 int64_t ttypeIndex = readSLEB128(&action);
anatofuz
parents:
diff changeset
713 if (ttypeIndex > 0)
anatofuz
parents:
diff changeset
714 {
anatofuz
parents:
diff changeset
715 // Found a catch, does it actually catch?
anatofuz
parents:
diff changeset
716 // First check for catch (...)
anatofuz
parents:
diff changeset
717 const __shim_type_info* catchType =
anatofuz
parents:
diff changeset
718 get_shim_type_info(static_cast<uint64_t>(ttypeIndex),
anatofuz
parents:
diff changeset
719 classInfo, ttypeEncoding,
anatofuz
parents:
diff changeset
720 native_exception, unwind_exception);
anatofuz
parents:
diff changeset
721 if (catchType == 0)
anatofuz
parents:
diff changeset
722 {
anatofuz
parents:
diff changeset
723 // Found catch (...) catches everything, including foreign exceptions
anatofuz
parents:
diff changeset
724 // If this is a type 1 search save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
725 // If this is a type 2 search save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
726 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
anatofuz
parents:
diff changeset
727 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
anatofuz
parents:
diff changeset
728 if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME))
anatofuz
parents:
diff changeset
729 {
anatofuz
parents:
diff changeset
730 // Save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
731 results.ttypeIndex = ttypeIndex;
anatofuz
parents:
diff changeset
732 results.actionRecord = actionRecord;
anatofuz
parents:
diff changeset
733 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
734 results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
anatofuz
parents:
diff changeset
735 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
736 return;
anatofuz
parents:
diff changeset
737 }
anatofuz
parents:
diff changeset
738 else if (!(actions & _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
739 {
anatofuz
parents:
diff changeset
740 // It looks like the exception table has changed
anatofuz
parents:
diff changeset
741 // on us. Likely stack corruption!
anatofuz
parents:
diff changeset
742 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
743 }
anatofuz
parents:
diff changeset
744 }
anatofuz
parents:
diff changeset
745 // Else this is a catch (T) clause and will never
anatofuz
parents:
diff changeset
746 // catch a foreign exception
anatofuz
parents:
diff changeset
747 else if (native_exception)
anatofuz
parents:
diff changeset
748 {
anatofuz
parents:
diff changeset
749 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
750 void* adjustedPtr = get_thrown_object_ptr(unwind_exception);
anatofuz
parents:
diff changeset
751 const __shim_type_info* excpType =
anatofuz
parents:
diff changeset
752 static_cast<const __shim_type_info*>(exception_header->exceptionType);
anatofuz
parents:
diff changeset
753 if (adjustedPtr == 0 || excpType == 0)
anatofuz
parents:
diff changeset
754 {
anatofuz
parents:
diff changeset
755 // Something very bad happened
anatofuz
parents:
diff changeset
756 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
757 }
anatofuz
parents:
diff changeset
758 if (catchType->can_catch(excpType, adjustedPtr))
anatofuz
parents:
diff changeset
759 {
anatofuz
parents:
diff changeset
760 // Found a matching handler
anatofuz
parents:
diff changeset
761 // If this is a type 1 search save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
762 // If this is a type 3 search and !_UA_FORCE_UNWIND, we should have found this in phase 1!
anatofuz
parents:
diff changeset
763 // If this is a type 3 search and _UA_FORCE_UNWIND, ignore handler and continue scan
anatofuz
parents:
diff changeset
764 if (actions & _UA_SEARCH_PHASE)
anatofuz
parents:
diff changeset
765 {
anatofuz
parents:
diff changeset
766 // Save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
767 results.ttypeIndex = ttypeIndex;
anatofuz
parents:
diff changeset
768 results.actionRecord = actionRecord;
anatofuz
parents:
diff changeset
769 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
770 results.adjustedPtr = adjustedPtr;
anatofuz
parents:
diff changeset
771 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
772 return;
anatofuz
parents:
diff changeset
773 }
anatofuz
parents:
diff changeset
774 else if (!(actions & _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
775 {
anatofuz
parents:
diff changeset
776 // It looks like the exception table has changed
anatofuz
parents:
diff changeset
777 // on us. Likely stack corruption!
anatofuz
parents:
diff changeset
778 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
779 }
anatofuz
parents:
diff changeset
780 }
anatofuz
parents:
diff changeset
781 }
anatofuz
parents:
diff changeset
782 // Scan next action ...
anatofuz
parents:
diff changeset
783 }
anatofuz
parents:
diff changeset
784 else if (ttypeIndex < 0)
anatofuz
parents:
diff changeset
785 {
anatofuz
parents:
diff changeset
786 // Found an exception spec. If this is a foreign exception,
anatofuz
parents:
diff changeset
787 // it is always caught.
anatofuz
parents:
diff changeset
788 if (native_exception)
anatofuz
parents:
diff changeset
789 {
anatofuz
parents:
diff changeset
790 // Does the exception spec catch this native exception?
anatofuz
parents:
diff changeset
791 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
792 void* adjustedPtr = get_thrown_object_ptr(unwind_exception);
anatofuz
parents:
diff changeset
793 const __shim_type_info* excpType =
anatofuz
parents:
diff changeset
794 static_cast<const __shim_type_info*>(exception_header->exceptionType);
anatofuz
parents:
diff changeset
795 if (adjustedPtr == 0 || excpType == 0)
anatofuz
parents:
diff changeset
796 {
anatofuz
parents:
diff changeset
797 // Something very bad happened
anatofuz
parents:
diff changeset
798 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
799 }
anatofuz
parents:
diff changeset
800 if (exception_spec_can_catch(ttypeIndex, classInfo,
anatofuz
parents:
diff changeset
801 ttypeEncoding, excpType,
anatofuz
parents:
diff changeset
802 adjustedPtr, unwind_exception))
anatofuz
parents:
diff changeset
803 {
anatofuz
parents:
diff changeset
804 // native exception caught by exception spec
anatofuz
parents:
diff changeset
805 // If this is a type 1 search, save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
806 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
anatofuz
parents:
diff changeset
807 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
anatofuz
parents:
diff changeset
808 if (actions & _UA_SEARCH_PHASE)
anatofuz
parents:
diff changeset
809 {
anatofuz
parents:
diff changeset
810 // Save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
811 results.ttypeIndex = ttypeIndex;
anatofuz
parents:
diff changeset
812 results.actionRecord = actionRecord;
anatofuz
parents:
diff changeset
813 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
814 results.adjustedPtr = adjustedPtr;
anatofuz
parents:
diff changeset
815 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
816 return;
anatofuz
parents:
diff changeset
817 }
anatofuz
parents:
diff changeset
818 else if (!(actions & _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
819 {
anatofuz
parents:
diff changeset
820 // It looks like the exception table has changed
anatofuz
parents:
diff changeset
821 // on us. Likely stack corruption!
anatofuz
parents:
diff changeset
822 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
823 }
anatofuz
parents:
diff changeset
824 }
anatofuz
parents:
diff changeset
825 }
anatofuz
parents:
diff changeset
826 else
anatofuz
parents:
diff changeset
827 {
anatofuz
parents:
diff changeset
828 // foreign exception caught by exception spec
anatofuz
parents:
diff changeset
829 // If this is a type 1 search, save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
830 // If this is a type 2 search, save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
831 // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
anatofuz
parents:
diff changeset
832 // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
anatofuz
parents:
diff changeset
833 if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME))
anatofuz
parents:
diff changeset
834 {
anatofuz
parents:
diff changeset
835 // Save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
836 results.ttypeIndex = ttypeIndex;
anatofuz
parents:
diff changeset
837 results.actionRecord = actionRecord;
anatofuz
parents:
diff changeset
838 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
839 results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
anatofuz
parents:
diff changeset
840 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
841 return;
anatofuz
parents:
diff changeset
842 }
anatofuz
parents:
diff changeset
843 else if (!(actions & _UA_FORCE_UNWIND))
anatofuz
parents:
diff changeset
844 {
anatofuz
parents:
diff changeset
845 // It looks like the exception table has changed
anatofuz
parents:
diff changeset
846 // on us. Likely stack corruption!
anatofuz
parents:
diff changeset
847 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
848 }
anatofuz
parents:
diff changeset
849 }
anatofuz
parents:
diff changeset
850 // Scan next action ...
anatofuz
parents:
diff changeset
851 }
anatofuz
parents:
diff changeset
852 else // ttypeIndex == 0
anatofuz
parents:
diff changeset
853 {
anatofuz
parents:
diff changeset
854 // Found a cleanup
anatofuz
parents:
diff changeset
855 // If this is a type 1 search, ignore it and continue scan
anatofuz
parents:
diff changeset
856 // If this is a type 2 search, ignore it and continue scan
anatofuz
parents:
diff changeset
857 // If this is a type 3 search, save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
858 if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME))
anatofuz
parents:
diff changeset
859 {
anatofuz
parents:
diff changeset
860 // Save state and return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
861 results.ttypeIndex = ttypeIndex;
anatofuz
parents:
diff changeset
862 results.actionRecord = actionRecord;
anatofuz
parents:
diff changeset
863 results.landingPad = landingPad;
anatofuz
parents:
diff changeset
864 results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
anatofuz
parents:
diff changeset
865 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
866 return;
anatofuz
parents:
diff changeset
867 }
anatofuz
parents:
diff changeset
868 }
anatofuz
parents:
diff changeset
869 const uint8_t* temp = action;
anatofuz
parents:
diff changeset
870 int64_t actionOffset = readSLEB128(&temp);
anatofuz
parents:
diff changeset
871 if (actionOffset == 0)
anatofuz
parents:
diff changeset
872 {
anatofuz
parents:
diff changeset
873 // End of action list, no matching handler or cleanup found
anatofuz
parents:
diff changeset
874 results.reason = _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
875 return;
anatofuz
parents:
diff changeset
876 }
anatofuz
parents:
diff changeset
877 // Go to next action
anatofuz
parents:
diff changeset
878 action += actionOffset;
anatofuz
parents:
diff changeset
879 } // there is no break out of this loop, only return
anatofuz
parents:
diff changeset
880 }
anatofuz
parents:
diff changeset
881 #ifndef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
882 else if (ipOffset < start)
anatofuz
parents:
diff changeset
883 {
anatofuz
parents:
diff changeset
884 // There is no call site for this ip
anatofuz
parents:
diff changeset
885 // Something bad has happened. We should never get here.
anatofuz
parents:
diff changeset
886 // Possible stack corruption.
anatofuz
parents:
diff changeset
887 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
888 }
anatofuz
parents:
diff changeset
889 #endif // !__USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
890 } // there might be some tricky cases which break out of this loop
anatofuz
parents:
diff changeset
891
anatofuz
parents:
diff changeset
892 // It is possible that no eh table entry specify how to handle
anatofuz
parents:
diff changeset
893 // this exception. By spec, terminate it immediately.
anatofuz
parents:
diff changeset
894 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
895 }
anatofuz
parents:
diff changeset
896
anatofuz
parents:
diff changeset
897 // public API
anatofuz
parents:
diff changeset
898
anatofuz
parents:
diff changeset
899 /*
anatofuz
parents:
diff changeset
900 The personality function branches on actions like so:
anatofuz
parents:
diff changeset
901
anatofuz
parents:
diff changeset
902 _UA_SEARCH_PHASE
anatofuz
parents:
diff changeset
903
anatofuz
parents:
diff changeset
904 If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's
anatofuz
parents:
diff changeset
905 an error from above, return _URC_FATAL_PHASE1_ERROR.
anatofuz
parents:
diff changeset
906
anatofuz
parents:
diff changeset
907 Scan for anything that could stop unwinding:
anatofuz
parents:
diff changeset
908
anatofuz
parents:
diff changeset
909 1. A catch clause that will catch this exception
anatofuz
parents:
diff changeset
910 (will never catch foreign).
anatofuz
parents:
diff changeset
911 2. A catch (...) (will always catch foreign).
anatofuz
parents:
diff changeset
912 3. An exception spec that will catch this exception
anatofuz
parents:
diff changeset
913 (will always catch foreign).
anatofuz
parents:
diff changeset
914 If a handler is found
anatofuz
parents:
diff changeset
915 If not foreign
anatofuz
parents:
diff changeset
916 Save state in header
anatofuz
parents:
diff changeset
917 return _URC_HANDLER_FOUND
anatofuz
parents:
diff changeset
918 Else a handler not found
anatofuz
parents:
diff changeset
919 return _URC_CONTINUE_UNWIND
anatofuz
parents:
diff changeset
920
anatofuz
parents:
diff changeset
921 _UA_CLEANUP_PHASE
anatofuz
parents:
diff changeset
922
anatofuz
parents:
diff changeset
923 If _UA_HANDLER_FRAME
anatofuz
parents:
diff changeset
924 If _UA_FORCE_UNWIND
anatofuz
parents:
diff changeset
925 How did this happen? return _URC_FATAL_PHASE2_ERROR
anatofuz
parents:
diff changeset
926 If foreign
anatofuz
parents:
diff changeset
927 Do _UA_SEARCH_PHASE to recover state
anatofuz
parents:
diff changeset
928 else
anatofuz
parents:
diff changeset
929 Recover state from header
anatofuz
parents:
diff changeset
930 Transfer control to landing pad. return _URC_INSTALL_CONTEXT
anatofuz
parents:
diff changeset
931
anatofuz
parents:
diff changeset
932 Else
anatofuz
parents:
diff changeset
933
anatofuz
parents:
diff changeset
934 This branch handles both normal C++ non-catching handlers (cleanups)
anatofuz
parents:
diff changeset
935 and forced unwinding.
anatofuz
parents:
diff changeset
936 Scan for anything that can not stop unwinding:
anatofuz
parents:
diff changeset
937
anatofuz
parents:
diff changeset
938 1. A cleanup.
anatofuz
parents:
diff changeset
939
anatofuz
parents:
diff changeset
940 If a cleanup is found
anatofuz
parents:
diff changeset
941 transfer control to it. return _URC_INSTALL_CONTEXT
anatofuz
parents:
diff changeset
942 Else a cleanup is not found: return _URC_CONTINUE_UNWIND
anatofuz
parents:
diff changeset
943 */
anatofuz
parents:
diff changeset
944
anatofuz
parents:
diff changeset
945 #if !defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
946 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
anatofuz
parents:
diff changeset
947 static _Unwind_Reason_Code __gxx_personality_imp
anatofuz
parents:
diff changeset
948 #else
anatofuz
parents:
diff changeset
949 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
anatofuz
parents:
diff changeset
950 #ifdef __USING_SJLJ_EXCEPTIONS__
anatofuz
parents:
diff changeset
951 __gxx_personality_sj0
anatofuz
parents:
diff changeset
952 #else
anatofuz
parents:
diff changeset
953 __gxx_personality_v0
anatofuz
parents:
diff changeset
954 #endif
anatofuz
parents:
diff changeset
955 #endif
anatofuz
parents:
diff changeset
956 (int version, _Unwind_Action actions, uint64_t exceptionClass,
anatofuz
parents:
diff changeset
957 _Unwind_Exception* unwind_exception, _Unwind_Context* context)
anatofuz
parents:
diff changeset
958 {
anatofuz
parents:
diff changeset
959 if (version != 1 || unwind_exception == 0 || context == 0)
anatofuz
parents:
diff changeset
960 return _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
961
anatofuz
parents:
diff changeset
962 bool native_exception = (exceptionClass & get_vendor_and_language) ==
anatofuz
parents:
diff changeset
963 (kOurExceptionClass & get_vendor_and_language);
anatofuz
parents:
diff changeset
964 scan_results results;
anatofuz
parents:
diff changeset
965 if (actions & _UA_SEARCH_PHASE)
anatofuz
parents:
diff changeset
966 {
anatofuz
parents:
diff changeset
967 // Phase 1 search: All we're looking for in phase 1 is a handler that
anatofuz
parents:
diff changeset
968 // halts unwinding
anatofuz
parents:
diff changeset
969 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
970 if (results.reason == _URC_HANDLER_FOUND)
anatofuz
parents:
diff changeset
971 {
anatofuz
parents:
diff changeset
972 // Found one. Can we cache the results somewhere to optimize phase 2?
anatofuz
parents:
diff changeset
973 if (native_exception)
anatofuz
parents:
diff changeset
974 {
anatofuz
parents:
diff changeset
975 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
976 exception_header->handlerSwitchValue = static_cast<int>(results.ttypeIndex);
anatofuz
parents:
diff changeset
977 exception_header->actionRecord = results.actionRecord;
anatofuz
parents:
diff changeset
978 exception_header->languageSpecificData = results.languageSpecificData;
anatofuz
parents:
diff changeset
979 exception_header->catchTemp = reinterpret_cast<void*>(results.landingPad);
anatofuz
parents:
diff changeset
980 exception_header->adjustedPtr = results.adjustedPtr;
anatofuz
parents:
diff changeset
981 }
anatofuz
parents:
diff changeset
982 return _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
983 }
anatofuz
parents:
diff changeset
984 // Did not find a catching-handler. Return the results of the scan
anatofuz
parents:
diff changeset
985 // (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE1_ERROR
anatofuz
parents:
diff changeset
986 // if we were called improperly).
anatofuz
parents:
diff changeset
987 return results.reason;
anatofuz
parents:
diff changeset
988 }
anatofuz
parents:
diff changeset
989 if (actions & _UA_CLEANUP_PHASE)
anatofuz
parents:
diff changeset
990 {
anatofuz
parents:
diff changeset
991 // Phase 2 search:
anatofuz
parents:
diff changeset
992 // Did we find a catching handler in phase 1?
anatofuz
parents:
diff changeset
993 if (actions & _UA_HANDLER_FRAME)
anatofuz
parents:
diff changeset
994 {
anatofuz
parents:
diff changeset
995 // Yes, phase 1 said we have a catching handler here.
anatofuz
parents:
diff changeset
996 // Did we cache the results of the scan?
anatofuz
parents:
diff changeset
997 if (native_exception)
anatofuz
parents:
diff changeset
998 {
anatofuz
parents:
diff changeset
999 // Yes, reload the results from the cache.
anatofuz
parents:
diff changeset
1000 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
1001 results.ttypeIndex = exception_header->handlerSwitchValue;
anatofuz
parents:
diff changeset
1002 results.actionRecord = exception_header->actionRecord;
anatofuz
parents:
diff changeset
1003 results.languageSpecificData = exception_header->languageSpecificData;
anatofuz
parents:
diff changeset
1004 results.landingPad = reinterpret_cast<uintptr_t>(exception_header->catchTemp);
anatofuz
parents:
diff changeset
1005 results.adjustedPtr = exception_header->adjustedPtr;
anatofuz
parents:
diff changeset
1006 }
anatofuz
parents:
diff changeset
1007 else
anatofuz
parents:
diff changeset
1008 {
anatofuz
parents:
diff changeset
1009 // No, do the scan again to reload the results.
anatofuz
parents:
diff changeset
1010 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
1011 // Phase 1 told us we would find a handler. Now in Phase 2 we
anatofuz
parents:
diff changeset
1012 // didn't find a handler. The eh table should not be changing!
anatofuz
parents:
diff changeset
1013 if (results.reason != _URC_HANDLER_FOUND)
anatofuz
parents:
diff changeset
1014 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
1015 }
anatofuz
parents:
diff changeset
1016 // Jump to the handler
anatofuz
parents:
diff changeset
1017 set_registers(unwind_exception, context, results);
anatofuz
parents:
diff changeset
1018 return _URC_INSTALL_CONTEXT;
anatofuz
parents:
diff changeset
1019 }
anatofuz
parents:
diff changeset
1020 // Either we didn't do a phase 1 search (due to forced unwinding), or
anatofuz
parents:
diff changeset
1021 // phase 1 reported no catching-handlers.
anatofuz
parents:
diff changeset
1022 // Search for a (non-catching) cleanup
anatofuz
parents:
diff changeset
1023 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
1024 if (results.reason == _URC_HANDLER_FOUND)
anatofuz
parents:
diff changeset
1025 {
anatofuz
parents:
diff changeset
1026 // Found a non-catching handler. Jump to it:
anatofuz
parents:
diff changeset
1027 set_registers(unwind_exception, context, results);
anatofuz
parents:
diff changeset
1028 return _URC_INSTALL_CONTEXT;
anatofuz
parents:
diff changeset
1029 }
anatofuz
parents:
diff changeset
1030 // Did not find a cleanup. Return the results of the scan
anatofuz
parents:
diff changeset
1031 // (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE2_ERROR
anatofuz
parents:
diff changeset
1032 // if we were called improperly).
anatofuz
parents:
diff changeset
1033 return results.reason;
anatofuz
parents:
diff changeset
1034 }
anatofuz
parents:
diff changeset
1035 // We were called improperly: neither a phase 1 or phase 2 search
anatofuz
parents:
diff changeset
1036 return _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
1037 }
anatofuz
parents:
diff changeset
1038
anatofuz
parents:
diff changeset
1039 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
anatofuz
parents:
diff changeset
1040 extern "C" _LIBCXXABI_FUNC_VIS EXCEPTION_DISPOSITION
anatofuz
parents:
diff changeset
1041 __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,
anatofuz
parents:
diff changeset
1042 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
anatofuz
parents:
diff changeset
1043 {
anatofuz
parents:
diff changeset
1044 return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp,
anatofuz
parents:
diff changeset
1045 __gxx_personality_imp);
anatofuz
parents:
diff changeset
1046 }
anatofuz
parents:
diff changeset
1047 #endif
anatofuz
parents:
diff changeset
1048
anatofuz
parents:
diff changeset
1049 #else
anatofuz
parents:
diff changeset
1050
anatofuz
parents:
diff changeset
1051 extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*,
anatofuz
parents:
diff changeset
1052 _Unwind_Context*);
anatofuz
parents:
diff changeset
1053
anatofuz
parents:
diff changeset
1054 // Helper function to unwind one frame.
anatofuz
parents:
diff changeset
1055 // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the
anatofuz
parents:
diff changeset
1056 // personality routine should update the virtual register set (VRS) according to the
anatofuz
parents:
diff changeset
1057 // corresponding frame unwinding instructions (ARM EHABI 9.3.)
anatofuz
parents:
diff changeset
1058 static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception,
anatofuz
parents:
diff changeset
1059 _Unwind_Context* context)
anatofuz
parents:
diff changeset
1060 {
anatofuz
parents:
diff changeset
1061 if (__gnu_unwind_frame(unwind_exception, context) != _URC_OK)
anatofuz
parents:
diff changeset
1062 return _URC_FAILURE;
anatofuz
parents:
diff changeset
1063 return _URC_CONTINUE_UNWIND;
anatofuz
parents:
diff changeset
1064 }
anatofuz
parents:
diff changeset
1065
anatofuz
parents:
diff changeset
1066 // ARM register names
anatofuz
parents:
diff changeset
1067 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
anatofuz
parents:
diff changeset
1068 static const uint32_t REG_UCB = 12; // Register to save _Unwind_Control_Block
anatofuz
parents:
diff changeset
1069 #endif
anatofuz
parents:
diff changeset
1070 static const uint32_t REG_SP = 13;
anatofuz
parents:
diff changeset
1071
anatofuz
parents:
diff changeset
1072 static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception,
anatofuz
parents:
diff changeset
1073 const scan_results& results)
anatofuz
parents:
diff changeset
1074 {
anatofuz
parents:
diff changeset
1075 unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr;
anatofuz
parents:
diff changeset
1076 unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord;
anatofuz
parents:
diff changeset
1077 unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData;
anatofuz
parents:
diff changeset
1078 unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad;
anatofuz
parents:
diff changeset
1079 unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex;
anatofuz
parents:
diff changeset
1080 }
anatofuz
parents:
diff changeset
1081
anatofuz
parents:
diff changeset
1082 static void load_results_from_barrier_cache(scan_results& results,
anatofuz
parents:
diff changeset
1083 const _Unwind_Exception* unwind_exception)
anatofuz
parents:
diff changeset
1084 {
anatofuz
parents:
diff changeset
1085 results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0];
anatofuz
parents:
diff changeset
1086 results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1];
anatofuz
parents:
diff changeset
1087 results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];
anatofuz
parents:
diff changeset
1088 results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3];
anatofuz
parents:
diff changeset
1089 results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];
anatofuz
parents:
diff changeset
1090 }
anatofuz
parents:
diff changeset
1091
anatofuz
parents:
diff changeset
1092 extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
anatofuz
parents:
diff changeset
1093 __gxx_personality_v0(_Unwind_State state,
anatofuz
parents:
diff changeset
1094 _Unwind_Exception* unwind_exception,
anatofuz
parents:
diff changeset
1095 _Unwind_Context* context)
anatofuz
parents:
diff changeset
1096 {
anatofuz
parents:
diff changeset
1097 if (unwind_exception == 0 || context == 0)
anatofuz
parents:
diff changeset
1098 return _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
1099
anatofuz
parents:
diff changeset
1100 bool native_exception = __isOurExceptionClass(unwind_exception);
anatofuz
parents:
diff changeset
1101
anatofuz
parents:
diff changeset
1102 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
anatofuz
parents:
diff changeset
1103 // Copy the address of _Unwind_Control_Block to r12 so that
anatofuz
parents:
diff changeset
1104 // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can
anatofuz
parents:
diff changeset
1105 // return correct address.
anatofuz
parents:
diff changeset
1106 _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception));
anatofuz
parents:
diff changeset
1107 #endif
anatofuz
parents:
diff changeset
1108
anatofuz
parents:
diff changeset
1109 // Check the undocumented force unwinding behavior
anatofuz
parents:
diff changeset
1110 bool is_force_unwinding = state & _US_FORCE_UNWIND;
anatofuz
parents:
diff changeset
1111 state &= ~_US_FORCE_UNWIND;
anatofuz
parents:
diff changeset
1112
anatofuz
parents:
diff changeset
1113 scan_results results;
anatofuz
parents:
diff changeset
1114 switch (state) {
anatofuz
parents:
diff changeset
1115 case _US_VIRTUAL_UNWIND_FRAME:
anatofuz
parents:
diff changeset
1116 if (is_force_unwinding)
anatofuz
parents:
diff changeset
1117 return continue_unwind(unwind_exception, context);
anatofuz
parents:
diff changeset
1118
anatofuz
parents:
diff changeset
1119 // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding
anatofuz
parents:
diff changeset
1120 scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
1121 if (results.reason == _URC_HANDLER_FOUND)
anatofuz
parents:
diff changeset
1122 {
anatofuz
parents:
diff changeset
1123 unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP);
anatofuz
parents:
diff changeset
1124 if (native_exception)
anatofuz
parents:
diff changeset
1125 save_results_to_barrier_cache(unwind_exception, results);
anatofuz
parents:
diff changeset
1126 return _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
1127 }
anatofuz
parents:
diff changeset
1128 // Did not find the catch handler
anatofuz
parents:
diff changeset
1129 if (results.reason == _URC_CONTINUE_UNWIND)
anatofuz
parents:
diff changeset
1130 return continue_unwind(unwind_exception, context);
anatofuz
parents:
diff changeset
1131 return results.reason;
anatofuz
parents:
diff changeset
1132
anatofuz
parents:
diff changeset
1133 case _US_UNWIND_FRAME_STARTING:
anatofuz
parents:
diff changeset
1134 // TODO: Support force unwinding in the phase 2 search.
anatofuz
parents:
diff changeset
1135 // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind()
anatofuz
parents:
diff changeset
1136 // will call this personality function with (_US_FORCE_UNWIND |
anatofuz
parents:
diff changeset
1137 // _US_UNWIND_FRAME_STARTING).
anatofuz
parents:
diff changeset
1138
anatofuz
parents:
diff changeset
1139 // Phase 2 search
anatofuz
parents:
diff changeset
1140 if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP))
anatofuz
parents:
diff changeset
1141 {
anatofuz
parents:
diff changeset
1142 // Found a catching handler in phase 1
anatofuz
parents:
diff changeset
1143 if (native_exception)
anatofuz
parents:
diff changeset
1144 {
anatofuz
parents:
diff changeset
1145 // Load the result from the native exception barrier cache.
anatofuz
parents:
diff changeset
1146 load_results_from_barrier_cache(results, unwind_exception);
anatofuz
parents:
diff changeset
1147 results.reason = _URC_HANDLER_FOUND;
anatofuz
parents:
diff changeset
1148 }
anatofuz
parents:
diff changeset
1149 else
anatofuz
parents:
diff changeset
1150 {
anatofuz
parents:
diff changeset
1151 // Search for the catching handler again for the foreign exception.
anatofuz
parents:
diff changeset
1152 scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME),
anatofuz
parents:
diff changeset
1153 native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
1154 if (results.reason != _URC_HANDLER_FOUND) // phase1 search should guarantee to find one
anatofuz
parents:
diff changeset
1155 call_terminate(native_exception, unwind_exception);
anatofuz
parents:
diff changeset
1156 }
anatofuz
parents:
diff changeset
1157
anatofuz
parents:
diff changeset
1158 // Install the context for the catching handler
anatofuz
parents:
diff changeset
1159 set_registers(unwind_exception, context, results);
anatofuz
parents:
diff changeset
1160 return _URC_INSTALL_CONTEXT;
anatofuz
parents:
diff changeset
1161 }
anatofuz
parents:
diff changeset
1162
anatofuz
parents:
diff changeset
1163 // Either we didn't do a phase 1 search (due to forced unwinding), or
anatofuz
parents:
diff changeset
1164 // phase 1 reported no catching-handlers.
anatofuz
parents:
diff changeset
1165 // Search for a (non-catching) cleanup
anatofuz
parents:
diff changeset
1166 scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, unwind_exception, context);
anatofuz
parents:
diff changeset
1167 if (results.reason == _URC_HANDLER_FOUND)
anatofuz
parents:
diff changeset
1168 {
anatofuz
parents:
diff changeset
1169 // Found a non-catching handler
anatofuz
parents:
diff changeset
1170
anatofuz
parents:
diff changeset
1171 // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some
anatofuz
parents:
diff changeset
1172 // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from
anatofuz
parents:
diff changeset
1173 // __cxa_get_globals().
anatofuz
parents:
diff changeset
1174 __cxa_begin_cleanup(unwind_exception);
anatofuz
parents:
diff changeset
1175
anatofuz
parents:
diff changeset
1176 // Install the context for the cleanup handler
anatofuz
parents:
diff changeset
1177 set_registers(unwind_exception, context, results);
anatofuz
parents:
diff changeset
1178 return _URC_INSTALL_CONTEXT;
anatofuz
parents:
diff changeset
1179 }
anatofuz
parents:
diff changeset
1180
anatofuz
parents:
diff changeset
1181 // Did not find any handler
anatofuz
parents:
diff changeset
1182 if (results.reason == _URC_CONTINUE_UNWIND)
anatofuz
parents:
diff changeset
1183 return continue_unwind(unwind_exception, context);
anatofuz
parents:
diff changeset
1184 return results.reason;
anatofuz
parents:
diff changeset
1185
anatofuz
parents:
diff changeset
1186 case _US_UNWIND_FRAME_RESUME:
anatofuz
parents:
diff changeset
1187 return continue_unwind(unwind_exception, context);
anatofuz
parents:
diff changeset
1188 }
anatofuz
parents:
diff changeset
1189
anatofuz
parents:
diff changeset
1190 // We were called improperly: neither a phase 1 or phase 2 search
anatofuz
parents:
diff changeset
1191 return _URC_FATAL_PHASE1_ERROR;
anatofuz
parents:
diff changeset
1192 }
anatofuz
parents:
diff changeset
1193 #endif
anatofuz
parents:
diff changeset
1194
anatofuz
parents:
diff changeset
1195
anatofuz
parents:
diff changeset
1196 __attribute__((noreturn))
anatofuz
parents:
diff changeset
1197 _LIBCXXABI_FUNC_VIS void
anatofuz
parents:
diff changeset
1198 __cxa_call_unexpected(void* arg)
anatofuz
parents:
diff changeset
1199 {
anatofuz
parents:
diff changeset
1200 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg);
anatofuz
parents:
diff changeset
1201 if (unwind_exception == 0)
anatofuz
parents:
diff changeset
1202 call_terminate(false, unwind_exception);
anatofuz
parents:
diff changeset
1203 __cxa_begin_catch(unwind_exception);
anatofuz
parents:
diff changeset
1204 bool native_old_exception = __isOurExceptionClass(unwind_exception);
anatofuz
parents:
diff changeset
1205 std::unexpected_handler u_handler;
anatofuz
parents:
diff changeset
1206 std::terminate_handler t_handler;
anatofuz
parents:
diff changeset
1207 __cxa_exception* old_exception_header = 0;
anatofuz
parents:
diff changeset
1208 int64_t ttypeIndex;
anatofuz
parents:
diff changeset
1209 const uint8_t* lsda;
anatofuz
parents:
diff changeset
1210 if (native_old_exception)
anatofuz
parents:
diff changeset
1211 {
anatofuz
parents:
diff changeset
1212 old_exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
anatofuz
parents:
diff changeset
1213 t_handler = old_exception_header->terminateHandler;
anatofuz
parents:
diff changeset
1214 u_handler = old_exception_header->unexpectedHandler;
anatofuz
parents:
diff changeset
1215 // If std::__unexpected(u_handler) rethrows the same exception,
anatofuz
parents:
diff changeset
1216 // these values get overwritten by the rethrow. So save them now:
anatofuz
parents:
diff changeset
1217 #if defined(_LIBCXXABI_ARM_EHABI)
anatofuz
parents:
diff changeset
1218 ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];
anatofuz
parents:
diff changeset
1219 lsda = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];
anatofuz
parents:
diff changeset
1220 #else
anatofuz
parents:
diff changeset
1221 ttypeIndex = old_exception_header->handlerSwitchValue;
anatofuz
parents:
diff changeset
1222 lsda = old_exception_header->languageSpecificData;
anatofuz
parents:
diff changeset
1223 #endif
anatofuz
parents:
diff changeset
1224 }
anatofuz
parents:
diff changeset
1225 else
anatofuz
parents:
diff changeset
1226 {
anatofuz
parents:
diff changeset
1227 t_handler = std::get_terminate();
anatofuz
parents:
diff changeset
1228 u_handler = std::get_unexpected();
anatofuz
parents:
diff changeset
1229 }
anatofuz
parents:
diff changeset
1230 try
anatofuz
parents:
diff changeset
1231 {
anatofuz
parents:
diff changeset
1232 std::__unexpected(u_handler);
anatofuz
parents:
diff changeset
1233 }
anatofuz
parents:
diff changeset
1234 catch (...)
anatofuz
parents:
diff changeset
1235 {
anatofuz
parents:
diff changeset
1236 // If the old exception is foreign, then all we can do is terminate.
anatofuz
parents:
diff changeset
1237 // We have no way to recover the needed old exception spec. There's
anatofuz
parents:
diff changeset
1238 // no way to pass that information here. And the personality routine
anatofuz
parents:
diff changeset
1239 // can't call us directly and do anything but terminate() if we throw
anatofuz
parents:
diff changeset
1240 // from here.
anatofuz
parents:
diff changeset
1241 if (native_old_exception)
anatofuz
parents:
diff changeset
1242 {
anatofuz
parents:
diff changeset
1243 // Have:
anatofuz
parents:
diff changeset
1244 // old_exception_header->languageSpecificData
anatofuz
parents:
diff changeset
1245 // old_exception_header->actionRecord
anatofuz
parents:
diff changeset
1246 // Need
anatofuz
parents:
diff changeset
1247 // const uint8_t* classInfo
anatofuz
parents:
diff changeset
1248 // uint8_t ttypeEncoding
anatofuz
parents:
diff changeset
1249 uint8_t lpStartEncoding = *lsda++;
anatofuz
parents:
diff changeset
1250 const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding);
anatofuz
parents:
diff changeset
1251 (void)lpStart; // purposefully unused. Just needed to increment lsda.
anatofuz
parents:
diff changeset
1252 uint8_t ttypeEncoding = *lsda++;
anatofuz
parents:
diff changeset
1253 if (ttypeEncoding == DW_EH_PE_omit)
anatofuz
parents:
diff changeset
1254 std::__terminate(t_handler);
anatofuz
parents:
diff changeset
1255 uintptr_t classInfoOffset = readULEB128(&lsda);
anatofuz
parents:
diff changeset
1256 const uint8_t* classInfo = lsda + classInfoOffset;
anatofuz
parents:
diff changeset
1257 // Is this new exception catchable by the exception spec at ttypeIndex?
anatofuz
parents:
diff changeset
1258 // The answer is obviously yes if the new and old exceptions are the same exception
anatofuz
parents:
diff changeset
1259 // If no
anatofuz
parents:
diff changeset
1260 // throw;
anatofuz
parents:
diff changeset
1261 __cxa_eh_globals* globals = __cxa_get_globals_fast();
anatofuz
parents:
diff changeset
1262 __cxa_exception* new_exception_header = globals->caughtExceptions;
anatofuz
parents:
diff changeset
1263 if (new_exception_header == 0)
anatofuz
parents:
diff changeset
1264 // This shouldn't be able to happen!
anatofuz
parents:
diff changeset
1265 std::__terminate(t_handler);
anatofuz
parents:
diff changeset
1266 bool native_new_exception = __isOurExceptionClass(&new_exception_header->unwindHeader);
anatofuz
parents:
diff changeset
1267 void* adjustedPtr;
anatofuz
parents:
diff changeset
1268 if (native_new_exception && (new_exception_header != old_exception_header))
anatofuz
parents:
diff changeset
1269 {
anatofuz
parents:
diff changeset
1270 const __shim_type_info* excpType =
anatofuz
parents:
diff changeset
1271 static_cast<const __shim_type_info*>(new_exception_header->exceptionType);
anatofuz
parents:
diff changeset
1272 adjustedPtr =
anatofuz
parents:
diff changeset
1273 __getExceptionClass(&new_exception_header->unwindHeader) == kOurDependentExceptionClass ?
anatofuz
parents:
diff changeset
1274 ((__cxa_dependent_exception*)new_exception_header)->primaryException :
anatofuz
parents:
diff changeset
1275 new_exception_header + 1;
anatofuz
parents:
diff changeset
1276 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,
anatofuz
parents:
diff changeset
1277 excpType, adjustedPtr, unwind_exception))
anatofuz
parents:
diff changeset
1278 {
anatofuz
parents:
diff changeset
1279 // We need to __cxa_end_catch, but for the old exception,
anatofuz
parents:
diff changeset
1280 // not the new one. This is a little tricky ...
anatofuz
parents:
diff changeset
1281 // Disguise new_exception_header as a rethrown exception, but
anatofuz
parents:
diff changeset
1282 // don't actually rethrow it. This means you can temporarily
anatofuz
parents:
diff changeset
1283 // end the catch clause enclosing new_exception_header without
anatofuz
parents:
diff changeset
1284 // __cxa_end_catch destroying new_exception_header.
anatofuz
parents:
diff changeset
1285 new_exception_header->handlerCount = -new_exception_header->handlerCount;
anatofuz
parents:
diff changeset
1286 globals->uncaughtExceptions += 1;
anatofuz
parents:
diff changeset
1287 // Call __cxa_end_catch for new_exception_header
anatofuz
parents:
diff changeset
1288 __cxa_end_catch();
anatofuz
parents:
diff changeset
1289 // Call __cxa_end_catch for old_exception_header
anatofuz
parents:
diff changeset
1290 __cxa_end_catch();
anatofuz
parents:
diff changeset
1291 // Renter this catch clause with new_exception_header
anatofuz
parents:
diff changeset
1292 __cxa_begin_catch(&new_exception_header->unwindHeader);
anatofuz
parents:
diff changeset
1293 // Rethrow new_exception_header
anatofuz
parents:
diff changeset
1294 throw;
anatofuz
parents:
diff changeset
1295 }
anatofuz
parents:
diff changeset
1296 }
anatofuz
parents:
diff changeset
1297 // Will a std::bad_exception be catchable by the exception spec at
anatofuz
parents:
diff changeset
1298 // ttypeIndex?
anatofuz
parents:
diff changeset
1299 // If no
anatofuz
parents:
diff changeset
1300 // throw std::bad_exception();
anatofuz
parents:
diff changeset
1301 const __shim_type_info* excpType =
anatofuz
parents:
diff changeset
1302 static_cast<const __shim_type_info*>(&typeid(std::bad_exception));
anatofuz
parents:
diff changeset
1303 std::bad_exception be;
anatofuz
parents:
diff changeset
1304 adjustedPtr = &be;
anatofuz
parents:
diff changeset
1305 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,
anatofuz
parents:
diff changeset
1306 excpType, adjustedPtr, unwind_exception))
anatofuz
parents:
diff changeset
1307 {
anatofuz
parents:
diff changeset
1308 // We need to __cxa_end_catch for both the old exception and the
anatofuz
parents:
diff changeset
1309 // new exception. Technically we should do it in that order.
anatofuz
parents:
diff changeset
1310 // But it is expedient to do it in the opposite order:
anatofuz
parents:
diff changeset
1311 // Call __cxa_end_catch for new_exception_header
anatofuz
parents:
diff changeset
1312 __cxa_end_catch();
anatofuz
parents:
diff changeset
1313 // Throw std::bad_exception will __cxa_end_catch for
anatofuz
parents:
diff changeset
1314 // old_exception_header
anatofuz
parents:
diff changeset
1315 throw be;
anatofuz
parents:
diff changeset
1316 }
anatofuz
parents:
diff changeset
1317 }
anatofuz
parents:
diff changeset
1318 }
anatofuz
parents:
diff changeset
1319 std::__terminate(t_handler);
anatofuz
parents:
diff changeset
1320 }
anatofuz
parents:
diff changeset
1321
anatofuz
parents:
diff changeset
1322 } // extern "C"
anatofuz
parents:
diff changeset
1323
anatofuz
parents:
diff changeset
1324 } // __cxxabiv1