Skip to content

Commit 7b08855

Browse files
committed
InterpCx::size_and_align_of to InterpCx::mem_pos_of
1 parent ae85682 commit 7b08855

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::hir::def_id::DefId;
77
use rustc::hir::def::DefKind;
88
use rustc::mir;
99
use rustc::ty::layout::{
10-
self, Size, Align, HasDataLayout, LayoutOf, TyLayout
10+
self, Size, MemoryPosition, HasDataLayout, LayoutOf, TyLayout
1111
};
1212
use rustc::ty::subst::SubstsRef;
1313
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
@@ -369,13 +369,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
369369
/// Returns the actual dynamic size and alignment of the place at the given type.
370370
/// Only the "meta" (metadata) part of the place matters.
371371
/// This can fail to provide an answer for extern types.
372-
pub(super) fn size_and_align_of(
372+
pub(super) fn mem_pos_of(
373373
&self,
374374
metadata: Option<Scalar<M::PointerTag>>,
375375
layout: TyLayout<'tcx>,
376-
) -> InterpResult<'tcx, Option<(Size, Align)>> {
376+
) -> InterpResult<'tcx, Option<MemoryPosition>> {
377377
if !layout.is_unsized() {
378-
return Ok(Some((layout.pref_pos.size, layout.pref_pos.align.abi)));
378+
return Ok(Some(layout.pref_pos.mem_pos()));
379379
}
380380
match layout.ty.kind {
381381
ty::Adt(..) | ty::Tuple(..) => {
@@ -399,7 +399,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
399399
// the last field). Can't have foreign types here, how would we
400400
// adjust alignment and size for them?
401401
let field = layout.field(self, layout.fields.count() - 1)?;
402-
let (unsized_size, unsized_align) = match self.size_and_align_of(metadata, field)? {
402+
let unsized_mem_pos = match self.mem_pos_of(metadata, field)? {
403403
Some(size_and_align) => size_and_align,
404404
None => {
405405
// A field with extern type. If this field is at offset 0, we behave
@@ -415,6 +415,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
415415
}
416416
};
417417

418+
let unsized_size = unsized_mem_pos.size;
419+
let unsized_align = unsized_mem_pos.align;
420+
418421
// FIXME (#26403, #27023): We should be adding padding
419422
// to `sized_size` (to accommodate the `unsized_align`
420423
// required of the unsized field that follows) before
@@ -438,12 +441,16 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
438441
throw_ub_format!("wide pointer metadata contains invalid information: \
439442
total size is bigger than largest supported object");
440443
}
441-
Ok(Some((size, align)))
442-
}
444+
let mem_pos = MemoryPosition::new(size, align).strided();
445+
446+
Ok(Some(mem_pos))
447+
},
443448
ty::Dynamic(..) => {
444449
let vtable = metadata.expect("dyn trait fat ptr must have vtable");
445450
// Read size and align from vtable (already checks size).
446-
Ok(Some(self.read_size_and_align_from_vtable(vtable)?))
451+
let (size, align) = self.read_size_and_align_from_vtable(vtable)?;
452+
let mem_pos = MemoryPosition::new(size, align);
453+
Ok(Some(mem_pos))
447454
}
448455

449456
ty::Slice(_) | ty::Str => {
@@ -454,22 +461,22 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
454461
let pref_pos = elem.pref_pos.checked_mul(len, &*self.tcx)
455462
.ok_or_else(|| err_ub_format!("invalid slice: \
456463
total size is bigger than largest supported object"))?;
457-
Ok(Some((pref_pos.size, pref_pos.align.abi)))
464+
Ok(Some(pref_pos.mem_pos()))
458465
}
459466

460467
ty::Foreign(_) => {
461468
Ok(None)
462469
}
463470

464-
_ => bug!("size_and_align_of::<{:?}> not supported", layout.ty),
471+
_ => bug!("mem_pos_of::<{:?}> not supported", layout.ty),
465472
}
466473
}
467474
#[inline]
468-
pub fn size_and_align_of_mplace(
475+
pub fn mem_pos_of_mplace(
469476
&self,
470477
mplace: MPlaceTy<'tcx, M::PointerTag>
471-
) -> InterpResult<'tcx, Option<(Size, Align)>> {
472-
self.size_and_align_of(mplace.meta, mplace.layout)
478+
) -> InterpResult<'tcx, Option<MemoryPosition>> {
479+
self.mem_pos_of(mplace.meta, mplace.layout)
473480
}
474481

