Skip to content

Commit c52b876

Browse files
committed
Auto merge of #118408 - RalfJung:aggregate-assign-uninit, r=saethlin
miri: add test checking that aggregate assignments reset memory to uninit Also, `write_aggregate` is really just a helper for evaluating `Aggregate` rvalues, so it should be in `step.rs`, not `place.rs`. Also factor out `Repeat` rvalues into their own function while we are at it. r? `@saethlin` Fixes rust-lang/miri#3195
2 parents e55544c + 53eb910 commit c52b876

File tree

4 files changed

+114
-60
lines changed

4 files changed

+114
-60
lines changed

compiler/rustc_const_eval/src/interpret/place.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ use std::assert_matches::assert_matches;
77
use either::{Either, Left, Right};
88

99
use rustc_ast::Mutability;
10-
use rustc_index::IndexSlice;
1110
use rustc_middle::mir;
1211
use rustc_middle::ty;
1312
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1413
use rustc_middle::ty::Ty;
15-
use rustc_target::abi::{Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT};
14+
use rustc_target::abi::{Abi, Align, HasDataLayout, Size};
1615

1716
use super::{
1817
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckAlignMsg, ImmTy,
@@ -977,34 +976,6 @@ where
977976
Ok(self.ptr_with_meta_to_mplace(ptr.into(), MemPlaceMeta::Meta(meta), layout))
978977
}
979978

980-
/// Writes the aggregate to the destination.
981-
#[instrument(skip(self), level = "trace")]
982-
pub fn write_aggregate(
983-
&mut self,
984-
kind: &mir::AggregateKind<'tcx>,
985-
operands: &IndexSlice<FieldIdx, mir::Operand<'tcx>>,
986-
dest: &PlaceTy<'tcx, M::Provenance>,
987-
) -> InterpResult<'tcx> {
988-
self.write_uninit(dest)?;
989-
let (variant_index, variant_dest, active_field_index) = match *kind {
990-
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
991-
let variant_dest = self.project_downcast(dest, variant_index)?;
992-
(variant_index, variant_dest, active_field_index)
993-
}
994-
_ => (FIRST_VARIANT, dest.clone(), None),
995-
};
996-
if active_field_index.is_some() {
997-
assert_eq!(operands.len(), 1);
998-
}
999-
for (field_index, operand) in operands.iter_enumerated() {
1000-
let field_index = active_field_index.unwrap_or(field_index);
1001-
let field_dest = self.project_field(&variant_dest, field_index.as_usize())?;
1002-
let op = self.eval_operand(operand, Some(field_dest.layout))?;
1003-
self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?;
1004-
}
1005-
self.write_discriminant(variant_index, dest)
1006-
}
1007-
1008979
pub fn raw_const_to_mplace(
1009980
&self,
1010981
raw: mir::ConstAlloc<'tcx>,

compiler/rustc_const_eval/src/interpret/step.rs

+71-30
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
55
use either::Either;
66

7+
use rustc_index::IndexSlice;
78
use rustc_middle::mir;
8-
use rustc_middle::mir::interpret::{InterpResult, Scalar};
99
use rustc_middle::ty::layout::LayoutOf;
10+
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};
1011

11-
use super::{ImmTy, InterpCx, Machine, Projectable};
12+
use super::{ImmTy, InterpCx, InterpResult, Machine, PlaceTy, Projectable, Scalar};
1213
use crate::util;
1314

1415
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
@@ -187,34 +188,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
187188
}
188189

189190
Repeat(ref operand, _) => {
190-
let src = self.eval_operand(operand, None)?;
191-
assert!(src.layout.is_sized());
192-
let dest = self.force_allocation(&dest)?;
193-
let length = dest.len(self)?;
194-
195-
if length == 0 {
196-
// Nothing to copy... but let's still make sure that `dest` as a place is valid.
197-
self.get_place_alloc_mut(&dest)?;
198-
} else {
199-
// Write the src to the first element.
200-
let first = self.project_index(&dest, 0)?;
201-
self.copy_op(&src, &first, /*allow_transmute*/ false)?;
202-
203-
// This is performance-sensitive code for big static/const arrays! So we
204-
// avoid writing each operand individually and instead just make many copies
205-
// of the first element.
206-
let elem_size = first.layout.size;
207-
let first_ptr = first.ptr();
208-
let rest_ptr = first_ptr.offset(elem_size, self)?;
209-
// No alignment requirement since `copy_op` above already checked it.
210-
self.mem_copy_repeatedly(
211-
first_ptr,
212-
rest_ptr,
213-
elem_size,
214-
length - 1,
215-
/*nonoverlapping:*/ true,
216-
)?;
217-
}
191+
self.write_repeat(operand, &dest)?;
218192
}
219193

