Migrate to strict and exposed provenance

This commit is contained in:
bakaq
2025-04-26 23:34:53 -03:00
committed by Mark Thom
parent cb9b988905
commit a9847eef65
11 changed files with 69 additions and 73 deletions

View File

@@ -611,12 +611,12 @@ mod tests {
let mut wam = MockWAM::new(); let mut wam = MockWAM::new();
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
let const_value = HeapCellValue::from(ConsPtr::build_with( let const_value = HeapCellValue::from(ConsPtr::build_with(
0x0000_0431 as *const _, std::ptr::without_provenance(0x0000_0431),
ConsPtrMaskTag::Cons, ConsPtrMaskTag::Cons,
)); ));
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
let const_value = HeapCellValue::from(ConsPtr::build_with( let const_value = HeapCellValue::from(ConsPtr::build_with(
0x0000_5555_ff00_0431 as *const _, std::ptr::without_provenance(0x0000_5555_ff00_0431),
ConsPtrMaskTag::Cons, ConsPtrMaskTag::Cons,
)); ));

View File

@@ -516,12 +516,12 @@ impl AtomTable {
} }
}; };
let ptr_base = block_epoch.block.base as usize; let ptr_base = block_epoch.block.base.addr();
write_to_ptr(string, len_ptr); write_to_ptr(string, len_ptr);
let atom = AtomCell::new() let atom = AtomCell::new()
.with_name((STRINGS.len() + len_ptr as usize - ptr_base) as u64) .with_name((STRINGS.len() + len_ptr.addr() - ptr_base) as u64)
.with_arity(0) .with_arity(0)
.with_f(false) .with_f(false)
.with_m(false) .with_m(false)

View File

