Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4e1565

Browse files
committedJul 10, 2019
Auto merge of #62441 - RalfJung:place-ptr-normalization, r=oli-obk
Miri: Provide pointer forcing methods for MemPlace and Op These are useful when one wants to to a lot of work with some place or operand and not to int-to-ptr casts all the time. In particular, this is needed to fix some test failures in Miri: we need to normalize before starting a visitor that walks a run-time value, so that we can later be sure (during the visitor walk) that we have a proper `Pointer`. Also see the Miri side at rust-lang/miri#830. Cc @eddyb @oli-obk
2 parents 0324a2b + ac4f6ab commit d4e1565

File tree

9 files changed

+132
-105
lines changed

9 files changed

+132
-105
lines changed
 

‎src/librustc/mir/interpret/value.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,6 @@ impl<'tcx, Tag> Scalar<Tag> {
262262
}
263263
}
264264

265-
/// Returns this pointer's offset from the allocation base, or from NULL (for
266-
/// integer pointers).
267-
#[inline]
268-
pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
269-
match self {
270-
Scalar::Raw { data, size } => {
271-
assert_eq!(size as u64, cx.pointer_size().bytes());
272-
Size::from_bytes(data as u64)
273-
}
274-
Scalar::Ptr(ptr) => ptr.offset,
275-
}
276-
}
277-
278265
#[inline]
279266
pub fn from_bool(b: bool) -> Self {
280267
Scalar::Raw { data: b as u128, size: 1 }
@@ -339,6 +326,10 @@ impl<'tcx, Tag> Scalar<Tag> {
339326
Scalar::Raw { data: f.to_bits(), size: 8 }
340327
}
341328

329+
/// This is very rarely the method you want! You should dispatch on the type
330+
/// and use `force_bits`/`assert_bits`/`force_ptr`/`assert_ptr`.
331+
/// This method only exists for the benefit of low-level memory operations
332+
/// as well as the implementation of the `force_*` methods.
342333
#[inline]
343334
pub fn to_bits_or_ptr(
344335
self,
@@ -359,6 +350,7 @@ impl<'tcx, Tag> Scalar<Tag> {
359350
}
360351
}
361352

353+
/// Do not call this method! Use either `assert_bits` or `force_bits`.
362354
#[inline]
363355
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
364356
match self {
@@ -372,6 +364,12 @@ impl<'tcx, Tag> Scalar<Tag> {
372364
}
373365
}
374366

367+
#[inline(always)]
368+
pub fn assert_bits(self, target_size: Size) -> u128 {
369+
self.to_bits(target_size).expect("Expected Raw bits but got a Pointer")
370+
}
371+
372+
/// Do not call this method! Use either `assert_ptr` or `force_ptr`.
375373
#[inline]
376374
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
377375
match self {
@@ -381,6 +379,12 @@ impl<'tcx, Tag> Scalar<Tag> {
381379
}
382380
}
383381

382+
#[inline(always)]
383+
pub fn assert_ptr(self) -> Pointer<Tag> {
384+
self.to_ptr().expect("Expected a Pointer but got Raw bits")
385+
}
386+
387+
/// Do not call this method! Dispatch based on the type instead.
384388
#[inline]
385389
pub fn is_bits(self) -> bool {
386390
match self {
@@ -389,6 +393,7 @@ impl<'tcx, Tag> Scalar<Tag> {
389393
}
390394
}
391395

396+
/// Do not call this method! Dispatch based on the type instead.
392397
#[inline]
393398
pub fn is_ptr(self) -> bool {
394399
match self {
@@ -536,11 +541,13 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
536541
}
537542
}
538543

544+
/// Do not call this method! Use either `assert_ptr` or `force_ptr`.
539545
#[inline(always)]
540546
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
541547
self.not_undef()?.to_ptr()
542548
}
543549

550+
/// Do not call this method! Use either `assert_bits` or `force_bits`.
544551
#[inline(always)]
545552
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
546553
self.not_undef()?.to_bits(target_size)

