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 // _Unwind_ForcedUnwind raised exception can be caught by catch (...) and be
10 // rethrown. If not rethrown, exception_cleanup will be called.
11 
12 // UNSUPPORTED: no-exceptions, c++03
13 
14 // These tests fail on previously released dylibs, investigation needed.
15 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}}
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unwind.h>
20 #include <tuple>
21 #include <__cxxabi_config.h>
22 
23 #if defined(_LIBCXXABI_ARM_EHABI)
24 int main(int, char**) {
25   return 0;
26 }
27 #else
28 static int bits = 0;
29 
30 struct C {
31   int bit;
32   C(int b) : bit(b) {}
33   ~C() { bits |= bit; }
34 };
35 
36 template <typename T>
37 struct Stop;
38 
39 template <typename R, typename... Args>
40 struct Stop<R (*)(Args...)> {
41   // The third argument of _Unwind_Stop_Fn is uint64_t in Itanium C++ ABI/LLVM
42   // libunwind while _Unwind_Exception_Class in libgcc.
43   typedef typename std::tuple_element<2, std::tuple<Args...>>::type type;
44 
45   static _Unwind_Reason_Code stop(int, _Unwind_Action actions, type,
46                                   struct _Unwind_Exception*,
47                                   struct _Unwind_Context*, void*) {
48     if (actions & _UA_END_OF_STACK)
49       abort();
50     return _URC_NO_REASON;
51   }
52 };
53 
54 static void cleanup(_Unwind_Reason_Code, struct _Unwind_Exception* exc) {
55   bits |= 8;
56   delete exc;
57 }
58 
59 static void forced_unwind() {
60   _Unwind_Exception* exc = new _Unwind_Exception;
61   exc->exception_class = 0;
62   exc->exception_cleanup = cleanup;
63   _Unwind_ForcedUnwind(exc, Stop<_Unwind_Stop_Fn>::stop, 0);
64   abort();
65 }
66 
67 static void test() {
68   try {
69     C four(4);
70     try {
71       C one(1);
72       forced_unwind();
73     } catch (...) {
74       bits |= 2;
75       throw;
76     }
77   } catch (int) {
78   } catch (...) {
79     // __cxa_end_catch calls cleanup.
80   }
81 }
82 
83 int main(int, char**) {
84   test();
85   return bits != 15;
86 }
87 #endif
88