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=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}} |
18 | // XFAIL: stdlib=apple-libc++ && 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 | _Unwind_Exception* exc = new _Unwind_Exception; |
48 | memset(s: &exc->exception_class, c: 0, n: sizeof(exc->exception_class)); |
49 | exc->exception_cleanup = 0; |
50 | _Unwind_ForcedUnwind(exc, Stop<_Unwind_Stop_Fn>::stop, 0); |
51 | abort(); |
52 | } |
53 | |
54 | static void test() noexcept { forced_unwind(); } |
55 | |
56 | static void terminate() { exit(status: 0); } |
57 | |
58 | int main(int, char**) { |
59 | std::set_terminate(terminate); |
60 | try { |
61 | test(); |
62 | } catch (...) { |
63 | } |
64 | abort(); |
65 | } |
66 | |