‎src/librustc_mir/const_eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn op_to_const<'tcx>(
109109
// `Immediate` is when we are called from `const_field`, and that `Immediate`
110110
// comes from a constant so it can happen have `Undef`, because the indirect
111111
// memory that was read had undefined bytes.
112-
let mplace = op.to_mem_place();
112+
let mplace = op.assert_mem_place();
113113
let ptr = mplace.ptr.to_ptr().unwrap();
114114
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
115115
ConstValue::ByRef { offset: ptr.offset, align: mplace.align, alloc }
@@ -661,7 +661,7 @@ pub fn const_eval_raw_provider<'tcx>(
661661
|body| eval_body_using_ecx(&mut ecx, cid, body, key.param_env)
662662
).and_then(|place| {
663663
Ok(RawConst {
664-
alloc_id: place.to_ptr().expect("we allocated this ptr!").alloc_id,
664+
alloc_id: place.ptr.assert_ptr().alloc_id,
665665
ty: place.layout.ty
666666
})
667667
}).map_err(|error| {

‎src/librustc_mir/interpret/memory.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
214214
None => Size::from_bytes(self.get(ptr.alloc_id)?.bytes.len() as u64),
215215
};
216216
self.copy(
217-
ptr.into(),
218-
Align::from_bytes(1).unwrap(), // old_align anyway gets checked below by `deallocate`
219-
new_ptr.into(),
220-
new_align,
217+
ptr,
218+
new_ptr,
221219
old_size.min(new_size),
222220
/*nonoverlapping*/ true,
223221
)?;
@@ -310,6 +308,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
310308
/// `Pointer` they need. And even if you already have a `Pointer`, call this method
311309
/// to make sure it is sufficiently aligned and not dangling. Not doing that may
312310
/// cause ICEs.
311+
///
312+
/// Most of the time you should use `check_mplace_access`, but when you just have a pointer,
313+
/// this method is still appropriate.
313314
pub fn check_ptr_access(
314315
&self,
315316
sptr: Scalar<M::PointerTag>,
@@ -751,39 +752,26 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
751752
self.get(ptr.alloc_id)?.read_c_str(self, ptr)
752753
}
753754

754-
/// Performs appropriate bounds checks.
755+
/// Expects the caller to have checked bounds and alignment.
755756
pub fn copy(
756757
&mut self,
757-
src: Scalar<M::PointerTag>,
758-
src_align: Align,
759-
dest: Scalar<M::PointerTag>,
760-
dest_align: Align,
758+
src: Pointer<M::PointerTag>,
759+
dest: Pointer<M::PointerTag>,
761760
size: Size,
762761
nonoverlapping: bool,
763762
) -> InterpResult<'tcx> {
764-
self.copy_repeatedly(src, src_align, dest, dest_align, size, 1, nonoverlapping)
763+
self.copy_repeatedly(src, dest, size, 1, nonoverlapping)
765764
}
766765

767-
/// Performs appropriate bounds checks.
766+
/// Expects the caller to have checked bounds and alignment.
768767
pub fn copy_repeatedly(
769768
&mut self,
770-
src: Scalar<M::PointerTag>,
771-
src_align: Align,
772-
dest: Scalar<M::PointerTag>,
773-
dest_align: Align,
769+
src: Pointer<M::PointerTag>,
770+
dest: Pointer<M::PointerTag>,
774771
size: Size,
775772
length: u64,
776773
nonoverlapping: bool,
777774
) -> InterpResult<'tcx> {
778-
// We need to check *both* before early-aborting due to the size being 0.
779-
let (src, dest) = match (self.check_ptr_access(src, size, src_align)?,
780-
self.check_ptr_access(dest, size * length, dest_align)?)
781-
{
782-
(Some(src), Some(dest)) => (src, dest),
783-
// One of the two sizes is 0.
784-
_ => return Ok(()),
785-
};
786-
787775
// first copy the relocations to a temporary buffer, because
788776
// `get_bytes_mut` will clear the relocations, which is correct,
789777
// since we don't want to keep any relocations at the target.

