-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Miscompilation with FFI bool return type on AArch64 #159244
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)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeI-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-AArch64Armv8-A or later processors in AArch64 modeArmv8-A or later processors in AArch64 modeP-criticalCritical priorityCritical 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.
Description
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)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeI-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-AArch64Armv8-A or later processors in AArch64 modeArmv8-A or later processors in AArch64 modeP-criticalCritical priorityCritical 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
No fields configured for issues without a type.
Projects
StatusShow more project fields
In progress
View all comments
I tried this C and Rust code:
with
gcc -c get_a.c -o get_a.o -O1 && ar rcs libget_a.a get_a.o && rustc main.rs -L . -C opt-level=1 && ./main.I expected to see this happen: clean program exit, because the value of
ashould get round-tripped andbrokenshould return321.Instead, this happened:
This is caused by
rustcemitting theboolreturn type asi1 zeroextto LLVM, but that is incorrect, because the AArch64 call ABI does not guarantee that the unused bits of the register are zeroed ("any unused bits in the register have unspecified value").boolsis passed in a single register. Theget_afunction is a singleretinstruction and doesn't touch the registers at all.The
ifgets turned into acmp w0, #0which compares the lower 32 bits of register, including the value ofb, to0.This is similar to #97463 which was fixed in #97800, but only covered integer types.
I originally encountered this as spurious Java exception because the
jni-rsExceptionCheck has aboolreturn type.LLM disclosure
I used LLMs to help find the cause of my initial issue, and discover resources for areas I wasn't familiar enough with (rustc internals, JNI, LLVM, AArch64 calling convention).
This issue and the reproducer were written and verified by me (a human).
Meta
rustc --version --verbose:gcc --version:Backtrace