[Support] Remove unused includes (NFC) (#116752)
[llvm-project.git] / libunwind / test / unwind_leaffunction.pass.cpp
blob98de7dc43260c293d9e6d0eb123c899ce9961b64
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 //===----------------------------------------------------------------------===//
10 // Ensure that leaf function can be unwund.
11 // REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
13 // TODO: Figure out why this fails with Memory Sanitizer.
14 // XFAIL: msan
16 // Note: this test fails on musl because:
18 // (a) musl disables emission of unwind information for its build, and
19 // (b) musl's signal trampolines don't include unwind information
21 // XFAIL: target={{.*}}-musl
23 #undef NDEBUG
24 #include <assert.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <unwind.h>
33 // Using __attribute__((section("main_func"))) is ELF specific, but then
34 // this entire test is marked as requiring Linux, so we should be good.
36 // We don't use dladdr() because on musl it's a no-op when statically linked.
37 extern char __start_main_func;
38 extern char __stop_main_func;
40 _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
41 (void)arg;
43 // Unwind until the main is reached, above frames depend on the platform and
44 // architecture.
45 uintptr_t ip = _Unwind_GetIP(ctx);
46 if (ip >= (uintptr_t)&__start_main_func &&
47 ip < (uintptr_t)&__stop_main_func) {
48 _Exit(0);
51 return _URC_NO_REASON;
54 void signal_handler(int signum) {
55 (void)signum;
56 _Unwind_Backtrace(frame_handler, NULL);
57 _Exit(-1);
60 __attribute__((noinline)) void crashing_leaf_func(int do_trap) {
61 // libunwind searches for the address before the return address which points
62 // to the trap instruction. We make the trap conditional and prevent inlining
63 // of the function to ensure that the compiler doesn't remove the `ret`
64 // instruction altogether.
66 // It's also important that the trap instruction isn't the first instruction
67 // in the function (which it isn't because of the branch) for other unwinders
68 // that also decrement pc.
69 if (do_trap)
70 __builtin_trap();
73 __attribute__((section("main_func"))) int main(int, char **) {
74 signal(SIGTRAP, signal_handler);
75 signal(SIGILL, signal_handler);
76 crashing_leaf_func(1);
77 return -2;