DemesneGH commented on code in PR #177:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/177#discussion_r2024179471


##########
optee-utee/src/tee_parameter.rs:
##########
@@ -0,0 +1,223 @@
+// 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, ParamTypes};
+use core::{marker::PhantomData, slice};
+use optee_utee_sys as raw;
+
+pub trait TeeParam {
+    fn into_raw(&mut self) -> raw::TEE_Param;
+    fn param_type(&self) -> ParamType;
+    fn from_raw(raw: raw::TEE_Param, param_type: ParamType) -> Self;
+}
+
+pub struct TeeParameters<A, B, C, D> {
+    raw: [raw::TEE_Param; 4],

Review Comment:
   I've tried multiple designs, and my thought process is as follows:  
   
   The first version:  
   ```rust
   pub struct TeeParameters(p0: Parameter, p1: Parameter, p2: Parameter, p3: 
Parameter);
   ```
   Here, `Parameter` is a union that can be one of three types: 
`TeeParamMemref`, `TeeParamValue`, or `TeeParamNone`.  
   ```rust
   impl From<TeeParamMemref> for Parameter { ... }
   ```
   However, when using this approach, we must explicitly call `.into()` like 
this:  
   ```rust
   let mut parameters = Parameters(param_in.into(), param_out.into(), 
ParamNone.into(), ParamNone.into()); 
   ```
   This `.into()` usage is not user-friendly, so I refined it in the second 
version:  
   
   ```rust
   pub struct TeeParameters<A, B, C, D> {
       p0: A,
       p1: B,
       p2: C,
       p3: D,
   }
   pub trait TeeParam { ... }
   impl TeeParam for TeeParamValue {...}
   impl TeeParam for TeeParamMemref {...}
   impl TeeParam for TeeParamNone {...}
   ```
   To pass the parameters as an array to the raw API, I dynamically allocate 
the array. The pseudo-code is:  
   ```rust
   impl<A: TeeParam, B: TeeParam, C: TeeParam, D: TeeParam> TeeParameters<A, B, 
C, D> {
       pub fn as_array(&self) -> [TEE_Param; 4] {
           // Convert each parameter to its raw representation
           [raw_p0, raw_p1, raw_p2, raw_p3]
       }
   }
   
   To invoke raw API:
   raw::TEE_InvokeTACommand(params: params.as_array().as_mut_ptr(), ...)
   ```
   However, this design has a flaw: we don't retain the raw array `[TEE_Param; 
4]` address (which is passed directly to the raw API), so we cannot retrieve 
the output values.  
   
   For example, in `hello_world`, the [TEE_Param; 4] memory layout looks like 
this:  
   ```
   [
   p0_inout_value_a, p0_inout_value_b,  // If a parameter is of type Value, it 
has two fields (a, b)
    none,  // p1
    none,  // p2
    none. // p3
   ]
   ```
   When the TA writes back to `p0_inout_value_a`, we must maintain the array 
address to properly fetch the updated value.  
   
   Then comes to the current definition, which is similar to existing 
`optee-teec/operation`:  
   ```rust
   pub struct TeeParameters<A, B, C, D> {
       raw: [raw::TEE_Param; 4],  // Maintains the raw buffer for interaction 
with raw API
       param_types: ParamTypes,
       phantom0: PhantomData<A>,
       phantom1: PhantomData<B>,
       phantom2: PhantomData<C>,
       phantom3: PhantomData<D>,
   }
   ```
   This structure ensures that we correctly track and interact with the 
parameter buffer, and use trait to eliminate `into()`.



-- 
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

Reply via email to