-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
MSVC on x86-32 Windows fails to align variables to their required alignment #112480
Copy link
Copy link
Open
Labels
A-ABIArea: Concerning the application binary interface (ABI)Area: Concerning the application binary interface (ABI)A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-windows-msvcToolchain: MSVC, Operating system: WindowsToolchain: MSVC, Operating system: WindowsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Metadata
Metadata
Assignees
Labels
A-ABIArea: Concerning the application binary interface (ABI)Area: Concerning the application binary interface (ABI)A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-windows-msvcToolchain: MSVC, Operating system: WindowsToolchain: MSVC, Operating system: WindowsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
This is a regression from #98112. I suppose it's not possible to disable this specific check only while preserving debug assertions...
The core problem is that the x86 ABI on Windows doesn't guarantee the stack alignment above 4. See for example https://developercommunity.visualstudio.com/t/vs2017-64-bit-int-alignment-problem/294259
And while some types have an alignment reported of 8 (e.g. UINT64), in practice, the C compiler will happily not align them on the stack.
So for example, this C code, compiled by MSVC for 32-bits:
will produce this assembly:
(on godbolt)
If the stack pointer is not 8-bytes aligned when entering the function, the pointer passed to
hogeis not going to be 8-bytes aligned.As mentioned in the linked community post above, adding
alignas(8)to the type definition makes the compiler align the stack:becomes
(on godbolt)
Now, what this means is that if that
hogefunction is a rust FFI function, and it uses that pointer, the "misaligned pointer dereference" check is hit and panic ensues.Real life case, for the curious:
https://github.com/servo/dwrote-rs/blob/master/src/font_file_loader_impl.rs#L116-L123
That function is called from dwrite.dll (which comes with Windows).
t-opsem FCP comment
Summary of the MSVC alignment rules