blob: 7e9adf64246dffe2af21829c6cd61c46335b9509 [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)
jinge90a318d6a2022-11-09 14:24:39 +080053#define __cet_ss_step_size 4
gejin7f493162021-08-26 16:20:38 +080054#define __unw_phase2_resume(cursor, fn) \
55 do { \
56 _LIBUNWIND_POP_CET_SSP((fn)); \
57 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
58 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
59 __asm__ volatile("push %%edi\n\t" \
60 "sub $4, %%esp\n\t" \
61 "jmp *%%edx\n\t" :: "D"(cetRegContext), \
62 "d"(cetJumpAddress)); \
63 } while (0)
64#elif defined(_LIBUNWIND_TARGET_X86_64)
jinge90a318d6a2022-11-09 14:24:39 +080065#define __cet_ss_step_size 8
gejin7f493162021-08-26 16:20:38 +080066#define __unw_phase2_resume(cursor, fn) \
67 do { \
68 _LIBUNWIND_POP_CET_SSP((fn)); \
69 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
70 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
71 __asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \
72 "d"(cetJumpAddress)); \
73 } while (0)
74#endif
75
Saleem Abdulrasool17552662015-04-24 19:39:17 +000076static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000077unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000078 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079
80 // Walk each frame looking for a place to stop.
Fangrui Song45db2df2020-11-21 12:38:00 -080081 while (true) {
Ed Maste6f723382016-08-30 13:08:21 +000082 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000083 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +000084 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000085 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000086 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +080087 "unwind_phase1(ex_obj=%p): __unw_step() reached "
Petr Hosek9bbfad52019-04-03 21:50:03 +000088 "bottom => _URC_END_OF_STACK",
89 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000090 return _URC_END_OF_STACK;
91 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000092 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +080093 "unwind_phase1(ex_obj=%p): __unw_step failed => "
Petr Hosek9bbfad52019-04-03 21:50:03 +000094 "_URC_FATAL_PHASE1_ERROR",
95 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 return _URC_FATAL_PHASE1_ERROR;
97 }
98
99 // See if frame has code to run (has personality routine).
100 unw_proc_info_t frameInfo;
101 unw_word_t sp;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000102 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
103 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800104 "unwind_phase1(ex_obj=%p): __unw_get_proc_info "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000105 "failed => _URC_FATAL_PHASE1_ERROR",
106 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000107 return _URC_FATAL_PHASE1_ERROR;
108 }
109
Daniel Kiss7729bc92021-08-11 10:11:31 +0200110#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000111 // When tracing, print state information.
112 if (_LIBUNWIND_TRACING_UNWINDING) {
113 char functionBuf[512];
114 const char *functionName = functionBuf;
115 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000116 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
117 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000118 (frameInfo.start_ip + offset > frameInfo.end_ip))
119 functionName = ".anonymous.";
120 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000121 __unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000122 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800123 "unwind_phase1(ex_obj=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
Martin Storsjo97e2d982017-10-30 19:06:34 +0000124 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000125 (void *)exception_object, pc, frameInfo.start_ip, functionName,
126 frameInfo.lsda, frameInfo.handler);
127 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200128#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000129
130 // If there is a personality routine, ask it if it will want to stop at
131 // this frame.
132 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800133 _Unwind_Personality_Fn p =
134 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000135 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800136 "unwind_phase1(ex_obj=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000137 (void *)exception_object, (void *)(uintptr_t)p);
138 _Unwind_Reason_Code personalityResult =
139 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000140 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000141 switch (personalityResult) {
142 case _URC_HANDLER_FOUND:
143 // found a catch clause or locals that need destructing in this frame
144 // stop search and remember stack pointer at the frame
Petr Hosek9bbfad52019-04-03 21:50:03 +0000145 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000146 exception_object->private_2 = (uintptr_t)sp;
147 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800148 "unwind_phase1(ex_obj=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 (void *)exception_object);
150 return _URC_NO_REASON;
151
152 case _URC_CONTINUE_UNWIND:
153 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800154 "unwind_phase1(ex_obj=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000155 (void *)exception_object);
156 // continue unwinding
157 break;
158
159 default:
160 // something went wrong
161 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800162 "unwind_phase1(ex_obj=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000163 (void *)exception_object);
164 return _URC_FATAL_PHASE1_ERROR;
165 }
166 }
167 }
168 return _URC_NO_REASON;
169}
Florian Mayer7ff728a2022-06-03 14:33:08 -0700170extern int __unw_step_stage2(unw_cursor_t *);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171
172static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000173unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000174 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000175
jinge9086213b72022-11-10 21:46:49 +0800176 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000177 (void *)exception_object);
178
gejin7f493162021-08-26 16:20:38 +0800179 // uc is initialized by __unw_getcontext in the parent frame. The first stack
180 // frame walked is unwind_phase2.
181 unsigned framesWalked = 1;
jinge90a318d6a2022-11-09 14:24:39 +0800182#ifdef _LIBUNWIND_USE_CET
183 unsigned long shadowStackTop = _get_ssp();
184#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000185 // Walk each frame until we reach where search phase said to stop.
186 while (true) {
187
Ed Maste6f723382016-08-30 13:08:21 +0000188 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000189 // _Unwind_RaiseException).
Florian Mayer7ff728a2022-06-03 14:33:08 -0700190 int stepResult = __unw_step_stage2(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000191 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000192 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800193 "unwind_phase2(ex_obj=%p): __unw_step_stage2() reached "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000194 "bottom => _URC_END_OF_STACK",
195 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000196 return _URC_END_OF_STACK;
197 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000198 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800199 "unwind_phase2(ex_obj=%p): __unw_step_stage2 failed => "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000200 "_URC_FATAL_PHASE1_ERROR",
201 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000202 return _URC_FATAL_PHASE2_ERROR;
203 }
204
205 // Get info about this frame.
206 unw_word_t sp;
207 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000208 __unw_get_reg(cursor, UNW_REG_SP, &sp);
209 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
210 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800211 "unwind_phase2(ex_obj=%p): __unw_get_proc_info "
Petr Hosek9bbfad52019-04-03 21:50:03 +0000212 "failed => _URC_FATAL_PHASE1_ERROR",
213 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000214 return _URC_FATAL_PHASE2_ERROR;
215 }
216
Daniel Kiss7729bc92021-08-11 10:11:31 +0200217#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000218 // When tracing, print state information.
219 if (_LIBUNWIND_TRACING_UNWINDING) {
220 char functionBuf[512];
221 const char *functionName = functionBuf;
222 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000223 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
224 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000225 (frameInfo.start_ip + offset > frameInfo.end_ip))
226 functionName = ".anonymous.";
jinge9086213b72022-11-10 21:46:49 +0800227 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p): start_ip=0x%" PRIxPTR
Martin Storsjo97e2d982017-10-30 19:06:34 +0000228 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
229 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000230 (void *)exception_object, frameInfo.start_ip,
231 functionName, sp, frameInfo.lsda,
232 frameInfo.handler);
233 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200234#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000235
jinge90a318d6a2022-11-09 14:24:39 +0800236// In CET enabled environment, we check return address stored in normal stack
237// against return address stored in CET shadow stack, if the 2 addresses don't
238// match, it means return address in normal stack has been corrupted, we return
239// _URC_FATAL_PHASE2_ERROR.
240#ifdef _LIBUNWIND_USE_CET
241 if (shadowStackTop != 0) {
242 unw_word_t retInNormalStack;
243 __unw_get_reg(cursor, UNW_REG_IP, &retInNormalStack);
244 unsigned long retInShadowStack = *(
245 unsigned long *)(shadowStackTop + __cet_ss_step_size * framesWalked);
246 if (retInNormalStack != retInShadowStack)
247 return _URC_FATAL_PHASE2_ERROR;
248 }
249#endif
gejin7f493162021-08-26 16:20:38 +0800250 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000251 // If there is a personality routine, tell it we are unwinding.
252 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800253 _Unwind_Personality_Fn p =
254 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000255 _Unwind_Action action = _UA_CLEANUP_PHASE;
256 if (sp == exception_object->private_2) {
257 // Tell personality this was the frame it marked in phase 1.
258 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
259 }
260 _Unwind_Reason_Code personalityResult =
261 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000262 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000263 switch (personalityResult) {
264 case _URC_CONTINUE_UNWIND:
265 // Continue unwinding
266 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800267 "unwind_phase2(ex_obj=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000268 (void *)exception_object);
269 if (sp == exception_object->private_2) {
270 // Phase 1 said we would stop at this frame, but we did not...
271 _LIBUNWIND_ABORT("during phase1 personality function said it would "
272 "stop here, but now in phase2 it did not stop here");
273 }
274 break;
275 case _URC_INSTALL_CONTEXT:
276 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800277 "unwind_phase2(ex_obj=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000278 (void *)exception_object);
279 // Personality routine says to transfer control to landing pad.
280 // We may get control back if landing pad calls _Unwind_Resume().
281 if (_LIBUNWIND_TRACING_UNWINDING) {
282 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000283 __unw_get_reg(cursor, UNW_REG_IP, &pc);
284 __unw_get_reg(cursor, UNW_REG_SP, &sp);
jinge9086213b72022-11-10 21:46:49 +0800285 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000286 "user code with ip=0x%" PRIxPTR
287 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000288 (void *)exception_object, pc, sp);
289 }
gejin7f493162021-08-26 16:20:38 +0800290
291 __unw_phase2_resume(cursor, framesWalked);
292 // __unw_phase2_resume() only returns if there was an error.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000293 return _URC_FATAL_PHASE2_ERROR;
294 default:
295 // Personality routine returned an unknown result code.
296 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
297 personalityResult);
298 return _URC_FATAL_PHASE2_ERROR;
299 }
300 }
301 }
302
303 // Clean up phase did not resume at the frame that the search phase
304 // said it would...
305 return _URC_FATAL_PHASE2_ERROR;
306}
307
308static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000309unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000310 _Unwind_Exception *exception_object,
311 _Unwind_Stop_Fn stop, void *stop_parameter) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000312 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000313
gejin7f493162021-08-26 16:20:38 +0800314 // uc is initialized by __unw_getcontext in the parent frame. The first stack
315 // frame walked is unwind_phase2_forced.
316 unsigned framesWalked = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000317 // Walk each frame until we reach where search phase said to stop
Florian Mayer7ff728a2022-06-03 14:33:08 -0700318 while (__unw_step_stage2(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000319
320 // Update info about this frame.
321 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000322 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Florian Mayer7ff728a2022-06-03 14:33:08 -0700323 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800324 "unwind_phase2_forced(ex_obj=%p): __unw_step_stage2 "
Florian Mayer7ff728a2022-06-03 14:33:08 -0700325 "failed => _URC_END_OF_STACK",
326 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000327 return _URC_FATAL_PHASE2_ERROR;
328 }
329
Daniel Kiss7729bc92021-08-11 10:11:31 +0200330#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000331 // When tracing, print state information.
332 if (_LIBUNWIND_TRACING_UNWINDING) {
333 char functionBuf[512];
334 const char *functionName = functionBuf;
335 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000336 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
337 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000338 (frameInfo.start_ip + offset > frameInfo.end_ip))
339 functionName = ".anonymous.";
340 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800341 "unwind_phase2_forced(ex_obj=%p): start_ip=0x%" PRIxPTR
Martin Storsjo97e2d982017-10-30 19:06:34 +0000342 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000343 (void *)exception_object, frameInfo.start_ip, functionName,
344 frameInfo.lsda, frameInfo.handler);
345 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200346#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000347
348 // Call stop function at each frame.
349 _Unwind_Action action =
350 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
351 _Unwind_Reason_Code stopResult =
352 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000353 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000354 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800355 "unwind_phase2_forced(ex_obj=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 (void *)exception_object, stopResult);
357 if (stopResult != _URC_NO_REASON) {
358 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800359 "unwind_phase2_forced(ex_obj=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000360 (void *)exception_object);
361 return _URC_FATAL_PHASE2_ERROR;
362 }
363
gejin7f493162021-08-26 16:20:38 +0800364 ++framesWalked;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000365 // If there is a personality routine, tell it we are unwinding.
366 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800367 _Unwind_Personality_Fn p =
368 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000369 _LIBUNWIND_TRACE_UNWINDING(
jinge9086213b72022-11-10 21:46:49 +0800370 "unwind_phase2_forced(ex_obj=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000371 (void *)exception_object, (void *)(uintptr_t)p);
372 _Unwind_Reason_Code personalityResult =
373 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000374 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000375 switch (personalityResult) {
376 case _URC_CONTINUE_UNWIND:
jinge9086213b72022-11-10 21:46:49 +0800377 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): "
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000378 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000379 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000380 (void *)exception_object);
381 // Destructors called, continue unwinding
382 break;
383 case _URC_INSTALL_CONTEXT:
jinge9086213b72022-11-10 21:46:49 +0800384 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): "
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000385 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000386 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000387 (void *)exception_object);
388 // We may get control back if landing pad calls _Unwind_Resume().
gejin7f493162021-08-26 16:20:38 +0800389 __unw_phase2_resume(cursor, framesWalked);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000390 break;
391 default:
392 // Personality routine returned an unknown result code.
jinge9086213b72022-11-10 21:46:49 +0800393 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): "
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000394 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000395 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000396 (void *)exception_object, personalityResult);
397 return _URC_FATAL_PHASE2_ERROR;
398 }
399 }
400 }
401
402 // Call stop function one last time and tell it we've reached the end
403 // of the stack.
jinge9086213b72022-11-10 21:46:49 +0800404 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000405 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000406 (void *)exception_object);
407 _Unwind_Action lastAction =
408 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
409 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000410 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000411
412 // Clean up phase did not resume at the frame that the search phase said it
413 // would.
414 return _URC_FATAL_PHASE2_ERROR;
415}
416
417
418/// Called by __cxa_throw. Only returns if there is a fatal error.
419_LIBUNWIND_EXPORT _Unwind_Reason_Code
420_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000421 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000422 (void *)exception_object);
423 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000424 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000425 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000426
427 // Mark that this is a non-forced unwind, so _Unwind_Resume()
428 // can do the right thing.
429 exception_object->private_1 = 0;
430 exception_object->private_2 = 0;
431
432 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000433 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000434 if (phase1 != _URC_NO_REASON)
435 return phase1;
436
437 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000438 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000439}
440
441
442
443/// When _Unwind_RaiseException() is in phase2, it hands control
444/// to the personality function at each frame. The personality
445/// may force a jump to a landing pad in that function, the landing
446/// pad code may then call _Unwind_Resume() to continue with the
447/// unwinding. Note: the call to _Unwind_Resume() is from compiler
Gabriel Ravier42aa6de2022-08-20 18:09:03 -0700448/// generated user code. All other _Unwind_* routines are called
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000449/// by the C++ runtime __cxa_* routines.
450///
451/// Note: re-throwing an exception (as opposed to continuing the unwind)
452/// is implemented by having the code call __cxa_rethrow() which
453/// in turn calls _Unwind_Resume_or_Rethrow().
454_LIBUNWIND_EXPORT void
455_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000456 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000457 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000458 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000459 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000460
461 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000462 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000463 (_Unwind_Stop_Fn) exception_object->private_1,
464 (void *)exception_object->private_2);
465 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000466 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000467
468 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
469 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
470}
471
472
473
474/// Not used by C++.
475/// Unwinds stack, calling "stop" function at each frame.
476/// Could be used to implement longjmp().
477_LIBUNWIND_EXPORT _Unwind_Reason_Code
478_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
479 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000480 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000481 (void *)exception_object, (void *)(uintptr_t)stop);
482 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000483 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000484 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000485
486 // Mark that this is a forced unwind, so _Unwind_Resume() can do
487 // the right thing.
488 exception_object->private_1 = (uintptr_t) stop;
489 exception_object->private_2 = (uintptr_t) stop_parameter;
490
491 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000492 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000493}
494
495
496/// Called by personality handler during phase 2 to get LSDA for current frame.
497_LIBUNWIND_EXPORT uintptr_t
498_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
499 unw_cursor_t *cursor = (unw_cursor_t *)context;
500 unw_proc_info_t frameInfo;
501 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000502 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000503 result = (uintptr_t)frameInfo.lsda;
504 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000505 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000506 (void *)context, result);
Xing Xuebbcbce92022-04-13 11:01:59 -0400507#if !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000508 if (result != 0) {
509 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000510 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000511 result);
512 }
Xing Xuebbcbce92022-04-13 11:01:59 -0400513#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000514 return result;
515}
516
517
518/// Called by personality handler during phase 2 to find the start of the
519/// function.
520_LIBUNWIND_EXPORT uintptr_t
521_Unwind_GetRegionStart(struct _Unwind_Context *context) {
522 unw_cursor_t *cursor = (unw_cursor_t *)context;
523 unw_proc_info_t frameInfo;
524 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000525 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000526 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000527 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000528 (void *)context, result);
529 return result;
530}
531
Charles Davisfa2e6202018-08-30 21:29:00 +0000532#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000533
534/// Called by personality handler during phase 2 if a foreign exception
535// is caught.
536_LIBUNWIND_EXPORT void
537_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000538 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000539 (void *)exception_object);
540 if (exception_object->exception_cleanup != NULL)
541 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
542 exception_object);
543}
544
545/// Called by personality handler during phase 2 to get register values.
546_LIBUNWIND_EXPORT uintptr_t
547_Unwind_GetGR(struct _Unwind_Context *context, int index) {
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, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000551 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
552 (void *)context, index, 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 register values.
557_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
558 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000559 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000560 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000561 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000562 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000563 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000564}
565
566/// Called by personality handler during phase 2 to get instruction pointer.
567_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
568 unw_cursor_t *cursor = (unw_cursor_t *)context;
569 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000570 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000571 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
572 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000573 return (uintptr_t)result;
574}
575
576/// Called by personality handler during phase 2 to alter instruction pointer,
577/// such as setting where the landing pad is, so _Unwind_Resume() will
578/// start executing in the landing pad.
579_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
580 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000581 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
582 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000583 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000584 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000585}
586
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000587#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)