[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / libunwind / test / forceunwind.pass.cpp
blob8c26551b6d0b6780baa2436c2c0344aa1fb3a10d
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 // REQUIRES: linux
12 // TODO: Figure out why this fails with Memory Sanitizer.
13 // XFAIL: msan
15 // Basic test for _Unwind_ForcedUnwind.
16 // See libcxxabi/test/forced_unwind* tests too.
18 #undef NDEBUG
19 #include <assert.h>
20 #include <dlfcn.h>
21 #include <signal.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <unwind.h>
30 void foo();
31 _Unwind_Exception ex;
33 _Unwind_Reason_Code stop(int version, _Unwind_Action actions,
34 _Unwind_Exception_Class exceptionClass,
35 _Unwind_Exception *exceptionObject,
36 struct _Unwind_Context *context,
37 void *stop_parameter) {
38 assert(version == 1);
39 assert((actions & _UA_FORCE_UNWIND) != 0);
40 (void)exceptionClass;
41 assert(exceptionObject == &ex);
42 assert(stop_parameter == &foo);
44 Dl_info info = {0, 0, 0, 0};
46 // Unwind util the main is reached, above frames depend on the platform and
47 // architecture.
48 if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(context)), &info) &&
49 info.dli_sname && !strcmp("main", info.dli_sname)) {
50 _Exit(0);
52 return _URC_NO_REASON;
55 __attribute__((noinline)) void foo() {
57 // Arm EHABI defines struct _Unwind_Control_Block as exception
58 // object. Ensure struct _Unwind_Exception* work there too,
59 // because _Unwind_Exception in this case is just an alias.
60 struct _Unwind_Exception *e = &ex;
61 #if defined(_LIBUNWIND_ARM_EHABI)
62 // Create a mock exception object.
63 memset(e, '\0', sizeof(*e));
64 strcpy(reinterpret_cast<char *>(&e->exception_class), "CLNGUNW");
65 #endif
66 _Unwind_ForcedUnwind(e, stop, (void *)&foo);
69 int main() {
70 foo();
71 return -2;