Skip to content

Commit f56ea14

Browse files
ff520gitMaskRay
authored andcommitted
[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
1 parent 6b4bb10 commit f56ea14

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

libunwind/src/UnwindCursor.hpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#endif
3232

3333
#if defined(_LIBUNWIND_TARGET_LINUX) && \
34-
(defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_S390X))
34+
(defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_RISCV) || \
35+
defined(_LIBUNWIND_TARGET_S390X))
3536
#include <sys/syscall.h>
3637
#include <sys/uio.h>
3738
#include <unistd.h>
@@ -993,6 +994,10 @@ class UnwindCursor : public AbstractUnwindCursor{
993994
bool setInfoForSigReturn(Registers_arm64 &);
994995
int stepThroughSigReturn(Registers_arm64 &);
995996
#endif
997+
#if defined(_LIBUNWIND_TARGET_RISCV)
998+
bool setInfoForSigReturn(Registers_riscv &);
999+
int stepThroughSigReturn(Registers_riscv &);
1000+
#endif
9961001
#if defined(_LIBUNWIND_TARGET_S390X)
9971002
bool setInfoForSigReturn(Registers_s390x &);
9981003
int stepThroughSigReturn(Registers_s390x &);
@@ -2720,6 +2725,60 @@ int UnwindCursor<A, R>::stepThroughSigReturn(Registers_arm64 &) {
27202725
#endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
27212726
// defined(_LIBUNWIND_TARGET_AARCH64)
27222727

2728+
#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \
2729+
defined(_LIBUNWIND_TARGET_RISCV)
2730+
template <typename A, typename R>
2731+
bool UnwindCursor<A, R>::setInfoForSigReturn(Registers_riscv &) {
2732+
const pint_t pc = static_cast<pint_t>(getReg(UNW_REG_IP));
2733+
uint32_t instructions[2];
2734+
struct iovec local_iov = {&instructions, sizeof instructions};
2735+
struct iovec remote_iov = {reinterpret_cast<void *>(pc), sizeof instructions};
2736+
long bytesRead =
2737+
syscall(SYS_process_vm_readv, getpid(), &local_iov, 1, &remote_iov, 1, 0);
2738+
// Look for the two instructions used in the sigreturn trampoline
2739+
// __vdso_rt_sigreturn:
2740+
//
2741+
// 0x08b00893 li a7,0x8b
2742+
// 0x00000073 ecall
2743+
if (bytesRead != sizeof instructions || instructions[0] != 0x08b00893 ||
2744+
instructions[1] != 0x00000073)
2745+
return false;
2746+
2747+
_info = {};
2748+
_info.start_ip = pc;
2749+
_info.end_ip = pc + 4;
2750+
_isSigReturn = true;
2751+
return true;
2752+
}
2753+
2754+
template <typename A, typename R>
2755+
int UnwindCursor<A, R>::stepThroughSigReturn(Registers_riscv &) {
2756+
// In the signal trampoline frame, sp points to an rt_sigframe[1], which is:
2757+
// - 128-byte siginfo struct
2758+
// - ucontext_t struct:
2759+
// - 8-byte long (__uc_flags)
2760+
// - 8-byte pointer (*uc_link)
2761+
// - 24-byte uc_stack
2762+
// - 8-byte uc_sigmask
2763+
// - 120-byte of padding to allow sigset_t to be expanded in the future
2764+
// - 8 bytes of padding because sigcontext has 16-byte alignment
2765+
// - struct sigcontext uc_mcontext
2766+
// [1]
2767+
// https://github.com/torvalds/linux/blob/master/arch/riscv/kernel/signal.c
2768+
const pint_t kOffsetSpToSigcontext = 128 + 8 + 8 + 24 + 8 + 128;
2769+
2770+
const pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
2771+
_registers.setIP(_addressSpace.get64(sigctx));
2772+
for (int i = UNW_RISCV_X1; i <= UNW_RISCV_X31; ++i) {
2773+
uint64_t value = _addressSpace.get64(sigctx + static_cast<pint_t>(i * 8));
2774+
_registers.setRegister(i, value);
2775+
}
2776+
_isSignalFrame = true;
2777+
return UNW_STEP_SUCCESS;
2778+
}
2779+
#endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
2780+
// defined(_LIBUNWIND_TARGET_RISCV)
2781+
27232782
#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) && \
27242783
defined(_LIBUNWIND_TARGET_S390X)
27252784
template <typename A, typename R>

libunwind/test/signal_unwind.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
// Ensure that the unwinder can cope with the signal handler.
11-
// REQUIRES: target={{(aarch64|s390x|x86_64)-.+linux.*}}
11+
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
1212

1313
// TODO: Figure out why this fails with Memory Sanitizer.
1414
// XFAIL: msan

libunwind/test/unwind_leaffunction.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
// Ensure that leaf function can be unwund.
11-
// REQUIRES: target={{(aarch64|s390x|x86_64)-.+linux.*}}
11+
// REQUIRES: target={{(aarch64|riscv64|s390x|x86_64)-.+linux.*}}
1212

1313
// TODO: Figure out why this fails with Memory Sanitizer.
1414
// XFAIL: msan

0 commit comments

Comments
 (0)