220194
Len(place) => {
@@ -307,6 +281,73 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
307281
Ok(())
308282
}
309283

284+
/// Writes the aggregate to the destination.
285+
#[instrument(skip(self), level = "trace")]
286+
fn write_aggregate(
287+
&mut self,
288+
kind: &mir::AggregateKind<'tcx>,
289+
operands: &IndexSlice<FieldIdx, mir::Operand<'tcx>>,
290+
dest: &PlaceTy<'tcx, M::Provenance>,
291+
) -> InterpResult<'tcx> {
292+
self.write_uninit(dest)?; // make sure all the padding ends up as uninit
293+
let (variant_index, variant_dest, active_field_index) = match *kind {
294+
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
295+
let variant_dest = self.project_downcast(dest, variant_index)?;
296+
(variant_index, variant_dest, active_field_index)
297+
}
298+
_ => (FIRST_VARIANT, dest.clone(), None),
299+
};
300+
if active_field_index.is_some() {
301+
assert_eq!(operands.len(), 1);
302+
}
303+
for (field_index, operand) in operands.iter_enumerated() {
304+
let field_index = active_field_index.unwrap_or(field_index);
305+
let field_dest = self.project_field(&variant_dest, field_index.as_usize())?;
306+
let op = self.eval_operand(operand, Some(field_dest.layout))?;
307+
self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?;
308+
}
309+
self.write_discriminant(variant_index, dest)
310+
}
311+
312+
/// Repeats `operand` into the destination. `dest` must have array type, and that type
313+
/// determines how often `operand` is repeated.
314+
fn write_repeat(
315+
&mut self,
316+
operand: &mir::Operand<'tcx>,
317+
dest: &PlaceTy<'tcx, M::Provenance>,
318+
) -> InterpResult<'tcx> {
319+
let src = self.eval_operand(operand, None)?;
320+
assert!(src.layout.is_sized());
321+
let dest = self.force_allocation(&dest)?;
322+
let length = dest.len(self)?;
323+
324+
if length == 0 {
325+
// Nothing to copy... but let's still make sure that `dest` as a place is valid.
326+
self.get_place_alloc_mut(&dest)?;
327+
} else {
328+
// Write the src to the first element.
329+
let first = self.project_index(&dest, 0)?;
330+
self.copy_op(&src, &first, /*allow_transmute*/ false)?;
331+
332+
// This is performance-sensitive code for big static/const arrays! So we
333+
// avoid writing each operand individually and instead just make many copies
334+
// of the first element.
335+
let elem_size = first.layout.size;
336+
let first_ptr = first.ptr();
337+
let rest_ptr = first_ptr.offset(elem_size, self)?;
338+
// No alignment requirement since `copy_op` above already checked it.
339+
self.mem_copy_repeatedly(
340+
first_ptr,
341+
rest_ptr,
342+
elem_size,
343+
length - 1,
344+
/*nonoverlapping:*/ true,
345+
)?;
346+
}
347+
348+
Ok(())
349+
}
350+
310351
/// Evaluate the given terminator. Will also adjust the stack frame and statement position accordingly.
311352
fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
312353
info!("{:?}", terminator.kind);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(custom_mir)]
3+
4+
use std::intrinsics::mir::*;
5+
use std::ptr;
6+
7+
#[repr(C)]
8+
struct S(u8, u16);
9+
10+
#[custom_mir(dialect = "runtime", phase = "optimized")]
11+
fn main() {
12+
mir! {
13+
let s: S;
14+
let sptr;
15+
let sptr2;
16+
let _val;
17+
{
18+
sptr = ptr::addr_of_mut!(s);
19+
sptr2 = sptr as *mut [u8; 4];
20+
*sptr2 = [0; 4];
21+
*sptr = S(0, 0); // should reset the padding
22+
_val = *sptr2; // should hence be UB
23+
//~^ERROR: encountered uninitialized memory
24+
Return()
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value at [1]: encountered uninitialized memory, but expected an integer
2+
--> $DIR/uninit-after-aggregate-assign.rs:LL:CC
3+
|
4+
LL | _val = *sptr2; // should hence be UB
5+
| ^^^^^^^^^^^^^ constructing invalid value at [1]: encountered uninitialized memory, but expected an integer
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/uninit-after-aggregate-assign.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)