Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

fix: fix the error handling crash problem after upgrading the rust version #123

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions tcx/src/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ thread_local! {

#[cfg_attr(tarpaulin, skip)]
#[allow(irrefutable_let_patterns)]
fn notify_err(err: Error) {
fn notify_err(err: Error) -> Error {
if let _backtrace = err.backtrace() {
LAST_BACKTRACE.with(|e| {
*e.borrow_mut() = Some((None, Backtrace::new()));
});
}
let display_err = format_err!("{}", &err);
LAST_ERROR.with(|e| {
*e.borrow_mut() = Some(err);
});
display_err
}

fn lock_all_keystore() {
Expand All @@ -32,11 +34,11 @@ fn lock_all_keystore() {
/// catch any error and format to string
/// ref: <https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/controlling-panics-with-std-panic.html>
#[cfg_attr(tarpaulin, skip)]
pub unsafe fn landingpad<F: FnOnce() -> Result<T> + panic::UnwindSafe, T>(f: F) -> T {
pub unsafe fn landingpad<F: FnOnce() -> Result<T> + panic::UnwindSafe, T>(f: F) -> Result<T> {
match panic::catch_unwind(f) {
Ok(rv) => {
lock_all_keystore();
rv.map_err(notify_err).unwrap_or_else(|_| mem::zeroed())
rv.map_err(notify_err)
}
Err(err) => {
lock_all_keystore();
Expand All @@ -49,8 +51,7 @@ pub unsafe fn landingpad<F: FnOnce() -> Result<T> + panic::UnwindSafe, T>(f: F)
None => "Box<Any>",
},
};
notify_err(format_err!("{}", msg));
mem::zeroed()
Err(notify_err(format_err!("{}", msg)))
}
}
}
24 changes: 13 additions & 11 deletions tcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::api::{Response, TcxAction};

pub mod error_handling;
pub mod handler;
use failure::Error;
use std::result;

use crate::error_handling::{landingpad, LAST_BACKTRACE, LAST_ERROR};
#[allow(deprecated)]
Expand Down Expand Up @@ -39,6 +41,8 @@ lazy_static! {
pub static ref IS_DEBUG: RwLock<bool> = RwLock::new(false);
}

pub type Result<T> = result::Result<T, Error>;

#[no_mangle]
pub unsafe extern "C" fn free_const_string(s: *const c_char) {
if s.is_null() {
Expand All @@ -57,7 +61,7 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char {

let data = hex::decode(hex_str).expect("parse_arguments hex decode");
let action: TcxAction = TcxAction::decode(data.as_slice()).expect("decode tcx api");
let reply: Vec<u8> = match action.method.to_lowercase().as_str() {
let reply: Result<Vec<u8>> = match action.method.to_lowercase().as_str() {
"init_token_core_x" => landingpad(|| {
handler::init_token_core_x(&action.param.unwrap().value).unwrap();
Ok(vec![])
Expand Down Expand Up @@ -117,9 +121,13 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char {
"unlock_then_crash" => landingpad(|| unlock_then_crash(&action.param.unwrap().value)),
_ => landingpad(|| Err(format_err!("unsupported_method"))),
};

let ret_str = hex::encode(reply);
CString::new(ret_str).unwrap().into_raw()
match reply {
Ok(reply) => {
let ret_str = hex::encode(reply);
CString::new(ret_str).unwrap().into_raw()
}
_ => CString::new("").unwrap().into_raw(),
}
}

#[no_mangle]
Expand All @@ -136,13 +144,7 @@ pub unsafe extern "C" fn clear_err() {
pub unsafe extern "C" fn get_last_err_message() -> *const c_char {
LAST_ERROR.with(|e| {
if let Some(ref err) = *e.borrow() {
let rsp = Response {
is_success: false,
error: err.to_string(),
};
let rsp_bytes = encode_message(rsp).expect("encode error");
let ret_str = hex::encode(rsp_bytes);
CString::new(ret_str).unwrap().into_raw()
CString::new(err.to_string()).unwrap().into_raw()
} else {
CString::new("").unwrap().into_raw()
}
Expand Down