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