1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | // REQUIRES: linux |
11 | |
12 | // TODO: Figure out why this fails with Memory Sanitizer. |
13 | // XFAIL: msan |
14 | |
15 | // Basic test for _Unwind_ForcedUnwind. |
16 | // See libcxxabi/test/forced_unwind* tests too. |
17 | |
18 | #undef NDEBUG |
19 | #include <assert.h> |
20 | #include <dlfcn.h> |
21 | #include <signal.h> |
22 | #include <stdint.h> |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <sys/types.h> |
27 | #include <unistd.h> |
28 | #include <unwind.h> |
29 | |
30 | void foo(); |
31 | _Unwind_Exception ex; |
32 | |
33 | _Unwind_Reason_Code stop(int version, _Unwind_Action actions, |
34 | _Unwind_Exception_Class exceptionClass, |
35 | _Unwind_Exception *exceptionObject, |
36 | struct _Unwind_Context *context, |
37 | void *stop_parameter) { |
38 | assert(version == 1); |
39 | assert((actions & _UA_FORCE_UNWIND) != 0); |
40 | (void)exceptionClass; |
41 | assert(exceptionObject == &ex); |
42 | assert(stop_parameter == &foo); |
43 | |
44 | Dl_info info = {.dli_fname: 0, .dli_fbase: 0, .dli_sname: 0, .dli_saddr: 0}; |
45 | |
46 | // Unwind util the main is reached, above frames depend on the platform and |
47 | // architecture. |
48 | if (dladdr(address: reinterpret_cast<void *>(_Unwind_GetIP(context)), info: &info) && |
49 | info.dli_sname && !strcmp(s1: "main" , s2: info.dli_sname)) { |
50 | _Exit(status: 0); |
51 | } |
52 | return _URC_NO_REASON; |
53 | } |
54 | |
55 | __attribute__((noinline)) void foo() { |
56 | |
57 | // Arm EHABI defines struct _Unwind_Control_Block as exception |
58 | // object. Ensure struct _Unwind_Exception* work there too, |
59 | // because _Unwind_Exception in this case is just an alias. |
60 | struct _Unwind_Exception *e = &ex; |
61 | #if defined(_LIBUNWIND_ARM_EHABI) |
62 | // Create a mock exception object. |
63 | memset(e, '\0', sizeof(*e)); |
64 | memcpy(&e->exception_class, "CLNGUNW" , sizeof(e->exception_class)); |
65 | #endif |
66 | _Unwind_ForcedUnwind(e, stop, (void *)&foo); |
67 | } |
68 | |
69 | int main() { |
70 | foo(); |
71 | return -2; |
72 | } |
73 | |