diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 99a4bc1b7d6e8..68773ccdddee2 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -893,7 +893,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { // # Global allocations if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) { - let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env); + let (size, mut align) = global_alloc.size_and_align(*self.tcx, self.typing_env); + if let Some(min_global_align) = self.tcx.sess.target.min_global_align { + align = Ord::max(align, min_global_align); + } let mutbl = global_alloc.mutability(*self.tcx, self.typing_env); let kind = match global_alloc { GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData, diff --git a/src/tools/miri/tests/pass/static_align.rs b/src/tools/miri/tests/pass/static_align.rs new file mode 100644 index 0000000000000..d6b72f7d2d5df --- /dev/null +++ b/src/tools/miri/tests/pass/static_align.rs @@ -0,0 +1,37 @@ +// Test that miri respects the `target.min_global_align` value for the target. +// +// The only way to observe its effect currently is to test for the alignment of statics with a +// natural alignment of 1 on s390x. On that target, the `min_global_align` is 2 bytes. + +fn main() { + let min_align = if cfg!(target_arch = "s390x") { 2 } else { 1 }; + + macro_rules! check { + ($x:ident, $v:expr) => { + static $x: bool = $v; + assert!(core::ptr::from_ref(&$x).addr().is_multiple_of(min_align)); + }; + } + + check!(T0, true); + check!(T1, true); + check!(T2, true); + check!(T3, true); + check!(T4, true); + check!(T5, true); + check!(T6, true); + check!(T7, true); + check!(T8, true); + check!(T9, true); + + check!(F0, false); + check!(F1, false); + check!(F2, false); + check!(F3, false); + check!(F4, false); + check!(F5, false); + check!(F6, false); + check!(F7, false); + check!(F8, false); + check!(F9, false); +}