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