From 1902c3862ba4976efb6e79d3092d965ebdbd8407 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Mon, 6 Jan 2020 12:39:31 -0800 Subject: [PATCH] [LibOS] Use set_cur_thread() to associate shim_tcb_t and shim_thread Uniformly use set_cur_thread() to associate shim_tcb_t to struct shim_thread. Remove unnecessary logic from init_fs_base() and update_fs_base(). --- LibOS/shim/include/shim_thread.h | 3 ++- LibOS/shim/src/bookkeep/shim_thread.c | 13 ++++++++++--- LibOS/shim/src/ipc/shim_ipc_helper.c | 1 + LibOS/shim/src/shim_async.c | 1 + LibOS/shim/src/shim_init.c | 17 +---------------- LibOS/shim/src/sys/shim_clone.c | 3 ++- 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/LibOS/shim/include/shim_thread.h b/LibOS/shim/include/shim_thread.h index 4c347cdd..2c360a55 100644 --- a/LibOS/shim/include/shim_thread.h +++ b/LibOS/shim/include/shim_thread.h @@ -205,7 +205,8 @@ void set_cur_thread (struct shim_thread * thread) if (tcb->tid != tid) { tcb->tid = tid; - debug_setprefix(tcb); + if (tcb->debug_buf) + debug_setprefix(tcb); } } diff --git a/LibOS/shim/src/bookkeep/shim_thread.c b/LibOS/shim/src/bookkeep/shim_thread.c index f5013ace..c0715d5a 100644 --- a/LibOS/shim/src/bookkeep/shim_thread.c +++ b/LibOS/shim/src/bookkeep/shim_thread.c @@ -737,7 +737,13 @@ BEGIN_CP_FUNC(running_thread) if (thread->shim_tcb) { ptr_t toff = ADD_CP_OFFSET(sizeof(shim_tcb_t)); new_thread->shim_tcb = (void *)(base + toff); - memcpy(new_thread->shim_tcb, thread->shim_tcb, sizeof(shim_tcb_t)); + struct shim_tcb* new_tcb = new_thread->shim_tcb; + memcpy(new_tcb, thread->shim_tcb, sizeof(*new_tcb)); + /* don't export stale pointers */ + new_tcb->self = NULL; + new_tcb->tp = NULL; + new_tcb->context.next = NULL; + new_tcb->debug_buf = NULL; } } END_CP_FUNC(running_thread) @@ -752,6 +758,7 @@ static int resume_wrapper (void * param) shim_tcb_init(); shim_tcb_t* saved_tcb = thread->shim_tcb; assert(saved_tcb->context.regs && saved_tcb->context.regs->rsp); + set_cur_thread(thread); unsigned long fs_base = saved_tcb->context.fs_base; assert(fs_base); init_fs_base(fs_base, thread); @@ -805,6 +812,7 @@ BEGIN_RS_FUNC(running_thread) shim_tcb_t* tcb = shim_get_tcb(); memcpy(tcb, saved_tcb, sizeof(*tcb)); __shim_tcb_init(tcb); + set_cur_thread(thread); assert(tcb->context.regs && tcb->context.regs->rsp); init_fs_base(tcb->context.fs_base, thread); @@ -822,9 +830,8 @@ BEGIN_RS_FUNC(running_thread) * shim_tcb = NULL * in_vm = false */ - thread->shim_tcb = shim_get_tcb(); - debug_setbuf(thread->shim_tcb, false); set_cur_thread(thread); + debug_setbuf(thread->shim_tcb, false); } thread->in_vm = thread->is_alive = true; diff --git a/LibOS/shim/src/ipc/shim_ipc_helper.c b/LibOS/shim/src/ipc/shim_ipc_helper.c index 9d19a105..ac91059d 100644 --- a/LibOS/shim/src/ipc/shim_ipc_helper.c +++ b/LibOS/shim/src/ipc/shim_ipc_helper.c @@ -793,6 +793,7 @@ static void shim_ipc_helper_prepare(void* arg) { return; shim_tcb_init(); + set_cur_thread(self); init_fs_base(0, self); debug_setbuf(shim_get_tcb(), true); diff --git a/LibOS/shim/src/shim_async.c b/LibOS/shim/src/shim_async.c index 6851b807..04ddd6bc 100644 --- a/LibOS/shim/src/shim_async.c +++ b/LibOS/shim/src/shim_async.c @@ -145,6 +145,7 @@ static void shim_async_helper(void * arg) { return; shim_tcb_init(); + set_cur_thread(self); init_fs_base(0, self); debug_setbuf(shim_get_tcb(), true); diff --git a/LibOS/shim/src/shim_init.c b/LibOS/shim/src/shim_init.c index a764be5a..73b253ab 100644 --- a/LibOS/shim/src/shim_init.c +++ b/LibOS/shim/src/shim_init.c @@ -202,17 +202,8 @@ bool lock_enabled; /* This function is used to allocate tls before interpreter start running */ void init_fs_base (unsigned long fs_base, struct shim_thread * thread) { + __UNUSED(thread); shim_tcb_t * shim_tcb = shim_get_tcb(); - - if (thread) { - thread->shim_tcb = shim_tcb; - shim_tcb->tp = thread; - shim_tcb->tid = thread->tid; - } else { - shim_tcb->tp = NULL; - shim_tcb->tid = 0; - } - shim_tcb->context.fs_base = fs_base; DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base); assert(shim_tcb_check_canary()); @@ -221,12 +212,6 @@ void init_fs_base (unsigned long fs_base, struct shim_thread * thread) void update_fs_base (unsigned long fs_base) { shim_tcb_t * shim_tcb = shim_get_tcb(); - - struct shim_thread * thread = shim_tcb->tp; - if (thread) { - thread->shim_tcb = shim_tcb; - } - shim_tcb->context.fs_base = fs_base; DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base); assert(shim_tcb_check_canary()); diff --git a/LibOS/shim/src/sys/shim_clone.c b/LibOS/shim/src/sys/shim_clone.c index f973fa87..896cca03 100644 --- a/LibOS/shim/src/sys/shim_clone.c +++ b/LibOS/shim/src/sys/shim_clone.c @@ -119,11 +119,12 @@ static int clone_implementation_wrapper(struct shim_clone_args * arg) assert(my_thread); shim_tcb_init(); + set_cur_thread(my_thread); init_fs_base(arg->fs_base, my_thread); shim_tcb_t * tcb = my_thread->shim_tcb; /* only now we can call LibOS/PAL functions because they require a set-up TCB; - * do not move the below functions before init_fs_base()! */ + * do not move the below functions before shim_tcb_init/set_cur_thread()! */ object_wait_with_retry(arg->create_event); DkObjectClose(arg->create_event);