Skip to content

Commit 563ab4a

Browse files
committed
add Align::ONE; add methods to access alloc.extra
1 parent 74995c4 commit 563ab4a

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<Tag> Allocation<Tag> {
114114
}
115115

116116
pub fn from_byte_aligned_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>) -> Self {
117-
Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
117+
Allocation::from_bytes(slice, Align::ONE)
118118
}
119119

120120
pub fn uninit(size: Size, align: Align) -> Self {

compiler/rustc_mir/src/interpret/intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty;
1414
use rustc_middle::ty::subst::SubstsRef;
1515
use rustc_middle::ty::{Ty, TyCtxt};
1616
use rustc_span::symbol::{sym, Symbol};
17-
use rustc_target::abi::{Abi, LayoutOf as _, Primitive, Size};
17+
use rustc_target::abi::{Abi, Align, LayoutOf as _, Primitive, Size};
1818

1919
use super::{
2020
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
@@ -525,7 +525,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
525525
self.memory.check_ptr_access_align(
526526
min_ptr,
527527
Size::from_bytes(size),
528-
None,
528+
Align::ONE,
529529
CheckInAllocMsg::PointerArithmeticTest,
530530
)?;
531531
Ok(offset_ptr)

compiler/rustc_mir/src/interpret/memory.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
264264
Some((size, _align)) => size,
265265
None => self.get_raw(ptr.alloc_id)?.size(),
266266
};
267-
let align = Align::from_bytes(1).unwrap();
268267
// This will also call the access hooks.
269268
self.copy(
270269
ptr.into(),
271-
align,
270+
Align::ONE,
272271
new_ptr.into(),
273-
align,
272+
Align::ONE,
274273
old_size.min(new_size),
275274
/*nonoverlapping*/ true,
276275
)?;
@@ -379,10 +378,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
379378
&self,
380379
sptr: Scalar<M::PointerTag>,
381380
size: Size,
382-
align: Option<Align>,
381+
align: Align,
383382
msg: CheckInAllocMsg,
384383
) -> InterpResult<'tcx> {
385-
self.check_and_deref_ptr(sptr, size, align, msg, |ptr| {
384+
self.check_and_deref_ptr(sptr, size, Some(align), msg, |ptr| {
386385
let (size, align) =
387386
self.get_size_and_align(ptr.alloc_id, AllocCheck::Dereferenceable)?;
388387
Ok((size, align, ()))
@@ -604,6 +603,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
604603
}
605604
}
606605

606+
/// Return the `extra` field of the given allocation.
607+
pub fn get_alloc_extra<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, &'a M::AllocExtra> {
608+
Ok(&self.get_raw(id)?.extra)
609+
}
610+
607611
/// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
608612
/// The caller is responsible for calling the access hooks!
609613
///
@@ -664,6 +668,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
664668
}
665669
}
666670

671+
/// Return the `extra` field of the given allocation.
672+
pub fn get_alloc_extra_mut<'a>(
673+
&'a mut self,
674+
id: AllocId,
675+
) -> InterpResult<'tcx, &'a mut M::AllocExtra> {
676+
Ok(&mut self.get_raw_mut(id)?.0.extra)
677+
}
678+
667679
/// Obtain the size and alignment of an allocation, even if that allocation has
668680
/// been deallocated.
669681
///
@@ -688,7 +700,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
688700
// The caller requested no function pointers.
689701
throw_ub!(DerefFunctionPointer(id))
690702
} else {
691-
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
703+
Ok((Size::ZERO, Align::ONE))
692704
};
693705
}
694706

@@ -930,7 +942,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
930942
///
931943
/// Performs appropriate bounds checks.
932944
pub fn read_bytes(&self, sptr: Scalar<M::PointerTag>, size: Size) -> InterpResult<'tcx, &[u8]> {
933-
let alloc_ref = match self.get(sptr, size, Align::from_bytes(1).unwrap())? {
945+
let alloc_ref = match self.get(sptr, size, Align::ONE)? {
934946
Some(a) => a,
935947
None => return Ok(&[]), // zero-sized access
936948
};
@@ -956,7 +968,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
956968
assert_eq!(lower, len, "can only write iterators with a precise length");
957969

958970
let size = Size::from_bytes(len);
959-
let alloc_ref = match self.get_mut(sptr, size, Align::from_bytes(1).unwrap())? {
971+
let alloc_ref = match self.get_mut(sptr, size, Align::ONE)? {
960972
Some(alloc_ref) => alloc_ref,
961973
None => {
962974
// zero-sized access

compiler/rustc_mir/src/interpret/place.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1031,11 +1031,8 @@ where
10311031
) -> MPlaceTy<'tcx, M::PointerTag> {
10321032
let ptr = self.memory.allocate_bytes(str.as_bytes(), kind);
10331033
let meta = Scalar::from_machine_usize(u64::try_from(str.len()).unwrap(), self);
1034-
let mplace = MemPlace {
1035-
ptr: ptr.into(),
1036-
align: Align::from_bytes(1).unwrap(),
1037-
meta: MemPlaceMeta::Meta(meta),
1038-
};
1034+
let mplace =
1035+
MemPlace { ptr: ptr.into(), align: Align::ONE, meta: MemPlaceMeta::Meta(meta) };
10391036

10401037
let layout = self.layout_of(self.tcx.mk_static_str()).unwrap();
10411038
MPlaceTy { mplace, layout }

compiler/rustc_mir/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
329329
self.ecx.memory.check_ptr_access_align(
330330
vtable,
331331
3 * self.ecx.tcx.data_layout.pointer_size, // drop, size, align
332-
Some(self.ecx.tcx.data_layout.pointer_align.abi),
332+
self.ecx.tcx.data_layout.pointer_align.abi,
333333
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
334334
),
335335
self.path,
@@ -415,7 +415,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
415415
self.ecx.memory.check_ptr_access_align(
416416
place.ptr,
417417
size,
418-
Some(align),
418+
align,
419419
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
420420
),
421421
self.path,

compiler/rustc_target/src/abi/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ pub struct Align {
441441
}
442442

443443
impl Align {
444+
pub const ONE: Align = Align { pow2: 0 };
445+
444446
#[inline]
445447
pub fn from_bits(bits: u64) -> Result<Align, String> {
446448
Align::from_bytes(Size::from_bits(bits).bytes())
@@ -450,7 +452,7 @@ impl Align {
450452
pub fn from_bytes(align: u64) -> Result<Align, String> {
451453
// Treat an alignment of 0 bytes like 1-byte alignment.
452454
if align == 0 {
453-
return Ok(Align { pow2: 0 });
455+
return Ok(Align::ONE);
454456
}
455457

456458
#[cold]

0 commit comments

Comments
 (0)