Skip to content

Commit

Permalink
[PAL/Linux-SGX] Implement status reporting from clone_thread
Browse files Browse the repository at this point in the history
Previously, `clone_thread()` only returned errors if the current-thread (parent)
side of cloning failed. This commit adds error reporting also in the case when
the newly spawned thread (child) side of cloning fails.

Signed-off-by: Marcelina Kościelnicka <mwk@0x04.net>
  • Loading branch information
mwkmwkmwk committed Nov 9, 2023
1 parent 0f5df73 commit d3e9b32
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pal/src/host/linux-sgx/host_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,16 @@ int pal_thread_init(void* tcbptr) {
return 0;
}

__atomic_store_n(tcb->start_status_ptr, 0, __ATOMIC_RELAXED);

/* not-first (child) thread, start it */
ecall_thread_start();

unmap_tcs();
ret = 0;
out:
if (ret != 0)
__atomic_store_n(tcb->start_status_ptr, ret, __ATOMIC_RELAXED);
return ret;
}

Expand Down Expand Up @@ -271,8 +275,9 @@ int clone_thread(void) {
/* align child_stack to 16 */
child_stack_top = ALIGN_DOWN_PTR(child_stack_top, 16);

// TODO: pal_thread_init() may fail during initialization (e.g. on TCS exhaustion), we should
// check its result (but this happens asynchronously, so it's not trivial to do).
int start_status = 1;
tcb->start_status_ptr = &start_status;

ret = clone(pal_thread_init, child_stack_top,
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM | CLONE_THREAD | CLONE_SIGHAND,
tcb, /*parent_tid=*/NULL, /*tls=*/NULL, /*child_tid=*/NULL, thread_exit);
Expand All @@ -281,7 +286,11 @@ int clone_thread(void) {
DO_SYSCALL(munmap, stack, THREAD_STACK_SIZE + ALT_STACK_SIZE);
return ret;
}
return 0;

while ((ret = __atomic_load_n(&start_status, __ATOMIC_RELAXED)) == 1)
CPU_RELAX();

return ret;
}

int get_tid_from_tcs(void* tcs) {
Expand Down
1 change: 1 addition & 0 deletions pal/src/host/linux-sgx/pal_tcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ typedef struct pal_host_tcb {
atomic_ulong async_signal_cnt; /* # of async signals, corresponds to # of SIGINT/SIGCONT/.. */
uint64_t profile_sample_time; /* last time sgx_profile_sample() recorded a sample */
int32_t last_async_event; /* last async signal, reported to the enclave on ocall return */
int* start_status_ptr; /* pointer to return value of clone_thread */
} PAL_HOST_TCB;

extern void pal_host_tcb_init(PAL_HOST_TCB* tcb, void* stack, void* alt_stack);
Expand Down

0 comments on commit d3e9b32

Please sign in to comment.