diff --git a/programs/mango-v4/src/allocator.rs b/programs/mango-v4/src/allocator.rs index 9cb72d153..9ed5e94d0 100644 --- a/programs/mango-v4/src/allocator.rs +++ b/programs/mango-v4/src/allocator.rs @@ -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;