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