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 // Forced unwinding causes std::terminate when going through noexcept.
10 
11 // UNSUPPORTED: no-exceptions, c++03
12 
13 // These tests fail on previously released dylibs, investigation needed.
14 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}}
15 
16 #include <exception>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unwind.h>
21 #include <tuple>
22 #include <__cxxabi_config.h>
23 
24 template <typename T>
25 struct Stop;
26 
27 template <typename R, typename... Args>
28 struct Stop<R (*)(Args...)> {
29   // The third argument of _Unwind_Stop_Fn is uint64_t in Itanium C++ ABI/LLVM
30   // libunwind while _Unwind_Exception_Class in libgcc.
31   typedef typename std::tuple_element<2, std::tuple<Args...>>::type type;
32 
33   static _Unwind_Reason_Code stop(int, _Unwind_Action actions, type,
34                                   struct _Unwind_Exception*,
35                                   struct _Unwind_Context*, void*) {
36     if (actions & _UA_END_OF_STACK)
37       abort();
38     return _URC_NO_REASON;
39   }
40 };
41 
42 static void forced_unwind() {
43   _Unwind_Exception* exc = new _Unwind_Exception;
44   memset(&exc->exception_class, 0, sizeof(exc->exception_class));
45   exc->exception_cleanup = 0;
46   _Unwind_ForcedUnwind(exc, Stop<_Unwind_Stop_Fn>::stop, 0);
47   abort();
48 }
49 
50 static void test() noexcept { forced_unwind(); }
51 
52 static void terminate() { exit(0); }
53 
54 int main(int, char**) {
55   std::set_terminate(terminate);
56   try {
57     test();
58   } catch (...) {
59   }
60   abort();
61 }
62