From ecde21da99ceeb273c3df736a152e9e6ab5ea23d Mon Sep 17 00:00:00 2001 From: johntaiko Date: Tue, 14 May 2024 19:12:15 +0800 Subject: [PATCH] fix: mismatch method signature of libc's calloc (#201) * fix: mismatch method signature of libc's calloc * fix: make calloc function public in mem.rs --- provers/risc0/guest/src/mem.rs | 9 ++++----- provers/sp1/guest/src/mem.rs | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/provers/risc0/guest/src/mem.rs b/provers/risc0/guest/src/mem.rs index 7a1d294ee..0a32268c1 100644 --- a/provers/risc0/guest/src/mem.rs +++ b/provers/risc0/guest/src/mem.rs @@ -3,14 +3,13 @@ use std::{ ffi::c_void, }; -/// This is a basic implementation of custom memory allocation functions that mimic C-style memory management. -/// This implementation is designed to be used in ZkVM where we cross-compile Rust code with C +/// This is a basic implementation of custom memory allocation functions that mimic C-style memory management. +/// This implementation is designed to be used in ZkVM where we cross-compile Rust code with C /// due to the dependency of c-kzg. This modification also requires env var: /// $ CC="gcc" /// $ CC_riscv32im-risc0-zkvm-elf="/opt/riscv/bin/riscv32-unknown-elf-gcc" /// which is set in the build pipeline - #[no_mangle] // TODO ideally this is c_size_t, but not stabilized (not guaranteed to be usize on all archs) pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void { @@ -26,8 +25,8 @@ pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void { #[no_mangle] // TODO shouldn't need to zero allocated bytes since the zkvm memory is zeroed, might want to zero anyway -pub unsafe extern "C" fn calloc(size: usize) -> *mut c_void { - malloc(size) +pub unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { + malloc(nobj * size) } #[no_mangle] diff --git a/provers/sp1/guest/src/mem.rs b/provers/sp1/guest/src/mem.rs index 405799eff..0a32268c1 100644 --- a/provers/sp1/guest/src/mem.rs +++ b/provers/sp1/guest/src/mem.rs @@ -25,8 +25,8 @@ pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void { #[no_mangle] // TODO shouldn't need to zero allocated bytes since the zkvm memory is zeroed, might want to zero anyway -pub unsafe extern "C" fn calloc(size: usize) -> *mut c_void { - malloc(size) +pub unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { + malloc(nobj * size) } #[no_mangle]