Skip to contentSkip to conversation

linux-user/sh4: fix unwinding through vdso signal trampolines

PATCH In review Matt Turner 4 participants 8 messages Sep 23 – Sep 25

lore
Share & tools
lore
#0 lore raw

cover linux-user/sh4: fix unwinding through vdso signal trampolines

Unwinding through a signal handler with the sh4 vdso is broken in two
ways: libunwind no longer recognizes the trampolines, since they don't use the kernel's instruction sequence, and libgcc applies the sigreturn CFA to rt_sigreturn frames. Patch 3 aligns the trampolines like the kernel does.

Matt Turner (3):
linux-user/sh4: use the kernel's sigreturn trampoline sequence in the
vdso
linux-user/sh4: fix vdso CFA for rt_sigreturn frames linux-user/sh4: align the vdso sigreturn trampolines

 linux-user/sh4/vdso-be.so | Bin 2704 -> 2724 bytes
 linux-user/sh4/vdso-le.so | Bin 2704 -> 2724 bytes
 linux-user/sh4/vdso.S     |  31 ++++++++++++++++++++++---------
 3 files changed, 22 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/3 linux-user/sh4: use the kernel's sigreturn trampoline sequence in the vdso

With the vdso, default_sigreturn and default_rt_sigreturn point at __kernel_sigreturn and __kernel_rt_sigreturn and setup_sigtramp() is no longer used. The vdso trampolines load the syscall number with mov.l instead of the kernel's mov.w/trapa/or r0,r0 sequence, which undoes commit 9ac5aa7227 ("linux-user/sh4: Fix setup_sigtramp to match Linux kernel trampoline pattern"): libunwind's unw_is_signal_frame() no longer recognizes the signal frame, so unwinding through a signal handler stops there.

Use the sequence from the kernel's own vdso,
arch/sh/kernel/vsyscall/vsyscall-sigreturn.S.

Fixes 975bd51a88 ("linux-user/sh4: add VDSO support for sh4 and sh4eb")
---
 linux-user/sh4/vdso-be.so | Bin 2704 -> 2720 bytes
 linux-user/sh4/vdso-le.so | Bin 2704 -> 2720 bytes
 linux-user/sh4/vdso.S     |  26 ++++++++++++++++++--------
 3 files changed, 18 insertions(+), 8 deletions(-)
linux-user/sh4/vdso-be.so binary +0 -0
index 4f50143f8284ab63693c1117e68f3efa886155e3..bc1fb37e1089b7aa5612da0039e24193b156b3d8 100755
GIT binary patch
linux-user/sh4/vdso-le.so binary +0 -0
index c41cb5831b9e5883a64564da8b2149a33e199f4f..c1ff560cc165413d7b07aa873225bd92afb8569e 100755
GIT binary patch
linux-user/sh4/vdso.S +18 -8
index 4fb5ee9d31..1faa665774 100644
@@ -43,6 +43,22 @@ vdso_syscall __vdso_clock_gettime64, __NR_clock_gettime64
 vdso_syscall __vdso_clock_getres, __NR_clock_getres
 vdso_syscall __vdso_gettimeofday, __NR_gettimeofday
 
+/*
+ * Same instruction sequence as the kernel's
+ * arch/sh/kernel/vsyscall/vsyscall-sigreturn.S. libunwind matches these
+ * bytes to recognize a signal frame.
+ */
+.macro sigreturn_tramp nr
+	mov.w	1f, r3
+	trapa	#0x10
+	or	r0, r0
+	or	r0, r0
+	or	r0, r0
+	or	r0, r0
+	or	r0, r0
+1:	.word	\nr
+.endm
+
 /*
  * Signal return trampolines.
  *
@@ -122,20 +138,14 @@ vdso_syscall __vdso_gettimeofday, __NR_gettimeofday
 
 sigreturn_region_start:
 __kernel_sigreturn:
-	mov.l	1f, r3
-	trapa	#0x10
-	.align	2
-1:	.long	__NR_sigreturn
+	sigreturn_tramp __NR_sigreturn
 endf __kernel_sigreturn
 
 	/* Update CFA base for the rt_sigreturn frame layout. */
 	.cfi_def_cfa_offset	RT_SIGFRAME_SC_OFFSET
 
 __kernel_rt_sigreturn:
