Skip to content

Commit 6ad2bed

Browse files
committed
Mem.check_ptr_access
1 parent 7c41628 commit 6ad2bed

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
309309
pub fn check_ptr_access(
310310
&self,
311311
sptr: Scalar<M::PointerTag>,
312-
size: Size,
313-
align: Align,
312+
mem_pos: MemoryPosition,
314313
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
315-
let align = if M::CHECK_ALIGN { Some(align) } else { None };
316-
self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest)
314+
let align = if M::CHECK_ALIGN { Some(mem_pos.align) } else { None };
315+
self.check_ptr_access_align(sptr, mem_pos.size, align, CheckInAllocMsg::MemoryAccessTest)
317316
}
318317

319318
/// Like `check_ptr_access`, but *definitely* checks alignment when `align`
@@ -776,7 +775,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
776775
ptr: Scalar<M::PointerTag>,
777776
size: Size,
778777
) -> InterpResult<'tcx, &[u8]> {
779-
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
778+
let mem_pos = MemoryPosition::new(size, Align::from_bytes(1).unwrap());
779+
let ptr = match self.check_ptr_access(ptr, mem_pos)? {
780780
Some(ptr) => ptr,
781781
None => return Ok(&[]), // zero-sized access
782782
};
@@ -803,7 +803,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
803803
let src = src.into_iter();
804804
let size = Size::from_bytes(src.size_hint().0 as u64);
805805
// `write_bytes` checks that this lower bound matches the upper bound matches reality.
806-
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
806+
let mem_pos = MemoryPosition::new(size, Align::from_bytes(1).unwrap());
807+
let ptr = match self.check_ptr_access(ptr, mem_pos)? {
807808
Some(ptr) => ptr,
808809
None => return Ok(()), // zero-sized access
809810
};

src/librustc_mir/interpret/place.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::mir;
99
use rustc::mir::interpret::truncate;
1010
use rustc::ty::{self, Ty};
1111
use rustc::ty::layout::{
12-
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
12+
self, Size, Align, MemoryPosition, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
1313
};
1414
use rustc::ty::TypeFoldable;
1515

@@ -332,12 +332,14 @@ where
332332
place: MPlaceTy<'tcx, M::PointerTag>,
333333
size: Option<Size>,
334334
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
335-
let size = size.unwrap_or_else(|| {
335+
let mem_pos = if let Some(size) = size {
336+
MemoryPosition::new(size, place.align)
337+
} else {
336338
assert!(!place.layout.is_unsized());
337339
assert!(place.meta.is_none());
338-
place.layout.pref_pos.size
339-
});
340-
self.memory.check_ptr_access(place.ptr, size, place.align)
340+
place.layout.pref_pos.mem_pos()
341+
};
342+
self.memory.check_ptr_access(place.ptr, mem_pos)
341343
}
342344

343345
/// Return the "access-checked" version of this `MPlace`, where for non-ZST

src/librustc_mir/interpret/terminator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
422422
// cannot use the shim here, because that will only result in infinite recursion
423423
ty::InstanceDef::Virtual(_, idx) => {
424424
let mut args = args.to_vec();
425-
let ptr_pos = self.pointer_pos();
425+
let ptr_pos = self.pointer_pos().mem_pos();
426426
// We have to implement all "object safe receivers". Currently we
427427
// support built-in pointers (&, &mut, Box) as well as unsized-self. We do
428428
// not yet support custom self types.
@@ -442,8 +442,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
442442
let vtable_slot = vtable.ptr_offset((ptr_pos * (idx as u64 + 3)).size, self)?;
443443
let vtable_slot = self.memory.check_ptr_access(
444444
vtable_slot,
445-
ptr_pos.size,
446-
ptr_pos.align.abi,
445+
ptr_pos,
447446
)?.expect("cannot be a ZST");
448447
let fn_ptr = self.memory.get_raw(vtable_slot.alloc_id)?
449448
.read_ptr_sized(self, vtable_slot)?.not_undef()?;

src/librustc_mir/interpret/traits.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
106106
// we don't care about the pointee type, we just want a pointer
107107
let vtable = self.memory.check_ptr_access(
108108
vtable,
109-
self.tcx.data_layout.pointer_pos.size,
110-
self.tcx.data_layout.pointer_pos.align.abi,
109+
self.tcx.data_layout.pointer_pos.mem_pos(),
111110
)?.expect("cannot be a ZST");
112111
let drop_fn = self.memory
113112
.get_raw(vtable.alloc_id)?
@@ -128,13 +127,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
128127
&self,
129128
vtable: Scalar<M::PointerTag>,
130129
) -> InterpResult<'tcx, (Size, Align)> {
131-
let ptr_pos = self.pointer_pos();
130+
let ptr_pos = self.pointer_pos().mem_pos();
132131
// We check for size = 3*ptr_size, that covers the drop fn (unused here),
133132
// the size, and the align (which we read below).
134133
let vtable = self.memory.check_ptr_access(
135134
vtable,
136-
(3 * ptr_pos).size,
137-
self.tcx.data_layout.pointer_pos.align.abi,
135+
3 * ptr_pos,
138136
)?.expect("cannot be a ZST");
139137
let alloc = self.memory.get_raw(vtable.alloc_id)?;
140138
let size = alloc.read_ptr_sized(

src/librustc_mir/interpret/validity.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
269269
try_validation!(
270270
self.ecx.memory.check_ptr_access(
271271
vtable,
272-
3*self.ecx.tcx.data_layout.pointer_pos.size, // drop, size, align
273-
self.ecx.tcx.data_layout.pointer_pos.align.abi,
272+
3 * self.ecx.tcx.data_layout.pointer_pos.mem_pos(), // drop, size, align
274273
),
275274
"dangling or unaligned vtable pointer in wide pointer or too small vtable",
276275
self.path

0 commit comments

Comments
 (0)