corda/sgx-jvm/noop-enclave/src/test.cpp
2017-08-01 16:49:44 +02:00

146 lines
3.6 KiB
C++

#include "empty_u.h"
#include <sgx_urts.h>
#include <sgx.h>
#include <cstdlib>
#include <cstdio>
typedef struct {
sgx_status_t err;
const char *message;
const char *suggestion;
} sgx_errlist_t;
/* Error code returned by sgx_create_enclave */
static sgx_errlist_t sgx_errlist[] = {
{
SGX_ERROR_UNEXPECTED,
"Unexpected error occurred.",
NULL
},
{
SGX_ERROR_INVALID_PARAMETER,
"Invalid parameter.",
NULL
},
{
SGX_ERROR_OUT_OF_MEMORY,
"Out of memory.",
NULL
},
{
SGX_ERROR_ENCLAVE_LOST,
"Power transition occurred.",
"Please refer to the sample \"PowerTransition\" for details."
},
{
SGX_ERROR_INVALID_ENCLAVE,
"Invalid enclave image.",
NULL
},
{
SGX_ERROR_INVALID_ENCLAVE_ID,
"Invalid enclave identification.",
NULL
},
{
SGX_ERROR_INVALID_SIGNATURE,
"Invalid enclave signature.",
NULL
},
{
SGX_ERROR_OUT_OF_EPC,
"Out of EPC memory.",
NULL
},
{
SGX_ERROR_NO_DEVICE,
"Invalid SGX device.",
"Please make sure SGX module is enabled in the BIOS, and install SGX driver afterwards."
},
{
SGX_ERROR_MEMORY_MAP_CONFLICT,
"Memory map conflicted.",
NULL
},
{
SGX_ERROR_INVALID_METADATA,
"Invalid enclave metadata.",
NULL
},
{
SGX_ERROR_DEVICE_BUSY,
"SGX device was busy.",
NULL
},
{
SGX_ERROR_INVALID_VERSION,
"Enclave version was invalid.",
NULL
},
{
SGX_ERROR_INVALID_ATTRIBUTE,
"Enclave was not authorized.",
NULL
},
{
SGX_ERROR_ENCLAVE_FILE_ACCESS,
"Can't open enclave file.",
NULL
},
};
/* Check error conditions for loading enclave */
void print_error_message(sgx_status_t ret)
{
size_t idx = 0;
size_t ttl = sizeof sgx_errlist/sizeof sgx_errlist[0];
for (idx = 0; idx < ttl; idx++) {
if(ret == sgx_errlist[idx].err) {
if(NULL != sgx_errlist[idx].suggestion)
printf("Info: %s\n", sgx_errlist[idx].suggestion);
printf("Error: %s\n", sgx_errlist[idx].message);
break;
}
}
if (idx == ttl)
printf("Error: Unexpected error occurred.\n");
}
inline bool check_sgx_return_value(sgx_status_t ret)
{
if (ret == SGX_SUCCESS)
{
return true;
}
else
{
print_error_message(ret);
return false;
}
}
int main(int argc, char **argv) {
if (argc != 2) {
puts("Usage: <binary> <signed.enclave.so>");
return 1;
}
const char *enclave_path = argv[1];
sgx_launch_token_t token = {0};
sgx_enclave_id_t enclave_id = {0};
int updated = 0;
if (false == check_sgx_return_value(sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, &enclave_id, NULL))) {
return 1;
}
if (false == check_sgx_return_value(noop(enclave_id))) {
return 1;
}
puts("Enclave ran successfully!");
return 0;
}