blob: 7160680467e42ac2cf1c4dd491271052452e1900 [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
Gabriel Ravier42aa6de2022-08-20 18:09:03 -070044// directly jump to __libunwind_Registers_x86/x86_64_jumpto instead of using
gejin7f493162021-08-26 16:20:38 +080045// 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}
Florian Mayer7ff728a2022-06-03 14:33:08 -0700168extern int __unw_step_stage2(unw_cursor_t *);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000169
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).
Florian Mayer7ff728a2022-06-03 14:33:08 -0700185 int stepResult = __unw_step_stage2(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000186 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000187 _LIBUNWIND_TRACE_UNWINDING(
Florian Mayer7ff728a2022-06-03 14:33:08 -0700188 "unwind_phase2(ex_ojb=%p): __unw_step_stage2() reached "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000189 "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(
Florian Mayer7ff728a2022-06-03 14:33:08 -0700194 "unwind_phase2(ex_ojb=%p): __unw_step_stage2 failed => "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000195 "_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
Florian Mayer7ff728a2022-06-03 14:33:08 -0700299 while (__unw_step_stage2(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) {
Florian Mayer7ff728a2022-06-03 14:33:08 -0700304 _LIBUNWIND_TRACE_UNWINDING(
305 "unwind_phase2_forced(ex_ojb=%p): __unw_step_stage2 "
306 "failed => _URC_END_OF_STACK",
307 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000308 return _URC_FATAL_PHASE2_ERROR;
309 }
310
Daniel Kiss7729bc92021-08-11 10:11:31 +0200311#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000312 // When tracing, print state information.
313 if (_LIBUNWIND_TRACING_UNWINDING) {
314 char functionBuf[512];
315 const char *functionName = functionBuf;
316 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000317 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
318 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000319 (frameInfo.start_ip + offset > frameInfo.end_ip))
320 functionName = ".anonymous.";
321 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000322 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
323 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000324 (void *)exception_object, frameInfo.start_ip, functionName,
325 frameInfo.lsda, frameInfo.handler);
326 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200327#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000328
329 // Call stop function at each frame.
330 _Unwind_Action action =
331 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
332 _Unwind_Reason_Code stopResult =
333 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000334 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000335 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000336 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000337 (void *)exception_object, stopResult);
338 if (stopResult != _URC_NO_REASON) {
339 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000340 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000341 (void *)exception_object);
342 return _URC_FATAL_PHASE2_ERROR;
343 }
344
gejin7f493162021-08-26 16:20:38 +0800345 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000346 // If there is a personality routine, tell it we are unwinding.
347 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800348 _Unwind_Personality_Fn p =
349 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000350 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000351 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000352 (void *)exception_object, (void *)(uintptr_t)p);
353 _Unwind_Reason_Code personalityResult =
354 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000355 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 switch (personalityResult) {
357 case _URC_CONTINUE_UNWIND:
358 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
359 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000360 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000361 (void *)exception_object);
362 // Destructors called, continue unwinding
363 break;
364 case _URC_INSTALL_CONTEXT:
365 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
366 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000367 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000368 (void *)exception_object);
369 // We may get control back if landing pad calls _Unwind_Resume().
gejin7f493162021-08-26 16:20:38 +0800370 __unw_phase2_resume(cursor, framesWalked);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000371 break;
372 default:
373 // Personality routine returned an unknown result code.
374 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
375 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000376 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000377 (void *)exception_object, personalityResult);
378 return _URC_FATAL_PHASE2_ERROR;
379 }
380 }
381 }
382
383 // Call stop function one last time and tell it we've reached the end
384 // of the stack.
385 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000386 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000387 (void *)exception_object);
388 _Unwind_Action lastAction =
389 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
390 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000391 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000392
393 // Clean up phase did not resume at the frame that the search phase said it
394 // would.
395 return _URC_FATAL_PHASE2_ERROR;
396}
397
398
399/// Called by __cxa_throw. Only returns if there is a fatal error.
400_LIBUNWIND_EXPORT _Unwind_Reason_Code
401_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000402 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000403 (void *)exception_object);
404 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000405 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000406 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000407
408 // Mark that this is a non-forced unwind, so _Unwind_Resume()
409 // can do the right thing.
410 exception_object->private_1 = 0;
411 exception_object->private_2 = 0;
412
413 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000414 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000415 if (phase1 != _URC_NO_REASON)
416 return phase1;
417
418 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000419 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000420}
421
422
423
424/// When _Unwind_RaiseException() is in phase2, it hands control
425/// to the personality function at each frame. The personality
426/// may force a jump to a landing pad in that function, the landing
427/// pad code may then call _Unwind_Resume() to continue with the
428/// unwinding. Note: the call to _Unwind_Resume() is from compiler
Gabriel Ravier42aa6de2022-08-20 18:09:03 -0700429/// generated user code. All other _Unwind_* routines are called
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000430/// by the C++ runtime __cxa_* routines.
431///
432/// Note: re-throwing an exception (as opposed to continuing the unwind)
433/// is implemented by having the code call __cxa_rethrow() which
434/// in turn calls _Unwind_Resume_or_Rethrow().
435_LIBUNWIND_EXPORT void
436_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000437 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000438 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000439 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000440 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000441
442 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000443 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000444 (_Unwind_Stop_Fn) exception_object->private_1,
445 (void *)exception_object->private_2);
446 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000447 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000448
449 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
450 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
451}
452
453
454
455/// Not used by C++.
456/// Unwinds stack, calling "stop" function at each frame.
457/// Could be used to implement longjmp().
458_LIBUNWIND_EXPORT _Unwind_Reason_Code
459_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
460 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000461 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000462 (void *)exception_object, (void *)(uintptr_t)stop);
463 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000464 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000465 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000466
467 // Mark that this is a forced unwind, so _Unwind_Resume() can do
468 // the right thing.
469 exception_object->private_1 = (uintptr_t) stop;
470 exception_object->private_2 = (uintptr_t) stop_parameter;
471
472 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000473 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000474}
475
476
477/// Called by personality handler during phase 2 to get LSDA for current frame.
478_LIBUNWIND_EXPORT uintptr_t
479_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
480 unw_cursor_t *cursor = (unw_cursor_t *)context;
481 unw_proc_info_t frameInfo;
482 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000483 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000484 result = (uintptr_t)frameInfo.lsda;
485 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000486 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000487 (void *)context, result);
Xing Xuebbcbce92022-04-13 11:01:59 -0400488#if !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000489 if (result != 0) {
490 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000491 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000492 result);
493 }
Xing Xuebbcbce92022-04-13 11:01:59 -0400494#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000495 return result;
496}
497
498
499/// Called by personality handler during phase 2 to find the start of the
500/// function.
501_LIBUNWIND_EXPORT uintptr_t
502_Unwind_GetRegionStart(struct _Unwind_Context *context) {
503 unw_cursor_t *cursor = (unw_cursor_t *)context;
504 unw_proc_info_t frameInfo;
505 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000506 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000507 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000508 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000509 (void *)context, result);
510 return result;
511}
512
Charles Davisfa2e6202018-08-30 21:29:00 +0000513#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000514
515/// Called by personality handler during phase 2 if a foreign exception
516// is caught.
517_LIBUNWIND_EXPORT void
518_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000519 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000520 (void *)exception_object);
521 if (exception_object->exception_cleanup != NULL)
522 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
523 exception_object);
524}
525
526/// Called by personality handler during phase 2 to get register values.
527_LIBUNWIND_EXPORT uintptr_t
528_Unwind_GetGR(struct _Unwind_Context *context, int index) {
529 unw_cursor_t *cursor = (unw_cursor_t *)context;
530 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000531 __unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000532 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
533 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000534 return (uintptr_t)result;
535}
536
537/// Called by personality handler during phase 2 to alter register values.
538_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
539 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000540 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000541 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000542 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000543 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000544 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000545}
546
547/// Called by personality handler during phase 2 to get instruction pointer.
548_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
549 unw_cursor_t *cursor = (unw_cursor_t *)context;
550 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000551 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000552 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
553 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000554 return (uintptr_t)result;
555}
556
557/// Called by personality handler during phase 2 to alter instruction pointer,
558/// such as setting where the landing pad is, so _Unwind_Resume() will
559/// start executing in the landing pad.
560_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
561 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000562 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
563 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000564 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000565 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000566}
567
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000568#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)