allocator: Don't allow growth beyond heap memory region

(cherry picked from commit afc2ff9e80)
This commit is contained in:
Christian Kamm 2024-01-29 15:01:10 +01:00
parent 8f593e00b8
commit 5253db8495
1 changed files with 8 additions and 0 deletions

View File

@ -2,6 +2,11 @@
use std::alloc::{GlobalAlloc, Layout};
/// The end of the region where heap space may be reserved for the program.
///
/// The actual size of the heap is currently not available at runtime.
pub const HEAP_END_ADDRESS: usize = 0x400000000;
#[cfg(not(feature = "no-entrypoint"))]
#[global_allocator]
pub static ALLOCATOR: BumpAllocator = BumpAllocator {};
@ -48,6 +53,9 @@ unsafe impl GlobalAlloc for BumpAllocator {
let end = begin.checked_add(layout.size()).unwrap();
*pos_ptr = end;
// Ensure huge allocations can't escape the dedicated heap memory region
assert!(end < HEAP_END_ADDRESS);
// Write a byte to trigger heap overflow errors early
let end_ptr = end as *mut u8;
*end_ptr = 0;