‎src/librustc_mir/interpret/operand.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,23 @@ pub enum Operand<Tag=(), Id=AllocId> {
123123

124124
impl<Tag> Operand<Tag> {
125125
#[inline]
126-
pub fn to_mem_place(self) -> MemPlace<Tag>
126+
pub fn assert_mem_place(self) -> MemPlace<Tag>
127127
where Tag: ::std::fmt::Debug
128128
{
129129
match self {
130130
Operand::Indirect(mplace) => mplace,
131-
_ => bug!("to_mem_place: expected Operand::Indirect, got {:?}", self),
131+
_ => bug!("assert_mem_place: expected Operand::Indirect, got {:?}", self),
132132

133133
}
134134
}
135135

136136
#[inline]
137-
pub fn to_immediate(self) -> Immediate<Tag>
137+
pub fn assert_immediate(self) -> Immediate<Tag>
138138
where Tag: ::std::fmt::Debug
139139
{
140140
match self {
141141
Operand::Immediate(imm) => imm,
142-
_ => bug!("to_immediate: expected Operand::Immediate, got {:?}", self),
142+
_ => bug!("assert_immediate: expected Operand::Immediate, got {:?}", self),
143143

144144
}
145145
}
@@ -214,6 +214,19 @@ pub(super) fn from_known_layout<'tcx>(
214214
}
215215

216216
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
217+
/// Normalice `place.ptr` to a `Pointer` if this is a place and not a ZST.
218+
/// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
219+
#[inline]
220+
pub fn force_op_ptr(
221+
&self,
222+
op: OpTy<'tcx, M::PointerTag>,
223+
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
224+
match op.try_as_mplace() {
225+
Ok(mplace) => Ok(self.force_mplace_ptr(mplace)?.into()),
226+
Err(imm) => Ok(imm.into()), // Nothing to cast/force
227+
}
228+
}
229+
217230
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
218231
/// Returns `None` if the layout does not permit loading this as a value.
219232
fn try_read_immediate_from_mplace(
@@ -224,9 +237,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
224237
// Don't touch unsized
225238
return Ok(None);
226239
}
227-
let (ptr, ptr_align) = mplace.to_scalar_ptr_align();
228240

229-
let ptr = match self.memory.check_ptr_access(ptr, mplace.layout.size, ptr_align)? {
241+
let ptr = match self.check_mplace_access(mplace, None)? {
230242
Some(ptr) => ptr,
231243
None => return Ok(Some(ImmTy { // zero-sized type
232244
imm: Immediate::Scalar(Scalar::zst().into()),
@@ -396,7 +408,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
396408
} else {
397409
// The rest should only occur as mplace, we do not use Immediates for types
398410
// allowing such operations. This matches place_projection forcing an allocation.
399-
let mplace = base.to_mem_place();
411+
let mplace = base.assert_mem_place();
400412
self.mplace_projection(mplace, proj_elem)?.into()
401413
}
402414
})

‎src/librustc_mir/interpret/place.rs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,6 @@ impl<Tag> MemPlace<Tag> {
122122
Self::from_scalar_ptr(ptr.into(), align)
123123
}
124124

125-
#[inline(always)]
126-
pub fn to_scalar_ptr_align(self) -> (Scalar<Tag>, Align) {
127-
assert!(self.meta.is_none());
128-
(self.ptr, self.align)
129-
}
130-
131-
/// metact the ptr part of the mplace
132-
#[inline(always)]
133-
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
134-
// At this point, we forget about the alignment information --
135-
// the place has been turned into a reference, and no matter where it came from,
136-
// it now must be aligned.
137-
self.to_scalar_ptr_align().0.to_ptr()
138-
}
139-
140125
/// Turn a mplace into a (thin or fat) pointer, as a reference, pointing to the same space.
141126
/// This is the inverse of `ref_to_mplace`.
142127
#[inline(always)]
@@ -230,6 +215,7 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
230215
}
231216
}
232217

