blob: 84f865e591fcb791c2e26de90491a811ab9ea00b [file] [log] [blame]
Louis Dionne7f068e52021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
Chandler Carruth61860a52019-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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
8// Implements C++ ABI Exception Handling Level 1 as documented at:
Louis Dionne59d0c602019-04-11 16:37:07 +00009// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
Saleem Abdulrasool17552662015-04-24 19:39:17 +000010// using libunwind
11//
12//===----------------------------------------------------------------------===//
13
Logan Chien5456e8d2015-07-24 00:16:48 +000014// ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are
15// defining inline functions to delegate the function calls to
16// _Unwind_VRS_{Get,Set}(). However, some applications might declare the
17// function protetype directly (instead of including <unwind.h>), thus we need
18// to export these functions from libunwind.so as well.
19#define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1
20
Saleem Abdulrasool17552662015-04-24 19:39:17 +000021#include <inttypes.h>
22#include <stdint.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27
gejin7f493162021-08-26 16:20:38 +080028#include "cet_unwind.h"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000029#include "config.h"
Petr Hosek9bbfad52019-04-03 21:50:03 +000030#include "libunwind.h"
31#include "libunwind_ext.h"
32#include "unwind.h"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000033
Martin Storsjo9f16c6c2017-09-26 08:07:26 +000034#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000035
Charles Davisfa2e6202018-08-30 21:29:00 +000036#ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
37
gejin7f493162021-08-26 16:20:38 +080038// When CET is enabled, each "call" instruction will push return address to
39// CET shadow stack, each "ret" instruction will pop current CET shadow stack
40// top and compare it with target address which program will return.
41// In exception handing, some stack frames will be skipped before jumping to
42// landing pad and we must adjust CET shadow stack accordingly.
43// _LIBUNWIND_POP_CET_SSP is used to adjust CET shadow stack pointer and we
44// directly jump to __libunwind_Registerts_x86/x86_64_jumpto instead of using
45// a regular function call to avoid pushing to CET shadow stack again.
46#if !defined(_LIBUNWIND_USE_CET)
kristina6e7aaea2022-04-08 17:04:35 +010047#define __unw_phase2_resume(cursor, fn) \
48 do { \
49 (void)fn; \
50 __unw_resume((cursor)); \
51 } while (0)
gejin7f493162021-08-26 16:20:38 +080052#elif defined(_LIBUNWIND_TARGET_I386)
53#define __unw_phase2_resume(cursor, fn) \
54 do { \
55 _LIBUNWIND_POP_CET_SSP((fn)); \
56 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
57 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
58 __asm__ volatile("push %%edi\n\t" \
59 "sub $4, %%esp\n\t" \
60 "jmp *%%edx\n\t" :: "D"(cetRegContext), \
61 "d"(cetJumpAddress)); \
62 } while (0)
63#elif defined(_LIBUNWIND_TARGET_X86_64)
64#define __unw_phase2_resume(cursor, fn) \
65 do { \
66 _LIBUNWIND_POP_CET_SSP((fn)); \
67 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
68 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
69 __asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \
70 "d"(cetJumpAddress)); \
71 } while (0)
72#endif
73
Saleem Abdulrasool17552662015-04-24 19:39:17 +000074static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000075unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000076 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000077
78 // Walk each frame looking for a place to stop.
Fangrui Song45db2df2020-11-21 12:38:00 -080079 while (true) {
Ed Maste6f723382016-08-30 13:08:21 +000080 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000081 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +000082 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000083 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000084 _LIBUNWIND_TRACE_UNWINDING(
85 "unwind_phase1(ex_ojb=%p): __unw_step() reached "
86 "bottom => _URC_END_OF_STACK",
87 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000088 return _URC_END_OF_STACK;
89 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000090 _LIBUNWIND_TRACE_UNWINDING(
91 "unwind_phase1(ex_ojb=%p): __unw_step failed => "
92 "_URC_FATAL_PHASE1_ERROR",
93 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000094 return _URC_FATAL_PHASE1_ERROR;
95 }
96
97 // See if frame has code to run (has personality routine).
98 unw_proc_info_t frameInfo;
99 unw_word_t sp;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000100 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
101 _LIBUNWIND_TRACE_UNWINDING(
102 "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
103 "failed => _URC_FATAL_PHASE1_ERROR",
104 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105 return _URC_FATAL_PHASE1_ERROR;
106 }
107
Daniel Kiss7729bc92021-08-11 10:11:31 +0200108#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000109 // When tracing, print state information.
110 if (_LIBUNWIND_TRACING_UNWINDING) {
111 char functionBuf[512];
112 const char *functionName = functionBuf;
113 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000114 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
115 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000116 (frameInfo.start_ip + offset > frameInfo.end_ip))
117 functionName = ".anonymous.";
118 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000119 __unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000120 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000121 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
122 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000123 (void *)exception_object, pc, frameInfo.start_ip, functionName,
124 frameInfo.lsda, frameInfo.handler);
125 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200126#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000127
128 // If there is a personality routine, ask it if it will want to stop at
129 // this frame.
130 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800131 _Unwind_Personality_Fn p =
132 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000133 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000134 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000135 (void *)exception_object, (void *)(uintptr_t)p);
136 _Unwind_Reason_Code personalityResult =
137 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000138 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000139 switch (personalityResult) {
140 case _URC_HANDLER_FOUND:
141 // found a catch clause or locals that need destructing in this frame
142 // stop search and remember stack pointer at the frame
Petr Hosek9bbfad52019-04-03 21:50:03 +0000143 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144 exception_object->private_2 = (uintptr_t)sp;
145 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000146 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000147 (void *)exception_object);
148 return _URC_NO_REASON;
149
150 case _URC_CONTINUE_UNWIND:
151 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000152 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000153 (void *)exception_object);
154 // continue unwinding
155 break;
156
157 default:
158 // something went wrong
159 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000160 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000161 (void *)exception_object);
162 return _URC_FATAL_PHASE1_ERROR;
163 }
164 }
165 }
166 return _URC_NO_REASON;
167}
168
169
170static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000171unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000172 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000173
Ed Maste41bc5a72016-08-30 15:38:10 +0000174 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000175 (void *)exception_object);
176
gejin7f493162021-08-26 16:20:38 +0800177 // uc is initialized by __unw_getcontext in the parent frame. The first stack
178 // frame walked is unwind_phase2.
179 unsigned framesWalked = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000180 // Walk each frame until we reach where search phase said to stop.
181 while (true) {
182
Ed Maste6f723382016-08-30 13:08:21 +0000183 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000184 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +0000185 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000186 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000187 _LIBUNWIND_TRACE_UNWINDING(
188 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
189 "bottom => _URC_END_OF_STACK",
190 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000191 return _URC_END_OF_STACK;
192 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000193 _LIBUNWIND_TRACE_UNWINDING(
194 "unwind_phase2(ex_ojb=%p): __unw_step failed => "
195 "_URC_FATAL_PHASE1_ERROR",
196 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000197 return _URC_FATAL_PHASE2_ERROR;
198 }
199
200 // Get info about this frame.
201 unw_word_t sp;
202 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000203 __unw_get_reg(cursor, UNW_REG_SP, &sp);
204 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
205 _LIBUNWIND_TRACE_UNWINDING(
206 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
207 "failed => _URC_FATAL_PHASE1_ERROR",
208 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000209 return _URC_FATAL_PHASE2_ERROR;
210 }
211
Daniel Kiss7729bc92021-08-11 10:11:31 +0200212#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000213 // When tracing, print state information.
214 if (_LIBUNWIND_TRACING_UNWINDING) {
215 char functionBuf[512];
216 const char *functionName = functionBuf;
217 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000218 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
219 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000220 (frameInfo.start_ip + offset > frameInfo.end_ip))
221 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000222 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
223 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
224 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000225 (void *)exception_object, frameInfo.start_ip,
226 functionName, sp, frameInfo.lsda,
227 frameInfo.handler);
228 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200229#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000230
gejin7f493162021-08-26 16:20:38 +0800231 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000232 // If there is a personality routine, tell it we are unwinding.
233 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800234 _Unwind_Personality_Fn p =
235 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000236 _Unwind_Action action = _UA_CLEANUP_PHASE;
237 if (sp == exception_object->private_2) {
238 // Tell personality this was the frame it marked in phase 1.
239 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
240 }
241 _Unwind_Reason_Code personalityResult =
242 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000243 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000244 switch (personalityResult) {
245 case _URC_CONTINUE_UNWIND:
246 // Continue unwinding
247 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000248 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000249 (void *)exception_object);
250 if (sp == exception_object->private_2) {
251 // Phase 1 said we would stop at this frame, but we did not...
252 _LIBUNWIND_ABORT("during phase1 personality function said it would "
253 "stop here, but now in phase2 it did not stop here");
254 }
255 break;
256 case _URC_INSTALL_CONTEXT:
257 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000258 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000259 (void *)exception_object);
260 // Personality routine says to transfer control to landing pad.
261 // We may get control back if landing pad calls _Unwind_Resume().
262 if (_LIBUNWIND_TRACING_UNWINDING) {
263 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000264 __unw_get_reg(cursor, UNW_REG_IP, &pc);
265 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000266 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000267 "user code with ip=0x%" PRIxPTR
268 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000269 (void *)exception_object, pc, sp);
270 }
gejin7f493162021-08-26 16:20:38 +0800271
272 __unw_phase2_resume(cursor, framesWalked);
273 // __unw_phase2_resume() only returns if there was an error.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000274 return _URC_FATAL_PHASE2_ERROR;
275 default:
276 // Personality routine returned an unknown result code.
277 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
278 personalityResult);
279 return _URC_FATAL_PHASE2_ERROR;
280 }
281 }
282 }
283
284 // Clean up phase did not resume at the frame that the search phase
285 // said it would...
286 return _URC_FATAL_PHASE2_ERROR;
287}
288
289static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000290unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000291 _Unwind_Exception *exception_object,
292 _Unwind_Stop_Fn stop, void *stop_parameter) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000293 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000294
gejin7f493162021-08-26 16:20:38 +0800295 // uc is initialized by __unw_getcontext in the parent frame. The first stack
296 // frame walked is unwind_phase2_forced.
297 unsigned framesWalked = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000298 // Walk each frame until we reach where search phase said to stop
Petr Hosek9bbfad52019-04-03 21:50:03 +0000299 while (__unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000300
301 // Update info about this frame.
302 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000303 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
304 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000305 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000306 (void *)exception_object);
307 return _URC_FATAL_PHASE2_ERROR;
308 }
309
Daniel Kiss7729bc92021-08-11 10:11:31 +0200310#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000311 // When tracing, print state information.
312 if (_LIBUNWIND_TRACING_UNWINDING) {
313 char functionBuf[512];
314 const char *functionName = functionBuf;
315 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000316 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
317 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000318 (frameInfo.start_ip + offset > frameInfo.end_ip))
319 functionName = ".anonymous.";
320 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000321 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
322 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000323 (void *)exception_object, frameInfo.start_ip, functionName,
324 frameInfo.lsda, frameInfo.handler);
325 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200326#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000327
328 // Call stop function at each frame.
329 _Unwind_Action action =
330 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
331 _Unwind_Reason_Code stopResult =
332 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000333 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000334 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000335 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000336 (void *)exception_object, stopResult);
337 if (stopResult != _URC_NO_REASON) {
338 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000339 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000340 (void *)exception_object);
341 return _URC_FATAL_PHASE2_ERROR;
342 }
343
gejin7f493162021-08-26 16:20:38 +0800344 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000345 // If there is a personality routine, tell it we are unwinding.
346 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800347 _Unwind_Personality_Fn p =
348 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000349 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000350 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000351 (void *)exception_object, (void *)(uintptr_t)p);
352 _Unwind_Reason_Code personalityResult =
353 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000354 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000355 switch (personalityResult) {
356 case _URC_CONTINUE_UNWIND:
357 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
358 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000359 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000360 (void *)exception_object);
361 // Destructors called, continue unwinding
362 break;
363 case _URC_INSTALL_CONTEXT:
364 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
365 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000366 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000367 (void *)exception_object);
368 // We may get control back if landing pad calls _Unwind_Resume().
gejin7f493162021-08-26 16:20:38 +0800369 __unw_phase2_resume(cursor, framesWalked);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000370 break;
371 default:
372 // Personality routine returned an unknown result code.
373 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
374 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000375 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000376 (void *)exception_object, personalityResult);
377 return _URC_FATAL_PHASE2_ERROR;
378 }
379 }
380 }
381
382 // Call stop function one last time and tell it we've reached the end
383 // of the stack.
384 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000385 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000386 (void *)exception_object);
387 _Unwind_Action lastAction =
388 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
389 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000390 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000391
392 // Clean up phase did not resume at the frame that the search phase said it
393 // would.
394 return _URC_FATAL_PHASE2_ERROR;
395}
396
397
398/// Called by __cxa_throw. Only returns if there is a fatal error.
399_LIBUNWIND_EXPORT _Unwind_Reason_Code
400_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000401 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000402 (void *)exception_object);
403 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000404 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000405 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000406
407 // Mark that this is a non-forced unwind, so _Unwind_Resume()
408 // can do the right thing.
409 exception_object->private_1 = 0;
410 exception_object->private_2 = 0;
411
412 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000413 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000414 if (phase1 != _URC_NO_REASON)
415 return phase1;
416
417 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000418 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000419}
420
421
422
423/// When _Unwind_RaiseException() is in phase2, it hands control
424/// to the personality function at each frame. The personality
425/// may force a jump to a landing pad in that function, the landing
426/// pad code may then call _Unwind_Resume() to continue with the
427/// unwinding. Note: the call to _Unwind_Resume() is from compiler
428/// geneated user code. All other _Unwind_* routines are called
429/// by the C++ runtime __cxa_* routines.
430///
431/// Note: re-throwing an exception (as opposed to continuing the unwind)
432/// is implemented by having the code call __cxa_rethrow() which
433/// in turn calls _Unwind_Resume_or_Rethrow().
434_LIBUNWIND_EXPORT void
435_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000436 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000437 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000438 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000439 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000440
441 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000442 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000443 (_Unwind_Stop_Fn) exception_object->private_1,
444 (void *)exception_object->private_2);
445 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000446 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000447
448 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
449 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
450}
451
452
453
454/// Not used by C++.
455/// Unwinds stack, calling "stop" function at each frame.
456/// Could be used to implement longjmp().
457_LIBUNWIND_EXPORT _Unwind_Reason_Code
458_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
459 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000460 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000461 (void *)exception_object, (void *)(uintptr_t)stop);
462 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000463 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000464 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000465
466 // Mark that this is a forced unwind, so _Unwind_Resume() can do
467 // the right thing.
468 exception_object->private_1 = (uintptr_t) stop;
469 exception_object->private_2 = (uintptr_t) stop_parameter;
470
471 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000472 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000473}
474
475
476/// Called by personality handler during phase 2 to get LSDA for current frame.
477_LIBUNWIND_EXPORT uintptr_t
478_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
479 unw_cursor_t *cursor = (unw_cursor_t *)context;
480 unw_proc_info_t frameInfo;
481 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000482 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000483 result = (uintptr_t)frameInfo.lsda;
484 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000485 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000486 (void *)context, result);
Xing Xuebbcbce92022-04-13 11:01:59 -0400487#if !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000488 if (result != 0) {
489 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000490 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000491 result);
492 }
Xing Xuebbcbce92022-04-13 11:01:59 -0400493#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000494 return result;
495}
496
497
498/// Called by personality handler during phase 2 to find the start of the
499/// function.
500_LIBUNWIND_EXPORT uintptr_t
501_Unwind_GetRegionStart(struct _Unwind_Context *context) {
502 unw_cursor_t *cursor = (unw_cursor_t *)context;
503 unw_proc_info_t frameInfo;
504 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000505 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000506 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000507 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000508 (void *)context, result);
509 return result;
510}
511
Charles Davisfa2e6202018-08-30 21:29:00 +0000512#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000513
514/// Called by personality handler during phase 2 if a foreign exception
515// is caught.
516_LIBUNWIND_EXPORT void
517_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000518 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000519 (void *)exception_object);
520 if (exception_object->exception_cleanup != NULL)
521 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
522 exception_object);
523}
524
525/// Called by personality handler during phase 2 to get register values.
526_LIBUNWIND_EXPORT uintptr_t
527_Unwind_GetGR(struct _Unwind_Context *context, int index) {
528 unw_cursor_t *cursor = (unw_cursor_t *)context;
529 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000530 __unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000531 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
532 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000533 return (uintptr_t)result;
534}
535
536/// Called by personality handler during phase 2 to alter register values.
537_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
538 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000539 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000540 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000541 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000542 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000543 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000544}
545
546/// Called by personality handler during phase 2 to get instruction pointer.
547_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
548 unw_cursor_t *cursor = (unw_cursor_t *)context;
549 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000550 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000551 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
552 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000553 return (uintptr_t)result;
554}
555
556/// Called by personality handler during phase 2 to alter instruction pointer,
557/// such as setting where the landing pad is, so _Unwind_Resume() will
558/// start executing in the landing pad.
559_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
560 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000561 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
562 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000563 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000564 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000565}
566
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000567#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)