You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by yu...@apache.org on 2025/09/30 02:58:03 UTC
(teaclave-trustzone-sdk) 01/03: optee-utee: clippy default enforcement
This is an automated email from the ASF dual-hosted git repository.
yuanz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/teaclave-trustzone-sdk.git
commit 3847baa34f23b9a44710ab8cc83987c7c131271d
Author: Yuan Zhuang <yu...@apache.org>
AuthorDate: Wed Sep 24 02:39:04 2025 +0000
optee-utee: clippy default enforcement
"source environment && cd optee-utee && cargo clippy --target aarch64-unknown-linux-gnu -- -D warnings"
---
.github/workflows/reuse_test.yml | 3 ++
optee-utee/macros/src/lib.rs | 49 ++++++-------------
optee-utee/optee-utee-sys/src/user_ta_header.rs | 12 +++--
optee-utee/src/arithmetical.rs | 12 ++---
optee-utee/src/crypto_op.rs | 34 ++++++-------
optee-utee/src/error.rs | 20 ++++----
optee-utee/src/extension.rs | 6 +--
optee-utee/src/net/optee.rs | 4 +-
optee-utee/src/object/attribute.rs | 2 +-
optee-utee/src/object/object_define.rs | 6 +--
optee-utee/src/object/object_info.rs | 2 +-
optee-utee/src/object/persistent_object.rs | 2 +-
optee-utee/src/object/transient_object.rs | 2 +-
optee-utee/src/parameter.rs | 8 +++-
optee-utee/src/property.rs | 64 +++++++++++++------------
optee-utee/src/tee_parameter.rs | 10 ++--
optee-utee/src/time.rs | 6 +++
17 files changed, 123 insertions(+), 119 deletions(-)
diff --git a/.github/workflows/reuse_test.yml b/.github/workflows/reuse_test.yml
index 9272d6c..6980089 100644
--- a/.github/workflows/reuse_test.yml
+++ b/.github/workflows/reuse_test.yml
@@ -59,6 +59,9 @@ jobs:
(cd optee-utee && cargo build --target aarch64-unknown-linux-gnu -vv)
(cd optee-teec && cargo build --target aarch64-unknown-linux-gnu -vv)
+ # Clippy checks
+ (cd optee-utee && cargo clippy --target aarch64-unknown-linux-gnu -- -D warnings)
+
# Cross-compile on host and run tests in QEMU
#
# Cross-compile target pairs:
diff --git a/optee-utee/macros/src/lib.rs b/optee-utee/macros/src/lib.rs
index 95142dd..ec667a5 100644
--- a/optee-utee/macros/src/lib.rs
+++ b/optee-utee/macros/src/lib.rs
@@ -40,10 +40,7 @@ pub fn ta_create(_args: TokenStream, input: TokenStream) -> TokenStream {
// check the function signature
let valid_signature = f.constness.is_none()
- && match f.vis {
- syn::Visibility::Inherited => true,
- _ => false,
- }
+ && matches!(f.vis, syn::Visibility::Inherited)
&& f.abi.is_none()
&& f.decl.inputs.is_empty()
&& f.decl.generics.where_clause.is_none()
@@ -87,18 +84,12 @@ pub fn ta_destroy(_args: TokenStream, input: TokenStream) -> TokenStream {
// check the function signature
let valid_signature = f.constness.is_none()
- && match f.vis {
- syn::Visibility::Inherited => true,
- _ => false,
- }
+ && matches!(f.vis, syn::Visibility::Inherited)
&& f.abi.is_none()
&& f.decl.inputs.is_empty()
&& f.decl.generics.where_clause.is_none()
&& f.decl.variadic.is_none()
- && match f.decl.output {
- syn::ReturnType::Default => true,
- _ => false,
- };
+ && matches!(f.decl.output, syn::ReturnType::Default);
if !valid_signature {
return syn::parse::Error::new(
@@ -141,10 +132,7 @@ pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
// check the function signature
let valid_signature = f.constness.is_none()
- && match f.vis {
- syn::Visibility::Inherited => true,
- _ => false,
- }
+ && matches!(f.vis, syn::Visibility::Inherited)
&& f.abi.is_none()
&& (f.decl.inputs.len() == 1 || f.decl.inputs.len() == 2)
&& f.decl.generics.where_clause.is_none()
@@ -184,12 +172,12 @@ pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
.inputs
.iter()
.map(|arg| match arg {
- &syn::FnArg::Captured(ref val) => &val.ty,
+ syn::FnArg::Captured(val) => &val.ty,
_ => unreachable!(),
})
.collect();
let ctx_type = match input_types.last().unwrap() {
- &syn::Type::Reference(ref r) => &r.elem,
+ syn::Type::Reference(r) => &r.elem,
_ => unreachable!(),
};
@@ -241,18 +229,12 @@ pub fn ta_close_session(_args: TokenStream, input: TokenStream) -> TokenStream {
// check the function signature
let valid_signature = f.constness.is_none()
- && match f.vis {
- syn::Visibility::Inherited => true,
- _ => false,
- }
+ && matches!(f.vis, syn::Visibility::Inherited)
&& f.abi.is_none()
- && (f.decl.inputs.len() == 0 || f.decl.inputs.len() == 1)
+ && (f.decl.inputs.is_empty() || f.decl.inputs.len() == 1)
&& f.decl.generics.where_clause.is_none()
&& f.decl.variadic.is_none()
- && match f.decl.output {
- syn::ReturnType::Default => true,
- _ => false,
- };
+ && matches!(f.decl.output, syn::ReturnType::Default);
if !valid_signature {
return syn::parse::Error::new(
@@ -279,12 +261,12 @@ pub fn ta_close_session(_args: TokenStream, input: TokenStream) -> TokenStream {
.inputs
.iter()
.map(|arg| match arg {
- &syn::FnArg::Captured(ref val) => &val.ty,
+ syn::FnArg::Captured(val) => &val.ty,
_ => unreachable!(),
})
.collect();
let t = match input_types.first().unwrap() {
- &syn::Type::Reference(ref r) => &r.elem,
+ syn::Type::Reference(r) => &r.elem,
_ => unreachable!(),
};
@@ -328,10 +310,7 @@ pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream
// check the function signature
let valid_signature = f.constness.is_none()
- && match f.vis {
- syn::Visibility::Inherited => true,
- _ => false,
- }
+ && matches!(f.vis, syn::Visibility::Inherited)
&& f.abi.is_none()
&& (f.decl.inputs.len() == 2 || f.decl.inputs.len() == 3)
&& f.decl.generics.where_clause.is_none()
@@ -373,12 +352,12 @@ pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream
.inputs
.iter()
.map(|arg| match arg {
- &syn::FnArg::Captured(ref val) => &val.ty,
+ syn::FnArg::Captured(val) => &val.ty,
_ => unreachable!(),
})
.collect();
let t = match input_types.first().unwrap() {
- &syn::Type::Reference(ref r) => &r.elem,
+ syn::Type::Reference(r) => &r.elem,
_ => unreachable!(),
};
diff --git a/optee-utee/optee-utee-sys/src/user_ta_header.rs b/optee-utee/optee-utee-sys/src/user_ta_header.rs
index bc5f8de..0d0c7eb 100644
--- a/optee-utee/optee-utee-sys/src/user_ta_header.rs
+++ b/optee-utee/optee-utee-sys/src/user_ta_header.rs
@@ -41,11 +41,17 @@ extern "C" {
pub fn __utee_entry(func: c_ulong, session_id: c_ulong, up: *mut utee_params, cmd_id: c_ulong) -> TEE_Result;
}
+/// # Safety
+/// This function is the main entry point for a Trusted Application (TA) in OP-TEE.
+/// It must only be called by the OP-TEE OS with valid parameters. The `up` parameter
+/// is a raw pointer that must point to a valid `utee_params` structure initialized
+/// by the OP-TEE runtime environment. This function should never be called directly
+/// from user code - it is only exported for the OP-TEE OS loader.
#[no_mangle]
-pub fn __ta_entry(func: c_ulong, session_id: c_ulong, up: *mut utee_params, cmd_id: c_ulong) -> ! {
- let res: u32 = unsafe { __utee_entry(func, session_id, up, cmd_id) };
+unsafe fn __ta_entry(func: c_ulong, session_id: c_ulong, up: *mut utee_params, cmd_id: c_ulong) -> ! {
+ let res: u32 = __utee_entry(func, session_id, up, cmd_id);
- unsafe { _utee_return(res.into()) };
+ _utee_return(res.into());
}
unsafe impl Sync for ta_head {}
diff --git a/optee-utee/src/arithmetical.rs b/optee-utee/src/arithmetical.rs
index ecb3ade..259898e 100644
--- a/optee-utee/src/arithmetical.rs
+++ b/optee-utee/src/arithmetical.rs
@@ -34,13 +34,13 @@ impl BigInt {
// size represents BigInt bits
pub fn size_in_u32(size: u32) -> u32 {
- return ((size + 31) / 32) + 2;
+ ((size + 31) / 32) + 2
}
pub fn new(bits: u32) -> Self {
let size: usize = Self::size_in_u32(bits) as usize;
let mut tmp_vec: Vec<BigIntUnit> = vec![0; size];
- unsafe { raw::TEE_BigIntInit(tmp_vec.as_mut_ptr(), size as usize) };
+ unsafe { raw::TEE_BigIntInit(tmp_vec.as_mut_ptr(), size) };
Self(tmp_vec)
}
@@ -49,7 +49,7 @@ impl BigInt {
raw::TEE_BigIntConvertFromOctetString(
self.0.as_mut_ptr(),
buffer.as_ptr(),
- buffer.len() as usize,
+ buffer.len(),
sign,
)
} {
@@ -70,7 +70,7 @@ impl BigInt {
} {
raw::TEE_SUCCESS => {
tmp_vec.truncate(buffer_size);
- return Ok(tmp_vec);
+ Ok(tmp_vec)
}
code => Err(Error::from_raw_error(code)),
}
@@ -272,7 +272,7 @@ impl BigIntFMMContext {
// Globalplatform define FMMContext1 here while OP-TEE does not update yet
pub fn new(bits: u32, modulus: BigInt) -> Result<Self> {
- let size: usize = Self::size_in_u32(bits as usize) as usize;
+ let size: usize = Self::size_in_u32(bits as usize);
let mut tmp_vec: Vec<BigIntFMMContextUnit> = vec![0; size];
unsafe {
raw::TEE_BigIntInitFMMContext(tmp_vec.as_mut_ptr(), size, modulus.data_ptr())
@@ -293,7 +293,7 @@ impl BigIntFMM {
}
pub fn new(bits: u32) -> Self {
- let size: usize = Self::size_in_u32(bits as usize) as usize;
+ let size: usize = Self::size_in_u32(bits as usize);
let mut tmp_vec: Vec<BigIntFMMUnit> = vec![0; size];
unsafe { raw::TEE_BigIntInitFMM(tmp_vec.as_mut_ptr(), size) };
Self(tmp_vec)
diff --git a/optee-utee/src/crypto_op.rs b/optee-utee/src/crypto_op.rs
index 17cafad..260c2ce 100644
--- a/optee-utee/src/crypto_op.rs
+++ b/optee-utee/src/crypto_op.rs
@@ -217,7 +217,7 @@ impl OperationHandle {
fn set_key<T: GenericObject>(&self, object: &T) -> Result<()> {
match unsafe { raw::TEE_SetOperationKey(self.handle(), object.handle()) } {
- raw::TEE_SUCCESS => return Ok(()),
+ raw::TEE_SUCCESS => Ok(()),
code => Err(Error::from_raw_error(code)),
}
}
@@ -243,7 +243,7 @@ pub fn is_algorithm_supported(alg_id: u32, element: u32) -> Result<()> {
impl Drop for OperationHandle {
fn drop(&mut self) {
unsafe {
- if self.raw != ptr::null_mut() {
+ if !self.raw.is_null() {
raw::TEE_FreeOperation(self.handle());
}
drop(Box::from_raw(self.raw));
@@ -336,7 +336,7 @@ impl Digest {
&mut hash_size,
)
} {
- raw::TEE_SUCCESS => return Ok(hash_size),
+ raw::TEE_SUCCESS => Ok(hash_size),
code => Err(Error::from_raw_error(code)),
}
}
@@ -622,7 +622,7 @@ impl Cipher {
)
} {
raw::TEE_SUCCESS => {
- return Ok(dest_size as usize);
+ Ok(dest_size)
}
code => Err(Error::from_raw_error(code)),
}
@@ -657,7 +657,7 @@ impl Cipher {
&mut dest_size,
)
} {
- raw::TEE_SUCCESS => return Ok(dest_size as usize),
+ raw::TEE_SUCCESS => Ok(dest_size),
code => Err(Error::from_raw_error(code)),
}
}
@@ -746,7 +746,7 @@ impl Cipher {
match unsafe {
raw::TEE_SetOperationKey2(self.handle(), object1.handle(), object2.handle())
} {
- raw::TEE_SUCCESS => return Ok(()),
+ raw::TEE_SUCCESS => Ok(()),
code => Err(Error::from_raw_error(code)),
}
}
@@ -1008,7 +1008,7 @@ impl AE {
pay_load_len,
)
} {
- raw::TEE_SUCCESS => return Ok(()),
+ raw::TEE_SUCCESS => Ok(()),
code => Err(Error::from_raw_error(code)),
}
}
@@ -1068,7 +1068,7 @@ impl AE {
)
} {
raw::TEE_SUCCESS => {
- return Ok(dest_size);
+ Ok(dest_size)
}
code => Err(Error::from_raw_error(code)),
}
@@ -1156,7 +1156,7 @@ impl AE {
)
} {
raw::TEE_SUCCESS => {
- return Ok((dest_size, tag_size));
+ Ok((dest_size, tag_size))
}
code => Err(Error::from_raw_error(code)),
}
@@ -1199,7 +1199,7 @@ impl AE {
)
} {
raw::TEE_SUCCESS => {
- return Ok(dest_size);
+ Ok(dest_size)
}
code => Err(Error::from_raw_error(code)),
}
@@ -1314,7 +1314,7 @@ impl Asymmetric {
pub fn encrypt(&self, params: &[Attribute], src: &[u8]) -> Result<Vec<u8>> {
let p: Vec<raw::TEE_Attribute> = params.iter().map(|p| p.raw()).collect();
let mut res_size: usize = self.info().key_size() as usize;
- let mut res_vec: Vec<u8> = vec![0u8; res_size as usize];
+ let mut res_vec: Vec<u8> = vec![0u8; res_size];
match unsafe {
raw::TEE_AsymmetricEncrypt(
self.handle(),
@@ -1328,7 +1328,7 @@ impl Asymmetric {
} {
raw::TEE_SUCCESS => {
res_vec.truncate(res_size);
- return Ok(res_vec);
+ Ok(res_vec)
}
code => Err(Error::from_raw_error(code)),
}
@@ -1357,7 +1357,7 @@ impl Asymmetric {
pub fn decrypt(&self, params: &[Attribute], src: &[u8]) -> Result<Vec<u8>> {
let p: Vec<raw::TEE_Attribute> = params.iter().map(|p| p.raw()).collect();
let mut res_size: usize = self.info().key_size() as usize;
- let mut res_vec: Vec<u8> = vec![0u8; res_size as usize];
+ let mut res_vec: Vec<u8> = vec![0u8; res_size];
match unsafe {
raw::TEE_AsymmetricDecrypt(
self.handle(),
@@ -1370,8 +1370,8 @@ impl Asymmetric {
)
} {
raw::TEE_SUCCESS => {
- res_vec.truncate(res_size as usize);
- return Ok(res_vec);
+ res_vec.truncate(res_size);
+ Ok(res_vec)
}
code => Err(Error::from_raw_error(code)),
}
@@ -1418,7 +1418,7 @@ impl Asymmetric {
)
} {
raw::TEE_SUCCESS => {
- return Ok(signature_size);
+ Ok(signature_size)
}
code => Err(Error::from_raw_error(code)),
}
@@ -1537,7 +1537,7 @@ impl DeriveKey {
/// key_pair_1.generate_key(256, &[attr_prime.into(), attr_base.into()])?;
/// key_pair_1.ref_attribute(AttributeId::DhPublicValue, &mut public_1)?;
/// Ok(())
- /// },
+ /// }
/// Err(e) => Err(e),
/// }
/// # }
diff --git a/optee-utee/src/error.rs b/optee-utee/src/error.rs
index fabf6ca..35f69d0 100644
--- a/optee-utee/src/error.rs
+++ b/optee-utee/src/error.rs
@@ -288,12 +288,12 @@ impl From<ErrorKind> for Error {
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[repr(u32)]
pub enum ErrorOrigin {
- API = raw::TEE_ORIGIN_API,
- COMMS = raw::TEE_ORIGIN_COMMS,
- TEE = raw::TEE_ORIGIN_TEE,
- TA = raw::TEE_ORIGIN_TRUSTED_APP,
+ Api = raw::TEE_ORIGIN_API,
+ Comms = raw::TEE_ORIGIN_COMMS,
+ Tee = raw::TEE_ORIGIN_TEE,
+ Ta = raw::TEE_ORIGIN_TRUSTED_APP,
#[default]
- UNKNOWN,
+ Unknown,
}
impl From<ErrorOrigin> for u32 {
@@ -305,11 +305,11 @@ impl From<ErrorOrigin> for u32 {
impl From<u32> for ErrorOrigin {
fn from(code: u32) -> ErrorOrigin {
match code {
- raw::TEE_ORIGIN_API => ErrorOrigin::API,
- raw::TEE_ORIGIN_COMMS => ErrorOrigin::COMMS,
- raw::TEE_ORIGIN_TEE => ErrorOrigin::TEE,
- raw::TEE_ORIGIN_TRUSTED_APP => ErrorOrigin::TA,
- _ => ErrorOrigin::UNKNOWN,
+ raw::TEE_ORIGIN_API => ErrorOrigin::Api,
+ raw::TEE_ORIGIN_COMMS => ErrorOrigin::Comms,
+ raw::TEE_ORIGIN_TEE => ErrorOrigin::Tee,
+ raw::TEE_ORIGIN_TRUSTED_APP => ErrorOrigin::Ta,
+ _ => ErrorOrigin::Unknown,
}
}
}
diff --git a/optee-utee/src/extension.rs b/optee-utee/src/extension.rs
index 175053a..50e86b6 100644
--- a/optee-utee/src/extension.rs
+++ b/optee-utee/src/extension.rs
@@ -110,12 +110,12 @@ impl LoadablePlugin {
/// # }
/// ```
/// Notice: the shared buffer could grow to fit the request data automatically.
- pub fn invoke_with_capacity<'a>(
- &'a self,
+ pub fn invoke_with_capacity(
+ &self,
command_id: u32,
subcommand_id: u32,
capacity: usize,
- ) -> LoadablePluginCommand<'a> {
+ ) -> LoadablePluginCommand<'_> {
LoadablePluginCommand::new_with_capacity(self, command_id, subcommand_id, capacity)
}
}
diff --git a/optee-utee/src/net/optee.rs b/optee-utee/src/net/optee.rs
index b198319..a7bbb58 100644
--- a/optee-utee/src/net/optee.rs
+++ b/optee-utee/src/net/optee.rs
@@ -110,7 +110,7 @@ impl SocketAdapter for TcpAdapter {
let ret = unsafe {
((*raw::TEE_tcpSocket).send)(
handle.0,
- buf.as_ptr() as *const u8 as _,
+ buf.as_ptr() as _,
&mut length,
timeout,
)
@@ -170,7 +170,7 @@ impl SocketAdapter for UdpAdapter {
let ret = unsafe {
((*raw::TEE_udpSocket).send)(
handle.0,
- buf.as_ptr() as *const u8 as _,
+ buf.as_ptr() as _,
&mut length,
timeout,
)
diff --git a/optee-utee/src/object/attribute.rs b/optee-utee/src/object/attribute.rs
index caa8e07..91ed92c 100644
--- a/optee-utee/src/object/attribute.rs
+++ b/optee-utee/src/object/attribute.rs
@@ -66,7 +66,7 @@ impl<'attrref> AttributeMemref<'attrref> {
attributeID: 0,
content: raw::content {
memref: raw::Memref {
- buffer: 0 as *mut _,
+ buffer: core::ptr::null_mut(),
size: 0,
},
},
diff --git a/optee-utee/src/object/object_define.rs b/optee-utee/src/object/object_define.rs
index a5586ae..88eac28 100644
--- a/optee-utee/src/object/object_define.rs
+++ b/optee-utee/src/object/object_define.rs
@@ -29,9 +29,9 @@ pub enum Whence {
DataSeekEnd,
}
-impl Into<raw::TEE_Whence> for Whence {
- fn into(self) -> raw::TEE_Whence {
- match self {
+impl From<Whence> for raw::TEE_Whence {
+ fn from(val: Whence) -> Self {
+ match val {
Whence::DataSeekSet => raw::TEE_Whence::TEE_DATA_SEEK_SET,
Whence::DataSeekCur => raw::TEE_Whence::TEE_DATA_SEEK_CUR,
Whence::DataSeekEnd => raw::TEE_Whence::TEE_DATA_SEEK_END,
diff --git a/optee-utee/src/object/object_info.rs b/optee-utee/src/object/object_info.rs
index 33e6e17..59aea11 100644
--- a/optee-utee/src/object/object_info.rs
+++ b/optee-utee/src/object/object_info.rs
@@ -63,7 +63,7 @@ impl ObjectInfo {
/// Return the `dataSize` field of the raw structure `TEE_ObjectInfo`.
pub fn data_size(&self) -> usize {
- self.raw.dataSize as usize
+ self.raw.dataSize
}
/// Return the `objectSize` field of the raw structure `TEE_ObjectInfo`.
diff --git a/optee-utee/src/object/persistent_object.rs b/optee-utee/src/object/persistent_object.rs
index 706ad10..35fbcec 100644
--- a/optee-utee/src/object/persistent_object.rs
+++ b/optee-utee/src/object/persistent_object.rs
@@ -262,7 +262,7 @@ impl PersistentObject {
// So we must forget the raw_handle to prevent calling TEE_CloseObject
// on it (no matter the result of TEE_CloseAndDeletePersistentObject1).
self.0.forget();
- return result;
+ result
}
/// Changes the identifier of an object.
diff --git a/optee-utee/src/object/transient_object.rs b/optee-utee/src/object/transient_object.rs
index 44f985f..f0f5794 100644
--- a/optee-utee/src/object/transient_object.rs
+++ b/optee-utee/src/object/transient_object.rs
@@ -253,7 +253,7 @@ impl TransientObject {
raw::TEE_PopulateTransientObject(self.0.handle(), p.as_ptr() as _, attrs.len() as u32)
} {
raw::TEE_SUCCESS => Ok(()),
- code => return Err(Error::from_raw_error(code)),
+ code => Err(Error::from_raw_error(code)),
}
}
diff --git a/optee-utee/src/parameter.rs b/optee-utee/src/parameter.rs
index 02b771a..120fe92 100644
--- a/optee-utee/src/parameter.rs
+++ b/optee-utee/src/parameter.rs
@@ -74,7 +74,7 @@ pub struct ParamMemref<'parameter> {
impl<'parameter> ParamMemref<'parameter> {
pub fn buffer(&mut self) -> &mut [u8] {
unsafe {
- slice::from_raw_parts_mut((*self.raw).buffer as *mut u8, (*self.raw).size as usize)
+ slice::from_raw_parts_mut((*self.raw).buffer as *mut u8, (*self.raw).size)
}
}
@@ -100,10 +100,12 @@ impl Parameter {
pub fn from_raw(ptr: *mut raw::TEE_Param, param_type: ParamType) -> Self {
Self {
raw: ptr,
- param_type: param_type,
+ param_type,
}
}
+ /// # Safety
+ /// The caller must ensure that the raw pointer is valid and points to a properly initialized TEE_Param.
pub unsafe fn as_value(&mut self) -> Result<ParamValue> {
match self.param_type {
ParamType::ValueInput | ParamType::ValueInout | ParamType::ValueOutput => {
@@ -117,6 +119,8 @@ impl Parameter {
}
}
+ /// # Safety
+ /// The caller must ensure that the raw pointer is valid and points to a properly initialized TEE_Param.
pub unsafe fn as_memref(&mut self) -> Result<ParamMemref> {
match self.param_type {
ParamType::MemrefInout | ParamType::MemrefInput | ParamType::MemrefOutput => {
diff --git a/optee-utee/src/property.rs b/optee-utee/src/property.rs
index 2a033a1..bcab863 100644
--- a/optee-utee/src/property.rs
+++ b/optee-utee/src/property.rs
@@ -46,18 +46,22 @@ impl PropertySet {
/// The property value is obtained from the TEE
/// property set using the TEE_GetPropertyAs* functions.
pub trait PropertyValue: Sized {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self>;
+ /// # Safety
+ /// This function is unsafe because it dereferences raw pointers from the TEE API.
+ /// The caller must ensure that the `set` handle is valid and that the TEE environment
+ /// is properly initialized.
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self>;
}
/// Implements the PropertyValue trait for all return types:
/// String, Bool, u32, u64, BinaryBlock, UUID, Identity.
impl PropertyValue for String {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut out_size = 0;
// The first call is to get the size of the string
// So we pass a null pointer and a size of 0
- let res = unsafe {
+ let res = {
raw::TEE_GetPropertyAsString(
set,
key.as_ptr() as *const core::ffi::c_char,
@@ -69,16 +73,16 @@ impl PropertyValue for String {
raw::TEE_SUCCESS => {
if out_size == 0 {
// return an empty string
- return Ok(String::new());
+ Ok(String::new())
}
else {
- return Err(Error::new(ErrorKind::Generic));
+ Err(Error::new(ErrorKind::Generic))
}
}
raw::TEE_ERROR_SHORT_BUFFER => {
// Resize the string to the actual size
- let mut out_buffer = vec![0; out_size as usize];
- let res = unsafe {
+ let mut out_buffer = vec![0; out_size];
+ let res = {
raw::TEE_GetPropertyAsString(
set,
key.as_ptr() as *const core::ffi::c_char,
@@ -99,17 +103,17 @@ impl PropertyValue for String {
Ok(result)
}
_ => {
- return Err(Error::from_raw_error(res));
+ Err(Error::from_raw_error(res))
}
}
}
}
impl PropertyValue for bool {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut b: bool = false;
- let res = unsafe { raw::TEE_GetPropertyAsBool(set, key.as_ptr() as *const core::ffi::c_char, &mut b) };
+ let res = raw::TEE_GetPropertyAsBool(set, key.as_ptr() as *const core::ffi::c_char, &mut b);
if res != 0 {
return Err(Error::from_raw_error(res));
}
@@ -119,10 +123,10 @@ impl PropertyValue for bool {
}
impl PropertyValue for u32 {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut value = 0;
- let res = unsafe { raw::TEE_GetPropertyAsU32(set, key.as_ptr() as *const core::ffi::c_char, &mut value) };
+ let res = raw::TEE_GetPropertyAsU32(set, key.as_ptr() as *const core::ffi::c_char, &mut value);
if res != 0 {
return Err(Error::from_raw_error(res));
}
@@ -132,10 +136,10 @@ impl PropertyValue for u32 {
}
impl PropertyValue for u64 {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut value = 0;
- let res = unsafe { raw::TEE_GetPropertyAsU64(set, key.as_ptr() as *const core::ffi::c_char, &mut value) };
+ let res = raw::TEE_GetPropertyAsU64(set, key.as_ptr() as *const core::ffi::c_char, &mut value);
if res != 0 {
return Err(Error::from_raw_error(res));
}
@@ -145,12 +149,12 @@ impl PropertyValue for u64 {
}
impl PropertyValue for Vec<u8> {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut out_size = 0;
// The first call is to get the size of the binary block
// So we pass a null pointer and a size of 0
- let res = unsafe {
+ let res = {
raw::TEE_GetPropertyAsBinaryBlock(
set,
key.as_ptr() as *const core::ffi::c_char,
@@ -163,16 +167,16 @@ impl PropertyValue for Vec<u8> {
raw::TEE_SUCCESS => {
if out_size == 0 {
// return an empty buffer
- return Ok(vec![]);
+ Ok(vec![])
}
else {
- return Err(Error::new(ErrorKind::Generic));
+ Err(Error::new(ErrorKind::Generic))
}
}
raw::TEE_ERROR_SHORT_BUFFER => {
- let mut buf = vec![0; out_size as usize];
+ let mut buf = vec![0; out_size];
- let res = unsafe {
+ let res = {
raw::TEE_GetPropertyAsBinaryBlock(
set,
key.as_ptr() as *const core::ffi::c_char,
@@ -181,20 +185,20 @@ impl PropertyValue for Vec<u8> {
)
};
if res != raw::TEE_SUCCESS {
- return Err(Error::from_raw_error(res));
+ Err(Error::from_raw_error(res))
+ } else {
+ Ok(buf)
}
-
- Ok(buf)
}
_ => {
- return Err(Error::from_raw_error(res));
+ Err(Error::from_raw_error(res))
}
}
}
}
impl PropertyValue for Uuid {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
let mut raw_uuid = raw::TEE_UUID {
timeLow: 0,
timeMid: 0,
@@ -203,7 +207,7 @@ impl PropertyValue for Uuid {
};
let res =
- unsafe { raw::TEE_GetPropertyAsUUID(set, key.as_ptr() as *const core::ffi::c_char, &mut raw_uuid) };
+ raw::TEE_GetPropertyAsUUID(set, key.as_ptr() as *const core::ffi::c_char, &mut raw_uuid);
if res != 0 {
return Err(Error::from_raw_error(res));
}
@@ -213,7 +217,7 @@ impl PropertyValue for Uuid {
}
impl PropertyValue for Identity {
- fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+ unsafe fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
// Allocate a buffer for the raw identity
let mut raw_id = raw::TEE_Identity {
login: 0,
@@ -225,9 +229,7 @@ impl PropertyValue for Identity {
},
};
- let res = unsafe {
- raw::TEE_GetPropertyAsIdentity(set, key.as_ptr() as *const core::ffi::c_char, &mut raw_id)
- };
+ let res = raw::TEE_GetPropertyAsIdentity(set, key.as_ptr() as *const core::ffi::c_char, &mut raw_id);
if res != 0 {
return Err(Error::from_raw_error(res));
}
@@ -248,7 +250,7 @@ pub trait PropertyKey {
fn set(&self) -> PropertySet;
fn get(&self) -> Result<Self::Output> {
- Self::Output::from_raw(self.set().as_raw(), self.key())
+ unsafe { Self::Output::from_raw(self.set().as_raw(), self.key()) }
}
}
diff --git a/optee-utee/src/tee_parameter.rs b/optee-utee/src/tee_parameter.rs
index 55a00e6..bce51c6 100644
--- a/optee-utee/src/tee_parameter.rs
+++ b/optee-utee/src/tee_parameter.rs
@@ -176,9 +176,7 @@ impl<'a> Param<'a> {
*written = new_size;
Ok(())
}
- _ => {
- return Err(Error::new(ErrorKind::BadFormat));
- }
+ _ => Err(Error::new(ErrorKind::BadFormat)),
}
}
@@ -214,6 +212,12 @@ pub struct TeeParams<'a> {
params: [Param<'a>; 4],
}
+impl<'a> Default for TeeParams<'a> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
impl<'a> TeeParams<'a> {
pub fn new() -> Self {
Self {
diff --git a/optee-utee/src/time.rs b/optee-utee/src/time.rs
index 47d64c7..2a27a54 100644
--- a/optee-utee/src/time.rs
+++ b/optee-utee/src/time.rs
@@ -27,6 +27,12 @@ pub struct Time {
pub millis: u32,
}
+impl Default for Time {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
impl Time {
/// Create a new empty time structure.
pub fn new() -> Self {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org