-	mov.l	2f, r3
-	trapa	#0x10
-	.align	2
-2:	.long	__NR_rt_sigreturn
+	sigreturn_tramp __NR_rt_sigreturn
 endf __kernel_rt_sigreturn
 sigreturn_region_end:
 
Signature · 1 line
2.54.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] linux-user/sh4: use the kernel's sigreturn trampoline sequence in the vdso

Quoted 12 lines · Matt Turner

With the vdso, default_sigreturn and default_rt_sigreturn point at __kernel_sigreturn and __kernel_rt_sigreturn and setup_sigtramp() is no longer used. The vdso trampolines load the syscall number with mov.l instead of the kernel's mov.w/trapa/or r0,r0 sequence, which undoes commit 9ac5aa7227 ("linux-user/sh4: Fix setup_sigtramp to match Linux kernel trampoline pattern"): libunwind's unw_is_signal_frame() no longer recognizes the signal frame, so unwinding through a signal handler stops there.

Use the sequence from the kernel's own vdso,
arch/sh/kernel/vsyscall/vsyscall-sigreturn.S.

Quoted 8 lines
Fixes 975bd51a88 ("linux-user/sh4: add VDSO support for sh4 and sh4eb")
---
  linux-user/sh4/vdso-be.so | Bin 2704 -> 2720 bytes
  linux-user/sh4/vdso-le.so | Bin 2704 -> 2720 bytes
  linux-user/sh4/vdso.S     |  26 ++++++++++++++++++--------
  3 files changed, 18 insertions(+), 8 deletions(-)

For the whole series:

Series applied to the linux-user-next git tree.

Thanks!
Helge

#3 lore raw

PATCH 2/3 linux-user/sh4: fix vdso CFA for rt_sigreturn frames

In reply to Matt Turner

The unwinder looks up a return address as IP - 1 (hence the nop before sigreturn_region_start). For a frame returning to __kernel_rt_sigreturn that is the last byte of __kernel_sigreturn, which still has the sigreturn CFA, so the rt_sigreturn frame is unwound with the wrong sigcontext offset. _Unwind_Backtrace() from an SA_SIGINFO handler stops after three frames.

Add a nop after .cfi_def_cfa_offset, as linux-user/i386/vdso.S does.

Fixes 975bd51a88 ("linux-user/sh4: add VDSO support for sh4 and sh4eb")
---
 linux-user/sh4/vdso-be.so | Bin 2720 -> 2720 bytes
 linux-user/sh4/vdso-le.so | Bin 2720 -> 2720 bytes
 linux-user/sh4/vdso.S     |   3 ++-
 3 files changed, 2 insertions(+), 1 deletion(-)
linux-user/sh4/vdso-be.so binary +0 -0
index bc1fb37e1089b7aa5612da0039e24193b156b3d8..df9fa22efab46bffbb9957af078199639998ea43 100755
GIT binary patch
linux-user/sh4/vdso-le.so binary +0 -0
index c1ff560cc165413d7b07aa873225bd92afb8569e..decd674202260f045b646b318ebb0b6daf416651 100755
GIT binary patch
linux-user/sh4/vdso.S +2 -1
index 1faa665774..290eb498ae 100644
@@ -141,8 +141,9 @@ __kernel_sigreturn:
 	sigreturn_tramp __NR_sigreturn
 endf __kernel_sigreturn
 
-	/* Update CFA base for the rt_sigreturn frame layout. */
+	/* rt_sigreturn frame layout; nop for the same reason as above. */
 	.cfi_def_cfa_offset	RT_SIGFRAME_SC_OFFSET
