-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
byval argument of C ABI function passed to Rust ABI function has insufficient alignment #122211
Copy link
Copy link
Closed
Labels
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/SoundnessP-highHigh priorityHigh priorityT-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.
Metadata
Metadata
Assignees
Labels
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/SoundnessP-highHigh priorityHigh priorityT-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.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
I expected to see this happen: Alignment requirements are upheld.
Instead, this happened: https://godbolt.org/z/ej65d5hsv
%xis clearly notalign 16as we indicate. This is unsound.@rustbot label I-unsound A-ffi
This issue is somewhat similar to #80127. The fix for that issue, #112157, made this problem worse by causing Rust and
byvalalignments to differ in more cases. (That change is correct, because the Rust andbyvalalignment must differ to satisfy the ABI. It just uncovered this issue in more cases.)This is also similar to #112480. However, unlike that one, this issue is practical (and easy) to fix.
byvalpointers are not exposed to Rust code, so we can just make a copy of the argument into a higher-aligned alloca, without breaking Rust semantics.In fact, we already do this in the reverse situation, when the Rust alignment is lower than the
byvalalignment (https://godbolt.org/z/Ksxoo3aja):Meta
rustc --version --verbose: