diff --git a/zkvm/entrypoint/src/memory.rs b/zkvm/entrypoint/src/memory.rs deleted file mode 100644 index b4c967ea1b..0000000000 --- a/zkvm/entrypoint/src/memory.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2023 RISC Zero, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -const SYSTEM_START: usize = 0x0C00_0000; - -#[allow(clippy::missing_safety_doc)] -#[no_mangle] -pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 { - extern "C" { - // https://lld.llvm.org/ELF/linker_script.html#sections-command - static _end: u8; - } - - // Pointer to next heap address to use, or 0 if the heap has not yet been - // initialized. - static mut HEAP_POS: usize = 0; - - // SAFETY: Single threaded, so nothing else can touch this while we're working. - let mut heap_pos = unsafe { HEAP_POS }; - - if heap_pos == 0 { - heap_pos = unsafe { (&_end) as *const u8 as usize }; - } - - let offset = heap_pos & (align - 1); - if offset != 0 { - heap_pos += align - offset; - } - - let ptr = heap_pos as *mut u8; - heap_pos += bytes; - - // Check to make sure heap doesn't collide with SYSTEM memory. - if SYSTEM_START < heap_pos { - panic!(); - } - - unsafe { HEAP_POS = heap_pos }; - ptr -} diff --git a/zkvm/entrypoint/src/syscalls/halt.rs b/zkvm/entrypoint/src/syscalls/halt.rs index caaf55d9ac..930685b588 100644 --- a/zkvm/entrypoint/src/syscalls/halt.rs +++ b/zkvm/entrypoint/src/syscalls/halt.rs @@ -26,33 +26,22 @@ pub extern "C" fn syscall_halt(exit_code: u8) -> ! { .unwrap() .finalize(); - // Convert the digest bytes into words, since we will be calling COMMIT ecall with - // the words as a parameter. - let pv_digest_words: [u32; PV_DIGEST_NUM_WORDS] = pv_digest_bytes - .as_slice() - .chunks_exact(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())) - .collect::>() - .try_into() - .unwrap(); - // For each digest word, call COMMIT ecall. In the runtime, this will store the digest words // into the runtime's execution record's public values digest. In the AIR, it will be used // to verify that the provided public values digest matches the one computed by the program. for i in 0..PV_DIGEST_NUM_WORDS { - asm!("ecall", in("t0") crate::syscalls::COMMIT, in("a0") i, in("a1") pv_digest_words[i]); + // Convert the digest bytes into words, since we will call COMMIT one word at a time. + let word = u32::from_le_bytes(pv_digest_bytes[i * 4..(i + 1) * 4].try_into().unwrap()); + asm!("ecall", in("t0") crate::syscalls::COMMIT, in("a0") i, in("a1") word); } cfg_if! { if #[cfg(feature = "verify")] { let deferred_proofs_digest = zkvm::DEFERRED_PROOFS_DIGEST.as_mut().unwrap(); - let deferred_proofs_digest_words = deferred_proofs_digest - .iter() - .map(|baby_bear| baby_bear.as_canonical_u32()) - .collect::>(); for i in 0..POSEIDON_NUM_WORDS { - asm!("ecall", in("t0") crate::syscalls::COMMIT_DEFERRED_PROOFS, in("a0") i, in("a1") deferred_proofs_digest_words[i]); + let word = deferred_proofs_digest[i].as_canonical_u32(); + asm!("ecall", in("t0") crate::syscalls::COMMIT_DEFERRED_PROOFS, in("a0") i, in("a1") word); } } else { for i in 0..POSEIDON_NUM_WORDS { diff --git a/zkvm/entrypoint/src/syscalls/memory.rs b/zkvm/entrypoint/src/syscalls/memory.rs index b4c967ea1b..28d22e22d6 100644 --- a/zkvm/entrypoint/src/syscalls/memory.rs +++ b/zkvm/entrypoint/src/syscalls/memory.rs @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -const SYSTEM_START: usize = 0x0C00_0000; +// Memory addresses must be lower than BabyBear prime. +const MAX_MEMORY: usize = 0x78000000; #[allow(clippy::missing_safety_doc)] #[no_mangle] @@ -39,11 +40,10 @@ pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u } let ptr = heap_pos as *mut u8; - heap_pos += bytes; + let (heap_pos, overflowed) = heap_pos.overflowing_add(bytes); - // Check to make sure heap doesn't collide with SYSTEM memory. - if SYSTEM_START < heap_pos { - panic!(); + if overflowed || MAX_MEMORY < heap_pos { + panic!("Memory limit exceeded (0x78000000)"); } unsafe { HEAP_POS = heap_pos };