Skip to content

Commit b9c31d4

Browse files
authored
Rollup merge of #95340 - RalfJung:pnvi, r=oli-obk
interpret: with enforce_number_validity, ensure integers are truly Scalar::Int (i.e., no pointers) This is required for rust-lang/miri#2040 r? ``@oli-obk``
2 parents df1a8b3 + 3bbcf64 commit b9c31d4

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
444444
match scalar.try_to_int() {
445445
Ok(int) => int.is_null(),
446446
Err(_) => {
447+
// Can only happen during CTFE.
447448
let ptr = self.scalar_to_ptr(scalar);
448449
match self.memory.ptr_try_get_alloc(ptr) {
449450
Ok((alloc_id, offset, _)) => {
@@ -455,7 +456,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
455456
// Note that one-past-the-end (offset == size) is still inbounds, and never null.
456457
offset > size
457458
}
458-
Err(offset) => offset == 0,
459+
Err(_offset) => bug!("a non-int scalar is always a pointer"),
459460
}
460461
}
461462
}

compiler/rustc_const_eval/src/interpret/validity.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::hash::Hash;
2121

2222
use super::{
2323
alloc_range, CheckInAllocMsg, GlobalAlloc, InterpCx, InterpResult, MPlaceTy, Machine,
24-
MemPlaceMeta, OpTy, ScalarMaybeUninit, ValueVisitor,
24+
MemPlaceMeta, OpTy, Scalar, ScalarMaybeUninit, ValueVisitor,
2525
};
2626

2727
macro_rules! throw_validation_failure {
@@ -521,8 +521,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
521521
// NOTE: Keep this in sync with the array optimization for int/float
522522
// types below!
523523
if M::enforce_number_validity(self.ecx) {
524-
// Integers/floats in CTFE: Must be scalar bits, pointers are dangerous
525-
let is_bits = value.check_init().map_or(false, |v| v.try_to_int().is_ok());
524+
// Integers/floats with number validity: Must be scalar bits, pointers are dangerous.
525+
// As a special exception we *do* match on a `Scalar` here, since we truly want
526+
// to know its underlying representation (and *not* cast it to an integer).
527+
let is_bits =
528+
value.check_init().map_or(false, |v| matches!(v, Scalar::Int(..)));
526529
if !is_bits {
527530
throw_validation_failure!(self.path,
528531
{ "{:x}", value } expected { "initialized plain (non-pointer) bytes" }

0 commit comments

Comments
 (0)