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 | // Ensure that leaf function can be unwund. |
11 | // REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}} |
12 | |
13 | // TODO: Figure out why this fails with Memory Sanitizer. |
14 | // XFAIL: msan |
15 | |
16 | #undef NDEBUG |
17 | #include <assert.h> |
18 | #include <dlfcn.h> |
19 | #include <signal.h> |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <sys/types.h> |
24 | #include <unistd.h> |
25 | #include <unwind.h> |
26 | |
27 | _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) { |
28 | (void)arg; |
29 | Dl_info info = { .dli_fname: 0, .dli_fbase: 0, .dli_sname: 0, .dli_saddr: 0 }; |
30 | |
31 | // Unwind until the main is reached, above frames depend on the platform and |
32 | // architecture. |
33 | if (dladdr(address: reinterpret_cast<void *>(_Unwind_GetIP(ctx)), info: &info) && |
34 | info.dli_sname && !strcmp(s1: "main" , s2: info.dli_sname)) { |
35 | _Exit(status: 0); |
36 | } |
37 | return _URC_NO_REASON; |
38 | } |
39 | |
40 | void signal_handler(int signum) { |
41 | (void)signum; |
42 | _Unwind_Backtrace(frame_handler, NULL); |
43 | _Exit(status: -1); |
44 | } |
45 | |
46 | __attribute__((noinline)) void crashing_leaf_func(int do_trap) { |
47 | // libunwind searches for the address before the return address which points |
48 | // to the trap instruction. We make the trap conditional and prevent inlining |
49 | // of the function to ensure that the compiler doesn't remove the `ret` |
50 | // instruction altogether. |
51 | // |
52 | // It's also important that the trap instruction isn't the first instruction |
53 | // in the function (which it isn't because of the branch) for other unwinders |
54 | // that also decrement pc. |
55 | if (do_trap) |
56 | __builtin_trap(); |
57 | } |
58 | |
59 | int main(int, char**) { |
60 | signal(SIGTRAP, handler: signal_handler); |
61 | signal(SIGILL, handler: signal_handler); |
62 | crashing_leaf_func(do_trap: 1); |
63 | return -2; |
64 | } |
65 | |