extend allocbytes with associated type

This commit is contained in:
Nia Espera 2025-05-24 23:18:32 +02:00
parent aa57e46e24
commit e388a3e405
No known key found for this signature in database
GPG key ID: E7A3AAA3B692EAD7
16 changed files with 81 additions and 34 deletions

View file

@ -139,7 +139,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
AllocKind::LiveData => {
if memory_kind == MiriMemoryKind::Global.into() {
// For new global allocations, we always pre-allocate the memory to be able use the machine address directly.
let prepared_bytes = MiriAllocBytes::zeroed(info.size, info.align)
let prepared_bytes = MiriAllocBytes::zeroed(info.size, info.align, ())
.unwrap_or_else(|| {
panic!("Miri ran out of memory: cannot create allocation of {size:?} bytes", size = info.size)
});
@ -159,7 +159,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
AllocKind::Function | AllocKind::VTable => {
// Allocate some dummy memory to get a unique address for this function/vtable.
let alloc_bytes =
MiriAllocBytes::from_bytes(&[0u8; 1], Align::from_bytes(1).unwrap());
MiriAllocBytes::from_bytes(&[0u8; 1], Align::from_bytes(1).unwrap(), ());
let ptr = alloc_bytes.as_ptr();
// Leak the underlying memory to ensure it remains unique.
std::mem::forget(alloc_bytes);
@ -429,7 +429,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
prepared_alloc_bytes.copy_from_slice(bytes);
interp_ok(prepared_alloc_bytes)
} else {
interp_ok(MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align))
interp_ok(MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align, ()))
}
}

View file

@ -24,7 +24,7 @@ impl Clone for MiriAllocBytes {
fn clone(&self) -> Self {
let bytes: Cow<'_, [u8]> = Cow::Borrowed(self);
let align = Align::from_bytes(self.layout.align().to_u64()).unwrap();
MiriAllocBytes::from_bytes(bytes, align)
MiriAllocBytes::from_bytes(bytes, align, ())
}
}
@ -86,7 +86,10 @@ impl MiriAllocBytes {
}
impl AllocBytes for MiriAllocBytes {
fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
/// Placeholder!
type AllocParams = ();
fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align, _params: ()) -> Self {
let slice = slice.into();
let size = slice.len();
let align = align.bytes();
@ -102,7 +105,7 @@ impl AllocBytes for MiriAllocBytes {
alloc_bytes
}
fn zeroed(size: Size, align: Align) -> Option<Self> {
fn zeroed(size: Size, align: Align, _params: ()) -> Option<Self> {
let size = size.bytes();
let align = align.bytes();
// SAFETY: `alloc_fn` will only be used with `size != 0`.

View file

@ -899,7 +899,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let mut alloc = alloc.inner().adjust_from_tcx(
&this.tcx,
|bytes, align| {
interp_ok(MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align))
interp_ok(MiriAllocBytes::from_bytes(std::borrow::Cow::Borrowed(bytes), align, ()))
},
|ptr| this.global_root_pointer(ptr),
)?;

View file

@ -1804,6 +1804,9 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
) -> Cow<'e, RangeSet> {
Cow::Borrowed(ecx.machine.union_data_ranges.entry(ty).or_insert_with(compute_range))
}
/// Placeholder!
fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams { () }
}
/// Trait for callbacks handling asynchronous machine operations.