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

Swift: Propagate callback errors directly #108

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
182 changes: 89 additions & 93 deletions rust/bridge/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use libsignal_protocol_rust::*;
use static_assertions::const_assert_eq;
use std::convert::TryFrom;
use std::ffi::{c_void, CString};
use std::fmt;

use aes_gcm_siv::Aes256GcmSiv;

Expand Down Expand Up @@ -854,6 +855,26 @@ impl FfiIdentityKeyStore {
}
}

#[derive(Debug)]
struct CallbackError {
value: std::num::NonZeroI32,
}

impl CallbackError {
fn check(value: i32) -> Option<Self> {
let value = std::num::NonZeroI32::try_from(value).ok()?;
Some(Self { value })
}
}

impl fmt::Display for CallbackError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "error code {}", self.value)
}
}

impl std::error::Error for CallbackError {}

#[async_trait(?Send)]
impl IdentityKeyStore for FfiIdentityKeyStore {
async fn get_identity_key_pair(
Expand All @@ -864,13 +885,11 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
let mut key = std::ptr::null_mut();
let result = (self.store.get_identity_key_pair)(self.store.ctx, &mut key, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"get_identity_key_pair",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"get_identity_key_pair",
Box::new(error),
));
}

if key.is_null() {
Expand All @@ -888,13 +907,11 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
let mut id = 0;
let result = (self.store.get_local_registration_id)(self.store.ctx, &mut id, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"get_local_registration_id",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"get_local_registration_id",
Box::new(error),
));
}

Ok(id)
Expand All @@ -913,9 +930,10 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
match result {
0 => Ok(false),
1 => Ok(true),
r => Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError("save_identity", r),
),
r => Err(SignalProtocolError::ApplicationCallbackError(
"save_identity",
Box::new(CallbackError::check(r).unwrap()),
)),
}
}

Expand All @@ -942,12 +960,10 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
match result {
0 => Ok(false),
1 => Ok(true),
r => Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"is_trusted_identity",
r,
),
),
r => Err(SignalProtocolError::ApplicationCallbackError(
"is_trusted_identity",
Box::new(CallbackError::check(r).unwrap()),
)),
}
}

Expand All @@ -960,13 +976,11 @@ impl IdentityKeyStore for FfiIdentityKeyStore {
let mut key = std::ptr::null_mut();
let result = (self.store.get_identity)(self.store.ctx, &mut key, &*address, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"get_identity",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"get_identity",
Box::new(error),
));
}

if key.is_null() {
Expand Down Expand Up @@ -1025,13 +1039,11 @@ impl PreKeyStore for FfiPreKeyStore {
let mut record = std::ptr::null_mut();
let result = (self.store.load_pre_key)(self.store.ctx, &mut record, prekey_id, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"load_pre_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"load_pre_key",
Box::new(error),
));
}

if record.is_null() {
Expand All @@ -1051,13 +1063,11 @@ impl PreKeyStore for FfiPreKeyStore {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store.store_pre_key)(self.store.ctx, prekey_id, &*record, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"store_pre_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"store_pre_key",
Box::new(error),
));
}

Ok(())
Expand All @@ -1071,13 +1081,11 @@ impl PreKeyStore for FfiPreKeyStore {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store.remove_pre_key)(self.store.ctx, prekey_id, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"remove_pre_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"remove_pre_key",
Box::new(error),
));
}

Ok(())
Expand Down Expand Up @@ -1128,13 +1136,11 @@ impl SignedPreKeyStore for FfiSignedPreKeyStore {
let mut record = std::ptr::null_mut();
let result = (self.store.load_signed_pre_key)(self.store.ctx, &mut record, prekey_id, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"load_signed_pre_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"load_signed_pre_key",
Box::new(error),
));
}

if record.is_null() {
Expand All @@ -1155,13 +1161,11 @@ impl SignedPreKeyStore for FfiSignedPreKeyStore {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store.store_signed_pre_key)(self.store.ctx, prekey_id, &*record, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"store_signed_pre_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"store_signed_pre_key",
Box::new(error),
));
}

Ok(())
Expand Down Expand Up @@ -1212,13 +1216,11 @@ impl SessionStore for FfiSessionStore {
let mut record = std::ptr::null_mut();
let result = (self.store.load_session)(self.store.ctx, &mut record, &*address, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"load_session",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"load_session",
Box::new(error),
));
}

if record.is_null() {
Expand All @@ -1239,13 +1241,11 @@ impl SessionStore for FfiSessionStore {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store.store_session)(self.store.ctx, &*address, &*record, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"store_session",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"store_session",
Box::new(error),
));
}

Ok(())
Expand Down Expand Up @@ -1474,13 +1474,11 @@ impl SenderKeyStore for FfiSenderKeyStore {
let result =
(self.store.store_sender_key)(self.store.ctx, &*sender_key_name, &*record, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"store_sender_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"store_sender_key",
Box::new(error),
));
}

Ok(())
Expand All @@ -1496,13 +1494,11 @@ impl SenderKeyStore for FfiSenderKeyStore {
let result =
(self.store.load_sender_key)(self.store.ctx, &mut record, &*sender_key_name, ctx);

if result != 0 {
return Err(
SignalProtocolError::ApplicationCallbackReturnedIntegerError(
"load_sender_key",
result,
),
);
if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
"load_sender_key",
Box::new(error),
));
}

if record.is_null() {
Expand Down
9 changes: 4 additions & 5 deletions rust/bridge/ffi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub enum SignalFfiError {
NullPointer,
InvalidUtf8String,
UnexpectedPanic(std::boxed::Box<dyn std::any::Any + std::marker::Send>),
CallbackError(i32),
InvalidType,
}

Expand Down Expand Up @@ -70,7 +69,6 @@ impl From<&SignalFfiError> for SignalErrorCode {
SignalFfiError::InvalidType => SignalErrorCode::InvalidType,
SignalFfiError::UnexpectedPanic(_) => SignalErrorCode::InternalError,

SignalFfiError::CallbackError(_) => SignalErrorCode::CallbackError,
SignalFfiError::InvalidUtf8String => SignalErrorCode::InvalidUtf8String,
SignalFfiError::InsufficientOutputSize(_, _) => SignalErrorCode::InsufficientOutputSize,

Expand Down Expand Up @@ -150,6 +148,10 @@ impl From<&SignalFfiError> for SignalErrorCode {
SignalFfiError::Signal(SignalProtocolError::InvalidArgument(_))
| SignalFfiError::AesGcmSiv(_) => SignalErrorCode::InvalidArgument,

SignalFfiError::Signal(SignalProtocolError::ApplicationCallbackError(_, _)) => {
SignalErrorCode::CallbackError
}

_ => SignalErrorCode::UnknownError,
}
}
Expand All @@ -159,9 +161,6 @@ impl fmt::Display for SignalFfiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SignalFfiError::Signal(s) => write!(f, "{}", s),
SignalFfiError::CallbackError(c) => {
write!(f, "callback invocation returned error code {}", c)
}
SignalFfiError::AesGcmSiv(c) => {
write!(f, "AES-GCM-SIV operation failed: {}", c)
}
Expand Down
Loading