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

From: Jiangfeng Xiao
Date: Wed Mar 20 2024 - 12:05:49 EST


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@xxxxxxxxxx>
Link: https://lkml.kernel.org/lkml/20240305175846.qnyiru7uaa7itqba@treble/
Suggested-by: "Russell King (Oracle)" <linux@xxxxxxxxxxxxxxx>
Link: https://lkml.kernel.org/lkml/Zeg8wRYFemMjcCxG@xxxxxxxxxxxxxxxxxxxxx/
Signed-off-by: Jiangfeng Xiao <xiaojiangfeng@xxxxxxxxxx>
---
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