blob: 9203ac771fc3b2eb3ee5b669fbcf48d958d2b9fb [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindLevel1.c -----------------------------===//
2//
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)
47#define __unw_phase2_resume(cursor, fn) __unw_resume((cursor))
48#elif defined(_LIBUNWIND_TARGET_I386)
49#define __unw_phase2_resume(cursor, fn) \
50 do { \
51 _LIBUNWIND_POP_CET_SSP((fn)); \
52 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
53 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
54 __asm__ volatile("push %%edi\n\t" \
55 "sub $4, %%esp\n\t" \
56 "jmp *%%edx\n\t" :: "D"(cetRegContext), \
57 "d"(cetJumpAddress)); \
58 } while (0)
59#elif defined(_LIBUNWIND_TARGET_X86_64)
60#define __unw_phase2_resume(cursor, fn) \
61 do { \
62 _LIBUNWIND_POP_CET_SSP((fn)); \
63 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
64 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
65 __asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \
66 "d"(cetJumpAddress)); \
67 } while (0)
68#endif
69
Saleem Abdulrasool17552662015-04-24 19:39:17 +000070static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000071unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000072 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000073
74 // Walk each frame looking for a place to stop.
Fangrui Song45db2df2020-11-21 12:38:00 -080075 while (true) {
Ed Maste6f723382016-08-30 13:08:21 +000076 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000077 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +000078 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000080 _LIBUNWIND_TRACE_UNWINDING(
81 "unwind_phase1(ex_ojb=%p): __unw_step() reached "
82 "bottom => _URC_END_OF_STACK",
83 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000084 return _URC_END_OF_STACK;
85 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000086 _LIBUNWIND_TRACE_UNWINDING(
87 "unwind_phase1(ex_ojb=%p): __unw_step failed => "
88 "_URC_FATAL_PHASE1_ERROR",
89 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000090 return _URC_FATAL_PHASE1_ERROR;
91 }
92
93 // See if frame has code to run (has personality routine).
94 unw_proc_info_t frameInfo;
95 unw_word_t sp;
Petr Hosek9bbfad52019-04-03 21:50:03 +000096 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
97 _LIBUNWIND_TRACE_UNWINDING(
98 "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
99 "failed => _URC_FATAL_PHASE1_ERROR",
100 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000101 return _URC_FATAL_PHASE1_ERROR;
102 }
103
Daniel Kiss7729bc92021-08-11 10:11:31 +0200104#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105 // When tracing, print state information.
106 if (_LIBUNWIND_TRACING_UNWINDING) {
107 char functionBuf[512];
108 const char *functionName = functionBuf;
109 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000110 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
111 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000112 (frameInfo.start_ip + offset > frameInfo.end_ip))
113 functionName = ".anonymous.";
114 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000115 __unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000116 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000117 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
118 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000119 (void *)exception_object, pc, frameInfo.start_ip, functionName,
120 frameInfo.lsda, frameInfo.handler);
121 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200122#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000123
124 // If there is a personality routine, ask it if it will want to stop at
125 // this frame.
126 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800127 _Unwind_Personality_Fn p =
128 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000129 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000130 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000131 (void *)exception_object, (void *)(uintptr_t)p);
132 _Unwind_Reason_Code personalityResult =
133 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000134 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000135 switch (personalityResult) {
136 case _URC_HANDLER_FOUND:
137 // found a catch clause or locals that need destructing in this frame
138 // stop search and remember stack pointer at the frame
Petr Hosek9bbfad52019-04-03 21:50:03 +0000139 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000140 exception_object->private_2 = (uintptr_t)sp;
141 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000142 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000143 (void *)exception_object);
144 return _URC_NO_REASON;
145
146 case _URC_CONTINUE_UNWIND:
147 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000148 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 (void *)exception_object);
150 // continue unwinding
151 break;
152
153 default:
154 // something went wrong
155 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000156 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000157 (void *)exception_object);
158 return _URC_FATAL_PHASE1_ERROR;
159 }
160 }
161 }
162 return _URC_NO_REASON;
163}
164
165
166static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000167unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000168 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000169
Ed Maste41bc5a72016-08-30 15:38:10 +0000170 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171 (void *)exception_object);
172
gejin7f493162021-08-26 16:20:38 +0800173 // uc is initialized by __unw_getcontext in the parent frame. The first stack
174 // frame walked is unwind_phase2.
175 unsigned framesWalked = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000176 // Walk each frame until we reach where search phase said to stop.
177 while (true) {
178
Ed Maste6f723382016-08-30 13:08:21 +0000179 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000180 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +0000181 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000182 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000183 _LIBUNWIND_TRACE_UNWINDING(
184 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
185 "bottom => _URC_END_OF_STACK",
186 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000187 return _URC_END_OF_STACK;
188 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000189 _LIBUNWIND_TRACE_UNWINDING(
190 "unwind_phase2(ex_ojb=%p): __unw_step failed => "
191 "_URC_FATAL_PHASE1_ERROR",
192 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000193 return _URC_FATAL_PHASE2_ERROR;
194 }
195
196 // Get info about this frame.
197 unw_word_t sp;
198 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000199 __unw_get_reg(cursor, UNW_REG_SP, &sp);
200 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
201 _LIBUNWIND_TRACE_UNWINDING(
202 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
203 "failed => _URC_FATAL_PHASE1_ERROR",
204 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000205 return _URC_FATAL_PHASE2_ERROR;
206 }
207
Daniel Kiss7729bc92021-08-11 10:11:31 +0200208#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000209 // When tracing, print state information.
210 if (_LIBUNWIND_TRACING_UNWINDING) {
211 char functionBuf[512];
212 const char *functionName = functionBuf;
213 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000214 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
215 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216 (frameInfo.start_ip + offset > frameInfo.end_ip))
217 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000218 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
219 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
220 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000221 (void *)exception_object, frameInfo.start_ip,
222 functionName, sp, frameInfo.lsda,
223 frameInfo.handler);
224 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200225#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000226
gejin7f493162021-08-26 16:20:38 +0800227 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000228 // If there is a personality routine, tell it we are unwinding.
229 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800230 _Unwind_Personality_Fn p =
231 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000232 _Unwind_Action action = _UA_CLEANUP_PHASE;
233 if (sp == exception_object->private_2) {
234 // Tell personality this was the frame it marked in phase 1.
235 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
236 }
237 _Unwind_Reason_Code personalityResult =
238 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000239 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000240 switch (personalityResult) {
241 case _URC_CONTINUE_UNWIND:
242 // Continue unwinding
243 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000244 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000245 (void *)exception_object);
246 if (sp == exception_object->private_2) {
247 // Phase 1 said we would stop at this frame, but we did not...
248 _LIBUNWIND_ABORT("during phase1 personality function said it would "
249 "stop here, but now in phase2 it did not stop here");
250 }
251 break;
252 case _URC_INSTALL_CONTEXT:
253 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000254 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000255 (void *)exception_object);
256 // Personality routine says to transfer control to landing pad.
257 // We may get control back if landing pad calls _Unwind_Resume().
258 if (_LIBUNWIND_TRACING_UNWINDING) {
259 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000260 __unw_get_reg(cursor, UNW_REG_IP, &pc);
261 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000262 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000263 "user code with ip=0x%" PRIxPTR
264 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000265 (void *)exception_object, pc, sp);
266 }
gejin7f493162021-08-26 16:20:38 +0800267
268 __unw_phase2_resume(cursor, framesWalked);
269 // __unw_phase2_resume() only returns if there was an error.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000270 return _URC_FATAL_PHASE2_ERROR;
271 default:
272 // Personality routine returned an unknown result code.
273 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
274 personalityResult);
275 return _URC_FATAL_PHASE2_ERROR;
276 }
277 }
278 }
279
280 // Clean up phase did not resume at the frame that the search phase
281 // said it would...
282 return _URC_FATAL_PHASE2_ERROR;
283}
284
285static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000286unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000287 _Unwind_Exception *exception_object,
288 _Unwind_Stop_Fn stop, void *stop_parameter) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000289 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000290
gejin7f493162021-08-26 16:20:38 +0800291 // uc is initialized by __unw_getcontext in the parent frame. The first stack
292 // frame walked is unwind_phase2_forced.
293 unsigned framesWalked = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000294 // Walk each frame until we reach where search phase said to stop
Petr Hosek9bbfad52019-04-03 21:50:03 +0000295 while (__unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000296
297 // Update info about this frame.
298 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000299 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
300 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000301 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000302 (void *)exception_object);
303 return _URC_FATAL_PHASE2_ERROR;
304 }
305
Daniel Kiss7729bc92021-08-11 10:11:31 +0200306#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000307 // When tracing, print state information.
308 if (_LIBUNWIND_TRACING_UNWINDING) {
309 char functionBuf[512];
310 const char *functionName = functionBuf;
311 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000312 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
313 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000314 (frameInfo.start_ip + offset > frameInfo.end_ip))
315 functionName = ".anonymous.";
316 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000317 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
318 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000319 (void *)exception_object, frameInfo.start_ip, functionName,
320 frameInfo.lsda, frameInfo.handler);
321 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200322#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000323
324 // Call stop function at each frame.
325 _Unwind_Action action =
326 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
327 _Unwind_Reason_Code stopResult =
328 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000329 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000330 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000331 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000332 (void *)exception_object, stopResult);
333 if (stopResult != _URC_NO_REASON) {
334 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000335 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000336 (void *)exception_object);
337 return _URC_FATAL_PHASE2_ERROR;
338 }
339
gejin7f493162021-08-26 16:20:38 +0800340 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000341 // If there is a personality routine, tell it we are unwinding.
342 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800343 _Unwind_Personality_Fn p =
344 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000345 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000346 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000347 (void *)exception_object, (void *)(uintptr_t)p);
348 _Unwind_Reason_Code personalityResult =
349 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000350 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000351 switch (personalityResult) {
352 case _URC_CONTINUE_UNWIND:
353 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
354 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000355 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 (void *)exception_object);
357 // Destructors called, continue unwinding
358 break;
359 case _URC_INSTALL_CONTEXT:
360 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
361 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000362 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000363 (void *)exception_object);
364 // We may get control back if landing pad calls _Unwind_Resume().
gejin7f493162021-08-26 16:20:38 +0800365 __unw_phase2_resume(cursor, framesWalked);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000366 break;
367 default:
368 // Personality routine returned an unknown result code.
369 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
370 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000371 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000372 (void *)exception_object, personalityResult);
373 return _URC_FATAL_PHASE2_ERROR;
374 }
375 }
376 }
377
378 // Call stop function one last time and tell it we've reached the end
379 // of the stack.
380 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000381 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000382 (void *)exception_object);
383 _Unwind_Action lastAction =
384 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
385 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000386 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000387
388 // Clean up phase did not resume at the frame that the search phase said it
389 // would.
390 return _URC_FATAL_PHASE2_ERROR;
391}
392
393
394/// Called by __cxa_throw. Only returns if there is a fatal error.
395_LIBUNWIND_EXPORT _Unwind_Reason_Code
396_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000397 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000398 (void *)exception_object);
399 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000400 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000401 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000402
403 // Mark that this is a non-forced unwind, so _Unwind_Resume()
404 // can do the right thing.
405 exception_object->private_1 = 0;
406 exception_object->private_2 = 0;
407
408 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000409 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000410 if (phase1 != _URC_NO_REASON)
411 return phase1;
412
413 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000414 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000415}
416
417
418
419/// When _Unwind_RaiseException() is in phase2, it hands control
420/// to the personality function at each frame. The personality
421/// may force a jump to a landing pad in that function, the landing
422/// pad code may then call _Unwind_Resume() to continue with the
423/// unwinding. Note: the call to _Unwind_Resume() is from compiler
424/// geneated user code. All other _Unwind_* routines are called
425/// by the C++ runtime __cxa_* routines.
426///
427/// Note: re-throwing an exception (as opposed to continuing the unwind)
428/// is implemented by having the code call __cxa_rethrow() which
429/// in turn calls _Unwind_Resume_or_Rethrow().
430_LIBUNWIND_EXPORT void
431_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000432 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000433 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000434 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000435 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000436
437 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000438 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000439 (_Unwind_Stop_Fn) exception_object->private_1,
440 (void *)exception_object->private_2);
441 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000442 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000443
444 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
445 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
446}
447
448
449
450/// Not used by C++.
451/// Unwinds stack, calling "stop" function at each frame.
452/// Could be used to implement longjmp().
453_LIBUNWIND_EXPORT _Unwind_Reason_Code
454_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
455 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000456 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000457 (void *)exception_object, (void *)(uintptr_t)stop);
458 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000459 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000460 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000461
462 // Mark that this is a forced unwind, so _Unwind_Resume() can do
463 // the right thing.
464 exception_object->private_1 = (uintptr_t) stop;
465 exception_object->private_2 = (uintptr_t) stop_parameter;
466
467 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000468 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000469}
470
471
472/// Called by personality handler during phase 2 to get LSDA for current frame.
473_LIBUNWIND_EXPORT uintptr_t
474_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
475 unw_cursor_t *cursor = (unw_cursor_t *)context;
476 unw_proc_info_t frameInfo;
477 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000478 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000479 result = (uintptr_t)frameInfo.lsda;
480 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000481 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000482 (void *)context, result);
483 if (result != 0) {
484 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000485 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000486 result);
487 }
488 return result;
489}
490
491
492/// Called by personality handler during phase 2 to find the start of the
493/// function.
494_LIBUNWIND_EXPORT uintptr_t
495_Unwind_GetRegionStart(struct _Unwind_Context *context) {
496 unw_cursor_t *cursor = (unw_cursor_t *)context;
497 unw_proc_info_t frameInfo;
498 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000499 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000500 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000501 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000502 (void *)context, result);
503 return result;
504}
505
Charles Davisfa2e6202018-08-30 21:29:00 +0000506#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000507
508/// Called by personality handler during phase 2 if a foreign exception
509// is caught.
510_LIBUNWIND_EXPORT void
511_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000512 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000513 (void *)exception_object);
514 if (exception_object->exception_cleanup != NULL)
515 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
516 exception_object);
517}
518
519/// Called by personality handler during phase 2 to get register values.
520_LIBUNWIND_EXPORT uintptr_t
521_Unwind_GetGR(struct _Unwind_Context *context, int index) {
522 unw_cursor_t *cursor = (unw_cursor_t *)context;
523 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000524 __unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000525 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
526 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000527 return (uintptr_t)result;
528}
529
530/// Called by personality handler during phase 2 to alter register values.
531_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
532 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000533 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000534 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000535 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000536 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000537 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000538}
539
540/// Called by personality handler during phase 2 to get instruction pointer.
541_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
542 unw_cursor_t *cursor = (unw_cursor_t *)context;
543 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000544 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000545 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
546 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000547 return (uintptr_t)result;
548}
549
550/// Called by personality handler during phase 2 to alter instruction pointer,
551/// such as setting where the landing pad is, so _Unwind_Resume() will
552/// start executing in the landing pad.
553_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
554 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000555 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
556 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000557 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000558 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000559}
560
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000561#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)