From 5253db8495df3b402d91e34ac5c7923c5e9811a4 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 29 Jan 2024 15:01:10 +0100 Subject: [PATCH] allocator: Don't allow growth beyond heap memory region (cherry picked from commit afc2ff9e80d443e86c09341240965c53d7f7be26) --- programs/mango-v4/src/allocator.rs | 8 ++++++++ 1 file changed, 8 insertions(+) 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;