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