[PATCH v3] ARM: unwind: improve unwinders for noreturn case

Jiangfeng Xiao xiaojiangfeng at huawei.com
Wed Mar 20 08:41:34 PDT 2024


This is an off-by-one bug which is common in unwinders,
due to the fact that the address on the stack points
to the return address rather than the call address.

So, for example, when the last instruction of a function
is a function call (e.g., to a noreturn function), it can
cause the unwinder to incorrectly try to unwind from
the function after the callee.

foo:
...
    bl      bar
... end of function and thus next function ...

which results in LR pointing into the next function.

Fixed this by subtracting 1 from frmae->pc in the call frame
like ORC on x86 does.

Refer to the unwind_next_frame function in the unwind_orc.c

Suggested-by: Josh Poimboeuf <jpoimboe at kernel.org>
Link: https://lkml.kernel.org/lkml/20240305175846.qnyiru7uaa7itqba@treble/
Suggested-by: "Russell King (Oracle)" <linux at armlinux.org.uk>
Link: https://lkml.kernel.org/lkml/Zeg8wRYFemMjcCxG@shell.armlinux.org.uk/
Signed-off-by: Jiangfeng Xiao <xiaojiangfeng at huawei.com>
---
ChangeLog v1->v2
- stay printk("%s...", loglvl, ...)
ChangeLog v2->v3
- Redundant code is deleted to simplify the code
---
 arch/arm/kernel/unwind.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
index 9d21921..abfa8e9 100644
--- a/arch/arm/kernel/unwind.c
+++ b/arch/arm/kernel/unwind.c
@@ -514,6 +514,14 @@ int unwind_frame(struct stackframe *frame)
 	frame->sp = ctrl.vrs[SP];
 	frame->lr = ctrl.vrs[LR];
 	frame->pc = ctrl.vrs[PC];
+	/*
+	 * When the last instruction of a function is a function call
+	 * (e.g., to a noreturn function), it can cause the unwinder
+	 * incorrectly try to unwind from the function after the callee,
+	 * fixed this by subtracting 1 from frame->pc in the call frame
+	 * like ORC on x86 does.
+	 */
+	frame->pc = frame->pc - 1;
 	frame->lr_addr = ctrl.lr_addr;
 
 	return URC_OK;
-- 
1.8.5.6




More information about the linux-arm-kernel mailing list