2020-09-16 23:03:19 +02:00
|
|
|
// -*- 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.*}}
|
2020-09-16 23:03:19 +02:00
|
|
|
|
2022-03-02 17:49:13 -05:00
|
|
|
// TODO: Figure out why this fails with Memory Sanitizer.
|
|
|
|
|
// XFAIL: msan
|
|
|
|
|
|
2024-06-10 15:21:13 +01:00
|
|
|
// 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
|
|
|
|
|
|
2022-08-04 15:02:52 -07:00
|
|
|
#undef NDEBUG
|
2020-09-16 23:03:19 +02:00
|
|
|
#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>
|
|
|
|
|
|
2024-06-10 15:21:13 +01:00
|
|
|
// 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;
|
|
|
|
|
|
2020-09-16 23:03:19 +02:00
|
|
|
_Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
2024-01-09 10:39:14 -05:00
|
|
|
// Unwind until the main is reached, above frames depend on the platform and
|
2022-08-20 18:09:03 -07:00
|
|
|
// architecture.
|
2024-06-10 15:21:13 +01:00
|
|
|
uintptr_t ip = _Unwind_GetIP(ctx);
|
|
|
|
|
if (ip >= (uintptr_t)&__start_main_func &&
|
|
|
|
|
ip < (uintptr_t)&__stop_main_func) {
|
2020-09-16 23:03:19 +02:00
|
|
|
_Exit(0);
|
|
|
|
|
}
|
2024-06-10 15:21:13 +01:00
|
|
|
|
2020-09-16 23:03:19 +02:00
|
|
|
return _URC_NO_REASON;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void signal_handler(int signum) {
|
|
|
|
|
(void)signum;
|
|
|
|
|
_Unwind_Backtrace(frame_handler, NULL);
|
|
|
|
|
_Exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 10:39:14 -05:00
|
|
|
__attribute__((noinline)) void crashing_leaf_func(int do_trap) {
|
2021-12-03 11:20:06 -08:00
|
|
|
// libunwind searches for the address before the return address which points
|
2024-01-09 10:39:14 -05:00
|
|
|
// 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();
|
2020-09-16 23:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-10 15:21:13 +01:00
|
|
|
__attribute__((section("main_func"))) int main(int, char **) {
|
2021-12-03 11:20:06 -08:00
|
|
|
signal(SIGTRAP, signal_handler);
|
|
|
|
|
signal(SIGILL, signal_handler);
|
2024-01-09 10:39:14 -05:00
|
|
|
crashing_leaf_func(1);
|
2020-09-16 23:03:19 +02:00
|
|
|
return -2;
|
2021-11-22 14:51:09 -05:00
|
|
|
}
|