Skip to content
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

interpret: fix alignment handling for Repeat expressions #114296

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let rest_ptr = first_ptr.offset(elem_size, self)?;
// For the alignment of `rest_ptr`, we crucially do *not* use `first.align` as
// that place might be more aligned than its type mandates (a `u8` array could
// be 4-aligned if it sits at the right spot in a struct). Instead we use
// `first.layout.align`, i.e., the alignment given by the type.
// be 4-aligned if it sits at the right spot in a struct). We have to also factor
// in element size.
self.mem_copy_repeatedly(
first_ptr,
first.align,
dest.align,
rest_ptr,
first.layout.align.abi,
dest.align.restrict_for_offset(elem_size),
elem_size,
length - 1,
/*nonoverlapping:*/ true,
Expand Down
22 changes: 22 additions & 0 deletions src/tools/miri/tests/pass/align_repeat_into_packed_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(custom_mir, core_intrinsics)]
use std::intrinsics::mir::*;

#[repr(packed)]
struct S { field: [u32; 2] }

#[custom_mir(dialect = "runtime", phase = "optimized")]
fn test() { mir! {
let s: S;
{
// Store a repeat expression directly into a field of a packed struct.
s.field = [0; 2];
Return()
}
} }

fn main() {
// Run this a bunch of time to make sure it doesn't pass by chance.
for _ in 0..20 {
test();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::mem::size_of;

fn main() {
let mut a = Params::new();
// The array itself here happens to be quite well-aligned, but not all its elements have that
// large alignment and we better make sure that is still accepted by Miri.
a.key_block = [0; BLOCKBYTES];
}

Expand Down