Skip to content

arch/armv7-a: fix arm_switchcontext() argument in up_schedyield() - #7654

Open
hyunjongkim123 wants to merge 1 commit into
Samsung:masterfrom
hyunjongkim123:fix_crash_os_arch_arm
Open

hyunjongkim123 wants to merge 1 commit into
Samsung:masterfrom
hyunjongkim123:fix_crash_os_arch_arm

Conversation

@hyunjongkim123

Copy link
Copy Markdown
Contributor

Summary

Correctness fix in code that is not compiled on any current defconfig.

up_schedyield() exists only with CONFIG_SCHED_YIELD_OPTIMIZATION=y,
which no defconfig enables today. In that path it calls
arm_switchcontext() with the value of rtcb->xcp.regs cast to
uint32_t ** instead of its address. The prototype in up_internal.h and
all four sibling callers (arm_blocktask, arm_unblocktask,
arm_releasepending, arm_reprioritizertr) take &rtcb->xcp.regs; the
SYS_switch_context handler stores through that pointer, so with the
current code the saved frame pointer would land in the old register
frame instead of the TCB and the next restore would use a stale frame.

The explicit cast silenced the compiler, which is why it went
unnoticed. Fixing it now so the option is usable if someone turns it
on; there is no change to any shipped build.

Change

Pass &rtcb->xcp.regs like the other callers.

Code path not compiled on any current defconfig
(CONFIG_SCHED_YIELD_OPTIMIZATION is off everywhere).

up_schedyield() passed the value of rtcb->xcp.regs cast to uint32_t **
where the prototype in up_internal.h and all four sibling callers
(arm_blocktask, arm_unblocktask, arm_releasepending,
arm_reprioritizertr) pass &rtcb->xcp.regs. The SYS_switch_context
handler stores the new frame pointer through that argument, so with the
old code it would land in the stale register frame instead of the TCB
and the next restore of the task would use a stale frame.

The explicit cast silenced the compiler. Corrected so the option is
usable if enabled later; no change to any shipped build.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@seokhun-eom24

Copy link
Copy Markdown
Contributor

🤖 AI Code Review · 692e8d718383

PR Summary: The change corrects the first argument of arm_switchcontext in up_schedyield from an incorrectly cast rtcb->xcp.regs to &rtcb->xcp.regs, matching the function signature and other call sites.

📋 Findings Overview (0 issues)

✅ Checked with no findings: Memory Leaks, Crashes, Hangs, Correctness

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against the merge-base. Not an approval — that's for a maintainer.

The bug is real and the fix is type-correct

xcp.regs is declared uint32_t *regs (os/arch/arm/include/armv7-a/irq.h:282) and the prototype is void arm_switchcontext(uint32_t **saveregs, uint32_t *restoreregs) (arm_switchcontext.c:61). So the first argument must be the address of the slot in the TCB, which is what the SYS_switch_context handler stores through. The old line passed the slot's value cast to uint32_t **, so the save landed inside the old register frame instead of the TCB, exactly as you describe. With the fix, &rtcb->xcp.regs is uint32_t ** outright — the cast is gone because none is needed, which is itself the sign the types now line up.

All four sibling callers confirmed

arm_blocktask.c:161        arm_switchcontext(&rtcb->xcp.regs, nexttcb->xcp.regs)
arm_unblocktask.c:149      arm_switchcontext(&rtcb->xcp.regs, nexttcb->xcp.regs)
arm_releasepending.c:134   arm_switchcontext(&rtcb->xcp.regs, nexttcb->xcp.regs)
arm_reprioritizertr.c:184  arm_switchcontext(&rtcb->xcp.regs, nexttcb->xcp.regs)
arm_schedyield.c:147       arm_switchcontext((uint32_t **) rtcb->xcp.regs, ...)   <- the outlier

The other ARM ports are not affected: armv7-r's arm_schedyield.c has no switchcontext call, and armv7-m has no arm_schedyield.c at all.

One correction to the description: a defconfig does enable it now

"No defconfig enables it today" is no longer literally true. build/configs/qemu/build_test/defconfig:381 sets CONFIG_SCHED_YIELD_OPTIMIZATION=y, added on 2026-11-11 in 0353fd5b.

It doesn't change the conclusion for this file, though: that config is CONFIG_ARCH_CHIP_LM3S6965, i.e. ARMv7-M, so it never compiles armv7-a/arm_schedyield.c. The accurate statement is "no armv7-a defconfig enables it", which still makes this a dormant-path fix rather than a live one. Worth tightening so a reader doesn't go looking for a QEMU repro that isn't there.

Nice catch — the cast did exactly what casts do: silenced the compiler about a real type error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants