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