openvm_platform/
memory.rs1pub const MEM_BITS: usize = 29;
2pub const MEM_SIZE: usize = 1 << MEM_BITS;
3pub const GUEST_MIN_MEM: usize = 0x0000_0400;
4pub const GUEST_MAX_MEM: usize = MEM_SIZE;
5
6pub const STACK_TOP: u32 = 0x0020_0400;
8pub const TEXT_START: u32 = 0x0020_0800;
11
12pub fn is_guest_memory(addr: u32) -> bool {
14 GUEST_MIN_MEM <= (addr as usize) && (addr as usize) < GUEST_MAX_MEM
15}
16
17#[cfg(feature = "rust-runtime")]
21#[no_mangle]
22pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 {
23 use crate::print::println;
24
25 #[cfg(target_os = "zkvm")]
26 extern "C" {
27 static _end: u8;
34 }
35
36 static mut HEAP_POS: usize = 0;
39
40 let mut heap_pos = unsafe { HEAP_POS };
42
43 #[cfg(target_os = "zkvm")]
44 if heap_pos == 0 {
45 heap_pos = unsafe { (&_end) as *const u8 as usize };
46 }
47
48 let align = usize::max(align, super::WORD_SIZE);
51
52 let offset = heap_pos & (align - 1);
53 if offset != 0 {
54 heap_pos += align - offset;
55 }
56
57 match heap_pos.checked_add(bytes) {
58 Some(new_heap_pos) if new_heap_pos <= GUEST_MAX_MEM => {
59 unsafe { HEAP_POS = new_heap_pos };
61 }
62 _ => {
63 println("ERROR: Maximum memory exceeded, program terminating.");
64 super::rust_rt::terminate::<1>();
65 }
66 }
67 heap_pos as *mut u8
68}