Skip to content

Commit 0aee0dd

Browse files
committed
rename Memory::get methods to get_raw to indicate their unchecked nature
1 parent 87cbf0a commit 0aee0dd

File tree

8 files changed

+46
-43
lines changed

8 files changed

+46
-43
lines changed

src/librustc_mir/interpret/intrinsics/caller_location.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3737
let col_out = self.force_ptr(self.mplace_field(location, 2)?.ptr)?;
3838

3939
let layout = &self.tcx.data_layout;
40-
let alloc = self.memory.get_mut(file_ptr_out.alloc_id)?;
40+
// We just allocated this, so we can skip the bounds checks.
41+
let alloc = self.memory.get_raw_mut(file_ptr_out.alloc_id)?;
4142

4243
alloc.write_scalar(layout, file_ptr_out, file.into(), ptr_size)?;
4344
alloc.write_scalar(layout, file_len_out, file_len.into(), ptr_size)?;

src/librustc_mir/interpret/memory.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
210210
let new_ptr = self.allocate(new_size, new_align, kind);
211211
let old_size = match old_size_and_align {
212212
Some((size, _align)) => size,
213-
None => self.get(ptr.alloc_id)?.size,
213+
None => self.get_raw(ptr.alloc_id)?.size,
214214
};
215215
self.copy(
216216
ptr,
@@ -478,7 +478,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
478478
).0)
479479
}
480480

481-
pub fn get(
481+
/// Gives raw access to the `Allocation`, without bounds or alignment checks.
482+
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCtx` instead!
483+
pub fn get_raw(
482484
&self,
483485
id: AllocId,
484486
) -> InterpResult<'tcx, &Allocation<M::PointerTag, M::AllocExtra>> {
@@ -511,7 +513,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
511513
}
512514
}
513515

514-
pub fn get_mut(
516+
/// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
517+
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCtx` instead!
518+
pub fn get_raw_mut(
515519
&mut self,
516520
id: AllocId,
517521
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
@@ -553,7 +557,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
553557
liveness: AllocCheck,
554558
) -> InterpResult<'static, (Size, Align)> {
555559
// # Regular allocations
556-
// Don't use `self.get` here as that will
560+
// Don't use `self.get_raw` here as that will
557561
// a) cause cycles in case `id` refers to a static
558562
// b) duplicate a static's allocation in miri
559563
if let Some((_, alloc)) = self.alloc_map.get(id) {
@@ -625,7 +629,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
625629
}
626630

627631
pub fn mark_immutable(&mut self, id: AllocId) -> InterpResult<'tcx> {
628-
self.get_mut(id)?.mutability = Mutability::Immutable;
632+
self.get_raw_mut(id)?.mutability = Mutability::Immutable;
629633
Ok(())
630634
}
631635

@@ -774,15 +778,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
774778
Some(ptr) => ptr,
775779
None => return Ok(&[]), // zero-sized access
776780
};
777-
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
781+
self.get_raw(ptr.alloc_id)?.get_bytes(self, ptr, size)
778782
}
779783

780784
/// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice.
781785
///
782786
/// Performs appropriate bounds checks.
783787
pub fn read_c_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, &[u8]> {
784788
let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we *need* a ptr.
785-
self.get(ptr.alloc_id)?.read_c_str(self, ptr)
789+
self.get_raw(ptr.alloc_id)?.read_c_str(self, ptr)
786790
}
787791

788792
/// Writes the given stream of bytes into memory.
@@ -802,7 +806,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
802806
None => return Ok(()), // zero-sized access
803807
};
804808
let tcx = self.tcx.tcx;
805-
self.get_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)
809+
self.get_raw_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)
806810
}
807811

808812
/// Expects the caller to have checked bounds and alignment.
@@ -830,16 +834,16 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
830834
// since we don't want to keep any relocations at the target.
831835
// (`get_bytes_with_undef_and_ptr` below checks that there are no
832836
// relocations overlapping the edges; those would not be handled correctly).
833-
let relocations = self.get(src.alloc_id)?
837+
let relocations = self.get_raw(src.alloc_id)?
834838
.prepare_relocation_copy(self, src, size, dest, length);
835839

836840
let tcx = self.tcx.tcx;
837841

838842
// This checks relocation edges on the src.
839-
let src_bytes = self.get(src.alloc_id)?
843+
let src_bytes = self.get_raw(src.alloc_id)?
840844
.get_bytes_with_undef_and_ptr(&tcx, src, size)?
841845
.as_ptr();
842-
let dest_bytes = self.get_mut(dest.alloc_id)?
846+
let dest_bytes = self.get_raw_mut(dest.alloc_id)?
843847
.get_bytes_mut(&tcx, dest, size * length)?
844848
.as_mut_ptr();
845849

