使用CMake来引起SGX_ERROR_UNEXPECTED?

时间:2019-02-17 16:26:45

标签: makefile cmake sgx

我最近正在尝试学习intel sgx sdk。

今天,我在代码中发现问题,无法在intel DOC或WEB中找到任何解释。

在某些情况下,我致电sgx_create_enclave时,代码将返回SGX_ERROR_UNEXPECTED。我认为CMakeLists.txt中有问题(我使用cmake来编译项目中不受信任的部分),因为当我在sdk示例中使用模板Makefile进行编译时,代码可以正确运行。更多细节一击:

在以下情况下代码将成功运行:

  * compile with Makefile

  * compile with cmake and comment out app.cpp:21

在这种情况下,代码将返回错误:

  * compile with cmake and do not comment out app.cpp:21

代码在这里可用(我已经删除了所有不必要的代码): https://github.com/chilogen/workspace/tree/master/error/SimpleEnclave

app.cpp

#include "enclave_u.h"
#include <sgx_urts.h>
#include <sgx_uae_service.h>
#include <sgx_ukey_exchange.h>
#include <iostream>
using  namespace std;

class testClass {
public:
      sgx_launch_token_t _token = {0};
      sgx_enclave_id_t _eid;
      sgx_ra_context_t _ctx;
      void init_enclave();
      bool request(uint8_t *src, uint32_t srcLen, uint8_t *cmac);
      //will set _ctx here
      //void do_attestation();
}x;

bool testClass::request(uint8_t *src, uint32_t srcLen, uint8_t *cmac) {
    sgx_status_t retval,status;
    status = ecall_calcmac(_eid, &retval,&_ctx, SGX_RA_KEY_SK, src, srcLen, cmac);
    return true;
}

void testClass::init_enclave(){
    sgx_enclave_id_t global_eid;
    sgx_launch_token_t token={0};
    sgx_status_t ret;
    int updated=0;
    ret=sgx_create_enclave("enclave.signed.so",SGX_DEBUG_FLAG, \
                        &token,&updated,&global_eid,NULL);

    if(ret!=SGX_SUCCESS){
        std::cout<<"error init enclavedsfdsf\n";
        printf("%08x\n",ret);
        exit(1);
    }
}

int main(){
    x.init_enclave();
    return 0;
}

CMakeLists.txt

include_directories (/opt/intel/sgxsdk/include)
link_directories (/opt/intel/sgxsdk/lib64)
add_library (enclave_untrusted enclave_u.c)
add_executable (app app.cpp)
target_link_libraries (app enclave_untrusted sgx_ukey_exchange sgx_urts sgx_uae_service pthread)

Makefile (我认为这很重要,如果您对intel sgx不太了解,那么您仍然可以检查CMakeLists.txt和Makefile之间的区别)

## SGX SDK设置
SGX_SDK ?= /opt/intel/sgxsdk

SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r

######## App Settings ########

App_Include_Paths := -I$(SGX_SDK)/include 

App_Link_Flags := -L /opt/intel/sgxsdk/lib64 -lsgx_urts -lsgx_ukey_exchange -lsgx_uae_service  -pthread 


.PHONY: all
all: app

######## App Objects ########

enclave_u.c: $(SGX_EDGER8R) enclave.edl
    @$(SGX_EDGER8R) --untrusted enclave.edl --search-path $(SGX_SDK)/include
    @echo "GEN  =>  $@"

enclave_u.o: enclave_u.c
    @$(CC) $(App_Include_Paths) -c $< -o $@
    @echo "CC   <=  $<"

app.o: app.cpp
    @$(CXX) $(App_Include_Paths) -c $< -o $@ 
    @echo "CXX  <=  $<"

app: app.o enclave_u.o
    @$(CXX) $^ -o $@ $(App_Link_Flags)
    @echo "LINK =>  $@"

.PHONY: clean

clean:
    @rm -f *.o app

更新:compile.sh

gcc -c -I /opt/intel/sgxsdk/include/ -o enclave_u.o enclave_u.c

g++ -c  app.cpp -o app.o -I /opt/intel/sgxsdk/include/

g++ -o app app.o enclave_u.o -L /opt/intel/sgxsdk/lib64 -lsgx_urts -lsgx_ukey_exchange -lsgx_uae_service  -pthread

那么,我的代码(或CMakeLists.txt)有什么问题,我该怎么办?

如果您对我有所了解,我将非常感激。

2 个答案:

答案 0 :(得分:0)

尝试使用基于目标的API而不是基于目录的

add_library (enclave_untrusted enclave_u.c)
add_executable (app app.cpp)

target_include_directories (enclave_untrusted PUBLIC /opt/intel/sgxsdk/include)

target_link_libraries (enclave_untrusted PUBLIC
    "/opt/intel/sgxsdk/lib64/libsgx_urts.a"
    "/opt/intel/sgxsdk/lib64/libsgx_ukey_exchange.a"
    "/opt/intel/sgxsdk/lib64/libsgx_uae_service.a"
)
target_link_libraries (app PRIVATE enclave_untrusted pthread)

但是我建议使用适当的CMake库,例如SGX-CMake

答案 1 :(得分:0)

问题似乎仍然很奇怪。我可以在这里使用SGX-CMAKE修复示例,但今天上午似乎不适用于我的项目。但是,经过大量尝试(我不知道这是关键)之后,它现在适用于我的项目。我会一直在寻找所有这些背后的钥匙,并在找到的时候在这里更新。

现在,如果有人需要,我将为上面的示例上传CMakeLists.txt。

list(APPEND CMAKE_MODULE_PATH ${PATH_TO_FindSGX.cmake_FILE})
find_package(SGX REQUIRED)
set(CMAKE_C_FLAGS "-fpie -fPIC -fstack-protector -g -O2")
set(CMAKE_CXX_FLAGS "-fpie -fPIC -fstack-protector -g -std=c++11 -O2 -DDEBUG -UNDEBUG -UEDEBUG")


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(EDL_SEARCH_PATHS .)

set(E_SRCS enclave.c)
set(LDS Enclave_debug.lds)


add_enclave_library(enclave SRCS ${E_SRCS} EDL enclave.edl EDL_SEARCH_PATHS            
                    ${EDL_SEARCH_PATHS} LDSCRIPT ${LDS})
enclave_sign(enclave KEY Enclave_private.pem CONFIG Enclave.config.xml)

set(SRCS app.cpp)
add_untrusted_executable(app SRCS ${SRCS} EDL enclave.edl EDL_SEARCH_PATHS   
                         ${EDL_SEARCH_PATHS})