Files
llvm/libunwind/test/unwind_leaffunction.pass.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.4 KiB
C++
Raw Normal View History

// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Ensure that leaf function can be unwund.
[libunwind] Unwind through Linux riscv sigreturn trampoline Similar to D90898 (Linux AArch64) and D124765 (SystemZ). On an Arch Linux RISC-V (riscv64gc), the following code ``` #define _GNU_SOURCE #include <dlfcn.h> #include <libunwind.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> static void handler(int signo) { unw_context_t context; unw_cursor_t cursor; unw_getcontext(&context); unw_init_local(&cursor, &context); unw_word_t pc, sp; do { unw_get_reg(&cursor, UNW_REG_IP, &pc); unw_get_reg(&cursor, UNW_REG_SP, &sp); printf("pc=0x%016zx sp=0x%016zx", (size_t)pc, (size_t)sp); Dl_info info = {}; if (dladdr((void *)pc, &info)) printf(" %s:%s", info.dli_fname, info.dli_sname ? info.dli_sname : ""); puts(""); } while (unw_step(&cursor) > 0); exit(0); } int main() { signal(SIGUSR1, handler); raise(SIGUSR1); return 1; } ``` linked with `-Wl,--export-dynamic` gives an output like ``` pc=0x0000000000010a82 sp=0x00007fffd8a0b910 ./b: pc=0x00007fffa7e77800 sp=0x00007fffd8a0c520 linux-vdso.so.1:__vdso_rt_sigreturn pc=0x00007fffa7d73bee sp=0x00007fffd8a0c960 /usr/lib/libc.so.6: pc=0x00007fffa7d3ed66 sp=0x00007fffd8a0c9b0 /usr/lib/libc.so.6:gsignal pc=0x0000000000010a3c sp=0x00007fffd8a0c9c0 ./b:main pc=0x00007fffa7d2f1d4 sp=0x00007fffd8a0c9e0 /usr/lib/libc.so.6: pc=0x00007fffa7d2f27c sp=0x00007fffd8a0cb10 /usr/lib/libc.so.6:__libc_start_main pc=0x00000000000109a0 sp=0x00007fffd8a0cb60 ./b:_start ``` Co-Authored-By: Fangrui Song <i@maskray.me> Reviewed By: #libunwind, MaskRay Differential Revision: https://reviews.llvm.org/D148499
2023-05-06 11:17:02 -07:00
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
// TODO: Figure out why this fails with Memory Sanitizer.
// XFAIL: msan
// Note: this test fails on musl because:
//
// (a) musl disables emission of unwind information for its build, and
// (b) musl's signal trampolines don't include unwind information
//
// XFAIL: target={{.*}}-musl
#undef NDEBUG
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <unwind.h>
// Using __attribute__((section("main_func"))) is ELF specific, but then
// this entire test is marked as requiring Linux, so we should be good.
//
// We don't use dladdr() because on musl it's a no-op when statically linked.
extern char __start_main_func;
extern char __stop_main_func;
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
(void)arg;
// Unwind until the main is reached, above frames depend on the platform and
// architecture.
uintptr_t ip = _Unwind_GetIP(ctx);
if (ip >= (uintptr_t)&__start_main_func &&
ip < (uintptr_t)&__stop_main_func) {
_Exit(0);
}
return _URC_NO_REASON;
}
void signal_handler(int signum) {
(void)signum;
_Unwind_Backtrace(frame_handler, NULL);
_Exit(-1);
}
__attribute__((noinline)) void crashing_leaf_func(int do_trap) {
// libunwind searches for the address before the return address which points
// to the trap instruction. We make the trap conditional and prevent inlining
// of the function to ensure that the compiler doesn't remove the `ret`
// instruction altogether.
//
// It's also important that the trap instruction isn't the first instruction
// in the function (which it isn't because of the branch) for other unwinders
// that also decrement pc.
if (do_trap)
__builtin_trap();
}
__attribute__((section("main_func"))) int main(int, char **) {
signal(SIGTRAP, signal_handler);
signal(SIGILL, signal_handler);
crashing_leaf_func(1);
return -2;
}