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