Skip to content

Commit

Permalink
fix (libzkp): free Rust CString by from_raw (potential memory leak) (
Browse files Browse the repository at this point in the history
…#539)

* Free Rust CString by `from_raw`.

* Update params version.
  • Loading branch information
silathdiir authored Oct 25, 2023
1 parent fcb3e17 commit adde9ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionPatch = 2 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
8 changes: 4 additions & 4 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
log.Debug("start to check circuit capacity for tx", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
rawResult := C.apply_tx(C.uint64_t(ccc.ID), tracesStr)
defer func() {
C.free(unsafe.Pointer(rawResult))
C.free_c_chars(rawResult)
}()
log.Debug("check circuit capacity for tx done", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)

Expand Down Expand Up @@ -125,7 +125,7 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
log.Debug("start to check circuit capacity for block", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())
rawResult := C.apply_block(C.uint64_t(ccc.ID), tracesStr)
defer func() {
C.free(unsafe.Pointer(rawResult))
C.free_c_chars(rawResult)
}()
log.Debug("check circuit capacity for block done", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())

Expand Down Expand Up @@ -157,7 +157,7 @@ func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error
log.Debug("ccc get_tx_num start", "id", ccc.ID)
rawResult := C.get_tx_num(C.uint64_t(ccc.ID))
defer func() {
C.free(unsafe.Pointer(rawResult))
C.free_c_chars(rawResult)
}()
log.Debug("ccc get_tx_num end", "id", ccc.ID)

Expand All @@ -180,7 +180,7 @@ func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error {
log.Debug("ccc set_light_mode start", "id", ccc.ID)
rawResult := C.set_light_mode(C.uint64_t(ccc.ID), C.bool(lightMode))
defer func() {
C.free(unsafe.Pointer(rawResult))
C.free_c_chars(rawResult)
}()
log.Debug("ccc set_light_mode end", "id", ccc.ID)

Expand Down
1 change: 1 addition & 0 deletions rollup/circuitcapacitychecker/libzkp/libzkp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ char* apply_tx(uint64_t id, char *tx_traces);
char* apply_block(uint64_t id, char *block_trace);
char* get_tx_num(uint64_t id);
char* set_light_mode(uint64_t id, bool light_mode);
void free_c_chars(char* ptr);
13 changes: 12 additions & 1 deletion rollup/circuitcapacitychecker/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,22 @@ pub mod checker {
}
}

pub(crate) mod utils {
pub mod utils {
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::str::Utf8Error;

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

let _ = CString::from_raw(ptr);
}

#[allow(dead_code)]
pub(crate) fn c_char_to_str(c: *const c_char) -> Result<&'static str, Utf8Error> {
let cstr = unsafe { CStr::from_ptr(c) };
Expand Down

0 comments on commit adde9ce

Please sign in to comment.