in nitro_enclaves/ne_ioctl_sample.c [766:882]
int main(int argc, char *argv[])
{
int enclave_fd = -1;
unsigned int i = 0;
int ne_dev_fd = -1;
struct ne_user_mem_region ne_user_mem_regions[NE_DEFAULT_NR_MEM_REGIONS] = {};
unsigned int ne_vcpus[NE_DEFAULT_NR_VCPUS] = {};
int rc = -EINVAL;
pthread_t thread_id = 0;
unsigned long slot_uid = 0;
if (argc != 2) {
printf("Usage: %s <path_to_enclave_image>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (strlen(argv[1]) >= PATH_MAX) {
printf("The size of the path to enclave image is higher than max path\n");
exit(EXIT_FAILURE);
}
ne_dev_fd = open(NE_DEV_NAME, O_RDWR | O_CLOEXEC);
if (ne_dev_fd < 0) {
printf("Error in open NE device [%m]\n");
exit(EXIT_FAILURE);
}
printf("Creating enclave slot ...\n");
rc = ne_create_vm(ne_dev_fd, &slot_uid, &enclave_fd);
close(ne_dev_fd);
if (rc < 0)
exit(EXIT_FAILURE);
printf("Enclave fd %d\n", enclave_fd);
rc = pthread_create(&thread_id, NULL, ne_poll_enclave_fd, (void *)&enclave_fd);
if (rc < 0) {
printf("Error in thread create [%m]\n");
close(enclave_fd);
exit(EXIT_FAILURE);
}
for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++) {
ne_user_mem_regions[i].memory_size = NE_MIN_MEM_REGION_SIZE;
rc = ne_alloc_user_mem_region(&ne_user_mem_regions[i]);
if (rc < 0) {
printf("Error in alloc userspace memory region, iter %d\n", i);
goto release_enclave_fd;
}
}
rc = ne_load_enclave_image(enclave_fd, ne_user_mem_regions, argv[1]);
if (rc < 0)
goto release_enclave_fd;
for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++) {
rc = ne_set_user_mem_region(enclave_fd, ne_user_mem_regions[i]);
if (rc < 0) {
printf("Error in set memory region, iter %d\n", i);
goto release_enclave_fd;
}
}
printf("Enclave memory regions were added\n");
for (i = 0; i < NE_DEFAULT_NR_VCPUS; i++) {
/*
* The vCPU is chosen from the enclave vCPU pool, if the value
* of the vcpu_id is 0.
*/
ne_vcpus[i] = 0;
rc = ne_add_vcpu(enclave_fd, &ne_vcpus[i]);
if (rc < 0) {
printf("Error in add vcpu, iter %d\n", i);
goto release_enclave_fd;
}
printf("Added vCPU %d to the enclave\n", ne_vcpus[i]);
}
printf("Enclave vCPUs were added\n");
rc = ne_start_enclave_check_booted(enclave_fd);
if (rc < 0) {
printf("Error in the enclave start / image loading heartbeat logic [rc=%d]\n", rc);
goto release_enclave_fd;
}
printf("Entering sleep for %d seconds ...\n", NE_SLEEP_TIME);
sleep(NE_SLEEP_TIME);
close(enclave_fd);
ne_free_mem_regions(ne_user_mem_regions);
exit(EXIT_SUCCESS);
release_enclave_fd:
close(enclave_fd);
ne_free_mem_regions(ne_user_mem_regions);
exit(EXIT_FAILURE);
}