Skip to content

arm64: repair stack placement of foreign-procedure arguments - #1091

Merged
mflatt merged 7 commits into
cisco:mainfrom
burinc:arm64-stack-args
Nov 5, 2026
Merged

mflatt merged 7 commits into
cisco:mainfrom
burinc:arm64-stack-args

Conversation

@burinc

@burinc burinc commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1090.

  • Non-HFA struct of 16 bytes or less (macOS). next-is-stack gives it an alignment of 8. Its stack size is (align 8 size), in a constant-case on the macOS machine types, since other platforms already rounded it.
  • Padding. The padding is (fxand (fx- next-align end-this-align) (fx- next-align 1)), so it's never negative, and the running alignment is the padded end.
  • One HFA test. The lookahead and categorize-arguments share hfa-register-count, which uses filter-union and allows at most 4 members, so the two agree.
  • Struct over 16 bytes on the stack (all arm64). It uses (immediate ,ind-sp) and copies the struct's size, as the register-indirect branch does.

Tests: seven cases in mats/foreign.ms, with C in mats/foreign2.c. Each C function folds its arguments in order, so a misplaced one changes the result.

  • 10.4.1 fails them: six wrong values and the compile error on macOS, the compile error on Linux.
  • With this change, foreign.mo is clean at o=0 and o=3 on tarm64osx and tarm64le, and test-some-fast is clean on tarm64osx.
  • In 3066 generated signatures checked against clang on macOS arm64, 650 were wrong before and none after.

x86_64 (second commit). The new mats failed on every x86_64 job: stack_char_three_chars received k = 0. In s/x86_64.ss, load-content-stack copied 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 %rax and a store of their own width, as load-content-regs already does. On a6osx, foreign.mo is clean at o=0 and o=3 and a sweep of stack structs of 3 to 15 bytes passes.

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.
@mflatt

mflatt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for these repairs!

…arguments

When a struct's size is 3 mod 4, the last three bytes were mishandled.
@mflatt

mflatt commented Nov 4, 2026

Copy link
Copy Markdown
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.

@mflatt

mflatt commented Nov 4, 2026

Copy link
Copy Markdown
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.
@mflatt

mflatt commented Nov 5, 2026

Copy link
Copy Markdown
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.

@mflatt
mflatt merged commit a8f47df into cisco:main Nov 5, 2026
33 of 34 checks passed
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
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.
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.

arm64: foreign-procedure misplaces some stack arguments, and a large struct on the stack doesn't compile

2 participants

⌂