@@ -878,7 +882,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
878882
// copy definedness to the destination
879883
self.copy_undef_mask(src, dest, size, length)?;
880884
// copy the relocations to the destination
881-
self.get_mut(dest.alloc_id)?.mark_relocation_range(relocations);
885+
self.get_raw_mut(dest.alloc_id)?.mark_relocation_range(relocations);
882886

883887
Ok(())
884888
}
@@ -897,11 +901,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
897901
// The bits have to be saved locally before writing to dest in case src and dest overlap.
898902
assert_eq!(size.bytes() as usize as u64, size.bytes());
899903

900-
let src_alloc = self.get(src.alloc_id)?;
904+
let src_alloc = self.get_raw(src.alloc_id)?;
901905
let compressed = src_alloc.compress_undef_range(src, size);
902906

903907
// now fill in all the data
904-
let dest_allocation = self.get_mut(dest.alloc_id)?;
908+
let dest_allocation = self.get_raw_mut(dest.alloc_id)?;
905909
dest_allocation.mark_compressed_undef_range(&compressed, dest, size, repeat);
906910

907911
Ok(())

src/librustc_mir/interpret/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
268268
match mplace.layout.abi {
269269
layout::Abi::Scalar(..) => {
270270
let scalar = self.memory
271-
.get(ptr.alloc_id)?
271+
.get_raw(ptr.alloc_id)?
272272
.read_scalar(self, ptr, mplace.layout.size)?;
273273
Ok(Some(ImmTy {
274274
imm: scalar.into(),
@@ -286,10 +286,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
286286
assert!(b_offset.bytes() > 0); // we later use the offset to tell apart the fields
287287
let b_ptr = ptr.offset(b_offset, self)?;
288288
let a_val = self.memory
289-
.get(ptr.alloc_id)?
289+
.get_raw(ptr.alloc_id)?
290290
.read_scalar(self, a_ptr, a_size)?;
291291
let b_val = self.memory
292-
.get(ptr.alloc_id)?
292+
.get_raw(ptr.alloc_id)?
293293
.read_scalar(self, b_ptr, b_size)?;
294294
Ok(Some(ImmTy {
295295
imm: Immediate::ScalarPair(a_val, b_val),

src/librustc_mir/interpret/place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ where
802802
_ => bug!("write_immediate_to_mplace: invalid Scalar layout: {:#?}",
803803
dest.layout)
804804
}
805-
self.memory.get_mut(ptr.alloc_id)?.write_scalar(
805+
self.memory.get_raw_mut(ptr.alloc_id)?.write_scalar(
806806
tcx, ptr, scalar, dest.layout.size
807807
)
808808
}
@@ -824,10 +824,10 @@ where
824824
// fields do not match the `ScalarPair` components.
825825

826826
self.memory
827-
.get_mut(ptr.alloc_id)?
827+
.get_raw_mut(ptr.alloc_id)?
828828
.write_scalar(tcx, ptr, a_val, a_size)?;
829829
self.memory
830-
.get_mut(b_ptr.alloc_id)?
830+
.get_raw_mut(b_ptr.alloc_id)?
831831
.write_scalar(tcx, b_ptr, b_val, b_size)
832832
}
833833
}

src/librustc_mir/interpret/snapshot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'b, 'mir, 'tcx> SnapshotContext<'b>
392392
for Memory<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>
393393
{
394394
fn resolve(&'b self, id: &AllocId) -> Option<&'b Allocation> {
395-
self.get(*id).ok()
395+
self.get_raw(*id).ok()
396396
}
397397
}
398398

src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
445445
ptr_size,
446446
self.tcx.data_layout.pointer_align.abi,
447447
)?.expect("cannot be a ZST");
448-
let fn_ptr = self.memory.get(vtable_slot.alloc_id)?
448+
let fn_ptr = self.memory.get_raw(vtable_slot.alloc_id)?
449449
.read_ptr_sized(self, vtable_slot)?.not_undef()?;
450450
let drop_fn = self.memory.get_fn(fn_ptr)?;
451451

src/librustc_mir/interpret/traits.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,30 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6363
let drop = Instance::resolve_drop_in_place(*tcx, ty);
6464
let drop = self.memory.create_fn_alloc(FnVal::Instance(drop));
6565

66-
// no need to do any alignment checks on the memory accesses below, because we know the
66+
// No need to do any alignment checks on the memory accesses below, because we know the
6767
// allocation is correctly aligned as we created it above. Also we're only offsetting by
6868
// multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
69-
self.memory
70-
.get_mut(vtable.alloc_id)?
71-
.write_ptr_sized(tcx, vtable, Scalar::Ptr(drop).into())?;
72-
73-
let size_ptr = vtable.offset(ptr_size, self)?;
74-
self.memory
75-
.get_mut(size_ptr.alloc_id)?
76-
.write_ptr_sized(tcx, size_ptr, Scalar::from_uint(size, ptr_size).into())?;
77-
let align_ptr = vtable.offset(ptr_size * 2, self)?;
78-
self.memory
79-
.get_mut(align_ptr.alloc_id)?
80-
.write_ptr_sized(tcx, align_ptr, Scalar::from_uint(align, ptr_size).into())?;
69+
let vtable_alloc = self.memory.get_raw_mut(vtable.alloc_id)?;
70+
vtable_alloc.write_ptr_sized(tcx, vtable, Scalar::Ptr(drop).into())?;
71+
72+
let size_ptr = vtable.offset(ptr_size, tcx)?;
73+
vtable_alloc.write_ptr_sized(tcx, size_ptr, Scalar::from_uint(size, ptr_size).into())?;
74+
let align_ptr = vtable.offset(ptr_size * 2, tcx)?;
75+
vtable_alloc.write_ptr_sized(tcx, align_ptr, Scalar::from_uint(align, ptr_size).into())?;
8176

8277
for (i, method) in methods.iter().enumerate() {
8378
if let Some((def_id, substs)) = *method {
8479
// resolve for vtable: insert shims where needed
8580
let instance = ty::Instance::resolve_for_vtable(
86-
*self.tcx,
81+
*tcx,
8782
self.param_env,
8883
def_id,
8984
substs,
9085
).ok_or_else(|| err_inval!(TooGeneric))?;
9186
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
92-
let method_ptr = vtable.offset(ptr_size * (3 + i as u64), self)?;
93-
self.memory
94-
.get_mut(method_ptr.alloc_id)?
87+
// We cannot use `vtable_allic` as we are creating fn ptrs in this loop.
88+
let method_ptr = vtable.offset(ptr_size * (3 + i as u64), tcx)?;
89+
self.memory.get_raw_mut(vtable.alloc_id)?
9590
.write_ptr_sized(tcx, method_ptr, Scalar::Ptr(fn_ptr).into())?;
9691
}
9792
}
@@ -114,7 +109,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
114109
self.tcx.data_layout.pointer_align.abi,
115110
)?.expect("cannot be a ZST");
116111
let drop_fn = self.memory
117-
.get(vtable.alloc_id)?
112+
.get_raw(vtable.alloc_id)?
118113
.read_ptr_sized(self, vtable)?
119114
.not_undef()?;
120115
// We *need* an instance here, no other kind of function value, to be able
@@ -140,7 +135,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
140135
3*pointer_size,
141136
self.tcx.data_layout.pointer_align.abi,
142137
)?.expect("cannot be a ZST");
143-
let alloc = self.memory.get(vtable.alloc_id)?;
138+
let alloc = self.memory.get_raw(vtable.alloc_id)?;
144139
let size = alloc.read_ptr_sized(
145140
self,
146141
vtable.offset(pointer_size, self)?

src/librustc_mir/interpret/validity.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
595595
_ => false,
596596
}
597597
} => {
598+
// Optimized handling for arrays of integer/float type.
599+
598600
// bailing out for zsts is ok, since the array element type can only be int/float
599601
if op.layout.is_zst() {
600602
return Ok(());
@@ -614,6 +616,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
614616
// Size is not 0, get a pointer.
615617
let ptr = self.ecx.force_ptr(mplace.ptr)?;
616618

619+
// This is the optimization: we just check the entire range at once.
617620
// NOTE: Keep this in sync with the handling of integer and float
618621
// types above, in `visit_primitive`.
619622
// In run-time mode, we accept pointers in here. This is actually more
@@ -623,7 +626,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
623626
// to reject those pointers, we just do not have the machinery to
624627
// talk about parts of a pointer.
625628
// We also accept undef, for consistency with the type-based checks.
626-
match self.ecx.memory.get(ptr.alloc_id)?.check_bytes(
629+
match self.ecx.memory.get_raw(ptr.alloc_id)?.check_bytes(
627630
self.ecx,
628631
ptr,
629632
size,

0 commit comments

Comments
 (0)