mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 00:38:31 +00:00
Hunt down more bugs
After going through the MODE=dbg and MODE=zero build modes, a bunch of little issues were identified, which have been addressed. Fixing those issues created even more troubles for the project, because it improved our ability to detect latent problems which are getting fixed so fast.
This commit is contained in:
parent
73c0faa1b5
commit
97b7116953
39 changed files with 557 additions and 754 deletions
|
@ -19,6 +19,7 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/calls/struct/ucontext.internal.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/nexgen32e/nexgen32e.h"
|
||||
#include "libc/nexgen32e/stackframe.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
@ -26,18 +27,18 @@
|
|||
#include "libc/str/str.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
typedef double vect __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
||||
struct Gadget {
|
||||
void (*func)();
|
||||
int args[6];
|
||||
long longs[6];
|
||||
vect vects[6];
|
||||
};
|
||||
|
||||
static void runcontext(struct Gadget *call, ucontext_t *link) {
|
||||
call->func(call->args[0], //
|
||||
call->args[1], //
|
||||
call->args[2], //
|
||||
call->args[3], //
|
||||
call->args[4], //
|
||||
call->args[5]);
|
||||
call->func(call->longs[0], call->longs[1], call->longs[2], call->longs[3],
|
||||
call->longs[4], call->longs[5], call->vects[0], call->vects[1],
|
||||
call->vects[2], call->vects[3], call->vects[4], call->vects[5]);
|
||||
if (link) {
|
||||
setcontext(link);
|
||||
abort();
|
||||
|
@ -61,46 +62,71 @@ static void runcontext(struct Gadget *call, ucontext_t *link) {
|
|||
*
|
||||
* exit(42);
|
||||
*
|
||||
* @param uc stores processor state; the caller must:
|
||||
* 1. initialize it using getcontext()
|
||||
* 2. assign new value to `uc->uc_stack.ss_sp`
|
||||
* 3. use `uc->uc_link` to define successor context
|
||||
* The safest way to allocate stack memory is to use NewCosmoStack() and
|
||||
* GetStackSize(), which will mmap() a fresh region of memory per a link
|
||||
* time configuration, mprotect() some guard pages at the bottom, poison
|
||||
* them if ASAN is in play, and then tell the OS that it's stack memory.
|
||||
* If that's overkill for your use case, then you could potentially pass
|
||||
* stacks as small as 1024 bytes; however they need to come from a stack
|
||||
* allocation Cosmo granted to your main process and threads. It needn't
|
||||
* be aligned, since this function takes care of that automatically. The
|
||||
* address selected shall be `uc_stack.ss_ip + uc_stack.ss_size` and all
|
||||
* the action happens beneath that address.
|
||||
*
|
||||
* On AMD64 and ARM64 you may pass up to six `long` integer args, and up
|
||||
* to six vectors (e.g. double, floats, __m128i, uint8x16_t). Thou shall
|
||||
* not call code created by Microsoft compilers, even though this should
|
||||
* work perfectly fine on Windows, as it is written in the System V ABI,
|
||||
* which specifies your parameters are always being passed in registers.
|
||||
*
|
||||
* @param uc stores processor state; the caller must have:
|
||||
* 1. initialized it using `getcontext(uc)`
|
||||
* 2. allocated new values for `uc->uc_stack`
|
||||
* 3. specified a successor context in `uc->uc_link`
|
||||
* @param func is the function to call when `uc` is activated;
|
||||
* when `func` returns control passes to the linked context
|
||||
* when `func` returns, control is passed to `uc->uc_link`,
|
||||
* which if null will result in pthread_exit() being called
|
||||
* @param argc is number of `int` arguments for `func` (max 6)
|
||||
* @param argc is effectively ignored (see notes above)
|
||||
* @see setcontext(), getcontext(), swapcontext()
|
||||
* @threadsafe
|
||||
*/
|
||||
void makecontext(ucontext_t *uc, void func(), int argc, ...) {
|
||||
int i;
|
||||
va_list va;
|
||||
uintptr_t sp;
|
||||
long sp, sb;
|
||||
struct Gadget *call;
|
||||
struct StackFrame *sf;
|
||||
assert(argc <= 6u);
|
||||
|
||||
// allocate call
|
||||
sp = (uintptr_t)uc->uc_stack.ss_sp;
|
||||
sp = sb = (long)uc->uc_stack.ss_sp;
|
||||
sp += uc->uc_stack.ss_size;
|
||||
sp -= 16; // openbsd:stackbound
|
||||
sp -= sizeof(*call);
|
||||
sp &= -alignof(*call);
|
||||
call = (struct Gadget *)sp;
|
||||
|
||||
// get arguments
|
||||
va_start(va, argc);
|
||||
call->func = func;
|
||||
for (i = 0; i < argc; ++i) {
|
||||
call->args[i] = va_arg(va, int);
|
||||
}
|
||||
va_start(va, argc);
|
||||
call->longs[0] = va_arg(va, long);
|
||||
call->longs[1] = va_arg(va, long);
|
||||
call->longs[2] = va_arg(va, long);
|
||||
call->longs[3] = va_arg(va, long);
|
||||
call->longs[4] = va_arg(va, long);
|
||||
call->longs[5] = va_arg(va, long);
|
||||
call->vects[0] = va_arg(va, vect);
|
||||
call->vects[1] = va_arg(va, vect);
|
||||
call->vects[2] = va_arg(va, vect);
|
||||
call->vects[3] = va_arg(va, vect);
|
||||
call->vects[4] = va_arg(va, vect);
|
||||
call->vects[5] = va_arg(va, vect);
|
||||
va_end(va);
|
||||
|
||||
// construct fake function call on new stack
|
||||
// constructs fake function call on new stack
|
||||
//
|
||||
// the location where getcontext() was called shall be the previous
|
||||
// entry in the backtrace when runcontext was called, e.g.
|
||||
//
|
||||
// 1000800bf160 423024 systemfive_linux+31
|
||||
// 1000800fff90 405299 abort+58
|
||||
// 1000800fff90 405299 func+58
|
||||
// 1000800fffb0 40d98c runcontext+42
|
||||
// 1000800fffd0 40b308 makecontext_backtrace+20
|
||||
// 7fff22a7ff50 40c2d5 testlib_runtestcases+218
|
||||
|
@ -110,12 +136,12 @@ void makecontext(ucontext_t *uc, void func(), int argc, ...) {
|
|||
// 7fff22a7fff0 4034e2 _start+137
|
||||
//
|
||||
// is about what it should look like.
|
||||
sp &= -(sizeof(uintptr_t) * 2);
|
||||
sp &= -(sizeof(long) * 2);
|
||||
#ifdef __x86__
|
||||
*(uintptr_t *)(sp -= sizeof(uintptr_t)) = uc->uc_mcontext.PC;
|
||||
*(long *)(sp -= sizeof(long)) = uc->uc_mcontext.PC;
|
||||
#elif defined(__aarch64__)
|
||||
*(uintptr_t *)(sp -= sizeof(uintptr_t)) = uc->uc_mcontext.regs[30];
|
||||
*(uintptr_t *)(sp -= sizeof(uintptr_t)) = uc->uc_mcontext.regs[29];
|
||||
*(long *)(sp -= sizeof(long)) = uc->uc_mcontext.regs[30];
|
||||
*(long *)(sp -= sizeof(long)) = uc->uc_mcontext.regs[29];
|
||||
uc->uc_mcontext.BP = uc->uc_mcontext.SP;
|
||||
#else
|
||||
#error "unsupported architecture"
|
||||
|
@ -123,7 +149,7 @@ void makecontext(ucontext_t *uc, void func(), int argc, ...) {
|
|||
|
||||
// program context
|
||||
uc->uc_mcontext.SP = sp;
|
||||
uc->uc_mcontext.PC = (uintptr_t)runcontext;
|
||||
uc->uc_mcontext.ARG0 = (uintptr_t)call;
|
||||
uc->uc_mcontext.ARG1 = (uintptr_t)uc->uc_link;
|
||||
uc->uc_mcontext.PC = (long)runcontext;
|
||||
uc->uc_mcontext.ARG0 = (long)call;
|
||||
uc->uc_mcontext.ARG1 = (long)uc->uc_link;
|
||||
}
|
||||
|
|
|
@ -31,13 +31,11 @@
|
|||
|
||||
#define I(x) ((uintptr_t)x)
|
||||
|
||||
void Bzero(void *, size_t) asm("bzero"); // gcc bug
|
||||
|
||||
static char *_mktls_finish(struct CosmoTib **out_tib, char *mem,
|
||||
struct CosmoTib *tib) {
|
||||
struct CosmoTib *old;
|
||||
old = __get_tls();
|
||||
Bzero(tib, sizeof(*tib));
|
||||
bzero(tib, sizeof(*tib));
|
||||
tib->tib_self = tib;
|
||||
tib->tib_self2 = tib;
|
||||
tib->tib_ftrace = old->tib_ftrace;
|
||||
|
@ -51,29 +49,37 @@ static char *_mktls_finish(struct CosmoTib **out_tib, char *mem,
|
|||
}
|
||||
|
||||
static char *_mktls_below(struct CosmoTib **out_tib) {
|
||||
char *tls;
|
||||
struct CosmoTib *neu;
|
||||
size_t siz;
|
||||
char *mem, *tls;
|
||||
struct CosmoTib *tib;
|
||||
|
||||
// allocate memory for tdata, tbss, and tib
|
||||
tls = memalign(TLS_ALIGNMENT, I(_tls_size) + sizeof(struct CosmoTib));
|
||||
if (!tls) return 0;
|
||||
siz = ROUNDUP(I(_tls_size) + sizeof(*tib), _Alignof(struct CosmoTib));
|
||||
siz = ROUNDUP(siz, _Alignof(struct CosmoTib));
|
||||
mem = memalign(_Alignof(struct CosmoTib), siz);
|
||||
|
||||
// poison memory between tdata and tbss
|
||||
if (IsAsan()) {
|
||||
__asan_poison(tls + I(_tdata_size), I(_tbss_offset) - I(_tdata_size),
|
||||
// poison the space between .tdata and .tbss
|
||||
__asan_poison(mem + I(_tdata_size), I(_tbss_offset) - I(_tdata_size),
|
||||
kAsanProtected);
|
||||
}
|
||||
|
||||
// initialize .tdata
|
||||
tib = (struct CosmoTib *)(mem + siz - sizeof(*tib));
|
||||
tls = mem + siz - sizeof(*tib) - I(_tls_size);
|
||||
|
||||
// copy in initialized data section
|
||||
if (I(_tdata_size)) {
|
||||
memmove(tls, _tdata_start, I(_tdata_size));
|
||||
if (IsAsan()) {
|
||||
__asan_memcpy(tls, _tdata_start, I(_tdata_size));
|
||||
} else {
|
||||
memcpy(tls, _tdata_start, I(_tdata_size));
|
||||
}
|
||||
}
|
||||
|
||||
// clear .tbss
|
||||
Bzero(tls + I(_tbss_offset), I(_tbss_size));
|
||||
bzero(tls + I(_tbss_offset), I(_tbss_size));
|
||||
|
||||
// set up thread information block
|
||||
return _mktls_finish(out_tib, tls, (struct CosmoTib *)(tls + I(_tls_size)));
|
||||
return _mktls_finish(out_tib, mem, tib);
|
||||
}
|
||||
|
||||
static char *_mktls_above(struct CosmoTib **out_tib) {
|
||||
|
@ -103,12 +109,16 @@ static char *_mktls_above(struct CosmoTib **out_tib) {
|
|||
|
||||
// initialize .tdata
|
||||
if (I(_tdata_size)) {
|
||||
memmove(tls, _tdata_start, I(_tdata_size));
|
||||
if (IsAsan()) {
|
||||
__asan_memcpy(tls, _tdata_start, I(_tdata_size));
|
||||
} else {
|
||||
memmove(tls, _tdata_start, I(_tdata_size));
|
||||
}
|
||||
}
|
||||
|
||||
// clear .tbss
|
||||
if (I(_tbss_size)) {
|
||||
Bzero(tls + I(_tbss_offset), I(_tbss_size));
|
||||
bzero(tls + I(_tbss_offset), I(_tbss_size));
|
||||
}
|
||||
|
||||
// set up thread information block
|
||||
|
|
|
@ -55,6 +55,9 @@
|
|||
* allocations, things like page size alignment, shall be handled
|
||||
* automatically for compatibility with existing codebases.
|
||||
*
|
||||
* The same stack shouldn't be used for two separate threads. Use
|
||||
* fresh stacks for each thread so that ASAN can be much happier.
|
||||
*
|
||||
* @param stackaddr is address of stack allocated by caller, and
|
||||
* may be NULL in which case default behavior is restored
|
||||
* @param stacksize is size of caller allocated stack
|
||||
|
|
|
@ -209,7 +209,7 @@ static void ListenForSigThr(void) {
|
|||
* void *p = _gc(malloc(123));
|
||||
* read(0, p, 123);
|
||||
*
|
||||
* It's possible to put a thread in asynchronous cancellation mode using
|
||||
* It's possible to put a thread in asynchronous cancellation mode with
|
||||
*
|
||||
* pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
||||
* for (;;) donothing;
|
||||
|
|
|
@ -75,15 +75,22 @@ static errno_t pthread_getname_impl(pthread_t thread, char *name, size_t size) {
|
|||
}
|
||||
return 0;
|
||||
|
||||
} else if (IsNetbsd()) {
|
||||
} else if (IsNetbsd() || IsOpenbsd()) {
|
||||
int ax;
|
||||
char cf;
|
||||
int ax, dx;
|
||||
long dx, si;
|
||||
if (IsNetbsd()) {
|
||||
ax = 324; // _lwp_getname
|
||||
} else {
|
||||
ax = 142; // sys_getthrname
|
||||
}
|
||||
// NetBSD doesn't document the subtleties of its nul-terminator
|
||||
// behavior, so like Linux we shall take the paranoid approach.
|
||||
dx = size - 1;
|
||||
si = (long)name;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
||||
: "1"(324 /* _lwp_getname */), "D"(tid), "S"(name),
|
||||
"d"(size - 1)
|
||||
: CFLAG_CONSTRAINT(cf), "+a"(ax), "+D"(tid), "+S"(si), "+d"(dx)
|
||||
: /* no outputs */
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (!cf) {
|
||||
// if size + our nul + kernel's nul is the buffer size, then we
|
||||
|
@ -116,7 +123,7 @@ static errno_t pthread_getname_impl(pthread_t thread, char *name, size_t size) {
|
|||
* @return 0 on success, or errno on error
|
||||
* @raise ERANGE if `size` wasn't large enough, in which case your
|
||||
* result will still be returned truncated if possible
|
||||
* @raise ENOSYS on MacOS, Windows, FreeBSD, and OpenBSD
|
||||
* @raise ENOSYS on MacOS, Windows, and FreeBSD
|
||||
*/
|
||||
errno_t pthread_getname_np(pthread_t thread, char *name, size_t size) {
|
||||
errno_t rc;
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/at.h"
|
||||
|
@ -72,23 +71,20 @@ static errno_t pthread_setname_impl(pthread_t thread, const char *name) {
|
|||
}
|
||||
return 0;
|
||||
|
||||
} else if (IsFreebsd()) {
|
||||
char cf;
|
||||
int ax, dx;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
||||
: "1"(323 /* thr_set_name */), "D"(tid), "S"(name)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
return !cf ? 0 : ax;
|
||||
|
||||
} else if (IsNetbsd()) {
|
||||
char cf;
|
||||
int ax, dx;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx)
|
||||
: "1"(323 /* _lwp_setname */), "D"(tid), "S"(name)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
return !cf ? 0 : ax;
|
||||
} else if (IsFreebsd() || IsNetbsd() || IsOpenbsd()) {
|
||||
int ax;
|
||||
if (IsFreebsd()) {
|
||||
ax = 464; // thr_set_name
|
||||
} else if (IsNetbsd()) {
|
||||
ax = 323; // _lwp_setname
|
||||
} else {
|
||||
ax = 143; // sys_setthrname
|
||||
}
|
||||
asm volatile("syscall"
|
||||
: "+a"(ax), "+D"(tid), "+S"(name)
|
||||
: /* no inputs */
|
||||
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
||||
return ax;
|
||||
|
||||
} else {
|
||||
return ENOSYS;
|
||||
|
@ -115,7 +111,7 @@ static errno_t pthread_setname_impl(pthread_t thread, const char *name) {
|
|||
* @return 0 on success, or errno on error
|
||||
* @raise ERANGE if length of `name` exceeded system limit, in which
|
||||
* case the name may have still been set with os using truncation
|
||||
* @raise ENOSYS on MacOS, Windows, and OpenBSD
|
||||
* @raise ENOSYS on MacOS, and Windows
|
||||
* @see pthread_getname_np()
|
||||
*/
|
||||
errno_t pthread_setname_np(pthread_t thread, const char *name) {
|
||||
|
|
|
@ -50,6 +50,8 @@
|
|||
* Your spawn library abstracts clone() which also works on all
|
||||
* platforms; however our implementation of clone() is significantly
|
||||
* complicated so we strongly recommend always favoring this API.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
#define _TLSZ ((intptr_t)_tls_size)
|
||||
|
@ -92,6 +94,7 @@ static int Spawner(void *arg, int tid) {
|
|||
* except when it isn't specified, in which case, the thread is kind
|
||||
* of detached and will (currently) just leak the stack / tls memory
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @deprecated
|
||||
*/
|
||||
int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
||||
errno_t rc;
|
||||
|
@ -144,6 +147,8 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
|||
* Waits for thread created by _spawn() to terminate.
|
||||
*
|
||||
* This will free your thread's stack and tls memory too.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
int _join(struct spawn *th) {
|
||||
int rc;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue