Skip to content

Commit 48c050f

Browse files
committed
interpret: fix overlapping aggregate initialization
1 parent 7ad23f4 commit 48c050f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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: 11 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,18 @@ 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+
self.validate_operand(
355+
dest,
356+
M::enforce_validity_recursively(self, dest.layout()),
357+
/*reset_provenance_and_padding*/ true,
358+
)?;
359+
interp_ok(())
352360
}
353361

354362
/// Repeats `operand` into the destination. `dest` must have array type, and that type
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(custom_mir, core_intrinsics)]
2+
#![allow(internal_features)]
3+
4+
use std::intrinsics::mir::*;
5+
6+
#[custom_mir(dialect = "runtime")]
7+
fn main() {
8+
mir! {
9+
let _1: (u8,);
10+
{
11+
_1.0 = 0_u8;
12+
// This is a scalar type, so overlap is (for now) not UB.
13+
// However, we used to treat such overlapping assignments incorrectly
14+
// (see <https://github.com/rust-lang/rust/issues/146383#issuecomment-3273224645>).
15+
_1 = (_1.0, );
16+
Return()
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)