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 static int bits = 0;
24 
25 struct C {
26   int bit;
27   C(int b) : bit(b) {}
28   ~C() { bits |= bit; }
29 };
30 
31 template <typename T>
32 struct Stop;
33 
34 template <typename R, typename... Args>
35 struct Stop<R (*)(Args...)> {
36   // The third argument of _Unwind_Stop_Fn is uint64_t in Itanium C++ ABI/LLVM
37   // libunwind while _Unwind_Exception_Class in libgcc.
38   typedef typename std::tuple_element<2, std::tuple<Args...>>::type type;
39 
40   static _Unwind_Reason_Code stop(int, _Unwind_Action actions, type,
41                                   struct _Unwind_Exception*,
42                                   struct _Unwind_Context*, void*) {
43     if (actions & _UA_END_OF_STACK)
44       abort();
45     return _URC_NO_REASON;
46   }
47 };
48 
49 static void cleanup(_Unwind_Reason_Code, struct _Unwind_Exception* exc) {
50   bits |= 8;
51   delete exc;
52 }
53 
54 static void forced_unwind() {
55   _Unwind_Exception* exc = new _Unwind_Exception;
56   memset(&exc->exception_class, 0, sizeof(exc->exception_class));
57   exc->exception_cleanup = cleanup;
58   _Unwind_ForcedUnwind(exc, Stop<_Unwind_Stop_Fn>::stop, 0);
59   abort();
60 }
61 
62 static void test() {
63   try {
64     C four(4);
65     try {
66       C one(1);
67       forced_unwind();
68     } catch (...) {
69       bits |= 2;
70       throw;
71     }
72   } catch (int) {
73   } catch (...) {
74     // __cxa_end_catch calls cleanup.
75   }
76 }
77 
78 int main(int, char**) {
79   test();
80   return bits != 15;
81 }
82