/ test / forced_unwind2.pass.cpp
forced_unwind2.pass.cpp
 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  #if defined(_LIBCXXABI_ARM_EHABI)
25  int main(int, char**) {
26    return 0;
27  }
28  #else
29  template <typename T>
30  struct Stop;
31  
32  template <typename R, typename... Args>
33  struct Stop<R (*)(Args...)> {
34    // The third argument of _Unwind_Stop_Fn is uint64_t in Itanium C++ ABI/LLVM
35    // libunwind while _Unwind_Exception_Class in libgcc.
36    typedef typename std::tuple_element<2, std::tuple<Args...>>::type type;
37  
38    static _Unwind_Reason_Code stop(int, _Unwind_Action actions, type,
39                                    struct _Unwind_Exception*,
40                                    struct _Unwind_Context*, void*) {
41      if (actions & _UA_END_OF_STACK)
42        abort();
43      return _URC_NO_REASON;
44    }
45  };
46  
47  static void forced_unwind() {
48    _Unwind_Exception* exc = new _Unwind_Exception;
49    exc->exception_class = 0;
50    exc->exception_cleanup = 0;
51    _Unwind_ForcedUnwind(exc, Stop<_Unwind_Stop_Fn>::stop, 0);
52    abort();
53  }
54  
55  static void test() noexcept { forced_unwind(); }
56  
57  static void terminate() { exit(0); }
58  
59  int main(int, char**) {
60    std::set_terminate(terminate);
61    try {
62      test();
63    } catch (...) {
64    }
65    abort();
66  }
67  #endif