Skip to content

Commit

Permalink
Handle sandbox thread initialization from the clone context
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Jan 31, 2025
1 parent d7999de commit cfb1536
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions liblfi/include/lfi.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ struct LFILoadOpts {
bool lfi_proc_loadelf(struct LFIAddrSpace* as, uint8_t* prog, size_t progsz, uint8_t* interp, size_t interpsz, struct LFILoadInfo* o_info, struct LFILoadOpts opts);
bool lfi_proc_init(struct LFIContext* ctx, struct LFIAddrSpace* as, struct LFILoadInfo info);

void lfi_thread_init(void* thread_create, void* pausefn);

char* lfi_strerror(void);
12 changes: 12 additions & 0 deletions liblfi/pal/arch/arm64/regs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ lfi_regs_init(struct TuxRegs* regs, struct LFIAddrSpace* as, struct LFIContext*
regs->x30 = as->base;
regs->REG_SYS = (uintptr_t) ctx->sys;
}

uintptr_t*
lfi_regs_entry(struct TuxRegs* regs)
{
return &regs->x30;
}

uintptr_t*
lfi_regs_arg0(struct TuxRegs* regs)
{
return &regs->x0;
}
12 changes: 12 additions & 0 deletions liblfi/pal/arch/x64/regs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ lfi_regs_init(struct TuxRegs* regs, struct LFIAddrSpace* as, struct LFIContext*
regs->rsp = as->base;
regs->r13 = (uintptr_t) ctx->sys;
}

uintptr_t*
lfi_regs_entry(struct TuxRegs* regs)
{
return &regs->r11;
}

uintptr_t*
lfi_regs_arg0(struct TuxRegs* regs)
{
return &regs->rdi;
}
15 changes: 12 additions & 3 deletions liblfi/pal/ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#include "pal/platform.h"
#include "pal/regs.h"

// context for injecting clone calls
pthread_mutex_t lfi_clonectx_lk = PTHREAD_MUTEX_INITIALIZER;
struct LFIContext* lfi_clonectx;
// for newly cloned thread contexts
_Thread_local struct LFIContext* lfi_newctx;

_Thread_local struct LFIContext* lfi_myctx;

Expand Down Expand Up @@ -154,8 +157,14 @@ pal_register_clonectx(struct LFIContext* ctx)
lfi_clonectx = ctx;
}

void
pal_register_myctx(struct LFIContext* ctx)
EXPORT void
lfi_thread_init(void* thread_create, void* pausefn)
{
assert(!"unimplemented");
// invoke sbx_thread_create(&_lfi_pause) with the clone context
LOCK_WITH_DEFER(&lfi_clonectx_lk, lk);
*lfi_regs_entry(&lfi_clonectx->regs) = (uintptr_t) thread_create;
*lfi_regs_arg0(&lfi_clonectx->regs) = (uintptr_t) pausefn;
lfi_ctx_run(lfi_clonectx, lfi_clonectx->as);
lfi_myctx = lfi_newctx;
lfi_newctx = NULL;
}
6 changes: 4 additions & 2 deletions liblfi/pal/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ gb(size_t x)

_Thread_local extern struct LFIContext* lfi_myctx;

void pal_register_clonectx(struct LFIContext* ctx);
_Thread_local extern struct LFIContext* lfi_newctx;

extern struct LFIContext* lfi_clonectx;

void pal_register_myctx(struct LFIContext* ctx);
void pal_register_clonectx(struct LFIContext* ctx);
4 changes: 4 additions & 0 deletions liblfi/pal/regs.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include "lfi_arch.h"

void lfi_regs_init(struct TuxRegs* regs, struct LFIAddrSpace* as, struct LFIContext* ctx);

uintptr_t* lfi_regs_entry(struct TuxRegs* regs);

uintptr_t* lfi_regs_arg0(struct TuxRegs* regs);
5 changes: 3 additions & 2 deletions liblfi/syscalls/sys_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ uintptr_t
sys_exit(struct TuxThread* p, uint64_t code)
{
clearctid(p);
if (p->proc->tux->opts.pause_on_exit)
if (p->proc->tux->opts.pause_on_exit) {
lfi_ctx_pause(p->p_ctx, code);
else
} else {
lfi_ctx_exit(p->p_ctx, code);
}
assert(!"unreachable");
}

Expand Down
5 changes: 5 additions & 0 deletions liblfi/syscalls/sys_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ spawn(struct TuxThread* p, uint64_t flags, uint64_t stack, uint64_t ptidp, uint6
// was created so it can be reused when we need to spawn threads in the
// future.
pal_register_clonectx(p2->p_ctx);
} else if (p->p_ctx == lfi_clonectx) {
threadspawn(p2);
lfi_newctx = p2->p_ctx;
// does not return
lfi_ctx_pause(p->p_ctx, 0);
} else {
// Actually create a new thread.
pthread_t thread;
Expand Down

0 comments on commit cfb1536

Please sign in to comment.