unsafe {
let ptr = self as *const crate::machine::stack::AndFrame as *const u8;
- let ptr = ptr as usize + prelude_offset + index_offset;
- &*(ptr as *const HeapCellValue)
+ // This address falls outside the provenance for self, therefore we have to get it
+ // from exposed provenance.
+ &*std::ptr::with_exposed_provenance(ptr.addr() + prelude_offset + index_offset)
}
}
}
let index_offset = (index - 1) * mem::size_of::<HeapCellValue>();
unsafe {
- let ptr = self as *mut crate::machine::stack::AndFrame as *const u8;
- let ptr = ptr as usize + prelude_offset + index_offset;
+ let ptr = self as *mut crate::machine::stack::AndFrame as *mut u8;
- &mut *(ptr as *mut HeapCellValue)
+ // This address falls outside the provenance for self, therefore we have to get it
+ // from exposed provenance.
+ &mut *std::ptr::with_exposed_provenance_mut(ptr.addr() + prelude_offset + index_offset)
}
}
}
#[inline]
fn index(&self, index: usize) -> &Self::Output {
- unsafe {
- let ptr = self.buf.base as usize + index;
- &*(ptr as *const HeapCellValue)
- }
+ unsafe { &*self.buf.base.add(index).cast() }
}
}
impl IndexMut<usize> for Stack {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
- unsafe {
- let ptr = self.buf.base as usize + index;
- &mut *(ptr as *mut HeapCellValue)
- }
+ unsafe { &mut *self.buf.base.add(index).cast_mut().cast() }
}
}
unsafe {
let ptr = self as *const crate::machine::stack::OrFrame as *const u8;
- let ptr = ptr as usize + prelude_offset + index_offset;
- &*(ptr as *const HeapCellValue)
+ // This address falls outside the provenance for self, therefore we have to get it
+ // from exposed provenance.
+ &*std::ptr::with_exposed_provenance(ptr.addr() + prelude_offset + index_offset)
}
}
}
let index_offset = index * mem::size_of::<HeapCellValue>();
unsafe {
- let ptr = self as *mut crate::machine::stack::OrFrame as *const u8;
- let ptr = ptr as usize + prelude_offset + index_offset;
+ let ptr = self as *mut crate::machine::stack::OrFrame as *mut u8;
- &mut *(ptr as *mut HeapCellValue)
+ // This address falls outside the provenance for self, therefore we have to get it
+ // from exposed provenance.
+ &mut *std::ptr::with_exposed_provenance_mut(ptr.addr() + prelude_offset + index_offset)
}
}
}
let frame_size = AndFrame::size_of(num_cells);
unsafe {
- let e = (*self.buf.ptr.get_mut()) as usize - self.buf.base as usize;
+ let e = (*self.buf.ptr.get_mut()).addr() - self.buf.base.addr();
let new_ptr = self.alloc(frame_size);
let mut offset = prelude_size::<AndFramePrelude>();
for idx in 0..num_cells {
- ptr::write(
- new_ptr.add(offset) as *mut HeapCellValue,
- stack_loc_as_cell!(AndFrame, e, idx + 1),
- );
+ let cell_ptr = new_ptr.add(offset) as *mut HeapCellValue;
+ ptr::write(cell_ptr, stack_loc_as_cell!(AndFrame, e, idx + 1));
+
+ // Because in the Index and IndexMut inplementations we need to get this from
+ // exposed provenance, we need to expose the provenance here, even though we don't
+ // actually use the value for anything. This is a reminder that `expose_provenance`
+ // isn't just a cast from a pointer to an integer but has actual side effects.
+ cell_ptr.expose_provenance();
offset += mem::size_of::<HeapCellValue>();
}
}
pub(crate) fn top(&self) -> usize {
- unsafe { (*self.buf.ptr.get()) as usize - self.buf.base as usize }
+ unsafe { (*self.buf.ptr.get()).addr() - self.buf.base.addr() }
}
pub(crate) fn allocate_or_frame(&mut self, num_cells: usize) -> usize {
let frame_size = OrFrame::size_of(num_cells);
unsafe {
- let b = (*self.buf.ptr.get_mut()) as usize - self.buf.base as usize;
+ let b = (*self.buf.ptr.get_mut()).addr() - self.buf.base.addr();
let new_ptr = self.alloc(frame_size);
let mut offset = prelude_size::<OrFramePrelude>();
for idx in 0..num_cells {
- ptr::write(
- new_ptr.byte_add(offset) as *mut HeapCellValue,
- stack_loc_as_cell!(OrFrame, b, idx),
- );
+ let cell_ptr = new_ptr.byte_add(offset) as *mut HeapCellValue;
+ ptr::write(cell_ptr, stack_loc_as_cell!(OrFrame, b, idx));
+
+ // Because in the Index and IndexMut inplementations we need to get this from
+ // exposed provenance, we need to expose the provenance here, even though we don't
+ // actually use the value for anything. This is a reminder that `expose_provenance`
+ // isn't just a cast from a pointer to an integer but has actual side effects.
+ cell_ptr.expose_provenance();
offset += mem::size_of::<HeapCellValue>();
}
#[inline(always)]
pub(crate) fn index_and_frame(&self, e: usize) -> &AndFrame {
- unsafe {
- let ptr = self.buf.base as usize + e;
- &*(ptr as *const AndFrame)
- }
+ unsafe { &*self.buf.base.add(e).cast() }
}
#[inline(always)]
#[inline(always)]
pub(crate) fn index_or_frame(&self, b: usize) -> &OrFrame {
- unsafe {
- let ptr = self.buf.base as usize + b;
- &*(ptr as *const OrFrame)
- }
+ unsafe { &*self.buf.base.add(b).cast() }
}
#[inline(always)]
pub(crate) fn index_or_frame_mut(&mut self, b: usize) -> &mut OrFrame {
- unsafe {
- let ptr = self.buf.base as usize + b;
- &mut *(ptr as *mut OrFrame)
- }
+ unsafe { &mut *self.buf.base.add(b).cast_mut().cast() }
}
#[inline(always)]
pub(crate) fn truncate(&mut self, b: usize) {
- let base = self.buf.base as usize + b;
+ let base = unsafe { self.buf.base.add(b) };
- if base < (*self.buf.ptr.get_mut()) as usize {
- *self.buf.ptr.get_mut() = base as *mut _;
+ if base < (*self.buf.ptr.get_mut()) {
+ *self.buf.ptr.get_mut() = base.cast_mut();
}
}
}
#[inline(always)]
pub fn build_with(ptr: *const ArenaHeader, tag: ConsPtrMaskTag) -> Self {
ConsPtr::new()
- .with_ptr(ptr as *const u8 as u64)
+ .with_ptr(ptr.expose_provenance() as u64)
.with_f(false)
.with_m(false)
.with_tag(tag)
#[inline(always)]
pub fn as_ptr(self) -> *mut u8 {
let addr: u64 = self.ptr();
- addr as usize as *mut _
+ std::ptr::with_exposed_provenance_mut(addr as usize)
}
#[inline(always)]
{
#[inline]
fn from(arena_ptr: TypedArenaPtr<T>) -> HeapCellValue {
- HeapCellValue::from(arena_ptr.header_ptr() as u64)
+ HeapCellValue::from(arena_ptr.header_ptr().expose_provenance() as u64)
}
}
#[inline(always)]
fn from(cons_ptr: ConsPtr) -> HeapCellValue {
HeapCellValue::from_bytes(
- ConsPtr::from(cons_ptr.as_ptr() as u64)
+ ConsPtr::from(cons_ptr.as_ptr().expose_provenance() as u64)
.with_tag(ConsPtrMaskTag::Cons)
.with_m(false)
.into_bytes(),
impl From<*const ArenaHeader> for UntypedArenaPtr {
#[inline]
fn from(ptr: *const ArenaHeader) -> UntypedArenaPtr {
- UntypedArenaPtr::build_with(ptr as usize)
+ UntypedArenaPtr::build_with(ptr.expose_provenance())
}
}
impl From<*const IndexPtr> for UntypedArenaPtr {
#[inline]
fn from(ptr: *const IndexPtr) -> UntypedArenaPtr {
- UntypedArenaPtr::build_with(ptr as usize)
+ UntypedArenaPtr::build_with(ptr.expose_provenance())
}
}
#[inline]
pub fn get_ptr(self) -> *const u8 {
let addr: u64 = self.ptr();
- addr as usize as *const u8
+ std::ptr::with_exposed_provenance(addr as usize)
}
#[inline]