Fixed stack allocation for debug

This commit is contained in:
Erin 2024-02-04 02:21:14 +01:00
parent bcbe47bcd6
commit 5f8864e251
2 changed files with 17 additions and 1 deletions

View file

@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
// Allocate stack // Allocate stack
let mut stack = Box::new(MaybeUninit::<[u8; 1024 * 1024 * 2]>::uninit()); let mut stack = unsafe { mem::alloc_stack() };
eprintln!("[I] Stack allocated at {:p}", stack.as_ptr()); eprintln!("[I] Stack allocated at {:p}", stack.as_ptr());
// Load program // Load program

View file

@ -1,3 +1,5 @@
use std::alloc::Layout;
use hbvm::mem::{Address, LoadError, Memory, StoreError}; use hbvm::mem::{Address, LoadError, Memory, StoreError};
pub struct HostMemory; pub struct HostMemory;
@ -29,3 +31,17 @@ impl Memory for HostMemory {
unsafe { core::ptr::read(addr.get() as *const T) } unsafe { core::ptr::read(addr.get() as *const T) }
} }
} }
const STACK_SIZE: usize = 2; // MiB
type Stack = [u8; 1024 * 1024 * STACK_SIZE];
/// Allocate stack of size [`STACK_SIZE`] MiB
pub unsafe fn alloc_stack() -> Box<Stack> {
let layout = Layout::new::<Stack>();
let ptr = unsafe { std::alloc::alloc(layout) };
if ptr.is_null() {
std::alloc::handle_alloc_error(layout);
}
unsafe { Box::from_raw(ptr.cast()) }
}