feat: print to stderr any error obtained while executing functions via FFI (#200)

This commit is contained in:
richΛrd 2023-08-09 05:05:33 -04:00 committed by GitHub
parent 7d63912ace
commit 99966d1a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 34 deletions

View File

@ -12,7 +12,15 @@ macro_rules! call {
($instance:expr, $method:ident $(, $arg:expr)*) => { ($instance:expr, $method:ident $(, $arg:expr)*) => {
{ {
let new_instance: &mut RLN = $instance.process(); let new_instance: &mut RLN = $instance.process();
new_instance.$method($($arg.process()),*).is_ok() match new_instance.$method($($arg.process()),*) {
Ok(()) => {
true
}
Err(err) => {
eprintln!("execution error: {err}");
false
}
}
} }
} }
} }
@ -30,13 +38,17 @@ macro_rules! call_with_output_arg {
{ {
let mut output_data: Vec<u8> = Vec::new(); let mut output_data: Vec<u8> = Vec::new();
let new_instance = $instance.process(); let new_instance = $instance.process();
if new_instance.$method(&mut output_data).is_ok() { match new_instance.$method(&mut output_data) {
unsafe { *$output_arg = Buffer::from(&output_data[..]) }; Ok(()) => {
std::mem::forget(output_data); unsafe { *$output_arg = Buffer::from(&output_data[..]) };
true std::mem::forget(output_data);
} else { true
std::mem::forget(output_data); }
false Err(err) => {
std::mem::forget(output_data);
eprintln!("execution error: {err}");
false
}
} }
} }
}; };
@ -44,13 +56,17 @@ macro_rules! call_with_output_arg {
{ {
let mut output_data: Vec<u8> = Vec::new(); let mut output_data: Vec<u8> = Vec::new();
let new_instance = $instance.process(); let new_instance = $instance.process();
if new_instance.$method($($arg.process()),*, &mut output_data).is_ok() { match new_instance.$method($($arg.process()),*, &mut output_data) {
unsafe { *$output_arg = Buffer::from(&output_data[..]) }; Ok(()) => {
std::mem::forget(output_data); unsafe { *$output_arg = Buffer::from(&output_data[..]) };
true std::mem::forget(output_data);
} else { true
std::mem::forget(output_data); }
false Err(err) => {
std::mem::forget(output_data);
eprintln!("execution error: {err}");
false
}
} }
} }
}; };
@ -66,13 +82,17 @@ macro_rules! no_ctx_call_with_output_arg {
($method:ident, $output_arg:expr, $( $arg:expr ),* ) => { ($method:ident, $output_arg:expr, $( $arg:expr ),* ) => {
{ {
let mut output_data: Vec<u8> = Vec::new(); let mut output_data: Vec<u8> = Vec::new();
if $method($($arg.process()),*, &mut output_data).is_ok() { match $method($($arg.process()),*, &mut output_data) {
unsafe { *$output_arg = Buffer::from(&output_data[..]) }; Ok(()) => {
std::mem::forget(output_data); unsafe { *$output_arg = Buffer::from(&output_data[..]) };
true std::mem::forget(output_data);
} else { true
std::mem::forget(output_data); }
false Err(err) => {
std::mem::forget(output_data);
eprintln!("execution error: {err}");
false
}
} }
} }
} }
@ -89,8 +109,11 @@ macro_rules! call_with_bool_arg {
{ {
let new_instance = $instance.process(); let new_instance = $instance.process();
if match new_instance.$method($($arg.process()),*,) { if match new_instance.$method($($arg.process()),*,) {
Ok(verified) => verified, Ok(result) => result,
Err(_) => return false, Err(err) => {
eprintln!("execution error: {err}");
return false
},
} { } {
unsafe { *$bool_arg = true }; unsafe { *$bool_arg = true };
} else { } else {
@ -171,11 +194,15 @@ impl<'a> From<&Buffer> for &'a [u8] {
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle] #[no_mangle]
pub extern "C" fn new(tree_height: usize, input_buffer: *const Buffer, ctx: *mut *mut RLN) -> bool { pub extern "C" fn new(tree_height: usize, input_buffer: *const Buffer, ctx: *mut *mut RLN) -> bool {
if let Ok(rln) = RLN::new(tree_height, input_buffer.process()) { match RLN::new(tree_height, input_buffer.process()) {
unsafe { *ctx = Box::into_raw(Box::new(rln)) }; Ok(rln) => {
true unsafe { *ctx = Box::into_raw(Box::new(rln)) };
} else { true
false }
Err(err) => {
eprintln!("could not instantiate rln: {err}");
false
}
} }
} }
@ -189,17 +216,21 @@ pub extern "C" fn new_with_params(
tree_config: *const Buffer, tree_config: *const Buffer,
ctx: *mut *mut RLN, ctx: *mut *mut RLN,
) -> bool { ) -> bool {
if let Ok(rln) = RLN::new_with_params( match RLN::new_with_params(
tree_height, tree_height,
circom_buffer.process().to_vec(), circom_buffer.process().to_vec(),
zkey_buffer.process().to_vec(), zkey_buffer.process().to_vec(),
vk_buffer.process().to_vec(), vk_buffer.process().to_vec(),
tree_config.process(), tree_config.process(),
) { ) {
unsafe { *ctx = Box::into_raw(Box::new(rln)) }; Ok(rln) => {
true unsafe { *ctx = Box::into_raw(Box::new(rln)) };
} else { true
false }
Err(err) => {
eprintln!("could not instantiate rln: {err}");
false
}
} }
} }