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

@ -5,7 +5,7 @@
extern "C" {
#endif
const char* errno2str(int errno_);
const char *errno2str(int errno_);
#ifdef __cplusplus
}

@ -30,14 +30,14 @@ typedef struct occlum_pal_attr {
// ".occlum"; it can be renamed to an arbitrary name.
//
// Mandatory field. Must not be NULL.
const char* instance_dir;
const char *instance_dir;
// Log level.
//
// Specifies the log level of Occlum LibOS. Valid values: "off", "error",
// "warn", "info", and "trace". Case insensitive.
//
// Optional field. If NULL, the LibOS will treat it as "off".
const char* log_level;
const char *log_level;
} occlum_pal_attr_t;
#define OCCLUM_PAL_ATTR_INITVAL { \
@ -61,7 +61,7 @@ typedef struct occlum_stdio_fds {
*
* @retval If 0, then success; otherwise, check errno for the exact error type.
*/
int occlum_pal_init(const struct occlum_pal_attr* attr);
int occlum_pal_init(const struct occlum_pal_attr *attr);
/*
* @brief Execute a command inside the Occlum enclave
@ -82,11 +82,11 @@ int occlum_pal_init(const struct occlum_pal_attr* attr);
*
* @retval If 0, then success; otherwise, check errno for the exact error type.
*/
int occlum_pal_exec(const char* cmd_path,
const char** cmd_args,
const char** cmd_env,
const struct occlum_stdio_fds* io_fds,
int* exit_status);
int occlum_pal_exec(const char *cmd_path,
const char **cmd_args,
const char **cmd_env,
const struct occlum_stdio_fds *io_fds,
int *exit_status);
/*
* @brief Send a signal to one or multiple LibOS processes

@ -1,21 +1,35 @@
#include <errno.h>
#include "errno2str.h"
const char* errno2str(int errno_) {
switch(errno_) {
case EPERM: return "EPERM";
case ENOENT: return "ENOENT";
case ESRCH: return "ESRCH";
case ENOEXEC: return "ENOEXEC";
case EBADF: return "EBADF";
case ECHILD: return "ECHILD";
case EAGAIN: 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";
const char *errno2str(int errno_) {
switch (errno_) {
case EPERM:
return "EPERM";
case ENOENT:
return "ENOENT";
case ESRCH:
return "ESRCH";
case ENOEXEC:
return "ENOEXEC";
case EBADF:
return "EBADF";
case ECHILD:
return "ECHILD";
case EAGAIN:
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";
}
}

@ -2,9 +2,8 @@
#include "ocalls.h"
sgx_status_t occlum_ocall_sgx_init_quote(
sgx_target_info_t* target_info,
sgx_epid_group_id_t* epid_group_id)
{
sgx_target_info_t *target_info,
sgx_epid_group_id_t *epid_group_id) {
// Intel's manual:
// 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.
@ -12,16 +11,15 @@ sgx_status_t occlum_ocall_sgx_init_quote(
}
sgx_status_t occlum_ocall_sgx_get_quote(
uint8_t* sigrl,
uint8_t *sigrl,
uint32_t sigrl_len,
sgx_report_t* report,
sgx_report_t *report,
sgx_quote_sign_type_t quote_type,
sgx_spid_t* spid,
sgx_quote_nonce_t* nonce,
sgx_report_t* qe_report,
sgx_quote_t* quote_buf,
uint32_t quote_buf_len)
{
sgx_spid_t *spid,
sgx_quote_nonce_t *nonce,
sgx_report_t *qe_report,
sgx_quote_t *quote_buf,
uint32_t quote_buf_len) {
sgx_status_t ret = SGX_SUCCESS;
uint32_t real_quote_len;

@ -16,18 +16,18 @@ typedef enum {
#define COLOR_GREEN "\x1B[32m"
static level_t new_level(unsigned int level) {
if (level >= 5) level = 5;
if (level >= 5) { level = 5; }
return (level_t) level;
}
void occlum_ocall_print_log(unsigned int _level, const char* msg) {
void occlum_ocall_print_log(unsigned int _level, const char *msg) {
level_t level = new_level(_level);
if (level == LEVEL_OFF) {
return;
}
const char* color;
switch(level) {
const char *color;
switch (level) {
case LEVEL_ERROR:
color = COLOR_RED;
break;

@ -1,15 +1,15 @@
#include <stdlib.h>
#include "ocalls.h"
void* occlum_ocall_posix_memalign(size_t alignment, size_t size) {
void* ptr = NULL;
void *occlum_ocall_posix_memalign(size_t alignment, size_t size) {
void *ptr = NULL;
int ret = posix_memalign(&ptr, alignment, size);
if (ret == 0) {
return ptr;
}
// Handle errors
switch(ret) {
switch (ret) {
case ENOMEM:
PAL_ERROR("Out of memory on the untrusted side");
break;
@ -22,6 +22,6 @@ void* occlum_ocall_posix_memalign(size_t alignment, size_t size) {
return NULL;
}
void occlum_ocall_free(void* ptr) {
void occlum_ocall_free(void *ptr) {
free(ptr);
}

@ -11,14 +11,13 @@ ssize_t occlum_ocall_sendmsg(int sockfd,
size_t msg_iovlen,
const void *msg_control,
size_t msg_controllen,
int flags)
{
int flags) {
struct msghdr msg = {
(void*) msg_name,
(void *) msg_name,
msg_namelen,
(struct iovec *) msg_iov,
msg_iovlen,
(void*) msg_control,
(void *) msg_control,
msg_controllen,
0,
};
@ -28,15 +27,14 @@ ssize_t occlum_ocall_sendmsg(int sockfd,
ssize_t occlum_ocall_recvmsg(int sockfd,
void *msg_name,
socklen_t msg_namelen,
socklen_t* msg_namelen_recv,
socklen_t *msg_namelen_recv,
struct iovec *msg_iov,
size_t msg_iovlen,
void *msg_control,
size_t msg_controllen,
size_t* msg_controllen_recv,
int* msg_flags_recv,
int flags)
{
size_t *msg_controllen_recv,
int *msg_flags_recv,
int flags) {
struct msghdr msg = {
msg_name,
msg_namelen,
@ -47,7 +45,7 @@ ssize_t occlum_ocall_recvmsg(int sockfd,
0,
};
ssize_t ret = recvmsg(sockfd, &msg, flags);
if (ret < 0) return ret;
if (ret < 0) { return ret; }
*msg_namelen_recv = msg.msg_namelen;
*msg_controllen_recv = msg.msg_controllen;

@ -3,7 +3,7 @@
#include <unistd.h>
#include "ocalls.h"
int occlum_ocall_sched_getaffinity(size_t cpusize, unsigned char* buf) {
int occlum_ocall_sched_getaffinity(size_t cpusize, unsigned char *buf) {
int ret;
cpu_set_t mask;
CPU_ZERO(&mask);
@ -13,7 +13,8 @@ int occlum_ocall_sched_getaffinity(size_t cpusize, unsigned char* buf) {
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);
}

@ -7,15 +7,16 @@ typedef struct {
int libos_tid;
} thread_data_t;
void* exec_libos_thread(void* _thread_data) {
thread_data_t* thread_data = _thread_data;
void *exec_libos_thread(void *_thread_data) {
thread_data_t *thread_data = _thread_data;
sgx_enclave_id_t eid = thread_data->enclave_id;
int host_tid = gettid();
int libos_tid = thread_data->libos_tid;
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) {
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);
exit(EXIT_FAILURE);
}
@ -29,7 +30,7 @@ int occlum_ocall_exec_thread_async(int libos_tid) {
int ret = 0;
pthread_t thread;
thread_data_t* thread_data = malloc(sizeof *thread_data);
thread_data_t *thread_data = malloc(sizeof * thread_data);
thread_data->enclave_id = pal_get_enclave_id();
thread_data->libos_tid = libos_tid;

@ -2,7 +2,7 @@
#include <sys/time.h>
#include "ocalls.h"
void occlum_ocall_gettimeofday(struct timeval* tv) {
void occlum_ocall_gettimeofday(struct timeval *tv) {
gettimeofday(tv, NULL);
}
@ -10,14 +10,14 @@ void occlum_ocall_clock_gettime(int clockid, struct timespec *tp) {
clock_gettime(clockid, tp);
}
void occlum_ocall_nanosleep(const struct timespec* req) {
void occlum_ocall_nanosleep(const struct timespec *req) {
nanosleep(req, NULL);
}
int occlum_ocall_thread_getcpuclock(struct timespec *tp) {
clockid_t thread_clock_id;
int ret = pthread_getcpuclockid(pthread_self(), &thread_clock_id);
if(ret != 0) {
if (ret != 0) {
PAL_ERROR("failed to get clock id");
return -1;
}
@ -25,7 +25,7 @@ int occlum_ocall_thread_getcpuclock(struct timespec *tp) {
return clock_gettime(thread_clock_id, tp);
}
void occlum_ocall_rdtsc(uint32_t* low, uint32_t* high) {
void occlum_ocall_rdtsc(uint32_t *low, uint32_t *high) {
uint64_t rax, rdx;
asm volatile("rdtsc" : "=a"(rax), "=d"(rdx));
*low = (uint32_t)rax;

@ -10,7 +10,7 @@ int occlum_pal_get_version(void) {
return OCCLUM_PAL_VERSION;
}
int occlum_pal_init(const struct occlum_pal_attr* attr) {
int occlum_pal_init(const struct occlum_pal_attr *attr) {
errno = 0;
if (attr == NULL) {
@ -35,9 +35,10 @@ int occlum_pal_init(const struct occlum_pal_attr* attr) {
eid = pal_get_enclave_id();
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) {
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);
return -1;
}
@ -49,11 +50,11 @@ int occlum_pal_init(const struct occlum_pal_attr* attr) {
return 0;
}
int occlum_pal_exec(const char* cmd_path,
const char** cmd_args,
const char** cmd_env,
const struct occlum_stdio_fds* io_fds,
int* exit_status) {
int occlum_pal_exec(const char *cmd_path,
const char **cmd_args,
const char **cmd_env,
const struct occlum_stdio_fds *io_fds,
int *exit_status) {
errno = 0;
if (cmd_path == NULL || cmd_args == NULL || exit_status == NULL) {
@ -69,9 +70,10 @@ int occlum_pal_exec(const char* cmd_path,
}
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) {
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);
return -1;
}
@ -85,7 +87,7 @@ int occlum_pal_exec(const char* cmd_path,
int host_tid = gettid();
ecall_status = occlum_ecall_exec_thread(eid, &ecall_ret, libos_tid, host_tid);
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);
return -1;
}
@ -112,7 +114,7 @@ int occlum_pal_kill(int pid, int sig) {
int ecall_ret = 0;
sgx_status_t ecall_status = occlum_ecall_kill(eid, &ecall_ret, pid, sig);
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);
return -1;
}

@ -31,7 +31,7 @@ static sgx_enclave_id_t global_eid = SGX_INVALID_ENCLAVE_ID;
/* Get enclave debug flag according to env "OCCLUM_RELEASE_ENCLAVE" */
static int get_enclave_debug_flag() {
const char* release_enclave_val = getenv("OCCLUM_RELEASE_ENCLAVE");
const char *release_enclave_val = getenv("OCCLUM_RELEASE_ENCLAVE");
if (release_enclave_val) {
if (!strcmp(release_enclave_val, "1") ||
!strcasecmp(release_enclave_val, "y") ||
@ -43,12 +43,12 @@ static int get_enclave_debug_flag() {
return 1;
}
static const char* get_enclave_absolute_path(const char* instance_dir) {
static const char *get_enclave_absolute_path(const char *instance_dir) {
static char enclave_path[MAX_PATH + 1] = {0};
strncat(enclave_path, instance_dir, MAX_PATH);
strncat(enclave_path, "/build/lib/", MAX_PATH);
strncat(enclave_path, ENCLAVE_FILENAME, MAX_PATH);
return (const char*)enclave_path;
return (const char *)enclave_path;
}
/* Initialize the enclave:
@ -56,7 +56,7 @@ static const char* get_enclave_absolute_path(const char* instance_dir) {
* Step 2: call sgx_create_enclave to initialize an enclave instance
* Step 3: save the launch token if it is updated
*/
int pal_init_enclave(const char* instance_dir) {
int pal_init_enclave(const char *instance_dir) {
char token_path[MAX_PATH] = {'\0'};
sgx_launch_token_t token = {0};
sgx_status_t ret = SGX_ERROR_UNEXPECTED;
@ -69,11 +69,11 @@ int pal_init_enclave(const char* instance_dir) {
const char *home_dir = getpwuid(getuid())->pw_dir;
if (home_dir != NULL &&
(strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
(strlen(home_dir) + strlen("/") + sizeof(TOKEN_FILENAME) + 1) <= MAX_PATH) {
/* compose the token path */
strncpy(token_path, home_dir, strlen(home_dir));
strncat(token_path, "/", strlen("/"));
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
} else {
/* if token path is too long or $HOME is NULL */
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
@ -96,29 +96,31 @@ int pal_init_enclave(const char* instance_dir) {
/* Step 2: call sgx_create_enclave to initialize an enclave instance */
/* 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();
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) {
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);
if (fp != NULL) fclose(fp);
if (fp != NULL) { fclose(fp); }
return -1;
}
/* Step 3: save the launch token if it is updated */
if (updated == 0 || fp == NULL) {
/* 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;
}
/* reopen the file with write capablity */
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);
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);
}
fclose(fp);
return 0;
}

@ -79,10 +79,10 @@ static sgx_err_msg_t err_msg_table[] = {
},
};
const char* pal_get_sgx_error_msg(sgx_status_t error) {
int err_max = sizeof err_msg_table/sizeof err_msg_table[0];
const char *pal_get_sgx_error_msg(sgx_status_t error) {
int err_max = sizeof err_msg_table / sizeof err_msg_table[0];
for (int err_i = 0; err_i < err_max; err_i++) {
if(error == err_msg_table[err_i].err) {
if (error == err_msg_table[err_i].err) {
return err_msg_table[err_i].msg;
}
}

@ -6,25 +6,24 @@
#include <sys/wait.h>
#include <occlum_pal_api.h>
static const char* get_instance_dir(void) {
const char* instance_dir_from_env = (const char*) getenv("OCCLUM_INSTANCE_DIR");
static const char *get_instance_dir(void) {
const char *instance_dir_from_env = (const char *) getenv("OCCLUM_INSTANCE_DIR");
if (instance_dir_from_env != NULL) {
return instance_dir_from_env;
}
else {
} else {
return "./.occlum";
}
}
int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
// Parse arguments
if (argc < 2) {
fprintf(stderr, "[ERROR] occlum-run: at least one argument must be provided\n\n");
fprintf(stderr, "Usage: occlum-run <executable> [<args>]\n");
return EXIT_FAILURE;
}
const char* cmd_path = (const char*) argv[1];
const char** cmd_args = (const char**) &argv[2];
const char *cmd_path = (const char *) argv[1];
const char **cmd_args = (const char **) &argv[2];
extern const char **environ;
// Check Occlum PAL version

@ -11,7 +11,7 @@
// ============================================================================
static int create_file(const char *file_path, mode_t mode) {
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int fd;
fd = open(file_path, flags, mode);
@ -116,8 +116,9 @@ static int test_access_framework(test_access_func_t fn) {
if (create_file(file_path, mode) < 0) {
return -1;
}
if (fn(file_path) < 0)
if (fn(file_path) < 0) {
return -1;
}
return 0;
}

@ -11,7 +11,7 @@
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00444;
fd = open(file_path, flags, mode);
@ -85,12 +85,15 @@ typedef int(*test_chmod_func_t)(const char *);
static int test_chmod_framework(test_chmod_func_t fn) {
const char *file_path = "/root/test_filesystem_chmod.txt";
if (create_file(file_path) < 0)
if (create_file(file_path) < 0) {
return -1;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -11,7 +11,7 @@
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00444;
fd = open(file_path, flags, mode);
@ -107,12 +107,15 @@ typedef int(*test_chown_func_t)(const char *);
static int test_chown_framework(test_chown_func_t fn) {
const char *file_path = "/root/test_filesystem_chown.txt";
if (create_file(file_path) < 0)
if (create_file(file_path) < 0) {
return -1;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

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

@ -1,5 +1,5 @@
#include <iostream>
int main(){
std::cout<< "hello world" <<std::endl;
int main() {
std::cout << "hello world" << std::endl;
return 0;
}

@ -17,8 +17,7 @@ typedef struct t_cpuid {
unsigned int edx;
} 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));
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
@ -184,7 +183,7 @@ static int test_cpuid_with_invalid_leaf() {
int leaf[] = {0x8, 0xC, 0xE, 0x11};
int subleaf = 0;
for (int i = 0; i < sizeof(leaf)/sizeof(leaf[0]); i++) {
for (int i = 0; i < sizeof(leaf) / sizeof(leaf[0]); i++) {
if (leaf[i] > g_max_basic_leaf) {
printf("Warning: test leaf 0x%x is greater than CPU max basic leaf. Skipped.\n", leaf[i]);
continue;
@ -235,7 +234,7 @@ static int test_cpuid_with_random_leaf() {
#define BUFF_SIZE (1024)
static int test_cpuid_with_host_cpuidinfo() {
char buff[BUFF_SIZE] = {0};
FILE * fp = fopen("./test_cpuid.txt","r");
FILE *fp = fopen("./test_cpuid.txt", "r");
if (fp == NULL) {
THROW_ERROR("failed to open host cpuid.txt");
}

@ -6,7 +6,7 @@
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
// This program consumes a specific amount of data from stdin
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
// Get the total number of bytes to read
size_t remain_bytes = 0;
while (read(0, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes));

@ -10,7 +10,7 @@
// Test utilities
// ============================================================================
static int check_file_readable(const char* filename) {
static int check_file_readable(const char *filename) {
int fd;
char buf[512] = {0};
int len;
@ -24,7 +24,7 @@ static int check_file_readable(const char* filename) {
return 0;
}
static int check_file_writable(const char* filename) {
static int check_file_writable(const char *filename) {
int fd;
char buf[512] = {0};
int len;

@ -1,3 +1,3 @@
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return 0;
}

@ -47,7 +47,7 @@ static inline uint64_t native_syscall(syscall_args_t *p) {
int test_mmap_and_munmap_via_syscall_instruction() {
int len = PAGE_SIZE;
syscall_args_t mmap_arg = {
.num= __NR_mmap,
.num = __NR_mmap,
.arg0 = (unsigned long) NULL,
.arg1 = len,
.arg2 = PROT_READ | PROT_WRITE,
@ -66,7 +66,7 @@ int test_mmap_and_munmap_via_syscall_instruction() {
}
syscall_args_t munmap_arg = {
.num= __NR_munmap,
.num = __NR_munmap,
.arg0 = (unsigned long) buf,
.arg1 = len,
};
@ -84,6 +84,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_mmap_and_munmap_via_syscall_instruction),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

20
test/env/main.c vendored

@ -13,12 +13,12 @@
// Helper structs & variables & functions
// ============================================================================
const char** g_argv;
const char **g_argv;
int g_argc;
// Expected arguments are given by Makefile throught macro ARGC, ARG1, ARG2 and
// ARG3
const char* expect_argv[EXPECT_ARGC] = {
const char *expect_argv[EXPECT_ARGC] = {
"env",
EXPECT_ARG1,
EXPECT_ARG2,
@ -27,22 +27,22 @@ const char* expect_argv[EXPECT_ARGC] = {
// Expected child arguments
const int child_argc = 2;
const char* child_argv[3] = {
const char *child_argv[3] = {
"env",
"child",
NULL
};
// Expected child environment variables
const char* child_envp[] = {
const char *child_envp[] = {
"ENV_CHILD=ok",
NULL
};
static int test_argv_val(const char** expect_argv) {
static int test_argv_val(const char **expect_argv) {
for (int arg_i = 0; arg_i < g_argc; arg_i++) {
const char* actual_arg = *(g_argv + arg_i);
const char* expect_arg = *(expect_argv + arg_i);
const char *actual_arg = *(g_argv + arg_i);
const char *expect_arg = *(expect_argv + arg_i);
if (strcmp(actual_arg, expect_arg) != 0) {
printf("ERROR: expect argument %d is %s, but given %s\n",
arg_i, expect_arg, actual_arg);
@ -52,8 +52,8 @@ static int test_argv_val(const char** expect_argv) {
return 0;
}
static int test_env_val(const char* expect_env_key, const char* expect_env_val) {
const char* actual_env_val = getenv(expect_env_key);
static int test_env_val(const char *expect_env_key, const char *expect_env_val) {
const char *actual_env_val = getenv(expect_env_key);
if (actual_env_val == NULL) {
printf("ERROR: cannot find %s\n", expect_env_key);
return -1;
@ -204,7 +204,7 @@ static test_case_t child_test_cases[] = {
TEST_CASE(test_env_child_getenv),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
// Save argument for test cases
g_argc = argc;
g_argv = argv;

@ -131,7 +131,7 @@ int test_read_write() {
THROW_ERROR("received length is not as expected");
}
data_recv += cur_data;
} while (data_recv != TEST_DATA*CHILD_NUM);
} while (data_recv != TEST_DATA * CHILD_NUM);
close(event_fd);
@ -162,7 +162,7 @@ int test_select_with_socket() {
FD_SET(sock, &wfds);
FD_SET(event_fd, &rfds);
FD_SET(event_fd, &wfds);
ret = select(sock > event_fd? sock + 1: event_fd + 1, &rfds, &wfds, NULL, &tv);
ret = select(sock > event_fd ? sock + 1 : event_fd + 1, &rfds, &wfds, NULL, &tv);
if (ret != 3) {
close_files(2, sock, event_fd);
THROW_ERROR("select failed");
@ -229,7 +229,7 @@ int test_epoll_with_socket() {
}
struct epoll_event events[MAXEVENTS] = {0};
if (epoll_pwait(epfd, events, MAXEVENTS, -1, NULL) <= 0){
if (epoll_pwait(epfd, events, MAXEVENTS, -1, NULL) <= 0) {
close_files(3, event_fd, sock, epfd);
THROW_ERROR("epoll failed");
}
@ -256,6 +256,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_select_with_socket),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

@ -16,7 +16,7 @@
//
// Type 1: a busy loop thread
static void* busyloop_thread_func(void* _) {
static void *busyloop_thread_func(void *_) {
while (1) {
// By calling getpid, we give the LibOS a chance to force the thread
// to terminate if exit_group is called by any thread in a thread group

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

@ -13,7 +13,7 @@
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666;
fd = open(file_path, flags, mode);
if (fd < 0) {
@ -99,7 +99,7 @@ static int __test_pwrite_pread(const char *file_path) {
}
static int __test_writev_readv(const char *file_path) {
const char* iov_msg[2] = {"hello_", "world!"};
const char *iov_msg[2] = {"hello_", "world!"};
char read_buf[128] = { 0 };
struct iovec iov[2];
int fd, len = 0;
@ -108,8 +108,8 @@ static int __test_writev_readv(const char *file_path) {
if (fd < 0) {
THROW_ERROR("failed to open a file to writev");
}
for(int i = 0; i < 2; ++i) {
iov[i].iov_base = (void*)iov_msg[i];
for (int i = 0; i < 2; ++i) {
iov[i].iov_base = (void *)iov_msg[i];
iov[i].iov_len = strlen(iov_msg[i]);
len += iov[i].iov_len;
}
@ -177,12 +177,15 @@ typedef int(*test_file_func_t)(const char *);
static int test_file_framework(test_file_func_t fn) {
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;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -30,11 +30,11 @@ static int open_file(const char *filename, int flags, int mode) {
return fd;
}
static const char* write_msg = "Hello SEFS 1234567890\n";
static const char *write_msg = "Hello SEFS 1234567890\n";
static int write_file(int fd) {
int len = strlen(write_msg);
if ((len = write(fd, write_msg, len)<= 0)) {
if ((len = write(fd, write_msg, len) <= 0)) {
PRINT_DBG("ERROR: failed to write to the file\n");
return -1;
}
@ -43,7 +43,7 @@ static int write_file(int fd) {
return 0;
}
static int read_file(int fd){
static int read_file(int fd) {
int len;
char read_buf[128] = {0};
if ((len = read(fd, read_buf, sizeof(read_buf) - 1)) <= 0) {
@ -65,14 +65,13 @@ static int read_file(int fd){
// do read or write according to do_write
// check the result of the read/write with the given expected_result
static int do_perm_tests(
const char** files,
const char **files,
size_t num_files,
int flags, int do_write,
int* expected_results)
{
int *expected_results) {
flags |= O_CREAT | O_TRUNC;
for(size_t i = 0; i < num_files; i++) {
const char* filename = files[i];
for (size_t i = 0; i < num_files; i++) {
const char *filename = files[i];
int expected_result = expected_results[i];
int fd = open_file(filename, flags, 0666);
@ -89,7 +88,7 @@ static int do_perm_tests(
// ============================================================================
// Test files
static const char* test_files[NUM_TEST_FILES] = {
static const char *test_files[NUM_TEST_FILES] = {
"/test_fs_perms.txt",
"/bin/test_fs_perms.txt",
"/lib/test_fs_perms.txt",
@ -98,7 +97,7 @@ static const char* test_files[NUM_TEST_FILES] = {
};
// Test cases X Test files -> Test Results
static int test_expected_results[NUM_TEST_CASES][NUM_TEST_FILES]= {
static int test_expected_results[NUM_TEST_CASES][NUM_TEST_FILES] = {
// test_open_ro_then_write()
{NG, NG, NG, NG, NG},
// test_open_wo_then_write()
@ -133,7 +132,7 @@ int test_open_ro_then_read() {
O_RDONLY, 0, test_expected_results[3]);
}
int test_open_wo_then_read(){
int test_open_wo_then_read() {
return do_perm_tests(test_files, NUM_TEST_FILES,
O_WRONLY, 0, test_expected_results[4]);
}
@ -156,6 +155,6 @@ test_case_t test_cases[NUM_TEST_CASES] = {
TEST_CASE(test_open_rw_then_read)
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, NUM_TEST_CASES);
}

@ -2,7 +2,7 @@
#include <unistd.h>
#include <stdio.h>
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
printf("Run a new process with pid = %d and ppid = %d\n", getpid(), getppid());
return 0;
}

@ -2,12 +2,11 @@
#include <string.h>
#include <stdio.h>
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
if (argc <= 1) {
printf("Hello World!\n");
}
else {
const char* echo_msg = argv[1];
} else {
const char *echo_msg = argv[1];
printf("%s\n", echo_msg);
}
return 0;

@ -12,7 +12,7 @@
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666;
fd = open(file_path, flags, mode);
if (fd < 0) {
@ -90,12 +90,15 @@ typedef int(*test_hostfs_func_t)(const char *);
static int test_hostfs_framework(test_hostfs_func_t fn) {
const char *file_path = "/host/hostfs_test.txt";
if (create_file(file_path) < 0)
if (create_file(file_path) < 0) {
return -1;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -14,7 +14,7 @@
typedef int(*test_case_func_t)(void);
typedef struct {
const char* name;
const char *name;
test_case_func_t func;
} test_case_t;
@ -26,9 +26,9 @@ typedef struct {
return -1; \
} while (0)
int test_suite_run(test_case_t* test_cases, int num_test_cases) {
int test_suite_run(test_case_t *test_cases, int num_test_cases) {
for (int ti = 0; ti < num_test_cases; ti++) {
test_case_t* tc = &test_cases[ti];
test_case_t *tc = &test_cases[ti];
if (tc->func() < 0) {
printf(" func %s - [ERR]\n", tc->name);
return -1;

@ -38,19 +38,19 @@ typedef struct {
sgx_quote_sign_type_t quote_type; // input
sgx_spid_t spid; // input
sgx_quote_nonce_t nonce; // input
const uint8_t* sigrl_ptr; // input (optional)
const uint8_t *sigrl_ptr; // input (optional)
uint32_t sigrl_len; // input (optional)
uint32_t quote_buf_len; // input
union {
uint8_t* as_buf;
sgx_quote_t* as_quote;
uint8_t *as_buf;
sgx_quote_t *as_quote;
} quote; // output
} sgxioc_gen_quote_arg_t;
typedef struct {
const sgx_target_info_t* target_info; // input (optinal)
const sgx_report_data_t* report_data; // input (optional)
sgx_report_t* report; // output
const sgx_target_info_t *target_info; // input (optinal)
const sgx_report_data_t *report_data; // input (optional)
sgx_report_t *report; // output
} sgxioc_create_report_arg_t;
#define SGXIOC_IS_EDMM_SUPPORTED _IOR('s', 0, int)
@ -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);
if (ret == 0) {
break;
}
else if (errno != EBUSY) {
} else if (errno != EBUSY) {
THROW_ERROR("failed to ioctl /dev/sgx");
}
@ -108,15 +107,14 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
.sigrl_ptr = NULL, // input (optional)
.sigrl_len = 0, // input (optional)
.quote_buf_len = sizeof(quote_buf), // input
.quote = { .as_buf = (uint8_t*) quote_buf } // output
.quote = { .as_buf = (uint8_t *) quote_buf } // output
};
int nretries = 0;
while (nretries < IOCTL_MAX_RETRIES) {
int ret = ioctl(sgx_fd, SGXIOC_GEN_QUOTE, &gen_quote_arg);
if (ret == 0) {
break;
}
else if (errno != EBUSY) {
} else if (errno != EBUSY) {
THROW_ERROR("failed to ioctl /dev/sgx");
}
@ -128,14 +126,15 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
THROW_ERROR("failed to ioctl /dev/sgx due to timeout");
}
sgx_quote_t* quote = (sgx_quote_t*)quote_buf;
sgx_quote_t *quote = (sgx_quote_t *)quote_buf;
if (quote->sign_type != SGX_LINKABLE_SIGNATURE) {
THROW_ERROR("invalid quote: wrong sign type");
}
if (quote->signature_len == 0) {
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");
}
return 0;
@ -174,19 +173,19 @@ static int do_SGXIOC_CREATE_AND_VERIFY_REPORT(int sgx_fd) {
sgxioc_create_report_arg_t args[] = {
{
.target_info = (const sgx_target_info_t*) &target_info,
.target_info = (const sgx_target_info_t *) &target_info,
.report_data = NULL,
.report = &report
},
{
.target_info = (const sgx_target_info_t*) &target_info,
.report_data = (const sgx_report_data_t*) &report_data,
.target_info = (const sgx_target_info_t *) &target_info,
.report_data = (const sgx_report_data_t *) &report_data,
.report = &report
}
};
for (int arg_i = 0; arg_i < ARRAY_SIZE(args); arg_i++) {
memset(&report, 0, sizeof(report));
sgxioc_create_report_arg_t* arg = &args[arg_i];
sgxioc_create_report_arg_t *arg = &args[arg_i];
if (ioctl(sgx_fd, SGXIOC_CREATE_REPORT, arg) < 0) {
THROW_ERROR("failed to create report");
}

@ -6,15 +6,15 @@
#include <stdio.h>
#include <errno.h>
int main(int argc, const char* argv[]) {
const char* file_name = "/root/test_filesystem_link.txt";
const char* link_name = "/root/link.txt";
const char* write_msg = "Hello World\n";
int main(int argc, const char *argv[]) {
const char *file_name = "/root/test_filesystem_link.txt";
const char *link_name = "/root/link.txt";
const char *write_msg = "Hello World\n";
char read_buf[128] = {0};
int ret;
// create a file and write message
int flags = O_WRONLY | O_CREAT| O_TRUNC;
int flags = O_WRONLY | O_CREAT | O_TRUNC;
int mode = 00666;
int fd = open(file_name, flags, mode);
if (fd < 0) {
@ -30,7 +30,7 @@ int main(int argc, const char* argv[]) {
// link
ret = link(file_name, link_name);
if(ret < 0) {
if (ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
@ -54,7 +54,7 @@ int main(int argc, const char* argv[]) {
// unlink
ret = unlink(link_name);
if(ret < 0) {
if (ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
@ -62,26 +62,26 @@ int main(int argc, const char* argv[]) {
// stat
struct stat stat_buf;
ret = stat(link_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
if (!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", link_name);
return -1;
}
// rename
ret = rename(file_name, link_name);
if(ret < 0) {
if (ret < 0) {
printf("ERROR: failed to rename the file");
return -1;
}
// stat
ret = stat(file_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
if (!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", file_name);
return -1;
}
ret = stat(link_name, &stat_buf);
if(ret < 0) {
if (ret < 0) {
printf("ERROR: failed to stat the file");
return -1;
}

@ -4,11 +4,11 @@
#define MAX_SIZE (1*1024*1024)
#define MIN_SIZE 8
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
printf("Testing malloc and free...\n");
for (size_t buf_size = MIN_SIZE; buf_size <= MAX_SIZE; buf_size *= 4) {
printf("buf_size = %lu\n", buf_size);
void* buf = malloc(buf_size);
void *buf = malloc(buf_size);
if (buf == NULL) {
printf("ERROR: failed to malloc for a buffer of %lu size\n", buf_size);
return -1;

@ -5,20 +5,20 @@
#include <stdio.h>
#include <errno.h>
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
const int BUF_SIZE = 20;
char buf[10];
int ret;
char* cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
char *cwd = getcwd(buf, BUF_SIZE);
if (cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
const char expect_cwd[] = "/";
if(strcmp(buf, expect_cwd)) {
if (strcmp(buf, expect_cwd)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd);
return -1;
}
@ -29,37 +29,37 @@ int main(int argc, const char* argv[]) {
const char DIR_PATH[] = "/root/test_dir";
const int DIR_MODE = 0664;
ret = mkdir(DIR_NAME, DIR_MODE);
if(ret < 0) {
if (ret < 0) {
printf("failed to mkdir \"%s\"", DIR_NAME);
return ret;
}
ret = chdir(DIR_NAME);
if(ret < 0) {
if (ret < 0) {
printf("failed to chdir to \"%s\"", DIR_NAME);
return ret;
}
cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
if (cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
if(strcmp(buf, DIR_PATH)) {
if (strcmp(buf, DIR_PATH)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH);
return -1;
}
ret = rmdir(DIR_PATH);
if(ret < 0) {
if (ret < 0) {
printf("failed to rmdir \"%s\"", DIR_PATH);
return ret;
}
struct stat stat_buf;
ret = stat(DIR_PATH, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
if (!(ret < 0 && errno == ENOENT)) {
printf("stat on \"%s\" should return ENOENT", DIR_PATH);
return ret;
}

@ -43,7 +43,7 @@ static int fill_file_with_repeated_bytes(int fd, size_t len, int byte_val) {
return 0;
}
static int check_bytes_in_buf(char* buf, size_t len, int expected_byte_val) {
static int check_bytes_in_buf(char *buf, size_t len, int expected_byte_val) {
for (size_t bi = 0; bi < len; bi++) {
if (buf[bi] != (char)expected_byte_val) {
printf("check_bytes_in_buf: expect %02X, but found %02X, at offset %lu\n",
@ -54,9 +54,9 @@ static int check_bytes_in_buf(char* buf, size_t len, int expected_byte_val) {
return 0;
}
static void* get_a_stack_ptr() {
static void *get_a_stack_ptr() {
volatile int a = 0;
return (void*) &a;
return (void *) &a;
}
// ============================================================================
@ -68,7 +68,7 @@ static int get_a_valid_range_of_hints(size_t *hint_begin, size_t *hint_end) {
size_t big_buf_len = MAX_MMAP_USED_MEMORY;
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* big_buf = mmap(NULL, big_buf_len, prot, flags, -1, 0);
void *big_buf = mmap(NULL, big_buf_len, prot, flags, -1, 0);
if (big_buf == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
@ -99,7 +99,7 @@ int test_anonymous_mmap() {
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
for (size_t len = PAGE_SIZE; len <= MAX_MMAP_USED_MEMORY; len *= 2) {
void* buf = mmap(NULL, len, prot, flags, -1, 0);
void *buf = mmap(NULL, len, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
@ -120,7 +120,7 @@ int test_anonymous_mmap_randomly() {
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* bufs[16] = {NULL};
void *bufs[16] = {NULL};
size_t lens[16];
size_t num_bufs = 0;
size_t used_memory = 0;
@ -129,14 +129,13 @@ int test_anonymous_mmap_randomly() {
// Phrase 1: do mmap with random sizes until no more buffers or memory
for (num_bufs = 0;
num_bufs < ARRAY_SIZE(bufs) && used_memory < MAX_MMAP_USED_MEMORY;
num_bufs++)
{
num_bufs++) {
// Choose the mmap size randomly
size_t len = rand() % (MAX_MMAP_USED_MEMORY - used_memory) + 1;
len = ALIGN_UP(len, PAGE_SIZE);
// Do mmap
void* buf = mmap(NULL, len, prot, flags, -1, 0);
void *buf = mmap(NULL, len, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
@ -149,7 +148,7 @@ int test_anonymous_mmap_randomly() {
// Phrase 2: do munmap to free all memory mapped memory
for (int bi = 0; bi < num_bufs; bi++) {
void* buf = bufs[bi];
void *buf = bufs[bi];
size_t len = lens[bi];
int ret = munmap(buf, len);
if (ret < 0) {
@ -175,10 +174,10 @@ int test_anonymous_mmap_randomly_with_good_hints() {
hint = ALIGN_DOWN(hint, PAGE_SIZE);
size_t len = rand() % (HINT_END - (size_t)hint);
len = ALIGN_UP(len+1, PAGE_SIZE);
len = ALIGN_UP(len + 1, PAGE_SIZE);
void* addr = mmap((void*)hint, len, prot, flags, -1, 0);
if (addr != (void*)hint) {
void *addr = mmap((void *)hint, len, prot, flags, -1, 0);
if (addr != (void *)hint) {
THROW_ERROR("mmap with hint failed");
}
@ -201,8 +200,8 @@ int test_anonymous_mmap_with_bad_hints() {
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
for (int hi = 0; hi < ARRAY_SIZE(bad_hints); hi++) {
void* bad_hint = (void*)bad_hints[hi];
void* addr = mmap(bad_hint, len, prot, flags, -1, 0);
void *bad_hint = (void *)bad_hints[hi];
void *addr = mmap(bad_hint, len, prot, flags, -1, 0);
if (addr == MAP_FAILED) {
THROW_ERROR("mmap should have tolerated a bad hint");
}
@ -221,7 +220,7 @@ int test_anonymous_mmap_with_zero_len() {
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
int len = 0; // invalid!
void* buf = mmap(NULL, len, prot, flags, -1, 0);
void *buf = mmap(NULL, len, prot, flags, -1, 0);
if (buf != MAP_FAILED) {
THROW_ERROR("mmap with zero len should have been failed");
}
@ -232,7 +231,7 @@ int test_anonymous_mmap_with_non_page_aligned_len() {
int len = PAGE_SIZE + 17; // length need not to be page aligned!
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* buf = mmap(NULL, len, prot, flags, -1, 0);
void *buf = mmap(NULL, len, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
THROW_ERROR("mmap with non-page aligned len should have worked");
}
@ -254,7 +253,7 @@ int test_anonymous_mmap_with_non_page_aligned_len() {
// ============================================================================
int test_file_mmap() {
const char* file_path = "/root/mmap_file.data";
const char *file_path = "/root/mmap_file.data";
int fd = open(file_path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd < 0) {
THROW_ERROR("file creation failed");
@ -272,7 +271,7 @@ int test_file_mmap() {
}
off_t offset = 0;
for (size_t len = PAGE_SIZE; len <= file_len; len *= 2) {
char* buf = mmap(NULL, len, prot, flags, fd, offset);
char *buf = mmap(NULL, len, prot, flags, fd, offset);
if (buf == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
@ -292,7 +291,7 @@ int test_file_mmap() {
}
int test_file_mmap_with_offset() {
const char* file_path = "/root/mmap_file.data";
const char *file_path = "/root/mmap_file.data";
int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (fd < 0) {
THROW_ERROR("file creation failed");
@ -310,12 +309,12 @@ int test_file_mmap_with_offset() {
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE;
assert(offset <= first_len);
char* buf = mmap(NULL, len, prot, flags, fd, offset);
char *buf = mmap(NULL, len, prot, flags, fd, offset);
if (buf == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
char* buf_cursor = buf;
char *buf_cursor = buf;
if (check_bytes_in_buf(buf_cursor, first_len - offset, first_val) < 0) {
THROW_ERROR("the buffer is not initialized according to the file");
}
@ -324,7 +323,8 @@ int test_file_mmap_with_offset() {
THROW_ERROR("the buffer is not initialized according to the file");
}
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");
}
@ -344,7 +344,7 @@ int test_file_mmap_with_invalid_fd() {
int flags = MAP_PRIVATE;
int fd = 1234; // invalid!
off_t offset = 0;
void* buf = mmap(NULL, len, prot, flags, fd, offset);
void *buf = mmap(NULL, len, prot, flags, fd, offset);
if (buf != MAP_FAILED) {
THROW_ERROR("file mmap with an invalid fd should have been failed");
}
@ -352,7 +352,7 @@ int test_file_mmap_with_invalid_fd() {
}
int test_file_mmap_with_non_page_aligned_offset() {
const char* file_path = "/root/mmap_file.data";
const char *file_path = "/root/mmap_file.data";
int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (fd < 0) {
THROW_ERROR("file creation failed");
@ -365,7 +365,7 @@ int test_file_mmap_with_non_page_aligned_offset() {
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
off_t offset = PAGE_SIZE + 127; // Invalid!
void* buf = mmap(NULL, len, prot, flags, fd, offset);
void *buf = mmap(NULL, len, prot, flags, fd, offset);
if (buf != MAP_FAILED) {
THROW_ERROR("mmap with non-page aligned len should have been failed");
}
@ -388,8 +388,8 @@ int test_fixed_mmap_that_does_not_override_any_mmaping() {
len = ALIGN_UP(len, PAGE_SIZE);
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
void* addr = mmap((void*)hint, len, prot, flags, -1, 0);
if (addr != (void*)hint) {
void *addr = mmap((void *)hint, len, prot, flags, -1, 0);
if (addr != (void *)hint) {
THROW_ERROR("mmap with fixed address failed");
}
@ -412,7 +412,7 @@ int test_fixed_mmap_that_overrides_existing_mmaping() {
// Allocate parent_buf
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* parent_buf = mmap(NULL, parent_len, prot, flags, -1, 0);
void *parent_buf = mmap(NULL, parent_len, prot, flags, -1, 0);
if (parent_buf == MAP_FAILED) {
THROW_ERROR("mmap for parent failed");
}
@ -420,7 +420,7 @@ int test_fixed_mmap_that_overrides_existing_mmaping() {
memset(parent_buf, parent_val, parent_len);
// Allocate child_buf
void* child_buf = (char*)parent_buf + pre_child_len;
void *child_buf = (char *)parent_buf + pre_child_len;
if (mmap(child_buf, child_len, prot, flags | MAP_FIXED, -1, 0) != child_buf) {
THROW_ERROR("mmap with fixed address failed");
}
@ -430,9 +430,9 @@ int test_fixed_mmap_that_overrides_existing_mmaping() {
THROW_ERROR("the content of child mmap memory is not initialized");
}
// Check that the rest of parent_buf are kept intact
if (check_bytes_in_buf((char*)child_buf - pre_child_len,
if (check_bytes_in_buf((char *)child_buf - pre_child_len,
pre_child_len, parent_val) < 0 ||
check_bytes_in_buf((char*)child_buf + child_len,
check_bytes_in_buf((char *)child_buf + child_len,
post_child_len, parent_val) < 0) {
THROW_ERROR("the content of parent mmap memory is broken");
}
@ -451,7 +451,7 @@ int test_fixed_mmap_with_non_page_aligned_addr() {
size_t len = 1 * PAGE_SIZE;
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
void* addr = mmap((void*)hint, len, prot, flags, -1, 0);
void *addr = mmap((void *)hint, len, prot, flags, -1, 0);
if (addr != MAP_FAILED) {
THROW_ERROR("fixed mmap with non-page aligned hint should have failed");
}
@ -462,15 +462,15 @@ int test_fixed_mmap_with_non_page_aligned_addr() {
// Test cases for munmap
// ============================================================================
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
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
// a hint for mmap.
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* real_addr = mmap(target_addr, len, prot, flags, -1, 0);
void *real_addr = mmap(target_addr, len, prot, flags, -1, 0);
if (real_addr != target_addr) {
THROW_ERROR("address is already mmaped");
}
@ -482,12 +482,12 @@ static int mmap_then_munmap(size_t mmap_len, ssize_t munmap_offset, size_t munma
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
// Make sure that we are manipulating memory between [HINT_BEGIN, HINT_END)
void* mmap_addr = (void*)(munmap_offset >= 0 ? HINT_BEGIN : HINT_BEGIN - munmap_offset);
void *mmap_addr = (void *)(munmap_offset >= 0 ? HINT_BEGIN : HINT_BEGIN - munmap_offset);
if (mmap(mmap_addr, mmap_len, prot, flags, -1, 0) != mmap_addr) {
THROW_ERROR("mmap failed");
}
void* munmap_addr = (char*)mmap_addr + munmap_offset;
void *munmap_addr = (char *)mmap_addr + munmap_offset;
if (munmap(munmap_addr, munmap_len) < 0) {
THROW_ERROR("munmap failed");
}
@ -497,7 +497,7 @@ static int mmap_then_munmap(size_t mmap_len, ssize_t munmap_offset, size_t munma
// Make sure that when this function returns, there are no memory mappings
// within [HINT_BEGIN, HINT_END)
if (munmap((void*)HINT_BEGIN, HINT_END - HINT_BEGIN) < 0) {
if (munmap((void *)HINT_BEGIN, HINT_END - HINT_BEGIN) < 0) {
THROW_ERROR("munmap failed");
}
return 0;
@ -548,13 +548,13 @@ int test_munmap_whose_range_intersects_with_multiple_mmap_regions() {
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
size_t mmap_len1 = 100 * PAGE_SIZE;
void* mmap_addr1 = mmap(NULL, mmap_len1, prot, flags, -1, 0);
void *mmap_addr1 = mmap(NULL, mmap_len1, prot, flags, -1, 0);
if (mmap_addr1 == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
size_t mmap_len2 = 12 * PAGE_SIZE;
void* mmap_addr2 = mmap(NULL, mmap_len2, prot, flags, -1, 0);
void *mmap_addr2 = mmap(NULL, mmap_len2, prot, flags, -1, 0);
if (mmap_addr2 == MAP_FAILED) {
THROW_ERROR("mmap failed");
}
@ -563,7 +563,7 @@ int test_munmap_whose_range_intersects_with_multiple_mmap_regions() {
size_t mmap_max = MAX((size_t)mmap_addr1 + mmap_len1,
(size_t)mmap_addr2 + mmap_len2);
void* munmap_addr = (void*)mmap_min;
void *munmap_addr = (void *)mmap_min;
size_t munmap_len = mmap_max - mmap_min;
if (munmap(munmap_addr, munmap_len) < 0) {
THROW_ERROR("munmap failed");
@ -581,7 +581,7 @@ int test_munmap_with_null_addr() {
// The man page of munmap states that "it is not an error if the indicated
// range does not contain any mapped pages". This is not considered as
// an error!
void* munmap_addr = NULL;
void *munmap_addr = NULL;
size_t munmap_len = PAGE_SIZE;
if (munmap(munmap_addr, munmap_len) < 0) {
THROW_ERROR("munmap failed");
@ -590,7 +590,7 @@ int test_munmap_with_null_addr() {
}
int test_munmap_with_zero_len() {
void* munmap_addr = (void*)HINT_BEGIN;
void *munmap_addr = (void *)HINT_BEGIN;
// Set the length for munmap to 0! This is invalid!
size_t munmap_len = 0;
if (munmap(munmap_addr, munmap_len) == 0) {

@ -74,13 +74,15 @@ typedef int(*test_open_func_t)(const char *, int, int);
static int test_open_framework(test_open_func_t fn) {
const char *file_path = "/root/test_filesystem_open.txt";
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666;
if (fn(file_path, flags, mode) < 0)
if (fn(file_path, flags, mode) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -83,17 +83,17 @@ int test_read_write() {
posix_spawn_file_actions_adddup2(&file_actions, pipe_wr_fd, STDOUT_FILENO);
posix_spawn_file_actions_addclose(&file_actions, pipe_rd_fd);
const char* msg = "Echo!\n";
const char* child_prog = "/bin/hello_world";
const char* child_argv[3] = { child_prog, msg, NULL };
const char *msg = "Echo!\n";
const char *child_prog = "/bin/hello_world";
const char *child_argv[3] = { child_prog, msg, NULL };
int child_pid;
if (posix_spawn(&child_pid, child_prog, &file_actions,
NULL, (char*const *)child_argv, NULL) < 0) {
NULL, (char *const *)child_argv, NULL) < 0) {
THROW_ERROR("failed to spawn a child process");
}
close(pipe_wr_fd);
const char* expected_str = msg;
const char *expected_str = msg;
size_t expected_len = strlen(expected_str);
char actual_str[32] = {0};
ssize_t actual_len;
@ -123,6 +123,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_read_write),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

@ -16,7 +16,7 @@
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
// Create pipe
int pipe_fds[2];
if (pipe(pipe_fds) < 0) {

@ -21,12 +21,12 @@
struct thread_arg {
int ti;
long local_count;
volatile unsigned long* global_count;
pthread_mutex_t* mutex;
volatile unsigned long *global_count;
pthread_mutex_t *mutex;
};
static void* thread_func(void* _arg) {
struct thread_arg* arg = _arg;
static void *thread_func(void *_arg) {
struct thread_arg *arg = _arg;
for (long i = 0; i < arg->local_count; i++) {
pthread_mutex_lock(arg->mutex);
(*arg->global_count)++;
@ -51,7 +51,7 @@ static int test_mutex_with_concurrent_counter(void) {
* Start the threads
*/
for (int ti = 0; ti < NTHREADS; ti++) {
struct thread_arg* thread_arg = &thread_args[ti];
struct thread_arg *thread_arg = &thread_args[ti];
thread_arg->ti = ti;
thread_arg->local_count = LOCAL_COUNT;
thread_arg->global_count = &global_count;
@ -92,14 +92,14 @@ static int test_mutex_with_concurrent_counter(void) {
struct thread_cond_arg {
int ti;
volatile unsigned int* val;
volatile int* exit_thread_count;
pthread_cond_t* cond_val;
pthread_mutex_t* mutex;
volatile unsigned int *val;
volatile int *exit_thread_count;
pthread_cond_t *cond_val;
pthread_mutex_t *mutex;
};
static void* thread_cond_wait(void* _arg) {
struct thread_cond_arg* arg = _arg;
static void *thread_cond_wait(void *_arg) {
struct thread_cond_arg *arg = _arg;
printf("Thread #%d: start to wait on condition variable.\n", arg->ti);
for (unsigned int i = 0; i < WAIT_ROUND; ++i) {
pthread_mutex_lock(arg->mutex);
@ -124,7 +124,7 @@ static int test_mutex_with_cond_wait(void) {
* Start the threads waiting on the condition variable
*/
for (int ti = 0; ti < NTHREADS; ti++) {
struct thread_cond_arg* thread_arg = &thread_args[ti];
struct thread_cond_arg *thread_arg = &thread_args[ti];
thread_arg->ti = ti;
thread_arg->val = &val;
thread_arg->exit_thread_count = &exit_thread_count;

@ -14,7 +14,7 @@
static int test_readdir() {
struct dirent *dp;
DIR* dirp;
DIR *dirp;
dirp = opendir("/");
if (dirp == NULL) {

@ -1,7 +1,7 @@
#include <sys/resource.h>
#include <stdio.h>
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
struct rlimit rlim;
if (getrlimit(RLIMIT_AS, &rlim) < 0) {
printf("ERROR: getrlimit failed\n");

@ -17,14 +17,14 @@
#define MAX_CPU_NUM 1024
static int* g_online_cpu_idxs;
static int *g_online_cpu_idxs;
int get_online_cpu() {
int online_num = sysconf(_SC_NPROCESSORS_ONLN);
cpu_set_t mask;
int index = 0;
g_online_cpu_idxs = (int*)calloc(online_num, sizeof(int));
g_online_cpu_idxs = (int *)calloc(online_num, sizeof(int));
CPU_ZERO(&mask);
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) < 0) {
THROW_ERROR("failed to call sched_getaffinity");
@ -94,7 +94,7 @@ static int test_sched_xetaffinity_with_child_pid() {
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(g_online_cpu_idxs[num - 1] , &mask);
CPU_SET(g_online_cpu_idxs[num - 1], &mask);
int ret = posix_spawn(&child_pid, "/bin/getpid", NULL, NULL, NULL, NULL);
if (ret < 0 ) {
THROW_ERROR("spawn process error");
@ -177,7 +177,7 @@ static int test_sched_setaffinity_with_zero_cpusetsize() {
static int test_sched_getaffinity_with_null_buffer() {
unsigned char *buf = NULL;
if (sched_getaffinity(0, sizeof(cpu_set_t), (cpu_set_t*)buf) != -1) {
if (sched_getaffinity(0, sizeof(cpu_set_t), (cpu_set_t *)buf) != -1) {
THROW_ERROR("check invalid buffer pointer(NULL) fail");
}
return 0;
@ -185,7 +185,7 @@ static int test_sched_getaffinity_with_null_buffer() {
static int test_sched_setaffinity_with_null_buffer() {
unsigned char *buf = NULL;
if (sched_setaffinity(0, sizeof(cpu_set_t), (cpu_set_t*)buf) != -1) {
if (sched_setaffinity(0, sizeof(cpu_set_t), (cpu_set_t *)buf) != -1) {
THROW_ERROR("check invalid buffer pointer(NULL) fail");
}
return 0;

@ -21,11 +21,13 @@
int connect_with_child(int port, int *child_pid) {
int ret = 0;
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0)
if (listen_fd < 0) {
THROW_ERROR("create socket error");
}
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");
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
@ -46,7 +48,7 @@ int connect_with_child(int port, int *child_pid) {
char port_string[8];
sprintf(port_string, "%d", port);
char* client_argv[] = {"client", "127.0.0.1", port_string, NULL};
char *client_argv[] = {"client", "127.0.0.1", port_string, NULL};
ret = posix_spawn(child_pid, "/bin/client", NULL, NULL, client_argv, NULL);
if (ret < 0) {
close(listen_fd);
@ -65,11 +67,13 @@ int connect_with_child(int port, int *child_pid) {
int neogotiate_msg(int client_fd) {
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");
}
if (read(client_fd, buf, sizeof(RESPONSE)) < 0)
if (read(client_fd, buf, sizeof(RESPONSE)) < 0) {
THROW_ERROR("read failed");
}
if (strncmp(buf, RESPONSE, sizeof(RESPONSE)) != 0) {
THROW_ERROR("msg recv mismatch");
@ -81,8 +85,9 @@ int server_recv(int client_fd) {
const int buf_size = 32;
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");
}
if (strncmp(buf, ECHO_MSG, strlen(ECHO_MSG)) != 0) {
THROW_ERROR("msg recv mismatch");
@ -125,8 +130,9 @@ int server_recvmsg(int client_fd) {
msg.msg_iov = NULL;
msg.msg_iovlen = 0;
ret = recvmsg(client_fd, &msg, 0);
if (ret != 0)
if (ret != 0) {
THROW_ERROR("recvmsg empty failed");
}
return ret;
}
@ -143,8 +149,9 @@ int server_connectionless_recvmsg() {
memset(&clientaddr, 0, sizeof(clientaddr));
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
if (sock < 0) {
THROW_ERROR("create socket error");
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
@ -175,7 +182,7 @@ int server_connectionless_recvmsg() {
} else {
inet_ntop(AF_INET, &clientaddr.sin_addr,
buf, sizeof(buf));
if(strcmp(buf, "127.0.0.1") !=0) {
if (strcmp(buf, "127.0.0.1") != 0) {
printf("from port %d and address %s\n", ntohs(clientaddr.sin_port), buf);
THROW_ERROR("client addr mismatch");
}
@ -196,10 +203,11 @@ int test_read_write() {
int ret = 0;
int child_pid = 0;
int client_fd = connect_with_child(8800, &child_pid);
if (client_fd < 0)
if (client_fd < 0) {
THROW_ERROR("connect failed");
else
} else {
ret = neogotiate_msg(client_fd);
}
//wait for the child to exit for next spawn
int status = 0;
@ -214,14 +222,16 @@ int test_send_recv() {
int ret = 0;
int child_pid = 0;
int client_fd = connect_with_child(8801, &child_pid);
if (client_fd < 0)
if (client_fd < 0) {
THROW_ERROR("connect failed");
}
if (neogotiate_msg(client_fd) < 0)
if (neogotiate_msg(client_fd) < 0) {
THROW_ERROR("neogotiate failed");
}
ret = server_recv(client_fd);
if (ret < 0) return -1;
if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid);
@ -232,14 +242,16 @@ int test_sendmsg_recvmsg() {
int ret = 0;
int child_pid = 0;
int client_fd = connect_with_child(8802, &child_pid);
if (client_fd < 0)
if (client_fd < 0) {
THROW_ERROR("connect failed");
}
if (neogotiate_msg(client_fd) < 0)
if (neogotiate_msg(client_fd) < 0) {
THROW_ERROR("neogotiate failed");
}
ret = server_recvmsg(client_fd);
if (ret < 0) return -1;
if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid);
@ -250,14 +262,14 @@ int test_sendmsg_recvmsg_connectionless() {
int ret = 0;
int child_pid = 0;
char* client_argv[] = {"client", "NULL", "8803", NULL};
char *client_argv[] = {"client", "NULL", "8803", NULL};
ret = posix_spawn(&child_pid, "/bin/client", NULL, NULL, client_argv, NULL);
if (ret < 0) {
THROW_ERROR("spawn client process error");
}
ret = server_connectionless_recvmsg();
if (ret < 0) return -1;
if (ret < 0) { return -1; }
ret = wait_for_child_exit(child_pid);
@ -271,19 +283,23 @@ int test_fcntl_setfl_and_getfl() {
int original_flags, actual_flags;
client_fd = connect_with_child(8804, &child_pid);
if (client_fd < 0)
if (client_fd < 0) {
THROW_ERROR("connect failed");
}
original_flags = fcntl(client_fd, F_GETFL, 0);
if (original_flags < 0)
if (original_flags < 0) {
THROW_ERROR("fcntl getfl failed");
}
ret = fcntl(client_fd, F_SETFL, original_flags | O_NONBLOCK);
if (ret < 0)
if (ret < 0) {
THROW_ERROR("fcntl setfl failed");
}
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");
}
ret = wait_for_child_exit(child_pid);
@ -300,14 +316,16 @@ int test_poll_sockets() {
};
ret = poll(pollfds, 2, 0);
if (ret < 0)
if (ret < 0) {
THROW_ERROR("poll error");
}
if (pollfds[0].fd != socks[0] ||
pollfds[0].events != POLLIN ||
pollfds[1].fd != socks[1] ||
pollfds[1].events != POLLIN)
pollfds[1].events != POLLIN) {
THROW_ERROR("fd and events of pollfd should remain unchanged");
}
return 0;
}
@ -320,6 +338,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_poll_sockets),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

@ -30,8 +30,9 @@ static int create_and_bind() {
servaddr.sin_port = htons(6667);
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");
}
int ret = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) {
@ -67,8 +68,8 @@ int test_ip_socket() {
int client_pid;
int proc_num = DEFAULT_PROC_NUM;
char* client_argv[] = {"client", "127.0.0.1", "6667", NULL};
for(int i=0; i<DEFAULT_PROC_NUM; ++i) {
char *client_argv[] = {"client", "127.0.0.1", "6667", NULL};
for (int i = 0; i < DEFAULT_PROC_NUM; ++i) {
int ret = posix_spawn(&client_pid, "/bin/client", NULL, NULL, client_argv, NULL);
if (ret < 0) {
if (i == 0) {
@ -124,7 +125,7 @@ int test_ip_socket() {
// Channel is ready to read.
char buf[36];
if ((read(events[i].data.fd, buf, sizeof buf)) != 0) {
if(strcmp(buf, DEFAULT_MSG) != 0) {
if (strcmp(buf, DEFAULT_MSG) != 0) {
close_files(2, server_fd, epfd);
THROW_ERROR("msg mismatched");
}
@ -159,6 +160,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_ip_socket),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

@ -99,13 +99,14 @@ int test_sigprocmask() {
#define MAX_RECURSION_LEVEL 3
static void handle_sigio(int num, siginfo_t* info, void* context) {
static void handle_sigio(int num, siginfo_t *info, void *context) {
static volatile int recursion_level = 0;
printf("Hello from SIGIO signal handler (recursion_level = %d)!\n", recursion_level);
recursion_level++;
if (recursion_level <= MAX_RECURSION_LEVEL)
if (recursion_level <= MAX_RECURSION_LEVEL) {
raise(SIGIO);
}
recursion_level--;
}
@ -134,7 +135,7 @@ int test_raise() {
int test_abort() {
pid_t child_pid;
char* child_argv[] = {"signal", "aborted_child", NULL};
char *child_argv[] = {"signal", "aborted_child", NULL};
int ret;
int status;
@ -170,7 +171,7 @@ static int aborted_child() {
int test_kill() {
pid_t child_pid;
char* child_argv[] = {"signal", "killed_child", NULL};
char *child_argv[] = {"signal", "killed_child", NULL};
int ret;
int status;
@ -207,13 +208,13 @@ static int killed_child() {
// Test catching and handling hardware exception
// ============================================================================
static void handle_sigfpe(int num, siginfo_t* info, void* _context) {
static void handle_sigfpe(int num, siginfo_t *info, void *_context) {
printf("SIGFPE Caught\n");
assert(num == SIGFPE);
assert(info->si_signo == SIGFPE);
ucontext_t* ucontext = _context;
mcontext_t* mcontext = &ucontext->uc_mcontext;
ucontext_t *ucontext = _context;
mcontext_t *mcontext = &ucontext->uc_mcontext;
// The faulty instruction should be `idiv %esi` (f7 fe)
mcontext->gregs[REG_RIP] += 2;
@ -268,7 +269,7 @@ int test_catch_fault() {
stack_t g_old_ss;
static void handle_sigpipe(int num, siginfo_t* info, void* context) {
static void handle_sigpipe(int num, siginfo_t *info, void *context) {
static volatile int recursion_level = 0;
printf("Hello from SIGPIPE signal handler on the alternate signal stack (recursion_level = %d)\n",
recursion_level);
@ -279,8 +280,9 @@ static void handle_sigpipe(int num, siginfo_t* info, void* context) {
g_old_ss = old_ss;
recursion_level++;
if (recursion_level <= MAX_ALTSTACK_RECURSION_LEVEL)
if (recursion_level <= MAX_ALTSTACK_RECURSION_LEVEL) {
raise(SIGPIPE);
}
recursion_level--;
}
@ -341,14 +343,14 @@ int test_sigchld() {
printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid());
ret = posix_spawn(&child_pid, "/bin/getpid", NULL, NULL, NULL, NULL);
if (ret < 0){
if (ret < 0) {
printf("ERROR: failed to spawn a child process\n");
return -1;
}
printf("Spawn a new proces successfully (pid = %d)\n", child_pid);
wait(NULL);
if (sigchld == 0) THROW_ERROR("Did not receive SIGCHLD");
if (sigchld == 0) { THROW_ERROR("Did not receive SIGCHLD"); }
return 0;
}
@ -367,16 +369,14 @@ static test_case_t test_cases[] = {
TEST_CASE(test_sigchld),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
if (argc > 1) {
const char* cmd = argv[1];
const char *cmd = argv[1];
if (strcmp(cmd, "aborted_child") == 0) {
return aborted_child();
}
else if (strcmp(cmd, "killed_child") == 0) {
} else if (strcmp(cmd, "killed_child") == 0) {
return killed_child();
}
else {
} else {
fprintf(stderr, "ERROR: unknown command: %s\n", cmd);
return EXIT_FAILURE;
}

@ -17,14 +17,13 @@
// Helper functions
// ============================================================================
static inline void validate_timespec(const struct timespec* tv) {
static inline void validate_timespec(const struct timespec *tv) {
assert(tv->tv_sec >= 0 && tv->tv_nsec >= 0 && tv->tv_nsec < S);
}
// 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(b);
@ -40,16 +39,17 @@ static int timespec_cmp(const struct timespec *a, const struct timespec *b)
// diff = | a - b |
static void timespec_diff(const struct timespec *a, const struct timespec *b,
struct timespec *diff)
{
struct timespec *diff) {
validate_timespec(a);
validate_timespec(b);
const struct timespec *begin, *end;
if (timespec_cmp(a, b) <= 0) {
begin = a; end = b;
begin = a;
end = b;
} else {
begin = b; end = a;
begin = b;
end = a;
}
diff->tv_nsec = end->tv_nsec - begin->tv_nsec;
@ -64,15 +64,14 @@ static void timespec_diff(const struct timespec *a, const struct timespec *b,
// retval = | a - b | <= precision
static int timespec_equal(const struct timespec *a, const struct timespec *b,
const struct timespec *precision)
{
const struct timespec *precision) {
struct timespec diff;
timespec_diff(a, b, &diff);
return timespec_cmp(&diff, precision) <= 0;
}
static int check_nanosleep(struct timespec* expected_sleep_period) {
static int check_nanosleep(struct timespec *expected_sleep_period) {
// The time obtained from Occlum is not very precise.
// Here we take 1 millisecond as the time precision of Occlum.
static struct timespec OS_TIME_PRECISION = {

@ -4,7 +4,7 @@
#include <stdio.h>
#include <spawn.h>
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
int ret, child_pid, status;
printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid());

@ -7,13 +7,13 @@
#define NREPEATS 5000
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
for (unsigned long i = 0; i < NREPEATS; i++) {
int child_pid, status;
if (posix_spawn(&child_pid, "/bin/empty", NULL, NULL, NULL, NULL) <0) {
if (posix_spawn(&child_pid, "/bin/empty", NULL, NULL, NULL, NULL) < 0) {
printf("ERROR: failed to spawn (# of repeats = %lu)\n", i);
return -1;
}

@ -12,7 +12,7 @@
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666;
fd = open(file_path, flags, mode);
@ -145,12 +145,15 @@ typedef int(*test_stat_func_t)(const char *);
static int test_stat_framework(test_stat_func_t fn) {
const char *file_path = "/root/test_filesystem_stat.txt";
if (create_file(file_path) < 0)
if (create_file(file_path) < 0) {
return -1;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -28,7 +28,7 @@ static ssize_t get_path_by_fd(int fd, char *buf, ssize_t buf_len) {
static int create_file(const char *file_path) {
int fd;
int flags = O_RDONLY | O_CREAT| O_TRUNC;
int flags = O_RDONLY | O_CREAT | O_TRUNC;
int mode = 00666;
fd = open(file_path, flags, mode);
@ -115,12 +115,15 @@ typedef int(*test_readlink_func_t)(const char *);
static int test_readlink_framework(test_readlink_func_t fn) {
const char *file_path = "/root/test_filesystem_symlink.txt";
if (create_file(file_path) < 0)
if (create_file(file_path) < 0) {
return -1;
if (fn(file_path) < 0)
}
if (fn(file_path) < 0) {
return -1;
if (remove_file(file_path) < 0)
}
if (remove_file(file_path) < 0) {
return -1;
}
return 0;
}

@ -1,11 +1,11 @@
volatile int g_int = 0;
static void use_int(int* a) {
static void use_int(int *a) {
g_int += *a;
}
__thread int tls_g_int = 0;
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
use_int(&tls_g_int);
return g_int;
}

@ -5,15 +5,15 @@
#include <string.h>
#include <stdio.h>
int main(int argc, const char* argv[]) {
const char* FILE_NAME = "root/test_filesystem_truncate.txt";
int main(int argc, const char *argv[]) {
const char *FILE_NAME = "root/test_filesystem_truncate.txt";
const int TRUNC_LEN = 256;
const int TRUNC_LEN1 = 128;
const int MODE_MASK = 0777;
int ret;
int flags = O_WRONLY | O_CREAT| O_TRUNC;
int flags = O_WRONLY | O_CREAT | O_TRUNC;
int mode = 00666;
int fd = open(FILE_NAME, flags, mode);
if (fd < 0) {

@ -4,11 +4,11 @@
int main(void) {
struct utsname name;
uname(&name);
printf("sysname = %s\n", (const char*)&name.sysname);
printf("nodename = %s\n", (const char*)&name.nodename);
printf("release = %s\n", (const char*)&name.release);
printf("version = %s\n", (const char*)&name.version);
printf("machine = %s\n", (const char*)&name.machine);
printf("domainname = %s\n", (const char*)&name.__domainname);
printf("sysname = %s\n", (const char *)&name.sysname);
printf("nodename = %s\n", (const char *)&name.nodename);
printf("release = %s\n", (const char *)&name.release);
printf("version = %s\n", (const char *)&name.version);
printf("machine = %s\n", (const char *)&name.machine);
printf("domainname = %s\n", (const char *)&name.__domainname);
return 0;
}

@ -64,8 +64,8 @@ int create_connceted_sockets_default(int *sockets) {
}
int verify_child_echo(int *connected_sockets) {
const char* child_prog = "/bin/hello_world";
const char* child_argv[3] = { child_prog, ECHO_MSG, NULL };
const char *child_prog = "/bin/hello_world";
const char *child_argv[3] = { child_prog, ECHO_MSG, NULL };
int child_pid;
posix_spawn_file_actions_t file_actions;
@ -74,7 +74,7 @@ int verify_child_echo(int *connected_sockets) {
posix_spawn_file_actions_addclose(&file_actions, connected_sockets[1]);
if (posix_spawn(&child_pid, child_prog, &file_actions,
NULL, (char*const*)child_argv, NULL) < 0) {
NULL, (char *const *)child_argv, NULL) < 0) {
THROW_ERROR("failed to spawn a child process");
}
@ -124,24 +124,24 @@ int test_multiple_socketpairs() {
int i;
int ret = 0;
for(i = 0; i < PAIR_NUM; i++) {
for (i = 0; i < PAIR_NUM; i++) {
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets[i]) < 0) {
THROW_ERROR("opening stream socket pair");
}
if(verify_connection(sockets[i][0], sockets[i][1]) < 0) {
if (verify_connection(sockets[i][0], sockets[i][1]) < 0) {
ret = -1;
goto cleanup;
}
if(verify_connection(sockets[i][1], sockets[i][0]) < 0) {
if (verify_connection(sockets[i][1], sockets[i][0]) < 0) {
ret = -1;
goto cleanup;
}
}
i--;
cleanup:
for(; i >= 0; i--){
for (; i >= 0; i--) {
close(sockets[i][0]);
close(sockets[i][1]);
}
@ -156,8 +156,9 @@ typedef int(*create_connection_func_t)(int *);
int test_connected_sockets_inter_process(create_connection_func_t fn) {
int ret = 0;
int sockets[2];
if (fn(sockets) < 0)
if (fn(sockets) < 0) {
return -1;
}
ret = verify_child_echo(sockets);
@ -180,6 +181,6 @@ static test_case_t test_cases[] = {
TEST_CASE(test_multiple_socketpairs),
};
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
}

@ -64,7 +64,7 @@ int create_client_socket() {
return fd;
}
int main(int argc, const char* argv[]) {
int main(int argc, const char *argv[]) {
size_t buf_size, total_bytes;
if (argc >= 2) {
buf_size = atol(argv[1]);
@ -75,7 +75,7 @@ int main(int argc, const char* argv[]) {
total_bytes = atol(argv[2]);
} else {
// BUG: throughput fall down when buf_size > 65536
total_bytes = buf_size > 65536? buf_size << 15: buf_size << 21;
total_bytes = buf_size > 65536 ? buf_size << 15 : buf_size << 21;
}
printf("buf_size = 0x%zx\n", buf_size);
printf("total_bytes = 0x%zx\n", total_bytes);
@ -109,8 +109,8 @@ int main(int argc, const char* argv[]) {
posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd);
int child_pid;
extern char ** environ;
char* new_argv[] = {"/bin/data_sink", NULL};
extern char **environ;
char *new_argv[] = {"/bin/data_sink", NULL};
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
NULL, new_argv, environ) < 0) {
printf("ERROR: failed to spawn a child process\n");

@ -113,25 +113,26 @@ static sgx_errlist_t sgx_errlist[] = {
};
/* 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 ttl = sizeof sgx_errlist/sizeof sgx_errlist[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].sug)
if (ret == sgx_errlist[idx].err) {
if (NULL != sgx_errlist[idx].sug) {
printf("Info: %s\n", sgx_errlist[idx].sug);
}
printf("Error: %s\n", sgx_errlist[idx].msg);
break;
}
}
if (idx == ttl)
if (idx == ttl) {
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};
// Get the absolute path of the executable
readlink("/proc/self/exe", enclave_path, sizeof(enclave_path));
@ -140,7 +141,7 @@ static const char* get_enclave_absolute_path() {
// Get the absolute path of the enclave
strncat(enclave_path, "/../lib/", sizeof(enclave_path));
strncat(enclave_path, ENCLAVE_FILENAME, sizeof(enclave_path));
return (const char*)enclave_path;
return (const char *)enclave_path;
}
/* Initialize the enclave:
@ -148,8 +149,7 @@ static const char* get_enclave_absolute_path() {
* Step 2: call sgx_create_enclave to initialize an enclave instance
* 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'};
sgx_launch_token_t token = {0};
sgx_status_t ret = SGX_ERROR_UNEXPECTED;
@ -162,11 +162,11 @@ static int initialize_enclave(void)
const char *home_dir = getpwuid(getuid())->pw_dir;
if (home_dir != NULL &&
(strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
(strlen(home_dir) + strlen("/") + sizeof(TOKEN_FILENAME) + 1) <= MAX_PATH) {
/* compose the token path */
strncpy(token_path, home_dir, strlen(home_dir));
strncat(token_path, "/", strlen("/"));
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
} else {
/* if token path is too long or $HOME is NULL */
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
@ -189,27 +189,29 @@ static int initialize_enclave(void)
/* Step 2: call sgx_create_enclave to initialize an enclave instance */
/* Debug Support: set 2nd parameter to 1 */
const char* enclave_path = get_enclave_absolute_path();
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL);
const char *enclave_path = get_enclave_absolute_path();
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated, &global_eid,
NULL);
if (ret != SGX_SUCCESS) {
print_error_message(ret);
if (fp != NULL) fclose(fp);
if (fp != NULL) { fclose(fp); }
return -1;
}
/* Step 3: save the launch token if it is updated */
if (updated == 0 || fp == NULL) {
/* 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;
}
/* reopen the file with write capablity */
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);
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);
}
fclose(fp);
return 0;
}
@ -226,15 +228,15 @@ void ocall_eprint(const char *str) {
fprintf(stderr, "%s", str);
}
int ocall_open(const char* path) {
int ocall_open(const char *path) {
return open(path, O_RDONLY);
}
ssize_t ocall_read(int fd, void* buf, size_t size) {
ssize_t ocall_read(int fd, void *buf, size_t size) {
return read(fd, buf, size);
}
ssize_t ocall_write(int fd, const void* buf, size_t size) {
ssize_t ocall_write(int fd, const void *buf, size_t size) {
return write(fd, buf, size);
}
@ -264,23 +266,19 @@ static void print_help(void) {
static int parse_args(
/* inputs */
int argc,
char* argv[],
char *argv[],
/* outputs */
int* arg_command,
char** arg_file_path)
{
if (argc != 3) return -1;
int *arg_command,
char **arg_file_path) {
if (argc != 3) { return -1; }
if (strcmp(argv[1], "protect") == 0) {
*arg_command = CMD_PROTECT;
}
else if (strcmp(argv[1], "show") == 0) {
} else if (strcmp(argv[1], "show") == 0) {
*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;
}
else {
} else {
return -1;
}
@ -295,7 +293,7 @@ static int parse_args(
int SGX_CDECL main(int argc, char *argv[]) {
/* Parse arguments */
int arg_command = CMD_ERROR;
char* arg_file_path = NULL;
char *arg_file_path = NULL;
if (parse_args(argc, argv, &arg_command, &arg_file_path) < 0) {
print_help();
return -1;
@ -309,14 +307,14 @@ int SGX_CDECL main(int argc, char *argv[]) {
/* Do the command */
int ret = 0;
switch(arg_command){
switch (arg_command) {
case CMD_PROTECT: {
const char* input_path = arg_file_path;
const char *input_path = arg_file_path;
const char* output_ext = ".protected";
const char *output_ext = ".protected";
size_t output_path_len = strlen(input_path) + strlen(output_ext) + 1;
char* output_path = (char*) malloc(output_path_len);
char *output_path = (char *) malloc(output_path_len);
strncpy(output_path, input_path, output_path_len);
strncat(output_path, output_ext, output_path_len);
@ -327,7 +325,7 @@ int SGX_CDECL main(int argc, char *argv[]) {
break;
}
case CMD_SHOW: {
const char* input_path = arg_file_path;
const char *input_path = arg_file_path;
if (ecall_show(global_eid, &ret, input_path)) {
fprintf(stderr, "Error: ecall failed\n");
ret = -1;
@ -335,7 +333,7 @@ int SGX_CDECL main(int argc, char *argv[]) {
break;
}
case CMD_SHOW_MAC: {
const char* input_path = arg_file_path;
const char *input_path = arg_file_path;
if (ecall_show_mac(global_eid, &ret, input_path)) {
fprintf(stderr, "Error: ecall failed\n");
ret = -1;

@ -10,7 +10,7 @@
#define PRINTF_BUFSIZE 512
static int printf(const char* fmt, ...) {
static int printf(const char *fmt, ...) {
char buf[PRINTF_BUFSIZE] = {0};
va_list args;
va_start(args, fmt);
@ -20,7 +20,7 @@ static int printf(const char* fmt, ...) {
return 0;
}
static int eprintf(const char* fmt, ...) {
static int eprintf(const char *fmt, ...) {
char buf[PRINTF_BUFSIZE] = {0};
va_list args;
va_start(args, fmt);
@ -30,29 +30,29 @@ static int eprintf(const char* fmt, ...) {
return 0;
}
static void print_mac(sgx_aes_gcm_128bit_tag_t* mac) {
unsigned char* bytes = (unsigned char*) mac;
static void print_mac(sgx_aes_gcm_128bit_tag_t *mac) {
unsigned char *bytes = (unsigned char *) mac;
for (size_t bi = 0; bi < sizeof(*mac); bi++) {
if (bi != 0) printf("-");
if (bi != 0) { printf("-"); }
printf("%02x", bytes[bi] & 0xFF);
}
printf("\n");
}
static int open(const char* path) {
static int open(const char *path) {
int fd = 0;
ocall_open(&fd, path);
return fd;
}
static ssize_t read(int fd, void* buf, size_t size) {
static ssize_t read(int fd, void *buf, size_t size) {
ssize_t ret = 0;
ocall_read(&ret, fd, buf, size);
return ret;
}
static ssize_t write(int fd, const void* buf, size_t size) {
static ssize_t write(int fd, const void *buf, size_t size) {
ssize_t ret = 0;
ocall_write(&ret, fd, buf, size);
return ret;
@ -68,9 +68,9 @@ static int close(int fd) {
// ECalls
// ==========================================================================
int ecall_protect(const char* input_path, const char* output_path) {
int ecall_protect(const char *input_path, const char *output_path) {
int input_file = -1;
SGX_FILE* output_file = NULL;
SGX_FILE *output_file = NULL;
size_t len;
char buf[4 * 1024];
@ -107,8 +107,8 @@ on_error:
return -1;
}
int ecall_show(const char* protected_file_path) {
SGX_FILE* protected_file = NULL;
int ecall_show(const char *protected_file_path) {
SGX_FILE *protected_file = NULL;
ssize_t len;
char buf[4 * 1024];
@ -136,8 +136,8 @@ on_error:
return -1;
}
int ecall_show_mac(const char* protected_file_path) {
SGX_FILE* protected_file = NULL;
int ecall_show_mac(const char *protected_file_path) {
SGX_FILE *protected_file = NULL;
sgx_aes_gcm_128bit_tag_t mac = { 0 };
protected_file = sgx_fopen_integrity_only(protected_file_path, "r");