Skip to content

Commit 15ec8f7

Browse files
committed
rename Memory::get methods to get_raw to indicate their unchecked nature
1 parent c34472b commit 15ec8f7

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,
@@ -480,7 +480,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
480480
).0)
481481
}
482482

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

516-
pub fn get_mut(
518+
/// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
519+
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCtx` instead!
520+
pub fn get_raw_mut(
517521
&mut self,
518522
id: AllocId,
519523
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
@@ -555,7 +559,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
555559
liveness: AllocCheck,
556560
) -> InterpResult<'static, (Size, Align)> {
557561
// # Regular allocations
558-
// Don't use `self.get` here as that will
562+
// Don't use `self.get_raw` here as that will
559563
// a) cause cycles in case `id` refers to a static
560564
// b) duplicate a static's allocation in miri
561565
if let Some((_, alloc)) = self.alloc_map.get(id) {
@@ -627,7 +631,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
627631
}
628632

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

@@ -776,15 +780,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
776780
Some(ptr) => ptr,
777781
None => return Ok(&[]), // zero-sized access
778782
};
779-
self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
783+
self.get_raw(ptr.alloc_id)?.get_bytes(self, ptr, size)
780784
}
781785

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

790794
/// Writes the given stream of bytes into memory.
@@ -804,7 +808,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
804808
None => return Ok(()), // zero-sized access
805809
};
806810
let tcx = self.tcx.tcx;
807-
self.get_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)
811+
self.get_raw_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)
808812
}
809813

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

838842
let tcx = self.tcx.tcx;
839843

840844
// This checks relocation edges on the src.
841-
let src_bytes = self.get(src.alloc_id)?
845+
let src_bytes = self.get_raw(src.alloc_id)?
842846
.get_bytes_with_undef_and_ptr(&tcx, src, size)?
843847
.as_ptr();
844-
let dest_bytes = self.get_mut(dest.alloc_id)?
848+
let dest_bytes = self.get_raw_mut(dest.alloc_id)?
845849
.get_bytes_mut(&tcx, dest, size * length)?
846850
.as_mut_ptr();
847851

@@ -880,7 +884,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
880884
// copy definedness to the destination
881885
self.copy_undef_mask(src, dest, size, length)?;
882886
// copy the relocations to the destination
883-
self.get_mut(dest.alloc_id)?.mark_relocation_range(relocations);
887+
self.get_raw_mut(dest.alloc_id)?.mark_relocation_range(relocations);
884888

885889
Ok(())
886890
}
@@ -899,11 +903,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
899903
// The bits have to be saved locally before writing to dest in case src and dest overlap.
900904
assert_eq!(size.bytes() as usize as u64, size.bytes());
901905

902-
let src_alloc = self.get(src.alloc_id)?;
906+
let src_alloc = self.get_raw(src.alloc_id)?;
903907
let compressed = src_alloc.compress_undef_range(src, size);
904908

905909
// now fill in all the data
906-
let dest_allocation = self.get_mut(dest.alloc_id)?;
910+
let dest_allocation = self.get_raw_mut(dest.alloc_id)?;
907911
dest_allocation.mark_compressed_undef_range(&compressed, dest, size, repeat);
908912

909913
Ok(())

src/librustc_mir/interpret/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
248248
match mplace.layout.abi {
249249
layout::Abi::Scalar(..) => {
250250
let scalar = self.memory
251-
.get(ptr.alloc_id)?
251+
.get_raw(ptr.alloc_id)?
252252
.read_scalar(self, ptr, mplace.layout.size)?;
253253
Ok(Some(ImmTy {
254254
imm: scalar.into(),
@@ -266,10 +266,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
266266
assert!(b_offset.bytes() > 0); // we later use the offset to tell apart the fields
267267
let b_ptr = ptr.offset(b_offset, self)?;
268268
let a_val = self.memory
269-
.get(ptr.alloc_id)?
269+
.get_raw(ptr.alloc_id)?
270270
.read_scalar(self, a_ptr, a_size)?;
271271
let b_val = self.memory
272-
.get(ptr.alloc_id)?
272+
.get_raw(ptr.alloc_id)?
273273
.read_scalar(self, b_ptr, b_size)?;
274274
Ok(Some(ImmTy {
275275
imm: Immediate::ScalarPair(a_val, b_val),

src/librustc_mir/interpret/place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ where
808808
_ => bug!("write_immediate_to_mplace: invalid Scalar layout: {:#?}",
809809
dest.layout)
810810
}
811-
self.memory.get_mut(ptr.alloc_id)?.write_scalar(
811+
self.memory.get_raw_mut(ptr.alloc_id)?.write_scalar(
812812
tcx, ptr, scalar, dest.layout.size
813813
)
814814
}
@@ -830,10 +830,10 @@ where
830830
// fields do not match the `ScalarPair` components.
831831

832832
self.memory
833-
.get_mut(ptr.alloc_id)?
833+
.get_raw_mut(ptr.alloc_id)?
834834
.write_scalar(tcx, ptr, a_val, a_size)?;
835835
self.memory
836-
.get_mut(b_ptr.alloc_id)?
836+
.get_raw_mut(b_ptr.alloc_id)?
837837
.write_scalar(tcx, b_ptr, b_val, b_size)
838838
}
839839
}

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
@@ -586,6 +586,8 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
586586
_ => false,
587587
}
588588
} => {
589+
// Optimized handling for arrays of integer/float type.
590+
589591
// bailing out for zsts is ok, since the array element type can only be int/float
590592
if op.layout.is_zst() {
591593
return Ok(());
@@ -605,6 +607,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
605607
// Size is not 0, get a pointer.
606608
let ptr = self.ecx.force_ptr(mplace.ptr)?;
607609

610+
// This is the optimization: we just check the entire range at once.
608611
// NOTE: Keep this in sync with the handling of integer and float
609612
// types above, in `visit_primitive`.
610613
// In run-time mode, we accept pointers in here. This is actually more
@@ -614,7 +617,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
614617
// to reject those pointers, we just do not have the machinery to
615618
// talk about parts of a pointer.
616619
// We also accept undef, for consistency with the slow path.
617-
match self.ecx.memory.get(ptr.alloc_id)?.check_bytes(
620+
match self.ecx.memory.get_raw(ptr.alloc_id)?.check_bytes(
618621
self.ecx,
619622
ptr,
620623
size,

0 commit comments

Comments
 (0)