Skip to content

Commit fa9d5c9

Browse files
committed
Auto merge of #146402 - RalfJung:aggregate-init, r=saethlin
interpret: fix overlapping aggregate initialization This fixes the problem pointed out by `@saethlin` in #146383 (comment). Also clarify when exactly current de-facto MIR semantics allow overlap of the LHS and RHS in an assignment.
2 parents 52618eb + 7222506 commit fa9d5c9

16 files changed

+92
-15
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use itertools::Itertools as _;
2-
use rustc_abi::{self as abi, FIRST_VARIANT};
2+
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT};
33
use rustc_middle::ty::adjustment::PointerCoercion;
44
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
55
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
@@ -25,6 +25,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2525
match *rvalue {
2626
mir::Rvalue::Use(ref operand) => {
2727
let cg_operand = self.codegen_operand(bx, operand);
28+
// Crucially, we do *not* use `OperandValue::Ref` for types with
29+
// `BackendRepr::Scalar | BackendRepr::ScalarPair`. This ensures we match the MIR
30+
// semantics regarding when assignment operators allow overlap of LHS and RHS.
31+
if matches!(
32+
cg_operand.layout.backend_repr,
33+
BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..),
34+
) {
35+
debug_assert!(!matches!(cg_operand.val, OperandValue::Ref(..)));
36+
}
2837
// FIXME: consider not copying constants through stack. (Fixable by codegen'ing
2938
// constants into `OperandValue::Ref`; why don’t we do that yet if we don’t?)
3039
cg_operand.val.store(bx, dest);

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ where
858858
/// Also, if you use this you are responsible for validating that things get copied at the
859859
/// right type.
860860
#[instrument(skip(self), level = "trace")]
861-
fn copy_op_no_validate(
861+
pub(super) fn copy_op_no_validate(
862862
&mut self,
863863
src: &impl Projectable<'tcx, M::Provenance>,
864864
dest: &impl Writeable<'tcx, M::Provenance>,

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
310310
operands: &IndexSlice<FieldIdx, mir::Operand<'tcx>>,
311311
dest: &PlaceTy<'tcx, M::Provenance>,
312312
) -> InterpResult<'tcx> {
313-
self.write_uninit(dest)?; // make sure all the padding ends up as uninit
314313
let (variant_index, variant_dest, active_field_index) = match *kind {
315314
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
316315
let variant_dest = self.project_downcast(dest, variant_index)?;
@@ -346,9 +345,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
346345
let field_index = active_field_index.unwrap_or(field_index);
347346
let field_dest = self.project_field(&variant_dest, field_index)?;
348347
let op = self.eval_operand(operand, Some(field_dest.layout))?;
349-
self.copy_op(&op, &field_dest)?;
348+
// We validate manually below so we don't have to do it here.
349+
self.copy_op_no_validate(&op, &field_dest, /*allow_transmute*/ false)?;
350350
}
351-
self.write_discriminant(variant_index, dest)
351+
self.write_discriminant(variant_index, dest)?;
352+
// Validate that the entire thing is valid, and reset padding that might be in between the
353+
// fields.
354+
if M::enforce_validity(self, dest.layout()) {
355+
self.validate_operand(
356+
dest,
357+
M::enforce_validity_recursively(self, dest.layout()),
358+
/*reset_provenance_and_padding*/ true,
359+
)?;
360+
}
361+
interp_ok(())
352362
}
353363

354364
/// Repeats `operand` into the destination. `dest` must have array type, and that type

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ pub enum StatementKind<'tcx> {
327327
/// interesting for optimizations? Do we want to allow such optimizations?
328328
///
329329
/// **Needs clarification**: We currently require that the LHS place not overlap with any place
330-
/// read as part of computation of the RHS for some rvalues (generally those not producing
331-
/// primitives). This requirement is under discussion in [#68364]. As a part of this discussion,
332-
/// it is also unclear in what order the components are evaluated.
330+
/// read as part of computation of the RHS for some rvalues. This requirement is under
331+
/// discussion in [#68364]. Specifically, overlap is permitted only for assignments of a type
332+
/// with `BackendRepr::Scalar | BackendRepr::ScalarPair` where all the scalar fields are
333+
/// [`Scalar::Initialized`][rustc_abi::Scalar::Initialized]. As a part of this discussion, it is
334+
/// also unclear in what order the components are evaluated.
333335
///
334336
/// [#68364]: https://github.com/rust-lang/rust/issues/68364
335337
///

src/tools/miri/tests/fail/both_borrows/issue-miri-1050-1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@revisions: stack tree
2+
// Ensure this even hits the aliasing model
3+
//@compile-flags: -Zmiri-disable-validation
24
//@[tree]compile-flags: -Zmiri-tree-borrows
35
//@error-in-other-file: pointer not dereferenceable
46

src/tools/miri/tests/fail/both_borrows/issue-miri-1050-2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@revisions: stack tree
2+
// Ensure this even hits the aliasing model
3+
//@compile-flags: -Zmiri-disable-validation
24
//@[tree]compile-flags: -Zmiri-tree-borrows
35
//@error-in-other-file: is a dangling pointer
46
use std::ptr::NonNull;

src/tools/miri/tests/fail/both_borrows/return_invalid_shr.stack.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ help: <TAG> was created by a SharedReadOnly retag at offsets [0x4..0x8]
1111
|
1212
LL | let ret = unsafe { &(*xraw).1 };
1313
| ^^^^^^^^^^
14-
help: <TAG> was later invalidated at offsets [0x0..0x8] by a write access
14+
help: <TAG> was later invalidated at offsets [0x4..0x8] by a write access
1515
--> tests/fail/both_borrows/return_invalid_shr.rs:LL:CC
1616
|
1717
LL | unsafe { *xraw = (42, 23) }; // unfreeze

src/tools/miri/tests/fail/both_borrows/return_invalid_shr.tree.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: the accessed tag <TAG> was created here, in the initial state Frozen
1212
|
1313
LL | let ret = unsafe { &(*xraw).1 };
1414
| ^^^^^^^^^^
15-
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
15+
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x4..0x8]
1616
--> tests/fail/both_borrows/return_invalid_shr.rs:LL:CC
1717
|
1818
LL | unsafe { *xraw = (42, 23) }; // unfreeze

src/tools/miri/tests/fail/both_borrows/return_invalid_shr_option.stack.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: <TAG> was created by a SharedReadOnly retag at offsets [0x4..0x8]
1414
|
1515
LL | let ret = Some(unsafe { &(*xraw).1 });
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
help: <TAG> was later invalidated at offsets [0x0..0x8] by a write access
17+
help: <TAG> was later invalidated at offsets [0x4..0x8] by a write access
1818
--> tests/fail/both_borrows/return_invalid_shr_option.rs:LL:CC
1919
|
2020
LL | unsafe { *xraw = (42, 23) }; // unfreeze

src/tools/miri/tests/fail/both_borrows/return_invalid_shr_option.tree.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: the accessed tag <TAG> was created here, in the initial state Frozen
1212
|
1313
LL | let ret = Some(unsafe { &(*xraw).1 });
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
15+
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x4..0x8]
1616
--> tests/fail/both_borrows/return_invalid_shr_option.rs:LL:CC
1717
|
1818
LL | unsafe { *xraw = (42, 23) }; // unfreeze

0 commit comments

Comments
 (0)