Format c/c++ files in src, tools and test

This commit is contained in:
Hui, Chunyang 2020-05-27 07:02:34 +00:00
parent 03ba13aec7
commit 5b695c9539
61 changed files with 567 additions and 508 deletions

@ -3,19 +3,33 @@
const char *errno2str(int errno_) { const char *errno2str(int errno_) {
switch (errno_) { switch (errno_) {
case EPERM: return "EPERM"; case EPERM:
case ENOENT: return "ENOENT"; return "EPERM";
case ESRCH: return "ESRCH"; case ENOENT:
case ENOEXEC: return "ENOEXEC"; return "ENOENT";
case EBADF: return "EBADF"; case ESRCH:
case ECHILD: return "ECHILD"; return "ESRCH";
case EAGAIN: return "EAGAIN"; case ENOEXEC:
case ENOMEM: return "ENOMEM"; return "ENOEXEC";
case EACCES: return "EACCES"; case EBADF:
case EFAULT: return "EFAULT"; return "EBADF";
case EBUSY: return "EBUSY"; case ECHILD:
case EINVAL: return "EINVAL"; return "ECHILD";
case ENOSYS: return "ENOSYS"; case EAGAIN:
default: return "unknown"; return "EAGAIN";
case ENOMEM:
return "ENOMEM";
case EACCES:
return "EACCES";
case EFAULT:
return "EFAULT";
case EBUSY:
return "EBUSY";
case EINVAL:
return "EINVAL";
case ENOSYS:
return "ENOSYS";
default:
return "unknown";
} }
} }

@ -3,8 +3,7 @@
sgx_status_t occlum_ocall_sgx_init_quote( sgx_status_t occlum_ocall_sgx_init_quote(
sgx_target_info_t *target_info, sgx_target_info_t *target_info,
sgx_epid_group_id_t* epid_group_id) sgx_epid_group_id_t *epid_group_id) {
{
// Intel's manual: // Intel's manual:
// It's suggested that the caller should wait (typically several seconds // It's suggested that the caller should wait (typically several seconds
// to ten of seconds) and retry this API if SGX_ERROR_BUSY is returned. // to ten of seconds) and retry this API if SGX_ERROR_BUSY is returned.
@ -20,8 +19,7 @@ sgx_status_t occlum_ocall_sgx_get_quote(
sgx_quote_nonce_t *nonce, sgx_quote_nonce_t *nonce,
sgx_report_t *qe_report, sgx_report_t *qe_report,
sgx_quote_t *quote_buf, sgx_quote_t *quote_buf,
uint32_t quote_buf_len) uint32_t quote_buf_len) {
{
sgx_status_t ret = SGX_SUCCESS; sgx_status_t ret = SGX_SUCCESS;
uint32_t real_quote_len; uint32_t real_quote_len;

@ -16,7 +16,7 @@ typedef enum {
#define COLOR_GREEN "\x1B[32m" #define COLOR_GREEN "\x1B[32m"
static level_t new_level(unsigned int level) { static level_t new_level(unsigned int level) {
if (level >= 5) level = 5; if (level >= 5) { level = 5; }
return (level_t) level; return (level_t) level;
} }

@ -11,8 +11,7 @@ ssize_t occlum_ocall_sendmsg(int sockfd,
size_t msg_iovlen, size_t msg_iovlen,
const void *msg_control, const void *msg_control,
size_t msg_controllen, size_t msg_controllen,
int flags) int flags) {
{
struct msghdr msg = { struct msghdr msg = {
(void *) msg_name, (void *) msg_name,
msg_namelen, msg_namelen,
@ -35,8 +34,7 @@ ssize_t occlum_ocall_recvmsg(int sockfd,
size_t msg_controllen, size_t msg_controllen,
size_t *msg_controllen_recv, size_t *msg_controllen_recv,
int *msg_flags_recv, int *msg_flags_recv,
int flags) int flags) {
{
struct msghdr msg = { struct msghdr msg = {
msg_name, msg_name,
msg_namelen, msg_namelen,
@ -47,7 +45,7 @@ ssize_t occlum_ocall_recvmsg(int sockfd,
0, 0,
}; };
ssize_t ret = recvmsg(sockfd, &msg, flags); ssize_t ret = recvmsg(sockfd, &msg, flags);
if (ret < 0) return ret; if (ret < 0) { return ret; }
*msg_namelen_recv = msg.msg_namelen; *msg_namelen_recv = msg.msg_namelen;
*msg_controllen_recv = msg.msg_controllen; *msg_controllen_recv = msg.msg_controllen;

@ -13,7 +13,8 @@ int occlum_ocall_sched_getaffinity(size_t cpusize, unsigned char* buf) {
return ret; return ret;
} }
int occlum_ocall_sched_setaffinity(int host_tid, size_t cpusize, const unsigned char* buf) { int occlum_ocall_sched_setaffinity(int host_tid, size_t cpusize,
const unsigned char *buf) {
return syscall(__NR_sched_setaffinity, host_tid, cpusize, buf); return syscall(__NR_sched_setaffinity, host_tid, cpusize, buf);
} }

@ -13,7 +13,8 @@ void* exec_libos_thread(void* _thread_data) {
int host_tid = gettid(); int host_tid = gettid();
int libos_tid = thread_data->libos_tid; int libos_tid = thread_data->libos_tid;
int libos_exit_status = -1; int libos_exit_status = -1;
sgx_status_t status = occlum_ecall_exec_thread(eid, &libos_exit_status, libos_tid, host_tid); sgx_status_t status = occlum_ecall_exec_thread(eid, &libos_exit_status, libos_tid,
host_tid);
if (status != SGX_SUCCESS) { if (status != SGX_SUCCESS) {
const char *sgx_err = pal_get_sgx_error_msg(status); const char *sgx_err = pal_get_sgx_error_msg(status);
PAL_ERROR("Failed to enter the enclave to execute a LibOS thread: %s", sgx_err); PAL_ERROR("Failed to enter the enclave to execute a LibOS thread: %s", sgx_err);

@ -35,7 +35,8 @@ int occlum_pal_init(const struct occlum_pal_attr* attr) {
eid = pal_get_enclave_id(); eid = pal_get_enclave_id();
int ecall_ret = 0; int ecall_ret = 0;
sgx_status_t ecall_status = occlum_ecall_init(eid, &ecall_ret, attr->log_level, attr->instance_dir); sgx_status_t ecall_status = occlum_ecall_init(eid, &ecall_ret, attr->log_level,
attr->instance_dir);
if (ecall_status != SGX_SUCCESS) { if (ecall_status != SGX_SUCCESS) {
const char *sgx_err = pal_get_sgx_error_msg(ecall_status); const char *sgx_err = pal_get_sgx_error_msg(ecall_status);
PAL_ERROR("Failed to do ECall: %s", sgx_err); PAL_ERROR("Failed to do ECall: %s", sgx_err);
@ -69,7 +70,8 @@ int occlum_pal_exec(const char* cmd_path,
} }
int ecall_ret = 0; // libos_tid int ecall_ret = 0; // libos_tid
sgx_status_t ecall_status = occlum_ecall_new_process(eid, &ecall_ret, cmd_path, cmd_args, cmd_env, io_fds); sgx_status_t ecall_status = occlum_ecall_new_process(eid, &ecall_ret, cmd_path, cmd_args,
cmd_env, io_fds);
if (ecall_status != SGX_SUCCESS) { if (ecall_status != SGX_SUCCESS) {
const char *sgx_err = pal_get_sgx_error_msg(ecall_status); const char *sgx_err = pal_get_sgx_error_msg(ecall_status);
PAL_ERROR("Failed to do ECall: %s", sgx_err); PAL_ERROR("Failed to do ECall: %s", sgx_err);

@ -98,27 +98,29 @@ int pal_init_enclave(const char* instance_dir) {
/* Debug Support: set 2nd parameter to 1 */ /* Debug Support: set 2nd parameter to 1 */
const char *enclave_path = get_enclave_absolute_path(instance_dir); const char *enclave_path = get_enclave_absolute_path(instance_dir);
int sgx_debug_flag = get_enclave_debug_flag(); int sgx_debug_flag = get_enclave_debug_flag();
ret = sgx_create_enclave(enclave_path, sgx_debug_flag, &token, &updated, &global_eid, NULL); ret = sgx_create_enclave(enclave_path, sgx_debug_flag, &token, &updated, &global_eid,
NULL);
if (ret != SGX_SUCCESS) { if (ret != SGX_SUCCESS) {
const char *sgx_err_msg = pal_get_sgx_error_msg(ret); const char *sgx_err_msg = pal_get_sgx_error_msg(ret);
PAL_ERROR("Failed to create enclave: %s", sgx_err_msg); PAL_ERROR("Failed to create enclave: %s", sgx_err_msg);
if (fp != NULL) fclose(fp); if (fp != NULL) { fclose(fp); }
return -1; return -1;
} }
/* Step 3: save the launch token if it is updated */ /* Step 3: save the launch token if it is updated */
if (updated == 0 || fp == NULL) { if (updated == 0 || fp == NULL) {
/* if the token is not updated, or file handler is invalid, do not perform saving */ /* if the token is not updated, or file handler is invalid, do not perform saving */
if (fp != NULL) fclose(fp); if (fp != NULL) { fclose(fp); }
return 0; return 0;
} }
/* reopen the file with write capablity */ /* reopen the file with write capablity */
fp = freopen(token_path, "wb", fp); fp = freopen(token_path, "wb", fp);
if (fp == NULL) return 0; if (fp == NULL) { return 0; }
size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp); size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
if (write_num != sizeof(sgx_launch_token_t)) if (write_num != sizeof(sgx_launch_token_t)) {
PAL_WARN("Warning: Failed to save launch token to \"%s\".\n", token_path); PAL_WARN("Warning: Failed to save launch token to \"%s\".\n", token_path);
}
fclose(fp); fclose(fp);
return 0; return 0;
} }

@ -10,8 +10,7 @@ static const char* get_instance_dir(void) {
const char *instance_dir_from_env = (const char *) getenv("OCCLUM_INSTANCE_DIR"); const char *instance_dir_from_env = (const char *) getenv("OCCLUM_INSTANCE_DIR");
if (instance_dir_from_env != NULL) { if (instance_dir_from_env != NULL) {
return instance_dir_from_env; return instance_dir_from_env;
} } else {
else {
return "./.occlum"; return "./.occlum";
} }
} }

@ -116,8 +116,9 @@ static int test_access_framework(test_access_func_t fn) {
if (create_file(file_path, mode) < 0) { if (create_file(file_path, mode) < 0) {
return -1; return -1;
} }
if (fn(file_path) < 0) if (fn(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -85,12 +85,15 @@ typedef int(*test_chmod_func_t)(const char *);
static int test_chmod_framework(test_chmod_func_t fn) { static int test_chmod_framework(test_chmod_func_t fn) {
const char *file_path = "/root/test_filesystem_chmod.txt"; const char *file_path = "/root/test_filesystem_chmod.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -107,12 +107,15 @@ typedef int(*test_chown_func_t)(const char *);
static int test_chown_framework(test_chown_func_t fn) { static int test_chown_framework(test_chown_func_t fn) {
const char *file_path = "/root/test_filesystem_chown.txt"; const char *file_path = "/root/test_filesystem_chown.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -16,13 +16,15 @@
int connect_with_server(const char *addr_string, const char *port_string) { int connect_with_server(const char *addr_string, const char *port_string) {
//"NULL" addr means connectionless, no need to connect to server //"NULL" addr means connectionless, no need to connect to server
if (strcmp(addr_string, "NULL") == 0) if (strcmp(addr_string, "NULL") == 0) {
return 0; return 0;
}
int ret = 0; int ret = 0;
int sockfd = socket(AF_INET, SOCK_STREAM, 0); int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0) {
THROW_ERROR("create socket error"); THROW_ERROR("create socket error");
}
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr)); memset(&servaddr, 0, sizeof(servaddr));
@ -44,8 +46,9 @@ int connect_with_server(const char *addr_string, const char *port_string) {
} }
int neogotiate_msg(int server_fd, char *buf, int buf_size) { int neogotiate_msg(int server_fd, char *buf, int buf_size) {
if (read(server_fd, buf, buf_size) < 0) if (read(server_fd, buf, buf_size) < 0) {
THROW_ERROR("read failed"); THROW_ERROR("read failed");
}
if (write(server_fd, RESPONSE, sizeof(RESPONSE)) < 0) { if (write(server_fd, RESPONSE, sizeof(RESPONSE)) < 0) {
THROW_ERROR("write failed"); THROW_ERROR("write failed");
@ -54,8 +57,9 @@ int neogotiate_msg(int server_fd, char *buf, int buf_size) {
} }
int client_send(int server_fd, char *buf) { int client_send(int server_fd, char *buf) {
if (send(server_fd, buf, strlen(buf), 0) < 0) if (send(server_fd, buf, strlen(buf), 0) < 0) {
THROW_ERROR("send msg error"); THROW_ERROR("send msg error");
}
return 0; return 0;
} }
@ -74,15 +78,17 @@ int client_sendmsg(int server_fd, char *buf) {
msg.msg_flags = 0; msg.msg_flags = 0;
ret = sendmsg(server_fd, &msg, 0); ret = sendmsg(server_fd, &msg, 0);
if (ret <= 0) if (ret <= 0) {
THROW_ERROR("sendmsg failed"); THROW_ERROR("sendmsg failed");
}
msg.msg_iov = NULL; msg.msg_iov = NULL;
msg.msg_iovlen = 0; msg.msg_iovlen = 0;
ret = sendmsg(server_fd, &msg, 0); ret = sendmsg(server_fd, &msg, 0);
if (ret != 0) if (ret != 0) {
THROW_ERROR("empty sendmsg failed"); THROW_ERROR("empty sendmsg failed");
}
return ret; return ret;
} }
@ -108,12 +114,14 @@ int client_connectionless_sendmsg(char *buf) {
msg.msg_flags = 0; msg.msg_flags = 0;
int server_fd = socket(AF_INET, SOCK_DGRAM, 0); int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (server_fd < 0) if (server_fd < 0) {
THROW_ERROR("create socket error"); THROW_ERROR("create socket error");
}
ret = sendmsg(server_fd, &msg, 0); ret = sendmsg(server_fd, &msg, 0);
if (ret <= 0) if (ret <= 0) {
THROW_ERROR("sendmsg failed"); THROW_ERROR("sendmsg failed");
}
return ret; return ret;
} }
@ -128,8 +136,7 @@ int main(int argc, const char *argv[]) {
int port = strtol(argv[2], NULL, 10); int port = strtol(argv[2], NULL, 10);
int server_fd = connect_with_server(argv[1], argv[2]); int server_fd = connect_with_server(argv[1], argv[2]);
switch (port) switch (port) {
{
case 8800: case 8800:
neogotiate_msg(server_fd, buf, buf_size); neogotiate_msg(server_fd, buf, buf_size);
break; break;

@ -17,8 +17,7 @@ typedef struct t_cpuid {
unsigned int edx; unsigned int edx;
} t_cpuid_t; } t_cpuid_t;
static inline void native_cpuid(int leaf, int subleaf, t_cpuid_t *p) static inline void native_cpuid(int leaf, int subleaf, t_cpuid_t *p) {
{
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
/* ecx is often an input as well as an output. */ /* ecx is often an input as well as an output. */
asm volatile("cpuid" asm volatile("cpuid"

@ -57,10 +57,11 @@ static int __fcntl_getlk_and_setlk(int fd, int open_flags) {
} }
// setlk // setlk
if ((open_flags & O_WRONLY) || (open_flags & O_RDWR)) if ((open_flags & O_WRONLY) || (open_flags & O_RDWR)) {
fl.l_type = F_WRLCK; fl.l_type = F_WRLCK;
else } else {
fl.l_type = F_RDLCK; fl.l_type = F_RDLCK;
}
ret = fcntl(fd, F_SETLK, &fl); ret = fcntl(fd, F_SETLK, &fl);
if (ret < 0) { if (ret < 0) {
THROW_ERROR("failed to call setlk"); THROW_ERROR("failed to call setlk");
@ -70,8 +71,9 @@ static int __fcntl_getlk_and_setlk(int fd, int open_flags) {
} }
static int __fcntl_dupfd(int fd, int open_flags) { static int __fcntl_dupfd(int fd, int open_flags) {
if (fcntl(fd, F_DUPFD, 0) < 0) if (fcntl(fd, F_DUPFD, 0) < 0) {
THROW_ERROR("failed to duplicate the fd"); THROW_ERROR("failed to duplicate the fd");
}
return 0; return 0;
} }
@ -87,8 +89,9 @@ static int test_fcntl_framework(test_fcntl_func_t fn) {
if (fd < 0) { if (fd < 0) {
THROW_ERROR("failed to open & create file"); THROW_ERROR("failed to open & create file");
} }
if (fn(fd, open_flags) < 0) if (fn(fd, open_flags) < 0) {
return -1; return -1;
}
close(fd); close(fd);
ret = unlink(file_path); ret = unlink(file_path);
if (ret < 0) { if (ret < 0) {

@ -177,12 +177,15 @@ typedef int(*test_file_func_t)(const char *);
static int test_file_framework(test_file_func_t fn) { static int test_file_framework(test_file_func_t fn) {
const char *file_path = "/root/test_filesystem_file_read_write.txt"; const char *file_path = "/root/test_filesystem_file_read_write.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -68,8 +68,7 @@ static int do_perm_tests(
const char **files, const char **files,
size_t num_files, size_t num_files,
int flags, int do_write, int flags, int do_write,
int* expected_results) int *expected_results) {
{
flags |= O_CREAT | O_TRUNC; flags |= O_CREAT | O_TRUNC;
for (size_t i = 0; i < num_files; i++) { for (size_t i = 0; i < num_files; i++) {
const char *filename = files[i]; const char *filename = files[i];

@ -5,8 +5,7 @@
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
if (argc <= 1) { if (argc <= 1) {
printf("Hello World!\n"); printf("Hello World!\n");
} } else {
else {
const char *echo_msg = argv[1]; const char *echo_msg = argv[1];
printf("%s\n", echo_msg); printf("%s\n", echo_msg);
} }

@ -90,12 +90,15 @@ typedef int(*test_hostfs_func_t)(const char *);
static int test_hostfs_framework(test_hostfs_func_t fn) { static int test_hostfs_framework(test_hostfs_func_t fn) {
const char *file_path = "/host/hostfs_test.txt"; const char *file_path = "/host/hostfs_test.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -83,8 +83,7 @@ static int do_SGXIOC_GET_EPID_GROUP_ID(int sgx_fd) {
int ret = ioctl(sgx_fd, SGXIOC_GET_EPID_GROUP_ID, &epid_group_id); int ret = ioctl(sgx_fd, SGXIOC_GET_EPID_GROUP_ID, &epid_group_id);
if (ret == 0) { if (ret == 0) {
break; break;
} } else if (errno != EBUSY) {
else if (errno != EBUSY) {
THROW_ERROR("failed to ioctl /dev/sgx"); THROW_ERROR("failed to ioctl /dev/sgx");
} }
@ -115,8 +114,7 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
int ret = ioctl(sgx_fd, SGXIOC_GEN_QUOTE, &gen_quote_arg); int ret = ioctl(sgx_fd, SGXIOC_GEN_QUOTE, &gen_quote_arg);
if (ret == 0) { if (ret == 0) {
break; break;
} } else if (errno != EBUSY) {
else if (errno != EBUSY) {
THROW_ERROR("failed to ioctl /dev/sgx"); THROW_ERROR("failed to ioctl /dev/sgx");
} }
@ -135,7 +133,8 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
if (quote->signature_len == 0) { if (quote->signature_len == 0) {
THROW_ERROR("invalid quote: zero-length signature"); THROW_ERROR("invalid quote: zero-length signature");
} }
if (memcmp(&gen_quote_arg.report_data, &quote->report_body.report_data, sizeof(sgx_report_data_t)) != 0) { if (memcmp(&gen_quote_arg.report_data, &quote->report_body.report_data,
sizeof(sgx_report_data_t)) != 0) {
THROW_ERROR("invalid quote: wrong report data"); THROW_ERROR("invalid quote: wrong report data");
} }
return 0; return 0;

@ -129,8 +129,7 @@ int test_anonymous_mmap_randomly() {
// Phrase 1: do mmap with random sizes until no more buffers or memory // Phrase 1: do mmap with random sizes until no more buffers or memory
for (num_bufs = 0; for (num_bufs = 0;
num_bufs < ARRAY_SIZE(bufs) && used_memory < MAX_MMAP_USED_MEMORY; num_bufs < ARRAY_SIZE(bufs) && used_memory < MAX_MMAP_USED_MEMORY;
num_bufs++) num_bufs++) {
{
// Choose the mmap size randomly // Choose the mmap size randomly
size_t len = rand() % (MAX_MMAP_USED_MEMORY - used_memory) + 1; size_t len = rand() % (MAX_MMAP_USED_MEMORY - used_memory) + 1;
len = ALIGN_UP(len, PAGE_SIZE); len = ALIGN_UP(len, PAGE_SIZE);
@ -324,7 +323,8 @@ int test_file_mmap_with_offset() {
THROW_ERROR("the buffer is not initialized according to the file"); THROW_ERROR("the buffer is not initialized according to the file");
} }
buf_cursor += second_len; buf_cursor += second_len;
if (check_bytes_in_buf(buf_cursor, ALIGN_UP(len, PAGE_SIZE) - (buf_cursor - buf), 0) < 0) { if (check_bytes_in_buf(buf_cursor, ALIGN_UP(len, PAGE_SIZE) - (buf_cursor - buf),
0) < 0) {
THROW_ERROR("the remaining of the last page occupied by the buffer is not initialized to zeros"); THROW_ERROR("the remaining of the last page occupied by the buffer is not initialized to zeros");
} }
@ -464,7 +464,7 @@ int test_fixed_mmap_with_non_page_aligned_addr() {
static int check_buf_is_munmapped(void *target_addr, size_t len) { static int check_buf_is_munmapped(void *target_addr, size_t len) {
// The trivial case of zero-len meory region is considered as unmapped // The trivial case of zero-len meory region is considered as unmapped
if (len == 0) return 0; if (len == 0) { return 0; }
// If the target_addr is not already mmaped, it should succeed to use it as // If the target_addr is not already mmaped, it should succeed to use it as
// a hint for mmap. // a hint for mmap.

@ -77,10 +77,12 @@ static int test_open_framework(test_open_func_t fn) {
int flags = O_RDONLY | O_CREAT | O_TRUNC; int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666; int mode = 00666;
if (fn(file_path, flags, mode) < 0) if (fn(file_path, flags, mode) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -21,11 +21,13 @@
int connect_with_child(int port, int *child_pid) { int connect_with_child(int port, int *child_pid) {
int ret = 0; int ret = 0;
int listen_fd = socket(AF_INET, SOCK_STREAM, 0); int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) if (listen_fd < 0) {
THROW_ERROR("create socket error"); THROW_ERROR("create socket error");
}
int reuse = 1; int reuse = 1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
THROW_ERROR("setsockopt port to reuse failed"); THROW_ERROR("setsockopt port to reuse failed");
}
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr)); memset(&servaddr, 0, sizeof(servaddr));
@ -65,11 +67,13 @@ int connect_with_child(int port, int *child_pid) {
int neogotiate_msg(int client_fd) { int neogotiate_msg(int client_fd) {
char buf[16]; char buf[16];
if (write(client_fd, ECHO_MSG, strlen(ECHO_MSG)) < 0) if (write(client_fd, ECHO_MSG, strlen(ECHO_MSG)) < 0) {
THROW_ERROR("write failed"); THROW_ERROR("write failed");
}
if (read(client_fd, buf, sizeof(RESPONSE)) < 0) if (read(client_fd, buf, sizeof(RESPONSE)) < 0) {
THROW_ERROR("read failed"); THROW_ERROR("read failed");
}
if (strncmp(buf, RESPONSE, sizeof(RESPONSE)) != 0) { if (strncmp(buf, RESPONSE, sizeof(RESPONSE)) != 0) {
THROW_ERROR("msg recv mismatch"); THROW_ERROR("msg recv mismatch");
@ -81,8 +85,9 @@ int server_recv(int client_fd) {
const int buf_size = 32; const int buf_size = 32;
char buf[buf_size]; char buf[buf_size];
if (recv(client_fd, buf, buf_size, 0) <= 0) if (recv(client_fd, buf, buf_size, 0) <= 0) {
THROW_ERROR("msg recv failed"); THROW_ERROR("msg recv failed");
}
if (strncmp(buf, ECHO_MSG, strlen(ECHO_MSG)) != 0) { if (strncmp(buf, ECHO_MSG, strlen(ECHO_MSG)) != 0) {
THROW_ERROR("msg recv mismatch"); THROW_ERROR("msg recv mismatch");
@ -125,8 +130,9 @@ int server_recvmsg(int client_fd) {
msg.msg_iov = NULL; msg.msg_iov = NULL;
msg.msg_iovlen = 0; msg.msg_iovlen = 0;
ret = recvmsg(client_fd, &msg, 0); ret = recvmsg(client_fd, &msg, 0);
if (ret != 0) if (ret != 0) {
THROW_ERROR("recvmsg empty failed"); THROW_ERROR("recvmsg empty failed");
}
return ret; return ret;
} }
@ -143,8 +149,9 @@ int server_connectionless_recvmsg() {
memset(&clientaddr, 0, sizeof(clientaddr)); memset(&clientaddr, 0, sizeof(clientaddr));
int sock = socket(AF_INET, SOCK_DGRAM, 0); int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) if (sock < 0) {
THROW_ERROR("create socket error"); THROW_ERROR("create socket error");
}
servaddr.sin_family = AF_INET; servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
@ -196,10 +203,11 @@ int test_read_write() {
int ret = 0; int ret = 0;
int child_pid = 0; int child_pid = 0;
int client_fd = connect_with_child(8800, &child_pid); int client_fd = connect_with_child(8800, &child_pid);
if (client_fd < 0) if (client_fd < 0) {
THROW_ERROR("connect failed"); THROW_ERROR("connect failed");
else } else {
ret = neogotiate_msg(client_fd); ret = neogotiate_msg(client_fd);
}
//wait for the child to exit for next spawn //wait for the child to exit for next spawn
int status = 0; int status = 0;
@ -214,14 +222,16 @@ int test_send_recv() {
int ret = 0; int ret = 0;
int child_pid = 0; int child_pid = 0;
int client_fd = connect_with_child(8801, &child_pid); int client_fd = connect_with_child(8801, &child_pid);
if (client_fd < 0) if (client_fd < 0) {
THROW_ERROR("connect failed"); THROW_ERROR("connect failed");
}
if (neogotiate_msg(client_fd) < 0) if (neogotiate_msg(client_fd) < 0) {
THROW_ERROR("neogotiate failed"); THROW_ERROR("neogotiate failed");
}
ret = server_recv(client_fd); ret = server_recv(client_fd);
if (ret < 0) return -1; if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid); ret = wait_for_child_exit(child_pid);
@ -232,14 +242,16 @@ int test_sendmsg_recvmsg() {
int ret = 0; int ret = 0;
int child_pid = 0; int child_pid = 0;
int client_fd = connect_with_child(8802, &child_pid); int client_fd = connect_with_child(8802, &child_pid);
if (client_fd < 0) if (client_fd < 0) {
THROW_ERROR("connect failed"); THROW_ERROR("connect failed");
}
if (neogotiate_msg(client_fd) < 0) if (neogotiate_msg(client_fd) < 0) {
THROW_ERROR("neogotiate failed"); THROW_ERROR("neogotiate failed");
}
ret = server_recvmsg(client_fd); ret = server_recvmsg(client_fd);
if (ret < 0) return -1; if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid); ret = wait_for_child_exit(child_pid);
@ -257,7 +269,7 @@ int test_sendmsg_recvmsg_connectionless() {
} }
ret = server_connectionless_recvmsg(); ret = server_connectionless_recvmsg();
if (ret < 0) return -1; if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid); ret = wait_for_child_exit(child_pid);
@ -271,19 +283,23 @@ int test_fcntl_setfl_and_getfl() {
int original_flags, actual_flags; int original_flags, actual_flags;
client_fd = connect_with_child(8804, &child_pid); client_fd = connect_with_child(8804, &child_pid);
if (client_fd < 0) if (client_fd < 0) {
THROW_ERROR("connect failed"); THROW_ERROR("connect failed");
}
original_flags = fcntl(client_fd, F_GETFL, 0); original_flags = fcntl(client_fd, F_GETFL, 0);
if (original_flags < 0) if (original_flags < 0) {
THROW_ERROR("fcntl getfl failed"); THROW_ERROR("fcntl getfl failed");
}
ret = fcntl(client_fd, F_SETFL, original_flags | O_NONBLOCK); ret = fcntl(client_fd, F_SETFL, original_flags | O_NONBLOCK);
if (ret < 0) if (ret < 0) {
THROW_ERROR("fcntl setfl failed"); THROW_ERROR("fcntl setfl failed");
}
actual_flags = fcntl(client_fd, F_GETFL, 0); actual_flags = fcntl(client_fd, F_GETFL, 0);
if (actual_flags != (original_flags | O_NONBLOCK)) if (actual_flags != (original_flags | O_NONBLOCK)) {
THROW_ERROR("check the getfl value after setfl failed"); THROW_ERROR("check the getfl value after setfl failed");
}
ret = wait_for_child_exit(child_pid); ret = wait_for_child_exit(child_pid);
@ -300,14 +316,16 @@ int test_poll_sockets() {
}; };
ret = poll(pollfds, 2, 0); ret = poll(pollfds, 2, 0);
if (ret < 0) if (ret < 0) {
THROW_ERROR("poll error"); THROW_ERROR("poll error");
}
if (pollfds[0].fd != socks[0] || if (pollfds[0].fd != socks[0] ||
pollfds[0].events != POLLIN || pollfds[0].events != POLLIN ||
pollfds[1].fd != socks[1] || pollfds[1].fd != socks[1] ||
pollfds[1].events != POLLIN) pollfds[1].events != POLLIN) {
THROW_ERROR("fd and events of pollfd should remain unchanged"); THROW_ERROR("fd and events of pollfd should remain unchanged");
}
return 0; return 0;
} }

@ -30,8 +30,9 @@ static int create_and_bind() {
servaddr.sin_port = htons(6667); servaddr.sin_port = htons(6667);
int reuse = 1; int reuse = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
THROW_ERROR("setsockopt port to reuse failed"); THROW_ERROR("setsockopt port to reuse failed");
}
int ret = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); int ret = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) { if (ret < 0) {

@ -104,8 +104,9 @@ static void handle_sigio(int num, siginfo_t* info, void* context) {
printf("Hello from SIGIO signal handler (recursion_level = %d)!\n", recursion_level); printf("Hello from SIGIO signal handler (recursion_level = %d)!\n", recursion_level);
recursion_level++; recursion_level++;
if (recursion_level <= MAX_RECURSION_LEVEL) if (recursion_level <= MAX_RECURSION_LEVEL) {
raise(SIGIO); raise(SIGIO);
}
recursion_level--; recursion_level--;
} }
@ -279,8 +280,9 @@ static void handle_sigpipe(int num, siginfo_t* info, void* context) {
g_old_ss = old_ss; g_old_ss = old_ss;
recursion_level++; recursion_level++;
if (recursion_level <= MAX_ALTSTACK_RECURSION_LEVEL) if (recursion_level <= MAX_ALTSTACK_RECURSION_LEVEL) {
raise(SIGPIPE); raise(SIGPIPE);
}
recursion_level--; recursion_level--;
} }
@ -348,7 +350,7 @@ int test_sigchld() {
printf("Spawn a new proces successfully (pid = %d)\n", child_pid); printf("Spawn a new proces successfully (pid = %d)\n", child_pid);
wait(NULL); wait(NULL);
if (sigchld == 0) THROW_ERROR("Did not receive SIGCHLD"); if (sigchld == 0) { THROW_ERROR("Did not receive SIGCHLD"); }
return 0; return 0;
} }
@ -372,11 +374,9 @@ int main(int argc, const char* argv[]) {
const char *cmd = argv[1]; const char *cmd = argv[1];
if (strcmp(cmd, "aborted_child") == 0) { if (strcmp(cmd, "aborted_child") == 0) {
return aborted_child(); return aborted_child();
} } else if (strcmp(cmd, "killed_child") == 0) {
else if (strcmp(cmd, "killed_child") == 0) {
return killed_child(); return killed_child();
} } else {
else {
fprintf(stderr, "ERROR: unknown command: %s\n", cmd); fprintf(stderr, "ERROR: unknown command: %s\n", cmd);
return EXIT_FAILURE; return EXIT_FAILURE;
} }

@ -23,8 +23,7 @@ static inline void validate_timespec(const struct timespec* tv) {
// retval = (a < b) ? -1 : ((a > b) ? 1 : 0) // retval = (a < b) ? -1 : ((a > b) ? 1 : 0)
static int timespec_cmp(const struct timespec *a, const struct timespec *b) static int timespec_cmp(const struct timespec *a, const struct timespec *b) {
{
validate_timespec(a); validate_timespec(a);
validate_timespec(b); validate_timespec(b);
@ -40,16 +39,17 @@ static int timespec_cmp(const struct timespec *a, const struct timespec *b)
// diff = | a - b | // diff = | a - b |
static void timespec_diff(const struct timespec *a, const struct timespec *b, static void timespec_diff(const struct timespec *a, const struct timespec *b,
struct timespec *diff) struct timespec *diff) {
{
validate_timespec(a); validate_timespec(a);
validate_timespec(b); validate_timespec(b);
const struct timespec *begin, *end; const struct timespec *begin, *end;
if (timespec_cmp(a, b) <= 0) { if (timespec_cmp(a, b) <= 0) {
begin = a; end = b; begin = a;
end = b;
} else { } else {
begin = b; end = a; begin = b;
end = a;
} }
diff->tv_nsec = end->tv_nsec - begin->tv_nsec; diff->tv_nsec = end->tv_nsec - begin->tv_nsec;
@ -64,8 +64,7 @@ static void timespec_diff(const struct timespec *a, const struct timespec *b,
// retval = | a - b | <= precision // retval = | a - b | <= precision
static int timespec_equal(const struct timespec *a, const struct timespec *b, static int timespec_equal(const struct timespec *a, const struct timespec *b,
const struct timespec *precision) const struct timespec *precision) {
{
struct timespec diff; struct timespec diff;
timespec_diff(a, b, &diff); timespec_diff(a, b, &diff);
return timespec_cmp(&diff, precision) <= 0; return timespec_cmp(&diff, precision) <= 0;

@ -145,12 +145,15 @@ typedef int(*test_stat_func_t)(const char *);
static int test_stat_framework(test_stat_func_t fn) { static int test_stat_framework(test_stat_func_t fn) {
const char *file_path = "/root/test_filesystem_stat.txt"; const char *file_path = "/root/test_filesystem_stat.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -115,12 +115,15 @@ typedef int(*test_readlink_func_t)(const char *);
static int test_readlink_framework(test_readlink_func_t fn) { static int test_readlink_framework(test_readlink_func_t fn) {
const char *file_path = "/root/test_filesystem_symlink.txt"; const char *file_path = "/root/test_filesystem_symlink.txt";
if (create_file(file_path) < 0) if (create_file(file_path) < 0) {
return -1; return -1;
if (fn(file_path) < 0) }
if (fn(file_path) < 0) {
return -1; return -1;
if (remove_file(file_path) < 0) }
if (remove_file(file_path) < 0) {
return -1; return -1;
}
return 0; return 0;
} }

@ -156,8 +156,9 @@ typedef int(*create_connection_func_t)(int *);
int test_connected_sockets_inter_process(create_connection_func_t fn) { int test_connected_sockets_inter_process(create_connection_func_t fn) {
int ret = 0; int ret = 0;
int sockets[2]; int sockets[2];
if (fn(sockets) < 0) if (fn(sockets) < 0) {
return -1; return -1;
}
ret = verify_child_echo(sockets); ret = verify_child_echo(sockets);

@ -113,23 +113,24 @@ static sgx_errlist_t sgx_errlist[] = {
}; };
/* Check error conditions for loading enclave */ /* Check error conditions for loading enclave */
static void print_error_message(sgx_status_t ret) static void print_error_message(sgx_status_t ret) {
{
size_t idx = 0; size_t idx = 0;
size_t ttl = sizeof sgx_errlist / sizeof sgx_errlist[0]; size_t ttl = sizeof sgx_errlist / sizeof sgx_errlist[0];
for (idx = 0; idx < ttl; idx++) { for (idx = 0; idx < ttl; idx++) {
if (ret == sgx_errlist[idx].err) { if (ret == sgx_errlist[idx].err) {
if(NULL != sgx_errlist[idx].sug) if (NULL != sgx_errlist[idx].sug) {
printf("Info: %s\n", sgx_errlist[idx].sug); printf("Info: %s\n", sgx_errlist[idx].sug);
}
printf("Error: %s\n", sgx_errlist[idx].msg); printf("Error: %s\n", sgx_errlist[idx].msg);
break; break;
} }
} }
if (idx == ttl) if (idx == ttl) {
printf("Error: Unexpected error occurred.\n"); printf("Error: Unexpected error occurred.\n");
} }
}
static const char *get_enclave_absolute_path() { static const char *get_enclave_absolute_path() {
static char enclave_path[MAX_PATH] = {0}; static char enclave_path[MAX_PATH] = {0};
@ -148,8 +149,7 @@ static const char* get_enclave_absolute_path() {
* Step 2: call sgx_create_enclave to initialize an enclave instance * Step 2: call sgx_create_enclave to initialize an enclave instance
* Step 3: save the launch token if it is updated * Step 3: save the launch token if it is updated
*/ */
static int initialize_enclave(void) static int initialize_enclave(void) {
{
char token_path[MAX_PATH] = {'\0'}; char token_path[MAX_PATH] = {'\0'};
sgx_launch_token_t token = {0}; sgx_launch_token_t token = {0};
sgx_status_t ret = SGX_ERROR_UNEXPECTED; sgx_status_t ret = SGX_ERROR_UNEXPECTED;
@ -190,26 +190,28 @@ static int initialize_enclave(void)
/* Step 2: call sgx_create_enclave to initialize an enclave instance */ /* Step 2: call sgx_create_enclave to initialize an enclave instance */
/* Debug Support: set 2nd parameter to 1 */ /* Debug Support: set 2nd parameter to 1 */
const char *enclave_path = get_enclave_absolute_path(); const char *enclave_path = get_enclave_absolute_path();
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL); ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, &global_eid,
NULL);
if (ret != SGX_SUCCESS) { if (ret != SGX_SUCCESS) {
print_error_message(ret); print_error_message(ret);
if (fp != NULL) fclose(fp); if (fp != NULL) { fclose(fp); }
return -1; return -1;
} }
/* Step 3: save the launch token if it is updated */ /* Step 3: save the launch token if it is updated */
if (updated == 0 || fp == NULL) { if (updated == 0 || fp == NULL) {
/* if the token is not updated, or file handler is invalid, do not perform saving */ /* if the token is not updated, or file handler is invalid, do not perform saving */
if (fp != NULL) fclose(fp); if (fp != NULL) { fclose(fp); }
return 0; return 0;
} }
/* reopen the file with write capablity */ /* reopen the file with write capablity */
fp = freopen(token_path, "wb", fp); fp = freopen(token_path, "wb", fp);
if (fp == NULL) return 0; if (fp == NULL) { return 0; }
size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp); size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
if (write_num != sizeof(sgx_launch_token_t)) if (write_num != sizeof(sgx_launch_token_t)) {
printf("Warning: Failed to save launch token to \"%s\".\n", token_path); printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
}
fclose(fp); fclose(fp);
return 0; return 0;
} }
@ -267,20 +269,16 @@ static int parse_args(
char *argv[], char *argv[],
/* outputs */ /* outputs */
int *arg_command, int *arg_command,
char** arg_file_path) char **arg_file_path) {
{ if (argc != 3) { return -1; }
if (argc != 3) return -1;
if (strcmp(argv[1], "protect") == 0) { if (strcmp(argv[1], "protect") == 0) {
*arg_command = CMD_PROTECT; *arg_command = CMD_PROTECT;
} } else if (strcmp(argv[1], "show") == 0) {
else if (strcmp(argv[1], "show") == 0) {
*arg_command = CMD_SHOW; *arg_command = CMD_SHOW;
} } else if (strcmp(argv[1], "show-mac") == 0) {
else if (strcmp(argv[1], "show-mac") == 0) {
*arg_command = CMD_SHOW_MAC; *arg_command = CMD_SHOW_MAC;
} } else {
else {
return -1; return -1;
} }

@ -33,7 +33,7 @@ static int eprintf(const char* fmt, ...) {
static void print_mac(sgx_aes_gcm_128bit_tag_t *mac) { static void print_mac(sgx_aes_gcm_128bit_tag_t *mac) {
unsigned char *bytes = (unsigned char *) mac; unsigned char *bytes = (unsigned char *) mac;
for (size_t bi = 0; bi < sizeof(*mac); bi++) { for (size_t bi = 0; bi < sizeof(*mac); bi++) {
if (bi != 0) printf("-"); if (bi != 0) { printf("-"); }
printf("%02x", bytes[bi] & 0xFF); printf("%02x", bytes[bi] & 0xFF);
} }
printf("\n"); printf("\n");