Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (libzkp): free Rust CString by from_raw #998

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions common/libzkp/impl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ use std::{
pub(crate) static OUTPUT_DIR: Lazy<Option<String>> =
Lazy::new(|| env::var("PROVER_OUTPUT_DIR").ok());

/// # Safety
#[no_mangle]
pub extern "C" fn free_c_chars(ptr: *mut c_char) {
if ptr.is_null() {
log::warn!("Try to free an empty pointer!");
return;
}

unsafe {
let _ = CString::from_raw(ptr);
georgehao marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub(crate) fn c_char_to_str(c: *const c_char) -> &'static str {
let cstr = unsafe { CStr::from_ptr(c) };
cstr.to_str().unwrap()
Expand Down
1 change: 1 addition & 0 deletions common/libzkp/interface/libzkp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ char* gen_chunk_proof(char* block_traces);
char verify_chunk_proof(char* proof);

char* block_traces_to_chunk_info(char* block_traces);
void free_c_chars(char* ptr);
10 changes: 5 additions & 5 deletions prover/core/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func NewProverCore(cfg *config.ProverCoreConfig) (*ProverCore, error) {

var vk string
var rawVK *C.char
defer C.free(unsafe.Pointer(rawVK))
if cfg.ProofType == message.ProofTypeBatch {
C.init_batch_prover(paramsPathStr, assetsPathStr)
rawVK = C.get_batch_vk()
} else if cfg.ProofType == message.ProofTypeChunk {
C.init_chunk_prover(paramsPathStr, assetsPathStr)
rawVK = C.get_chunk_vk()
}
defer C.free_c_chars(rawVK)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rawVK is newed in go code, so it should be released in defer C.free(unsafe.Pointer(rawVK))?


if rawVK != nil {
vk = C.GoString(rawVK)
Expand Down Expand Up @@ -162,7 +162,7 @@ func (p *ProverCore) checkChunkProofs(chunkProofsByt []byte) (bool, error) {

log.Info("Start to check chunk proofs ...")
cResult := C.check_chunk_proofs(chunkProofsStr)
defer C.free(unsafe.Pointer(cResult))
defer C.free_c_chars(cResult)
log.Info("Finish checking chunk proofs!")

var result CheckChunkProofsResponse
Expand All @@ -189,7 +189,7 @@ func (p *ProverCore) proveBatch(chunkInfosByt []byte, chunkProofsByt []byte) ([]

log.Info("Start to create batch proof ...")
bResult := C.gen_batch_proof(chunkInfosStr, chunkProofsStr)
defer C.free(unsafe.Pointer(bResult))
defer C.free_c_chars(bResult)
log.Info("Finish creating batch proof!")

var result ProofResult
Expand All @@ -211,7 +211,7 @@ func (p *ProverCore) proveChunk(tracesByt []byte) ([]byte, error) {

log.Info("Start to create chunk proof ...")
cProof := C.gen_chunk_proof(tracesStr)
defer C.free(unsafe.Pointer(cProof))
defer C.free_c_chars(cProof)
log.Info("Finish creating chunk proof!")

var result ProofResult
Expand Down Expand Up @@ -246,7 +246,7 @@ func (p *ProverCore) tracesToChunkInfo(tracesByt []byte) []byte {
defer C.free(unsafe.Pointer(tracesStr))

cChunkInfo := C.block_traces_to_chunk_info(tracesStr)
defer C.free(unsafe.Pointer(cChunkInfo))
defer C.free_c_chars(cChunkInfo)

chunkInfo := C.GoString(cChunkInfo)
return []byte(chunkInfo)
Expand Down