Skip to content

Commit

Permalink
fix: memory limit (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctian1 authored Jul 19, 2024
1 parent b17f86e commit 6ec3378
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 72 deletions.
51 changes: 0 additions & 51 deletions zkvm/entrypoint/src/memory.rs

This file was deleted.

21 changes: 5 additions & 16 deletions zkvm/entrypoint/src/syscalls/halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.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::<Vec<_>>();

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 {
Expand Down
10 changes: 5 additions & 5 deletions zkvm/entrypoint/src/syscalls/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 };
Expand Down

0 comments on commit 6ec3378

Please sign in to comment.