From 71d119181e4b434a20161d21dd6d9285cfb75905 Mon Sep 17 00:00:00 2001 From: "Tate, Hongliang Tian" Date: Tue, 8 Jan 2019 20:08:01 +0800 Subject: [PATCH] Support argc and argv for child process after spawn --- src/Enclave.edl | 2 +- src/libos/src/util/mem_util.rs | 13 +++++++++++-- src/pal/pal.c | 2 +- test/argv/main.c | 2 +- test/empty/main.c | 2 +- test/file/main.c | 2 +- test/getpid/main.c | 2 +- test/hello_world/main.c | 12 ++++++++---- test/malloc/main.c | 2 +- test/pipe/Makefile | 2 +- test/pipe/main.c | 11 +++++++---- test/spawn/main.c | 2 +- test/time/main.c | 2 +- 13 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/Enclave.edl b/src/Enclave.edl index d95b33af..e3a2014e 100644 --- a/src/Enclave.edl +++ b/src/Enclave.edl @@ -7,7 +7,7 @@ enclave { trusted { /* define ECALLs here. */ - public int libos_boot([in, string] const char* executable_path, [user_check] char** argv); + public int libos_boot([in, string] const char* executable_path, [user_check] const char** argv); public int libos_run(void); }; diff --git a/src/libos/src/util/mem_util.rs b/src/libos/src/util/mem_util.rs index 60bfc2e4..74e6c669 100644 --- a/src/libos/src/util/mem_util.rs +++ b/src/libos/src/util/mem_util.rs @@ -45,11 +45,20 @@ pub mod from_user { -> Result, Error> { let mut cstrings = Vec::new(); + if user_ptr == ptr::null() { return Ok(cstrings); } + let mut user_ptr = user_ptr; - while user_ptr != ptr::null() { - let cstr_ptr = unsafe { *user_ptr }; + loop { + check_ptr(user_ptr); + let cstr_ptr = { + let cstr_ptr = unsafe { *user_ptr }; + if cstr_ptr == ptr::null() { break; } + check_ptr(cstr_ptr); + cstr_ptr + }; let cstring = clone_cstring_safely(cstr_ptr)?; cstrings.push(cstring); + user_ptr = unsafe { user_ptr.offset(1) }; } Ok(cstrings) diff --git a/src/pal/pal.c b/src/pal/pal.c index 91104f6a..c8399074 100644 --- a/src/pal/pal.c +++ b/src/pal/pal.c @@ -207,7 +207,7 @@ void ocall_gettimeofday(long* seconds, long* microseconds) { /* Application entry */ -int SGX_CDECL main(int argc, char *argv[]) +int SGX_CDECL main(int argc, const char *argv[]) { sgx_status_t sgx_ret = SGX_SUCCESS; int status = 0; diff --git a/test/argv/main.c b/test/argv/main.c index e372d2e4..ed19366e 100644 --- a/test/argv/main.c +++ b/test/argv/main.c @@ -10,7 +10,7 @@ const char* expected_argv[EXPECTED_ARGC] = { EXPECTED_ARG3, }; -int main(int argc, char* argv[]) { +int main(int argc, const char* argv[]) { if (argc != EXPECTED_ARGC) { printf("ERROR: expect %d arguments, but %d are given\n", EXPECTED_ARGC, argc); return -1; diff --git a/test/empty/main.c b/test/empty/main.c index 9b6bdc2e..11a3d8fc 100644 --- a/test/empty/main.c +++ b/test/empty/main.c @@ -1,3 +1,3 @@ -int main(void) { +int main(int argc, const char* argv[]) { return 0; } diff --git a/test/file/main.c b/test/file/main.c index 23c8d9d2..dc649ba1 100644 --- a/test/file/main.c +++ b/test/file/main.c @@ -5,7 +5,7 @@ #include #include -int main() { +int main(int argc, const char* argv[]) { const char* file_name = "tmp.txt"; int fd, flags, mode, len; const char* write_msg = "Hello World\n"; diff --git a/test/getpid/main.c b/test/getpid/main.c index 8636dcf3..4479952f 100644 --- a/test/getpid/main.c +++ b/test/getpid/main.c @@ -2,7 +2,7 @@ #include #include -int main(void) { +int main(int argc, const char* argv[]) { printf("Run a new process with pid = %d and ppid = %d\n", getpid(), getppid()); return 0; } diff --git a/test/hello_world/main.c b/test/hello_world/main.c index 45176c02..82872cff 100644 --- a/test/hello_world/main.c +++ b/test/hello_world/main.c @@ -2,9 +2,13 @@ #include #include -static const char* msg = "Hello World\n"; - -int main() { - printf("%s", msg); +int main(int argc, const char* argv[]) { + if (argc <= 1) { + printf("Hello World!\n"); + } + else { + const char* echo_msg = argv[1]; + printf("%s\n", echo_msg); + } return 0; } diff --git a/test/malloc/main.c b/test/malloc/main.c index 93188911..a3ec42c1 100644 --- a/test/malloc/main.c +++ b/test/malloc/main.c @@ -4,7 +4,7 @@ #define MAX_SIZE (1*1024*1024) #define MIN_SIZE 8 -int main(void) { +int main(int argc, const char* argv[]) { printf("Testing malloc and free...\n"); for (size_t buf_size = MIN_SIZE; buf_size <= MAX_SIZE; buf_size *= 4) { printf("buf_size = %lu\n", buf_size); diff --git a/test/pipe/Makefile b/test/pipe/Makefile index 9e1b6dec..8c5a2fb4 100644 --- a/test/pipe/Makefile +++ b/test/pipe/Makefile @@ -1,5 +1,5 @@ include ../test_common.mk -EXTRA_C_FLAGS := +EXTRA_C_FLAGS := -Wno-incompatible-pointer-types-discards-qualifiers EXTRA_LINK_FLAGS := BIN_ARGS := diff --git a/test/pipe/main.c b/test/pipe/main.c index 5b31de6e..b3bc7da0 100644 --- a/test/pipe/main.c +++ b/test/pipe/main.c @@ -6,7 +6,7 @@ #include #include -int main(void) { +int main(int argc, const char* argv[]) { // XXX: this is a hack! remove this in the future void* ptr = malloc(64); free(ptr); @@ -24,15 +24,18 @@ int main(void) { posix_spawn_file_actions_adddup2(&file_actions, pipe_wr_fd, STDOUT_FILENO); posix_spawn_file_actions_addclose(&file_actions, pipe_rd_fd); + const char* msg = "Echo!\n"; + const char* child_prog = "hello_world/bin.encrypted"; + const char* child_argv[3] = { child_prog, msg, NULL }; int child_pid; - if (posix_spawn(&child_pid, "hello_world/bin.encrypted", &file_actions, - NULL, NULL, NULL) < 0) { + if (posix_spawn(&child_pid, child_prog, &file_actions, + NULL, child_argv, NULL) < 0) { printf("ERROR: failed to spawn a child process\n"); return -1; } close(pipe_wr_fd); - const char* expected_str = "Hello World\n"; + const char* expected_str = msg; size_t expected_len = strlen(expected_str); char actual_str[32] = {0}; ssize_t actual_len; diff --git a/test/spawn/main.c b/test/spawn/main.c index 8157d67c..f098fc9d 100644 --- a/test/spawn/main.c +++ b/test/spawn/main.c @@ -4,7 +4,7 @@ #include #include -int main(void) { +int main(int argc, const char* argv[]) { int ret, child_pid, status; printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid()); diff --git a/test/time/main.c b/test/time/main.c index 367522f8..aa7d253a 100644 --- a/test/time/main.c +++ b/test/time/main.c @@ -1,7 +1,7 @@ #include #include -int main() { +int main(int argc, const char* argv[]) { struct timeval tv; gettimeofday(&tv, NULL); printf("sec = %lu, usec = %lu\n", tv.tv_sec, tv.tv_usec);