interpret: remove MemoryExtra in favor of giving access to the Machine
This commit is contained in:
@@ -93,10 +93,7 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
|
||||
|
||||
/// The virtual call stack.
|
||||
pub(crate) stack: Vec<Frame<'mir, 'tcx, AllocId, ()>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct MemoryExtra {
|
||||
/// We need to make sure consts never point to anything mutable, even recursively. That is
|
||||
/// relied on for pattern matching on consts with references.
|
||||
/// To achieve this, two pieces have to work together:
|
||||
@@ -107,8 +104,12 @@ pub struct MemoryExtra {
|
||||
}
|
||||
|
||||
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
|
||||
pub(super) fn new(const_eval_limit: Limit) -> Self {
|
||||
CompileTimeInterpreter { steps_remaining: const_eval_limit.0, stack: Vec::new() }
|
||||
pub(super) fn new(const_eval_limit: Limit, can_access_statics: bool) -> Self {
|
||||
CompileTimeInterpreter {
|
||||
steps_remaining: const_eval_limit.0,
|
||||
stack: Vec::new(),
|
||||
can_access_statics,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,8 +234,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
|
||||
type MemoryKind = MemoryKind;
|
||||
|
||||
type MemoryExtra = MemoryExtra;
|
||||
|
||||
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
|
||||
|
||||
fn load_mir(
|
||||
@@ -345,7 +344,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
|
||||
};
|
||||
|
||||
let ptr = ecx.memory.allocate(
|
||||
let ptr = ecx.allocate_ptr(
|
||||
Size::from_bytes(size as u64),
|
||||
align,
|
||||
interpret::MemoryKind::Machine(MemoryKind::Heap),
|
||||
@@ -365,14 +364,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
|
||||
// If an allocation is created in an another const,
|
||||
// we don't deallocate it.
|
||||
let (alloc_id, _, _) = ecx.memory.ptr_get_alloc(ptr)?;
|
||||
let (alloc_id, _, _) = ecx.ptr_get_alloc(ptr)?;
|
||||
let is_allocated_in_another_const = matches!(
|
||||
ecx.tcx.get_global_alloc(alloc_id),
|
||||
Some(interpret::GlobalAlloc::Memory(_))
|
||||
);
|
||||
|
||||
if !is_allocated_in_another_const {
|
||||
ecx.memory.deallocate(
|
||||
ecx.deallocate_ptr(
|
||||
ptr,
|
||||
Some((size, align)),
|
||||
interpret::MemoryKind::Machine(MemoryKind::Heap),
|
||||
@@ -472,7 +471,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
}
|
||||
|
||||
fn before_access_global(
|
||||
memory_extra: &MemoryExtra,
|
||||
machine: &Self,
|
||||
alloc_id: AllocId,
|
||||
alloc: ConstAllocation<'tcx>,
|
||||
static_def_id: Option<DefId>,
|
||||
@@ -488,7 +487,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
}
|
||||
} else {
|
||||
// Read access. These are usually allowed, with some exceptions.
|
||||
if memory_extra.can_access_statics {
|
||||
if machine.can_access_statics {
|
||||
// Machine configuration allows us read from anything (e.g., `static` initializer).
|
||||
Ok(())
|
||||
} else if static_def_id.is_some() {
|
||||
|
||||
Reference in New Issue
Block a user