<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Returning pointer from ECALL in Intel® Software Guard Extensions (Intel® SGX)</title>
    <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119440#M1501</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;In the Intel SGX user guide it mentions that pointers to structures may be returned from ECALLS. On page 43, "pointers returned by an ECALL or OCALL function are not checked by the edge-routines and must be verified by the enclave and application code."&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em;"&gt;I am not able to get pointers to return from an ECALL, however, and instead get functions that always return sgx_status_t.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Can you please explain how I can return a pointer from an ECALL? Is there an edger8r flag that I need to specify so that ECALLS are not assumed to return sgx_status_t?&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Example:&lt;/P&gt;

&lt;P&gt;enclave.edl&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;trusted{ 
   public int* returns_int_ptr(void);
}&lt;/PRE&gt;

&lt;P&gt;enclave.c&lt;/P&gt;

&lt;PRE class="brush:cpp;" style="font-size: 13.008px;"&gt;int* returns_int_ptr(void)
{
    /* do some stuff */
&amp;nbsp;   return int_ptr;
}&lt;/PRE&gt;

&lt;P style="font-size: 13.008px;"&gt;enclave_u.h&lt;/P&gt;

&lt;PRE class="brush:cpp;" style="font-size: 13.008px;"&gt;/* I want int* as return type */
sgx_status_t returns_int_ptr(sgx_enclave_id_t eid, int* retval);
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Jun 2017 20:06:08 GMT</pubDate>
    <dc:creator>nickolas_l_</dc:creator>
    <dc:date>2017-06-01T20:06:08Z</dc:date>
    <item>
      <title>Returning pointer from ECALL</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119440#M1501</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;In the Intel SGX user guide it mentions that pointers to structures may be returned from ECALLS. On page 43, "pointers returned by an ECALL or OCALL function are not checked by the edge-routines and must be verified by the enclave and application code."&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em;"&gt;I am not able to get pointers to return from an ECALL, however, and instead get functions that always return sgx_status_t.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Can you please explain how I can return a pointer from an ECALL? Is there an edger8r flag that I need to specify so that ECALLS are not assumed to return sgx_status_t?&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Example:&lt;/P&gt;

&lt;P&gt;enclave.edl&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;trusted{ 
   public int* returns_int_ptr(void);
}&lt;/PRE&gt;

&lt;P&gt;enclave.c&lt;/P&gt;

&lt;PRE class="brush:cpp;" style="font-size: 13.008px;"&gt;int* returns_int_ptr(void)
{
    /* do some stuff */
&amp;nbsp;   return int_ptr;
}&lt;/PRE&gt;

&lt;P style="font-size: 13.008px;"&gt;enclave_u.h&lt;/P&gt;

&lt;PRE class="brush:cpp;" style="font-size: 13.008px;"&gt;/* I want int* as return type */
sgx_status_t returns_int_ptr(sgx_enclave_id_t eid, int* retval);
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:06:08 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119440#M1501</guid>
      <dc:creator>nickolas_l_</dc:creator>
      <dc:date>2017-06-01T20:06:08Z</dc:date>
    </item>
    <item>
      <title>Hi, Nickolas.</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119441#M1502</link>
      <description>&lt;P&gt;Hi, Nickolas.&lt;/P&gt;

&lt;P&gt;The SGX Edger8r tool automatically converts your function to the one that you pasted here.&lt;BR /&gt;
	It means that you need to get the "returned" int pointer from the out pointer parameter.&lt;/P&gt;

&lt;P&gt;To execute that eCall, your code should look like this:&lt;/P&gt;

&lt;P&gt;app.cpp&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;sgx_enclave_id_t eid;

/* create enclave.. */

sgx_status_t ret = SGX_SUCCESS;
int myInt;

ret = returns_int_ptr(eid, &amp;amp;myInt);

/* use myInt */&lt;/PRE&gt;

&lt;P&gt;Now, if you wanted to return several integers instead of only one, your code should look like this:&lt;/P&gt;

&lt;P&gt;enclave.edl&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;enclave{
    trusted{
        public void returns_int_ptr([out, size=len] int *p_return_ptr, size_t len);
    };
};&lt;/PRE&gt;

