diff --git a/src/libos/src/process/wait.rs b/src/libos/src/process/wait.rs index e736e9d2..7cf4d37f 100644 --- a/src/libos/src/process/wait.rs +++ b/src/libos/src/process/wait.rs @@ -40,7 +40,7 @@ impl Waiter pub fn wait_on(&self) -> R { if !self.inner.lock().unwrap().has_waken { unsafe { - sgx_thread_wait_untrusted_event_ocall(self.thread); + wait_event(self.thread); } } @@ -93,27 +93,47 @@ impl WaitQueue waiter_i.unwrap() }; let del_waiter = waiters.swap_remove(del_waiter_i); - unsafe { - sgx_thread_set_untrusted_event_ocall(del_waiter.thread); - } + set_event(del_waiter.thread); 1 } } +fn wait_event(thread: *const c_void) { + let mut ret : c_int = 0; + let mut sgx_ret : c_int = 0; + unsafe { + sgx_ret = sgx_thread_wait_untrusted_event_ocall(&mut ret as *mut c_int, thread); + } + if ret != 0 || sgx_ret != 0 { + panic!("ERROR: OCall failed!"); + } +} + +fn set_event(thread: *const c_void) { + let mut ret : c_int = 0; + let mut sgx_ret : c_int = 0; + unsafe { + sgx_ret = sgx_thread_set_untrusted_event_ocall(&mut ret as *mut c_int, thread); + } + if ret != 0 || sgx_ret != 0 { + panic!("ERROR: OCall failed!"); + } +} + extern { fn sgx_thread_get_self() -> *const c_void; /* Go outside and wait on my untrusted event */ - fn sgx_thread_wait_untrusted_event_ocall(self_thread: *const c_void) -> c_int; + fn sgx_thread_wait_untrusted_event_ocall(ret: *mut c_int, self_thread: *const c_void) -> c_int; /* Wake a thread waiting on its untrusted event */ - fn sgx_thread_set_untrusted_event_ocall(waiter_thread: *const c_void) -> c_int; + fn sgx_thread_set_untrusted_event_ocall(ret: *mut c_int, waiter_thread: *const c_void) -> c_int; /* Wake a thread waiting on its untrusted event, and wait on my untrusted event */ - fn sgx_thread_setwait_untrusted_events_ocall( + fn sgx_thread_setwait_untrusted_events_ocall(ret: *mut c_int, waiter_thread: *const c_void, self_thread: *const c_void) -> c_int; /* Wake multiple threads waiting on their untrusted events */ - fn sgx_thread_set_multiple_untrusted_events_ocall( + fn sgx_thread_set_multiple_untrusted_events_ocall(ret: *mut c_int, waiter_threads: *const *const c_void, total: size_t ) -> c_int; } diff --git a/src/libos/src/syscall/mod.rs b/src/libos/src/syscall/mod.rs index 602c7275..4aa3c23e 100644 --- a/src/libos/src/syscall/mod.rs +++ b/src/libos/src/syscall/mod.rs @@ -378,7 +378,9 @@ pub extern "C" fn occlum_spawn( } fn do_wait4(pid: c_int, _exit_status: *mut c_int) -> Result { - check_mut_ptr_from_user(_exit_status)?; + if _exit_status != 0 as *mut c_int { + check_mut_ptr_from_user(_exit_status)?; + } let child_process_filter = match pid { pid if pid < -1 => { @@ -401,7 +403,9 @@ fn do_wait4(pid: c_int, _exit_status: *mut c_int) -> Result { let mut exit_status = 0; match process::do_wait4(&child_process_filter, &mut exit_status) { Ok(pid) => { - unsafe { *_exit_status = exit_status; } + if _exit_status != 0 as *mut c_int { + unsafe { *_exit_status = exit_status; } + } Ok(pid) } Err(e) => { diff --git a/test/Makefile b/test/Makefile index 9c33b5cc..0ad8fa51 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,7 @@ CUR_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) PROJECT_DIR := $(realpath $(CUR_DIR)/../) -TEST_SUITES := empty hello_world malloc getpid +TEST_SUITES := empty hello_world malloc getpid spawn BUILD_TEST_SUITES := $(TEST_SUITES:%=%) RUN_TEST_SUITES := $(TEST_SUITES:%=run-%) CLEAN_TEST_SUITES := $(TEST_SUITES:%=clean-%) diff --git a/test/getpid/main.c b/test/getpid/main.c index 668b6d97..8636dcf3 100644 --- a/test/getpid/main.c +++ b/test/getpid/main.c @@ -3,7 +3,6 @@ #include int main(void) { - printf("pid = %d\n", getpid()); - printf("ppid = %d\n", getppid()); + printf("Run a new process with pid = %d and ppid = %d\n", getpid(), getppid()); return 0; } diff --git a/test/spawn/Makefile b/test/spawn/Makefile new file mode 100644 index 00000000..b71a4d49 --- /dev/null +++ b/test/spawn/Makefile @@ -0,0 +1,4 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := diff --git a/test/spawn/main.c b/test/spawn/main.c new file mode 100644 index 00000000..60f1b18b --- /dev/null +++ b/test/spawn/main.c @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(void) { + int ret, child_pid, status; + printf("Run a parent process has pid = %d and ppid = %d\n", getpid(), getppid()); + + ret = syscall(__NR_spawn, &child_pid, "getpid/bin.encrypted", NULL, NULL); + if (ret < 0) { + printf("ERROR: failed to spawn a child process\n"); + return -1; + } + printf("Spawn a new proces successfully (pid = %d)\n", child_pid); + + ret = syscall(__NR_wait4, -1, &status, 0); + if (ret < 0) { + printf("ERROR: failed to wait4 the child process\n"); + return -1; + } + printf("Child process exited with status = %d\n", status); + + return 0; +}