218+
// These are defined here because they produce a place.
233219
impl<'tcx, Tag: ::std::fmt::Debug + Copy> OpTy<'tcx, Tag> {
234220
#[inline(always)]
235221
pub fn try_as_mplace(self) -> Result<MPlaceTy<'tcx, Tag>, ImmTy<'tcx, Tag>> {
@@ -240,12 +226,12 @@ impl<'tcx, Tag: ::std::fmt::Debug + Copy> OpTy<'tcx, Tag> {
240226
}
241227

242228
#[inline(always)]
243-
pub fn to_mem_place(self) -> MPlaceTy<'tcx, Tag> {
229+
pub fn assert_mem_place(self) -> MPlaceTy<'tcx, Tag> {
244230
self.try_as_mplace().unwrap()
245231
}
246232
}
247233

248-
impl<'tcx, Tag: ::std::fmt::Debug> Place<Tag> {
234+
impl<Tag: ::std::fmt::Debug> Place<Tag> {
249235
/// Produces a Place that will error if attempted to be read from or written to
250236
#[inline(always)]
251237
pub fn null(cx: &impl HasDataLayout) -> Self {
@@ -263,29 +249,19 @@ impl<'tcx, Tag: ::std::fmt::Debug> Place<Tag> {
263249
}
264250

265251
#[inline]
266-
pub fn to_mem_place(self) -> MemPlace<Tag> {
252+
pub fn assert_mem_place(self) -> MemPlace<Tag> {
267253
match self {
268254
Place::Ptr(mplace) => mplace,
269-
_ => bug!("to_mem_place: expected Place::Ptr, got {:?}", self),
255+
_ => bug!("assert_mem_place: expected Place::Ptr, got {:?}", self),
270256

271257
}
272258
}
273-
274-
#[inline]
275-
pub fn to_scalar_ptr_align(self) -> (Scalar<Tag>, Align) {
276-
self.to_mem_place().to_scalar_ptr_align()
277-
}
278-
279-
#[inline]
280-
pub fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
281-
self.to_mem_place().to_ptr()
282-
}
283259
}
284260

285261
impl<'tcx, Tag: ::std::fmt::Debug> PlaceTy<'tcx, Tag> {
286262
#[inline]
287-
pub fn to_mem_place(self) -> MPlaceTy<'tcx, Tag> {
288-
MPlaceTy { mplace: self.place.to_mem_place(), layout: self.layout }
263+
pub fn assert_mem_place(self) -> MPlaceTy<'tcx, Tag> {
264+
MPlaceTy { mplace: self.place.assert_mem_place(), layout: self.layout }
289265
}
290266
}
291267

