Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mitigate MSVC alignment issue on x86-32 #139261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Apr 2, 2025

This implements mitigation for #112480 by stopping to emit align attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align u64 and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: i686-msvc*

@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

These commits modify compiler targets.
(See the Target Tier Policy.)

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

pub fn has_unreliable_alignment(&self) -> bool {
// FIXME(#112480) MSVC on x86-32 is unsound and fails to properly align many types with
// more-than-4-byte-alignment on the stack.
if self.is_like_msvc && self.arch == "x86" {
Copy link
Member Author

Choose a reason for hiding this comment

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

This function is called a lot during codegen. Should I be worried about the (short) string comparison here?

We can also add a new bool flag to the target spec that directly controls this.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, thinking about this some more it might make sense to make it a part of the target spec so that custom targets can be supported.

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently we auto-detect this for custom targets, if we make it part of the spec custom target specs will have to remember to do this.

Copy link
Member

Choose a reason for hiding this comment

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

Right but I mean the trade-off is that a potential non-msvc target that's not in-tree will have no way to set this. I don't know how likely that is but you mentioned the possibility earlier. And maybe it's fine in any case if such a targets are likely to have other custom requirements that will necessitate forking.

@RalfJung
Copy link
Member Author

RalfJung commented Apr 2, 2025

This will probably be hellish for the codegen test suite since all the align things are missing now...
@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 2, 2025
mitigate MSVC alignment issue on x86-32

This implements mitigation for rust-lang#112480 by stopping to emit `align` attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align `u64` and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: `i686-msvc*`
@bors
Copy link
Collaborator

bors commented Apr 2, 2025

⌛ Trying commit ce44d17 with merge 21a86ec...

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from ce44d17 to 66f7993 Compare April 2, 2025 15:44
@beetrees
Copy link
Contributor

beetrees commented Apr 2, 2025

Looking at #112480 (comment), I think the only alignment this PR needs to change as it that a type with an alignment of 8 bytes might only have an alignment of 4 bytes. MSVC will always align primitive types with an alignment < 8 bytes correctly, and the only types with an alignment > 8 bytes are SIMD types which MSVC aligns correctly anyway.

@RalfJung
Copy link
Member Author

RalfJung commented Apr 2, 2025

Looking at #112480 (comment), I think the only alignment this PR needs to change as it that a type with an alignment of 8 bytes might only have an alignment of 4 bytes. MSVC will always align primitive types with an alignment < 8 bytes correctly, and the only types with an alignment > 8 bytes are SIMD types which MSVC aligns correctly anyway.

I'd rather not rely on the "> 8" part. But yeah I think changing the logic to "clamp to 4" rather than "skip applying alignment" will probably be better, also to affect fewer tests.

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from 66f7993 to ca29777 Compare April 3, 2025 15:48
@RalfJung
Copy link
Member Author

RalfJung commented Apr 3, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 3, 2025
mitigate MSVC alignment issue on x86-32

This implements mitigation for rust-lang#112480 by stopping to emit `align` attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align `u64` and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: `i686-msvc*`
@bors
Copy link
Collaborator

bors commented Apr 3, 2025

⌛ Trying commit ca29777 with merge 0b5c0be...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Apr 3, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 3, 2025
@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from ca29777 to d4ce381 Compare April 4, 2025 13:14
@RalfJung
Copy link
Member Author

RalfJung commented Apr 4, 2025

@saethlin I adjusted the alignment check so that it is still active on MSVC but only checks an alignment of up to 4. I hope I did this right. :D What would be the best way to test this? An only-msvc MIR test?

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from 178790f to df69e5d Compare April 4, 2025 16:07
@RalfJung
Copy link
Member Author

RalfJung commented Apr 4, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 4, 2025
mitigate MSVC alignment issue on x86-32

This implements mitigation for rust-lang#112480 by stopping to emit `align` attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align `u64` and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: `i686-msvc*`
@bors
Copy link
Collaborator

bors commented Apr 4, 2025

⌛ Trying commit df69e5d with merge ae95269...

@bors
Copy link
Collaborator

bors commented Apr 4, 2025

☀️ Try build successful - checks-actions
Build commit: ae95269 (ae95269e81a16ddfe3e2acd26241f427eb8ffda5)

@beetrees
Copy link
Contributor

beetrees commented Apr 4, 2025

I think this also needs to be mitigated on the i686-pc-windows-gnu* targets (which are is_like_windows but not is_like_msvc) that as they can also interact with x86-32 MSVC-built code (such as by calling MSVC-built Windows libraries).

@RalfJung
Copy link
Member Author

RalfJung commented Apr 4, 2025

I think this also needs to be mitigated on the i686-pc-windows-gnu* targets (which are is_like_windows but not is_like_msvc) that as they can also interact with x86-32 MSVC-built code (such as by calling MSVC-built Windows libraries).

I was wondering about that, but the existing logic only kicked in for the MSVC targets.

@beetrees
Copy link
Contributor

beetrees commented Apr 4, 2025

I think it's just that no-one noticed as i686-pc-windows-gnu is much less popular than i686-pc-windows-msvc.

@RalfJung
Copy link
Member Author

RalfJung commented Apr 5, 2025

Fair. There's also been a proposal to demote that target (rust-lang/rfcs#3771).

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from df69e5d to 82ab25c Compare April 7, 2025 15:18
@RalfJung
Copy link
Member Author

RalfJung commented Apr 7, 2025

@bors try

@RalfJung
Copy link
Member Author

RalfJung commented Apr 7, 2025

Oh wait there's conflicts, why did bors not post about that? Odd.

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

🔒 Merge conflict

This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again.

How do I rebase?

Assuming self is your fork and upstream is this repository, you can resolve the conflict following these steps:

  1. git checkout msvc-align-mitigation (switch to your branch)
  2. git fetch upstream master (retrieve the latest master)
  3. git rebase upstream/master -p (rebase on top of it)
  4. Follow the on-screen instruction to resolve conflicts (check git status if you got lost).
  5. git push self msvc-align-mitigation --force-with-lease (update this PR)

You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial.

Please avoid the "Resolve conflicts" button on GitHub. It uses git merge instead of git rebase which makes the PR commit history more difficult to read.

Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Cargo.lock conflict is handled during merge and rebase. This is normal, and you should still perform step 5 to update this PR.

Error message
Auto-merging src/doc/rustc/src/platform-support.md
CONFLICT (content): Merge conflict in src/doc/rustc/src/platform-support.md
Auto-merging compiler/rustc_ty_utils/src/abi.rs
Auto-merging compiler/rustc_target/src/spec/mod.rs
Auto-merging compiler/rustc_target/src/callconv/mod.rs
Auto-merging compiler/rustc_codegen_llvm/src/builder.rs
Automatic merge failed; fix conflicts and then commit the result.

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from 82ab25c to d9b828f Compare April 7, 2025 17:26
@RalfJung
Copy link
Member Author

RalfJung commented Apr 7, 2025

@bors try

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

⌛ Trying commit d9b828f with merge b01413c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
mitigate MSVC alignment issue on x86-32

This implements mitigation for rust-lang#112480 by stopping to emit `align` attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align `u64` and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: `i686-msvc*`
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

💔 Test failed - checks-actions

@RalfJung RalfJung force-pushed the msvc-align-mitigation branch from d9b828f to e702f96 Compare April 7, 2025 21:31
@RalfJung
Copy link
Member Author

RalfJung commented Apr 7, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
mitigate MSVC alignment issue on x86-32

This implements mitigation for rust-lang#112480 by stopping to emit `align` attributes on loads and function arguments when building for a win32 MSVC target. MSVC is known to not properly align `u64` and similar types, and claiming to LLVM that everything is properly aligned increases the chance that this will cause problems.

Of course, the misalignment is still a bug, but we can't fix that bug, only MSVC can.

Also add an errata note to the platform support page warning users about this known problem.

try-job: `i686-msvc*`
@bors
Copy link
Collaborator

bors commented Apr 7, 2025

⌛ Trying commit e702f96 with merge 27ef8fe...

@bors
Copy link
Collaborator

bors commented Apr 8, 2025

☀️ Try build successful - checks-actions
Build commit: 27ef8fe (27ef8fe238fd8ed3348e55d4d58f946056e636d8)

@RalfJung
Copy link
Member Author

RalfJung commented Apr 8, 2025

All right this should be @rustbot ready now.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants