fix(thumb): keep SP 8-byte aligned for native calls with >4 argv words#5007
Open
SupposedlySam wants to merge 1 commit into
Open
fix(thumb): keep SP 8-byte aligned for native calls with >4 argv words#5007SupposedlySam wants to merge 1 commit into
SupposedlySam wants to merge 1 commit into
Conversation
invokeNative_thumb.s rounds the outgoing-stack reservation up to 8 bytes and
then unconditionally added another 4 ("+4 because odd(5) registers are in
stack"). But SP is already 8-byte aligned at that point: the prologue pushes
r4-r7 + lr (20 bytes) and does sub sp,bytecodealliance#4, i.e. 24 bytes total
(.cfi_def_cfa_offset 24). The reservation must therefore be a multiple of 8 and
nothing more; the extra word left SP at 4 mod 8 at the blx for any native
called with more than 4 packed argv words. AAPCS requires 8-byte stack
alignment at a public interface.
Effect: the compiler places an outgoing 8-byte argument (e.g. a vararg i64) at
a slot it models as 8-aligned but whose real address is 4 mod 8; the callee's
va_arg realigns to the true 8-byte boundary 4 bytes lower and reads the low
word paired with a zero, so a %lld in such a native printed value * 2^32. The
value itself arrives intact in r2:r3, which is why register-level inspection
looked clean.
Remove the stray add r6, r6, bytecodealliance#4; the reservation is already correctly rounded,
and the restore path (add sp, sp, r6) is symmetric, so nothing else changes.
Signed-off-by: Jonah Walker <supposedlysam@gmail.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
SupposedlySam
requested review from
TianlongLiang,
lum1n0us,
no1wudi and
yamt
as code owners
July 21, 2026 04:42
Author
|
The one red check — |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
invokeNative_thumb.sleaves SP at 4 mod 8 at the native call for any native invoked with more than 4 packed argv words, violating AAPCS 8-byte stack alignment.Root cause
The stack reservation for arguments beyond r0–r3 is rounded up to a multiple of 8 (
add #7/bic #7), then an unconditionaladd r6, r6, #4adds one more word. SP is already 8-byte aligned at that point — the prologue pushes r4–r7 + lr (20 bytes) and doessub sp, #4, 24 bytes total (.cfi_def_cfa_offset 24) — so the reservation must be a multiple of 8 and nothing more. The extra word makes SP4 mod 8atblx ipwheneverargc > 4.Effect
An outgoing 8-byte argument (e.g. a vararg
i64) is placed by the compiler at a slot it models as 8-aligned but whose real address is4 mod 8; the callee'sva_argrealigns to the true boundary 4 bytes lower and reads the low word paired with a zero — so a%lldin such a native printsvalue * 2^32. The value itself arrives intact in r2:r3, which is why it looks correct at the register level.Fix
Remove the stray
add r6, r6, #4. The restore path (add sp, sp, r6) is symmetric, so nothing else changes.Testing
cortex-m33(arm-none-eabi-gcc -x assembler-with-cpp -mthumb -mcpu=cortex-m33).blx.i64vararg printedvalue * 2^32before this change, correct value after.