const_deallocate: Don't deallocate memory allocated in an another const. Does nothing at runtime.

`const_allocate`:  Returns a null pointer at runtime.
This commit is contained in:
woppopo 2022-01-26 13:06:09 +09:00
parent aa6795e2d4
commit 29932db09b
8 changed files with 69 additions and 11 deletions

View file

@ -358,11 +358,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
};
ecx.memory.deallocate(
ptr,
Some((size, align)),
interpret::MemoryKind::Machine(MemoryKind::Heap),
)?;
// If an allocation is created in an another const,
// we don't deallocate it.
let (alloc_id, _, _) = ecx.memory.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(
ptr,
Some((size, align)),
interpret::MemoryKind::Machine(MemoryKind::Heap),
)?;
}
}
_ => {
return Err(ConstEvalErrKind::NeedsRfc(format!(