Skip to content

Commit d1787bd

Browse files
committed
Catch panic in create empty block state
1 parent 0aff697 commit d1787bd

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

plt/plt-block-state/src/ffi/block_state.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,41 @@ use crate::block_state::{BlockState, blob_store};
1010
use crate::ffi::blob_store_callbacks::{LoadCallback, StoreCallback};
1111
use concordium_base::base::ProtocolVersion;
1212

13-
/// Allocate a new empty PLT block state and returns it.
13+
/// Allocate a new empty PLT block state.
1414
///
15-
/// The returned pointer is to a uniquely owned instance.
16-
/// It must be freed by calling [`ffi_free_plt_block_state`].
15+
/// - [`status::FfiStatusCode::Success`]: Creating the block state was successful.
16+
/// - [`status::FfiStatusCode::Panic`]: Creating the block state resulted in an unrecoverable error or panic.
1717
///
1818
/// # Arguments
1919
///
2020
/// - `protocol_version` Protocol version for the block state to create.
21+
/// - `block_state_out`: Location for writing the pointer of the new, empty block state.
22+
/// The new block state is only written if return value is [`status::FfiStatusCode::Success`].
23+
/// The pointer written is to a uniquely owned instance.
24+
/// The caller must free the written block state using `ffi_free_plt_block_state` when it is no longer used.
25+
///
26+
/// # Safety
27+
///
28+
/// - Argument `block_state_out` must be a non-null and valid pointer for writing
2129
#[unsafe(no_mangle)]
22-
extern "C" fn ffi_empty_plt_block_state(protocol_version: u64) -> *mut BlockState {
23-
let protocol_version =
24-
ProtocolVersion::try_from(protocol_version).expect("Unknown protocol version");
25-
let block_state = BlockState::empty(protocol_version);
26-
Box::into_raw(Box::new(block_state))
30+
extern "C" fn ffi_empty_plt_block_state(
31+
protocol_version: u64,
32+
block_state_out: *mut *mut BlockState,
33+
) -> status::FfiStatusCode {
34+
let panic_message = status::catch_unwind(move || {
35+
let protocol_version =
36+
ProtocolVersion::try_from(protocol_version).expect("Unknown protocol version");
37+
let block_state = BlockState::empty(protocol_version);
38+
unsafe {
39+
*block_state_out = Box::into_raw(Box::new(block_state));
40+
}
41+
});
42+
if let Some(message) = panic_message {
43+
eprintln!("{}", message);
44+
status::FfiStatusCode::Panic
45+
} else {
46+
status::FfiStatusCode::Success
47+
}
2748
}
2849

2950
/// Deallocate the PLT block state.

0 commit comments

Comments
 (0)