Search code examples
clinuxbashintelsgx

SGX Enclave: Where the actual function that does the procession goes and how it gets compiled


After reading lots of documentation i did the first simple enclave function:

enclave {
     //Include files 

     //Import other edl files

     //Data structure declarations to be used as parameters of the
     //function prototypes in edl

     trusted {
          public function myFirstMethod([in]  int *a, [in]  int *b,[out] int *sum);
     };

     untrusted {
     };
};

Then over bash I run the edger8r:

sgx_edger8r enclave.edl

Then it generated the following files as you can see over the schema:

Generated files

So I assume somewhere over the enclave_t.c the only reference I found is in this function:

static sgx_status_t SGX_CDECL sgx_myFirstMethod(void* pms)
{
    CHECK_REF_POINTER(pms, sizeof(ms_myFirstMethod_t));
    ms_myFirstMethod_t* ms = SGX_CAST(ms_myFirstMethod_t*, pms);
    sgx_status_t status = SGX_SUCCESS;
    int* _tmp_a = ms->ms_a;
    size_t _len_a = sizeof(*_tmp_a);
    int* _in_a = NULL;
    int* _tmp_b = ms->ms_b;
    size_t _len_b = sizeof(*_tmp_b);
    int* _in_b = NULL;

    CHECK_UNIQUE_POINTER(_tmp_a, _len_a);
    CHECK_UNIQUE_POINTER(_tmp_b, _len_b);

    if (_tmp_a != NULL) {
        _in_a = (int*)malloc(_len_a);
        if (_in_a == NULL) {
            status = SGX_ERROR_OUT_OF_MEMORY;
            goto err;
        }

        memcpy(_in_a, _tmp_a, _len_a);
    }
    if (_tmp_b != NULL) {
        _in_b = (int*)malloc(_len_b);
        if (_in_b == NULL) {
            status = SGX_ERROR_OUT_OF_MEMORY;
            goto err;
        }

        memcpy(_in_b, _tmp_b, _len_b);
    }
    ms->ms_retval = myFirstMethod(_in_a, _in_b);
err:
    if (_in_a) free(_in_a);
    if (_in_b) free(_in_b);

    return status;
}

Especially in

    ms->ms_retval = myFirstMethod(_in_a, _in_b);

But where to put the myFirstMethod? Also how I will compile my enclave as a part of an application as a static library.

As fas as I searched is the tutorial in theese links:

All mention Visual Studio that does not natively run over GNU/Linux so are a bit hard for me to follow.

Edit 1:

Further looking I have seen on https://github.com/01org/linux-sgx that I can compile over simulation mode as the link mentions:

make SGX_MODE=SIM

And I successfully I have installed the driver and the sdk. I want to compile over SIMULATION mode and not real one.


Solution

  • The autogenerated outputs of edger8r are only to provide interface between the enclave and the untrusted outside world. They are not supposed to contain your implementations.

    You should define myFirstMethod in another source file, say enclave.c or enclave.cpp and link it with the rest of your project. The signature of the function being exactly what you declared in your edl, except for the pointer qualifiers, which are for edger8r to consume.

    It will go like this:

    enclave.cpp:

    void myFirstMethod(int *a, int *b, int *sum)
    {
      *sum = *a + *b;
    }