diff --git a/src/libos/Enclave_config.xml b/src/libos/Enclave_config.xml index 1473a769..f18079c4 100644 --- a/src/libos/Enclave_config.xml +++ b/src/libos/Enclave_config.xml @@ -2,7 +2,7 @@ 0 0 - 0x400000 + 0x80000 0x1000000 8 1 diff --git a/src/libos/src/mm.rs b/src/libos/src/mm.rs index 173efb18..d79cb039 100644 --- a/src/libos/src/mm.rs +++ b/src/libos/src/mm.rs @@ -27,9 +27,9 @@ impl MemObj { unsafe { memset(mem_ptr, 0 as c_int, mem_size as size_t) }; Ok(MemObj { - mem_ptr: mem_ptr, - mem_size: mem_size, - mem_align: mem_align, + mem_ptr, + mem_size, + mem_align, }) } diff --git a/src/libos/src/process.rs b/src/libos/src/process.rs index 1936709c..8a98039e 100644 --- a/src/libos/src/process.rs +++ b/src/libos/src/process.rs @@ -91,6 +91,17 @@ pub fn set_current(process: &ProcessRef) { }); } +pub fn reset_current() { + let mut process_ptr = 0 as *const SgxMutex; + _CURRENT_PROCESS_PTR.with(|cp| { + process_ptr = cp.get(); + cp.set(0 as *const SgxMutex); + }); + + // Prevent memory leakage + unsafe { drop(Arc::from_raw(process_ptr)); } +} + pub fn get_current() -> &'static SgxMutex { let mut process_ptr : *const SgxMutex = 0 as *const SgxMutex; _CURRENT_PROCESS_PTR.with(|cp| { @@ -124,6 +135,7 @@ pub fn do_wait4(child_pid: u32, exit_code: &mut i32) -> Result<(), &'static str> *exit_code = guard.exit_code; break; } + del_from_pid_table(guard.pid); drop(guard); } Ok(()) @@ -133,16 +145,26 @@ pub fn run_task() -> Result<(), &'static str> { let new_process : ProcessRef = dequeue_new_process().ok_or("No new process to run")?; set_current(&new_process); + let pid; let new_task; { let guard = new_process.lock().unwrap(); let process : &Process = &guard; - println!("Run process: {:#x?}", process); + pid = process.pid; + //println!("Run process: {:#x?}", process); + println!("Run process (pid = {})", process.pid); new_task = &process.task as *const Task }; unsafe { do_run_task(new_task as *const Task); } + // Init process does not have any parent, so it has to release itself + if pid == 1 { + del_from_pid_table(1); + } + + reset_current(); + Ok(()) } @@ -152,6 +174,7 @@ fn open_elf>(path: &P) -> io::Result> { let mut elf_buf = Vec::::new(); elf_file.read_to_end(&mut elf_buf); + drop(elf_file); Ok(elf_buf) } @@ -251,7 +274,6 @@ impl Process { impl Drop for Process { fn drop(&mut self) { - del_from_pid_table(self.pid); free_pid(self.pid); } } diff --git a/src/libos/src/vma.rs b/src/libos/src/vma.rs index abc3be9a..684c0c86 100644 --- a/src/libos/src/vma.rs +++ b/src/libos/src/vma.rs @@ -141,7 +141,10 @@ pub fn mprotect_batch(vma_list: &[&Vma]) let size = (vma.mem_end - vma.mem_begin) as size_t; let perms = vma.mem_flags.0 as uint64_t; let status = unsafe { - trts_mprotect(start, size, perms) + //TODO: use proper permissions + //TODO: reset the permissions when drop VMA + //trts_mprotect(start, size, perms) + trts_mprotect(start, size, (PERM_R | PERM_W | PERM_X) as uint64_t) }; if (status != sgx_status_t::SGX_SUCCESS) { return Err("trts_mprotect failed"); @@ -151,7 +154,7 @@ pub fn mprotect_batch(vma_list: &[&Vma]) } -#[derive(Copy, Clone, Debug, Default)] +#[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct Perms(pub u32); pub const PERM_R: u32 = 0x1; diff --git a/test/spawn_and_wait4_raw/main.c b/test/spawn_and_wait4_raw/main.c index e9417c8b..92ff5417 100644 --- a/test/spawn_and_wait4_raw/main.c +++ b/test/spawn_and_wait4_raw/main.c @@ -7,18 +7,23 @@ static void print_ok(void) { __rusgx_write(1, success_str_buf, success_str_size); } +#define NUM_CHILDREN 100 + void _start(void) { - int ret = 0; - int pid = 0; + for (int ci = 0; ci < NUM_CHILDREN; ci++) { + int ret = 0; + int pid = 0; - ret = __rusgx_spawn(&pid, "hello_world_raw/bin.encrypted", NULL, NULL); - if (ret < 0) { __rusgx_exit(0); } + ret = __rusgx_spawn(&pid, "hello_world_raw/bin.encrypted", NULL, NULL); + if (ret < 0) { __rusgx_exit(0); } + print_ok(); - int status; - ret = __rusgx_wait4(pid, &status, 0); - if (ret < 0) { __rusgx_exit(0); } + int status; + ret = __rusgx_wait4(pid, &status, 0); + if (ret < 0) { __rusgx_exit(0); } - print_ok(); + //print_ok(); + } __rusgx_exit(0); }