@@ -301,8 +277,6 @@ where
301277
{
302278
/// Take a value, which represents a (thin or fat) reference, and make it a place.
303279
/// Alignment is just based on the type. This is the inverse of `MemPlace::to_ref()`.
304-
/// This does NOT call the "deref" machine hook, so it does NOT count as a
305-
/// deref as far as Stacked Borrows is concerned. Use `deref_operand` for that!
306280
pub fn ref_to_mplace(
307281
&self,
308282
val: ImmTy<'tcx, M::PointerTag>,
@@ -322,8 +296,8 @@ where
322296
Ok(MPlaceTy { mplace, layout })
323297
}
324298

325-
// Take an operand, representing a pointer, and dereference it to a place -- that
326-
// will always be a MemPlace. Lives in `place.rs` because it creates a place.
299+
/// Take an operand, representing a pointer, and dereference it to a place -- that
300+
/// will always be a MemPlace. Lives in `place.rs` because it creates a place.
327301
pub fn deref_operand(
328302
&self,
329303
src: OpTy<'tcx, M::PointerTag>,
@@ -333,6 +307,36 @@ where
333307
self.ref_to_mplace(val)
334308
}
335309

310+
/// Check if the given place is good for memory access with the given
311+
/// size, falling back to the layout's size if `None` (in the latter case,
312+
/// this must be a statically sized type).
313+
///
314+
/// On success, returns `None` for zero-sized accesses (where nothing else is
315+
/// left to do) and a `Pointer` to use for the actual access otherwise.
316+
#[inline]
317+
pub fn check_mplace_access(
318+
&self,
319+
place: MPlaceTy<'tcx, M::PointerTag>,
320+
size: Option<Size>,
321+
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
322+
let size = size.unwrap_or_else(|| {
323+
assert!(!place.layout.is_unsized());
324+
assert!(place.meta.is_none());
325+
place.layout.size
326+
});
327+
self.memory.check_ptr_access(place.ptr, size, place.align)
328+
}
329+
330+
/// Force `place.ptr` to a `Pointer`.
331+
/// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
332+
pub fn force_mplace_ptr(
333+
&self,
334+
mut place: MPlaceTy<'tcx, M::PointerTag>,
335+
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
336+
place.mplace.ptr = self.force_ptr(place.mplace.ptr)?.into();
337+
Ok(place)
338+
}
339+
336340
/// Offset a pointer to project to a field. Unlike `place_field`, this is always
337341
/// possible without allocating, so it can take `&self`. Also return the field's layout.
338342
/// This supports both struct and array fields.
@@ -741,14 +745,12 @@ where
741745
value: Immediate<M::PointerTag>,
742746
dest: MPlaceTy<'tcx, M::PointerTag>,
743747
) -> InterpResult<'tcx> {
744-
let (ptr, ptr_align) = dest.to_scalar_ptr_align();
745748
// Note that it is really important that the type here is the right one, and matches the
746749
// type things are read at. In case `src_val` is a `ScalarPair`, we don't do any magic here
747750
// to handle padding properly, which is only correct if we never look at this data with the
748751
// wrong type.
749-
assert!(!dest.layout.is_unsized());
750752

751-
let ptr = match self.memory.check_ptr_access(ptr, dest.layout.size, ptr_align)? {
753+
let ptr = match self.check_mplace_access(dest, None)? {
752754
Some(ptr) => ptr,
753755
None => return Ok(()), // zero-sized access
754756
};
@@ -850,14 +852,21 @@ where
850852
dest.layout.size
851853
});
852854
assert_eq!(src.meta, dest.meta, "Can only copy between equally-sized instances");
855+
856+
let src = self.check_mplace_access(src, Some(size))?;
857+
let dest = self.check_mplace_access(dest, Some(size))?;
858+
let (src_ptr, dest_ptr) = match (src, dest) {
859+
(Some(src_ptr), Some(dest_ptr)) => (src_ptr, dest_ptr),
860+
(None, None) => return Ok(()), // zero-sized copy
861+
_ => bug!("The pointers should both be Some or both None"),
862+
};
863+
853864
self.memory.copy(
854-
src.ptr, src.align,
855-
dest.ptr, dest.align,
865+
src_ptr,
866+
dest_ptr,
856867
size,
857868
/*nonoverlapping*/ true,
858-
)?;
859-
860-
Ok(())
869+
)
861870
}
862871

863872
/// Copies the data from an operand to a place. The layouts may disagree, but they must

‎src/librustc_mir/interpret/step.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
209209
let dest = self.force_allocation(dest)?;
210210
let length = dest.len(self)?;
211211

212-
if length > 0 {
213-
// write the first
212+
if let Some(first_ptr) = self.check_mplace_access(dest, None)? {
213+
// Write the first.
214214
let first = self.mplace_field(dest, 0)?;
215215
self.copy_op(op, first.into())?;
216216

217217
if length > 1 {
218-
// copy the rest
219-
let (dest, dest_align) = first.to_scalar_ptr_align();
220-
let rest = dest.ptr_offset(first.layout.size, self)?;
218+
let elem_size = first.layout.size;
219+
// Copy the rest. This is performance-sensitive code
220+
// for big static/const arrays!
221+
let rest_ptr = first_ptr.offset(elem_size, self)?;
221222
self.memory.copy_repeatedly(
222-
dest, dest_align, rest, dest_align, first.layout.size, length - 1, true
223+
first_ptr, rest_ptr, elem_size, length - 1, /*nonoverlapping:*/true
223224
)?;
224225
}
225226
}

‎src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
426426
}
427427
None => {
428428
// Unsized self.
429-
args[0].to_mem_place()
429+
args[0].assert_mem_place()
430430
}
431431
};
432432
// Find and consult vtable

