m4sterchain commented on code in PR #178: URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/178#discussion_r2033124940
########## optee-utee/src/tee_parameter.rs: ########## @@ -0,0 +1,302 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::ParamType; +use core::ops::{Index, IndexMut}; +use optee_utee_sys as raw; + +#[derive(Copy, Clone, Debug)] +pub enum ParamIndex { + Arg0, + Arg1, + Arg2, + Arg3, +} + +impl ParamIndex { + fn to_usize(self) -> usize { + match self { + ParamIndex::Arg0 => 0, + ParamIndex::Arg1 => 1, + ParamIndex::Arg2 => 2, + ParamIndex::Arg3 => 3, + } + } +} + +enum ParamContent<'a> { + None, + MemrefInput { + buffer: &'a [u8], + }, + MemrefOutput { + buffer: &'a mut [u8], + written: usize, + }, + MemrefInout { + buffer: &'a mut [u8], + written: usize, + }, + ValueInput { + a: u32, + b: u32, + }, + ValueOutput { + a: u32, + b: u32, + }, + ValueInout { + a: u32, + b: u32, + }, +} + +pub struct Param<'a> { + content: ParamContent<'a>, +} + +impl<'a> Param<'a> { + fn new() -> Self { + Self { + content: ParamContent::None, + } + } + + pub fn written_slice(&self) -> Option<&[u8]> { + match &self.content { + ParamContent::MemrefOutput { buffer, written } => Some(&buffer[..*written]), + ParamContent::MemrefInout { buffer, written } => Some(&buffer[..*written]), + _ => None, + } + } + + pub fn output_value(&self) -> Option<(u32, u32)> { + match &self.content { + ParamContent::ValueOutput { a, b } => Some((*a, *b)), + ParamContent::ValueInout { a, b } => Some((*a, *b)), + _ => None, + } + } + + fn get_type(&self) -> ParamType { + match &self.content { + ParamContent::None => ParamType::None, + ParamContent::MemrefInput { .. } => ParamType::MemrefInput, + ParamContent::MemrefOutput { .. } => ParamType::MemrefOutput, + ParamContent::MemrefInout { .. } => ParamType::MemrefInout, + ParamContent::ValueInput { .. } => ParamType::ValueInput, + ParamContent::ValueOutput { .. } => ParamType::ValueOutput, + ParamContent::ValueInout { .. } => ParamType::ValueInout, + } + } + + fn get_raw_type(&self) -> u32 { + self.get_type() as u32 + } + + fn as_raw(&mut self) -> raw::TEE_Param { + match &mut self.content { + ParamContent::None => raw::TEE_Param { + memref: raw::Memref { + buffer: core::ptr::null_mut(), + size: 0, + }, + }, + ParamContent::MemrefInput { buffer } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::MemrefOutput { buffer, written: _ } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::MemrefInout { buffer, written: _ } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::ValueInput { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + ParamContent::ValueInout { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + ParamContent::ValueOutput { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + } + } + + fn update_size_from_raw(&mut self, raw_param: &raw::TEE_Param) { + match &mut self.content { + ParamContent::MemrefOutput { buffer: _, written } => { + *written = unsafe { raw_param.memref.size }; + } + ParamContent::MemrefInout { buffer: _, written } => { + *written = unsafe { raw_param.memref.size }; + } + _ => {} + } + } + + fn update_value_from_raw(&mut self, raw_param: &raw::TEE_Param) { + match &mut self.content { + ParamContent::ValueInout { a, b } => { + *a = unsafe { raw_param.value.a }; + *b = unsafe { raw_param.value.b }; + } + ParamContent::ValueOutput { a, b } => { + *a = unsafe { raw_param.value.a }; + *b = unsafe { raw_param.value.b }; + } + _ => {} + } + } +} + +/// The TeeParams struct is used to manage the parameters for TEE commands. +pub struct TeeParams<'a> { + params: [Param<'a>; 4], +} + +impl<'a> TeeParams<'a> { + pub fn new() -> Self { + Self { + params: [Param::new(), Param::new(), Param::new(), Param::new()], + } + } + + // Method-chaining parameter setters + pub fn with_memref_in(mut self, idx: ParamIndex, buffer: &'a [u8]) -> Self { + self.params[idx.to_usize()].content = ParamContent::MemrefInput { buffer }; Review Comment: We can optimize all the `idx.to_usize()` if the `ops::IndexMut` is implemented for `[Param<'a>; 4]` or related wrapped type. ########## optee-utee/src/tee_parameter.rs: ########## @@ -0,0 +1,302 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::ParamType; +use core::ops::{Index, IndexMut}; +use optee_utee_sys as raw; + +#[derive(Copy, Clone, Debug)] +pub enum ParamIndex { + Arg0, + Arg1, + Arg2, + Arg3, +} + +impl ParamIndex { + fn to_usize(self) -> usize { + match self { + ParamIndex::Arg0 => 0, + ParamIndex::Arg1 => 1, + ParamIndex::Arg2 => 2, + ParamIndex::Arg3 => 3, + } + } +} + +enum ParamContent<'a> { + None, + MemrefInput { + buffer: &'a [u8], + }, + MemrefOutput { + buffer: &'a mut [u8], + written: usize, + }, + MemrefInout { + buffer: &'a mut [u8], + written: usize, + }, + ValueInput { + a: u32, + b: u32, + }, + ValueOutput { + a: u32, + b: u32, + }, + ValueInout { + a: u32, + b: u32, + }, +} + +pub struct Param<'a> { + content: ParamContent<'a>, +} + +impl<'a> Param<'a> { + fn new() -> Self { + Self { + content: ParamContent::None, + } + } + + pub fn written_slice(&self) -> Option<&[u8]> { + match &self.content { + ParamContent::MemrefOutput { buffer, written } => Some(&buffer[..*written]), + ParamContent::MemrefInout { buffer, written } => Some(&buffer[..*written]), + _ => None, + } + } + + pub fn output_value(&self) -> Option<(u32, u32)> { + match &self.content { + ParamContent::ValueOutput { a, b } => Some((*a, *b)), + ParamContent::ValueInout { a, b } => Some((*a, *b)), + _ => None, + } + } + + fn get_type(&self) -> ParamType { + match &self.content { + ParamContent::None => ParamType::None, + ParamContent::MemrefInput { .. } => ParamType::MemrefInput, + ParamContent::MemrefOutput { .. } => ParamType::MemrefOutput, + ParamContent::MemrefInout { .. } => ParamType::MemrefInout, + ParamContent::ValueInput { .. } => ParamType::ValueInput, + ParamContent::ValueOutput { .. } => ParamType::ValueOutput, + ParamContent::ValueInout { .. } => ParamType::ValueInout, + } + } + + fn get_raw_type(&self) -> u32 { + self.get_type() as u32 + } + + fn as_raw(&mut self) -> raw::TEE_Param { + match &mut self.content { + ParamContent::None => raw::TEE_Param { + memref: raw::Memref { + buffer: core::ptr::null_mut(), + size: 0, + }, + }, + ParamContent::MemrefInput { buffer } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::MemrefOutput { buffer, written: _ } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::MemrefInout { buffer, written: _ } => raw::TEE_Param { + memref: raw::Memref { + buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void, + size: buffer.len(), + }, + }, + ParamContent::ValueInput { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + ParamContent::ValueInout { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + ParamContent::ValueOutput { a, b } => raw::TEE_Param { + value: raw::Value { a: *a, b: *b }, + }, + } + } + + fn update_size_from_raw(&mut self, raw_param: &raw::TEE_Param) { + match &mut self.content { + ParamContent::MemrefOutput { buffer: _, written } => { + *written = unsafe { raw_param.memref.size }; Review Comment: I suggest we follow `https://github.com/rust-lang/rust-clippy/issues/9330` to explicitly add comment to `unsafe` block; and run `cargo lint` to help automatically enforce available rules. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org For additional commands, e-mail: dev-h...@teaclave.apache.org