-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
llvm lint: Undefined behavior: Memory reference address is misaligned #56267
Comments
What’s the target? Ah, never mind, I missed that its |
More minimal: // rustc file.rs -O -Cpasses=lint --crate-type=rlib
pub fn get_physical() {
get_num_physical_cpus()
}
fn get_num_physical_cpus() {
use std::collections::HashMap;
let mut set = HashMap::new();
set.insert((0, 0), ());
} Issue does not occur on stable (or 1.30.0 nightly), but occurs on rustc beta. |
Marking T-libs as that is likely to be an issue within implementation of |
Excerpt:
Clearly both of those adjacent i32 aren't going to be 8-byte aligned ^^ It seems that we're storing to an |
Don't need to look particularly far, this code pub struct Foo<T> {
foo: u64,
bar: T,
}
pub fn test(x: (i32, i32)) -> Foo<(i32, i32)> {
Foo { foo: 0, bar: x }
} generates
It looks like some code is assuming that both elements in a scalar pair have the same alignment as the whole pair. |
Probably the easiest way to get to the root cause is to bisect. |
This code is very likely the culprit: rust/src/librustc_codegen_ssa/mir/operand.rs Lines 333 to 339 in 400c2bc
It takes the 0 and 1 GEPs, but uses the same alignment, without offset adjustment. |
Ouch, that's bad. To obtain the alignment for both fields, we need to do: let (a_scalar, b_scalar) = match dest.layout.abi {
layout::Abi::ScalarPair(ref a, ref b) => (a, b),
_ => bug!("store_with_flags: invalid ScalarPair layout: {:#?}", layout)
};
// This has been copy-pasted all over the place, I wonder how to deduplicate it
let b_offset = a_scalar.value.size(bx).align_to(b_scalar.value.align(bx).abi);
let a_align = dest.align;
let b_align = dest.align.restrict_for_offset(b_offset); Note that we can't use |
Fix alignment of stores to scalar pair The alignment for the second element of a scalar pair is not the same as for the first element, make sure it is calculated correctly. This fixes rust-lang#56267. r? @eddyb
reduced from num_cpus:
built with
RUSTFLAGS="-C passes=lint" cargo build --release -j 1
yieldscc #7463
The text was updated successfully, but these errors were encountered: