Repository navigation
arm64: repair stack placement of foreign-procedure arguments - #1091
Merged
Merged
Conversation
Once the argument registers are used up, three things went wrong:
* On macOS, a struct of 16 bytes or less that isn't an HFA was packed
onto the stack like a scalar. Apple's ABI packs only scalars at their
natural alignment; such a struct still starts on an 8-byte boundary
and takes a multiple of 8 bytes. `next-is-stack` now gives it an
alignment of 8, and the struct takes `(align 8 size)` bytes. The
lookahead and `categorize-arguments` share one HFA test,
`hfa-register-count`, so they agree on unions with repeated members.
* On macOS, the padding before a stack argument was
`next-align - (end mod 8)`, which is negative when the end mod 8 is
larger than `next-align` (three chars then a short; three floats, a
char, three floats, where the second struct started at 13, not 16).
The padding is now the distance to the next multiple of `next-align`,
and the running alignment is the padded end.
* On all arm64 platforms, a struct over 16 bytes passed by pointer on
the stack failed to compile ("unexpected Triv"): the pointer's offset
was the copying procedure instead of `(immediate ind-sp)`, and the
copy took 8 bytes instead of the struct's size.
The new foreign.ms tests fold each argument into the result in order, so
an argument read from the wrong place changes it.
A struct passed by value on the stack is copied in 8, 4, 2 and 1 byte loads, but load-content-stack wrote every piece back with a word-sized store. For a size whose last slot takes more than one piece (3, 5, 6, 7, 11, 13, 14, 15) the store of a later piece reached into the next argument's slot, and since the argument stores run last to first, that slot had already been filled: stack_char_three_chars received k = 0. The sub-word pieces now go through %rax and a store of their own width, as load-content-regs already does for the register case. Found by the new mats on every x86_64 CI job (a6le, ta6le, a6osx, ta6osx); with this change foreign.mo is clean at o=0 and o=3 on a6osx, and a sweep of stack structs of 3 to 15 bytes passes.
burinc
force-pushed
the
arm64-stack-args
branch
from
November 4, 2026 14:09
724cb93 to
d916cb4
Compare
mflatt
approved these changes
Nov 4, 2026
Contributor
|
Thanks for these repairs! |
…arguments When a struct's size is 3 mod 4, the last three bytes were mishandled.
Contributor
|
Unsurprisingly, the new test exposed problems for arm32, too, so I added a repair. I'll check ppc32 when I can, but won't necessarily hold up the PR for it. |
Contributor
|
This is a good fishing hole, and we might as well keep going. |
burinc
added a commit
to burinc/jolt
that referenced
this pull request
Nov 4, 2026
… 0007
The ffi-stack-args gate failed one row on the x86_64 test shard from this
PR's first run: "char, then struct of three chars". Chez 10.4.1's
load-content-stack copied a struct on the stack in 8/4/2/1-byte loads but
stored each piece as a full word, so for a size whose last slot takes
more than one piece (3, 5, 6, 7, 11, 13, 14, 15 bytes) the later store
reached into the next argument's slot. A probe on an x86_64 build of
v10.4.1 with patches 0001-0006 received i=9 j={41,42,43} k=0.
0007 is the s/x86_64.ss half of cisco/ChezScheme#1091, both commits on
it (d916cb4817, then 5925ab98ac, which also covers callables), against
v10.4.1. It applies after 0001-0006, and on the same x86_64 build the
gate is 8/8 and the probe receives k=44. jolt carries it, like 0006,
until a Chez release has the fix.
Summarize the large set of changes that are all about foreign call/callable arguments, especially struct arguments.
Contributor
|
The last commit reaches a consistent point in my tests, with tests passing on various platforms in addition to the ones that CI will check: RISC-V, LoongArch, Arm32, PPC32 Linux, and PPC32 Mac. I think it probably makes sense to merge these commits as-is, instead of squashing them as we usually do for PRs. I've added one more commit that squash the release-note changes to a summary, though. |
This was referenced Nov 6, 2026
Merged
yogthos
pushed a commit
to jolt-lang/jolt
that referenced
this pull request
Nov 6, 2026
…merged #1091 merged into Chez main on 2026-11-05, as eb6cb62b (arm64) and 251d3962 and 048879e3 (x86_64). No Chez release has them yet, since the newest is still v10.4.1, which is what jolt builds, so the two patches stay. Each now opens with a header naming the merged commits, saying it can be deleted in the change that moves jolt to the first Chez release carrying them. The ffi-stack-args test's comment says the same. Applied to v10.4.1, the patches give an s/arm64.ss and s/x86_64.ss byte-identical to v10.4.1 with those three commits applied. git apply and patch(1) both skip text before the first "diff --git", so all seven patches still apply through ci/apply-chez-patches.sh.
yogthos
added a commit
to jolt-lang/jolt
that referenced
this pull request
Nov 6, 2026
Point Chez patches 0006 and 0007 at the commits cisco/ChezScheme#1091 merged
This was referenced Nov 10, 2026
yogthos
pushed a commit
to jolt-lang/jolt
that referenced
this pull request
Nov 12, 2026
Chez 10.4.1 misplaces some foreign-procedure arguments past the argument registers (cisco/ChezScheme#1091). jolt's release builds carry the fix as chez-patches 0006 and 0007, but a jolt built over a system Chez runs without them and nothing about the binary says which. A library binding structs by value had to refuse every affected signature everywhere. stack-args-correct? asks Chez's code generator once per process. On arm64, a struct over 16 bytes on the stack before an int: 10.4.1 can't compile it, the fixed back end can. On x86_64, a three-char struct between two chars, round-tripped through a foreign-callable: 10.4.1 delivers the last char as 0. It answers nil on other machines, pb builds, and binaries without the compiler. Because it asks the back end and not the build, a Chez release with the fix answers true with no patch. ffi-stack-args-test.sh now checks the answer against its C witness: false where the witness reports a stock kernel, true where its rows pass. Closes #1629.
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.
Fixes #1090.
next-is-stackgives it an alignment of 8. Its stack size is(align 8 size), in aconstant-caseon the macOS machine types, since other platforms already rounded it.(fxand (fx- next-align end-this-align) (fx- next-align 1)), so it's never negative, and the running alignment is the padded end.categorize-argumentssharehfa-register-count, which usesfilter-unionand allows at most 4 members, so the two agree.(immediate ,ind-sp)and copies the struct's size, as the register-indirect branch does.Tests: seven cases in
mats/foreign.ms, with C inmats/foreign2.c. Each C function folds its arguments in order, so a misplaced one changes the result.foreign.mois clean at o=0 and o=3 on tarm64osx and tarm64le, andtest-some-fastis clean on tarm64osx.x86_64 (second commit). The new mats failed on every x86_64 job:
stack_char_three_charsreceivedk = 0. Ins/x86_64.ss,load-content-stackcopied a stack struct in 8/4/2/1-byte loads but stored each piece as a full word, so for a size whose last slot takes more than one piece (3, 5, 6, 7, 11, 13, 14, 15) the later store reached into the next argument's slot, which had already been filled (the argument stores run last to first). The sub-word pieces now go through%raxand a store of their own width, asload-content-regsalready does. On a6osx,foreign.mois clean at o=0 and o=3 and a sweep of stack structs of 3 to 15 bytes passes.