From 523ac6ce73dad6997047f380a0e37609c6306d1d Mon Sep 17 00:00:00 2001 From: john xu Date: Tue, 14 May 2024 13:18:30 +0800 Subject: [PATCH 1/2] fix: mismatch method signature of libc's calloc --- 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 7a1d294e..e4629dd4 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) +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 405799ef..e4629dd4 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) +unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { + malloc(nobj * size) } #[no_mangle] From 853909b6f17e75cf6ba4de202e380eb4c1c96c9b Mon Sep 17 00:00:00 2001 From: john xu Date: Tue, 14 May 2024 14:34:02 +0800 Subject: [PATCH 2/2] fix: make calloc function public in mem.rs --- provers/risc0/guest/src/mem.rs | 2 +- provers/sp1/guest/src/mem.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/provers/risc0/guest/src/mem.rs b/provers/risc0/guest/src/mem.rs index e4629dd4..0a32268c 100644 --- a/provers/risc0/guest/src/mem.rs +++ b/provers/risc0/guest/src/mem.rs @@ -25,7 +25,7 @@ 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 -unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { +pub unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { malloc(nobj * size) } diff --git a/provers/sp1/guest/src/mem.rs b/provers/sp1/guest/src/mem.rs index e4629dd4..0a32268c 100644 --- a/provers/sp1/guest/src/mem.rs +++ b/provers/sp1/guest/src/mem.rs @@ -25,7 +25,7 @@ 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 -unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { +pub unsafe extern "C" fn calloc(nobj: usize, size: usize) -> *mut c_void { malloc(nobj * size) }