ivila commented on issue #153:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/issues/153#issuecomment-2515986613

   @DemesneGH 
   
   ## What's wrong
   Sorry, my previous solution has some mistakes and is misleading, there are 
limitations that you can not get data from ree more than `len` bytes:
   
https://github.com/apache/incubator-teaclave-trustzone-sdk/blob/bc14fb63f65bfff77456eea1b9097c1619d30b04/optee-utee/optee-utee-sys/src/tee_internal_api_extensions.rs#L21-L39
   Because OP-TEE will map your memory by the parameter `len`, see the 
[codes](https://github.com/OP-TEE/optee_os/blob/a1a90747178affa60869cf5f6506ac4872ad7a4d/lib/libutee/tee_system_pta.c#L81-L111):
   
![image](https://github.com/user-attachments/assets/62eeddb1-204a-484e-943d-4c769c3c988b)
   
   So even you have buffer more than `len` bytes, and you did copy more than 
this in plugin, and set out_len greater than len, you will only get `len` bytes 
copy to you buffer. 
   Therefore, when your response size greater than you request size, you must 
not use the request_size as `len` parameter, it should be `max(request_size, 
response_size)`.
   
   ## Test
   You can try it by the following test.
   Prepare buffer of 32 bytes in both plugin and TA side, and TA use sub_cmd to 
tell plugin to copy how many bytes into the buffer, and set the out_len.
   In Plugin:
   ```C
   static TEEC_Result plugin_invoke(unsigned int cmd, unsigned int sub_cmd,
                                           void *data, size_t data_len,
                                           size_t *out_len)
   {
       const char bytes[] = 
"1111111111111111111111111111111111111111111111111111111111111111";
       memcpy(data, bytes, sub_cmd);
       *out_len = sub_cmd;
       return TEEC_SUCCESS;
   }
   ```
   In TA:
   ```C
   static TEE_Result plugin_copy(uint32_t input_size, uint32_t request_size)
   {
       TEE_Result res = TEE_SUCCESS;
       char buffer[32] = {0};
       TEE_UUID uuid = PLUGIN_UUID;
       size_t out_len = 0;
       res = tee_invoke_supp_plugin(&uuid, 0, request_size, buffer, input_size, 
&out_len);
       if (res)
           EMSG("invoke plugin failed with code 0x%x", res);
   
       assert(out_len == request_size);
       printf("data: ");
       for (size_t i = 0; i < out_len; i++) {
           printf("%d,", buffer[i]);
       }
       printf("\n");
       return res;
   }
   
   TEE_Result TA_InvokeCommandEntryPoint(void __unused *sess_ctx,
                                         uint32_t __unused cmd_id, uint32_t 
__unused param_types,
                                         TEE_Param __unused params[4])
   {
       // request size > response size, out_len is 20,
       // print 49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49
       // work as expected
       plugin_copy(32, 20);
       // request size = response size, out_len is  32,
       // print 
49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49
       // work as expected
       plugin_copy(32, 32);
       // request size < response size, out_len is 32,
       // print 
49,49,49,49,49,49,49,49,49,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       // just copy len of request size (which is 10), not working
       plugin_copy(10, 32);
       return TEE_SUCCESS;
   }
   ```
   you can just download this  
[plugins.zip](https://github.com/user-attachments/files/18001328/plugins.zip), 
and run the test.
   
   ## My Scenario
   I am trying deep learning in TEE, which I will need to copy large amount of 
data from REE(getting training data), request size just several bytes(for 
example, the filename and offset), but the response size is huge(for example, 
up to 100M), so setting a const ShareBuffer is not working for me, and making a 
copy is not acceptable too(consumes too many memory).
   
   ## How do we know the request size
   During my development, in 2 ways:
   1. a zerocopy struct: like what we used to do in C, both TEE and REE know 
the size of it, but it is hard to upgrade.
   2. use sub_cmd: as the request size will not get too big, an int32 is enough 
for telling REE how many bytes is the request.
   
   ## What's More
   I am trying to find a better solution, currently, I am trying to use generic 
to make the invoke method flexible(result type could be Vec or usize, just as 
what developers define, like serde_json), I will update you if I'm done will it.


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