‎src/librustc_mir/interpret/validity.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,16 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
440440
}
441441
}
442442
}
443-
// Check if we have encountered this pointer+layout combination
444-
// before. Proceed recursively even for ZST, no
445-
// reason to skip them! E.g., `!` is a ZST and we want to validate it.
443+
// Proceed recursively even for ZST, no reason to skip them!
444+
// `!` is a ZST and we want to validate it.
445+
// Normalize before handing `place` to tracking because that will
446+
// check for duplicates.
447+
let place = if size.bytes() > 0 {
448+
self.ecx.force_mplace_ptr(place)
449+
.expect("we already bounds-checked")
450+
} else {
451+
place
452+
};
446453
let path = &self.path;
447454
ref_tracking.track(place, || {
448455
// We need to clone the path anyway, make sure it gets created
@@ -548,7 +555,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
548555
) -> InterpResult<'tcx> {
549556
match op.layout.ty.sty {
550557
ty::Str => {
551-
let mplace = op.to_mem_place(); // strings are never immediate
558+
let mplace = op.assert_mem_place(); // strings are never immediate
552559
try_validation!(self.ecx.read_str(mplace),
553560
"uninitialized or non-UTF-8 data in str", self.path);
554561
}
@@ -565,7 +572,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
565572
return Ok(());
566573
}
567574
// non-ZST array cannot be immediate, slices are never immediate
568-
let mplace = op.to_mem_place();
575+
let mplace = op.assert_mem_place();
569576
// This is the length of the array/slice.
570577
let len = mplace.len(self.ecx)?;
571578
// zero length slices have nothing to be checked
@@ -576,7 +583,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
576583
let ty_size = self.ecx.layout_of(tys)?.size;
577584
// This is the size in bytes of the whole array.
578585
let size = ty_size * len;
579-
586+
// Size is not 0, get a pointer.
580587
let ptr = self.ecx.force_ptr(mplace.ptr)?;
581588

582589
// NOTE: Keep this in sync with the handling of integer and float
@@ -633,7 +640,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
633640
/// `ref_tracking_for_consts` can be `None` to avoid recursive checking below references.
634641
/// This also toggles between "run-time" (no recursion) and "compile-time" (with recursion)
635642
/// validation (e.g., pointer values are fine in integers at runtime) and various other const
636-
/// specific validation checks
643+
/// specific validation checks.
637644
pub fn validate_operand(
638645
&self,
639646
op: OpTy<'tcx, M::PointerTag>,
@@ -652,6 +659,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
652659
ecx: self,
653660
};
654661

662+
// Try to cast to ptr *once* instead of all the time.
663+
let op = self.force_op_ptr(op).unwrap_or(op);
664+
655665
// Run it
656666
visitor.visit_value(op)
657667
}

‎src/librustc_mir/interpret/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ macro_rules! make_value_visitor {
242242
match v.layout().ty.sty {
243243
ty::Dynamic(..) => {
244244
// immediate trait objects are not a thing
245-
let dest = v.to_op(self.ecx())?.to_mem_place();
245+
let dest = v.to_op(self.ecx())?.assert_mem_place();
246246
let inner = self.ecx().unpack_dyn_trait(dest)?.1;
247247
trace!("walk_value: dyn object layout: {:#?}", inner.layout);
248248
// recurse with the inner type
@@ -316,7 +316,7 @@ macro_rules! make_value_visitor {
316316
MPlaceTy::dangling(v.layout(), self.ecx())
317317
} else {
318318
// non-ZST array/slice/str cannot be immediate
319-
v.to_op(self.ecx())?.to_mem_place()
319+
v.to_op(self.ecx())?.assert_mem_place()
320320
};
321321
// Now we can go over all the fields.
322322
let iter = self.ecx().mplace_array_fields(mplace)?

0 commit comments

Comments
 (0)
Please sign in to comment.