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

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

The test pushes values 1 and 2 onto the stack, waits for SIGALRM,
then the handler advances SP by 4 (emulating unwinding past a
trampoline frame) and redirects PC to a resume function that pops
and verifies the value equals 1.

Requires CONFIG_ARMV7M_SP_CONTEXT_RESTORE or
CONFIG_ARMV8M_SP_CONTEXT_RESTORE to be enabled. Only available
for flat builds on ARM (accesses nxsched_self for saved_regs).

Signed-off-by: Andrew Au <cshung@gmail.com>
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.

1 participant