blob: 8b8797fb88ad4b55cd8bf33107c6b47b84009e4f [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
Saleem Abdulrasool17552662015-04-24 19:39:17 +000028#include "config.h"
Petr Hosek9bbfad52019-04-03 21:50:03 +000029#include "libunwind.h"
30#include "libunwind_ext.h"
31#include "unwind.h"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000032
Martin Storsjo9f16c6c2017-09-26 08:07:26 +000033#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000034
Charles Davisfa2e6202018-08-30 21:29:00 +000035#ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
36
Saleem Abdulrasool17552662015-04-24 19:39:17 +000037static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000038unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000039 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000040
41 // Walk each frame looking for a place to stop.
Fangrui Song45db2df2020-11-21 12:38:00 -080042 while (true) {
Ed Maste6f723382016-08-30 13:08:21 +000043 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000044 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +000045 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000046 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000047 _LIBUNWIND_TRACE_UNWINDING(
48 "unwind_phase1(ex_ojb=%p): __unw_step() reached "
49 "bottom => _URC_END_OF_STACK",
50 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000051 return _URC_END_OF_STACK;
52 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +000053 _LIBUNWIND_TRACE_UNWINDING(
54 "unwind_phase1(ex_ojb=%p): __unw_step failed => "
55 "_URC_FATAL_PHASE1_ERROR",
56 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000057 return _URC_FATAL_PHASE1_ERROR;
58 }
59
60 // See if frame has code to run (has personality routine).
61 unw_proc_info_t frameInfo;
62 unw_word_t sp;
Petr Hosek9bbfad52019-04-03 21:50:03 +000063 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
64 _LIBUNWIND_TRACE_UNWINDING(
65 "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
66 "failed => _URC_FATAL_PHASE1_ERROR",
67 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000068 return _URC_FATAL_PHASE1_ERROR;
69 }
70
Daniel Kiss7729bc92021-08-11 10:11:31 +020071#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +000072 // When tracing, print state information.
73 if (_LIBUNWIND_TRACING_UNWINDING) {
74 char functionBuf[512];
75 const char *functionName = functionBuf;
76 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +000077 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
78 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079 (frameInfo.start_ip + offset > frameInfo.end_ip))
80 functionName = ".anonymous.";
81 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +000082 __unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000083 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +000084 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
85 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000086 (void *)exception_object, pc, frameInfo.start_ip, functionName,
87 frameInfo.lsda, frameInfo.handler);
88 }
Daniel Kiss7729bc92021-08-11 10:11:31 +020089#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000090
91 // If there is a personality routine, ask it if it will want to stop at
92 // this frame.
93 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -080094 _Unwind_Personality_Fn p =
95 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +000097 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000098 (void *)exception_object, (void *)(uintptr_t)p);
99 _Unwind_Reason_Code personalityResult =
100 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000101 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102 switch (personalityResult) {
103 case _URC_HANDLER_FOUND:
104 // found a catch clause or locals that need destructing in this frame
105 // stop search and remember stack pointer at the frame
Petr Hosek9bbfad52019-04-03 21:50:03 +0000106 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000107 exception_object->private_2 = (uintptr_t)sp;
108 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000109 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000110 (void *)exception_object);
111 return _URC_NO_REASON;
112
113 case _URC_CONTINUE_UNWIND:
114 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000115 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000116 (void *)exception_object);
117 // continue unwinding
118 break;
119
120 default:
121 // something went wrong
122 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000123 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000124 (void *)exception_object);
125 return _URC_FATAL_PHASE1_ERROR;
126 }
127 }
128 }
129 return _URC_NO_REASON;
130}
131
132
133static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000134unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000135 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000136
Ed Maste41bc5a72016-08-30 15:38:10 +0000137 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000138 (void *)exception_object);
139
140 // Walk each frame until we reach where search phase said to stop.
141 while (true) {
142
Ed Maste6f723382016-08-30 13:08:21 +0000143 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +0000145 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000146 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000147 _LIBUNWIND_TRACE_UNWINDING(
148 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
149 "bottom => _URC_END_OF_STACK",
150 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000151 return _URC_END_OF_STACK;
152 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000153 _LIBUNWIND_TRACE_UNWINDING(
154 "unwind_phase2(ex_ojb=%p): __unw_step failed => "
155 "_URC_FATAL_PHASE1_ERROR",
156 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000157 return _URC_FATAL_PHASE2_ERROR;
158 }
159
160 // Get info about this frame.
161 unw_word_t sp;
162 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000163 __unw_get_reg(cursor, UNW_REG_SP, &sp);
164 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
165 _LIBUNWIND_TRACE_UNWINDING(
166 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
167 "failed => _URC_FATAL_PHASE1_ERROR",
168 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000169 return _URC_FATAL_PHASE2_ERROR;
170 }
171
Daniel Kiss7729bc92021-08-11 10:11:31 +0200172#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000173 // When tracing, print state information.
174 if (_LIBUNWIND_TRACING_UNWINDING) {
175 char functionBuf[512];
176 const char *functionName = functionBuf;
177 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000178 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
179 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000180 (frameInfo.start_ip + offset > frameInfo.end_ip))
181 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000182 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
183 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
184 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000185 (void *)exception_object, frameInfo.start_ip,
186 functionName, sp, frameInfo.lsda,
187 frameInfo.handler);
188 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200189#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000190
191 // If there is a personality routine, tell it we are unwinding.
192 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800193 _Unwind_Personality_Fn p =
194 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000195 _Unwind_Action action = _UA_CLEANUP_PHASE;
196 if (sp == exception_object->private_2) {
197 // Tell personality this was the frame it marked in phase 1.
198 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
199 }
200 _Unwind_Reason_Code personalityResult =
201 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000202 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000203 switch (personalityResult) {
204 case _URC_CONTINUE_UNWIND:
205 // Continue unwinding
206 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000207 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000208 (void *)exception_object);
209 if (sp == exception_object->private_2) {
210 // Phase 1 said we would stop at this frame, but we did not...
211 _LIBUNWIND_ABORT("during phase1 personality function said it would "
212 "stop here, but now in phase2 it did not stop here");
213 }
214 break;
215 case _URC_INSTALL_CONTEXT:
216 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000217 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000218 (void *)exception_object);
219 // Personality routine says to transfer control to landing pad.
220 // We may get control back if landing pad calls _Unwind_Resume().
221 if (_LIBUNWIND_TRACING_UNWINDING) {
222 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000223 __unw_get_reg(cursor, UNW_REG_IP, &pc);
224 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000225 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000226 "user code with ip=0x%" PRIxPTR
227 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000228 (void *)exception_object, pc, sp);
229 }
Petr Hosek9bbfad52019-04-03 21:50:03 +0000230 __unw_resume(cursor);
231 // __unw_resume() only returns if there was an error.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000232 return _URC_FATAL_PHASE2_ERROR;
233 default:
234 // Personality routine returned an unknown result code.
235 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
236 personalityResult);
237 return _URC_FATAL_PHASE2_ERROR;
238 }
239 }
240 }
241
242 // Clean up phase did not resume at the frame that the search phase
243 // said it would...
244 return _URC_FATAL_PHASE2_ERROR;
245}
246
247static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000248unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000249 _Unwind_Exception *exception_object,
250 _Unwind_Stop_Fn stop, void *stop_parameter) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000251 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000252
253 // Walk each frame until we reach where search phase said to stop
Petr Hosek9bbfad52019-04-03 21:50:03 +0000254 while (__unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000255
256 // Update info about this frame.
257 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000258 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
259 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000260 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000261 (void *)exception_object);
262 return _URC_FATAL_PHASE2_ERROR;
263 }
264
Daniel Kiss7729bc92021-08-11 10:11:31 +0200265#ifndef NDEBUG
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000266 // When tracing, print state information.
267 if (_LIBUNWIND_TRACING_UNWINDING) {
268 char functionBuf[512];
269 const char *functionName = functionBuf;
270 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000271 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
272 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000273 (frameInfo.start_ip + offset > frameInfo.end_ip))
274 functionName = ".anonymous.";
275 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000276 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
277 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000278 (void *)exception_object, frameInfo.start_ip, functionName,
279 frameInfo.lsda, frameInfo.handler);
280 }
Daniel Kiss7729bc92021-08-11 10:11:31 +0200281#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000282
283 // Call stop function at each frame.
284 _Unwind_Action action =
285 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
286 _Unwind_Reason_Code stopResult =
287 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000288 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000289 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000290 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000291 (void *)exception_object, stopResult);
292 if (stopResult != _URC_NO_REASON) {
293 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000294 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000295 (void *)exception_object);
296 return _URC_FATAL_PHASE2_ERROR;
297 }
298
299 // If there is a personality routine, tell it we are unwinding.
300 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800301 _Unwind_Personality_Fn p =
302 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000303 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000304 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000305 (void *)exception_object, (void *)(uintptr_t)p);
306 _Unwind_Reason_Code personalityResult =
307 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000308 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000309 switch (personalityResult) {
310 case _URC_CONTINUE_UNWIND:
311 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
312 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000313 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000314 (void *)exception_object);
315 // Destructors called, continue unwinding
316 break;
317 case _URC_INSTALL_CONTEXT:
318 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
319 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000320 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000321 (void *)exception_object);
322 // We may get control back if landing pad calls _Unwind_Resume().
Petr Hosek9bbfad52019-04-03 21:50:03 +0000323 __unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000324 break;
325 default:
326 // Personality routine returned an unknown result code.
327 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
328 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000329 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000330 (void *)exception_object, personalityResult);
331 return _URC_FATAL_PHASE2_ERROR;
332 }
333 }
334 }
335
336 // Call stop function one last time and tell it we've reached the end
337 // of the stack.
338 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000339 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000340 (void *)exception_object);
341 _Unwind_Action lastAction =
342 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
343 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000344 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000345
346 // Clean up phase did not resume at the frame that the search phase said it
347 // would.
348 return _URC_FATAL_PHASE2_ERROR;
349}
350
351
352/// Called by __cxa_throw. Only returns if there is a fatal error.
353_LIBUNWIND_EXPORT _Unwind_Reason_Code
354_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000355 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 (void *)exception_object);
357 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000358 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000359 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000360
361 // Mark that this is a non-forced unwind, so _Unwind_Resume()
362 // can do the right thing.
363 exception_object->private_1 = 0;
364 exception_object->private_2 = 0;
365
366 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000367 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000368 if (phase1 != _URC_NO_REASON)
369 return phase1;
370
371 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000372 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000373}
374
375
376
377/// When _Unwind_RaiseException() is in phase2, it hands control
378/// to the personality function at each frame. The personality
379/// may force a jump to a landing pad in that function, the landing
380/// pad code may then call _Unwind_Resume() to continue with the
381/// unwinding. Note: the call to _Unwind_Resume() is from compiler
382/// geneated user code. All other _Unwind_* routines are called
383/// by the C++ runtime __cxa_* routines.
384///
385/// Note: re-throwing an exception (as opposed to continuing the unwind)
386/// is implemented by having the code call __cxa_rethrow() which
387/// in turn calls _Unwind_Resume_or_Rethrow().
388_LIBUNWIND_EXPORT void
389_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000390 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000391 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000392 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000393 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000394
395 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000396 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000397 (_Unwind_Stop_Fn) exception_object->private_1,
398 (void *)exception_object->private_2);
399 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000400 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000401
402 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
403 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
404}
405
406
407
408/// Not used by C++.
409/// Unwinds stack, calling "stop" function at each frame.
410/// Could be used to implement longjmp().
411_LIBUNWIND_EXPORT _Unwind_Reason_Code
412_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
413 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000414 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000415 (void *)exception_object, (void *)(uintptr_t)stop);
416 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000417 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000418 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000419
420 // Mark that this is a forced unwind, so _Unwind_Resume() can do
421 // the right thing.
422 exception_object->private_1 = (uintptr_t) stop;
423 exception_object->private_2 = (uintptr_t) stop_parameter;
424
425 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000426 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000427}
428
429
430/// Called by personality handler during phase 2 to get LSDA for current frame.
431_LIBUNWIND_EXPORT uintptr_t
432_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
433 unw_cursor_t *cursor = (unw_cursor_t *)context;
434 unw_proc_info_t frameInfo;
435 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000436 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000437 result = (uintptr_t)frameInfo.lsda;
438 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000439 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000440 (void *)context, result);
441 if (result != 0) {
442 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000443 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000444 result);
445 }
446 return result;
447}
448
449
450/// Called by personality handler during phase 2 to find the start of the
451/// function.
452_LIBUNWIND_EXPORT uintptr_t
453_Unwind_GetRegionStart(struct _Unwind_Context *context) {
454 unw_cursor_t *cursor = (unw_cursor_t *)context;
455 unw_proc_info_t frameInfo;
456 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000457 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000458 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000459 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000460 (void *)context, result);
461 return result;
462}
463
Charles Davisfa2e6202018-08-30 21:29:00 +0000464#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000465
466/// Called by personality handler during phase 2 if a foreign exception
467// is caught.
468_LIBUNWIND_EXPORT void
469_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000470 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000471 (void *)exception_object);
472 if (exception_object->exception_cleanup != NULL)
473 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
474 exception_object);
475}
476
477/// Called by personality handler during phase 2 to get register values.
478_LIBUNWIND_EXPORT uintptr_t
479_Unwind_GetGR(struct _Unwind_Context *context, int index) {
480 unw_cursor_t *cursor = (unw_cursor_t *)context;
481 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000482 __unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000483 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
484 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000485 return (uintptr_t)result;
486}
487
488/// Called by personality handler during phase 2 to alter register values.
489_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
490 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000491 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000492 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000493 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000494 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000495 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000496}
497
498/// Called by personality handler during phase 2 to get instruction pointer.
499_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
500 unw_cursor_t *cursor = (unw_cursor_t *)context;
501 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000502 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000503 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
504 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000505 return (uintptr_t)result;
506}
507
508/// Called by personality handler during phase 2 to alter instruction pointer,
509/// such as setting where the landing pad is, so _Unwind_Resume() will
510/// start executing in the landing pad.
511_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
512 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000513 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
514 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000515 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000516 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000517}
518
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000519#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)