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