This is an automated email from the ASF dual-hosted git repository. ivila pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-teaclave-trustzone-sdk.git
commit 2144da9efbfc0436f27dcd6356c54d60ee8045b5 Author: ivila <[email protected]> AuthorDate: Tue Mar 25 11:11:59 2025 +0800 optee-teec: Error: add error_origin * Add ErrorOrigin enum. * Error: Support setting and getting ErrorOrigin. * ErrorKind: Use const variables from optee_utee_sys instead of magic number. * Fix doc test. Signed-off-by: Zehui Chen <[email protected]> Reviewed-by: Yuan Zhuang <[email protected]> --- optee-teec/Cargo.toml | 1 + optee-teec/src/error.rs | 134 +++++++++++++++++++++++++++--------------------- 2 files changed, 77 insertions(+), 58 deletions(-) diff --git a/optee-teec/Cargo.toml b/optee-teec/Cargo.toml index cfd0bca..8a9e0d5 100644 --- a/optee-teec/Cargo.toml +++ b/optee-teec/Cargo.toml @@ -30,6 +30,7 @@ optee-teec-macros = { version = "0.4.0", path = "macros" } libc = "0.2" uuid = "0.7" hex = "0.3" +num_enum = "0.7.3" [workspace] members = ['systest'] diff --git a/optee-teec/src/error.rs b/optee-teec/src/error.rs index d81b2d3..830db71 100644 --- a/optee-teec/src/error.rs +++ b/optee-teec/src/error.rs @@ -16,6 +16,7 @@ // under the License. use crate::raw; +use num_enum::{FromPrimitive, IntoPrimitive}; use std::fmt; /// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) @@ -24,8 +25,11 @@ use std::fmt; /// # Examples /// /// ``` no_run +/// use optee_teec::Context; +/// /// fn main() -> optee_teec::Result<()> { /// let mut ctx = Context::new()?; +/// Ok(()) /// } /// ```` pub type Result<T> = std::result::Result<T, Error>; @@ -34,60 +38,65 @@ pub type Result<T> = std::result::Result<T, Error>; /// /// [`Context`]: struct.Context.html /// [`Session`]: struct.Session.html +#[derive(Clone)] pub struct Error { - code: u32, + kind: ErrorKind, + origin: Option<ErrorOrigin>, } /// A list specifying general categories of TEE client error and its /// corresponding code in OP-TEE client library. -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive( + Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, FromPrimitive, IntoPrimitive, +)] #[repr(u32)] pub enum ErrorKind { /// Non-specific cause. - Generic = 0xFFFF0000, + Generic = raw::TEEC_ERROR_GENERIC, /// Access privileges are not sufficient. - AccessDenied = 0xFFFF0001, + AccessDenied = raw::TEEC_ERROR_ACCESS_DENIED, /// The operation was canceled. - Cancel = 0xFFFF0002, + Cancel = raw::TEEC_ERROR_CANCEL, /// Concurrent accesses caused conflict. - AccessConflict = 0xFFFF0003, + AccessConflict = raw::TEEC_ERROR_ACCESS_CONFLICT, /// Too much data for the requested operation was passed. - ExcessData = 0xFFFF0004, + ExcessData = raw::TEEC_ERROR_EXCESS_DATA, /// Input data was of invalid format. - BadFormat = 0xFFFF0005, + BadFormat = raw::TEEC_ERROR_BAD_FORMAT, /// Input parameters were invalid. - BadParameters = 0xFFFF0006, + BadParameters = raw::TEEC_ERROR_BAD_PARAMETERS, /// Operation is not valid in the current state. - BadState = 0xFFFF0007, + BadState = raw::TEEC_ERROR_BAD_STATE, /// The requested data item is not found. - ItemNotFound = 0xFFFF0008, + ItemNotFound = raw::TEEC_ERROR_ITEM_NOT_FOUND, /// The requested operation should exist but is not yet implemented. - NotImplemented = 0xFFFF0009, + NotImplemented = raw::TEEC_ERROR_NOT_IMPLEMENTED, /// The requested operation is valid but is not supported in this implementation. - NotSupported = 0xFFFF000A, + NotSupported = raw::TEEC_ERROR_NOT_SUPPORTED, /// Expected data was missing. - NoData = 0xFFFF000B, + NoData = raw::TEEC_ERROR_NO_DATA, /// System ran out of resources. - OutOfMemory = 0xFFFF000C, + OutOfMemory = raw::TEEC_ERROR_OUT_OF_MEMORY, /// The system is busy working on something else. - Busy = 0xFFFF000D, + Busy = raw::TEEC_ERROR_BUSY, /// Communication with a remote party failed. - Communication = 0xFFFF000E, + Communication = raw::TEEC_ERROR_COMMUNICATION, /// A security fault was detected. - Security = 0xFFFF000F, + Security = raw::TEEC_ERROR_SECURITY, /// The supplied buffer is too short for the generated output. - ShortBuffer = 0xFFFF0010, + ShortBuffer = raw::TEEC_ERROR_SHORT_BUFFER, /// Implementation defined error code. - ExternalCancel = 0xFFFF0011, + ExternalCancel = raw::TEEC_ERROR_EXTERNAL_CANCEL, /// Implementation defined error code: trusted Application has panicked during the operation. - TargetDead = 0xFFFF3024, + TargetDead = raw::TEEC_ERROR_TARGET_DEAD, /// Unknown error. + #[default] Unknown, } impl ErrorKind { pub(crate) fn as_str(&self) -> &'static str { - match *self { + match self { ErrorKind::Generic => "Non-specific cause.", ErrorKind::AccessDenied => "Access privileges are not sufficient.", ErrorKind::Cancel => "The operation was canceled.", @@ -118,59 +127,51 @@ impl ErrorKind { impl Error { pub fn new(kind: ErrorKind) -> Error { - Error { code: kind as u32 } + Error { kind, origin: None } } /// Creates a new instance of an `Error` from a particular TEE error code. /// /// # Examples /// - /// ``` no_run - /// use optee_teec; + /// ``` + /// use optee_teec::{Error, ErrorKind}; /// - /// let error = optee_teec::Error::from_raw_error(0xFFFF000F); - /// assert_eq!(error.kind(), optee_teec::ErrorKind::Security); + /// let error = Error::from_raw_error(0xFFFF000F); + /// assert_eq!(error.kind(), ErrorKind::Security); /// ``` pub fn from_raw_error(code: u32) -> Error { - Error { code } + Error { + kind: ErrorKind::from(code), + origin: None, + } + } + + pub fn with_origin(mut self, origin: ErrorOrigin) -> Self { + self.origin = Some(origin); + self } /// Returns the corresponding `ErrorKind` for this error. /// /// # Examples /// - /// ``` no_run - /// use optee_teec; + /// ``` + /// use optee_teec::{Error, ErrorKind}; /// - /// let error = optee_teec::Error::new(optee_teec::ErrorKind::Security); + /// let error = Error::new(ErrorKind::Security); /// ``` pub fn kind(&self) -> ErrorKind { - match self.code { - raw::TEEC_ERROR_GENERIC => ErrorKind::Generic, - raw::TEEC_ERROR_ACCESS_DENIED => ErrorKind::AccessDenied, - raw::TEEC_ERROR_CANCEL => ErrorKind::Cancel, - raw::TEEC_ERROR_ACCESS_CONFLICT => ErrorKind::AccessConflict, - raw::TEEC_ERROR_EXCESS_DATA => ErrorKind::ExcessData, - raw::TEEC_ERROR_BAD_FORMAT => ErrorKind::BadFormat, - raw::TEEC_ERROR_BAD_PARAMETERS => ErrorKind::BadParameters, - raw::TEEC_ERROR_BAD_STATE => ErrorKind::BadState, - raw::TEEC_ERROR_ITEM_NOT_FOUND => ErrorKind::ItemNotFound, - raw::TEEC_ERROR_NOT_IMPLEMENTED => ErrorKind::NotImplemented, - raw::TEEC_ERROR_NOT_SUPPORTED => ErrorKind::NotSupported, - raw::TEEC_ERROR_NO_DATA => ErrorKind::NoData, - raw::TEEC_ERROR_OUT_OF_MEMORY => ErrorKind::OutOfMemory, - raw::TEEC_ERROR_BUSY => ErrorKind::Busy, - raw::TEEC_ERROR_COMMUNICATION => ErrorKind::Communication, - raw::TEEC_ERROR_SECURITY => ErrorKind::Security, - raw::TEEC_ERROR_SHORT_BUFFER => ErrorKind::ShortBuffer, - raw::TEEC_ERROR_EXTERNAL_CANCEL => ErrorKind::ExternalCancel, - raw::TEEC_ERROR_TARGET_DEAD => ErrorKind::TargetDead, - _ => ErrorKind::Unknown, - } + self.kind + } + + /// Returns the origin of this error. + pub fn origin(&self) -> Option<ErrorOrigin> { + self.origin.clone() } /// Returns raw code of this error. pub fn raw_code(&self) -> u32 { - self.code + self.kind.into() } /// Returns corresponding error message of this error. @@ -181,13 +182,19 @@ impl Error { impl fmt::Debug for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{} (error code 0x{:x})", self.message(), self.code) + write!( + fmt, + "{} (error code 0x{:x}, origin 0x{:x})", + self.message(), + self.raw_code(), + self.origin().map(|v| v.into()).unwrap_or(0_u32), + ) } } impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{} (error code 0x{:x})", self.message(), self.code) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(self, f) } } @@ -200,6 +207,17 @@ impl std::error::Error for Error { impl From<ErrorKind> for Error { #[inline] fn from(kind: ErrorKind) -> Error { - Error { code: kind as u32 } + Error { kind, origin: None } } } + +#[derive(Clone, Debug, Eq, PartialEq, FromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum ErrorOrigin { + API = raw::TEEC_ORIGIN_API, + COMMS = raw::TEEC_ORIGIN_COMMS, + TEE = raw::TEEC_ORIGIN_TEE, + TA = raw::TEEC_ORIGIN_TRUSTED_APP, + #[default] + UNKNOWN, +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
