blob: 8c6c8bca853c6b460eec066b11d9b0368c44fd30 [file] [log] [blame]
Nico Weber086048d2019-08-12 19:11:23 +00001//===------------------------- cxa_exception.h ----------------------------===//
Howard Hinnant987afbe2011-12-06 18:01:47 +00002//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant987afbe2011-12-06 18:01:47 +00006//
Louis Dionne2cee0e22019-10-01 18:28:20 +00007//
Howard Hinnant987afbe2011-12-06 18:01:47 +00008// This file implements the "Exception Handling APIs"
Louis Dionne2b0da3d2019-04-11 16:37:07 +00009// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
Louis Dionne2cee0e22019-10-01 18:28:20 +000010//
Howard Hinnant987afbe2011-12-06 18:01:47 +000011//===----------------------------------------------------------------------===//
12
Howard Hinnant4ac72dd2012-03-19 16:20:34 +000013#ifndef _CXA_EXCEPTION_H
14#define _CXA_EXCEPTION_H
15
Marshall Clow1df50ca2011-07-20 14:53:53 +000016#include <exception> // for std::unexpected_handler and std::terminate_handler
Mehdi Amini5a403582017-04-04 05:38:38 +000017#include "cxxabi.h"
Marshall Clow1df50ca2011-07-20 14:53:53 +000018#include "unwind.h"
19
20namespace __cxxabiv1 {
21
Howard Hinnant6830b2a2012-01-24 18:15:20 +000022static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
23static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
Dan Albertb98c20c2015-02-05 23:48:06 +000024static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
25
Louis Dionne6f9459f2019-10-03 14:24:53 +000026_LIBCXXABI_HIDDEN uint64_t __getExceptionClass (const _Unwind_Exception*);
27_LIBCXXABI_HIDDEN void __setExceptionClass ( _Unwind_Exception*, uint64_t);
28_LIBCXXABI_HIDDEN bool __isOurExceptionClass(const _Unwind_Exception*);
Marshall Clow611a55a2018-10-10 16:18:37 +000029
Shoaib Meenaife989a92017-03-01 03:55:57 +000030struct _LIBCXXABI_HIDDEN __cxa_exception {
Martin Storsjö09dc8842020-02-01 13:32:57 +020031#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
Steven Wuf2a43602020-01-30 10:02:23 -080032 // Now _Unwind_Exception is marked with __attribute__((aligned)),
33 // which implies __cxa_exception is also aligned. Insert padding
34 // in the beginning of the struct, rather than before unwindHeader.
35 void *reserve;
36
Marshall Clow1df50ca2011-07-20 14:53:53 +000037 // This is a new field to support C++ 0x exception_ptr.
38 // For binary compatibility it is at the start of this
39 // struct which is prepended to the object thrown in
40 // __cxa_allocate_exception.
Logan Chienaacc1c72014-04-12 11:56:41 +000041 size_t referenceCount;
Marshall Clow1df50ca2011-07-20 14:53:53 +000042#endif
Dan Albertb98c20c2015-02-05 23:48:06 +000043
Marshall Clow1df50ca2011-07-20 14:53:53 +000044 // Manage the exception object itself.
Logan Chienaacc1c72014-04-12 11:56:41 +000045 std::type_info *exceptionType;
Dan Albertb98c20c2015-02-05 23:48:06 +000046 void (*exceptionDestructor)(void *);
Logan Chienaacc1c72014-04-12 11:56:41 +000047 std::unexpected_handler unexpectedHandler;
48 std::terminate_handler terminateHandler;
49
50 __cxa_exception *nextException;
51
52 int handlerCount;
53
Ranjeet Singhef6e6722017-03-01 11:42:01 +000054#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chienaacc1c72014-04-12 11:56:41 +000055 __cxa_exception* nextPropagatingException;
56 int propagationCount;
Marshall Clow1df50ca2011-07-20 14:53:53 +000057#else
Logan Chienaacc1c72014-04-12 11:56:41 +000058 int handlerSwitchValue;
59 const unsigned char *actionRecord;
60 const unsigned char *languageSpecificData;
61 void *catchTemp;
62 void *adjustedPtr;
Marshall Clow1df50ca2011-07-20 14:53:53 +000063#endif
64
Martin Storsjö09dc8842020-02-01 13:32:57 +020065#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
Marshall Clow1df50ca2011-07-20 14:53:53 +000066 // This is a new field to support C++ 0x exception_ptr.
67 // For binary compatibility it is placed where the compiler
68 // previously adding padded to 64-bit align unwindHeader.
Logan Chienaacc1c72014-04-12 11:56:41 +000069 size_t referenceCount;
Marshall Clow1df50ca2011-07-20 14:53:53 +000070#endif
Akira Hatanakac57477b22017-05-13 06:28:17 +000071 _Unwind_Exception unwindHeader;
Logan Chienaacc1c72014-04-12 11:56:41 +000072};
Howard Hinnant6ccae152011-12-08 19:35:18 +000073
74// http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
Nico Weberae543872014-06-25 23:52:07 +000075// The layout of this structure MUST match the layout of __cxa_exception, with
76// primaryException instead of referenceCount.
Shoaib Meenaife989a92017-03-01 03:55:57 +000077struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
Martin Storsjö09dc8842020-02-01 13:32:57 +020078#if defined(__LP64__) || defined(_WIN64) || defined(_LIBCXXABI_ARM_EHABI)
Steven Wuf2a43602020-01-30 10:02:23 -080079 void* reserve; // padding.
Logan Chienaacc1c72014-04-12 11:56:41 +000080 void* primaryException;
Marshall Clow1df50ca2011-07-20 14:53:53 +000081#endif
Dan Albertb98c20c2015-02-05 23:48:06 +000082
Logan Chienaacc1c72014-04-12 11:56:41 +000083 std::type_info *exceptionType;
Dan Albertb98c20c2015-02-05 23:48:06 +000084 void (*exceptionDestructor)(void *);
Logan Chienaacc1c72014-04-12 11:56:41 +000085 std::unexpected_handler unexpectedHandler;
86 std::terminate_handler terminateHandler;
87
88 __cxa_exception *nextException;
89
90 int handlerCount;
Dan Albertb98c20c2015-02-05 23:48:06 +000091
Ranjeet Singhef6e6722017-03-01 11:42:01 +000092#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chienaacc1c72014-04-12 11:56:41 +000093 __cxa_exception* nextPropagatingException;
94 int propagationCount;
Marshall Clow1df50ca2011-07-20 14:53:53 +000095#else
Logan Chienaacc1c72014-04-12 11:56:41 +000096 int handlerSwitchValue;
97 const unsigned char *actionRecord;
98 const unsigned char *languageSpecificData;
99 void * catchTemp;
100 void *adjustedPtr;
Marshall Clow1df50ca2011-07-20 14:53:53 +0000101#endif
Dan Albertb98c20c2015-02-05 23:48:06 +0000102
Martin Storsjö09dc8842020-02-01 13:32:57 +0200103#if !defined(__LP64__) && !defined(_WIN64) && !defined(_LIBCXXABI_ARM_EHABI)
Logan Chienaacc1c72014-04-12 11:56:41 +0000104 void* primaryException;
Marshall Clow1df50ca2011-07-20 14:53:53 +0000105#endif
Akira Hatanakac57477b22017-05-13 06:28:17 +0000106 _Unwind_Exception unwindHeader;
Logan Chienaacc1c72014-04-12 11:56:41 +0000107};
108
Steven Wuf2a43602020-01-30 10:02:23 -0800109// Verify the negative offsets of different fields.
110static_assert(sizeof(_Unwind_Exception) +
111 offsetof(__cxa_exception, unwindHeader) ==
112 sizeof(__cxa_exception),
113 "unwindHeader has wrong negative offsets");
114static_assert(sizeof(_Unwind_Exception) +
115 offsetof(__cxa_dependent_exception, unwindHeader) ==
116 sizeof(__cxa_dependent_exception),
117 "unwindHeader has wrong negative offsets");
118
119#if defined(_LIBCXXABI_ARM_EHABI)
120static_assert(offsetof(__cxa_exception, propagationCount) +
121 sizeof(_Unwind_Exception) + sizeof(void*) ==
122 sizeof(__cxa_exception),
123 "propagationCount has wrong negative offset");
124static_assert(offsetof(__cxa_dependent_exception, propagationCount) +
125 sizeof(_Unwind_Exception) + sizeof(void*) ==
126 sizeof(__cxa_dependent_exception),
127 "propagationCount has wrong negative offset");
Martin Storsjö09dc8842020-02-01 13:32:57 +0200128#elif defined(__LP64__) || defined(_WIN64)
Steven Wuf2a43602020-01-30 10:02:23 -0800129static_assert(offsetof(__cxa_exception, adjustedPtr) +
130 sizeof(_Unwind_Exception) + sizeof(void*) ==
131 sizeof(__cxa_exception),
132 "adjustedPtr has wrong negative offset");
133static_assert(offsetof(__cxa_dependent_exception, adjustedPtr) +
134 sizeof(_Unwind_Exception) + sizeof(void*) ==
135 sizeof(__cxa_dependent_exception),
136 "adjustedPtr has wrong negative offset");
137#else
138static_assert(offsetof(__cxa_exception, referenceCount) +
139 sizeof(_Unwind_Exception) + sizeof(void*) ==
140 sizeof(__cxa_exception),
141 "referenceCount has wrong negative offset");
142static_assert(offsetof(__cxa_dependent_exception, primaryException) +
143 sizeof(_Unwind_Exception) + sizeof(void*) ==
144 sizeof(__cxa_dependent_exception),
145 "primaryException has wrong negative offset");
146#endif
147
Shoaib Meenaife989a92017-03-01 03:55:57 +0000148struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
Logan Chienaacc1c72014-04-12 11:56:41 +0000149 __cxa_exception * caughtExceptions;
150 unsigned int uncaughtExceptions;
Ranjeet Singhef6e6722017-03-01 11:42:01 +0000151#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chienaacc1c72014-04-12 11:56:41 +0000152 __cxa_exception* propagatingExceptions;
Marshall Clow1df50ca2011-07-20 14:53:53 +0000153#endif
Logan Chienaacc1c72014-04-12 11:56:41 +0000154};
Marshall Clow1df50ca2011-07-20 14:53:53 +0000155
Shoaib Meenaife989a92017-03-01 03:55:57 +0000156extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals ();
157extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
Howard Hinnanteaa65af2012-02-02 20:47:28 +0000158
Shoaib Meenaife989a92017-03-01 03:55:57 +0000159extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
160extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
Logan Chienaacc1c72014-04-12 11:56:41 +0000161
162} // namespace __cxxabiv1
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000163
164#endif // _CXA_EXCEPTION_H