475482
pub fn push_stack_frame(

src/librustc_mir/interpret/place.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ where
346346
&self,
347347
mut place: MPlaceTy<'tcx, M::PointerTag>,
348348
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
349-
let (size, align) = self.size_and_align_of_mplace(place)?
350-
.unwrap_or((place.layout.pref_pos.size, place.layout.pref_pos.align.abi));
349+
let mem_pos = self.mem_pos_of_mplace(place)?
350+
.unwrap_or(place.layout.pref_pos.mem_pos());
351+
let (size, align) = (mem_pos.size, mem_pos.align);
351352
assert!(place.mplace.align <= align, "dynamic alignment less strict than static one?");
352353
place.mplace.align = align; // maximally strict checking
353354
// When dereferencing a pointer, it must be non-NULL, aligned, and live.
@@ -407,8 +408,8 @@ where
407408
// Re-use parent metadata to determine dynamic field layout.
408409
// With custom DSTS, this *will* execute user-defined code, but the same
409410
// happens at run-time so that's okay.
410-
let align = match self.size_and_align_of(base.meta, field_layout)? {
411-
Some((_, align)) => align,
411+
let align = match self.mem_pos_of(base.meta, field_layout)? {
412+
Some(mem_pos) => mem_pos.align,
412413
None if offset == Size::ZERO =>
413414
// An extern type at offset 0, we fall back to its static alignment.
414415
// FIXME: Once we have made decisions for how to handle size and alignment
@@ -989,8 +990,9 @@ where
989990
// that has different alignment than the outer field.
990991
// We also need to support unsized types, and hence cannot use `allocate`.
991992
let local_layout = self.layout_of_local(&self.stack[frame], local, None)?;
992-
let (size, align) = self.size_and_align_of(meta, local_layout)?
993+
let mem_pos = self.mem_pos_of(meta, local_layout)?
993994
.expect("Cannot allocate for non-dyn-sized type");
995+
let (size, align) = (mem_pos.size, mem_pos.align);
994996
let ptr = self.memory.allocate(size, align, MemoryKind::Stack);
995997
let mplace = MemPlace { ptr: ptr.into(), align, meta };
996998
if let Some(value) = old_val {

src/librustc_mir/interpret/validity.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,25 +405,24 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
405405
self.check_wide_ptr_meta(place.meta, place.layout)?;
406406
}
407407
// Make sure this is dereferencable and all.
408-
let (size, align) = self.ecx.size_and_align_of(place.meta, place.layout)?
408+
let mem_pos = self.ecx.mem_pos_of(place.meta, place.layout)?
409409
// for the purpose of validity, consider foreign types to have
410410
// alignment and size determined by the layout (size will be 0,
411411
// alignment should take attributes into account).
412-
.unwrap_or_else(|| (place.layout.pref_pos.size,
413-
place.layout.pref_pos.align.abi));
412+
.unwrap_or_else(|| place.layout.pref_pos.mem_pos());
414413
let ptr: Option<_> = match
415414
self.ecx.memory.check_ptr_access_align(
416415
place.ptr,
417-
size,
418-
Some(align),
416+
mem_pos.size,
417+
Some(mem_pos.align),
419418
CheckInAllocMsg::InboundsTest,
420419
)
421420
{
422421
Ok(ptr) => ptr,
423422
Err(err) => {
424423
info!(
425424
"{:?} did not pass access check for size {:?}, align {:?}",
426-
place.ptr, size, align
425+
place.ptr, mem_pos.size, mem_pos.align
427426
);
428427
match err.kind {
429428
err_unsup!(InvalidNullPointerUsage) =>
@@ -464,7 +463,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
464463
// `!` is a ZST and we want to validate it.
465464
// Normalize before handing `place` to tracking because that will
466465
// check for duplicates.
467-
let place = if size.bytes() > 0 {
466+
let place = if mem_pos.size.bytes() > 0 {
468467
self.ecx.force_mplace_ptr(place)
469468
.expect("we already bounds-checked")
470469
} else {

0 commit comments

Comments
 (0)