xref: /llvm-project/libcxxabi/test/forced_unwind2.pass.cpp (revision a4538cdcee75b78b7165dda05c9aa7718c4837c4)
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 // VE only supports SjLj and doesn't provide _Unwind_ForcedUnwind.
14 // UNSUPPORTED: target={{ve-.*}}
15 
16 // These tests fail on previously released dylibs, investigation needed.
17 // XFAIL: stdlib=system && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}}
18 // XFAIL: stdlib=system && target={{.+}}-apple-macosx{{11.0|12.0}}
19 
20 #include <exception>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unwind.h>
25 #include <tuple>
26 #include <__cxxabi_config.h>
27 
28 template <typename T>
29 struct Stop;
30 
31 template <typename R, typename... Args>
32 struct Stop<R (*)(Args...)> {
33   // The third argument of _Unwind_Stop_Fn is uint64_t in Itanium C++ ABI/LLVM
34   // libunwind while _Unwind_Exception_Class in libgcc.
35   typedef typename std::tuple_element<2, std::tuple<Args...>>::type type;
36 
37   static _Unwind_Reason_Code stop(int, _Unwind_Action actions, type,
38                                   struct _Unwind_Exception*,
39                                   struct _Unwind_Context*, void*) {
40     if (actions & _UA_END_OF_STACK)
41       abort();
42     return _URC_NO_REASON;
43   }
44 };
45 
46 static void forced_unwind() {
47   static _Unwind_Exception exc = {};
48   _Unwind_ForcedUnwind(&exc, Stop<_Unwind_Stop_Fn>::stop, 0);
49   abort();
50 }
51 
52 static void test() noexcept { forced_unwind(); }
53 
54 static void terminate() { exit(0); }
55 
56 int main(int, char**) {
57   std::set_terminate(terminate);
58   try {
59     test();
60   } catch (...) {
61   }
62   abort();
63 }
64