- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Intel SGX Community,
I am writing to post the question I meet, firstly I define an ecall interface at edl:
that I define the p_return_ptr as the out variable, which can be change in the ecall function, and work as the return value for returns_int_ptr()
The ecall function content is:
that each element in the p_return_ptr would be: p_return_ptr[i]=i. such as:
p_return_ptr[0]=0
p_return_ptr[1]=1
p_return_ptr[2]=2
p_return_ptr[3]=3
p_return_ptr[4]=4 ........
The App.cpp is:
And I have verify that the out variable p_return_ptr has been changed in ecall, but it is not changed in App.cpp(REE), that the return operation is not successful in fact, I dont know where is the problem. I would appreciate a lot for your help. Thank you!
From the output the p_return_ptr at ecall is:
While it does not change as an output variable, that at REE/App.cpp the p_return_ptr is:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks so much for your patience while we looked into this issue.
As you rightfully said, you should use [out, count=len] instead of [out, size=len], but that does not solve the problem.
Short answer:
Remove the line p_return_ptr = (int*)malloc(len*sizeof(int)); in your Enclave.cpp and you program works as expected.
More details:
The function sgx_returns_int_ptr generated for you in the file Enclave/Enclave_t.c already does the memory allocation (inside enclave memory) in the line _in_p_return_ptr = (int*)malloc(_len_p_return_ptr);.
It then passes the pointer _in_p_return_ptr to your function returns_int_ptr with returns_int_ptr(_in_p_return_ptr, _tmp_len);.
As a result, the pointer p_return_ptr in your function does point to the space allocated before.
Note, however, that _in_p_return_ptr and p_return_ptr are two independent pointers (i.e., both pointers are stored at different memory locations) that point to the same memory location allocated before.
At this point, you currently perform another allocation and change the pointer p_return_ptr to this new location.
As _in_p_return_ptr and p_return_ptr are independent pointers, the former does still point to the memory location allocated by sgx_returns_int_ptr, and p_return_ptr does point to the newly allocated memory.
Then, you do changes to the newly allocated memory.
After your function returns, sgx_returns_int_ptr does copy data from the memory location allocated before (and pointed to by sgx_returns_int_ptr) to space allocated outside of the enclave with memcpy_s(_tmp_p_return_ptr, _len_p_return_ptr, _in_p_return_ptr, _len_p_return_ptr).
As you never did anything with this memory location, your result is not as expected.
Side note: you currently have a memory leak in your code, because p_return_ptr is never freed.
Please let us know if you have any further questions.
Sincerely,
Sahira
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello MoonHasSevenColor,
Thanks for reaching out to us.
Could you please share the required scripts and files with us to replicate the issue from our end?
Regards,
Wan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sure! I attach the code name Code_JinXin.zip
unzip the folder, enter the flolder
make clean
make
./app
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I test further and find in edl file it should be [out, count=len] instead of [out, size=len]
But it still does not resolve the question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello MoonHasSevenColor,
Thanks for the information.
We'll investigate the issue and update you at the earliest.
Regards,
Wan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Wan,
Do you have any new discoveries for the code? Thank you for your help!
Best Reagrds,
Jin Xin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks so much for your patience while we looked into this issue.
As you rightfully said, you should use [out, count=len] instead of [out, size=len], but that does not solve the problem.
Short answer:
Remove the line p_return_ptr = (int*)malloc(len*sizeof(int)); in your Enclave.cpp and you program works as expected.
More details:
The function sgx_returns_int_ptr generated for you in the file Enclave/Enclave_t.c already does the memory allocation (inside enclave memory) in the line _in_p_return_ptr = (int*)malloc(_len_p_return_ptr);.
It then passes the pointer _in_p_return_ptr to your function returns_int_ptr with returns_int_ptr(_in_p_return_ptr, _tmp_len);.
As a result, the pointer p_return_ptr in your function does point to the space allocated before.
Note, however, that _in_p_return_ptr and p_return_ptr are two independent pointers (i.e., both pointers are stored at different memory locations) that point to the same memory location allocated before.
At this point, you currently perform another allocation and change the pointer p_return_ptr to this new location.
As _in_p_return_ptr and p_return_ptr are independent pointers, the former does still point to the memory location allocated by sgx_returns_int_ptr, and p_return_ptr does point to the newly allocated memory.
Then, you do changes to the newly allocated memory.
After your function returns, sgx_returns_int_ptr does copy data from the memory location allocated before (and pointed to by sgx_returns_int_ptr) to space allocated outside of the enclave with memcpy_s(_tmp_p_return_ptr, _len_p_return_ptr, _in_p_return_ptr, _len_p_return_ptr).
As you never did anything with this memory location, your result is not as expected.
Side note: you currently have a memory leak in your code, because p_return_ptr is never freed.
Please let us know if you have any further questions.
Sincerely,
Sahira
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Sahira,
Thank youuuuuuu for your so nice help and great answer! Thank you for your reminding to free the memory! It helps me a lot! Have a nice day!
Best Regards,
Jin Xin
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page