-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add misaligned_transmute lint #2400
Add misaligned_transmute lint #2400
Conversation
Thanks! |
I came from here and noticed that the test in transmute.rs includes
But I don't actually see an alignment problem here as Alignment is usually an issue when pointers & references are involved, e.g. creating a |
Indeed, this should probably be alright. I say "probably", because the semantics of |
@sellibitze @oli-obk Are you sure? Maybe I am misreading this assembly, but for example in this function the Rust: pub fn foo(input: &[u8; 4]) -> bool {
let y: u32 = unsafe {std::mem::transmute(*input)};
y == 123456
} x86 (compiled with example::foo:
push rbp
mov rbp, rsp
cmp dword ptr [rdi], 123456
sete al
pop rbp
ret edit: You can remove the pointer entirely in that input and get the same issue: Rust: pub fn foo(input: [u8; 4]) -> bool {
let y: u32 = unsafe {std::mem::transmute(input)};
y == 123456
} x86: example::foo:
push rbp
mov rbp, rsp
cmp edi, 123456
sete al
pop rbp
ret |
@devonhollowood the |
@oli-obk I think I was somewhat unclear. I agree that the |
I don't know enough assembler to make a qualified statement about that. Is the |
@devonhollowood: Very interesting! I guess LLVM optimizes the temporary away and directly accesses the @oli-obk: registers don't have addresses themselves, just names. But edi could store an odd memory address that points to a |
that means we're on the safe side assuming that small align -> large align transmutes are wrong? |
I maintain that
is (or at least should be) perfectly OK and no error. Its observable behaviour is just implementation-defined (depending on the endianness). The assembly code we've seen is what LLVM comes up with after optimization. It made the program access the possibly unaligned memory via I think the question is whether the optimizer makes an assumption that it shouldn't make (in which case the implementation of mem::transmute would be broken). But I can't tell whether that's the case. The data seems inconclusive to me. But I might be wrong. Someone more involved with rustc / LLVM should weigh in, IMHO. |
@sellibitze Okay, so I went back and re-read the relevant docs.
The nomicon section on transmute doesn't mention alignment. The Rust Reference's list of UB says:
Because this so specifically only refers to referencing unaligned pointers, and because transmute is defined to be equivalent to |
Fix #1384