1//===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===//
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
9/* zig patch: remove compiler-rt int_lib.h dependency */
10#if __ARM_EABI__
11#ifdef COMPILER_RT_ARMHF_TARGET
12#define COMPILER_RT_ABI
13#else
14#define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
15#endif
16#else
17#define COMPILER_RT_ABI
18#endif
19
20#define compilerrt_abort() __builtin_unreachable()
21
22#include <unwind.h>
23/* zig patch: remove unwind-ehabi-helpers.h dependency */
24
25#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
26#include <windows.h>
27#include <winnt.h>
28
29EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT,
30 PDISPATCHER_CONTEXT,
31 _Unwind_Personality_Fn);
32#endif
33
34#ifndef __has_feature
35#define __has_feature(__feature) 0
36#endif
37
38#if __has_feature(ptrauth_calls)
39#include <ptrauth.h>
40
41// `__ptrauth_restricted_intptr` is a feature of apple clang that predates
42// support for direct application of `__ptrauth` to integer types. This
43// guard is necessary to support compilation with those compiler.
44#if __has_feature(ptrauth_restricted_intptr_qualifier)
45#define __ptrauth_gcc_personality_intptr(key, addressDiscriminated, \
46 discriminator) \
47 __ptrauth_restricted_intptr(key, addressDiscriminated, discriminator)
48#else
49#define __ptrauth_gcc_personality_intptr(key, addressDiscriminated, \
50 discriminator) \
51 __ptrauth(key, addressDiscriminated, discriminator)
52#endif
53#else
54#define __ptrauth_gcc_personality_intptr(...)
55#endif
56
57#define __ptrauth_gcc_personality_func_key ptrauth_key_function_pointer
58
59// ptrauth_string_discriminator("__gcc_personality_v0'funcStart") == 0xDFEB
60#define __ptrauth_gcc_personality_func_start \
61 __ptrauth_gcc_personality_intptr(__ptrauth_gcc_personality_func_key, 1, \
62 0xDFEB)
63
64// ptrauth_string_discriminator("__gcc_personality_v0'start") == 0x52DC
65#define __ptrauth_gcc_personality_start \
66 __ptrauth_gcc_personality_intptr(__ptrauth_gcc_personality_func_key, 1, \
67 0x52DC)
68
69// ptrauth_string_discriminator("__gcc_personality_v0'length") == 0xFFF7
70#define __ptrauth_gcc_personality_length \
71 __ptrauth_gcc_personality_intptr(__ptrauth_gcc_personality_func_key, 1, \
72 0xFFF7)
73
74// ptrauth_string_discriminator("__gcc_personality_v0'landingPadOffset") ==
75// 0x6498
76#define __ptrauth_gcc_personality_lpoffset \
77 __ptrauth_gcc_personality_intptr(__ptrauth_gcc_personality_func_key, 1, \
78 0x6498)
79
80// ptrauth_string_discriminator("__gcc_personality_v0'landingPad") == 0xA134
81#define __ptrauth_gcc_personality_lpad_disc 0xA134
82#define __ptrauth_gcc_personality_lpad \
83 __ptrauth_gcc_personality_intptr(__ptrauth_gcc_personality_func_key, 1, \
84 __ptrauth_gcc_personality_lpad_disc)
85
86// Pointer encodings documented at:
87// http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
88
89#define DW_EH_PE_omit 0xff // no data follows
90
91#define DW_EH_PE_absptr 0x00
92#define DW_EH_PE_uleb128 0x01
93#define DW_EH_PE_udata2 0x02
94#define DW_EH_PE_udata4 0x03
95#define DW_EH_PE_udata8 0x04
96#define DW_EH_PE_sleb128 0x09
97#define DW_EH_PE_sdata2 0x0A
98#define DW_EH_PE_sdata4 0x0B
99#define DW_EH_PE_sdata8 0x0C
100
101#define DW_EH_PE_pcrel 0x10
102#define DW_EH_PE_textrel 0x20
103#define DW_EH_PE_datarel 0x30
104#define DW_EH_PE_funcrel 0x40
105#define DW_EH_PE_aligned 0x50
106#define DW_EH_PE_indirect 0x80 // gcc extension
107
108// read a uleb128 encoded value and advance pointer
109static size_t readULEB128(const uint8_t **data) {
110 size_t result = 0;
111 size_t shift = 0;
112 unsigned char byte;
113 const uint8_t *p = *data;
114 do {
115 byte = *p++;
116 result |= (byte & 0x7f) << shift;
117 shift += 7;
118 } while (byte & 0x80);
119 *data = p;
120 return result;
121}
122
123// read a pointer encoded value and advance pointer
124static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
125 const uint8_t *p = *data;
126 uintptr_t result = 0;
127
128 if (encoding == DW_EH_PE_omit)
129 return 0;
130
131 // first get value
132 switch (encoding & 0x0F) {
133 case DW_EH_PE_absptr:
134 result = *((const uintptr_t *)p);
135 p += sizeof(uintptr_t);
136 break;
137 case DW_EH_PE_uleb128:
138 result = readULEB128(&p);
139 break;
140 case DW_EH_PE_udata2:
141 result = *((const uint16_t *)p);
142 p += sizeof(uint16_t);
143 break;
144 case DW_EH_PE_udata4:
145 result = *((const uint32_t *)p);
146 p += sizeof(uint32_t);
147 break;
148 case DW_EH_PE_udata8:
149 result = *((const uint64_t *)p);
150 p += sizeof(uint64_t);
151 break;
152 case DW_EH_PE_sdata2:
153 result = *((const int16_t *)p);
154 p += sizeof(int16_t);
155 break;
156 case DW_EH_PE_sdata4:
157 result = *((const int32_t *)p);
158 p += sizeof(int32_t);
159 break;
160 case DW_EH_PE_sdata8:
161 result = *((const int64_t *)p);
162 p += sizeof(int64_t);
163 break;
164 case DW_EH_PE_sleb128:
165 default:
166 // not supported
167 compilerrt_abort();
168 break;
169 }
170
171 // then add relative offset
172 switch (encoding & 0x70) {
173 case DW_EH_PE_absptr:
174 // do nothing
175 break;
176 case DW_EH_PE_pcrel:
177 result += (uintptr_t)(*data);
178 break;
179 case DW_EH_PE_textrel:
180 case DW_EH_PE_datarel:
181 case DW_EH_PE_funcrel:
182 case DW_EH_PE_aligned:
183 default:
184 // not supported
185 compilerrt_abort();
186 break;
187 }
188
189 // then apply indirection
190 if (encoding & DW_EH_PE_indirect) {
191 result = *((const uintptr_t *)result);
192 }
193
194 *data = p;
195 return result;
196}
197
198#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
199 !defined(__ARM_DWARF_EH__) && !defined(__SEH__)
200#define USING_ARM_EHABI 1
201_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception *,
202 struct _Unwind_Context *);
203#endif
204
205static inline _Unwind_Reason_Code
206continueUnwind(struct _Unwind_Exception *exceptionObject,
207 struct _Unwind_Context *context) {
208#if USING_ARM_EHABI
209 // On ARM EHABI the personality routine is responsible for actually
210 // unwinding a single stack frame before returning (ARM EHABI Sec. 6.1).
211 if (__gnu_unwind_frame(exceptionObject, context) != _URC_OK)
212 return _URC_FAILURE;
213#endif
214 return _URC_CONTINUE_UNWIND;
215}
216
217// The C compiler makes references to __gcc_personality_v0 in
218// the dwarf unwind information for translation units that use
219// __attribute__((cleanup(xx))) on local variables.
220// This personality routine is called by the system unwinder
221// on each frame as the stack is unwound during a C++ exception
222// throw through a C function compiled with -fexceptions.
223#if __USING_SJLJ_EXCEPTIONS__
224// the setjump-longjump based exceptions personality routine has a
225// different name
226COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_sj0(
227 int version, _Unwind_Action actions, uint64_t exceptionClass,
228 struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
229#elif USING_ARM_EHABI
230// The ARM EHABI personality routine has a different signature.
231COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
232 _Unwind_State state, struct _Unwind_Exception *exceptionObject,
233 struct _Unwind_Context *context)
234#elif defined(__SEH__)
235static _Unwind_Reason_Code __gcc_personality_imp(
236 int version, _Unwind_Action actions, uint64_t exceptionClass,
237 struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
238#else
239COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
240 int version, _Unwind_Action actions, uint64_t exceptionClass,
241 struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
242#endif
243{
244 // Since C does not have catch clauses, there is nothing to do during
245 // phase 1 (the search phase).
246#if USING_ARM_EHABI
247 // After resuming from a cleanup we should also continue on to the next
248 // frame straight away.
249 if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
250#else
251 if (actions & _UA_SEARCH_PHASE)
252#endif
253 return continueUnwind(exceptionObject, context);
254
255 // There is nothing to do if there is no LSDA for this frame.
256 const uint8_t *lsda = (uint8_t *)_Unwind_GetLanguageSpecificData(context);
257 if (lsda == (uint8_t *)0)
258 return continueUnwind(exceptionObject, context);
259
260 uintptr_t pc = (uintptr_t)_Unwind_GetIP(context) - 1;
261 uintptr_t __ptrauth_gcc_personality_func_start funcStart =
262 (uintptr_t)_Unwind_GetRegionStart(context);
263 uintptr_t pcOffset = pc - funcStart;
264
265 // Parse LSDA header.
266 uint8_t lpStartEncoding = *lsda++;
267 if (lpStartEncoding != DW_EH_PE_omit) {
268 readEncodedPointer(&lsda, lpStartEncoding);
269 }
270 uint8_t ttypeEncoding = *lsda++;
271 if (ttypeEncoding != DW_EH_PE_omit) {
272 readULEB128(&lsda);
273 }
274 // Walk call-site table looking for range that includes current PC.
275 uint8_t callSiteEncoding = *lsda++;
276 size_t callSiteTableLength = readULEB128(&lsda);
277 const uint8_t *callSiteTableStart = lsda;
278 const uint8_t *callSiteTableEnd = callSiteTableStart + callSiteTableLength;
279 const uint8_t *p = callSiteTableStart;
280 while (p < callSiteTableEnd) {
281 uintptr_t __ptrauth_gcc_personality_start start =
282 readEncodedPointer(&p, callSiteEncoding);
283 size_t __ptrauth_gcc_personality_length length =
284 readEncodedPointer(&p, callSiteEncoding);
285 size_t __ptrauth_gcc_personality_lpoffset landingPadOffset =
286 readEncodedPointer(&p, callSiteEncoding);
287 readULEB128(&p); // action value not used for C code
288 if (landingPadOffset == 0)
289 continue; // no landing pad for this entry
290 if ((start <= pcOffset) && (pcOffset < (start + length))) {
291 // Found landing pad for the PC.
292 // Set Instruction Pointer to so we re-enter function
293 // at landing pad. The landing pad is created by the compiler
294 // to take two parameters in registers.
295 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
296 (uintptr_t)exceptionObject);
297 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
298 size_t __ptrauth_gcc_personality_lpad landingPad =
299 funcStart + landingPadOffset;
300#if __has_feature(ptrauth_calls)
301 uintptr_t stackPointer = _Unwind_GetGR(context, -2);
302 const uintptr_t existingDiscriminator = ptrauth_blend_discriminator(
303 &landingPad, __ptrauth_gcc_personality_lpad_disc);
304 // newIP is authenticated as if it were qualified with a pseudo qualifier
305 // along the lines of:
306 // __ptrauth(ptrauth_key_return_address, <stackPointer>, 0)
307 // where the stack pointer is used in place of the strict storage
308 // address.
309 uintptr_t newIP = (uintptr_t)ptrauth_auth_and_resign(
310 *(void **)&landingPad, __ptrauth_gcc_personality_func_key,
311 existingDiscriminator, ptrauth_key_return_address, stackPointer);
312 _Unwind_SetIP(context, newIP);
313#else
314 _Unwind_SetIP(context, landingPad);
315#endif
316 return _URC_INSTALL_CONTEXT;
317 }
318 }
319
320 // No landing pad found, continue unwinding.
321 return continueUnwind(exceptionObject, context);
322}
323
324#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
325COMPILER_RT_ABI EXCEPTION_DISPOSITION
326__gcc_personality_seh0(PEXCEPTION_RECORD ms_exc, void *this_frame,
327 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp) {
328 return _GCC_specific_handler(ms_exc, this_frame, ms_orig_context, ms_disp,
329 __gcc_personality_imp);
330}
331#endif