1*0faf1914Srobert //===----------------------------------------------------------------------===//
2f6c50668Spatrick //
3f6c50668Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f6c50668Spatrick // See https://llvm.org/LICENSE.txt for license information.
5f6c50668Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f6c50668Spatrick //
7f6c50668Spatrick //===----------------------------------------------------------------------===//
8f6c50668Spatrick //
9f6c50668Spatrick // Implements SEH-based Itanium C++ exceptions.
10f6c50668Spatrick //
11f6c50668Spatrick //===----------------------------------------------------------------------===//
12f6c50668Spatrick
13f6c50668Spatrick #include "config.h"
14f6c50668Spatrick
15f6c50668Spatrick #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
16f6c50668Spatrick
17f6c50668Spatrick #include <unwind.h>
18f6c50668Spatrick
19f6c50668Spatrick #include <stdint.h>
20f6c50668Spatrick #include <stdbool.h>
21f6c50668Spatrick #include <stdlib.h>
22f6c50668Spatrick
23f6c50668Spatrick #include <windef.h>
24f6c50668Spatrick #include <excpt.h>
25f6c50668Spatrick #include <winnt.h>
26f6c50668Spatrick #include <ntstatus.h>
27f6c50668Spatrick
28f6c50668Spatrick #include "libunwind_ext.h"
29f6c50668Spatrick #include "UnwindCursor.hpp"
30f6c50668Spatrick
31f6c50668Spatrick using namespace libunwind;
32f6c50668Spatrick
33f6c50668Spatrick #define STATUS_USER_DEFINED (1u << 29)
34f6c50668Spatrick
35f6c50668Spatrick #define STATUS_GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
36f6c50668Spatrick
37f6c50668Spatrick #define MAKE_CUSTOM_STATUS(s, c) \
38f6c50668Spatrick ((NTSTATUS)(((s) << 30) | STATUS_USER_DEFINED | (c)))
39f6c50668Spatrick #define MAKE_GCC_EXCEPTION(c) \
40f6c50668Spatrick MAKE_CUSTOM_STATUS(STATUS_SEVERITY_SUCCESS, STATUS_GCC_MAGIC | ((c) << 24))
41f6c50668Spatrick
42f6c50668Spatrick /// SEH exception raised by libunwind when the program calls
43f6c50668Spatrick /// \c _Unwind_RaiseException.
44f6c50668Spatrick #define STATUS_GCC_THROW MAKE_GCC_EXCEPTION(0) // 0x20474343
45f6c50668Spatrick /// SEH exception raised by libunwind to initiate phase 2 of exception
46f6c50668Spatrick /// handling.
47f6c50668Spatrick #define STATUS_GCC_UNWIND MAKE_GCC_EXCEPTION(1) // 0x21474343
48f6c50668Spatrick
49f6c50668Spatrick static int __unw_init_seh(unw_cursor_t *cursor, CONTEXT *ctx);
50f6c50668Spatrick static DISPATCHER_CONTEXT *__unw_seh_get_disp_ctx(unw_cursor_t *cursor);
51f6c50668Spatrick static void __unw_seh_set_disp_ctx(unw_cursor_t *cursor,
52f6c50668Spatrick DISPATCHER_CONTEXT *disp);
53f6c50668Spatrick
54f6c50668Spatrick /// Common implementation of SEH-style handler functions used by Itanium-
55f6c50668Spatrick /// style frames. Depending on how and why it was called, it may do one of:
56f6c50668Spatrick /// a) Delegate to the given Itanium-style personality function; or
57f6c50668Spatrick /// b) Initiate a collided unwind to halt unwinding.
58f6c50668Spatrick _LIBUNWIND_EXPORT EXCEPTION_DISPOSITION
_GCC_specific_handler(PEXCEPTION_RECORD ms_exc,PVOID frame,PCONTEXT ms_ctx,DISPATCHER_CONTEXT * disp,_Unwind_Personality_Fn pers)59f6c50668Spatrick _GCC_specific_handler(PEXCEPTION_RECORD ms_exc, PVOID frame, PCONTEXT ms_ctx,
60f6c50668Spatrick DISPATCHER_CONTEXT *disp, _Unwind_Personality_Fn pers) {
61f6c50668Spatrick unw_cursor_t cursor;
62f6c50668Spatrick _Unwind_Exception *exc;
63f6c50668Spatrick _Unwind_Action action;
64f6c50668Spatrick struct _Unwind_Context *ctx = nullptr;
65f6c50668Spatrick _Unwind_Reason_Code urc;
66f6c50668Spatrick uintptr_t retval, target;
67f6c50668Spatrick bool ours = false;
68f6c50668Spatrick
69f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler(%#010lx(%lx), %p)",
70f6c50668Spatrick ms_exc->ExceptionCode, ms_exc->ExceptionFlags,
71f6c50668Spatrick (void *)frame);
72f6c50668Spatrick if (ms_exc->ExceptionCode == STATUS_GCC_UNWIND) {
73f6c50668Spatrick if (IS_TARGET_UNWIND(ms_exc->ExceptionFlags)) {
74f6c50668Spatrick // Set up the upper return value (the lower one and the target PC
75f6c50668Spatrick // were set in the call to RtlUnwindEx()) for the landing pad.
76f6c50668Spatrick #ifdef __x86_64__
77f6c50668Spatrick disp->ContextRecord->Rdx = ms_exc->ExceptionInformation[3];
78f6c50668Spatrick #elif defined(__arm__)
79f6c50668Spatrick disp->ContextRecord->R1 = ms_exc->ExceptionInformation[3];
80f6c50668Spatrick #elif defined(__aarch64__)
81f6c50668Spatrick disp->ContextRecord->X1 = ms_exc->ExceptionInformation[3];
82f6c50668Spatrick #endif
83f6c50668Spatrick }
84f6c50668Spatrick // This is the collided unwind to the landing pad. Nothing to do.
85f6c50668Spatrick return ExceptionContinueSearch;
86f6c50668Spatrick }
87f6c50668Spatrick
88f6c50668Spatrick if (ms_exc->ExceptionCode == STATUS_GCC_THROW) {
89f6c50668Spatrick // This is (probably) a libunwind-controlled exception/unwind. Recover the
90f6c50668Spatrick // parameters which we set below, and pass them to the personality function.
91f6c50668Spatrick ours = true;
92f6c50668Spatrick exc = (_Unwind_Exception *)ms_exc->ExceptionInformation[0];
93f6c50668Spatrick if (!IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1) {
94f6c50668Spatrick ctx = (struct _Unwind_Context *)ms_exc->ExceptionInformation[1];
95f6c50668Spatrick action = (_Unwind_Action)ms_exc->ExceptionInformation[2];
96f6c50668Spatrick }
97f6c50668Spatrick } else {
98f6c50668Spatrick // Foreign exception.
99b3056a3bSpatrick // We can't interact with them (we don't know the original target frame
100b3056a3bSpatrick // that we should pass on to RtlUnwindEx in _Unwind_Resume), so just
101b3056a3bSpatrick // pass without calling our destructors here.
102b3056a3bSpatrick return ExceptionContinueSearch;
103f6c50668Spatrick }
104f6c50668Spatrick if (!ctx) {
105f6c50668Spatrick __unw_init_seh(&cursor, disp->ContextRecord);
106f6c50668Spatrick __unw_seh_set_disp_ctx(&cursor, disp);
107*0faf1914Srobert __unw_set_reg(&cursor, UNW_REG_IP, disp->ControlPc);
108f6c50668Spatrick ctx = (struct _Unwind_Context *)&cursor;
109f6c50668Spatrick
110f6c50668Spatrick if (!IS_UNWINDING(ms_exc->ExceptionFlags)) {
111f6c50668Spatrick if (ours && ms_exc->NumberParameters > 1)
112f6c50668Spatrick action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND);
113f6c50668Spatrick else
114f6c50668Spatrick action = _UA_SEARCH_PHASE;
115f6c50668Spatrick } else {
116f6c50668Spatrick if (ours && ms_exc->ExceptionInformation[1] == (ULONG_PTR)frame)
117f6c50668Spatrick action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
118f6c50668Spatrick else
119f6c50668Spatrick action = _UA_CLEANUP_PHASE;
120f6c50668Spatrick }
121f6c50668Spatrick }
122f6c50668Spatrick
123f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() calling personality "
124f6c50668Spatrick "function %p(1, %d, %llx, %p, %p)",
125f6c50668Spatrick (void *)pers, action, exc->exception_class,
126f6c50668Spatrick (void *)exc, (void *)ctx);
127f6c50668Spatrick urc = pers(1, action, exc->exception_class, exc, ctx);
128f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() personality returned %d", urc);
129f6c50668Spatrick switch (urc) {
130f6c50668Spatrick case _URC_CONTINUE_UNWIND:
131f6c50668Spatrick // If we're in phase 2, and the personality routine said to continue
132f6c50668Spatrick // at the target frame, we're in real trouble.
133f6c50668Spatrick if (action & _UA_HANDLER_FRAME)
134f6c50668Spatrick _LIBUNWIND_ABORT("Personality continued unwind at the target frame!");
135f6c50668Spatrick return ExceptionContinueSearch;
136f6c50668Spatrick case _URC_HANDLER_FOUND:
137f6c50668Spatrick // If we were called by __libunwind_seh_personality(), indicate that
138f6c50668Spatrick // a handler was found; otherwise, initiate phase 2 by unwinding.
139f6c50668Spatrick if (ours && ms_exc->NumberParameters > 1)
140*0faf1914Srobert return 4 /* ExceptionExecuteHandler in mingw */;
141f6c50668Spatrick // This should never happen in phase 2.
142f6c50668Spatrick if (IS_UNWINDING(ms_exc->ExceptionFlags))
143f6c50668Spatrick _LIBUNWIND_ABORT("Personality indicated exception handler in phase 2!");
144f6c50668Spatrick exc->private_[1] = (ULONG_PTR)frame;
145f6c50668Spatrick if (ours) {
146f6c50668Spatrick ms_exc->NumberParameters = 4;
147f6c50668Spatrick ms_exc->ExceptionInformation[1] = (ULONG_PTR)frame;
148f6c50668Spatrick }
149f6c50668Spatrick // FIXME: Indicate target frame in foreign case!
150f6c50668Spatrick // phase 2: the clean up phase
151f6c50668Spatrick RtlUnwindEx(frame, (PVOID)disp->ControlPc, ms_exc, exc, ms_ctx, disp->HistoryTable);
152f6c50668Spatrick _LIBUNWIND_ABORT("RtlUnwindEx() failed");
153f6c50668Spatrick case _URC_INSTALL_CONTEXT: {
154f6c50668Spatrick // If we were called by __libunwind_seh_personality(), indicate that
155f6c50668Spatrick // a handler was found; otherwise, it's time to initiate a collided
156f6c50668Spatrick // unwind to the target.
157f6c50668Spatrick if (ours && !IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1)
158*0faf1914Srobert return 4 /* ExceptionExecuteHandler in mingw */;
159f6c50668Spatrick // This should never happen in phase 1.
160f6c50668Spatrick if (!IS_UNWINDING(ms_exc->ExceptionFlags))
161f6c50668Spatrick _LIBUNWIND_ABORT("Personality installed context during phase 1!");
162f6c50668Spatrick #ifdef __x86_64__
163f6c50668Spatrick exc->private_[2] = disp->TargetIp;
164f6c50668Spatrick __unw_get_reg(&cursor, UNW_X86_64_RAX, &retval);
165f6c50668Spatrick __unw_get_reg(&cursor, UNW_X86_64_RDX, &exc->private_[3]);
166f6c50668Spatrick #elif defined(__arm__)
167f6c50668Spatrick exc->private_[2] = disp->TargetPc;
168f6c50668Spatrick __unw_get_reg(&cursor, UNW_ARM_R0, &retval);
169f6c50668Spatrick __unw_get_reg(&cursor, UNW_ARM_R1, &exc->private_[3]);
170f6c50668Spatrick #elif defined(__aarch64__)
171f6c50668Spatrick exc->private_[2] = disp->TargetPc;
172*0faf1914Srobert __unw_get_reg(&cursor, UNW_AARCH64_X0, &retval);
173*0faf1914Srobert __unw_get_reg(&cursor, UNW_AARCH64_X1, &exc->private_[3]);
174f6c50668Spatrick #endif
175f6c50668Spatrick __unw_get_reg(&cursor, UNW_REG_IP, &target);
176f6c50668Spatrick ms_exc->ExceptionCode = STATUS_GCC_UNWIND;
177f6c50668Spatrick #ifdef __x86_64__
178f6c50668Spatrick ms_exc->ExceptionInformation[2] = disp->TargetIp;
179f6c50668Spatrick #elif defined(__arm__) || defined(__aarch64__)
180f6c50668Spatrick ms_exc->ExceptionInformation[2] = disp->TargetPc;
181f6c50668Spatrick #endif
182f6c50668Spatrick ms_exc->ExceptionInformation[3] = exc->private_[3];
183f6c50668Spatrick // Give NTRTL some scratch space to keep track of the collided unwind.
184f6c50668Spatrick // Don't use the one that was passed in; we don't want to overwrite the
185f6c50668Spatrick // context in the DISPATCHER_CONTEXT.
186f6c50668Spatrick CONTEXT new_ctx;
187f6c50668Spatrick RtlUnwindEx(frame, (PVOID)target, ms_exc, (PVOID)retval, &new_ctx, disp->HistoryTable);
188f6c50668Spatrick _LIBUNWIND_ABORT("RtlUnwindEx() failed");
189f6c50668Spatrick }
190f6c50668Spatrick // Anything else indicates a serious problem.
191f6c50668Spatrick default: return ExceptionContinueExecution;
192f6c50668Spatrick }
193f6c50668Spatrick }
194f6c50668Spatrick
195f6c50668Spatrick /// Personality function returned by \c __unw_get_proc_info() in SEH contexts.
196f6c50668Spatrick /// This is a wrapper that calls the real SEH handler function, which in
197f6c50668Spatrick /// turn (at least, for Itanium-style frames) calls the real Itanium
198f6c50668Spatrick /// personality function (see \c _GCC_specific_handler()).
199f6c50668Spatrick extern "C" _Unwind_Reason_Code
__libunwind_seh_personality(int version,_Unwind_Action state,uint64_t klass,_Unwind_Exception * exc,struct _Unwind_Context * context)200f6c50668Spatrick __libunwind_seh_personality(int version, _Unwind_Action state,
201f6c50668Spatrick uint64_t klass, _Unwind_Exception *exc,
202f6c50668Spatrick struct _Unwind_Context *context) {
203f6c50668Spatrick (void)version;
204f6c50668Spatrick (void)klass;
205f6c50668Spatrick EXCEPTION_RECORD ms_exc;
206f6c50668Spatrick bool phase2 = (state & (_UA_SEARCH_PHASE|_UA_CLEANUP_PHASE)) == _UA_CLEANUP_PHASE;
207f6c50668Spatrick ms_exc.ExceptionCode = STATUS_GCC_THROW;
208f6c50668Spatrick ms_exc.ExceptionFlags = 0;
209f6c50668Spatrick ms_exc.NumberParameters = 3;
210f6c50668Spatrick ms_exc.ExceptionInformation[0] = (ULONG_PTR)exc;
211f6c50668Spatrick ms_exc.ExceptionInformation[1] = (ULONG_PTR)context;
212f6c50668Spatrick ms_exc.ExceptionInformation[2] = state;
213f6c50668Spatrick DISPATCHER_CONTEXT *disp_ctx =
214f6c50668Spatrick __unw_seh_get_disp_ctx((unw_cursor_t *)context);
215f6c50668Spatrick EXCEPTION_DISPOSITION ms_act = disp_ctx->LanguageHandler(&ms_exc,
216f6c50668Spatrick (PVOID)disp_ctx->EstablisherFrame,
217f6c50668Spatrick disp_ctx->ContextRecord,
218f6c50668Spatrick disp_ctx);
219f6c50668Spatrick switch (ms_act) {
220f6c50668Spatrick case ExceptionContinueSearch: return _URC_CONTINUE_UNWIND;
221f6c50668Spatrick case 4 /*ExceptionExecuteHandler*/:
222f6c50668Spatrick return phase2 ? _URC_INSTALL_CONTEXT : _URC_HANDLER_FOUND;
223f6c50668Spatrick default:
224f6c50668Spatrick return phase2 ? _URC_FATAL_PHASE2_ERROR : _URC_FATAL_PHASE1_ERROR;
225f6c50668Spatrick }
226f6c50668Spatrick }
227f6c50668Spatrick
228f6c50668Spatrick static _Unwind_Reason_Code
unwind_phase2_forced(unw_context_t * uc,_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)229f6c50668Spatrick unwind_phase2_forced(unw_context_t *uc,
230f6c50668Spatrick _Unwind_Exception *exception_object,
231f6c50668Spatrick _Unwind_Stop_Fn stop, void *stop_parameter) {
232f6c50668Spatrick unw_cursor_t cursor2;
233f6c50668Spatrick __unw_init_local(&cursor2, uc);
234f6c50668Spatrick
235f6c50668Spatrick // Walk each frame until we reach where search phase said to stop
236f6c50668Spatrick while (__unw_step(&cursor2) > 0) {
237f6c50668Spatrick
238f6c50668Spatrick // Update info about this frame.
239f6c50668Spatrick unw_proc_info_t frameInfo;
240f6c50668Spatrick if (__unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) {
241f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
242f6c50668Spatrick "failed => _URC_END_OF_STACK",
243f6c50668Spatrick (void *)exception_object);
244f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
245f6c50668Spatrick }
246f6c50668Spatrick
247*0faf1914Srobert #ifndef NDEBUG
248f6c50668Spatrick // When tracing, print state information.
249f6c50668Spatrick if (_LIBUNWIND_TRACING_UNWINDING) {
250f6c50668Spatrick char functionBuf[512];
251f6c50668Spatrick const char *functionName = functionBuf;
252f6c50668Spatrick unw_word_t offset;
253f6c50668Spatrick if ((__unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf),
254f6c50668Spatrick &offset) != UNW_ESUCCESS) ||
255f6c50668Spatrick (frameInfo.start_ip + offset > frameInfo.end_ip))
256f6c50668Spatrick functionName = ".anonymous.";
257f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
258*0faf1914Srobert "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
259*0faf1914Srobert ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
260f6c50668Spatrick (void *)exception_object, frameInfo.start_ip, functionName,
261f6c50668Spatrick frameInfo.lsda, frameInfo.handler);
262f6c50668Spatrick }
263*0faf1914Srobert #endif
264f6c50668Spatrick
265f6c50668Spatrick // Call stop function at each frame.
266f6c50668Spatrick _Unwind_Action action =
267f6c50668Spatrick (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
268f6c50668Spatrick _Unwind_Reason_Code stopResult =
269f6c50668Spatrick (*stop)(1, action, exception_object->exception_class, exception_object,
270f6c50668Spatrick (struct _Unwind_Context *)(&cursor2), stop_parameter);
271f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
272f6c50668Spatrick "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
273f6c50668Spatrick (void *)exception_object, stopResult);
274f6c50668Spatrick if (stopResult != _URC_NO_REASON) {
275f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
276f6c50668Spatrick "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
277f6c50668Spatrick (void *)exception_object);
278f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
279f6c50668Spatrick }
280f6c50668Spatrick
281f6c50668Spatrick // If there is a personality routine, tell it we are unwinding.
282f6c50668Spatrick if (frameInfo.handler != 0) {
283f6c50668Spatrick _Unwind_Personality_Fn p =
284f6c50668Spatrick (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
285f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
286f6c50668Spatrick "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
287f6c50668Spatrick (void *)exception_object, (void *)(uintptr_t)p);
288f6c50668Spatrick _Unwind_Reason_Code personalityResult =
289f6c50668Spatrick (*p)(1, action, exception_object->exception_class, exception_object,
290f6c50668Spatrick (struct _Unwind_Context *)(&cursor2));
291f6c50668Spatrick switch (personalityResult) {
292f6c50668Spatrick case _URC_CONTINUE_UNWIND:
293f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
294f6c50668Spatrick "personality returned "
295f6c50668Spatrick "_URC_CONTINUE_UNWIND",
296f6c50668Spatrick (void *)exception_object);
297f6c50668Spatrick // Destructors called, continue unwinding
298f6c50668Spatrick break;
299f6c50668Spatrick case _URC_INSTALL_CONTEXT:
300f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
301f6c50668Spatrick "personality returned "
302f6c50668Spatrick "_URC_INSTALL_CONTEXT",
303f6c50668Spatrick (void *)exception_object);
304f6c50668Spatrick // We may get control back if landing pad calls _Unwind_Resume().
305f6c50668Spatrick __unw_resume(&cursor2);
306f6c50668Spatrick break;
307f6c50668Spatrick default:
308f6c50668Spatrick // Personality routine returned an unknown result code.
309f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
310f6c50668Spatrick "personality returned %d, "
311f6c50668Spatrick "_URC_FATAL_PHASE2_ERROR",
312f6c50668Spatrick (void *)exception_object, personalityResult);
313f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
314f6c50668Spatrick }
315f6c50668Spatrick }
316f6c50668Spatrick }
317f6c50668Spatrick
318f6c50668Spatrick // Call stop function one last time and tell it we've reached the end
319f6c50668Spatrick // of the stack.
320f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
321f6c50668Spatrick "function with _UA_END_OF_STACK",
322f6c50668Spatrick (void *)exception_object);
323f6c50668Spatrick _Unwind_Action lastAction =
324f6c50668Spatrick (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
325f6c50668Spatrick (*stop)(1, lastAction, exception_object->exception_class, exception_object,
326f6c50668Spatrick (struct _Unwind_Context *)(&cursor2), stop_parameter);
327f6c50668Spatrick
328f6c50668Spatrick // Clean up phase did not resume at the frame that the search phase said it
329f6c50668Spatrick // would.
330f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
331f6c50668Spatrick }
332f6c50668Spatrick
333f6c50668Spatrick /// Called by \c __cxa_throw(). Only returns if there is a fatal error.
334f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception * exception_object)335f6c50668Spatrick _Unwind_RaiseException(_Unwind_Exception *exception_object) {
336f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
337f6c50668Spatrick (void *)exception_object);
338f6c50668Spatrick
339f6c50668Spatrick // Mark that this is a non-forced unwind, so _Unwind_Resume()
340f6c50668Spatrick // can do the right thing.
341f6c50668Spatrick memset(exception_object->private_, 0, sizeof(exception_object->private_));
342f6c50668Spatrick
343f6c50668Spatrick // phase 1: the search phase
344f6c50668Spatrick // We'll let the system do that for us.
345f6c50668Spatrick RaiseException(STATUS_GCC_THROW, 0, 1, (ULONG_PTR *)&exception_object);
346f6c50668Spatrick
347f6c50668Spatrick // If we get here, either something went horribly wrong or we reached the
348f6c50668Spatrick // top of the stack. Either way, let libc++abi call std::terminate().
349f6c50668Spatrick return _URC_END_OF_STACK;
350f6c50668Spatrick }
351f6c50668Spatrick
352f6c50668Spatrick /// When \c _Unwind_RaiseException() is in phase2, it hands control
353f6c50668Spatrick /// to the personality function at each frame. The personality
354f6c50668Spatrick /// may force a jump to a landing pad in that function; the landing
355f6c50668Spatrick /// pad code may then call \c _Unwind_Resume() to continue with the
356f6c50668Spatrick /// unwinding. Note: the call to \c _Unwind_Resume() is from compiler
357*0faf1914Srobert /// generated user code. All other \c _Unwind_* routines are called
358f6c50668Spatrick /// by the C++ runtime \c __cxa_* routines.
359f6c50668Spatrick ///
360f6c50668Spatrick /// Note: re-throwing an exception (as opposed to continuing the unwind)
361f6c50668Spatrick /// is implemented by having the code call \c __cxa_rethrow() which
362f6c50668Spatrick /// in turn calls \c _Unwind_Resume_or_Rethrow().
363f6c50668Spatrick _LIBUNWIND_EXPORT void
_Unwind_Resume(_Unwind_Exception * exception_object)364f6c50668Spatrick _Unwind_Resume(_Unwind_Exception *exception_object) {
365f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
366f6c50668Spatrick
367f6c50668Spatrick if (exception_object->private_[0] != 0) {
368f6c50668Spatrick unw_context_t uc;
369f6c50668Spatrick
370f6c50668Spatrick __unw_getcontext(&uc);
371f6c50668Spatrick unwind_phase2_forced(&uc, exception_object,
372f6c50668Spatrick (_Unwind_Stop_Fn) exception_object->private_[0],
373f6c50668Spatrick (void *)exception_object->private_[4]);
374f6c50668Spatrick } else {
375f6c50668Spatrick // Recover the parameters for the unwind from the exception object
376f6c50668Spatrick // so we can start unwinding again.
377f6c50668Spatrick EXCEPTION_RECORD ms_exc;
378f6c50668Spatrick CONTEXT ms_ctx;
379f6c50668Spatrick UNWIND_HISTORY_TABLE hist;
380f6c50668Spatrick
381f6c50668Spatrick memset(&ms_exc, 0, sizeof(ms_exc));
382f6c50668Spatrick memset(&hist, 0, sizeof(hist));
383f6c50668Spatrick ms_exc.ExceptionCode = STATUS_GCC_THROW;
384f6c50668Spatrick ms_exc.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
385f6c50668Spatrick ms_exc.NumberParameters = 4;
386f6c50668Spatrick ms_exc.ExceptionInformation[0] = (ULONG_PTR)exception_object;
387f6c50668Spatrick ms_exc.ExceptionInformation[1] = exception_object->private_[1];
388f6c50668Spatrick ms_exc.ExceptionInformation[2] = exception_object->private_[2];
389f6c50668Spatrick ms_exc.ExceptionInformation[3] = exception_object->private_[3];
390f6c50668Spatrick RtlUnwindEx((PVOID)exception_object->private_[1],
391f6c50668Spatrick (PVOID)exception_object->private_[2], &ms_exc,
392f6c50668Spatrick exception_object, &ms_ctx, &hist);
393f6c50668Spatrick }
394f6c50668Spatrick
395f6c50668Spatrick // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
396f6c50668Spatrick _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
397f6c50668Spatrick }
398f6c50668Spatrick
399f6c50668Spatrick /// Not used by C++.
400f6c50668Spatrick /// Unwinds stack, calling "stop" function at each frame.
401f6c50668Spatrick /// Could be used to implement \c longjmp().
402f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_ForcedUnwind(_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)403f6c50668Spatrick _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
404f6c50668Spatrick _Unwind_Stop_Fn stop, void *stop_parameter) {
405f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
406f6c50668Spatrick (void *)exception_object, (void *)(uintptr_t)stop);
407f6c50668Spatrick unw_context_t uc;
408f6c50668Spatrick __unw_getcontext(&uc);
409f6c50668Spatrick
410f6c50668Spatrick // Mark that this is a forced unwind, so _Unwind_Resume() can do
411f6c50668Spatrick // the right thing.
412f6c50668Spatrick exception_object->private_[0] = (uintptr_t) stop;
413f6c50668Spatrick exception_object->private_[4] = (uintptr_t) stop_parameter;
414f6c50668Spatrick
415f6c50668Spatrick // do it
416f6c50668Spatrick return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter);
417f6c50668Spatrick }
418f6c50668Spatrick
419f6c50668Spatrick /// Called by personality handler during phase 2 to get LSDA for current frame.
420f6c50668Spatrick _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)421f6c50668Spatrick _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
422f6c50668Spatrick uintptr_t result =
423f6c50668Spatrick (uintptr_t)__unw_seh_get_disp_ctx((unw_cursor_t *)context)->HandlerData;
424f6c50668Spatrick _LIBUNWIND_TRACE_API(
425f6c50668Spatrick "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
426f6c50668Spatrick (void *)context, result);
427f6c50668Spatrick return result;
428f6c50668Spatrick }
429f6c50668Spatrick
430f6c50668Spatrick /// Called by personality handler during phase 2 to find the start of the
431f6c50668Spatrick /// function.
432f6c50668Spatrick _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetRegionStart(struct _Unwind_Context * context)433f6c50668Spatrick _Unwind_GetRegionStart(struct _Unwind_Context *context) {
434f6c50668Spatrick DISPATCHER_CONTEXT *disp = __unw_seh_get_disp_ctx((unw_cursor_t *)context);
435f6c50668Spatrick uintptr_t result = (uintptr_t)disp->FunctionEntry->BeginAddress + disp->ImageBase;
436f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
437f6c50668Spatrick (void *)context, result);
438f6c50668Spatrick return result;
439f6c50668Spatrick }
440f6c50668Spatrick
__unw_init_seh(unw_cursor_t * cursor,CONTEXT * context)441f6c50668Spatrick static int __unw_init_seh(unw_cursor_t *cursor, CONTEXT *context) {
442f6c50668Spatrick #ifdef _LIBUNWIND_TARGET_X86_64
443f6c50668Spatrick new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor))
444f6c50668Spatrick UnwindCursor<LocalAddressSpace, Registers_x86_64>(
445f6c50668Spatrick context, LocalAddressSpace::sThisAddressSpace);
446f6c50668Spatrick auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
447f6c50668Spatrick co->setInfoBasedOnIPRegister();
448f6c50668Spatrick return UNW_ESUCCESS;
449f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_ARM)
450f6c50668Spatrick new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor))
451f6c50668Spatrick UnwindCursor<LocalAddressSpace, Registers_arm>(
452f6c50668Spatrick context, LocalAddressSpace::sThisAddressSpace);
453f6c50668Spatrick auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
454f6c50668Spatrick co->setInfoBasedOnIPRegister();
455f6c50668Spatrick return UNW_ESUCCESS;
456f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_AARCH64)
457f6c50668Spatrick new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor))
458f6c50668Spatrick UnwindCursor<LocalAddressSpace, Registers_arm64>(
459f6c50668Spatrick context, LocalAddressSpace::sThisAddressSpace);
460f6c50668Spatrick auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
461f6c50668Spatrick co->setInfoBasedOnIPRegister();
462f6c50668Spatrick return UNW_ESUCCESS;
463f6c50668Spatrick #else
464f6c50668Spatrick return UNW_EINVAL;
465f6c50668Spatrick #endif
466f6c50668Spatrick }
467f6c50668Spatrick
__unw_seh_get_disp_ctx(unw_cursor_t * cursor)468f6c50668Spatrick static DISPATCHER_CONTEXT *__unw_seh_get_disp_ctx(unw_cursor_t *cursor) {
469f6c50668Spatrick #ifdef _LIBUNWIND_TARGET_X86_64
470f6c50668Spatrick return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->getDispatcherContext();
471f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_ARM)
472f6c50668Spatrick return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->getDispatcherContext();
473f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_AARCH64)
474f6c50668Spatrick return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->getDispatcherContext();
475f6c50668Spatrick #else
476f6c50668Spatrick return nullptr;
477f6c50668Spatrick #endif
478f6c50668Spatrick }
479f6c50668Spatrick
__unw_seh_set_disp_ctx(unw_cursor_t * cursor,DISPATCHER_CONTEXT * disp)480f6c50668Spatrick static void __unw_seh_set_disp_ctx(unw_cursor_t *cursor,
481f6c50668Spatrick DISPATCHER_CONTEXT *disp) {
482f6c50668Spatrick #ifdef _LIBUNWIND_TARGET_X86_64
483f6c50668Spatrick reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->setDispatcherContext(disp);
484f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_ARM)
485f6c50668Spatrick reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->setDispatcherContext(disp);
486f6c50668Spatrick #elif defined(_LIBUNWIND_TARGET_AARCH64)
487f6c50668Spatrick reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->setDispatcherContext(disp);
488f6c50668Spatrick #endif
489f6c50668Spatrick }
490f6c50668Spatrick
491f6c50668Spatrick #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
492