+	nop
 
 __kernel_rt_sigreturn:
 	sigreturn_tramp __NR_rt_sigreturn
Signature · 1 line
2.54.0
#4 lore raw

PATCH 3/3 linux-user/sh4: align the vdso sigreturn trampolines

In reply to Matt Turner

libunwind's unw_is_signal_frame() reads the trampoline as 32-bit words, but __kernel_sigreturn is only 2-byte aligned. Align both trampolines to 4 bytes, as the kernel's vsyscall-sigreturn.S does (with .balign 32).

---
 linux-user/sh4/vdso-be.so | Bin 2720 -> 2724 bytes
 linux-user/sh4/vdso-le.so | Bin 2720 -> 2724 bytes
 linux-user/sh4/vdso.S     |   2 ++
 3 files changed, 2 insertions(+)
linux-user/sh4/vdso-be.so binary +0 -0
index df9fa22efab46bffbb9957af078199639998ea43..d74a2d78e869d7bc6c531c36d82d6c2b1315287b 100755
GIT binary patch
linux-user/sh4/vdso-le.so binary +0 -0
index decd674202260f045b646b318ebb0b6daf416651..a59c9e5244dbebd709f187028831f0a51a8b0706 100755
GIT binary patch
linux-user/sh4/vdso.S +2 -0
index 290eb498ae..a66cac7439 100644
@@ -135,6 +135,7 @@ vdso_syscall __vdso_gettimeofday, __NR_gettimeofday
 	.cfi_offset		24, SC_FPSCR
 
 	nop
+	.balign	4
 
 sigreturn_region_start:
 __kernel_sigreturn:
@@ -144,6 +145,7 @@ endf __kernel_sigreturn
 	/* rt_sigreturn frame layout; nop for the same reason as above. */
 	.cfi_def_cfa_offset	RT_SIGFRAME_SC_OFFSET
 	nop
+	.balign	4
 
 __kernel_rt_sigreturn:
 	sigreturn_tramp __NR_rt_sigreturn
Signature · 1 line
2.54.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] linux-user/sh4: align the vdso sigreturn trampolines

Quoted 10 lines · Matt Turner

libunwind's unw_is_signal_frame() reads the trampoline as 32-bit words, but __kernel_sigreturn is only 2-byte aligned. Align both trampolines to 4 bytes, as the kernel's vsyscall-sigreturn.S does (with .balign 32).

---
  linux-user/sh4/vdso-be.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso-le.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso.S     |   2 ++
  3 files changed, 2 insertions(+)
#6 lore raw

Re: [PATCH 3/3] linux-user/sh4: align the vdso sigreturn trampolines

In reply to Matt Turner
Quoted 10 lines · Matt Turner

libunwind's unw_is_signal_frame() reads the trampoline as 32-bit words, but __kernel_sigreturn is only 2-byte aligned. Align both trampolines to 4 bytes, as the kernel's vsyscall-sigreturn.S does (with .balign 32).

---
  linux-user/sh4/vdso-be.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso-le.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso.S     |   2 ++
  3 files changed, 2 insertions(+)

Shouldn't this one be picked up for 11.1.x stable branch too, like the other 2 in this series?

Thanks,

/mjt

#7 lore raw

Re: [PATCH 3/3] linux-user/sh4: align the vdso sigreturn trampolines

Quoted 13 lines · Michael Tokarev
Quoted 10 lines · Matt Turner

libunwind's unw_is_signal_frame() reads the trampoline as 32-bit words, but __kernel_sigreturn is only 2-byte aligned. Align both trampolines to 4 bytes, as the kernel's vsyscall-sigreturn.S does (with .balign 32).

---
  linux-user/sh4/vdso-be.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso-le.so | Bin 2720 -> 2724 bytes
  linux-user/sh4/vdso.S     |   2 ++
  3 files changed, 2 insertions(+)
Quoted 2 lines

Shouldn't this one be picked up for 11.1.x stable branch too, like the other 2 in this series?

Yes please (and thanks!)