Skip to content

Commit ec04283

Browse files
committed
avoid into_pointer_or_addr and into_parts in visit_freeze_sensitive
1 parent 976a413 commit ec04283

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

src/helpers.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
271271
/// Visits the memory covered by `place`, sensitive to freezing: the 2nd parameter
272272
/// of `action` will be true if this is frozen, false if this is in an `UnsafeCell`.
273273
/// The range is relative to `place`.
274-
///
275-
/// Assumes that the `place` has a proper pointer in it.
276274
fn visit_freeze_sensitive(
277275
&self,
278276
place: &MPlaceTy<'tcx, Tag>,
@@ -290,33 +288,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
290288
// Store how far we proceeded into the place so far. Everything to the left of
291289
// this offset has already been handled, in the sense that the frozen parts
292290
// have had `action` called on them.
293-
let ptr = place.ptr.into_pointer_or_addr().unwrap();
294-
let start_offset = ptr.into_parts().1 as Size; // we just compare offsets, the abs. value never matters
295-
let mut cur_offset = start_offset;
291+
let start_addr = place.ptr.addr();
292+
let mut cur_addr = start_addr;
296293
// Called when we detected an `UnsafeCell` at the given offset and size.
297294
// Calls `action` and advances `cur_ptr`.
298-
let mut unsafe_cell_action = |unsafe_cell_ptr: Pointer<Option<Tag>>,
295+
let mut unsafe_cell_action = |unsafe_cell_ptr: &Pointer<Option<Tag>>,
299296
unsafe_cell_size: Size| {
300-
let unsafe_cell_ptr = unsafe_cell_ptr.into_pointer_or_addr().unwrap();
301-
debug_assert_eq!(unsafe_cell_ptr.provenance, ptr.provenance);
302297
// We assume that we are given the fields in increasing offset order,
303298
// and nothing else changes.
304-
let unsafe_cell_offset = unsafe_cell_ptr.into_parts().1 as Size; // we just compare offsets, the abs. value never matters
305-
assert!(unsafe_cell_offset >= cur_offset);
306-
let frozen_size = unsafe_cell_offset - cur_offset;
299+
let unsafe_cell_addr = unsafe_cell_ptr.addr();
300+
assert!(unsafe_cell_addr >= cur_addr);
301+
let frozen_size = unsafe_cell_addr - cur_addr;
307302
// Everything between the cur_ptr and this `UnsafeCell` is frozen.
308303
if frozen_size != Size::ZERO {
309-
action(alloc_range(cur_offset - start_offset, frozen_size), /*frozen*/ true)?;
304+
action(alloc_range(cur_addr - start_addr, frozen_size), /*frozen*/ true)?;
310305
}
311-
cur_offset += frozen_size;
306+
cur_addr += frozen_size;
312307
// This `UnsafeCell` is NOT frozen.
313308
if unsafe_cell_size != Size::ZERO {
314309
action(
315-
alloc_range(cur_offset - start_offset, unsafe_cell_size),
310+
alloc_range(cur_addr - start_addr, unsafe_cell_size),
316311
/*frozen*/ false,
317312
)?;
318313
}
319-
cur_offset += unsafe_cell_size;
314+
cur_addr += unsafe_cell_size;
320315
// Done
321316
Ok(())
322317
};
@@ -334,7 +329,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
334329
.unwrap_or_else(|| place.layout.size);
335330
// Now handle this `UnsafeCell`, unless it is empty.
336331
if unsafe_cell_size != Size::ZERO {
337-
unsafe_cell_action(place.ptr, unsafe_cell_size)
332+
unsafe_cell_action(&place.ptr, unsafe_cell_size)
338333
} else {
339334
Ok(())
340335
}
@@ -344,7 +339,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
344339
}
345340
// The part between the end_ptr and the end of the place is also frozen.
346341
// So pretend there is a 0-sized `UnsafeCell` at the end.
347-
unsafe_cell_action(place.ptr.wrapping_offset(size, this), Size::ZERO)?;
342+
unsafe_cell_action(&place.ptr.offset(size, this)?, Size::ZERO)?;
348343
// Done!
349344
return Ok(());
350345

@@ -428,9 +423,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
428423
let mut places =
429424
fields.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
430425
// we just compare offsets, the abs. value never matters
431-
places.sort_by_key(|place| {
432-
place.ptr.into_pointer_or_addr().unwrap().into_parts().1 as Size
433-
});
426+
places.sort_by_key(|place| place.ptr.addr());
434427
self.walk_aggregate(place, places.into_iter().map(Ok))
435428
}
436429
FieldsShape::Union { .. } | FieldsShape::Primitive => {
@@ -777,6 +770,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
777770
/// Mark a machine allocation that was just created as immutable.
778771
fn mark_immutable(&mut self, mplace: &MemPlace<Tag>) {
779772
let this = self.eval_context_mut();
773+
// This got just allocated, so there definitely is a pointer here.
780774
this.alloc_mark_immutable(mplace.ptr.into_pointer_or_addr().unwrap().provenance.alloc_id)
781775
.unwrap();
782776
}

src/intptrcast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'mir, 'tcx> GlobalStateInner {
128128

129129
/// Convert a relative (tcx) pointer to an absolute address.
130130
pub fn rel_ptr_to_addr(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<AllocId>) -> u64 {
131-
let (alloc_id, offset) = ptr.into_parts(); // offset is relative
131+
let (alloc_id, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
132132
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
133133

134134
// Add offset with the right kind of pointer-overflowing arithmetic.
@@ -137,7 +137,7 @@ impl<'mir, 'tcx> GlobalStateInner {
137137
}
138138

139139
pub fn abs_ptr_to_rel(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<Tag>) -> Size {
140-
let (tag, addr) = ptr.into_parts(); // addr is absolute
140+
let (tag, addr) = ptr.into_parts(); // addr is absolute (Tag provenance)
141141
let base_addr = GlobalStateInner::alloc_base_addr(ecx, tag.alloc_id);
142142

143143
// Wrapping "addr - base_addr"

src/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
360360
name: &str,
361361
ptr: Pointer<Option<Tag>>,
362362
) {
363+
// This got just allocated, so there definitely is a pointer here.
363364
let ptr = ptr.into_pointer_or_addr().unwrap();
364365
this.machine.extern_statics.try_insert(Symbol::intern(name), ptr).unwrap();
365366
}

src/shims/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8383
}
8484

8585
let ptr = this.read_pointer(ptr_op)?;
86-
if let Ok(ptr) = ptr.into_pointer_or_addr() {
86+
if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) {
8787
// Only do anything if we can identify the allocation this goes to.
88-
let (_, cur_align) =
89-
this.get_alloc_size_and_align(ptr.provenance.alloc_id, AllocCheck::MaybeDead)?;
88+
let (_, cur_align) = this.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)?;
9089
if cur_align.bytes() >= req_align {
9190
// If the allocation alignment is at least the required alignment we use the
9291
// real implementation.

0 commit comments

Comments
 (0)