Format c/c++ files in src, tools and test
This commit is contained in:
parent
03ba13aec7
commit
5b695c9539
@ -5,7 +5,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char* errno2str(int errno_);
|
const char *errno2str(int errno_);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -30,14 +30,14 @@ typedef struct occlum_pal_attr {
|
|||||||
// ".occlum"; it can be renamed to an arbitrary name.
|
// ".occlum"; it can be renamed to an arbitrary name.
|
||||||
//
|
//
|
||||||
// Mandatory field. Must not be NULL.
|
// Mandatory field. Must not be NULL.
|
||||||
const char* instance_dir;
|
const char *instance_dir;
|
||||||
// Log level.
|
// Log level.
|
||||||
//
|
//
|
||||||
// Specifies the log level of Occlum LibOS. Valid values: "off", "error",
|
// Specifies the log level of Occlum LibOS. Valid values: "off", "error",
|
||||||
// "warn", "info", and "trace". Case insensitive.
|
// "warn", "info", and "trace". Case insensitive.
|
||||||
//
|
//
|
||||||
// Optional field. If NULL, the LibOS will treat it as "off".
|
// Optional field. If NULL, the LibOS will treat it as "off".
|
||||||
const char* log_level;
|
const char *log_level;
|
||||||
} occlum_pal_attr_t;
|
} occlum_pal_attr_t;
|
||||||
|
|
||||||
#define OCCLUM_PAL_ATTR_INITVAL { \
|
#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.
|
* @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
|
* @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.
|
* @retval If 0, then success; otherwise, check errno for the exact error type.
|
||||||
*/
|
*/
|
||||||
int occlum_pal_exec(const char* cmd_path,
|
int occlum_pal_exec(const char *cmd_path,
|
||||||
const char** cmd_args,
|
const char **cmd_args,
|
||||||
const char** cmd_env,
|
const char **cmd_env,
|
||||||
const struct occlum_stdio_fds* io_fds,
|
const struct occlum_stdio_fds *io_fds,
|
||||||
int* exit_status);
|
int *exit_status);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief Send a signal to one or multiple LibOS processes
|
* @brief Send a signal to one or multiple LibOS processes
|
||||||
|
@ -1,21 +1,35 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "errno2str.h"
|
#include "errno2str.h"
|
||||||
|
|
||||||
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,8 @@
|
|||||||
#include "ocalls.h"
|
#include "ocalls.h"
|
||||||
|
|
||||||
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.
|
||||||
@ -12,16 +11,15 @@ sgx_status_t occlum_ocall_sgx_init_quote(
|
|||||||
}
|
}
|
||||||
|
|
||||||
sgx_status_t occlum_ocall_sgx_get_quote(
|
sgx_status_t occlum_ocall_sgx_get_quote(
|
||||||
uint8_t* sigrl,
|
uint8_t *sigrl,
|
||||||
uint32_t sigrl_len,
|
uint32_t sigrl_len,
|
||||||
sgx_report_t* report,
|
sgx_report_t *report,
|
||||||
sgx_quote_sign_type_t quote_type,
|
sgx_quote_sign_type_t quote_type,
|
||||||
sgx_spid_t* spid,
|
sgx_spid_t *spid,
|
||||||
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,18 +16,18 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
level_t level = new_level(_level);
|
||||||
if (level == LEVEL_OFF) {
|
if (level == LEVEL_OFF) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* color;
|
const char *color;
|
||||||
switch(level) {
|
switch (level) {
|
||||||
case LEVEL_ERROR:
|
case LEVEL_ERROR:
|
||||||
color = COLOR_RED;
|
color = COLOR_RED;
|
||||||
break;
|
break;
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "ocalls.h"
|
#include "ocalls.h"
|
||||||
|
|
||||||
void* occlum_ocall_posix_memalign(size_t alignment, size_t size) {
|
void *occlum_ocall_posix_memalign(size_t alignment, size_t size) {
|
||||||
void* ptr = NULL;
|
void *ptr = NULL;
|
||||||
int ret = posix_memalign(&ptr, alignment, size);
|
int ret = posix_memalign(&ptr, alignment, size);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
switch(ret) {
|
switch (ret) {
|
||||||
case ENOMEM:
|
case ENOMEM:
|
||||||
PAL_ERROR("Out of memory on the untrusted side");
|
PAL_ERROR("Out of memory on the untrusted side");
|
||||||
break;
|
break;
|
||||||
@ -22,6 +22,6 @@ void* occlum_ocall_posix_memalign(size_t alignment, size_t size) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void occlum_ocall_free(void* ptr) {
|
void occlum_ocall_free(void *ptr) {
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,13 @@ 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,
|
||||||
(struct iovec *) msg_iov,
|
(struct iovec *) msg_iov,
|
||||||
msg_iovlen,
|
msg_iovlen,
|
||||||
(void*) msg_control,
|
(void *) msg_control,
|
||||||
msg_controllen,
|
msg_controllen,
|
||||||
0,
|
0,
|
||||||
};
|
};
|
||||||
@ -28,15 +27,14 @@ ssize_t occlum_ocall_sendmsg(int sockfd,
|
|||||||
ssize_t occlum_ocall_recvmsg(int sockfd,
|
ssize_t occlum_ocall_recvmsg(int sockfd,
|
||||||
void *msg_name,
|
void *msg_name,
|
||||||
socklen_t msg_namelen,
|
socklen_t msg_namelen,
|
||||||
socklen_t* msg_namelen_recv,
|
socklen_t *msg_namelen_recv,
|
||||||
struct iovec *msg_iov,
|
struct iovec *msg_iov,
|
||||||
size_t msg_iovlen,
|
size_t msg_iovlen,
|
||||||
void *msg_control,
|
void *msg_control,
|
||||||
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;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "ocalls.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;
|
int ret;
|
||||||
cpu_set_t mask;
|
cpu_set_t mask;
|
||||||
CPU_ZERO(&mask);
|
CPU_ZERO(&mask);
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,15 +7,16 @@ typedef struct {
|
|||||||
int libos_tid;
|
int libos_tid;
|
||||||
} thread_data_t;
|
} thread_data_t;
|
||||||
|
|
||||||
void* exec_libos_thread(void* _thread_data) {
|
void *exec_libos_thread(void *_thread_data) {
|
||||||
thread_data_t* thread_data = _thread_data;
|
thread_data_t *thread_data = _thread_data;
|
||||||
sgx_enclave_id_t eid = thread_data->enclave_id;
|
sgx_enclave_id_t eid = thread_data->enclave_id;
|
||||||
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);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -29,7 +30,7 @@ int occlum_ocall_exec_thread_async(int libos_tid) {
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
pthread_t thread;
|
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->enclave_id = pal_get_enclave_id();
|
||||||
thread_data->libos_tid = libos_tid;
|
thread_data->libos_tid = libos_tid;
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include "ocalls.h"
|
#include "ocalls.h"
|
||||||
|
|
||||||
void occlum_ocall_gettimeofday(struct timeval* tv) {
|
void occlum_ocall_gettimeofday(struct timeval *tv) {
|
||||||
gettimeofday(tv, NULL);
|
gettimeofday(tv, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10,22 +10,22 @@ void occlum_ocall_clock_gettime(int clockid, struct timespec *tp) {
|
|||||||
clock_gettime(clockid, tp);
|
clock_gettime(clockid, tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void occlum_ocall_nanosleep(const struct timespec* req) {
|
void occlum_ocall_nanosleep(const struct timespec *req) {
|
||||||
nanosleep(req, NULL);
|
nanosleep(req, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int occlum_ocall_thread_getcpuclock(struct timespec *tp) {
|
int occlum_ocall_thread_getcpuclock(struct timespec *tp) {
|
||||||
clockid_t thread_clock_id;
|
clockid_t thread_clock_id;
|
||||||
int ret = pthread_getcpuclockid(pthread_self(), &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");
|
PAL_ERROR("failed to get clock id");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return clock_gettime(thread_clock_id, 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;
|
uint64_t rax, rdx;
|
||||||
asm volatile("rdtsc" : "=a"(rax), "=d"(rdx));
|
asm volatile("rdtsc" : "=a"(rax), "=d"(rdx));
|
||||||
*low = (uint32_t)rax;
|
*low = (uint32_t)rax;
|
||||||
|
@ -10,7 +10,7 @@ int occlum_pal_get_version(void) {
|
|||||||
return OCCLUM_PAL_VERSION;
|
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;
|
errno = 0;
|
||||||
|
|
||||||
if (attr == NULL) {
|
if (attr == NULL) {
|
||||||
@ -35,9 +35,10 @@ 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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -49,11 +50,11 @@ int occlum_pal_init(const struct occlum_pal_attr* attr) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int occlum_pal_exec(const char* cmd_path,
|
int occlum_pal_exec(const char *cmd_path,
|
||||||
const char** cmd_args,
|
const char **cmd_args,
|
||||||
const char** cmd_env,
|
const char **cmd_env,
|
||||||
const struct occlum_stdio_fds* io_fds,
|
const struct occlum_stdio_fds *io_fds,
|
||||||
int* exit_status) {
|
int *exit_status) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
if (cmd_path == NULL || cmd_args == NULL || exit_status == NULL) {
|
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
|
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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -85,7 +87,7 @@ int occlum_pal_exec(const char* cmd_path,
|
|||||||
int host_tid = gettid();
|
int host_tid = gettid();
|
||||||
ecall_status = occlum_ecall_exec_thread(eid, &ecall_ret, libos_tid, host_tid);
|
ecall_status = occlum_ecall_exec_thread(eid, &ecall_ret, libos_tid, host_tid);
|
||||||
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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -112,7 +114,7 @@ int occlum_pal_kill(int pid, int sig) {
|
|||||||
int ecall_ret = 0;
|
int ecall_ret = 0;
|
||||||
sgx_status_t ecall_status = occlum_ecall_kill(eid, &ecall_ret, pid, sig);
|
sgx_status_t ecall_status = occlum_ecall_kill(eid, &ecall_ret, pid, sig);
|
||||||
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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -31,24 +31,24 @@ static sgx_enclave_id_t global_eid = SGX_INVALID_ENCLAVE_ID;
|
|||||||
|
|
||||||
/* Get enclave debug flag according to env "OCCLUM_RELEASE_ENCLAVE" */
|
/* Get enclave debug flag according to env "OCCLUM_RELEASE_ENCLAVE" */
|
||||||
static int get_enclave_debug_flag() {
|
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 (release_enclave_val) {
|
||||||
if (!strcmp(release_enclave_val, "1") ||
|
if (!strcmp(release_enclave_val, "1") ||
|
||||||
!strcasecmp(release_enclave_val, "y") ||
|
!strcasecmp(release_enclave_val, "y") ||
|
||||||
!strcasecmp(release_enclave_val, "yes") ||
|
!strcasecmp(release_enclave_val, "yes") ||
|
||||||
!strcasecmp(release_enclave_val, "true")) {
|
!strcasecmp(release_enclave_val, "true")) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
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};
|
static char enclave_path[MAX_PATH + 1] = {0};
|
||||||
strncat(enclave_path, instance_dir, MAX_PATH);
|
strncat(enclave_path, instance_dir, MAX_PATH);
|
||||||
strncat(enclave_path, "/build/lib/", MAX_PATH);
|
strncat(enclave_path, "/build/lib/", MAX_PATH);
|
||||||
strncat(enclave_path, ENCLAVE_FILENAME, MAX_PATH);
|
strncat(enclave_path, ENCLAVE_FILENAME, MAX_PATH);
|
||||||
return (const char*)enclave_path;
|
return (const char *)enclave_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the enclave:
|
/* 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 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
|
||||||
*/
|
*/
|
||||||
int pal_init_enclave(const char* instance_dir) {
|
int pal_init_enclave(const char *instance_dir) {
|
||||||
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;
|
||||||
@ -69,11 +69,11 @@ int pal_init_enclave(const char* instance_dir) {
|
|||||||
const char *home_dir = getpwuid(getuid())->pw_dir;
|
const char *home_dir = getpwuid(getuid())->pw_dir;
|
||||||
|
|
||||||
if (home_dir != NULL &&
|
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 */
|
/* compose the token path */
|
||||||
strncpy(token_path, home_dir, strlen(home_dir));
|
strncpy(token_path, home_dir, strlen(home_dir));
|
||||||
strncat(token_path, "/", strlen("/"));
|
strncat(token_path, "/", strlen("/"));
|
||||||
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
|
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
|
||||||
} else {
|
} else {
|
||||||
/* if token path is too long or $HOME is NULL */
|
/* if token path is too long or $HOME is NULL */
|
||||||
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
|
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 */
|
/* 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(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;
|
||||||
}
|
}
|
||||||
|
@ -79,10 +79,10 @@ static sgx_err_msg_t err_msg_table[] = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* pal_get_sgx_error_msg(sgx_status_t error) {
|
const char *pal_get_sgx_error_msg(sgx_status_t error) {
|
||||||
int err_max = sizeof err_msg_table/sizeof err_msg_table[0];
|
int err_max = sizeof err_msg_table / sizeof err_msg_table[0];
|
||||||
for (int err_i = 0; err_i < err_max; err_i++) {
|
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;
|
return err_msg_table[err_i].msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,25 +6,24 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <occlum_pal_api.h>
|
#include <occlum_pal_api.h>
|
||||||
|
|
||||||
static const char* get_instance_dir(void) {
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stderr, "[ERROR] occlum-run: at least one argument must be provided\n\n");
|
fprintf(stderr, "[ERROR] occlum-run: at least one argument must be provided\n\n");
|
||||||
fprintf(stderr, "Usage: occlum-run <executable> [<args>]\n");
|
fprintf(stderr, "Usage: occlum-run <executable> [<args>]\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
const char* cmd_path = (const char*) argv[1];
|
const char *cmd_path = (const char *) argv[1];
|
||||||
const char** cmd_args = (const char**) &argv[2];
|
const char **cmd_args = (const char **) &argv[2];
|
||||||
extern const char **environ;
|
extern const char **environ;
|
||||||
|
|
||||||
// Check Occlum PAL version
|
// Check Occlum PAL version
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
static int create_file(const char *file_path, mode_t mode) {
|
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;
|
int fd;
|
||||||
|
|
||||||
fd = open(file_path, flags, mode);
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
static int create_file(const char *file_path) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00444;
|
int mode = 00444;
|
||||||
|
|
||||||
fd = open(file_path, flags, mode);
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
static int create_file(const char *file_path) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00444;
|
int mode = 00444;
|
||||||
|
|
||||||
fd = open(file_path, flags, mode);
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +101,7 @@ int client_connectionless_sendmsg(char *buf) {
|
|||||||
|
|
||||||
servaddr.sin_family = AF_INET;
|
servaddr.sin_family = AF_INET;
|
||||||
servaddr.sin_port = htons(9900);
|
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_name = &servaddr;
|
||||||
msg.msg_namelen = sizeof(servaddr);
|
msg.msg_namelen = sizeof(servaddr);
|
||||||
@ -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;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main(){
|
int main() {
|
||||||
std::cout<< "hello world" <<std::endl;
|
std::cout << "hello world" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -17,16 +17,15 @@ 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"
|
||||||
: "=a" (p->eax),
|
: "=a" (p->eax),
|
||||||
"=b" (p->ebx),
|
"=b" (p->ebx),
|
||||||
"=c" (p->ecx),
|
"=c" (p->ecx),
|
||||||
"=d" (p->edx)
|
"=d" (p->edx)
|
||||||
: "a" (leaf), "c" (subleaf));
|
: "a" (leaf), "c" (subleaf));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_cpuidinfo_equal(int leaf, t_cpuid_t *cpu, t_cpuid_t *cpu_sgx) {
|
static bool is_cpuidinfo_equal(int leaf, t_cpuid_t *cpu, t_cpuid_t *cpu_sgx) {
|
||||||
@ -184,7 +183,7 @@ static int test_cpuid_with_invalid_leaf() {
|
|||||||
int leaf[] = {0x8, 0xC, 0xE, 0x11};
|
int leaf[] = {0x8, 0xC, 0xE, 0x11};
|
||||||
int subleaf = 0;
|
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) {
|
if (leaf[i] > g_max_basic_leaf) {
|
||||||
printf("Warning: test leaf 0x%x is greater than CPU max basic leaf. Skipped.\n", leaf[i]);
|
printf("Warning: test leaf 0x%x is greater than CPU max basic leaf. Skipped.\n", leaf[i]);
|
||||||
continue;
|
continue;
|
||||||
@ -210,7 +209,7 @@ static int test_cpuid_with_oversized_leaf() {
|
|||||||
native_cpuid(leaf, subleaf, &cpu_max);
|
native_cpuid(leaf, subleaf, &cpu_max);
|
||||||
|
|
||||||
if ((cpu.eax != cpu_max.eax) || (cpu.ebx != cpu_max.ebx) ||
|
if ((cpu.eax != cpu_max.eax) || (cpu.ebx != cpu_max.ebx) ||
|
||||||
(cpu.ecx != cpu_max.ecx) || (cpu.edx != cpu_max.edx)) {
|
(cpu.ecx != cpu_max.ecx) || (cpu.edx != cpu_max.edx)) {
|
||||||
THROW_ERROR("failed to call cpuid with oversize leaf");
|
THROW_ERROR("failed to call cpuid with oversize leaf");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -235,7 +234,7 @@ static int test_cpuid_with_random_leaf() {
|
|||||||
#define BUFF_SIZE (1024)
|
#define BUFF_SIZE (1024)
|
||||||
static int test_cpuid_with_host_cpuidinfo() {
|
static int test_cpuid_with_host_cpuidinfo() {
|
||||||
char buff[BUFF_SIZE] = {0};
|
char buff[BUFF_SIZE] = {0};
|
||||||
FILE * fp = fopen("./test_cpuid.txt","r");
|
FILE *fp = fopen("./test_cpuid.txt", "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
THROW_ERROR("failed to open host cpuid.txt");
|
THROW_ERROR("failed to open host cpuid.txt");
|
||||||
}
|
}
|
||||||
@ -244,7 +243,7 @@ static int test_cpuid_with_host_cpuidinfo() {
|
|||||||
uint32_t subleaf = 0;
|
uint32_t subleaf = 0;
|
||||||
t_cpuid_t cpu = {0};
|
t_cpuid_t cpu = {0};
|
||||||
int num = sscanf(buff, " %x %x: eax=%x ebx=%x ecx=%x edx=%x", &leaf, &subleaf,
|
int num = sscanf(buff, " %x %x: eax=%x ebx=%x ecx=%x edx=%x", &leaf, &subleaf,
|
||||||
&cpu.eax, &cpu.ebx, &cpu.ecx, &cpu.edx);
|
&cpu.eax, &cpu.ebx, &cpu.ecx, &cpu.edx);
|
||||||
if (num != 6) {
|
if (num != 6) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -253,9 +252,9 @@ static int test_cpuid_with_host_cpuidinfo() {
|
|||||||
if (!is_cpuidinfo_equal(leaf, &cpu, &cpu_sgx)) {
|
if (!is_cpuidinfo_equal(leaf, &cpu, &cpu_sgx)) {
|
||||||
printf("leaf:0x%x subleaf:0x%x\n", leaf, subleaf);
|
printf("leaf:0x%x subleaf:0x%x\n", leaf, subleaf);
|
||||||
printf("ori_eax:0x%x ori_ebx:0x%x ori_ecx:0x%x ori_edx:0x%x\n",
|
printf("ori_eax:0x%x ori_ebx:0x%x ori_ecx:0x%x ori_edx:0x%x\n",
|
||||||
cpu.eax, cpu.ebx, cpu.ecx, cpu.edx);
|
cpu.eax, cpu.ebx, cpu.ecx, cpu.edx);
|
||||||
printf("sgx_eax:0x%x sgx_ebx:0x%x sgx_ecx:0x%x sgx_edx:0x%x\n",
|
printf("sgx_eax:0x%x sgx_ebx:0x%x sgx_ecx:0x%x sgx_edx:0x%x\n",
|
||||||
cpu_sgx.eax, cpu_sgx.ebx, cpu_sgx.ecx, cpu_sgx.edx);
|
cpu_sgx.eax, cpu_sgx.ebx, cpu_sgx.ecx, cpu_sgx.edx);
|
||||||
THROW_ERROR("failed to check cpuid info");
|
THROW_ERROR("failed to check cpuid info");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
|
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
|
||||||
|
|
||||||
// This program consumes a specific amount of data from stdin
|
// 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
|
// Get the total number of bytes to read
|
||||||
size_t remain_bytes = 0;
|
size_t remain_bytes = 0;
|
||||||
while (read(0, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes));
|
while (read(0, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes));
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
// Test utilities
|
// Test utilities
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
static int check_file_readable(const char* filename) {
|
static int check_file_readable(const char *filename) {
|
||||||
int fd;
|
int fd;
|
||||||
char buf[512] = {0};
|
char buf[512] = {0};
|
||||||
int len;
|
int len;
|
||||||
@ -24,7 +24,7 @@ static int check_file_readable(const char* filename) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_file_writable(const char* filename) {
|
static int check_file_writable(const char *filename) {
|
||||||
int fd;
|
int fd;
|
||||||
char buf[512] = {0};
|
char buf[512] = {0};
|
||||||
int len;
|
int len;
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,8 @@ static inline uint64_t native_syscall(syscall_args_t *p) {
|
|||||||
register unsigned long arg5 asm ("r9") = p->arg5;
|
register unsigned long arg5 asm ("r9") = p->arg5;
|
||||||
|
|
||||||
asm volatile("syscall"
|
asm volatile("syscall"
|
||||||
: "=a" (ret)
|
: "=a" (ret)
|
||||||
: "r" (num), "r" (arg0), "r" (arg1), "r" (arg2), "r" (arg3), "r" (arg4), "r" (arg5));
|
: "r" (num), "r" (arg0), "r" (arg1), "r" (arg2), "r" (arg3), "r" (arg4), "r" (arg5));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,13 +47,13 @@ static inline uint64_t native_syscall(syscall_args_t *p) {
|
|||||||
int test_mmap_and_munmap_via_syscall_instruction() {
|
int test_mmap_and_munmap_via_syscall_instruction() {
|
||||||
int len = PAGE_SIZE;
|
int len = PAGE_SIZE;
|
||||||
syscall_args_t mmap_arg = {
|
syscall_args_t mmap_arg = {
|
||||||
.num= __NR_mmap,
|
.num = __NR_mmap,
|
||||||
.arg0 = (unsigned long) NULL,
|
.arg0 = (unsigned long) NULL,
|
||||||
.arg1 = len,
|
.arg1 = len,
|
||||||
.arg2 = PROT_READ | PROT_WRITE,
|
.arg2 = PROT_READ | PROT_WRITE,
|
||||||
.arg3 = MAP_PRIVATE | MAP_ANONYMOUS,
|
.arg3 = MAP_PRIVATE | MAP_ANONYMOUS,
|
||||||
.arg4 = -1,
|
.arg4 = -1,
|
||||||
.arg5 = 0,
|
.arg5 = 0,
|
||||||
};
|
};
|
||||||
char *buf = (char *) native_syscall(&mmap_arg);
|
char *buf = (char *) native_syscall(&mmap_arg);
|
||||||
if (buf == MAP_FAILED) {
|
if (buf == MAP_FAILED) {
|
||||||
@ -66,9 +66,9 @@ int test_mmap_and_munmap_via_syscall_instruction() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
syscall_args_t munmap_arg = {
|
syscall_args_t munmap_arg = {
|
||||||
.num= __NR_munmap,
|
.num = __NR_munmap,
|
||||||
.arg0 = (unsigned long) buf,
|
.arg0 = (unsigned long) buf,
|
||||||
.arg1 = len,
|
.arg1 = len,
|
||||||
};
|
};
|
||||||
int ret = native_syscall(&munmap_arg);
|
int ret = native_syscall(&munmap_arg);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -84,6 +84,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_mmap_and_munmap_via_syscall_instruction),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
24
test/env/main.c
vendored
24
test/env/main.c
vendored
@ -13,12 +13,12 @@
|
|||||||
// Helper structs & variables & functions
|
// Helper structs & variables & functions
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
const char** g_argv;
|
const char **g_argv;
|
||||||
int g_argc;
|
int g_argc;
|
||||||
|
|
||||||
// Expected arguments are given by Makefile throught macro ARGC, ARG1, ARG2 and
|
// Expected arguments are given by Makefile throught macro ARGC, ARG1, ARG2 and
|
||||||
// ARG3
|
// ARG3
|
||||||
const char* expect_argv[EXPECT_ARGC] = {
|
const char *expect_argv[EXPECT_ARGC] = {
|
||||||
"env",
|
"env",
|
||||||
EXPECT_ARG1,
|
EXPECT_ARG1,
|
||||||
EXPECT_ARG2,
|
EXPECT_ARG2,
|
||||||
@ -27,40 +27,40 @@ const char* expect_argv[EXPECT_ARGC] = {
|
|||||||
|
|
||||||
// Expected child arguments
|
// Expected child arguments
|
||||||
const int child_argc = 2;
|
const int child_argc = 2;
|
||||||
const char* child_argv[3] = {
|
const char *child_argv[3] = {
|
||||||
"env",
|
"env",
|
||||||
"child",
|
"child",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
// Expected child environment variables
|
// Expected child environment variables
|
||||||
const char* child_envp[] = {
|
const char *child_envp[] = {
|
||||||
"ENV_CHILD=ok",
|
"ENV_CHILD=ok",
|
||||||
NULL
|
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++) {
|
for (int arg_i = 0; arg_i < g_argc; arg_i++) {
|
||||||
const char* actual_arg = *(g_argv + arg_i);
|
const char *actual_arg = *(g_argv + arg_i);
|
||||||
const char* expect_arg = *(expect_argv + arg_i);
|
const char *expect_arg = *(expect_argv + arg_i);
|
||||||
if (strcmp(actual_arg, expect_arg) != 0) {
|
if (strcmp(actual_arg, expect_arg) != 0) {
|
||||||
printf("ERROR: expect argument %d is %s, but given %s\n",
|
printf("ERROR: expect argument %d is %s, but given %s\n",
|
||||||
arg_i, expect_arg, actual_arg);
|
arg_i, expect_arg, actual_arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_env_val(const char* expect_env_key, const char* expect_env_val) {
|
static int test_env_val(const char *expect_env_key, const char *expect_env_val) {
|
||||||
const char* actual_env_val = getenv(expect_env_key);
|
const char *actual_env_val = getenv(expect_env_key);
|
||||||
if (actual_env_val == NULL) {
|
if (actual_env_val == NULL) {
|
||||||
printf("ERROR: cannot find %s\n", expect_env_key);
|
printf("ERROR: cannot find %s\n", expect_env_key);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (strcmp(actual_env_val, expect_env_val) != 0) {
|
if (strcmp(actual_env_val, expect_env_val) != 0) {
|
||||||
printf("ERROR: environment variable %s=%s expected, but given %s\n",
|
printf("ERROR: environment variable %s=%s expected, but given %s\n",
|
||||||
expect_env_key, expect_env_val, actual_env_val);
|
expect_env_key, expect_env_val, actual_env_val);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -204,7 +204,7 @@ static test_case_t child_test_cases[] = {
|
|||||||
TEST_CASE(test_env_child_getenv),
|
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
|
// Save argument for test cases
|
||||||
g_argc = argc;
|
g_argc = argc;
|
||||||
g_argv = argv;
|
g_argv = argv;
|
||||||
|
@ -131,7 +131,7 @@ int test_read_write() {
|
|||||||
THROW_ERROR("received length is not as expected");
|
THROW_ERROR("received length is not as expected");
|
||||||
}
|
}
|
||||||
data_recv += cur_data;
|
data_recv += cur_data;
|
||||||
} while (data_recv != TEST_DATA*CHILD_NUM);
|
} while (data_recv != TEST_DATA * CHILD_NUM);
|
||||||
|
|
||||||
close(event_fd);
|
close(event_fd);
|
||||||
|
|
||||||
@ -162,14 +162,14 @@ int test_select_with_socket() {
|
|||||||
FD_SET(sock, &wfds);
|
FD_SET(sock, &wfds);
|
||||||
FD_SET(event_fd, &rfds);
|
FD_SET(event_fd, &rfds);
|
||||||
FD_SET(event_fd, &wfds);
|
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) {
|
if (ret != 3) {
|
||||||
close_files(2, sock, event_fd);
|
close_files(2, sock, event_fd);
|
||||||
THROW_ERROR("select failed");
|
THROW_ERROR("select failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FD_ISSET(event_fd, &rfds) == 1 || FD_ISSET(event_fd, &wfds) == 0 ||
|
if (FD_ISSET(event_fd, &rfds) == 1 || FD_ISSET(event_fd, &wfds) == 0 ||
|
||||||
FD_ISSET(sock, &rfds) == 0 || FD_ISSET(sock, &wfds) == 0) {
|
FD_ISSET(sock, &rfds) == 0 || FD_ISSET(sock, &wfds) == 0) {
|
||||||
close_files(2, sock, event_fd);
|
close_files(2, sock, event_fd);
|
||||||
THROW_ERROR("bad select return");
|
THROW_ERROR("bad select return");
|
||||||
}
|
}
|
||||||
@ -211,15 +211,15 @@ int test_epoll_with_socket() {
|
|||||||
|
|
||||||
struct epoll_event ctl_events[2] = {0};
|
struct epoll_event ctl_events[2] = {0};
|
||||||
// Add eventfd to the interest list
|
// Add eventfd to the interest list
|
||||||
ctl_events[0].data.fd = event_fd;
|
ctl_events[0].data.fd = event_fd;
|
||||||
ctl_events[0].events = EPOLLIN | EPOLLET;
|
ctl_events[0].events = EPOLLIN | EPOLLET;
|
||||||
// Add socket to the interest list
|
// Add socket to the interest list
|
||||||
ctl_events[1].data.fd = sock;
|
ctl_events[1].data.fd = sock;
|
||||||
ctl_events[1].events = EPOLLIN | EPOLLET;
|
ctl_events[1].events = EPOLLIN | EPOLLET;
|
||||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, event_fd, &ctl_events[0]) == -1 ||
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, event_fd, &ctl_events[0]) == -1 ||
|
||||||
epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ctl_events[1]) == -1) {
|
epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ctl_events[1]) == -1) {
|
||||||
close_files(3, event_fd, sock, epfd);
|
close_files(3, event_fd, sock, epfd);
|
||||||
THROW_ERROR("epoll_ctl");
|
THROW_ERROR("epoll_ctl");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct thread_arg child_arg = { .tid = 0, .fd = event_fd, .data = TEST_DATA };
|
struct thread_arg child_arg = { .tid = 0, .fd = event_fd, .data = TEST_DATA };
|
||||||
@ -229,7 +229,7 @@ int test_epoll_with_socket() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct epoll_event events[MAXEVENTS] = {0};
|
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);
|
close_files(3, event_fd, sock, epfd);
|
||||||
THROW_ERROR("epoll failed");
|
THROW_ERROR("epoll failed");
|
||||||
}
|
}
|
||||||
@ -256,6 +256,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_select_with_socket),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
// Type 1: a busy loop thread
|
// Type 1: a busy loop thread
|
||||||
static void* busyloop_thread_func(void* _) {
|
static void *busyloop_thread_func(void *_) {
|
||||||
while (1) {
|
while (1) {
|
||||||
// By calling getpid, we give the LibOS a chance to force the thread
|
// 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
|
// 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
|
// 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) {
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
static int create_file(const char *file_path) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00666;
|
int mode = 00666;
|
||||||
fd = open(file_path, flags, mode);
|
fd = open(file_path, flags, mode);
|
||||||
if (fd < 0) {
|
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) {
|
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 };
|
char read_buf[128] = { 0 };
|
||||||
struct iovec iov[2];
|
struct iovec iov[2];
|
||||||
int fd, len = 0;
|
int fd, len = 0;
|
||||||
@ -108,8 +108,8 @@ static int __test_writev_readv(const char *file_path) {
|
|||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
THROW_ERROR("failed to open a file to writev");
|
THROW_ERROR("failed to open a file to writev");
|
||||||
}
|
}
|
||||||
for(int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
iov[i].iov_base = (void*)iov_msg[i];
|
iov[i].iov_base = (void *)iov_msg[i];
|
||||||
iov[i].iov_len = strlen(iov_msg[i]);
|
iov[i].iov_len = strlen(iov_msg[i]);
|
||||||
len += iov[i].iov_len;
|
len += iov[i].iov_len;
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ static int __test_writev_readv(const char *file_path) {
|
|||||||
THROW_ERROR("failed to read vectors from the file");
|
THROW_ERROR("failed to read vectors from the file");
|
||||||
}
|
}
|
||||||
if (memcmp(read_buf, iov_msg[0], strlen(iov_msg[0])) != 0 ||
|
if (memcmp(read_buf, iov_msg[0], strlen(iov_msg[0])) != 0 ||
|
||||||
memcmp(read_buf + strlen(iov_msg[0]), iov_msg[1], strlen(iov_msg[1])) != 0) {
|
memcmp(read_buf + strlen(iov_msg[0]), iov_msg[1], strlen(iov_msg[1])) != 0) {
|
||||||
THROW_ERROR("the message read from the file is not as it was written");
|
THROW_ERROR("the message read from the file is not as it was written");
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,11 +30,11 @@ static int open_file(const char *filename, int flags, int mode) {
|
|||||||
return fd;
|
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) {
|
static int write_file(int fd) {
|
||||||
int len = strlen(write_msg);
|
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");
|
PRINT_DBG("ERROR: failed to write to the file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ static int write_file(int fd) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_file(int fd){
|
static int read_file(int fd) {
|
||||||
int len;
|
int len;
|
||||||
char read_buf[128] = {0};
|
char read_buf[128] = {0};
|
||||||
if ((len = read(fd, read_buf, sizeof(read_buf) - 1)) <= 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
|
// do read or write according to do_write
|
||||||
// check the result of the read/write with the given expected_result
|
// check the result of the read/write with the given expected_result
|
||||||
static int do_perm_tests(
|
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];
|
||||||
int expected_result = expected_results[i];
|
int expected_result = expected_results[i];
|
||||||
|
|
||||||
int fd = open_file(filename, flags, 0666);
|
int fd = open_file(filename, flags, 0666);
|
||||||
@ -89,7 +88,7 @@ static int do_perm_tests(
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
// Test files
|
// Test files
|
||||||
static const char* test_files[NUM_TEST_FILES] = {
|
static const char *test_files[NUM_TEST_FILES] = {
|
||||||
"/test_fs_perms.txt",
|
"/test_fs_perms.txt",
|
||||||
"/bin/test_fs_perms.txt",
|
"/bin/test_fs_perms.txt",
|
||||||
"/lib/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
|
// 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()
|
// test_open_ro_then_write()
|
||||||
{NG, NG, NG, NG, NG},
|
{NG, NG, NG, NG, NG},
|
||||||
// test_open_wo_then_write()
|
// test_open_wo_then_write()
|
||||||
@ -115,32 +114,32 @@ static int test_expected_results[NUM_TEST_CASES][NUM_TEST_FILES]= {
|
|||||||
|
|
||||||
int test_open_ro_then_write() {
|
int test_open_ro_then_write() {
|
||||||
return do_perm_tests(test_files, NUM_TEST_FILES,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_RDONLY, 1, test_expected_results[0]);
|
O_RDONLY, 1, test_expected_results[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_open_wo_then_write() {
|
int test_open_wo_then_write() {
|
||||||
return do_perm_tests(test_files, NUM_TEST_FILES,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_WRONLY, 1, test_expected_results[1]);
|
O_WRONLY, 1, test_expected_results[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_open_rw_then_write() {
|
int test_open_rw_then_write() {
|
||||||
return do_perm_tests(test_files, NUM_TEST_FILES,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_RDWR, 1, test_expected_results[2]);
|
O_RDWR, 1, test_expected_results[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_open_ro_then_read() {
|
int test_open_ro_then_read() {
|
||||||
return do_perm_tests(test_files, NUM_TEST_FILES,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_RDONLY, 0, test_expected_results[3]);
|
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,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_WRONLY, 0, test_expected_results[4]);
|
O_WRONLY, 0, test_expected_results[4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_open_rw_then_read() {
|
int test_open_rw_then_read() {
|
||||||
return do_perm_tests(test_files, NUM_TEST_FILES,
|
return do_perm_tests(test_files, NUM_TEST_FILES,
|
||||||
O_RDWR, 0, test_expected_results[5]);
|
O_RDWR, 0, test_expected_results[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@ -156,6 +155,6 @@ test_case_t test_cases[NUM_TEST_CASES] = {
|
|||||||
TEST_CASE(test_open_rw_then_read)
|
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);
|
return test_suite_run(test_cases, NUM_TEST_CASES);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.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());
|
printf("Run a new process with pid = %d and ppid = %d\n", getpid(), getppid());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
static int create_file(const char *file_path) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00666;
|
int mode = 00666;
|
||||||
fd = open(file_path, flags, mode);
|
fd = open(file_path, flags, mode);
|
||||||
if (fd < 0) {
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
typedef int(*test_case_func_t)(void);
|
typedef int(*test_case_func_t)(void);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char* name;
|
const char *name;
|
||||||
test_case_func_t func;
|
test_case_func_t func;
|
||||||
} test_case_t;
|
} test_case_t;
|
||||||
|
|
||||||
@ -26,9 +26,9 @@ typedef struct {
|
|||||||
return -1; \
|
return -1; \
|
||||||
} while (0)
|
} 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++) {
|
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) {
|
if (tc->func() < 0) {
|
||||||
printf(" func %s - [ERR]\n", tc->name);
|
printf(" func %s - [ERR]\n", tc->name);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -38,19 +38,19 @@ typedef struct {
|
|||||||
sgx_quote_sign_type_t quote_type; // input
|
sgx_quote_sign_type_t quote_type; // input
|
||||||
sgx_spid_t spid; // input
|
sgx_spid_t spid; // input
|
||||||
sgx_quote_nonce_t nonce; // 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 sigrl_len; // input (optional)
|
||||||
uint32_t quote_buf_len; // input
|
uint32_t quote_buf_len; // input
|
||||||
union {
|
union {
|
||||||
uint8_t* as_buf;
|
uint8_t *as_buf;
|
||||||
sgx_quote_t* as_quote;
|
sgx_quote_t *as_quote;
|
||||||
} quote; // output
|
} quote; // output
|
||||||
} sgxioc_gen_quote_arg_t;
|
} sgxioc_gen_quote_arg_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const sgx_target_info_t* target_info; // input (optinal)
|
const sgx_target_info_t *target_info; // input (optinal)
|
||||||
const sgx_report_data_t* report_data; // input (optional)
|
const sgx_report_data_t *report_data; // input (optional)
|
||||||
sgx_report_t* report; // output
|
sgx_report_t *report; // output
|
||||||
} sgxioc_create_report_arg_t;
|
} sgxioc_create_report_arg_t;
|
||||||
|
|
||||||
#define SGXIOC_IS_EDMM_SUPPORTED _IOR('s', 0, int)
|
#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);
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,15 +107,14 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
|
|||||||
.sigrl_ptr = NULL, // input (optional)
|
.sigrl_ptr = NULL, // input (optional)
|
||||||
.sigrl_len = 0, // input (optional)
|
.sigrl_len = 0, // input (optional)
|
||||||
.quote_buf_len = sizeof(quote_buf), // input
|
.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;
|
int nretries = 0;
|
||||||
while (nretries < IOCTL_MAX_RETRIES) {
|
while (nretries < IOCTL_MAX_RETRIES) {
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,14 +126,15 @@ static int do_SGXIOC_GEN_QUOTE(int sgx_fd) {
|
|||||||
THROW_ERROR("failed to ioctl /dev/sgx due to timeout");
|
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) {
|
if (quote->sign_type != SGX_LINKABLE_SIGNATURE) {
|
||||||
THROW_ERROR("invalid quote: wrong sign type");
|
THROW_ERROR("invalid quote: wrong sign type");
|
||||||
}
|
}
|
||||||
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, "e->report_body.report_data, sizeof(sgx_report_data_t)) != 0) {
|
if (memcmp(&gen_quote_arg.report_data, "e->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;
|
||||||
@ -174,19 +173,19 @@ static int do_SGXIOC_CREATE_AND_VERIFY_REPORT(int sgx_fd) {
|
|||||||
|
|
||||||
sgxioc_create_report_arg_t args[] = {
|
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_data = NULL,
|
||||||
.report = &report
|
.report = &report
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.target_info = (const sgx_target_info_t*) &target_info,
|
.target_info = (const sgx_target_info_t *) &target_info,
|
||||||
.report_data = (const sgx_report_data_t*) &report_data,
|
.report_data = (const sgx_report_data_t *) &report_data,
|
||||||
.report = &report
|
.report = &report
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for (int arg_i = 0; arg_i < ARRAY_SIZE(args); arg_i++) {
|
for (int arg_i = 0; arg_i < ARRAY_SIZE(args); arg_i++) {
|
||||||
memset(&report, 0, sizeof(report));
|
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) {
|
if (ioctl(sgx_fd, SGXIOC_CREATE_REPORT, arg) < 0) {
|
||||||
THROW_ERROR("failed to create report");
|
THROW_ERROR("failed to create report");
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,15 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
const char* file_name = "/root/test_filesystem_link.txt";
|
const char *file_name = "/root/test_filesystem_link.txt";
|
||||||
const char* link_name = "/root/link.txt";
|
const char *link_name = "/root/link.txt";
|
||||||
const char* write_msg = "Hello World\n";
|
const char *write_msg = "Hello World\n";
|
||||||
char read_buf[128] = {0};
|
char read_buf[128] = {0};
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
// create a file and write message
|
// 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 mode = 00666;
|
||||||
int fd = open(file_name, flags, mode);
|
int fd = open(file_name, flags, mode);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
@ -30,7 +30,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
// link
|
// link
|
||||||
ret = link(file_name, link_name);
|
ret = link(file_name, link_name);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ERROR: failed to link the file\n");
|
printf("ERROR: failed to link the file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
// unlink
|
// unlink
|
||||||
ret = unlink(link_name);
|
ret = unlink(link_name);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ERROR: failed to link the file\n");
|
printf("ERROR: failed to link the file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -62,26 +62,26 @@ int main(int argc, const char* argv[]) {
|
|||||||
// stat
|
// stat
|
||||||
struct stat stat_buf;
|
struct stat stat_buf;
|
||||||
ret = stat(link_name, &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);
|
printf("ERROR: stat on \"%s\" should return ENOENT", link_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// rename
|
// rename
|
||||||
ret = rename(file_name, link_name);
|
ret = rename(file_name, link_name);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ERROR: failed to rename the file");
|
printf("ERROR: failed to rename the file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// stat
|
// stat
|
||||||
ret = stat(file_name, &stat_buf);
|
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);
|
printf("ERROR: stat on \"%s\" should return ENOENT", file_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ret = stat(link_name, &stat_buf);
|
ret = stat(link_name, &stat_buf);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ERROR: failed to stat the file");
|
printf("ERROR: failed to stat the file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
#define MAX_SIZE (1*1024*1024)
|
#define MAX_SIZE (1*1024*1024)
|
||||||
#define MIN_SIZE 8
|
#define MIN_SIZE 8
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
printf("Testing malloc and free...\n");
|
printf("Testing malloc and free...\n");
|
||||||
for (size_t buf_size = MIN_SIZE; buf_size <= MAX_SIZE; buf_size *= 4) {
|
for (size_t buf_size = MIN_SIZE; buf_size <= MAX_SIZE; buf_size *= 4) {
|
||||||
printf("buf_size = %lu\n", buf_size);
|
printf("buf_size = %lu\n", buf_size);
|
||||||
void* buf = malloc(buf_size);
|
void *buf = malloc(buf_size);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
printf("ERROR: failed to malloc for a buffer of %lu size\n", buf_size);
|
printf("ERROR: failed to malloc for a buffer of %lu size\n", buf_size);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -5,20 +5,20 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
|
||||||
const int BUF_SIZE = 20;
|
const int BUF_SIZE = 20;
|
||||||
char buf[10];
|
char buf[10];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
char* cwd = getcwd(buf, BUF_SIZE);
|
char *cwd = getcwd(buf, BUF_SIZE);
|
||||||
if(cwd != buf) {
|
if (cwd != buf) {
|
||||||
printf("failed to getcwd\n");
|
printf("failed to getcwd\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char expect_cwd[] = "/";
|
const char expect_cwd[] = "/";
|
||||||
if(strcmp(buf, expect_cwd)) {
|
if (strcmp(buf, expect_cwd)) {
|
||||||
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd);
|
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -29,37 +29,37 @@ int main(int argc, const char* argv[]) {
|
|||||||
const char DIR_PATH[] = "/root/test_dir";
|
const char DIR_PATH[] = "/root/test_dir";
|
||||||
const int DIR_MODE = 0664;
|
const int DIR_MODE = 0664;
|
||||||
ret = mkdir(DIR_NAME, DIR_MODE);
|
ret = mkdir(DIR_NAME, DIR_MODE);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("failed to mkdir \"%s\"", DIR_NAME);
|
printf("failed to mkdir \"%s\"", DIR_NAME);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = chdir(DIR_NAME);
|
ret = chdir(DIR_NAME);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("failed to chdir to \"%s\"", DIR_NAME);
|
printf("failed to chdir to \"%s\"", DIR_NAME);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
cwd = getcwd(buf, BUF_SIZE);
|
cwd = getcwd(buf, BUF_SIZE);
|
||||||
if(cwd != buf) {
|
if (cwd != buf) {
|
||||||
printf("failed to getcwd\n");
|
printf("failed to getcwd\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strcmp(buf, DIR_PATH)) {
|
if (strcmp(buf, DIR_PATH)) {
|
||||||
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH);
|
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = rmdir(DIR_PATH);
|
ret = rmdir(DIR_PATH);
|
||||||
if(ret < 0) {
|
if (ret < 0) {
|
||||||
printf("failed to rmdir \"%s\"", DIR_PATH);
|
printf("failed to rmdir \"%s\"", DIR_PATH);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat stat_buf;
|
struct stat stat_buf;
|
||||||
ret = stat(DIR_PATH, &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);
|
printf("stat on \"%s\" should return ENOENT", DIR_PATH);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -43,20 +43,20 @@ static int fill_file_with_repeated_bytes(int fd, size_t len, int byte_val) {
|
|||||||
return 0;
|
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++) {
|
for (size_t bi = 0; bi < len; bi++) {
|
||||||
if (buf[bi] != (char)expected_byte_val) {
|
if (buf[bi] != (char)expected_byte_val) {
|
||||||
printf("check_bytes_in_buf: expect %02X, but found %02X, at offset %lu\n",
|
printf("check_bytes_in_buf: expect %02X, but found %02X, at offset %lu\n",
|
||||||
(unsigned char)expected_byte_val, (unsigned char)buf[bi], bi);
|
(unsigned char)expected_byte_val, (unsigned char)buf[bi], bi);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* get_a_stack_ptr() {
|
static void *get_a_stack_ptr() {
|
||||||
volatile int a = 0;
|
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;
|
size_t big_buf_len = MAX_MMAP_USED_MEMORY;
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
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) {
|
if (big_buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap failed");
|
THROW_ERROR("mmap failed");
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ int test_anonymous_mmap() {
|
|||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
|
|
||||||
for (size_t len = PAGE_SIZE; len <= MAX_MMAP_USED_MEMORY; len *= 2) {
|
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) {
|
if (buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap failed");
|
THROW_ERROR("mmap failed");
|
||||||
}
|
}
|
||||||
@ -120,7 +120,7 @@ int test_anonymous_mmap_randomly() {
|
|||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
|
|
||||||
void* bufs[16] = {NULL};
|
void *bufs[16] = {NULL};
|
||||||
size_t lens[16];
|
size_t lens[16];
|
||||||
size_t num_bufs = 0;
|
size_t num_bufs = 0;
|
||||||
size_t used_memory = 0;
|
size_t used_memory = 0;
|
||||||
@ -128,15 +128,14 @@ int test_anonymous_mmap_randomly() {
|
|||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
// 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);
|
||||||
|
|
||||||
// Do mmap
|
// Do mmap
|
||||||
void* buf = mmap(NULL, len, prot, flags, -1, 0);
|
void *buf = mmap(NULL, len, prot, flags, -1, 0);
|
||||||
if (buf == MAP_FAILED) {
|
if (buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap failed");
|
THROW_ERROR("mmap failed");
|
||||||
}
|
}
|
||||||
@ -149,7 +148,7 @@ int test_anonymous_mmap_randomly() {
|
|||||||
|
|
||||||
// Phrase 2: do munmap to free all memory mapped memory
|
// Phrase 2: do munmap to free all memory mapped memory
|
||||||
for (int bi = 0; bi < num_bufs; bi++) {
|
for (int bi = 0; bi < num_bufs; bi++) {
|
||||||
void* buf = bufs[bi];
|
void *buf = bufs[bi];
|
||||||
size_t len = lens[bi];
|
size_t len = lens[bi];
|
||||||
int ret = munmap(buf, len);
|
int ret = munmap(buf, len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -175,10 +174,10 @@ int test_anonymous_mmap_randomly_with_good_hints() {
|
|||||||
hint = ALIGN_DOWN(hint, PAGE_SIZE);
|
hint = ALIGN_DOWN(hint, PAGE_SIZE);
|
||||||
|
|
||||||
size_t len = rand() % (HINT_END - (size_t)hint);
|
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);
|
void *addr = mmap((void *)hint, len, prot, flags, -1, 0);
|
||||||
if (addr != (void*)hint) {
|
if (addr != (void *)hint) {
|
||||||
THROW_ERROR("mmap with hint failed");
|
THROW_ERROR("mmap with hint failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,8 +200,8 @@ int test_anonymous_mmap_with_bad_hints() {
|
|||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
for (int hi = 0; hi < ARRAY_SIZE(bad_hints); hi++) {
|
for (int hi = 0; hi < ARRAY_SIZE(bad_hints); hi++) {
|
||||||
void* bad_hint = (void*)bad_hints[hi];
|
void *bad_hint = (void *)bad_hints[hi];
|
||||||
void* addr = mmap(bad_hint, len, prot, flags, -1, 0);
|
void *addr = mmap(bad_hint, len, prot, flags, -1, 0);
|
||||||
if (addr == MAP_FAILED) {
|
if (addr == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap should have tolerated a bad hint");
|
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 prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
int len = 0; // invalid!
|
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) {
|
if (buf != MAP_FAILED) {
|
||||||
THROW_ERROR("mmap with zero len should have been 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 len = PAGE_SIZE + 17; // length need not to be page aligned!
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
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) {
|
if (buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap with non-page aligned len should have worked");
|
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() {
|
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);
|
int fd = open(file_path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
THROW_ERROR("file creation failed");
|
THROW_ERROR("file creation failed");
|
||||||
@ -272,7 +271,7 @@ int test_file_mmap() {
|
|||||||
}
|
}
|
||||||
off_t offset = 0;
|
off_t offset = 0;
|
||||||
for (size_t len = PAGE_SIZE; len <= file_len; len *= 2) {
|
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) {
|
if (buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap failed");
|
THROW_ERROR("mmap failed");
|
||||||
}
|
}
|
||||||
@ -292,7 +291,7 @@ int test_file_mmap() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int test_file_mmap_with_offset() {
|
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);
|
int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
THROW_ERROR("file creation failed");
|
THROW_ERROR("file creation failed");
|
||||||
@ -310,12 +309,12 @@ int test_file_mmap_with_offset() {
|
|||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE;
|
int flags = MAP_PRIVATE;
|
||||||
assert(offset <= first_len);
|
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) {
|
if (buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap 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) {
|
if (check_bytes_in_buf(buf_cursor, first_len - offset, first_val) < 0) {
|
||||||
THROW_ERROR("the buffer is not initialized according to the file");
|
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");
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,7 +344,7 @@ int test_file_mmap_with_invalid_fd() {
|
|||||||
int flags = MAP_PRIVATE;
|
int flags = MAP_PRIVATE;
|
||||||
int fd = 1234; // invalid!
|
int fd = 1234; // invalid!
|
||||||
off_t offset = 0;
|
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) {
|
if (buf != MAP_FAILED) {
|
||||||
THROW_ERROR("file mmap with an invalid fd should have been 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() {
|
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);
|
int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
THROW_ERROR("file creation failed");
|
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 prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
off_t offset = PAGE_SIZE + 127; // Invalid!
|
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) {
|
if (buf != MAP_FAILED) {
|
||||||
THROW_ERROR("mmap with non-page aligned len should have been 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);
|
len = ALIGN_UP(len, PAGE_SIZE);
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
|
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 != (void*)hint) {
|
if (addr != (void *)hint) {
|
||||||
THROW_ERROR("mmap with fixed address failed");
|
THROW_ERROR("mmap with fixed address failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,7 +412,7 @@ int test_fixed_mmap_that_overrides_existing_mmaping() {
|
|||||||
// Allocate parent_buf
|
// Allocate parent_buf
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
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) {
|
if (parent_buf == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap for parent 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);
|
memset(parent_buf, parent_val, parent_len);
|
||||||
|
|
||||||
// Allocate child_buf
|
// 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) {
|
if (mmap(child_buf, child_len, prot, flags | MAP_FIXED, -1, 0) != child_buf) {
|
||||||
THROW_ERROR("mmap with fixed address failed");
|
THROW_ERROR("mmap with fixed address failed");
|
||||||
}
|
}
|
||||||
@ -430,10 +430,10 @@ int test_fixed_mmap_that_overrides_existing_mmaping() {
|
|||||||
THROW_ERROR("the content of child mmap memory is not initialized");
|
THROW_ERROR("the content of child mmap memory is not initialized");
|
||||||
}
|
}
|
||||||
// Check that the rest of parent_buf are kept intact
|
// 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 ||
|
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) {
|
post_child_len, parent_val) < 0) {
|
||||||
THROW_ERROR("the content of parent mmap memory is broken");
|
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;
|
size_t len = 1 * PAGE_SIZE;
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
|
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) {
|
if (addr != MAP_FAILED) {
|
||||||
THROW_ERROR("fixed mmap with non-page aligned hint should have 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
|
// 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
|
// 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.
|
||||||
int prot = PROT_READ | PROT_WRITE;
|
int prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
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) {
|
if (real_addr != target_addr) {
|
||||||
THROW_ERROR("address is already mmaped");
|
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 prot = PROT_READ | PROT_WRITE;
|
||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
|
||||||
// Make sure that we are manipulating memory between [HINT_BEGIN, HINT_END)
|
// 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) {
|
if (mmap(mmap_addr, mmap_len, prot, flags, -1, 0) != mmap_addr) {
|
||||||
THROW_ERROR("mmap failed");
|
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) {
|
if (munmap(munmap_addr, munmap_len) < 0) {
|
||||||
THROW_ERROR("munmap failed");
|
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
|
// Make sure that when this function returns, there are no memory mappings
|
||||||
// within [HINT_BEGIN, HINT_END)
|
// 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");
|
THROW_ERROR("munmap failed");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -548,13 +548,13 @@ int test_munmap_whose_range_intersects_with_multiple_mmap_regions() {
|
|||||||
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||||
|
|
||||||
size_t mmap_len1 = 100 * PAGE_SIZE;
|
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) {
|
if (mmap_addr1 == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap failed");
|
THROW_ERROR("mmap failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mmap_len2 = 12 * PAGE_SIZE;
|
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) {
|
if (mmap_addr2 == MAP_FAILED) {
|
||||||
THROW_ERROR("mmap 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_max = MAX((size_t)mmap_addr1 + mmap_len1,
|
||||||
(size_t)mmap_addr2 + mmap_len2);
|
(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;
|
size_t munmap_len = mmap_max - mmap_min;
|
||||||
if (munmap(munmap_addr, munmap_len) < 0) {
|
if (munmap(munmap_addr, munmap_len) < 0) {
|
||||||
THROW_ERROR("munmap failed");
|
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
|
// 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
|
// range does not contain any mapped pages". This is not considered as
|
||||||
// an error!
|
// an error!
|
||||||
void* munmap_addr = NULL;
|
void *munmap_addr = NULL;
|
||||||
size_t munmap_len = PAGE_SIZE;
|
size_t munmap_len = PAGE_SIZE;
|
||||||
if (munmap(munmap_addr, munmap_len) < 0) {
|
if (munmap(munmap_addr, munmap_len) < 0) {
|
||||||
THROW_ERROR("munmap failed");
|
THROW_ERROR("munmap failed");
|
||||||
@ -590,7 +590,7 @@ int test_munmap_with_null_addr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int test_munmap_with_zero_len() {
|
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!
|
// Set the length for munmap to 0! This is invalid!
|
||||||
size_t munmap_len = 0;
|
size_t munmap_len = 0;
|
||||||
if (munmap(munmap_addr, 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) {
|
static int test_open_framework(test_open_func_t fn) {
|
||||||
const char *file_path = "/root/test_filesystem_open.txt";
|
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;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ int test_fcntl_get_flags() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((fcntl(pipe_fds[0], F_GETFL, 0) != O_RDONLY) ||
|
if ((fcntl(pipe_fds[0], F_GETFL, 0) != O_RDONLY) ||
|
||||||
(fcntl(pipe_fds[1], F_GETFL, 0) != O_WRONLY)) {
|
(fcntl(pipe_fds[1], F_GETFL, 0) != O_WRONLY)) {
|
||||||
free_pipe(pipe_fds);
|
free_pipe(pipe_fds);
|
||||||
THROW_ERROR("fcntl get flags failed");
|
THROW_ERROR("fcntl get flags failed");
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ int test_fcntl_set_flags() {
|
|||||||
|
|
||||||
fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK);
|
fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK);
|
||||||
if ((fcntl(pipe_fds[0], F_GETFL, 0) != (O_NONBLOCK | O_RDONLY)) ||
|
if ((fcntl(pipe_fds[0], F_GETFL, 0) != (O_NONBLOCK | O_RDONLY)) ||
|
||||||
(fcntl(pipe_fds[1], F_GETFL, 0) != O_WRONLY)) {
|
(fcntl(pipe_fds[1], F_GETFL, 0) != O_WRONLY)) {
|
||||||
free_pipe(pipe_fds);
|
free_pipe(pipe_fds);
|
||||||
THROW_ERROR("fcntl set flags failed");
|
THROW_ERROR("fcntl set flags failed");
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ int test_create_with_flags() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((fcntl(pipe_fds[0], F_GETFL, 0) != (O_NONBLOCK | O_RDONLY)) ||
|
if ((fcntl(pipe_fds[0], F_GETFL, 0) != (O_NONBLOCK | O_RDONLY)) ||
|
||||||
(fcntl(pipe_fds[1], F_GETFL, 0) != (O_NONBLOCK | O_WRONLY))) {
|
(fcntl(pipe_fds[1], F_GETFL, 0) != (O_NONBLOCK | O_WRONLY))) {
|
||||||
free_pipe(pipe_fds);
|
free_pipe(pipe_fds);
|
||||||
THROW_ERROR("create flags failed\n");
|
THROW_ERROR("create flags failed\n");
|
||||||
}
|
}
|
||||||
@ -83,17 +83,17 @@ int test_read_write() {
|
|||||||
posix_spawn_file_actions_adddup2(&file_actions, pipe_wr_fd, STDOUT_FILENO);
|
posix_spawn_file_actions_adddup2(&file_actions, pipe_wr_fd, STDOUT_FILENO);
|
||||||
posix_spawn_file_actions_addclose(&file_actions, pipe_rd_fd);
|
posix_spawn_file_actions_addclose(&file_actions, pipe_rd_fd);
|
||||||
|
|
||||||
const char* msg = "Echo!\n";
|
const char *msg = "Echo!\n";
|
||||||
const char* child_prog = "/bin/hello_world";
|
const char *child_prog = "/bin/hello_world";
|
||||||
const char* child_argv[3] = { child_prog, msg, NULL };
|
const char *child_argv[3] = { child_prog, msg, NULL };
|
||||||
int child_pid;
|
int child_pid;
|
||||||
if (posix_spawn(&child_pid, child_prog, &file_actions,
|
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");
|
THROW_ERROR("failed to spawn a child process");
|
||||||
}
|
}
|
||||||
close(pipe_wr_fd);
|
close(pipe_wr_fd);
|
||||||
|
|
||||||
const char* expected_str = msg;
|
const char *expected_str = msg;
|
||||||
size_t expected_len = strlen(expected_str);
|
size_t expected_len = strlen(expected_str);
|
||||||
char actual_str[32] = {0};
|
char actual_str[32] = {0};
|
||||||
ssize_t actual_len;
|
ssize_t actual_len;
|
||||||
@ -123,6 +123,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_read_write),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
|
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
// Create pipe
|
// Create pipe
|
||||||
int pipe_fds[2];
|
int pipe_fds[2];
|
||||||
if (pipe(pipe_fds) < 0) {
|
if (pipe(pipe_fds) < 0) {
|
||||||
@ -34,7 +34,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
int child_pid;
|
int child_pid;
|
||||||
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
|
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
|
||||||
NULL, NULL, NULL) < 0) {
|
NULL, NULL, NULL) < 0) {
|
||||||
printf("ERROR: failed to spawn a child process\n");
|
printf("ERROR: failed to spawn a child process\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
// Calculate the throughput
|
// Calculate the throughput
|
||||||
double total_s = (tv_end.tv_sec - tv_start.tv_sec)
|
double total_s = (tv_end.tv_sec - tv_start.tv_sec)
|
||||||
+ (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000;
|
+ (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000;
|
||||||
if (total_s < 1.0) {
|
if (total_s < 1.0) {
|
||||||
printf("WARNING: run long enough to get meaningful results\n");
|
printf("WARNING: run long enough to get meaningful results\n");
|
||||||
if (total_s == 0) { return 0; }
|
if (total_s == 0) { return 0; }
|
||||||
|
@ -21,12 +21,12 @@
|
|||||||
struct thread_arg {
|
struct thread_arg {
|
||||||
int ti;
|
int ti;
|
||||||
long local_count;
|
long local_count;
|
||||||
volatile unsigned long* global_count;
|
volatile unsigned long *global_count;
|
||||||
pthread_mutex_t* mutex;
|
pthread_mutex_t *mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void* thread_func(void* _arg) {
|
static void *thread_func(void *_arg) {
|
||||||
struct thread_arg* arg = _arg;
|
struct thread_arg *arg = _arg;
|
||||||
for (long i = 0; i < arg->local_count; i++) {
|
for (long i = 0; i < arg->local_count; i++) {
|
||||||
pthread_mutex_lock(arg->mutex);
|
pthread_mutex_lock(arg->mutex);
|
||||||
(*arg->global_count)++;
|
(*arg->global_count)++;
|
||||||
@ -51,7 +51,7 @@ static int test_mutex_with_concurrent_counter(void) {
|
|||||||
* Start the threads
|
* Start the threads
|
||||||
*/
|
*/
|
||||||
for (int ti = 0; ti < NTHREADS; ti++) {
|
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->ti = ti;
|
||||||
thread_arg->local_count = LOCAL_COUNT;
|
thread_arg->local_count = LOCAL_COUNT;
|
||||||
thread_arg->global_count = &global_count;
|
thread_arg->global_count = &global_count;
|
||||||
@ -76,7 +76,7 @@ static int test_mutex_with_concurrent_counter(void) {
|
|||||||
*/
|
*/
|
||||||
if (global_count != EXPECTED_GLOBAL_COUNT) {
|
if (global_count != EXPECTED_GLOBAL_COUNT) {
|
||||||
printf("ERROR: incorrect global_count (actual = %ld, expected = %ld)\n",
|
printf("ERROR: incorrect global_count (actual = %ld, expected = %ld)\n",
|
||||||
global_count, EXPECTED_GLOBAL_COUNT);
|
global_count, EXPECTED_GLOBAL_COUNT);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,14 +92,14 @@ static int test_mutex_with_concurrent_counter(void) {
|
|||||||
|
|
||||||
struct thread_cond_arg {
|
struct thread_cond_arg {
|
||||||
int ti;
|
int ti;
|
||||||
volatile unsigned int* val;
|
volatile unsigned int *val;
|
||||||
volatile int* exit_thread_count;
|
volatile int *exit_thread_count;
|
||||||
pthread_cond_t* cond_val;
|
pthread_cond_t *cond_val;
|
||||||
pthread_mutex_t* mutex;
|
pthread_mutex_t *mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void* thread_cond_wait(void* _arg) {
|
static void *thread_cond_wait(void *_arg) {
|
||||||
struct thread_cond_arg* arg = _arg;
|
struct thread_cond_arg *arg = _arg;
|
||||||
printf("Thread #%d: start to wait on condition variable.\n", arg->ti);
|
printf("Thread #%d: start to wait on condition variable.\n", arg->ti);
|
||||||
for (unsigned int i = 0; i < WAIT_ROUND; ++i) {
|
for (unsigned int i = 0; i < WAIT_ROUND; ++i) {
|
||||||
pthread_mutex_lock(arg->mutex);
|
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
|
* Start the threads waiting on the condition variable
|
||||||
*/
|
*/
|
||||||
for (int ti = 0; ti < NTHREADS; ti++) {
|
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->ti = ti;
|
||||||
thread_arg->val = &val;
|
thread_arg->val = &val;
|
||||||
thread_arg->exit_thread_count = &exit_thread_count;
|
thread_arg->exit_thread_count = &exit_thread_count;
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
static int test_readdir() {
|
static int test_readdir() {
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
DIR* dirp;
|
DIR *dirp;
|
||||||
|
|
||||||
dirp = opendir("/");
|
dirp = opendir("/");
|
||||||
if (dirp == NULL) {
|
if (dirp == NULL) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
struct rlimit rlim;
|
struct rlimit rlim;
|
||||||
if (getrlimit(RLIMIT_AS, &rlim) < 0) {
|
if (getrlimit(RLIMIT_AS, &rlim) < 0) {
|
||||||
printf("ERROR: getrlimit failed\n");
|
printf("ERROR: getrlimit failed\n");
|
||||||
|
@ -17,14 +17,14 @@
|
|||||||
|
|
||||||
#define MAX_CPU_NUM 1024
|
#define MAX_CPU_NUM 1024
|
||||||
|
|
||||||
static int* g_online_cpu_idxs;
|
static int *g_online_cpu_idxs;
|
||||||
|
|
||||||
int get_online_cpu() {
|
int get_online_cpu() {
|
||||||
int online_num = sysconf(_SC_NPROCESSORS_ONLN);
|
int online_num = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
cpu_set_t mask;
|
cpu_set_t mask;
|
||||||
int index = 0;
|
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);
|
CPU_ZERO(&mask);
|
||||||
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) < 0) {
|
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) < 0) {
|
||||||
THROW_ERROR("failed to call sched_getaffinity");
|
THROW_ERROR("failed to call sched_getaffinity");
|
||||||
@ -94,7 +94,7 @@ static int test_sched_xetaffinity_with_child_pid() {
|
|||||||
}
|
}
|
||||||
cpu_set_t mask;
|
cpu_set_t mask;
|
||||||
CPU_ZERO(&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);
|
int ret = posix_spawn(&child_pid, "/bin/getpid", NULL, NULL, NULL, NULL);
|
||||||
if (ret < 0 ) {
|
if (ret < 0 ) {
|
||||||
THROW_ERROR("spawn process error");
|
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() {
|
static int test_sched_getaffinity_with_null_buffer() {
|
||||||
unsigned char *buf = NULL;
|
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");
|
THROW_ERROR("check invalid buffer pointer(NULL) fail");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -185,7 +185,7 @@ static int test_sched_getaffinity_with_null_buffer() {
|
|||||||
|
|
||||||
static int test_sched_setaffinity_with_null_buffer() {
|
static int test_sched_setaffinity_with_null_buffer() {
|
||||||
unsigned char *buf = NULL;
|
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");
|
THROW_ERROR("check invalid buffer pointer(NULL) fail");
|
||||||
}
|
}
|
||||||
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));
|
||||||
@ -46,7 +48,7 @@ int connect_with_child(int port, int *child_pid) {
|
|||||||
|
|
||||||
char port_string[8];
|
char port_string[8];
|
||||||
sprintf(port_string, "%d", port);
|
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);
|
ret = posix_spawn(child_pid, "/bin/client", NULL, NULL, client_argv, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
close(listen_fd);
|
close(listen_fd);
|
||||||
@ -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);
|
||||||
@ -174,8 +181,8 @@ int server_connectionless_recvmsg() {
|
|||||||
THROW_ERROR("msg recvmsg mismatch");
|
THROW_ERROR("msg recvmsg mismatch");
|
||||||
} else {
|
} else {
|
||||||
inet_ntop(AF_INET, &clientaddr.sin_addr,
|
inet_ntop(AF_INET, &clientaddr.sin_addr,
|
||||||
buf, sizeof(buf));
|
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);
|
printf("from port %d and address %s\n", ntohs(clientaddr.sin_port), buf);
|
||||||
THROW_ERROR("client addr mismatch");
|
THROW_ERROR("client addr mismatch");
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
|
|
||||||
@ -250,14 +262,14 @@ int test_sendmsg_recvmsg_connectionless() {
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
int child_pid = 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);
|
ret = posix_spawn(&child_pid, "/bin/client", NULL, NULL, client_argv, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
THROW_ERROR("spawn client process error");
|
THROW_ERROR("spawn client process error");
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +338,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_poll_sockets),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
@ -67,8 +68,8 @@ int test_ip_socket() {
|
|||||||
|
|
||||||
int client_pid;
|
int client_pid;
|
||||||
int proc_num = DEFAULT_PROC_NUM;
|
int proc_num = DEFAULT_PROC_NUM;
|
||||||
char* client_argv[] = {"client", "127.0.0.1", "6667", NULL};
|
char *client_argv[] = {"client", "127.0.0.1", "6667", NULL};
|
||||||
for(int i=0; i<DEFAULT_PROC_NUM; ++i) {
|
for (int i = 0; i < DEFAULT_PROC_NUM; ++i) {
|
||||||
int ret = posix_spawn(&client_pid, "/bin/client", NULL, NULL, client_argv, NULL);
|
int ret = posix_spawn(&client_pid, "/bin/client", NULL, NULL, client_argv, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
@ -124,7 +125,7 @@ int test_ip_socket() {
|
|||||||
// Channel is ready to read.
|
// Channel is ready to read.
|
||||||
char buf[36];
|
char buf[36];
|
||||||
if ((read(events[i].data.fd, buf, sizeof buf)) != 0) {
|
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);
|
close_files(2, server_fd, epfd);
|
||||||
THROW_ERROR("msg mismatched");
|
THROW_ERROR("msg mismatched");
|
||||||
}
|
}
|
||||||
@ -159,6 +160,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_ip_socket),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
@ -99,13 +99,14 @@ int test_sigprocmask() {
|
|||||||
|
|
||||||
#define MAX_RECURSION_LEVEL 3
|
#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;
|
static volatile int recursion_level = 0;
|
||||||
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--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +135,7 @@ int test_raise() {
|
|||||||
|
|
||||||
int test_abort() {
|
int test_abort() {
|
||||||
pid_t child_pid;
|
pid_t child_pid;
|
||||||
char* child_argv[] = {"signal", "aborted_child", NULL};
|
char *child_argv[] = {"signal", "aborted_child", NULL};
|
||||||
int ret;
|
int ret;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
@ -170,7 +171,7 @@ static int aborted_child() {
|
|||||||
|
|
||||||
int test_kill() {
|
int test_kill() {
|
||||||
pid_t child_pid;
|
pid_t child_pid;
|
||||||
char* child_argv[] = {"signal", "killed_child", NULL};
|
char *child_argv[] = {"signal", "killed_child", NULL};
|
||||||
int ret;
|
int ret;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
@ -207,13 +208,13 @@ static int killed_child() {
|
|||||||
// Test catching and handling hardware exception
|
// 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");
|
printf("SIGFPE Caught\n");
|
||||||
assert(num == SIGFPE);
|
assert(num == SIGFPE);
|
||||||
assert(info->si_signo == SIGFPE);
|
assert(info->si_signo == SIGFPE);
|
||||||
|
|
||||||
ucontext_t* ucontext = _context;
|
ucontext_t *ucontext = _context;
|
||||||
mcontext_t* mcontext = &ucontext->uc_mcontext;
|
mcontext_t *mcontext = &ucontext->uc_mcontext;
|
||||||
// The faulty instruction should be `idiv %esi` (f7 fe)
|
// The faulty instruction should be `idiv %esi` (f7 fe)
|
||||||
mcontext->gregs[REG_RIP] += 2;
|
mcontext->gregs[REG_RIP] += 2;
|
||||||
|
|
||||||
@ -230,7 +231,7 @@ int div_maybe_zero(int x, int y) {
|
|||||||
int test_catch_fault() {
|
int test_catch_fault() {
|
||||||
#ifdef SGX_MODE_SIM
|
#ifdef SGX_MODE_SIM
|
||||||
printf("WARNING: Skip this test case as we do not support "
|
printf("WARNING: Skip this test case as we do not support "
|
||||||
"capturing hardware exception in SGX simulation mode\n");
|
"capturing hardware exception in SGX simulation mode\n");
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
// Set up a signal handler that handles divide-by-zero exception
|
// Set up a signal handler that handles divide-by-zero exception
|
||||||
@ -268,10 +269,10 @@ int test_catch_fault() {
|
|||||||
|
|
||||||
stack_t g_old_ss;
|
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;
|
static volatile int recursion_level = 0;
|
||||||
printf("Hello from SIGPIPE signal handler on the alternate signal stack (recursion_level = %d)\n",
|
printf("Hello from SIGPIPE signal handler on the alternate signal stack (recursion_level = %d)\n",
|
||||||
recursion_level);
|
recursion_level);
|
||||||
|
|
||||||
// save old_ss to check if we are on stack
|
// save old_ss to check if we are on stack
|
||||||
stack_t old_ss;
|
stack_t old_ss;
|
||||||
@ -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--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,8 +301,8 @@ int test_sigaltstack() {
|
|||||||
THROW_ERROR("failed to call sigaltstack");
|
THROW_ERROR("failed to call sigaltstack");
|
||||||
}
|
}
|
||||||
if (actual_ss.ss_size != expected_ss.ss_size
|
if (actual_ss.ss_size != expected_ss.ss_size
|
||||||
|| actual_ss.ss_sp != expected_ss.ss_sp
|
|| actual_ss.ss_sp != expected_ss.ss_sp
|
||||||
|| actual_ss.ss_flags != expected_ss.ss_flags) {
|
|| actual_ss.ss_flags != expected_ss.ss_flags) {
|
||||||
THROW_ERROR("failed to check the signal stack after set");
|
THROW_ERROR("failed to check the signal stack after set");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,14 +343,14 @@ int test_sigchld() {
|
|||||||
printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid());
|
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);
|
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");
|
printf("ERROR: failed to spawn a child process\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -367,16 +369,14 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_sigchld),
|
TEST_CASE(test_sigchld),
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,13 @@
|
|||||||
// Helper functions
|
// 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);
|
assert(tv->tv_sec >= 0 && tv->tv_nsec >= 0 && tv->tv_nsec < S);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
@ -34,22 +33,23 @@ static int timespec_cmp(const struct timespec *a, const struct timespec *b)
|
|||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return a->tv_nsec < b->tv_nsec ? -1 :
|
return a->tv_nsec < b->tv_nsec ? -1 :
|
||||||
(a->tv_nsec > b->tv_nsec ? 1 : 0);
|
(a->tv_nsec > b->tv_nsec ? 1 : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,15 +64,14 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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.
|
// The time obtained from Occlum is not very precise.
|
||||||
// Here we take 1 millisecond as the time precision of Occlum.
|
// Here we take 1 millisecond as the time precision of Occlum.
|
||||||
static struct timespec OS_TIME_PRECISION = {
|
static struct timespec OS_TIME_PRECISION = {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
int ret, child_pid, status;
|
int ret, child_pid, status;
|
||||||
printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid());
|
printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid());
|
||||||
|
|
||||||
|
@ -7,13 +7,13 @@
|
|||||||
|
|
||||||
#define NREPEATS 5000
|
#define NREPEATS 5000
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
struct timeval tv_start, tv_end;
|
struct timeval tv_start, tv_end;
|
||||||
|
|
||||||
gettimeofday(&tv_start, NULL);
|
gettimeofday(&tv_start, NULL);
|
||||||
for (unsigned long i = 0; i < NREPEATS; i++) {
|
for (unsigned long i = 0; i < NREPEATS; i++) {
|
||||||
int child_pid, status;
|
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);
|
printf("ERROR: failed to spawn (# of repeats = %lu)\n", i);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -29,7 +29,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
gettimeofday(&tv_end, NULL);
|
gettimeofday(&tv_end, NULL);
|
||||||
|
|
||||||
suseconds_t total_us = (tv_end.tv_sec - tv_start.tv_sec) * 1000000UL +
|
suseconds_t total_us = (tv_end.tv_sec - tv_start.tv_sec) * 1000000UL +
|
||||||
+ (tv_end.tv_usec - tv_start.tv_usec);
|
+ (tv_end.tv_usec - tv_start.tv_usec);
|
||||||
suseconds_t latency = total_us / NREPEATS;
|
suseconds_t latency = total_us / NREPEATS;
|
||||||
printf("Latency of spawn/exit = %lu us\n", latency);
|
printf("Latency of spawn/exit = %lu us\n", latency);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
static int create_file(const char *file_path) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00666;
|
int mode = 00666;
|
||||||
|
|
||||||
fd = open(file_path, flags, mode);
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
static int create_file(const char *file_path) {
|
||||||
int fd;
|
int fd;
|
||||||
int flags = O_RDONLY | O_CREAT| O_TRUNC;
|
int flags = O_RDONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00666;
|
int mode = 00666;
|
||||||
|
|
||||||
fd = open(file_path, flags, mode);
|
fd = open(file_path, flags, mode);
|
||||||
@ -87,7 +87,7 @@ static int __test_realpath(const char *file_path) {
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (snprintf(dirc, sizeof(dirc), "%s", file_path) < 0 ||
|
if (snprintf(dirc, sizeof(dirc), "%s", file_path) < 0 ||
|
||||||
snprintf(basec, sizeof(dirc), "%s", file_path) < 0) {
|
snprintf(basec, sizeof(dirc), "%s", file_path) < 0) {
|
||||||
THROW_ERROR("failed to copy file path");
|
THROW_ERROR("failed to copy file path");
|
||||||
}
|
}
|
||||||
dir_name = dirname(dirc);
|
dir_name = dirname(dirc);
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
volatile int g_int = 0;
|
volatile int g_int = 0;
|
||||||
static void use_int(int* a) {
|
static void use_int(int *a) {
|
||||||
g_int += *a;
|
g_int += *a;
|
||||||
}
|
}
|
||||||
|
|
||||||
__thread int tls_g_int = 0;
|
__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);
|
use_int(&tls_g_int);
|
||||||
return g_int;
|
return g_int;
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
const char* FILE_NAME = "root/test_filesystem_truncate.txt";
|
const char *FILE_NAME = "root/test_filesystem_truncate.txt";
|
||||||
const int TRUNC_LEN = 256;
|
const int TRUNC_LEN = 256;
|
||||||
const int TRUNC_LEN1 = 128;
|
const int TRUNC_LEN1 = 128;
|
||||||
const int MODE_MASK = 0777;
|
const int MODE_MASK = 0777;
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
int flags = O_WRONLY | O_CREAT| O_TRUNC;
|
int flags = O_WRONLY | O_CREAT | O_TRUNC;
|
||||||
int mode = 00666;
|
int mode = 00666;
|
||||||
int fd = open(FILE_NAME, flags, mode);
|
int fd = open(FILE_NAME, flags, mode);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
struct utsname name;
|
struct utsname name;
|
||||||
uname(&name);
|
uname(&name);
|
||||||
printf("sysname = %s\n", (const char*)&name.sysname);
|
printf("sysname = %s\n", (const char *)&name.sysname);
|
||||||
printf("nodename = %s\n", (const char*)&name.nodename);
|
printf("nodename = %s\n", (const char *)&name.nodename);
|
||||||
printf("release = %s\n", (const char*)&name.release);
|
printf("release = %s\n", (const char *)&name.release);
|
||||||
printf("version = %s\n", (const char*)&name.version);
|
printf("version = %s\n", (const char *)&name.version);
|
||||||
printf("machine = %s\n", (const char*)&name.machine);
|
printf("machine = %s\n", (const char *)&name.machine);
|
||||||
printf("domainname = %s\n", (const char*)&name.__domainname);
|
printf("domainname = %s\n", (const char *)&name.__domainname);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,8 @@ int create_connceted_sockets_default(int *sockets) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int verify_child_echo(int *connected_sockets) {
|
int verify_child_echo(int *connected_sockets) {
|
||||||
const char* child_prog = "/bin/hello_world";
|
const char *child_prog = "/bin/hello_world";
|
||||||
const char* child_argv[3] = { child_prog, ECHO_MSG, NULL };
|
const char *child_argv[3] = { child_prog, ECHO_MSG, NULL };
|
||||||
int child_pid;
|
int child_pid;
|
||||||
posix_spawn_file_actions_t file_actions;
|
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]);
|
posix_spawn_file_actions_addclose(&file_actions, connected_sockets[1]);
|
||||||
|
|
||||||
if (posix_spawn(&child_pid, child_prog, &file_actions,
|
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");
|
THROW_ERROR("failed to spawn a child process");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,24 +124,24 @@ int test_multiple_socketpairs() {
|
|||||||
int i;
|
int i;
|
||||||
int ret = 0;
|
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) {
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets[i]) < 0) {
|
||||||
THROW_ERROR("opening stream socket pair");
|
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;
|
ret = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(verify_connection(sockets[i][1], sockets[i][0]) < 0) {
|
if (verify_connection(sockets[i][1], sockets[i][0]) < 0) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i--;
|
i--;
|
||||||
cleanup:
|
cleanup:
|
||||||
for(; i >= 0; i--){
|
for (; i >= 0; i--) {
|
||||||
close(sockets[i][0]);
|
close(sockets[i][0]);
|
||||||
close(sockets[i][1]);
|
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 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);
|
||||||
|
|
||||||
@ -180,6 +181,6 @@ static test_case_t test_cases[] = {
|
|||||||
TEST_CASE(test_multiple_socketpairs),
|
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));
|
return test_suite_run(test_cases, ARRAY_SIZE(test_cases));
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ int create_client_socket() {
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
size_t buf_size, total_bytes;
|
size_t buf_size, total_bytes;
|
||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
buf_size = atol(argv[1]);
|
buf_size = atol(argv[1]);
|
||||||
@ -75,7 +75,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
total_bytes = atol(argv[2]);
|
total_bytes = atol(argv[2]);
|
||||||
} else {
|
} else {
|
||||||
// BUG: throughput fall down when buf_size > 65536
|
// 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("buf_size = 0x%zx\n", buf_size);
|
||||||
printf("total_bytes = 0x%zx\n", total_bytes);
|
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);
|
posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd);
|
||||||
|
|
||||||
int child_pid;
|
int child_pid;
|
||||||
extern char ** environ;
|
extern char **environ;
|
||||||
char* new_argv[] = {"/bin/data_sink", NULL};
|
char *new_argv[] = {"/bin/data_sink", NULL};
|
||||||
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
|
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
|
||||||
NULL, new_argv, environ) < 0) {
|
NULL, new_argv, environ) < 0) {
|
||||||
printf("ERROR: failed to spawn a child process\n");
|
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 */
|
/* 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};
|
||||||
// Get the absolute path of the executable
|
// Get the absolute path of the executable
|
||||||
readlink("/proc/self/exe", enclave_path, sizeof(enclave_path));
|
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
|
// Get the absolute path of the enclave
|
||||||
strncat(enclave_path, "/../lib/", sizeof(enclave_path));
|
strncat(enclave_path, "/../lib/", sizeof(enclave_path));
|
||||||
strncat(enclave_path, ENCLAVE_FILENAME, sizeof(enclave_path));
|
strncat(enclave_path, ENCLAVE_FILENAME, sizeof(enclave_path));
|
||||||
return (const char*)enclave_path;
|
return (const char *)enclave_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the enclave:
|
/* 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 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;
|
||||||
@ -162,11 +162,11 @@ static int initialize_enclave(void)
|
|||||||
const char *home_dir = getpwuid(getuid())->pw_dir;
|
const char *home_dir = getpwuid(getuid())->pw_dir;
|
||||||
|
|
||||||
if (home_dir != NULL &&
|
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 */
|
/* compose the token path */
|
||||||
strncpy(token_path, home_dir, strlen(home_dir));
|
strncpy(token_path, home_dir, strlen(home_dir));
|
||||||
strncat(token_path, "/", strlen("/"));
|
strncat(token_path, "/", strlen("/"));
|
||||||
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
|
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
|
||||||
} else {
|
} else {
|
||||||
/* if token path is too long or $HOME is NULL */
|
/* if token path is too long or $HOME is NULL */
|
||||||
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
|
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 */
|
/* 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;
|
||||||
}
|
}
|
||||||
@ -226,15 +228,15 @@ void ocall_eprint(const char *str) {
|
|||||||
fprintf(stderr, "%s", str);
|
fprintf(stderr, "%s", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ocall_open(const char* path) {
|
int ocall_open(const char *path) {
|
||||||
return open(path, O_RDONLY);
|
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);
|
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);
|
return write(fd, buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,25 +264,21 @@ static void print_help(void) {
|
|||||||
#define CMD_SHOW_MAC 2
|
#define CMD_SHOW_MAC 2
|
||||||
|
|
||||||
static int parse_args(
|
static int parse_args(
|
||||||
/* inputs */
|
/* inputs */
|
||||||
int argc,
|
int argc,
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,7 +293,7 @@ static int parse_args(
|
|||||||
int SGX_CDECL main(int argc, char *argv[]) {
|
int SGX_CDECL main(int argc, char *argv[]) {
|
||||||
/* Parse arguments */
|
/* Parse arguments */
|
||||||
int arg_command = CMD_ERROR;
|
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) {
|
if (parse_args(argc, argv, &arg_command, &arg_file_path) < 0) {
|
||||||
print_help();
|
print_help();
|
||||||
return -1;
|
return -1;
|
||||||
@ -309,14 +307,14 @@ int SGX_CDECL main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/* Do the command */
|
/* Do the command */
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
switch(arg_command){
|
switch (arg_command) {
|
||||||
case CMD_PROTECT: {
|
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;
|
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);
|
strncpy(output_path, input_path, output_path_len);
|
||||||
strncat(output_path, output_ext, output_path_len);
|
strncat(output_path, output_ext, output_path_len);
|
||||||
|
|
||||||
@ -327,7 +325,7 @@ int SGX_CDECL main(int argc, char *argv[]) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CMD_SHOW: {
|
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)) {
|
if (ecall_show(global_eid, &ret, input_path)) {
|
||||||
fprintf(stderr, "Error: ecall failed\n");
|
fprintf(stderr, "Error: ecall failed\n");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
@ -335,7 +333,7 @@ int SGX_CDECL main(int argc, char *argv[]) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CMD_SHOW_MAC: {
|
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)) {
|
if (ecall_show_mac(global_eid, &ret, input_path)) {
|
||||||
fprintf(stderr, "Error: ecall failed\n");
|
fprintf(stderr, "Error: ecall failed\n");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#define PRINTF_BUFSIZE 512
|
#define PRINTF_BUFSIZE 512
|
||||||
|
|
||||||
static int printf(const char* fmt, ...) {
|
static int printf(const char *fmt, ...) {
|
||||||
char buf[PRINTF_BUFSIZE] = {0};
|
char buf[PRINTF_BUFSIZE] = {0};
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
@ -20,7 +20,7 @@ static int printf(const char* fmt, ...) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int eprintf(const char* fmt, ...) {
|
static int eprintf(const char *fmt, ...) {
|
||||||
char buf[PRINTF_BUFSIZE] = {0};
|
char buf[PRINTF_BUFSIZE] = {0};
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
@ -30,29 +30,29 @@ static int eprintf(const char* fmt, ...) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int open(const char* path) {
|
static int open(const char *path) {
|
||||||
int fd = 0;
|
int fd = 0;
|
||||||
ocall_open(&fd, path);
|
ocall_open(&fd, path);
|
||||||
return fd;
|
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;
|
ssize_t ret = 0;
|
||||||
ocall_read(&ret, fd, buf, size);
|
ocall_read(&ret, fd, buf, size);
|
||||||
return ret;
|
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;
|
ssize_t ret = 0;
|
||||||
ocall_write(&ret, fd, buf, size);
|
ocall_write(&ret, fd, buf, size);
|
||||||
return ret;
|
return ret;
|
||||||
@ -68,9 +68,9 @@ static int close(int fd) {
|
|||||||
// ECalls
|
// 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;
|
int input_file = -1;
|
||||||
SGX_FILE* output_file = NULL;
|
SGX_FILE *output_file = NULL;
|
||||||
size_t len;
|
size_t len;
|
||||||
char buf[4 * 1024];
|
char buf[4 * 1024];
|
||||||
|
|
||||||
@ -107,8 +107,8 @@ on_error:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ecall_show(const char* protected_file_path) {
|
int ecall_show(const char *protected_file_path) {
|
||||||
SGX_FILE* protected_file = NULL;
|
SGX_FILE *protected_file = NULL;
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
char buf[4 * 1024];
|
char buf[4 * 1024];
|
||||||
|
|
||||||
@ -136,8 +136,8 @@ on_error:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ecall_show_mac(const char* protected_file_path) {
|
int ecall_show_mac(const char *protected_file_path) {
|
||||||
SGX_FILE* protected_file = NULL;
|
SGX_FILE *protected_file = NULL;
|
||||||
sgx_aes_gcm_128bit_tag_t mac = { 0 };
|
sgx_aes_gcm_128bit_tag_t mac = { 0 };
|
||||||
|
|
||||||
protected_file = sgx_fopen_integrity_only(protected_file_path, "r");
|
protected_file = sgx_fopen_integrity_only(protected_file_path, "r");
|
||||||
|
Loading…
Reference in New Issue
Block a user