hujun260 opened a new pull request, #21689:
URL: https://github.com/apache/nuttx/pull/21689
# NuttX PR Description
## Title
arm: prevent unwinding to invalid addresses from signal handlers
## Description
This PR clears the link register in the saved signal-delivery context before
entering the signal trampoline. This gives the ARM stack unwinder an explicit
termination point and prevents it from following a stale LR value into an
invalid address after unwinding through `arm_sigdeliver()`.
The change is applied consistently to the ARM, ARMv6-M, ARMv7-A, ARMv7-M,
ARMv7-R, ARMv8-M, and ARMv8-R signal scheduling implementations.
## Problem
Before this change, a backtrace captured from a signal callback could
continue
past `arm_sigdeliver()` using an unrelated LR value. The unwinder could then
report invalid frames or fault while trying to unwind the corrupted chain.
Example invalid unwind result before the change:
```text
(gdb) bt
#7 _assert (...) at misc/assert.c:952
#8 arm_dataabort (...) at armv7-a/arm_dataabort.c:172
#9 arm_vectordata () at armv7-a/arm_vectors.S:453
#10 unwind_frame (...) at common/arm_backtrace_unwind.c:568
#11 backtrace_unwind (...) at common/arm_backtrace_unwind.c:652
#12 up_backtrace (...) at common/arm_backtrace_unwind.c:768
#13 sched_backtrace (...) at sched/sched_backtrace.c:106
...
#28 arm_sigdeliver () at armv7-a/arm_sigdeliver.c:77
#29 nxsched_add_prioritized (...)
#30 nxsem_wait_slow (...)
#31 0x55555554 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
```
The captured unwind buffer also contained an invalid address after
`arm_sigdeliver()`:
```text
0x43158a5c: 0x68fa18 <file_readv+212> 0x68fade <read+78>
0x43158a64: 0x72c494 <uv__signal_handler+84> 0x60ebc2 <nxsig_deliver+482>
0x43158a6c: 0x6505d2 <arm_sigdeliver+78> 0x6cf02a <nxsem_wait_slow+1018>
0x43158a74: 0x55555552 0x0
```
## Changes
- Clear the saved LR value when preparing a signal-delivery context.
- Stop the unwinder at the signal trampoline instead of following stale
state.
- Apply the fix to:
- `arch/arm/src/arm/arm_schedulesigaction.c`
- `arch/arm/src/armv6-m/arm_schedulesigaction.c`
- `arch/arm/src/armv7-a/arm_schedulesigaction.c`
- `arch/arm/src/armv7-m/arm_schedulesigaction.c`
- `arch/arm/src/armv7-r/arm_schedulesigaction.c`
- `arch/arm/src/armv8-m/arm_schedulesigaction.c`
- `arch/arm/src/armv8-r/arm_schedulesigaction.c`
## Result
After the change, sending signal 10 to task 3 produces a bounded and valid
backtrace from the signal callback:
```text
nsh> kill -10 3
nsh> sched_dumpstack: backtrace:
sched_dumpstack: [ 3] [<0x1b0f6>] backtrace_unwind+0x157/0x15a
sched_dumpstack: [ 3] [<0xb262>] sched_backtrace+0x39/0x88
sched_dumpstack: [ 3] [<0x6e3e>] sched_dumpstack+0x19/0x98
sched_dumpstack: [ 3] [<0x37850>] siguser_action+0x13/0x20
sched_dumpstack: [ 3] [<0x17a46>] nxsig_deliver+0x103/0x204
sched_dumpstack: [ 3] [<0x1986>] arm_sigdeliver+0x3d/0xa4
nsh>
```
The unwind terminates at `arm_sigdeliver()` without continuing to invalid or
unrelated addresses.
## Testing Status
- [x] Triggered a signal callback with `kill -10 3`
- [x] Captured the signal callback backtrace
- [x] Confirmed that unwinding terminates at `arm_sigdeliver()`
- [x] Confirmed that no invalid address is reported after the signal frame
- [ ] Compile-tested all affected ARM architecture variants
## Commit
- `3107efd51823f4d09cacdb8d9086f4e0e3a46309` arm: fix unwind to invalid
addresses in signal
## Change Summary
The commit modifies 7 files with 11 insertions.
Signed-off-by: xuxingliang <[email protected]>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]