Skip to content

Commit 9c7013c

Browse files
committed
Auto merge of #131006 - RalfJung:immediate-sanity, r=saethlin
interpret: always enable write_immediate sanity checks Writing a wrongly-sized scalar somewhere can have quite confusing effects. Let's see how expensive it is to catch this early.
2 parents 18b1161 + 7caf2cd commit 9c7013c

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl<Prov: Provenance> Immediate<Prov> {
118118
(Immediate::Scalar(scalar), Abi::Scalar(s)) => {
119119
assert_eq!(scalar.size(), s.size(cx));
120120
if !matches!(s.primitive(), abi::Pointer(..)) {
121+
// This is not a pointer, it should not carry provenance.
121122
assert!(matches!(scalar, Scalar::Int(..)));
122123
}
123124
}

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ where
655655
M::after_local_write(self, local, /*storage_live*/ false)?;
656656
}
657657
// Double-check that the value we are storing and the local fit to each other.
658+
// Things can ge wrong in quite weird ways when this is violated.
659+
// Unfortunately this is too expensive to do in release builds.
658660
if cfg!(debug_assertions) {
659661
src.assert_matches_abi(local_layout.abi, self);
660662
}
@@ -675,9 +677,9 @@ where
675677
layout: TyAndLayout<'tcx>,
676678
dest: MemPlace<M::Provenance>,
677679
) -> InterpResult<'tcx> {
678-
if cfg!(debug_assertions) {
679-
value.assert_matches_abi(layout.abi, self);
680-
}
680+
// We use the sizes from `value` below.
681+
// Ensure that matches the type of the place it is written to.
682+
value.assert_matches_abi(layout.abi, self);
681683
// Note that it is really important that the type here is the right one, and matches the
682684
// type things are read at. In case `value` is a `ScalarPair`, we don't do any magic here
683685
// to handle padding properly, which is only correct if we never look at this data with the

0 commit comments

Comments
 (0)