openenclave / openenclave

SDK for developing enclaves
https://openenclave.io/sdk/
MIT License
1.05k stars 354 forks source link

Link openenclave project with sgxsdk project #4943

Open sjrrr13 opened 5 months ago

sjrrr13 commented 5 months ago

I'm try to link two projects, from which A is built on top of Open Enclave while B is built on top of SGX SDK. Specifically, B is executable and A is a library, and I want to call function enclave_helloworld in A from B. Unfortunately, I've encountered the following issue:

error: openenclave/edl/sgx/attestation.edl:104:46: unexpected token: expiration_check_date

make: *** [Makefile:220: App/Enclave_u.c] Error 255

Here is part of my Enclave.edl:

    from "openenclave/edl/syscall.edl" import *;
    from "openenclave/edl/sgx/platform.edl" import *;
    from "openenclave/edl/ertlibc.edl" import *;

    trusted {
        public void enclave_helloworld();
    };

And I'm working on a SGX server with Ubuntu 20.04. What can I do to link those projects successfully?

anakrish commented 5 months ago

Are you using oeedger8r on your Enclave.edl or are you using sgxedger8r

sjrrr13 commented 5 months ago

Hello, I'm using sgxedger8r on my Enclave.edl.

sjrrr13 commented 5 months ago

Use oeedger8r on Enclave.edl, the ECALL function needs an argument oe_enclave_t* (aka _oe_enclave*). But use sgxedger8r, the ECALL function needs an argument sgx_enclave_id_t (aka long unsigned int). If I want to call an Open Enclave ECALL in a SGXSDK project, what can I do to merge the difference between function arguments?

anakrish commented 5 months ago

You need to use oeedger8r for EDL files intended for use with openenclave. Since the enclave is written using OpenEnclave, on the host side, the EDL must be processed using oeedger8r.

sjrrr13 commented 5 months ago

Thanks first! In fact, I have two EDL files, Enclave.edl (use sgx sdk) and helloworld.edl (use open enclave). I defined a ECALL enclave_helloworld() in helloworld.edl, and I use oeedger8r to process helloworld.edl. After that, I got a function

oe_result_t enclave_helloworld(oe_enclave_t* enclave);

Now I'm trying to call this function in the host side in my sgx sdk project. Specifically, in the host side in the sgx sdk project, named App.cpp, I import helloworld_u.h generated by oeedger8r and call enclave_helloworld.

// App.cpp in sgx sdk project
#include "helloworld_u.h"    // generated by oeedger8r

...
enclave_helloworld()    // what arg can I pass to this function?
...

However, sgx sdk does not support struct oe_enclave_t*. So I wonder is there any thing I can do to solve this problem?

anakrish commented 5 months ago

On the host-side you need to link with liboehost.a. The hellworld_u.h will have the declaration for a create_helloworld_enclave function. Calling that will create and enclave and get you a oe_enclave_t. See https://github.com/openenclave/openenclave/blob/424e62c5e4a11395c4e5ac283d52637fa3ccf5a7/samples/helloworld/host/host.c#L54