Skip to content

Commit

Permalink
fix: mismatch method signature of libc's calloc (#201)
Browse files Browse the repository at this point in the history
* fix: mismatch method signature of libc's calloc

* fix: make calloc function public in mem.rs
  • Loading branch information
johntaiko authored May 14, 2024
1 parent b3c2c1d commit ecde21d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions provers/risc0/guest/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions provers/sp1/guest/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ecde21d

Please sign in to comment.