Skip to content

x64: fix misaligned load fault with sunk load when AVX is disabled#10417

Merged
alexcrichton merged 1 commit intobytecodealliance:mainfrom
abrown:assembler-fix-10408
Mar 18, 2025
Merged

x64: fix misaligned load fault with sunk load when AVX is disabled#10417
alexcrichton merged 1 commit intobytecodealliance:mainfrom
abrown:assembler-fix-10408

Conversation

@abrown
Copy link
Member

@abrown abrown commented Mar 17, 2025

In #10408, the new assembler re-opened an old issue related to unaligned loads with SSE instructions. SSE instructions expect 128-bit aligned loads when using the m128 operand and fault if that is not the case. This had been fixed previously by disallowing load-sinking for XmmMem (#4891) but more recently we had adopted the use of XmmMemAligned in cranelift-codegen. Since #10316 had no knowledge of XmmMemAligned (only XmmMem), it caused the same kind fault--an OOB trap that was in fact an unaligned load.

Why didn't CI catch this? Since all the CI machines have AVX and we do not explicitly test the SSE-only case, these unaligned, sunk loads would use the AVX lowering in CI. AVX loads handle unaligned accesses without a fault. This was only discovered during fuzzing when AVX was disabled (i.e., --target x86_64-unknown-linux-gnu).

To fix this, this change adopts the XmmMemAligned type in the generated assembler code. This is temporary, though: a more lasting fix should pass along an "alignment required" bit from the assembler AST. In the meantime, this closes #10408.

In [bytecodealliance#10408], the new assembler re-opened an old issue related to
unaligned loads with SSE instructions. SSE instructions expect 128-bit
aligned loads when using the `m128` operand and fault if that is not the
case. This had been fixed previously by disallowing load-sinking for
`XmmMem` ([bytecodealliance#4891]) but more recently we had adopted the use of
`XmmMemAligned` in `cranelift-codegen`. Since [bytecodealliance#10316] had no knowledge
of `XmmMemAligned` (only `XmmMem`), it caused the same kind fault--an
OOB trap that was in fact an unaligned load.

Why didn't CI catch this? Since all the CI machines have AVX and we do
not explicitly test the SSE-only case, these unaligned, sunk loads would
use the AVX lowering in CI. AVX loads handle unaligned accesses without
a fault. This was only discovered during fuzzing when AVX was disabled
(i.e., `--target x86_64-unknown-linux-gnu`).

To fix this, this change adopts the `XmmMemAligned` type in the
generated assembler code. This is temporary, though: a more lasting fix
should pass along an "alignment required" bit from the assembler AST.
In the meantime, this closes bytecodealliance#10408.

[bytecodealliance#10408]: bytecodealliance#10408
[bytecodealliance#4891]: bytecodealliance#4891
[bytecodealliance#10316]: bytecodealliance#10316
@abrown abrown requested a review from a team as a code owner March 17, 2025 23:43
@abrown abrown requested review from alexcrichton and fitzgen and removed request for a team and fitzgen March 17, 2025 23:43
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. isle Related to the ISLE domain-specific language labels Mar 18, 2025
@github-actions
Copy link

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:x64", "cranelift:meta", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@alexcrichton alexcrichton added this pull request to the merge queue Mar 18, 2025
Merged via the queue into bytecodealliance:main with commit ac03075 Mar 18, 2025
52 checks passed
@abrown abrown deleted the assembler-fix-10408 branch March 18, 2025 15:13
abrown added a commit to abrown/wasmtime that referenced this pull request Mar 21, 2025
This change improves the definitions of the assembler's SSE instructions
in two ways:
- vector instructions that require aligned memory accesses (i.e., most
  everything pre-AVX) are now noted with an `align` attribute in the
  AST. This is used for generating the expected `XmmMemAligned` types in
  `cranelift-codegen-meta` the "right way," resolving the temporary fix
  introduced in bytecodealliance#10417.
- previously-added vector instructions did not have the correct feature
  flags; this change adds the `sse` feature and also tags the applicable
  instructions with the `compat` feature to allow their use in some future
  32-bit target.
github-merge-queue bot pushed a commit that referenced this pull request Mar 24, 2025
* asm: notate instruction alignment, SSE feature flags

This change improves the definitions of the assembler's SSE instructions
in two ways:
- vector instructions that require aligned memory accesses (i.e., most
  everything pre-AVX) are now noted with an `align` attribute in the
  AST. This is used for generating the expected `XmmMemAligned` types in
  `cranelift-codegen-meta` the "right way," resolving the temporary fix
  introduced in #10417.
- previously-added vector instructions did not have the correct feature
  flags; this change adds the `sse` feature and also tags the applicable
  instructions with the `compat` feature to allow their use in some future
  32-bit target.

* asm: disallow duplicate features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

x64: Misaligned load fault with sunk float operation when AVX disabled

3 participants