@@ -517,7 +517,7 @@ impl Value {
fn as_ptr(&mut self) -> Result<*mut c_void, FFIError> { fn as_ptr(&mut self) -> Result<*mut c_void, FFIError> {
match self { match self {
Value::CString(ref mut cstr) => Ok(&mut *cstr as *mut _ as *mut c_void), Value::CString(ref mut cstr) => Ok(&mut *cstr as *mut _ as *mut c_void),
Value::Int(n) => Ok(*n as *mut c_void), Value::Int(n) => Ok(std::ptr::with_exposed_provenance_mut(*n as usize)),
_ => Err(FFIError::ValueCast), _ => Err(FFIError::ValueCast),
} }
} }

View File

@@ -984,7 +984,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
#[inline] #[inline]
fn print_raw_ptr(&mut self, ptr: *const ArenaHeader) { fn print_raw_ptr(&mut self, ptr: *const ArenaHeader) {
append_str!(self, &format!("0x{:x}", ptr as *const u8 as usize)); append_str!(self, &format!("0x{:x}", ptr.addr()));
} }
fn print_number(&mut self, max_depth: usize, n: NumberFocus, op: &Option<DirectedOp>) { fn print_number(&mut self, max_depth: usize, n: NumberFocus, op: &Option<DirectedOp>) {

View File

@@ -97,7 +97,7 @@ unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan {
.unwrap_or(heap_slice.len()); .unwrap_or(heap_slice.len());
let zero_byte_addr = heap_slice.as_ptr().add(string_len); let zero_byte_addr = heap_slice.as_ptr().add(string_len);
let sentinel_len = pstr_sentinel_length(zero_byte_addr as usize); let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr());
let tail_idx = cell_index!( let tail_idx = cell_index!(
(string_len + sentinel_len).next_multiple_of(ALIGN) (string_len + sentinel_len).next_multiple_of(ALIGN)
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 } + if sentinel_len <= 1 { heap_index!(1) } else { 0 }

View File

@@ -297,7 +297,7 @@ impl Term {
Term::atom(alias.as_str().to_string()) Term::atom(alias.as_str().to_string())
} else { } else {
Term::compound("$stream", [ Term::compound("$stream", [
Term::integer(stream.as_ptr() as usize) Term::integer(stream.as_ptr().addr())
]) ])
}; };
term_stack.push(stream_term); term_stack.push(stream_term);

View File

@@ -59,9 +59,10 @@ impl Index<usize> for AndFrame {
unsafe { unsafe {
let ptr = self as *const crate::machine::stack::AndFrame as *const u8; 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)
} }
} }
} }
@@ -72,10 +73,11 @@ impl IndexMut<usize> for AndFrame {
let index_offset = (index - 1) * mem::size_of::<HeapCellValue>(); let index_offset = (index - 1) * mem::size_of::<HeapCellValue>();
unsafe { unsafe {
let ptr = self as *mut crate::machine::stack::AndFrame as *const u8; let ptr = self as *mut crate::machine::stack::AndFrame as *mut u8;
let ptr = ptr as usize + prelude_offset + index_offset;
&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)
} }
} }
} }
@@ -85,20 +87,14 @@ impl Index<usize> for Stack {
#[inline] #[inline]
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
unsafe { unsafe { &*self.buf.base.add(index).cast() }
let ptr = self.buf.base as usize + index;
&*(ptr as *const HeapCellValue)
}
} }
} }
impl IndexMut<usize> for Stack { impl IndexMut<usize> for Stack {
#[inline] #[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output { fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe { unsafe { &mut *self.buf.base.add(index).cast_mut().cast() }
let ptr = self.buf.base as usize + index;
&mut *(ptr as *mut HeapCellValue)
}
} }
} }
@@ -132,9 +128,10 @@ impl Index<usize> for OrFrame {
unsafe { unsafe {
let ptr = self as *const crate::machine::stack::OrFrame as *const u8; 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)
} }
} }
} }
@@ -146,10 +143,11 @@ impl IndexMut<usize> for OrFrame {
let index_offset = index * mem::size_of::<HeapCellValue>(); let index_offset = index * mem::size_of::<HeapCellValue>();
unsafe { unsafe {
let ptr = self as *mut crate::machine::stack::OrFrame as *const u8; let ptr = self as *mut crate::machine::stack::OrFrame as *mut u8;
let ptr = ptr as usize + prelude_offset + index_offset;
&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)
} }
} }
} }
@@ -187,15 +185,19 @@ impl Stack {
let frame_size = AndFrame::size_of(num_cells); let frame_size = AndFrame::size_of(num_cells);
unsafe { 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 new_ptr = self.alloc(frame_size);
let mut offset = prelude_size::<AndFramePrelude>(); let mut offset = prelude_size::<AndFramePrelude>();
for idx in 0..num_cells { for idx in 0..num_cells {
ptr::write( let cell_ptr = new_ptr.add(offset) as *mut HeapCellValue;
new_ptr.add(offset) as *mut HeapCellValue, ptr::write(cell_ptr, stack_loc_as_cell!(AndFrame, e, idx + 1));
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>(); offset += mem::size_of::<HeapCellValue>();
} }
@@ -208,22 +210,26 @@ impl Stack {
} }
pub(crate) fn top(&self) -> usize { 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 { pub(crate) fn allocate_or_frame(&mut self, num_cells: usize) -> usize {
let frame_size = OrFrame::size_of(num_cells); let frame_size = OrFrame::size_of(num_cells);
unsafe { 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 new_ptr = self.alloc(frame_size);
let mut offset = prelude_size::<OrFramePrelude>(); let mut offset = prelude_size::<OrFramePrelude>();
for idx in 0..num_cells { for idx in 0..num_cells {
ptr::write( let cell_ptr = new_ptr.byte_add(offset) as *mut HeapCellValue;
new_ptr.byte_add(offset) as *mut HeapCellValue, ptr::write(cell_ptr, stack_loc_as_cell!(OrFrame, b, idx));
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>(); offset += mem::size_of::<HeapCellValue>();
} }
@@ -237,10 +243,7 @@ impl Stack {
#[inline(always)] #[inline(always)]
pub(crate) fn index_and_frame(&self, e: usize) -> &AndFrame { pub(crate) fn index_and_frame(&self, e: usize) -> &AndFrame {
unsafe { unsafe { &*self.buf.base.add(e).cast() }
let ptr = self.buf.base as usize + e;
&*(ptr as *const AndFrame)
}
} }
#[inline(always)] #[inline(always)]
@@ -254,26 +257,20 @@ impl Stack {
#[inline(always)] #[inline(always)]
pub(crate) fn index_or_frame(&self, b: usize) -> &OrFrame { pub(crate) fn index_or_frame(&self, b: usize) -> &OrFrame {
unsafe { unsafe { &*self.buf.base.add(b).cast() }
let ptr = self.buf.base as usize + b;
&*(ptr as *const OrFrame)
}
} }
#[inline(always)] #[inline(always)]
pub(crate) fn index_or_frame_mut(&mut self, b: usize) -> &mut OrFrame { pub(crate) fn index_or_frame_mut(&mut self, b: usize) -> &mut OrFrame {
unsafe { unsafe { &mut *self.buf.base.add(b).cast_mut().cast() }
let ptr = self.buf.base as usize + b;
&mut *(ptr as *mut OrFrame)
}
} }
#[inline(always)] #[inline(always)]
pub(crate) fn truncate(&mut self, b: usize) { 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 { if base < (*self.buf.ptr.get_mut()) {
*self.buf.ptr.get_mut() = base as *mut _; *self.buf.ptr.get_mut() = base.cast_mut();
} }
} }
} }

View File

@@ -146,11 +146,11 @@ macro_rules! typed_arena_ptr_as_cell {
macro_rules! raw_ptr_as_cell { macro_rules! raw_ptr_as_cell {
($ptr:expr) => {{ ($ptr:expr) => {{
// Cell is 64-bit, but raw ptr is 32-bit in 32-bit systems // Cell is 64-bit, but raw ptr is 32-bit in 32-bit systems
// TODO use <*{const,mut} _>::addr instead of as when the strict_provenance feature is stable rust-lang/rust#95228 let ptr: *const _ = $ptr;
// we might need <*{const,mut} _>::expose_provenance for strict provenance, depending on how we recreate a pointer later // This needs to expose provenance because it needs to be turned back into a pointer
let ptr : *const _ = $ptr; // in contexts where there is no available provenance locally. For example, in
debug_assert!(!$ptr.is_null()); // `ConsPtr::as_ptr`.
HeapCellValue::from_ptr_addr(ptr as usize) HeapCellValue::from_ptr_addr(ptr.expose_provenance())
}}; }};
} }

View File

@@ -141,9 +141,8 @@ where
ptr::write(ptr as *mut T, value); ptr::write(ptr as *mut T, value);
let value = <OffsetTableImpl<T> as OffsetTable>::Offset::from( let value =
ptr as usize - block_epoch.base as usize, <OffsetTableImpl<T> as OffsetTable>::Offset::from(ptr.addr() - block_epoch.base.addr());
);
// AtomTable would have to update the index table at this point // AtomTable would have to update the index table at this point
// explicit drop to ensure we don't accidentally drop it early // explicit drop to ensure we don't accidentally drop it early
@@ -270,7 +269,7 @@ where
#[inline(always)] #[inline(always)]
pub fn as_offset(&self) -> <OffsetTableImpl<T> as OffsetTable>::Offset { pub fn as_offset(&self) -> <OffsetTableImpl<T> as OffsetTable>::Offset {
<OffsetTableImpl<T> as OffsetTable>::Offset::from( <OffsetTableImpl<T> as OffsetTable>::Offset::from(
self.0.get() as usize - RcuRef::get_root(&self.0).base as usize, self.0.get().addr() - RcuRef::get_root(&self.0).base.addr(),
) )
} }
} }

View File

@@ -66,8 +66,8 @@ impl<T: RawBlockTraits> RawBlock<T> {
false false
} else { } else {
self.base = new_base; self.base = new_base;
self.top = (self.base as usize + size * 2) as *const _; self.top = self.base.add(size * 2);
*self.ptr.get_mut() = (self.base as usize + size) as *mut _; *self.ptr.get_mut() = self.base.add(size).cast_mut();
true true
} }
} }
@@ -83,7 +83,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
// allocation failed // allocation failed
None None
} else { } else {
let allocated = (*self.ptr.get()) as usize - self.base as usize; let allocated = (*self.ptr.get()).addr() - self.base.addr();
self.base.copy_to(new_block.base.cast_mut(), allocated); self.base.copy_to(new_block.base.cast_mut(), allocated);
*new_block.ptr.get_mut() = new_block.base.add(allocated).cast_mut(); *new_block.ptr.get_mut() = new_block.base.add(allocated).cast_mut();
Some(new_block) Some(new_block)
@@ -93,7 +93,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
#[inline] #[inline]
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
self.top as usize - self.base as usize self.top.addr() - self.base.addr()
} }
#[inline(always)] #[inline(always)]
@@ -105,7 +105,7 @@ impl<T: RawBlockTraits> RawBlock<T> {
self.base self.base
); );
self.top as usize - (*self.ptr.get()) as usize self.top.addr() - (*self.ptr.get()).addr()
} }
pub unsafe fn alloc(&self, size: usize) -> *mut u8 { pub unsafe fn alloc(&self, size: usize) -> *mut u8 {

View File

@@ -93,7 +93,7 @@ impl ConsPtr {
#[inline(always)] #[inline(always)]
pub fn build_with(ptr: *const ArenaHeader, tag: ConsPtrMaskTag) -> Self { pub fn build_with(ptr: *const ArenaHeader, tag: ConsPtrMaskTag) -> Self {
ConsPtr::new() ConsPtr::new()
.with_ptr(ptr as *const u8 as u64) .with_ptr(ptr.expose_provenance() as u64)
.with_f(false) .with_f(false)
.with_m(false) .with_m(false)
.with_tag(tag) .with_tag(tag)
@@ -102,7 +102,7 @@ impl ConsPtr {
#[inline(always)] #[inline(always)]
pub fn as_ptr(self) -> *mut u8 { pub fn as_ptr(self) -> *mut u8 {
let addr: u64 = self.ptr(); let addr: u64 = self.ptr();
addr as usize as *mut _ std::ptr::with_exposed_provenance_mut(addr as usize)
} }
#[inline(always)] #[inline(always)]
@@ -377,7 +377,7 @@ where
{ {
#[inline] #[inline]
fn from(arena_ptr: TypedArenaPtr<T>) -> HeapCellValue { 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)
} }
} }
@@ -402,7 +402,7 @@ impl From<ConsPtr> for HeapCellValue {
#[inline(always)] #[inline(always)]
fn from(cons_ptr: ConsPtr) -> HeapCellValue { fn from(cons_ptr: ConsPtr) -> HeapCellValue {
HeapCellValue::from_bytes( 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_tag(ConsPtrMaskTag::Cons)
.with_m(false) .with_m(false)
.into_bytes(), .into_bytes(),
@@ -724,14 +724,14 @@ const_assert!(mem::size_of::<UntypedArenaPtr>() == 8);
impl From<*const ArenaHeader> for UntypedArenaPtr { impl From<*const ArenaHeader> for UntypedArenaPtr {
#[inline] #[inline]
fn from(ptr: *const ArenaHeader) -> UntypedArenaPtr { fn from(ptr: *const ArenaHeader) -> UntypedArenaPtr {
UntypedArenaPtr::build_with(ptr as usize) UntypedArenaPtr::build_with(ptr.expose_provenance())
} }
} }
impl From<*const IndexPtr> for UntypedArenaPtr { impl From<*const IndexPtr> for UntypedArenaPtr {
#[inline] #[inline]
fn from(ptr: *const IndexPtr) -> UntypedArenaPtr { fn from(ptr: *const IndexPtr) -> UntypedArenaPtr {
UntypedArenaPtr::build_with(ptr as usize) UntypedArenaPtr::build_with(ptr.expose_provenance())
} }
} }
@@ -751,7 +751,7 @@ impl UntypedArenaPtr {
#[inline] #[inline]
pub fn get_ptr(self) -> *const u8 { pub fn get_ptr(self) -> *const u8 {
let addr: u64 = self.ptr(); let addr: u64 = self.ptr();
addr as usize as *const u8 std::ptr::with_exposed_provenance(addr as usize)
} }
#[inline] #[inline]