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