&lt;P&gt;enclave.cpp&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;void returns_int_ptr(int *p_return_ptr, size_t len)
{
    int *p_ints = (int *) malloc(len*sizeof(int));

    /* do some stuff */

    memcpy(p_return_ptr, p_ints, len);
    free(p_ints);
}&lt;/PRE&gt;

&lt;P&gt;app.cpp&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#define BUFFER_SIZE 100
sgx_enclave_id_t eid;

/* create enclave */

int *p_ints = (int *) malloc(BUFFER_SIZE * sizeof(int));

sgx_status_t ret = SGX_SUCCESS;
ret = returns_int_ptr(eid, p_ints, BUFFER_SIZE);

/* do some stuff with p_ints */&lt;/PRE&gt;

&lt;P&gt;Hope this is helpful.&lt;/P&gt;

&lt;P&gt;Best regards,&lt;BR /&gt;
	Rodolfo&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 22:03:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119441#M1502</guid>
      <dc:creator>Rodolfo_S_</dc:creator>
      <dc:date>2017-06-01T22:03:46Z</dc:date>
    </item>
    <item>
      <title>When the trusted function</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119442#M1503</link>
      <description>&lt;P&gt;&lt;SPAN style="font-size: 1em;"&gt;When the trusted function returns "&lt;/SPAN&gt;&lt;CODE class="color1 bold" style="font-size: 13.008px; background-color: rgb(248, 248, 248); font-family: Consolas, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, Courier, monospace !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; min-height: auto !important; color: rgb(128, 128, 128) !important;"&gt;int&amp;nbsp;&lt;/CODE&gt;&lt;CODE class="plain" style="font-size: 13.008px; background-color: rgb(248, 248, 248); color: rgb(0, 0, 0); font-family: Consolas, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, Courier, monospace !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; min-height: auto !important;"&gt;*&lt;/CODE&gt;&lt;SPAN style="font-size: 13.008px;"&gt;", the result is the following untrusted bridge:&lt;/SPAN&gt;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;sgx_status_t returns_int_ptr(sgx_enclave_id_t eid, int** retval);&lt;/PRE&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.008px;"&gt;and the untrusted app could then obtain the pointer as follows:&lt;/SPAN&gt;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;sgx_status_t ret = SGX_SUCCESS;
int * myInt = NULL;
ret = returns_int_ptr(eid, &amp;amp;myInt);
&lt;/PRE&gt;

&lt;P&gt;The documentation is saying that the such pointer is not marshalled, i.e., the untrusted application gets a pointer (address) but it doesn't get the content of the buffer. If you want to get the buffer you have to pass the pointer as a parameter as Rodolfo explained above.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;static sgx_status_t SGX_CDECL sgx_returns_int_ptr(void* pms)
{
	ms_returns_int_ptr_t* ms = SGX_CAST(ms_returns_int_ptr_t*, pms);
	sgx_status_t status = SGX_SUCCESS;
	CHECK_REF_POINTER(pms, sizeof(ms_returns_int_ptr_t));
	ms-&amp;gt;ms_retval = returns_int_ptr(); // We just get the pointer. No memcpy() below.
	return status;
}&lt;/PRE&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.008px;"&gt;This is important when an OCALL returns a pointer, since the enclave has to check it as if it had the "user_check" attribute.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 13:33:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119442#M1503</guid>
      <dc:creator>Juan_d_Intel</dc:creator>
      <dc:date>2017-06-09T13:33:00Z</dc:date>
    </item>
    <item>
      <title>Thank you for both of your</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119443#M1504</link>
      <description>&lt;P&gt;Thank you for both of your responses. They clear this up perfectly, and shows nicely where I was misunderstanding the SGX framework.&lt;/P&gt;

&lt;P&gt;Nick&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 17:58:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Returning-pointer-from-ECALL/m-p/1119443#M1504</guid>
      <dc:creator>nickolas_l_</dc:creator>
      <dc:date>2017-06-09T17:58:21Z</dc:date>
    </item>
  </channel>
</rss>

