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 memory leak caused by impl.go call libzkp #57

Closed
wants to merge 3 commits into from
Closed
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
27 changes: 9 additions & 18 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,12 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
}

tracesStr := C.CString(string(tracesByt))
defer func() {
C.free(unsafe.Pointer(tracesStr))
}()
defer C.free(unsafe.Pointer(tracesStr))
Copy link
Collaborator

Choose a reason for hiding this comment

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

how does this change make it different?

Copy link
Author

Choose a reason for hiding this comment

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

There are some subtle differences, I mainly refer to scroll's update


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_c_chars(rawResult)
}()
defer C.free_c_chars(rawResult)

log.Debug("check circuit capacity for tx done", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)

result := &WrappedRowUsage{}
Expand Down Expand Up @@ -119,15 +116,11 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
}

tracesStr := C.CString(string(tracesByt))
defer func() {
C.free(unsafe.Pointer(tracesStr))
}()
defer C.free(unsafe.Pointer(tracesStr))

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_c_chars(rawResult)
}()
defer C.free_c_chars(rawResult)
log.Debug("check circuit capacity for block done", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())

result := &WrappedRowUsage{}
Expand Down Expand Up @@ -157,9 +150,8 @@ 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_c_chars(rawResult)
}()
defer C.free_c_chars(rawResult)

log.Debug("ccc get_tx_num end", "id", ccc.ID)

result := &WrappedTxNum{}
Expand All @@ -180,9 +172,8 @@ 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_c_chars(rawResult)
}()
defer C.free_c_chars(rawResult)

log.Debug("ccc set_light_mode end", "id", ccc.ID)

result := &WrappedCommonResult{}
Expand Down
149 changes: 80 additions & 69 deletions rollup/circuitcapacitychecker/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ pub mod checker {

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn init() {
pub extern "C" fn init() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug"))
.format_timestamp_millis()
.init();
let checkers = HashMap::new();
CHECKERS
.set(checkers)
.expect("circuit capacity checker initialized twice");
unsafe {
CHECKERS
.set(checkers)
.expect("circuit capacity checker initialized twice");
}
}

/// # Safety
Expand Down Expand Up @@ -70,7 +72,7 @@ pub mod checker {

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn apply_tx(id: u64, tx_traces: *const c_char) -> *const c_char {
pub extern "C" fn apply_tx(id: u64, tx_traces: *const c_char) -> *const c_char {
let result = apply_tx_inner(id, tx_traces);
let r = match result {
Ok(acc_row_usage) => {
Expand All @@ -92,7 +94,7 @@ pub mod checker {
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
}

unsafe fn apply_tx_inner(id: u64, tx_traces: *const c_char) -> Result<RowUsage, Error> {
fn apply_tx_inner(id: u64, tx_traces: *const c_char) -> Result<RowUsage, Error> {
log::debug!(
"ccc apply_tx raw input, id: {:?}, tx_traces: {:?}",
id,
Expand All @@ -111,18 +113,21 @@ pub mod checker {
bail!("traces.tx_storage_trace.len() != 1");
}

let r = panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in apply_tx"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id:?}) in apply_tx"
))?
.estimate_circuit_capacity(&[traces])
});
let r = unsafe {
panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in apply_tx"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id:?}) in apply_tx"
))?
.estimate_circuit_capacity(&[traces])
})
};

match r {
Ok(result) => result,
Err(e) => {
Expand All @@ -133,7 +138,7 @@ pub mod checker {

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn apply_block(id: u64, block_trace: *const c_char) -> *const c_char {
pub extern "C" fn apply_block(id: u64, block_trace: *const c_char) -> *const c_char {
let result = apply_block_inner(id, block_trace);
let r = match result {
Ok(acc_row_usage) => {
Expand All @@ -155,7 +160,7 @@ pub mod checker {
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
}

unsafe fn apply_block_inner(id: u64, block_trace: *const c_char) -> Result<RowUsage, Error> {
fn apply_block_inner(id: u64, block_trace: *const c_char) -> Result<RowUsage, Error> {
log::debug!(
"ccc apply_block raw input, id: {:?}, block_trace: {:?}",
id,
Expand All @@ -164,18 +169,20 @@ pub mod checker {
let block_trace = c_char_to_vec(block_trace);
let traces = serde_json::from_slice::<BlockTrace>(&block_trace)?;

let r = panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in apply_block"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id:?}) in apply_block"
))?
.estimate_circuit_capacity(&[traces])
});
let r = unsafe {
panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in apply_block"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id:?}) in apply_block"
))?
.estimate_circuit_capacity(&[traces])
})
};
match r {
Ok(result) => result,
Err(e) => {
Expand All @@ -186,7 +193,7 @@ pub mod checker {

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn get_tx_num(id: u64) -> *const c_char {
pub extern "C" fn get_tx_num(id: u64) -> *const c_char {
let result = get_tx_num_inner(id);
let r = match result {
Ok(tx_num) => {
Expand All @@ -204,29 +211,31 @@ pub mod checker {
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
}

unsafe fn get_tx_num_inner(id: u64) -> Result<u64, Error> {
fn get_tx_num_inner(id: u64) -> Result<u64, Error> {
log::debug!("ccc get_tx_num raw input, id: {id}");
panic::catch_unwind(|| {
Ok(CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in get_tx_num"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id}) in get_tx_num"
))?
.get_tx_num() as u64)
})
.map_or_else(
|e| bail!("circuit capacity checker (id: {id}) error in get_tx_num: {e:?}"),
|result| result,
)
unsafe {
panic::catch_unwind(|| {
Ok(CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in get_tx_num"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id}) in get_tx_num"
))?
.get_tx_num() as u64)
})
.map_or_else(
|e| bail!("circuit capacity checker (id: {id}) error in get_tx_num: {e:?}"),
|result| result,
)
}
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn set_light_mode(id: u64, light_mode: bool) -> *const c_char {
pub extern "C" fn set_light_mode(id: u64, light_mode: bool) -> *const c_char {
let result = set_light_mode_inner(id, light_mode);
let r = match result {
Ok(()) => CommonResult { error: None },
Expand All @@ -237,25 +246,27 @@ pub mod checker {
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
}

unsafe fn set_light_mode_inner(id: u64, light_mode: bool) -> Result<(), Error> {
fn set_light_mode_inner(id: u64, light_mode: bool) -> Result<(), Error> {
log::debug!("ccc set_light_mode raw input, id: {id}");
panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in set_light_mode"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id}) in set_light_mode"
))?
.set_light_mode(light_mode);
Ok(())
})
.map_or_else(
|e| bail!("circuit capacity checker (id: {id}) error in set_light_mode: {e:?}"),
|result| result,
)
unsafe {
panic::catch_unwind(|| {
CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in set_light_mode"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id}) in set_light_mode"
))?
.set_light_mode(light_mode);
Ok(())
})
.map_or_else(
|e| bail!("circuit capacity checker (id: {id}) error in set_light_mode: {e:?}"),
|result| result,
)
}
}
}

Expand Down