mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Rollup merge of #127399 - cjgillot:issue-127396, r=oli-obk
Verify that allocations output by GVN are sufficiently aligned. Fixes #127396 r? `@oli-obk`
This commit is contained in:
commit
3e8e8df7c0
@ -1391,11 +1391,15 @@ fn op_to_prop_const<'tcx>(
|
|||||||
let (prov, offset) = pointer.into_parts();
|
let (prov, offset) = pointer.into_parts();
|
||||||
let alloc_id = prov.alloc_id();
|
let alloc_id = prov.alloc_id();
|
||||||
intern_const_alloc_for_constprop(ecx, alloc_id).ok()?;
|
intern_const_alloc_for_constprop(ecx, alloc_id).ok()?;
|
||||||
if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) {
|
|
||||||
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
|
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
|
||||||
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
|
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
|
||||||
// FIXME: find a way to treat this more uniformly
|
// FIXME: find a way to treat this more uniformly (probably by fixing codegen)
|
||||||
// (probably by fixing codegen)
|
if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
|
||||||
|
// Transmuting a constant is just an offset in the allocation. If the alignment of the
|
||||||
|
// allocation is not enough, fallback to copying into a properly aligned value.
|
||||||
|
&& alloc.inner().align >= op.layout.align.abi
|
||||||
|
{
|
||||||
return Some(ConstValue::Indirect { alloc_id, offset });
|
return Some(ConstValue::Indirect { alloc_id, offset });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
tests/ui/mir/alignment/misaligned-constant-gvn.rs
Normal file
8
tests/ui/mir/alignment/misaligned-constant-gvn.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
//@ build-pass
|
||||||
|
//@ compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+GVN
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let variant: Option<u32> = None;
|
||||||
|
let transmuted: u64 = unsafe { std::mem::transmute(variant) };
|
||||||
|
println!("{transmuted}");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user