blob: 68e5e48b8c05786213a638388e0eb0b0ba19705b [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
71 // When tracing, print state information.
72 if (_LIBUNWIND_TRACING_UNWINDING) {
73 char functionBuf[512];
74 const char *functionName = functionBuf;
75 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +000076 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
77 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +000078 (frameInfo.start_ip + offset > frameInfo.end_ip))
79 functionName = ".anonymous.";
80 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +000081 __unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +000083 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
84 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000085 (void *)exception_object, pc, frameInfo.start_ip, functionName,
86 frameInfo.lsda, frameInfo.handler);
87 }
88
89 // If there is a personality routine, ask it if it will want to stop at
90 // this frame.
91 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -080092 _Unwind_Personality_Fn p =
93 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000094 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +000095 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 (void *)exception_object, (void *)(uintptr_t)p);
97 _Unwind_Reason_Code personalityResult =
98 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000099 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000100 switch (personalityResult) {
101 case _URC_HANDLER_FOUND:
102 // found a catch clause or locals that need destructing in this frame
103 // stop search and remember stack pointer at the frame
Petr Hosek9bbfad52019-04-03 21:50:03 +0000104 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105 exception_object->private_2 = (uintptr_t)sp;
106 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000107 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000108 (void *)exception_object);
109 return _URC_NO_REASON;
110
111 case _URC_CONTINUE_UNWIND:
112 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000113 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000114 (void *)exception_object);
115 // continue unwinding
116 break;
117
118 default:
119 // something went wrong
120 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000121 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000122 (void *)exception_object);
123 return _URC_FATAL_PHASE1_ERROR;
124 }
125 }
126 }
127 return _URC_NO_REASON;
128}
129
130
131static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000132unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000133 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000134
Ed Maste41bc5a72016-08-30 15:38:10 +0000135 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000136 (void *)exception_object);
137
138 // Walk each frame until we reach where search phase said to stop.
139 while (true) {
140
Ed Maste6f723382016-08-30 13:08:21 +0000141 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000142 // _Unwind_RaiseException).
Petr Hosek9bbfad52019-04-03 21:50:03 +0000143 int stepResult = __unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144 if (stepResult == 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000145 _LIBUNWIND_TRACE_UNWINDING(
146 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
147 "bottom => _URC_END_OF_STACK",
148 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 return _URC_END_OF_STACK;
150 } else if (stepResult < 0) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000151 _LIBUNWIND_TRACE_UNWINDING(
152 "unwind_phase2(ex_ojb=%p): __unw_step failed => "
153 "_URC_FATAL_PHASE1_ERROR",
154 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000155 return _URC_FATAL_PHASE2_ERROR;
156 }
157
158 // Get info about this frame.
159 unw_word_t sp;
160 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000161 __unw_get_reg(cursor, UNW_REG_SP, &sp);
162 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
163 _LIBUNWIND_TRACE_UNWINDING(
164 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
165 "failed => _URC_FATAL_PHASE1_ERROR",
166 (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000167 return _URC_FATAL_PHASE2_ERROR;
168 }
169
170 // When tracing, print state information.
171 if (_LIBUNWIND_TRACING_UNWINDING) {
172 char functionBuf[512];
173 const char *functionName = functionBuf;
174 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000175 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
176 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000177 (frameInfo.start_ip + offset > frameInfo.end_ip))
178 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000179 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
180 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
181 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000182 (void *)exception_object, frameInfo.start_ip,
183 functionName, sp, frameInfo.lsda,
184 frameInfo.handler);
185 }
186
187 // If there is a personality routine, tell it we are unwinding.
188 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800189 _Unwind_Personality_Fn p =
190 (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000191 _Unwind_Action action = _UA_CLEANUP_PHASE;
192 if (sp == exception_object->private_2) {
193 // Tell personality this was the frame it marked in phase 1.
194 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
195 }
196 _Unwind_Reason_Code personalityResult =
197 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000198 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199 switch (personalityResult) {
200 case _URC_CONTINUE_UNWIND:
201 // Continue unwinding
202 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000203 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000204 (void *)exception_object);
205 if (sp == exception_object->private_2) {
206 // Phase 1 said we would stop at this frame, but we did not...
207 _LIBUNWIND_ABORT("during phase1 personality function said it would "
208 "stop here, but now in phase2 it did not stop here");
209 }
210 break;
211 case _URC_INSTALL_CONTEXT:
212 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000213 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000214 (void *)exception_object);
215 // Personality routine says to transfer control to landing pad.
216 // We may get control back if landing pad calls _Unwind_Resume().
217 if (_LIBUNWIND_TRACING_UNWINDING) {
218 unw_word_t pc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000219 __unw_get_reg(cursor, UNW_REG_IP, &pc);
220 __unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000221 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000222 "user code with ip=0x%" PRIxPTR
223 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000224 (void *)exception_object, pc, sp);
225 }
Petr Hosek9bbfad52019-04-03 21:50:03 +0000226 __unw_resume(cursor);
227 // __unw_resume() only returns if there was an error.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000228 return _URC_FATAL_PHASE2_ERROR;
229 default:
230 // Personality routine returned an unknown result code.
231 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
232 personalityResult);
233 return _URC_FATAL_PHASE2_ERROR;
234 }
235 }
236 }
237
238 // Clean up phase did not resume at the frame that the search phase
239 // said it would...
240 return _URC_FATAL_PHASE2_ERROR;
241}
242
243static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000244unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000245 _Unwind_Exception *exception_object,
246 _Unwind_Stop_Fn stop, void *stop_parameter) {
Petr Hosek9bbfad52019-04-03 21:50:03 +0000247 __unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000248
249 // Walk each frame until we reach where search phase said to stop
Petr Hosek9bbfad52019-04-03 21:50:03 +0000250 while (__unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000251
252 // Update info about this frame.
253 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000254 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
255 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000256 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000257 (void *)exception_object);
258 return _URC_FATAL_PHASE2_ERROR;
259 }
260
261 // When tracing, print state information.
262 if (_LIBUNWIND_TRACING_UNWINDING) {
263 char functionBuf[512];
264 const char *functionName = functionBuf;
265 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000266 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
267 &offset) != UNW_ESUCCESS) ||
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000268 (frameInfo.start_ip + offset > frameInfo.end_ip))
269 functionName = ".anonymous.";
270 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000271 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
272 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000273 (void *)exception_object, frameInfo.start_ip, functionName,
274 frameInfo.lsda, frameInfo.handler);
275 }
276
277 // Call stop function at each frame.
278 _Unwind_Action action =
279 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
280 _Unwind_Reason_Code stopResult =
281 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000282 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000283 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000284 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000285 (void *)exception_object, stopResult);
286 if (stopResult != _URC_NO_REASON) {
287 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000288 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000289 (void *)exception_object);
290 return _URC_FATAL_PHASE2_ERROR;
291 }
292
293 // If there is a personality routine, tell it we are unwinding.
294 if (frameInfo.handler != 0) {
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800295 _Unwind_Personality_Fn p =
296 (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000297 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000298 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000299 (void *)exception_object, (void *)(uintptr_t)p);
300 _Unwind_Reason_Code personalityResult =
301 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000302 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000303 switch (personalityResult) {
304 case _URC_CONTINUE_UNWIND:
305 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
306 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000307 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000308 (void *)exception_object);
309 // Destructors called, continue unwinding
310 break;
311 case _URC_INSTALL_CONTEXT:
312 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
313 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000314 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000315 (void *)exception_object);
316 // We may get control back if landing pad calls _Unwind_Resume().
Petr Hosek9bbfad52019-04-03 21:50:03 +0000317 __unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000318 break;
319 default:
320 // Personality routine returned an unknown result code.
321 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
322 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000323 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000324 (void *)exception_object, personalityResult);
325 return _URC_FATAL_PHASE2_ERROR;
326 }
327 }
328 }
329
330 // Call stop function one last time and tell it we've reached the end
331 // of the stack.
332 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000333 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000334 (void *)exception_object);
335 _Unwind_Action lastAction =
336 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
337 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000338 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000339
340 // Clean up phase did not resume at the frame that the search phase said it
341 // would.
342 return _URC_FATAL_PHASE2_ERROR;
343}
344
345
346/// Called by __cxa_throw. Only returns if there is a fatal error.
347_LIBUNWIND_EXPORT _Unwind_Reason_Code
348_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000349 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000350 (void *)exception_object);
351 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000352 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000353 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000354
355 // Mark that this is a non-forced unwind, so _Unwind_Resume()
356 // can do the right thing.
357 exception_object->private_1 = 0;
358 exception_object->private_2 = 0;
359
360 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000361 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000362 if (phase1 != _URC_NO_REASON)
363 return phase1;
364
365 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000366 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000367}
368
369
370
371/// When _Unwind_RaiseException() is in phase2, it hands control
372/// to the personality function at each frame. The personality
373/// may force a jump to a landing pad in that function, the landing
374/// pad code may then call _Unwind_Resume() to continue with the
375/// unwinding. Note: the call to _Unwind_Resume() is from compiler
376/// geneated user code. All other _Unwind_* routines are called
377/// by the C++ runtime __cxa_* routines.
378///
379/// Note: re-throwing an exception (as opposed to continuing the unwind)
380/// is implemented by having the code call __cxa_rethrow() which
381/// in turn calls _Unwind_Resume_or_Rethrow().
382_LIBUNWIND_EXPORT void
383_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000384 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000385 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000386 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000387 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000388
389 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000390 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000391 (_Unwind_Stop_Fn) exception_object->private_1,
392 (void *)exception_object->private_2);
393 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000394 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000395
396 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
397 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
398}
399
400
401
402/// Not used by C++.
403/// Unwinds stack, calling "stop" function at each frame.
404/// Could be used to implement longjmp().
405_LIBUNWIND_EXPORT _Unwind_Reason_Code
406_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
407 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000408 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000409 (void *)exception_object, (void *)(uintptr_t)stop);
410 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000411 unw_cursor_t cursor;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000412 __unw_getcontext(&uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000413
414 // Mark that this is a forced unwind, so _Unwind_Resume() can do
415 // the right thing.
416 exception_object->private_1 = (uintptr_t) stop;
417 exception_object->private_2 = (uintptr_t) stop_parameter;
418
419 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000420 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000421}
422
423
424/// Called by personality handler during phase 2 to get LSDA for current frame.
425_LIBUNWIND_EXPORT uintptr_t
426_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
427 unw_cursor_t *cursor = (unw_cursor_t *)context;
428 unw_proc_info_t frameInfo;
429 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000430 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000431 result = (uintptr_t)frameInfo.lsda;
432 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000433 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000434 (void *)context, result);
435 if (result != 0) {
436 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000437 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000438 result);
439 }
440 return result;
441}
442
443
444/// Called by personality handler during phase 2 to find the start of the
445/// function.
446_LIBUNWIND_EXPORT uintptr_t
447_Unwind_GetRegionStart(struct _Unwind_Context *context) {
448 unw_cursor_t *cursor = (unw_cursor_t *)context;
449 unw_proc_info_t frameInfo;
450 uintptr_t result = 0;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000451 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000452 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000453 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000454 (void *)context, result);
455 return result;
456}
457
Charles Davisfa2e6202018-08-30 21:29:00 +0000458#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000459
460/// Called by personality handler during phase 2 if a foreign exception
461// is caught.
462_LIBUNWIND_EXPORT void
463_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000464 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000465 (void *)exception_object);
466 if (exception_object->exception_cleanup != NULL)
467 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
468 exception_object);
469}
470
471/// Called by personality handler during phase 2 to get register values.
472_LIBUNWIND_EXPORT uintptr_t
473_Unwind_GetGR(struct _Unwind_Context *context, int index) {
474 unw_cursor_t *cursor = (unw_cursor_t *)context;
475 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000476 __unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000477 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
478 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000479 return (uintptr_t)result;
480}
481
482/// Called by personality handler during phase 2 to alter register values.
483_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
484 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000485 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000486 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000487 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000488 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000489 __unw_set_reg(cursor, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000490}
491
492/// Called by personality handler during phase 2 to get instruction pointer.
493_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
494 unw_cursor_t *cursor = (unw_cursor_t *)context;
495 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000496 __unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000497 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
498 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000499 return (uintptr_t)result;
500}
501
502/// Called by personality handler during phase 2 to alter instruction pointer,
503/// such as setting where the landing pad is, so _Unwind_Resume() will
504/// start executing in the landing pad.
505_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
506 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000507 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
508 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000509 unw_cursor_t *cursor = (unw_cursor_t *)context;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000510 __unw_set_reg(cursor, UNW_REG_IP, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000511}
512
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000513#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)