1 //===--------------------------- Unwind-seh.cpp ---------------------------===//
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 - 1);
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 /* ExecptionExecuteHandler 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 /* ExecptionExecuteHandler 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_ARM64_X0, &retval);
173     __unw_get_reg(&cursor, UNW_ARM64_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   EXCEPTION_DISPOSITION ms_act = disp_ctx->LanguageHandler(&ms_exc,
216                                                            (PVOID)disp_ctx->EstablisherFrame,
217                                                            disp_ctx->ContextRecord,
218                                                            disp_ctx);
219   switch (ms_act) {
220   case ExceptionContinueSearch: return _URC_CONTINUE_UNWIND;
221   case 4 /*ExceptionExecuteHandler*/:
222     return phase2 ? _URC_INSTALL_CONTEXT : _URC_HANDLER_FOUND;
223   default:
224     return phase2 ? _URC_FATAL_PHASE2_ERROR : _URC_FATAL_PHASE1_ERROR;
225   }
226 }
227 
228 static _Unwind_Reason_Code
unwind_phase2_forced(unw_context_t * uc,_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)229 unwind_phase2_forced(unw_context_t *uc,
230                      _Unwind_Exception *exception_object,
231                      _Unwind_Stop_Fn stop, void *stop_parameter) {
232   unw_cursor_t cursor2;
233   __unw_init_local(&cursor2, uc);
234 
235   // Walk each frame until we reach where search phase said to stop
236   while (__unw_step(&cursor2) > 0) {
237 
238     // Update info about this frame.
239     unw_proc_info_t frameInfo;
240     if (__unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) {
241       _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
242                                  "failed => _URC_END_OF_STACK",
243                                  (void *)exception_object);
244       return _URC_FATAL_PHASE2_ERROR;
245     }
246 
247     // When tracing, print state information.
248     if (_LIBUNWIND_TRACING_UNWINDING) {
249       char functionBuf[512];
250       const char *functionName = functionBuf;
251       unw_word_t offset;
252       if ((__unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf),
253                                &offset) != UNW_ESUCCESS) ||
254           (frameInfo.start_ip + offset > frameInfo.end_ip))
255         functionName = ".anonymous.";
256       _LIBUNWIND_TRACE_UNWINDING(
257           "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIx64
258           ", func=%s, lsda=0x%" PRIx64 ", personality=0x%" PRIx64,
259           (void *)exception_object, frameInfo.start_ip, functionName,
260           frameInfo.lsda, frameInfo.handler);
261     }
262 
263     // Call stop function at each frame.
264     _Unwind_Action action =
265         (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
266     _Unwind_Reason_Code stopResult =
267         (*stop)(1, action, exception_object->exception_class, exception_object,
268                 (struct _Unwind_Context *)(&cursor2), stop_parameter);
269     _LIBUNWIND_TRACE_UNWINDING(
270         "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
271         (void *)exception_object, stopResult);
272     if (stopResult != _URC_NO_REASON) {
273       _LIBUNWIND_TRACE_UNWINDING(
274           "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
275           (void *)exception_object);
276       return _URC_FATAL_PHASE2_ERROR;
277     }
278 
279     // If there is a personality routine, tell it we are unwinding.
280     if (frameInfo.handler != 0) {
281       _Unwind_Personality_Fn p =
282           (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
283       _LIBUNWIND_TRACE_UNWINDING(
284           "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
285           (void *)exception_object, (void *)(uintptr_t)p);
286       _Unwind_Reason_Code personalityResult =
287           (*p)(1, action, exception_object->exception_class, exception_object,
288                (struct _Unwind_Context *)(&cursor2));
289       switch (personalityResult) {
290       case _URC_CONTINUE_UNWIND:
291         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
292                                    "personality returned "
293                                    "_URC_CONTINUE_UNWIND",
294                                    (void *)exception_object);
295         // Destructors called, continue unwinding
296         break;
297       case _URC_INSTALL_CONTEXT:
298         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
299                                    "personality returned "
300                                    "_URC_INSTALL_CONTEXT",
301                                    (void *)exception_object);
302         // We may get control back if landing pad calls _Unwind_Resume().
303         __unw_resume(&cursor2);
304         break;
305       default:
306         // Personality routine returned an unknown result code.
307         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
308                                    "personality returned %d, "
309                                    "_URC_FATAL_PHASE2_ERROR",
310                                    (void *)exception_object, personalityResult);
311         return _URC_FATAL_PHASE2_ERROR;
312       }
313     }
314   }
315 
316   // Call stop function one last time and tell it we've reached the end
317   // of the stack.
318   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
319                              "function with _UA_END_OF_STACK",
320                              (void *)exception_object);
321   _Unwind_Action lastAction =
322       (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
323   (*stop)(1, lastAction, exception_object->exception_class, exception_object,
324           (struct _Unwind_Context *)(&cursor2), stop_parameter);
325 
326   // Clean up phase did not resume at the frame that the search phase said it
327   // would.
328   return _URC_FATAL_PHASE2_ERROR;
329 }
330 
331 /// Called by \c __cxa_throw().  Only returns if there is a fatal error.
332 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception * exception_object)333 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
334   _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
335                        (void *)exception_object);
336 
337   // Mark that this is a non-forced unwind, so _Unwind_Resume()
338   // can do the right thing.
339   memset(exception_object->private_, 0, sizeof(exception_object->private_));
340 
341   // phase 1: the search phase
342   // We'll let the system do that for us.
343   RaiseException(STATUS_GCC_THROW, 0, 1, (ULONG_PTR *)&exception_object);
344 
345   // If we get here, either something went horribly wrong or we reached the
346   // top of the stack. Either way, let libc++abi call std::terminate().
347   return _URC_END_OF_STACK;
348 }
349 
350 /// When \c _Unwind_RaiseException() is in phase2, it hands control
351 /// to the personality function at each frame.  The personality
352 /// may force a jump to a landing pad in that function; the landing
353 /// pad code may then call \c _Unwind_Resume() to continue with the
354 /// unwinding.  Note: the call to \c _Unwind_Resume() is from compiler
355 /// geneated user code.  All other \c _Unwind_* routines are called
356 /// by the C++ runtime \c __cxa_* routines.
357 ///
358 /// Note: re-throwing an exception (as opposed to continuing the unwind)
359 /// is implemented by having the code call \c __cxa_rethrow() which
360 /// in turn calls \c _Unwind_Resume_or_Rethrow().
361 _LIBUNWIND_EXPORT void
_Unwind_Resume(_Unwind_Exception * exception_object)362 _Unwind_Resume(_Unwind_Exception *exception_object) {
363   _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
364 
365   if (exception_object->private_[0] != 0) {
366     unw_context_t uc;
367 
368     __unw_getcontext(&uc);
369     unwind_phase2_forced(&uc, exception_object,
370                          (_Unwind_Stop_Fn) exception_object->private_[0],
371                          (void *)exception_object->private_[4]);
372   } else {
373     // Recover the parameters for the unwind from the exception object
374     // so we can start unwinding again.
375     EXCEPTION_RECORD ms_exc;
376     CONTEXT ms_ctx;
377     UNWIND_HISTORY_TABLE hist;
378 
379     memset(&ms_exc, 0, sizeof(ms_exc));
380     memset(&hist, 0, sizeof(hist));
381     ms_exc.ExceptionCode = STATUS_GCC_THROW;
382     ms_exc.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
383     ms_exc.NumberParameters = 4;
384     ms_exc.ExceptionInformation[0] = (ULONG_PTR)exception_object;
385     ms_exc.ExceptionInformation[1] = exception_object->private_[1];
386     ms_exc.ExceptionInformation[2] = exception_object->private_[2];
387     ms_exc.ExceptionInformation[3] = exception_object->private_[3];
388     RtlUnwindEx((PVOID)exception_object->private_[1],
389                 (PVOID)exception_object->private_[2], &ms_exc,
390                 exception_object, &ms_ctx, &hist);
391   }
392 
393   // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
394   _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
395 }
396 
397 /// Not used by C++.
398 /// Unwinds stack, calling "stop" function at each frame.
399 /// Could be used to implement \c longjmp().
400 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_ForcedUnwind(_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)401 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
402                      _Unwind_Stop_Fn stop, void *stop_parameter) {
403   _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
404                        (void *)exception_object, (void *)(uintptr_t)stop);
405   unw_context_t uc;
406   __unw_getcontext(&uc);
407 
408   // Mark that this is a forced unwind, so _Unwind_Resume() can do
409   // the right thing.
410   exception_object->private_[0] = (uintptr_t) stop;
411   exception_object->private_[4] = (uintptr_t) stop_parameter;
412 
413   // do it
414   return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter);
415 }
416 
417 /// Called by personality handler during phase 2 to get LSDA for current frame.
418 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)419 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
420   uintptr_t result =
421       (uintptr_t)__unw_seh_get_disp_ctx((unw_cursor_t *)context)->HandlerData;
422   _LIBUNWIND_TRACE_API(
423       "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
424       (void *)context, result);
425   return result;
426 }
427 
428 /// Called by personality handler during phase 2 to find the start of the
429 /// function.
430 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetRegionStart(struct _Unwind_Context * context)431 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
432   DISPATCHER_CONTEXT *disp = __unw_seh_get_disp_ctx((unw_cursor_t *)context);
433   uintptr_t result = (uintptr_t)disp->FunctionEntry->BeginAddress + disp->ImageBase;
434   _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
435                        (void *)context, result);
436   return result;
437 }
438 
__unw_init_seh(unw_cursor_t * cursor,CONTEXT * context)439 static int __unw_init_seh(unw_cursor_t *cursor, CONTEXT *context) {
440 #ifdef _LIBUNWIND_TARGET_X86_64
441   new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor))
442       UnwindCursor<LocalAddressSpace, Registers_x86_64>(
443           context, LocalAddressSpace::sThisAddressSpace);
444   auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
445   co->setInfoBasedOnIPRegister();
446   return UNW_ESUCCESS;
447 #elif defined(_LIBUNWIND_TARGET_ARM)
448   new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor))
449       UnwindCursor<LocalAddressSpace, Registers_arm>(
450           context, LocalAddressSpace::sThisAddressSpace);
451   auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
452   co->setInfoBasedOnIPRegister();
453   return UNW_ESUCCESS;
454 #elif defined(_LIBUNWIND_TARGET_AARCH64)
455   new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor))
456       UnwindCursor<LocalAddressSpace, Registers_arm64>(
457           context, LocalAddressSpace::sThisAddressSpace);
458   auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
459   co->setInfoBasedOnIPRegister();
460   return UNW_ESUCCESS;
461 #else
462   return UNW_EINVAL;
463 #endif
464 }
465 
__unw_seh_get_disp_ctx(unw_cursor_t * cursor)466 static DISPATCHER_CONTEXT *__unw_seh_get_disp_ctx(unw_cursor_t *cursor) {
467 #ifdef _LIBUNWIND_TARGET_X86_64
468   return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->getDispatcherContext();
469 #elif defined(_LIBUNWIND_TARGET_ARM)
470   return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->getDispatcherContext();
471 #elif defined(_LIBUNWIND_TARGET_AARCH64)
472   return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->getDispatcherContext();
473 #else
474   return nullptr;
475 #endif
476 }
477 
__unw_seh_set_disp_ctx(unw_cursor_t * cursor,DISPATCHER_CONTEXT * disp)478 static void __unw_seh_set_disp_ctx(unw_cursor_t *cursor,
479                                    DISPATCHER_CONTEXT *disp) {
480 #ifdef _LIBUNWIND_TARGET_X86_64
481   reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->setDispatcherContext(disp);
482 #elif defined(_LIBUNWIND_TARGET_ARM)
483   reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->setDispatcherContext(disp);
484 #elif defined(_LIBUNWIND_TARGET_AARCH64)
485   reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->setDispatcherContext(disp);
486 #endif
487 }
488 
489 #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
490