mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-06 08:56:22 +00:00
Make garbage collection thread safe
- You can now use _gc(malloc()) in multithreaded programs - This change fixes a bug where fork() on NT disabled TLS - Fixed TLS code morphing on XNU/NT, for R8-R15 registers
This commit is contained in:
parent
571c2c3c69
commit
0e2b1bfeed
37 changed files with 310 additions and 189 deletions
|
@ -8,6 +8,7 @@
|
||||||
╚─────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────*/
|
||||||
#endif
|
#endif
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/fmt/conv.h"
|
#include "libc/fmt/conv.h"
|
||||||
#include "libc/log/check.h"
|
#include "libc/log/check.h"
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
╚─────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────*/
|
||||||
#endif
|
#endif
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
|
|
|
@ -593,7 +593,7 @@ typedef struct {
|
||||||
|
|
||||||
#define notpossible \
|
#define notpossible \
|
||||||
do { \
|
do { \
|
||||||
asm("ud2"); \
|
asm("ud2\nnop"); \
|
||||||
unreachable; \
|
unreachable; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,9 @@
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/mem/reverse.internal.h"
|
#include "libc/mem/reverse.internal.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
|
#include "libc/nexgen32e/gettls.h"
|
||||||
#include "libc/nexgen32e/stackframe.h"
|
#include "libc/nexgen32e/stackframe.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/nt/enum/version.h"
|
#include "libc/nt/enum/version.h"
|
||||||
#include "libc/nt/runtime.h"
|
#include "libc/nt/runtime.h"
|
||||||
#include "libc/runtime/directmap.internal.h"
|
#include "libc/runtime/directmap.internal.h"
|
||||||
|
@ -56,6 +58,7 @@
|
||||||
#include "libc/sysv/consts/nr.h"
|
#include "libc/sysv/consts/nr.h"
|
||||||
#include "libc/sysv/consts/prot.h"
|
#include "libc/sysv/consts/prot.h"
|
||||||
#include "libc/sysv/errfuns.h"
|
#include "libc/sysv/errfuns.h"
|
||||||
|
#include "libc/thread/thread.h"
|
||||||
#include "third_party/dlmalloc/dlmalloc.h"
|
#include "third_party/dlmalloc/dlmalloc.h"
|
||||||
|
|
||||||
STATIC_YOINK("_init_asan");
|
STATIC_YOINK("_init_asan");
|
||||||
|
@ -936,7 +939,7 @@ static void __asan_trace(struct AsanTrace *bt, const struct StackFrame *bp) {
|
||||||
size_t i, gi;
|
size_t i, gi;
|
||||||
intptr_t addr;
|
intptr_t addr;
|
||||||
struct Garbages *garbage;
|
struct Garbages *garbage;
|
||||||
garbage = weaken(__garbage);
|
garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0;
|
||||||
gi = garbage ? garbage->i : 0;
|
gi = garbage ? garbage->i : 0;
|
||||||
for (f1 = -1, i = 0; bp && i < ARRAYLEN(bt->p); ++i, bp = bp->next) {
|
for (f1 = -1, i = 0; bp && i < ARRAYLEN(bt->p); ++i, bp = bp->next) {
|
||||||
if (f1 != (f2 = ((intptr_t)bp >> 16))) {
|
if (f1 != (f2 = ((intptr_t)bp >> 16))) {
|
||||||
|
|
25
libc/intrin/tlsisrequired.c
Normal file
25
libc/intrin/tlsisrequired.c
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
|
|
||||||
|
void TlsIsRequired(void) {
|
||||||
|
if (!__tls_enabled) {
|
||||||
|
notpossible;
|
||||||
|
}
|
||||||
|
}
|
|
@ -37,6 +37,8 @@
|
||||||
#include "libc/mem/alg.h"
|
#include "libc/mem/alg.h"
|
||||||
#include "libc/mem/bisectcarleft.internal.h"
|
#include "libc/mem/bisectcarleft.internal.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
|
#include "libc/nexgen32e/gettls.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/runtime/stack.h"
|
#include "libc/runtime/stack.h"
|
||||||
|
@ -47,6 +49,7 @@
|
||||||
#include "libc/sysv/consts/fileno.h"
|
#include "libc/sysv/consts/fileno.h"
|
||||||
#include "libc/sysv/consts/o.h"
|
#include "libc/sysv/consts/o.h"
|
||||||
#include "libc/sysv/consts/sig.h"
|
#include "libc/sysv/consts/sig.h"
|
||||||
|
#include "libc/thread/thread.h"
|
||||||
#include "libc/x/x.h"
|
#include "libc/x/x.h"
|
||||||
|
|
||||||
#define kBacktraceMaxFrames 128
|
#define kBacktraceMaxFrames 128
|
||||||
|
@ -106,7 +109,7 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
|
||||||
argv[i++] = "-a"; /* filter out w/ shell script wrapper for old versions */
|
argv[i++] = "-a"; /* filter out w/ shell script wrapper for old versions */
|
||||||
argv[i++] = "-pCife";
|
argv[i++] = "-pCife";
|
||||||
argv[i++] = debugbin;
|
argv[i++] = debugbin;
|
||||||
garbage = weaken(__garbage);
|
garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0;
|
||||||
gi = garbage ? garbage->i : 0;
|
gi = garbage ? garbage->i : 0;
|
||||||
for (frame = bp; frame && i < kBacktraceMaxFrames - 1; frame = frame->next) {
|
for (frame = bp; frame && i < kBacktraceMaxFrames - 1; frame = frame->next) {
|
||||||
addr = frame->addr;
|
addr = frame->addr;
|
||||||
|
|
|
@ -16,21 +16,24 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/mem/bisectcarleft.internal.h"
|
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
#include "libc/intrin/weaken.h"
|
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/fmt/fmt.h"
|
#include "libc/fmt/fmt.h"
|
||||||
#include "libc/fmt/itoa.h"
|
#include "libc/fmt/itoa.h"
|
||||||
#include "libc/intrin/kprintf.h"
|
#include "libc/intrin/kprintf.h"
|
||||||
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/log/backtrace.internal.h"
|
#include "libc/log/backtrace.internal.h"
|
||||||
#include "libc/macros.internal.h"
|
#include "libc/macros.internal.h"
|
||||||
|
#include "libc/mem/bisectcarleft.internal.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
|
#include "libc/nexgen32e/gettls.h"
|
||||||
#include "libc/nexgen32e/stackframe.h"
|
#include "libc/nexgen32e/stackframe.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/runtime/memtrack.internal.h"
|
#include "libc/runtime/memtrack.internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/runtime/symbols.internal.h"
|
#include "libc/runtime/symbols.internal.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
|
#include "libc/thread/thread.h"
|
||||||
|
|
||||||
#define LIMIT 100
|
#define LIMIT 100
|
||||||
|
|
||||||
|
@ -54,7 +57,7 @@ noinstrument noasan int PrintBacktraceUsingSymbols(int fd,
|
||||||
struct Garbages *garbage;
|
struct Garbages *garbage;
|
||||||
const struct StackFrame *frame;
|
const struct StackFrame *frame;
|
||||||
if (!bp) bp = __builtin_frame_address(0);
|
if (!bp) bp = __builtin_frame_address(0);
|
||||||
garbage = weaken(__garbage);
|
garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0;
|
||||||
gi = garbage ? garbage->i : 0;
|
gi = garbage ? garbage->i : 0;
|
||||||
for (i = 0, frame = bp; frame; frame = frame->next) {
|
for (i = 0, frame = bp; frame; frame = frame->next) {
|
||||||
if (++i == LIMIT) {
|
if (++i == LIMIT) {
|
||||||
|
|
|
@ -69,6 +69,7 @@ o/$(MODE)/libc/log/watch.o: private \
|
||||||
OVERRIDE_CFLAGS += \
|
OVERRIDE_CFLAGS += \
|
||||||
-ffreestanding
|
-ffreestanding
|
||||||
|
|
||||||
|
o/$(MODE)/libc/log/watch.o \
|
||||||
o/$(MODE)/libc/log/attachdebugger.o \
|
o/$(MODE)/libc/log/attachdebugger.o \
|
||||||
o/$(MODE)/libc/log/checkaligned.o \
|
o/$(MODE)/libc/log/checkaligned.o \
|
||||||
o/$(MODE)/libc/log/checkfail.o \
|
o/$(MODE)/libc/log/checkfail.o \
|
||||||
|
|
|
@ -20,8 +20,11 @@
|
||||||
#include "libc/intrin/kprintf.h"
|
#include "libc/intrin/kprintf.h"
|
||||||
#include "libc/log/log.h"
|
#include "libc/log/log.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
|
#include "libc/nexgen32e/gettls.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
/* clang-format off */
|
#include "libc/thread/thread.h"
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints list of deferred operations on shadow stack.
|
* Prints list of deferred operations on shadow stack.
|
||||||
|
@ -30,24 +33,25 @@ void PrintGarbage(void) {
|
||||||
size_t i;
|
size_t i;
|
||||||
char name[19];
|
char name[19];
|
||||||
const char *symbol;
|
const char *symbol;
|
||||||
|
struct Garbages *g;
|
||||||
kprintf("\n");
|
kprintf("\n");
|
||||||
kprintf(" SHADOW STACK @ %p\n", __builtin_frame_address(0));
|
kprintf(" SHADOW STACK @ %p\n", __builtin_frame_address(0));
|
||||||
kprintf("garbage ent. parent frame original ret callback arg \n");
|
kprintf("garbage ent. parent frame original ret callback arg \n");
|
||||||
kprintf("------------ ------------ ------------------ ------------------ ------------------\n");
|
kprintf("------------ ------------ ------------------ ------------------ ------------------\n");
|
||||||
if (__garbage.i) {
|
if ((g = __tls_enabled ? ((cthread_t)__get_tls())->garbages:0) && g->i) {
|
||||||
for (i = __garbage.i; i--;) {
|
for (i = g->i; i--;) {
|
||||||
symbol = __get_symbol_by_addr(__garbage.p[i].ret);
|
symbol = __get_symbol_by_addr(g->p[i].ret);
|
||||||
if (symbol) {
|
if (symbol) {
|
||||||
ksnprintf(name, sizeof(name), "%s", symbol);
|
ksnprintf(name, sizeof(name), "%s", symbol);
|
||||||
} else {
|
} else {
|
||||||
ksnprintf(name, sizeof(name), "%#014lx", __garbage.p[i].ret);
|
ksnprintf(name, sizeof(name), "%#014lx", g->p[i].ret);
|
||||||
}
|
}
|
||||||
kprintf("%12lx %12lx %18s %18s %#18lx\n",
|
kprintf("%12lx %12lx %18s %18s %#18lx\n",
|
||||||
__garbage.p + i,
|
g->p + i,
|
||||||
__garbage.p[i].frame,
|
g->p[i].frame,
|
||||||
name,
|
name,
|
||||||
__get_symbol_by_addr(__garbage.p[i].fn),
|
__get_symbol_by_addr(g->p[i].fn),
|
||||||
__garbage.p[i].arg);
|
g->p[i].arg);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
kprintf("%12s %12s %18s %18s %18s\n","empty","-","-","-","-");
|
kprintf("%12s %12s %18s %18s %18s\n","empty","-","-","-","-");
|
||||||
|
|
24
libc/mem/_gc_free.c
Normal file
24
libc/mem/_gc_free.c
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/runtime/gc.h"
|
||||||
|
|
||||||
|
void _gc_free(void *p) {
|
||||||
|
free(p);
|
||||||
|
}
|
|
@ -1,85 +0,0 @@
|
||||||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
||||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
||||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
||||||
│ │
|
|
||||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
||||||
│ any purpose with or without fee is hereby granted, provided that the │
|
|
||||||
│ above copyright notice and this permission notice appear in all copies. │
|
|
||||||
│ │
|
|
||||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
||||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
||||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
||||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
||||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
||||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
||||||
#include "libc/assert.h"
|
|
||||||
#include "libc/intrin/likely.h"
|
|
||||||
#include "libc/intrin/weaken.h"
|
|
||||||
#include "libc/calls/calls.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
|
||||||
#include "libc/runtime/gc.internal.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/str/str.h"
|
|
||||||
|
|
||||||
forceinline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
|
|
||||||
struct StackFrame *parent,
|
|
||||||
void *ptr) {
|
|
||||||
return !(((intptr_t)ptr > (intptr_t)frame) &&
|
|
||||||
((intptr_t)ptr < (intptr_t)parent));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void __garbage_destroy(void) {
|
|
||||||
if (weaken(free)) {
|
|
||||||
weaken(free)(__garbage.p);
|
|
||||||
}
|
|
||||||
bzero(&__garbage, sizeof(__garbage));
|
|
||||||
}
|
|
||||||
|
|
||||||
void __deferer(struct StackFrame *frame, void *fn, void *arg) {
|
|
||||||
size_t n2;
|
|
||||||
struct Garbage *p2;
|
|
||||||
if (UNLIKELY(__garbage.i == __garbage.n)) {
|
|
||||||
p2 = __garbage.p;
|
|
||||||
n2 = __garbage.n + (__garbage.n >> 1);
|
|
||||||
if (__garbage.p != __garbage.initmem) {
|
|
||||||
if (!weaken(realloc)) return;
|
|
||||||
if (!(p2 = weaken(realloc)(p2, n2 * sizeof(*p2)))) return;
|
|
||||||
} else {
|
|
||||||
if (!weaken(malloc)) return;
|
|
||||||
if (!(p2 = weaken(malloc)(n2 * sizeof(*p2)))) return;
|
|
||||||
memcpy(p2, __garbage.p, __garbage.n * sizeof(*p2));
|
|
||||||
atexit(__garbage_destroy);
|
|
||||||
}
|
|
||||||
__garbage.p = p2;
|
|
||||||
__garbage.n = n2;
|
|
||||||
}
|
|
||||||
__garbage.p[__garbage.i].frame = frame;
|
|
||||||
__garbage.p[__garbage.i].fn = (intptr_t)fn;
|
|
||||||
__garbage.p[__garbage.i].arg = (intptr_t)arg;
|
|
||||||
__garbage.p[__garbage.i].ret = frame->addr;
|
|
||||||
__garbage.i++;
|
|
||||||
frame->addr = (intptr_t)__gc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds destructor to garbage shadow stack.
|
|
||||||
*
|
|
||||||
* @param frame is passed automatically by wrapper macro
|
|
||||||
* @param fn takes one argument
|
|
||||||
* @param arg is passed to fn(arg)
|
|
||||||
* @return arg
|
|
||||||
*/
|
|
||||||
void __defer(struct StackFrame *frame, void *fn, void *arg) {
|
|
||||||
struct StackFrame *f;
|
|
||||||
if (!arg) return;
|
|
||||||
f = __builtin_frame_address(0);
|
|
||||||
assert(__garbage.n);
|
|
||||||
assert(f->next == frame);
|
|
||||||
assert(PointerNotOwnedByParentStackFrame(f, frame, arg));
|
|
||||||
__deferer(frame, fn, arg);
|
|
||||||
}
|
|
|
@ -16,9 +16,93 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/assert.h"
|
||||||
|
#include "libc/intrin/kprintf.h"
|
||||||
|
#include "libc/intrin/likely.h"
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
|
#include "libc/nexgen32e/gettls.h"
|
||||||
#include "libc/nexgen32e/stackframe.h"
|
#include "libc/nexgen32e/stackframe.h"
|
||||||
#include "libc/runtime/gc.h"
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
|
#include "libc/str/str.h"
|
||||||
|
#include "libc/thread/thread.h"
|
||||||
|
|
||||||
|
static inline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
|
||||||
|
struct StackFrame *parent,
|
||||||
|
void *ptr) {
|
||||||
|
return !(((intptr_t)ptr > (intptr_t)frame) &&
|
||||||
|
((intptr_t)ptr < (intptr_t)parent));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TeardownGc(void) {
|
||||||
|
int i;
|
||||||
|
cthread_t tls;
|
||||||
|
struct Garbages *g;
|
||||||
|
if (__tls_enabled) {
|
||||||
|
tls = (cthread_t)__get_tls();
|
||||||
|
if ((g = tls->garbages)) {
|
||||||
|
// exit() currently doesn't use _gclongjmp() like pthread_exit()
|
||||||
|
// so we need to run the deferred functions manually.
|
||||||
|
while (g->i) {
|
||||||
|
--g->i;
|
||||||
|
((void (*)(intptr_t))g->p[g->i].fn)(g->p[g->i].arg);
|
||||||
|
}
|
||||||
|
free(g->p);
|
||||||
|
free(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((__constructor__)) static void InitializeGc(void) {
|
||||||
|
atexit(TeardownGc);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add item to garbage shadow stack.
|
||||||
|
// then rewrite caller's return address on stack.
|
||||||
|
static void DeferFunction(struct StackFrame *frame, void *fn, void *arg) {
|
||||||
|
int n2;
|
||||||
|
cthread_t tls;
|
||||||
|
struct Garbage *p2;
|
||||||
|
struct Garbages *g;
|
||||||
|
TlsIsRequired();
|
||||||
|
tls = (cthread_t)__get_tls();
|
||||||
|
g = tls->garbages;
|
||||||
|
if (UNLIKELY(!g)) {
|
||||||
|
if (!(g = malloc(sizeof(struct Garbages)))) notpossible;
|
||||||
|
g->i = 0;
|
||||||
|
g->n = 4;
|
||||||
|
if (!(g->p = malloc(g->n * sizeof(struct Garbage)))) {
|
||||||
|
kprintf("malloc failed\n");
|
||||||
|
notpossible;
|
||||||
|
}
|
||||||
|
tls->garbages = g;
|
||||||
|
} else if (UNLIKELY(g->i == g->n)) {
|
||||||
|
p2 = g->p;
|
||||||
|
n2 = g->n + (g->n >> 1);
|
||||||
|
if ((p2 = realloc(p2, n2 * sizeof(*p2)))) {
|
||||||
|
g->p = p2;
|
||||||
|
g->n = n2;
|
||||||
|
} else {
|
||||||
|
notpossible;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g->p[g->i].frame = frame;
|
||||||
|
g->p[g->i].fn = (intptr_t)fn;
|
||||||
|
g->p[g->i].arg = (intptr_t)arg;
|
||||||
|
g->p[g->i].ret = frame->addr;
|
||||||
|
g->i++;
|
||||||
|
frame->addr = (intptr_t)__gc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the gnu extension macros for _gc / _defer point here
|
||||||
|
void __defer(void *rbp, void *fn, void *arg) {
|
||||||
|
struct StackFrame *f, *frame = rbp;
|
||||||
|
f = __builtin_frame_address(0);
|
||||||
|
assert(f->next == frame);
|
||||||
|
assert(PointerNotOwnedByParentStackFrame(f, frame, arg));
|
||||||
|
DeferFunction(frame, fn, arg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees memory when function returns.
|
* Frees memory when function returns.
|
||||||
|
@ -38,11 +122,12 @@
|
||||||
* @warning do not realloc() with gc()'d pointer
|
* @warning do not realloc() with gc()'d pointer
|
||||||
* @warning be careful about static keyword due to impact of inlining
|
* @warning be careful about static keyword due to impact of inlining
|
||||||
* @note you should use -fno-omit-frame-pointer
|
* @note you should use -fno-omit-frame-pointer
|
||||||
|
* @threadsafe
|
||||||
*/
|
*/
|
||||||
void *(_gc)(void *thing) {
|
void *(_gc)(void *thing) {
|
||||||
struct StackFrame *frame;
|
struct StackFrame *frame;
|
||||||
frame = __builtin_frame_address(0);
|
frame = __builtin_frame_address(0);
|
||||||
__deferer(frame->next, _weakfree, thing);
|
DeferFunction(frame->next, _weakfree, thing);
|
||||||
return thing;
|
return thing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,10 +142,11 @@ void *(_gc)(void *thing) {
|
||||||
* @warning do not realloc() with gc()'d pointer
|
* @warning do not realloc() with gc()'d pointer
|
||||||
* @warning be careful about static keyword due to impact of inlining
|
* @warning be careful about static keyword due to impact of inlining
|
||||||
* @note you should use -fno-omit-frame-pointer
|
* @note you should use -fno-omit-frame-pointer
|
||||||
|
* @threadsafe
|
||||||
*/
|
*/
|
||||||
void *(_defer)(void *fn, void *arg) {
|
void *(_defer)(void *fn, void *arg) {
|
||||||
struct StackFrame *frame;
|
struct StackFrame *frame;
|
||||||
frame = __builtin_frame_address(0);
|
frame = __builtin_frame_address(0);
|
||||||
__deferer(frame->next, fn, arg);
|
DeferFunction(frame->next, fn, arg);
|
||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,6 @@
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/notice.inc"
|
#include "libc/notice.inc"
|
||||||
|
|
||||||
#define INITIAL_CAPACITY 4
|
|
||||||
|
|
||||||
nop
|
nop
|
||||||
|
|
||||||
// Invokes deferred function calls.
|
// Invokes deferred function calls.
|
||||||
|
@ -34,9 +32,12 @@
|
||||||
//
|
//
|
||||||
// @param rax,rdx,xmm0,xmm1,st0,st1 is return value
|
// @param rax,rdx,xmm0,xmm1,st0,st1 is return value
|
||||||
// @see test/libc/runtime/gc_test.c
|
// @see test/libc/runtime/gc_test.c
|
||||||
__gc: decq __garbage(%rip)
|
// @threadsafe
|
||||||
mov __garbage(%rip),%r8
|
__gc: mov %fs:0,%rcx # __get_tls()
|
||||||
mov __garbage+16(%rip),%r9
|
mov 0x18(%rcx),%rcx # cthread_t::garbages
|
||||||
|
decl (%rcx) # ++g->i
|
||||||
|
mov (%rcx),%r8d # r8 = g->i
|
||||||
|
mov 8(%rcx),%r9 # r9 = g->p
|
||||||
js 9f
|
js 9f
|
||||||
shl $5,%r8
|
shl $5,%r8
|
||||||
lea (%r9,%r8),%r8
|
lea (%r9,%r8),%r8
|
||||||
|
@ -55,25 +56,5 @@ __gc: decq __garbage(%rip)
|
||||||
mov -8(%rbp),%rax
|
mov -8(%rbp),%rax
|
||||||
leave
|
leave
|
||||||
ret
|
ret
|
||||||
9: hlt
|
9: ud2
|
||||||
.endfn __gc,globl,hidden
|
.endfn __gc,globl,hidden
|
||||||
|
|
||||||
.bss
|
|
||||||
.align 8
|
|
||||||
__garbage:
|
|
||||||
.quad 0 # garbage.i
|
|
||||||
.quad 0 # garbage.n
|
|
||||||
.quad 0 # garbage.p
|
|
||||||
.rept INITIAL_CAPACITY
|
|
||||||
.quad 0 # garbage.p[𝑖].frame
|
|
||||||
.quad 0 # garbage.p[𝑖].fn
|
|
||||||
.quad 0 # garbage.p[𝑖].arg
|
|
||||||
.quad 0 # garbage.p[𝑖].ret
|
|
||||||
.endr
|
|
||||||
.endobj __garbage,globl,hidden
|
|
||||||
.previous
|
|
||||||
|
|
||||||
.init.start 100,_init_garbage
|
|
||||||
movb $INITIAL_CAPACITY,__garbage+8(%rip)
|
|
||||||
movl $__garbage+24,__garbage+16(%rip)
|
|
||||||
.init.end 100,_init_garbage
|
|
||||||
|
|
|
@ -12,13 +12,10 @@ struct Garbage {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Garbages {
|
struct Garbages {
|
||||||
size_t i, n;
|
int i, n;
|
||||||
struct Garbage *p;
|
struct Garbage *p;
|
||||||
struct Garbage initmem[1];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct Garbages __garbage;
|
|
||||||
|
|
||||||
int64_t __gc(void);
|
int64_t __gc(void);
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
|
|
|
@ -28,29 +28,33 @@
|
||||||
// @param esi is returned by setjmp() invocation (coerced nonzero)
|
// @param esi is returned by setjmp() invocation (coerced nonzero)
|
||||||
// @assume system five nexgen32e abi conformant
|
// @assume system five nexgen32e abi conformant
|
||||||
// @see examples/ctrlc.c
|
// @see examples/ctrlc.c
|
||||||
|
// @threadsafe
|
||||||
// @noreturn
|
// @noreturn
|
||||||
_gclongjmp:
|
_gclongjmp:
|
||||||
push %rbp
|
push %rbp
|
||||||
mov %rsp,%rbp
|
mov %rsp,%rbp
|
||||||
.profilable
|
.profilable
|
||||||
lea __garbage(%rip),%r12
|
mov %fs:0,%r12 # __get_tls()
|
||||||
mov (%r12),%r13 # garbage.i
|
mov 0x18(%r12),%r12 # cthread_t::garbages
|
||||||
test %r13,%r13
|
test %r12,%r12
|
||||||
|
jz 0f
|
||||||
|
movl (%r12),%r13d # garbages.i
|
||||||
|
test %r13d,%r13d
|
||||||
jnz .L.unwind.destructors
|
jnz .L.unwind.destructors
|
||||||
0: jmp longjmp
|
0: jmp longjmp
|
||||||
.L.unwind.destructors:
|
.L.unwind.destructors:
|
||||||
push %rdi
|
push %rdi
|
||||||
push %rsi
|
push %rsi
|
||||||
mov 16(%r12),%r14 # garbage.p
|
mov 8(%r12),%r14 # garbages.p
|
||||||
mov (%rdi),%r15 # jmp_buf[0] is new %rsp
|
mov (%rdi),%r15 # jmp_buf[0] is new %rsp
|
||||||
shl $5,%r13 # log2(sizeof(struct Garbage))
|
shl $5,%r13 # log2(sizeof(struct Garbage))
|
||||||
1: sub $32,%r13 # 𝑖--
|
1: sub $32,%r13 # 𝑖--
|
||||||
js 2f
|
js 2f
|
||||||
cmp (%r14,%r13),%r15 # new %rsp > garbage.p[𝑖].frame
|
cmp (%r14,%r13),%r15 # new %rsp > garbages.p[𝑖].frame
|
||||||
jbe 2f
|
jbe 2f
|
||||||
mov 16(%r14,%r13),%rdi # garbage.p[𝑖].arg
|
mov 16(%r14,%r13),%rdi # garbages.p[𝑖].arg
|
||||||
callq *8(%r14,%r13) # garbage.p[𝑖].fn
|
callq *8(%r14,%r13) # garbages.p[𝑖].fn
|
||||||
decq (%r12) # garbage.i--
|
decl (%r12) # garbages.i--
|
||||||
jmp 1b
|
jmp 1b
|
||||||
2: pop %rsi
|
2: pop %rsi
|
||||||
pop %rdi
|
pop %rdi
|
||||||
|
|
|
@ -7,6 +7,7 @@ extern int __threaded;
|
||||||
extern bool __tls_enabled;
|
extern bool __tls_enabled;
|
||||||
extern unsigned __tls_index;
|
extern unsigned __tls_index;
|
||||||
|
|
||||||
|
void TlsIsRequired(void);
|
||||||
void *__initialize_tls(char[64]);
|
void *__initialize_tls(char[64]);
|
||||||
void __install_tls(char[64]);
|
void __install_tls(char[64]);
|
||||||
|
|
||||||
|
|
|
@ -230,7 +230,7 @@ privileged void __enable_tls(void) {
|
||||||
|
|
||||||
// we're checking for the following expression:
|
// we're checking for the following expression:
|
||||||
// 0144 == p[0] && // %fs
|
// 0144 == p[0] && // %fs
|
||||||
// 0110 == p[1] && // rex.w (64-bit operand size)
|
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
|
||||||
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
|
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
|
||||||
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
|
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
|
||||||
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
|
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
|
||||||
|
@ -239,7 +239,7 @@ privileged void __enable_tls(void) {
|
||||||
// 0000 == p[6] && // displacement
|
// 0000 == p[6] && // displacement
|
||||||
// 0000 == p[7] && // displacement
|
// 0000 == p[7] && // displacement
|
||||||
// 0000 == p[8] // displacement
|
// 0000 == p[8] // displacement
|
||||||
w = READ64LE(p) & READ64LE("\377\377\377\307\377\377\377\377");
|
w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
|
||||||
if ((w == READ64LE("\144\110\213\004\045\000\000\000") ||
|
if ((w == READ64LE("\144\110\213\004\045\000\000\000") ||
|
||||||
w == READ64LE("\144\110\003\004\045\000\000\000")) &&
|
w == READ64LE("\144\110\003\004\045\000\000\000")) &&
|
||||||
!p[8]) {
|
!p[8]) {
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "libc/mem/alloca.h"
|
#include "libc/mem/alloca.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/nexgen32e/nt2sysv.h"
|
#include "libc/nexgen32e/nt2sysv.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/nt/console.h"
|
#include "libc/nt/console.h"
|
||||||
#include "libc/nt/createfile.h"
|
#include "libc/nt/createfile.h"
|
||||||
#include "libc/nt/enum/accessmask.h"
|
#include "libc/nt/enum/accessmask.h"
|
||||||
|
@ -88,7 +89,6 @@ static inline textwindows ssize_t ForkIo(int64_t h, char *p, size_t n,
|
||||||
static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
||||||
bool32 (*fn)(), const char *sf) {
|
bool32 (*fn)(), const char *sf) {
|
||||||
ssize_t rc = ForkIo(h, buf, n, fn);
|
ssize_t rc = ForkIo(h, buf, n, fn);
|
||||||
__tls_enabled = false; // prevent tls crash in kprintf
|
|
||||||
NTTRACE("%s(%ld, %p, %'zu) → %'zd% m", sf, h, buf, n, rc);
|
NTTRACE("%s(%ld, %p, %'zu) → %'zd% m", sf, h, buf, n, rc);
|
||||||
return rc != -1;
|
return rc != -1;
|
||||||
}
|
}
|
||||||
|
@ -98,6 +98,7 @@ static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static textwindows dontinline void ReadOrDie(int64_t h, void *buf, size_t n) {
|
static textwindows dontinline void ReadOrDie(int64_t h, void *buf, size_t n) {
|
||||||
|
__tls_enabled = false; // prevent tls crash in kprintf
|
||||||
if (!ForkIo2(h, buf, n, ReadFile, "ReadFile")) {
|
if (!ForkIo2(h, buf, n, ReadFile, "ReadFile")) {
|
||||||
AbortFork("ReadFile");
|
AbortFork("ReadFile");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,22 @@
|
||||||
#ifndef COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
#ifndef COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
||||||
#define COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
#define COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
||||||
#include "libc/calls/calls.h"
|
|
||||||
#include "libc/nexgen32e/stackframe.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||||
COSMOPOLITAN_C_START_
|
COSMOPOLITAN_C_START_
|
||||||
|
|
||||||
void *_gc(void *) hidden;
|
void *_gc(void *);
|
||||||
void *_defer(void *, void *) hidden;
|
void *_defer(void *, void *);
|
||||||
void __defer(struct StackFrame *, void *, void *) hidden;
|
void __defer(void *, void *, void *);
|
||||||
void __deferer(struct StackFrame *, void *, void *) hidden;
|
void _gclongjmp(void *, int) dontthrow wontreturn;
|
||||||
void _gclongjmp(jmp_buf, int) dontthrow wontreturn;
|
void _gc_free(void *);
|
||||||
|
|
||||||
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||||
#define _gc(THING) _defer((void *)_weakfree, (void *)(THING))
|
#define _gc(THING) _defer((void *)_gc_free, (void *)(THING))
|
||||||
#define _defer(FN, ARG) \
|
#define _defer(FN, ARG) \
|
||||||
({ \
|
({ \
|
||||||
autotype(ARG) Arg = (ARG); \
|
autotype(ARG) Arg = (ARG); \
|
||||||
/* prevent weird opts like tail call */ \
|
/* prevent weird opts like tail call */ \
|
||||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||||
__defer((struct StackFrame *)__builtin_frame_address(0), FN, Arg); \
|
__defer(__builtin_frame_address(0), FN, Arg); \
|
||||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||||
Arg; \
|
Arg; \
|
||||||
})
|
})
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/assert.h"
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/intrin/asan.internal.h"
|
#include "libc/intrin/asan.internal.h"
|
||||||
|
@ -24,7 +25,9 @@
|
||||||
#include "libc/intrin/wait0.internal.h"
|
#include "libc/intrin/wait0.internal.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
#include "libc/nexgen32e/gettls.h"
|
#include "libc/nexgen32e/gettls.h"
|
||||||
|
#include "libc/nexgen32e/threaded.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/sysv/consts/clone.h"
|
#include "libc/sysv/consts/clone.h"
|
||||||
#include "libc/sysv/consts/map.h"
|
#include "libc/sysv/consts/map.h"
|
||||||
|
@ -56,6 +59,7 @@ static int PosixThread(void *arg, int tid) {
|
||||||
if (weaken(_pthread_key_destruct)) {
|
if (weaken(_pthread_key_destruct)) {
|
||||||
weaken(_pthread_key_destruct)(0);
|
weaken(_pthread_key_destruct)(0);
|
||||||
}
|
}
|
||||||
|
cthread_ungarbage();
|
||||||
if (atomic_load_explicit(&pt->status, memory_order_acquire) ==
|
if (atomic_load_explicit(&pt->status, memory_order_acquire) ==
|
||||||
kPosixThreadDetached) {
|
kPosixThreadDetached) {
|
||||||
atomic_store_explicit(&pt->status, kPosixThreadZombie,
|
atomic_store_explicit(&pt->status, kPosixThreadZombie,
|
||||||
|
@ -108,6 +112,7 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
||||||
int rc, e = errno;
|
int rc, e = errno;
|
||||||
struct PosixThread *pt;
|
struct PosixThread *pt;
|
||||||
pthread_attr_t default_attr;
|
pthread_attr_t default_attr;
|
||||||
|
TlsIsRequired();
|
||||||
_pthread_zombies_decimate();
|
_pthread_zombies_decimate();
|
||||||
|
|
||||||
// default attributes
|
// default attributes
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/intrin/pthread.h"
|
#include "libc/intrin/pthread.h"
|
||||||
#include "libc/nexgen32e/gettls.h"
|
#include "libc/nexgen32e/gettls.h"
|
||||||
|
#include "libc/runtime/gc.h"
|
||||||
#include "libc/thread/posixthread.internal.h"
|
#include "libc/thread/posixthread.internal.h"
|
||||||
#include "libc/thread/spawn.h"
|
#include "libc/thread/spawn.h"
|
||||||
#include "libc/thread/thread.h"
|
#include "libc/thread/thread.h"
|
||||||
|
@ -38,7 +39,7 @@ wontreturn void pthread_exit(void *rc) {
|
||||||
struct PosixThread *pt;
|
struct PosixThread *pt;
|
||||||
if ((pt = ((cthread_t)__get_tls())->pthread)) {
|
if ((pt = ((cthread_t)__get_tls())->pthread)) {
|
||||||
pt->rc = rc;
|
pt->rc = rc;
|
||||||
longjmp(pt->exiter, 1);
|
_gclongjmp(pt->exiter, 1);
|
||||||
} else {
|
} else {
|
||||||
_Exit1((int)(intptr_t)rc);
|
_Exit1((int)(intptr_t)rc);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "libc/sysv/consts/clone.h"
|
#include "libc/sysv/consts/clone.h"
|
||||||
#include "libc/sysv/consts/map.h"
|
#include "libc/sysv/consts/map.h"
|
||||||
#include "libc/sysv/consts/prot.h"
|
#include "libc/sysv/consts/prot.h"
|
||||||
|
#include "libc/sysv/errfuns.h"
|
||||||
#include "libc/thread/spawn.h"
|
#include "libc/thread/spawn.h"
|
||||||
#include "libc/thread/thread.h"
|
#include "libc/thread/thread.h"
|
||||||
|
|
||||||
|
@ -54,6 +55,20 @@
|
||||||
#define _TIBZ sizeof(struct cthread_descriptor_t)
|
#define _TIBZ sizeof(struct cthread_descriptor_t)
|
||||||
#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, alignof(struct cthread_descriptor_t))
|
#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, alignof(struct cthread_descriptor_t))
|
||||||
|
|
||||||
|
struct spawner {
|
||||||
|
int (*fun)(void *, int);
|
||||||
|
void *arg;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int Spawner(void *arg, int tid) {
|
||||||
|
int rc;
|
||||||
|
struct spawner *spawner = arg;
|
||||||
|
rc = spawner->fun(spawner->arg, tid);
|
||||||
|
cthread_ungarbage();
|
||||||
|
free(spawner);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns thread, e.g.
|
* Spawns thread, e.g.
|
||||||
*
|
*
|
||||||
|
@ -78,6 +93,9 @@
|
||||||
*/
|
*/
|
||||||
int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
||||||
struct spawn *th, ths;
|
struct spawn *th, ths;
|
||||||
|
struct spawner *spawner;
|
||||||
|
TlsIsRequired();
|
||||||
|
if (!fun) return einval();
|
||||||
|
|
||||||
// we need to to clobber the output memory before calling clone, since
|
// we need to to clobber the output memory before calling clone, since
|
||||||
// there's no guarantee clone() won't suspend the parent, and focus on
|
// there's no guarantee clone() won't suspend the parent, and focus on
|
||||||
|
@ -102,11 +120,14 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clone(fun, th->stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
spawner = malloc(sizeof(struct spawner));
|
||||||
|
spawner->fun = fun;
|
||||||
|
spawner->arg = arg;
|
||||||
|
if (clone(Spawner, th->stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
||||||
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
|
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
|
||||||
CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
|
CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
|
||||||
CLONE_CHILD_CLEARTID,
|
CLONE_CHILD_CLEARTID,
|
||||||
arg, &th->ptid, th->tib, th->ctid) == -1) {
|
spawner, &th->ptid, th->tib, th->ctid) == -1) {
|
||||||
_freestack(th->stk);
|
_freestack(th->stk);
|
||||||
free(th->tls);
|
free(th->tls);
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -28,7 +28,7 @@ struct FtraceTls { /* 16 */
|
||||||
struct cthread_descriptor_t {
|
struct cthread_descriptor_t {
|
||||||
struct cthread_descriptor_t *self; /* 0x00 */
|
struct cthread_descriptor_t *self; /* 0x00 */
|
||||||
struct FtraceTls ftrace; /* 0x08 */
|
struct FtraceTls ftrace; /* 0x08 */
|
||||||
void *garbages; /* 0x10 */
|
void *garbages; /* 0x18 */
|
||||||
locale_t locale; /* 0x20 */
|
locale_t locale; /* 0x20 */
|
||||||
pthread_t pthread; /* 0x28 */
|
pthread_t pthread; /* 0x28 */
|
||||||
struct cthread_descriptor_t *self2; /* 0x30 */
|
struct cthread_descriptor_t *self2; /* 0x30 */
|
||||||
|
@ -64,6 +64,7 @@ int cthread_sem_wait(cthread_sem_t *, int, const struct timespec *);
|
||||||
int cthread_sem_signal(cthread_sem_t *);
|
int cthread_sem_signal(cthread_sem_t *);
|
||||||
int cthread_memory_wait32(int *, int, const struct timespec *);
|
int cthread_memory_wait32(int *, int, const struct timespec *);
|
||||||
int cthread_memory_wake32(int *, int);
|
int cthread_memory_wake32(int *, int);
|
||||||
|
void cthread_ungarbage(void);
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
│ │
|
│ │
|
||||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
│ any purpose with or without fee is hereby granted, provided that the │
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
@ -16,27 +16,19 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/log/log.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/nexgen32e/gettls.h"
|
||||||
/* clang-format off */
|
#include "libc/thread/thread.h"
|
||||||
|
|
||||||
/**
|
void cthread_ungarbage(void) {
|
||||||
* Prints list of deferred operations on shadow stack w/o symbols.
|
struct Garbages *g;
|
||||||
*/
|
if ((g = ((cthread_t)__get_tls())->garbages)) {
|
||||||
void PrintGarbageNumeric(FILE *f) {
|
// _pthread_exit() uses _gclongjmp() so if this assertion fails,
|
||||||
size_t i;
|
// then the likely cause is the thread used gc() with longjmp().
|
||||||
f = stderr;
|
assert(!g->i);
|
||||||
fprintf(f, "\n");
|
free(g->p);
|
||||||
fprintf(f, " SHADOW STACK @ 0x%016lx\n", __builtin_frame_address(0));
|
free(g);
|
||||||
fprintf(f, " garbage entry parent frame original ret callback arg \n");
|
|
||||||
fprintf(f, "-------------- -------------- ------------------ ------------------ ------------------\n");
|
|
||||||
for (i = __garbage.i; i--;) {
|
|
||||||
fprintf(f, "0x%012lx 0x%012lx 0x%016lx 0x%016lx 0x%016lx\n",
|
|
||||||
__garbage.p + i,
|
|
||||||
__garbage.p[i].frame,
|
|
||||||
__garbage.p[i].ret,
|
|
||||||
__garbage.p[i].fn,
|
|
||||||
__garbage.p[i].arg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,6 +16,7 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/internal.h"
|
#include "libc/calls/internal.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
|
|
|
@ -20,12 +20,14 @@
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/nexgen32e/gc.internal.h"
|
#include "libc/nexgen32e/gc.internal.h"
|
||||||
#include "libc/nexgen32e/nexgen32e.h"
|
#include "libc/nexgen32e/nexgen32e.h"
|
||||||
|
#include "libc/runtime/gc.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
#include "libc/testlib/ezbench.h"
|
#include "libc/testlib/ezbench.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
#include "libc/thread/spawn.h"
|
||||||
#include "libc/x/x.h"
|
#include "libc/x/x.h"
|
||||||
|
|
||||||
#define GC(x) _defer(Free, x)
|
#define GC(x) _defer(Free, x)
|
||||||
|
@ -77,6 +79,45 @@ TEST(gclongjmp, test) {
|
||||||
free(x);
|
free(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void crawl(const char *path) {
|
||||||
|
const char *dir;
|
||||||
|
if (!strcmp(path, "/") || !strcmp(path, ".")) return;
|
||||||
|
crawl(_gc(xdirname(path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int Worker(void *arg, int tid) {
|
||||||
|
crawl("a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(gc, torture) {
|
||||||
|
int i, n = 32;
|
||||||
|
struct spawn *t = gc(malloc(sizeof(struct spawn) * n));
|
||||||
|
for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, _spawn(Worker, 0, t + i));
|
||||||
|
for (i = 0; i < n; ++i) EXPECT_SYS(0, 0, _join(t + i));
|
||||||
|
}
|
||||||
|
|
||||||
|
void crawl2(jmp_buf jb, const char *path) {
|
||||||
|
const char *dir;
|
||||||
|
if (!strcmp(path, "/") || !strcmp(path, ".")) _gclongjmp(jb, 1);
|
||||||
|
crawl2(jb, _gc(xdirname(path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int Worker2(void *arg, int tid) {
|
||||||
|
jmp_buf jb;
|
||||||
|
if (!setjmp(jb)) {
|
||||||
|
crawl2(jb, "a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(_gclongjmp, torture) {
|
||||||
|
int i, n = 32;
|
||||||
|
struct spawn *t = gc(malloc(sizeof(struct spawn) * n));
|
||||||
|
for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, _spawn(Worker2, 0, t + i));
|
||||||
|
for (i = 0; i < n; ++i) EXPECT_SYS(0, 0, _join(t + i));
|
||||||
|
}
|
||||||
|
|
||||||
dontinline void F1(void) {
|
dontinline void F1(void) {
|
||||||
/* 3x slower than F2() but sooo worth it */
|
/* 3x slower than F2() but sooo worth it */
|
||||||
gc(malloc(16));
|
gc(malloc(16));
|
||||||
|
|
|
@ -36,6 +36,7 @@ TEST_LIBC_NEXGEN32E_DIRECTDEPS = \
|
||||||
LIBC_STR \
|
LIBC_STR \
|
||||||
LIBC_STUBS \
|
LIBC_STUBS \
|
||||||
LIBC_SYSV \
|
LIBC_SYSV \
|
||||||
|
LIBC_THREAD \
|
||||||
LIBC_TESTLIB \
|
LIBC_TESTLIB \
|
||||||
LIBC_X \
|
LIBC_X \
|
||||||
TOOL_VIZ_LIB \
|
TOOL_VIZ_LIB \
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/struct/iovec.h"
|
#include "libc/calls/struct/iovec.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
|
|
|
@ -34,6 +34,7 @@ TEST_LIBC_SOCK_DIRECTDEPS = \
|
||||||
LIBC_STR \
|
LIBC_STR \
|
||||||
LIBC_STUBS \
|
LIBC_STUBS \
|
||||||
LIBC_SYSV \
|
LIBC_SYSV \
|
||||||
|
LIBC_LOG \
|
||||||
LIBC_SYSV_CALLS \
|
LIBC_SYSV_CALLS \
|
||||||
LIBC_TESTLIB \
|
LIBC_TESTLIB \
|
||||||
LIBC_X \
|
LIBC_X \
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
|
|
1
third_party/quickjs/call.c
vendored
1
third_party/quickjs/call.c
vendored
|
@ -25,6 +25,7 @@
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "third_party/quickjs/internal.h"
|
#include "third_party/quickjs/internal.h"
|
||||||
|
|
||||||
asm(".ident\t\"\\n\\n\
|
asm(".ident\t\"\\n\\n\
|
||||||
|
|
1
third_party/smallz4/smallz4cat.c
vendored
1
third_party/smallz4/smallz4cat.c
vendored
|
@ -29,6 +29,7 @@
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
|
|
||||||
|
|
1
third_party/tidy/config.c
vendored
1
third_party/tidy/config.c
vendored
|
@ -18,6 +18,7 @@
|
||||||
#include "libc/calls/calls.h"
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "third_party/tidy/tags.h"
|
#include "third_party/tidy/tags.h"
|
||||||
|
|
||||||
#ifdef WINDOWS_OS
|
#ifdef WINDOWS_OS
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/elf/def.h"
|
#include "libc/elf/def.h"
|
||||||
#include "libc/log/check.h"
|
#include "libc/log/check.h"
|
||||||
#include "libc/mem/arraylist2.internal.h"
|
#include "libc/mem/arraylist2.internal.h"
|
||||||
|
|
|
@ -16,10 +16,12 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/struct/stat.h"
|
#include "libc/calls/struct/stat.h"
|
||||||
#include "libc/log/check.h"
|
#include "libc/log/check.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/sysv/consts/o.h"
|
#include "libc/sysv/consts/o.h"
|
||||||
#include "libc/x/x.h"
|
#include "libc/x/x.h"
|
||||||
#include "tool/build/lib/psk.h"
|
#include "tool/build/lib/psk.h"
|
||||||
|
|
|
@ -22,6 +22,7 @@ TOOL_HASH_DIRECTDEPS = \
|
||||||
LIBC_RUNTIME \
|
LIBC_RUNTIME \
|
||||||
LIBC_STDIO \
|
LIBC_STDIO \
|
||||||
LIBC_STR \
|
LIBC_STR \
|
||||||
|
LIBC_MEM \
|
||||||
LIBC_STUBS
|
LIBC_STUBS
|
||||||
|
|
||||||
TOOL_HASH_DEPS := \
|
TOOL_HASH_DEPS := \
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include "dsp/scale/scale.h"
|
#include "dsp/scale/scale.h"
|
||||||
#include "dsp/tty/quant.h"
|
#include "dsp/tty/quant.h"
|
||||||
#include "dsp/tty/tty.h"
|
#include "dsp/tty/tty.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/calls/ioctl.h"
|
#include "libc/calls/ioctl.h"
|
||||||
#include "libc/calls/struct/stat.h"
|
#include "libc/calls/struct/stat.h"
|
||||||
#include "libc/calls/struct/winsize.h"
|
#include "libc/calls/struct/winsize.h"
|
||||||
|
|
Loading…
Add table
Reference in a new issue