Skip to content

testing/sig_sp_test: add test for SP context restore on signal return#3536

Open
cshung wants to merge 1 commit into
apache:masterfrom
cshung:arm-sp-context-restore
Open

testing/sig_sp_test: add test for SP context restore on signal return#3536
cshung wants to merge 1 commit into
apache:masterfrom
cshung:arm-sp-context-restore

Conversation

@cshung

@cshung cshung commented Jun 11, 2026

Copy link
Copy Markdown

Summary

Add a test application that verifies modifying REG_R13 (SP) in saved register context during a signal handler is honored on exception return.

Test scenario (simulates a managed runtime unwinding past a trampoline):

  1. Push values 1 and 2 onto the stack (simulating trampoline pushes)
  2. Wait for SIGALRM (async delivery from timer interrupt)
  3. Handler advances SP by 4 (emulates "pop" / unwind past one frame)
  4. Handler redirects PC to a resume function
  5. Resume function pops one value — verifies it equals 1 (not 2)

This exercises the HW exception frame relocation fix in arm_exception.S.

Dependencies:

  • Requires CONFIG_ARMV7M_SP_CONTEXT_RESTORE=y or CONFIG_ARMV8M_SP_CONTEXT_RESTORE=y
  • Flat build only (BUILD_FLAT) — accesses nxsched_self() for saved_regs
  • ARM architecture only

Companion OS fix PR: apache/nuttx#19123

Impact

  • New test application only, no impact on existing code
  • Gated by Kconfig dependencies (will not appear unless SP context restore is enabled)

Testing

Host: Ubuntu 22.04 x86_64, arm-none-eabi-gcc 13.3, QEMU 8.2.2 (via Docker)

Targets tested:

  • lm3s6965-ek:qemu-flat (ARMv7-M, Cortex-M3)
  • mps2-an521:nsh (ARMv8-M, Cortex-M33)

Test log (ARMv7-M):

nsh> sig_sp_test
sig_sp_test: Signal SP restore test
sig_sp_test: push 1, push 2, alarm, handler SP+=4, pop => 1
sig_sp_test: handler - PC=0x00022770 SP=0x2000a9a8
sig_sp_test: handler - new SP=0x2000a9ac PC=0x000226ac
sig_sp_test: popped value = 1 (expected 1)
sig_sp_test: PASS

Test log (ARMv8-M):

nsh> sig_sp_test
sig_sp_test: Signal SP restore test
sig_sp_test: push 1, push 2, alarm, handler SP+=4, pop => 1
sig_sp_test: handler - PC=0x1002b2b8 SP=0x38007b08
sig_sp_test: handler - new SP=0x38007b0c PC=0x1002b1fc
sig_sp_test: popped value = 1 (expected 1)
sig_sp_test: PASS

Sanity check (without SP adjustment in handler):

sig_sp_test: popped value = 2 (expected 1)
sig_sp_test: FAIL - expected 1, got 2

Comment thread testing/sig_sp_test/sig_sp_test_main.c
Add a test that verifies modifying SP (REG_R13) in the saved register
context is honored on signal return. The test pushes values onto the
stack, triggers SIGALRM, and in the handler advances SP to skip a
value, then verifies the correct value is popped after signal return.

This exercises the SP relocation fix in arm_sigdeliver.c for ARMv7-M
and ARMv8-M architectures.

Signed-off-by: Andrew Au <cshung@gmail.com>
@cshung cshung force-pushed the arm-sp-context-restore branch from 8a1ed41 to 4760baa Compare June 19, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants