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