mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
- Every unit test now passes on Apple Silicon. The final piece of this puzzle was porting our POSIX threads cancelation support, since that works differently on ARM64 XNU vs. AMD64. Our semaphore support on Apple Silicon is also superior now compared to AMD64, thanks to the grand central dispatch library which lets *NSYNC locks go faster. - The Cosmopolitan runtime is now more stable, particularly on Windows. To do this, thread local storage is mandatory at all runtime levels, and the innermost packages of the C library is no longer being built using ASAN. TLS is being bootstrapped with a 128-byte TIB during the process startup phase, and then later on the runtime re-allocates it either statically or dynamically to support code using _Thread_local. fork() and execve() now do a better job cooperating with threads. We can now check how much stack memory is left in the process or thread when functions like kprintf() / execve() etc. call alloca(), so that ENOMEM can be raised, reduce a buffer size, or just print a warning. - POSIX signal emulation is now implemented the same way kernels do it with pthread_kill() and raise(). Any thread can interrupt any other thread, regardless of what it's doing. If it's blocked on read/write then the killer thread will cancel its i/o operation so that EINTR can be returned in the mark thread immediately. If it's doing a tight CPU bound operation, then that's also interrupted by the signal delivery. Signal delivery works now by suspending a thread and pushing context data structures onto its stack, and redirecting its execution to a trampoline function, which calls SetThreadContext(GetCurrentThread()) when it's done. - We're now doing a better job managing locks and handles. On NetBSD we now close semaphore file descriptors in forked children. Semaphores on Windows can now be canceled immediately, which means mutexes/condition variables will now go faster. Apple Silicon semaphores can be canceled too. We're now using Apple's pthread_yield() funciton. Apple _nocancel syscalls are now used on XNU when appropriate to ensure pthread_cancel requests aren't lost. The MbedTLS library has been updated to support POSIX thread cancelations. See tool/build/runitd.c for an example of how it can be used for production multi-threaded tls servers. Handles on Windows now leak less often across processes. All i/o operations on Windows are now overlapped, which means file pointers can no longer be inherited across dup() and fork() for the time being. - We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4() which means, for example, that posix_spawn() now goes 3x faster. POSIX spawn is also now more correct. Like Musl, it's now able to report the failure code of execve() via a pipe although our approach favors using shared memory to do that on systems that have a true vfork() function. - We now spawn a thread to deliver SIGALRM to threads when setitimer() is used. This enables the most precise wakeups the OS makes possible. - The Cosmopolitan runtime now uses less memory. On NetBSD for example, it turned out the kernel would actually commit the PT_GNU_STACK size which caused RSS to be 6mb for every process. Now it's down to ~4kb. On Apple Silicon, we reduce the mandatory upstream thread size to the smallest possible size to reduce the memory overhead of Cosmo threads. The examples directory has a program called greenbean which can spawn a web server on Linux with 10,000 worker threads and have the memory usage of the process be ~77mb. The 1024 byte overhead of POSIX-style thread-local storage is now optional; it won't be allocated until the pthread_setspecific/getspecific functions are called. On Windows, the threads that get spawned which are internal to the libc implementation use reserve rather than commit memory, which shaves a few hundred kb. - sigaltstack() is now supported on Windows, however it's currently not able to be used to handle stack overflows, since crash signals are still generated by WIN32. However the crash handler will still switch to the alt stack, which is helpful in environments with tiny threads. - Test binaries are now smaller. Many of the mandatory dependencies of the test runner have been removed. This ensures many programs can do a better job only linking the the thing they're testing. This caused the test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb - long double is no longer used in the implementation details of libc, except in the APIs that define it. The old code that used long double for time (instead of struct timespec) has now been thoroughly removed. - ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing backtraces itself, it'll just print a command you can run on the shell using our new `cosmoaddr2line` program to view the backtrace. - Crash report signal handling now works in a much better way. Instead of terminating the process, it now relies on SA_RESETHAND so that the default SIG_IGN behavior can terminate the process if necessary. - Our pledge() functionality has now been fully ported to AARCH64 Linux.
373 lines
12 KiB
C
373 lines
12 KiB
C
/*-*- 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 2023 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 "ape/sections.internal.h"
|
|
#include "libc/assert.h"
|
|
#include "libc/calls/blockcancel.internal.h"
|
|
#include "libc/calls/calls.h"
|
|
#include "libc/calls/struct/aarch64.internal.h"
|
|
#include "libc/calls/struct/rusage.internal.h"
|
|
#include "libc/calls/struct/sigaction.h"
|
|
#include "libc/calls/struct/siginfo.h"
|
|
#include "libc/calls/struct/sigset.h"
|
|
#include "libc/calls/struct/sigset.internal.h"
|
|
#include "libc/calls/struct/ucontext.internal.h"
|
|
#include "libc/calls/struct/utsname.h"
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
#include "libc/calls/ucontext.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/intrin/describebacktrace.internal.h"
|
|
#include "libc/intrin/describeflags.internal.h"
|
|
#include "libc/intrin/kprintf.h"
|
|
#include "libc/log/internal.h"
|
|
#include "libc/log/log.h"
|
|
#include "libc/macros.internal.h"
|
|
#include "libc/mem/mem.h"
|
|
#include "libc/nexgen32e/stackframe.h"
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
#include "libc/runtime/runtime.h"
|
|
#include "libc/runtime/stack.h"
|
|
#include "libc/runtime/symbols.internal.h"
|
|
#include "libc/stdio/stdio.h"
|
|
#include "libc/str/str.h"
|
|
#include "libc/sysv/consts/auxv.h"
|
|
#include "libc/sysv/consts/sig.h"
|
|
#include "libc/thread/thread.h"
|
|
#ifdef __aarch64__
|
|
|
|
__static_yoink("strerror_wr"); // for kprintf %m
|
|
__static_yoink("strsignal_r"); // for kprintf %G
|
|
|
|
#define STACK_ERROR "error: not enough room on stack to print crash report\n"
|
|
|
|
#define RESET "\e[0m"
|
|
#define BOLD "\e[1m"
|
|
#define STRONG "\e[30;101m"
|
|
#define RED "\e[31;1m"
|
|
#define GREEN "\e[32;1m"
|
|
#define BLUE "\e[34;1m"
|
|
#define YELLOW "\e[33;1m"
|
|
#define MAGENTA "\e[35;1m"
|
|
|
|
struct Buffer {
|
|
char *p;
|
|
int n;
|
|
int i;
|
|
};
|
|
|
|
static relegated bool IsCode(uintptr_t p) {
|
|
return __executable_start <= (uint8_t *)p && (uint8_t *)p < _etext;
|
|
}
|
|
|
|
static relegated void Append(struct Buffer *b, const char *fmt, ...) {
|
|
va_list va;
|
|
va_start(va, fmt);
|
|
b->i += kvsnprintf(b->p + b->i, b->n - b->i, fmt, va);
|
|
va_end(va);
|
|
}
|
|
|
|
static relegated const char *ColorRegister(int r) {
|
|
if (__nocolor) return "";
|
|
switch (r) {
|
|
case 0: // arg / res
|
|
case 1: // arg / res
|
|
case 2: // arg / res
|
|
case 3: // arg / res
|
|
case 4: // arg / res
|
|
case 5: // arg / res
|
|
case 6: // arg / res
|
|
case 7: // arg / res
|
|
return GREEN;
|
|
case 9: // volatile
|
|
case 10: // volatile
|
|
case 11: // volatile
|
|
case 12: // volatile
|
|
case 13: // volatile
|
|
case 14: // volatile
|
|
case 15: // volatile
|
|
return BLUE;
|
|
case 19: // saved
|
|
case 20: // saved
|
|
case 21: // saved
|
|
case 22: // saved
|
|
case 23: // saved
|
|
case 24: // saved
|
|
case 25: // saved
|
|
case 26: // saved
|
|
case 27: // saved
|
|
return MAGENTA;
|
|
case 18: // platform register
|
|
case 28: // our tls register
|
|
case 29: // frame pointer
|
|
case 30: // return pointer
|
|
case 31: // stack pointer
|
|
return RED;
|
|
default: // miscellaneous registers
|
|
return YELLOW;
|
|
}
|
|
}
|
|
|
|
static relegated bool AppendFileLine(struct Buffer *b, const char *addr2line,
|
|
const char *debugbin, long addr) {
|
|
ssize_t rc;
|
|
char *p, *q, buf[128];
|
|
int j, k, ws, pid, pfd[2];
|
|
if (!debugbin || !*debugbin) return false;
|
|
if (!addr2line || !*addr2line) return false;
|
|
if (sys_pipe(pfd)) return false;
|
|
ksnprintf(buf, sizeof(buf), "%lx", addr);
|
|
if ((pid = vfork()) == -1) {
|
|
sys_close(pfd[1]);
|
|
sys_close(pfd[0]);
|
|
return false;
|
|
}
|
|
if (!pid) {
|
|
sys_close(pfd[0]);
|
|
sys_dup2(pfd[1], 1, 0);
|
|
sys_close(2);
|
|
sys_execve(
|
|
addr2line,
|
|
(char *const[]){(char *)addr2line, "-pifCe", (char *)debugbin, buf, 0},
|
|
(char *const[]){0});
|
|
_Exit(127);
|
|
}
|
|
sys_close(pfd[1]);
|
|
// copy addr2line stdout to buffer. normally it is "file:line\n".
|
|
// however additional lines can get created for inline functions.
|
|
j = b->i;
|
|
Append(b, "in ");
|
|
k = b->i;
|
|
while ((rc = sys_read(pfd[0], buf, sizeof(buf))) > 0) {
|
|
Append(b, "%.*s", (int)rc, buf);
|
|
}
|
|
// remove the annoying `foo.c:123 (discriminator 3)` suffixes, because
|
|
// they break emacs, and who on earth knows what those things mean lol
|
|
while ((p = memmem(b->p + k, b->i - k, " (discriminator ", 16)) &&
|
|
(q = memchr(p + 16, '\n', (b->p + b->i) - (p + 16)))) {
|
|
memmove(p, q, (b->p + b->i) - q);
|
|
b->i -= q - p;
|
|
}
|
|
// mop up after the process and sanity check captured text
|
|
sys_close(pfd[0]);
|
|
if (sys_wait4(pid, &ws, 0, 0) != -1 && !ws && b->p[k] != ':' &&
|
|
b->p[k] != '?' && b->p[b->i - 1] == '\n') {
|
|
--b->i; // chomp last newline
|
|
return true;
|
|
} else {
|
|
b->i = j; // otherwise reset the buffer
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static relegated char *GetSymbolName(struct SymbolTable *st, int symbol,
|
|
char **mem, size_t *memsz) {
|
|
char *s, *t;
|
|
if ((s = __get_symbol_name(st, symbol)) && //
|
|
s[0] == '_' && s[1] == 'Z' && //
|
|
(t = __cxa_demangle(s, *mem, memsz, 0))) {
|
|
*mem = s = t;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
static relegated void __oncrash_impl(int sig, struct siginfo *si,
|
|
ucontext_t *ctx) {
|
|
#pragma GCC push_options
|
|
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
|
long size = __get_safe_size(10000, 4096);
|
|
if (size < 80) {
|
|
// almost certainly guaranteed to succeed
|
|
klog(STACK_ERROR, sizeof(STACK_ERROR) - 1);
|
|
__restore_tty();
|
|
__minicrash(sig, si, ctx);
|
|
return;
|
|
}
|
|
char *buf = alloca(size);
|
|
CheckLargeStackAllocation(buf, size);
|
|
#pragma GCC pop_options
|
|
int i, j;
|
|
const char *kind;
|
|
const char *reset;
|
|
const char *strong;
|
|
char host[64] = "unknown";
|
|
struct utsname names = {0};
|
|
struct Buffer b[1] = {{buf, size}};
|
|
b->p[b->i++] = '\n';
|
|
ftrace_enabled(-1);
|
|
strace_enabled(-1);
|
|
__restore_tty();
|
|
uname(&names);
|
|
gethostname(host, sizeof(host));
|
|
reset = !__nocolor ? RESET : "";
|
|
strong = !__nocolor ? STRONG : "";
|
|
if (__is_stack_overflow(si, ctx)) {
|
|
kind = "\e[7mStack Overflow\e[0m";
|
|
} else {
|
|
kind = DescribeSiCode(sig, si->si_code);
|
|
}
|
|
Append(b, "%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n", strong, reset,
|
|
sig, kind, host, getpid(), gettid());
|
|
if (program_invocation_name) {
|
|
Append(b, " %s\n", program_invocation_name);
|
|
}
|
|
if (errno) {
|
|
Append(b, " %s\n", strerror(errno));
|
|
}
|
|
Append(b, " %s %s %s %s\n", names.sysname, names.version, names.nodename,
|
|
names.release);
|
|
if (ctx) {
|
|
long pc;
|
|
char *mem = 0;
|
|
size_t memsz = 0;
|
|
int addend, symbol;
|
|
const char *debugbin;
|
|
const char *addr2line;
|
|
struct StackFrame *fp;
|
|
struct SymbolTable *st;
|
|
struct fpsimd_context *vc;
|
|
st = GetSymbolTable();
|
|
debugbin = FindDebugBinary();
|
|
addr2line = GetAddr2linePath();
|
|
|
|
if (sig == SIGFPE || //
|
|
sig == SIGILL || //
|
|
sig == SIGBUS || //
|
|
sig == SIGSEGV || //
|
|
sig == SIGTRAP) {
|
|
Append(b, " faulting address is %016lx\n", si->si_addr);
|
|
}
|
|
|
|
// PRINT REGISTERS
|
|
for (i = 0; i < 8; ++i) {
|
|
Append(b, " ");
|
|
for (j = 0; j < 4; ++j) {
|
|
int r = 8 * j + i;
|
|
if (j) Append(b, " ");
|
|
Append(b, "%s%016lx%s x%d%s", ColorRegister(r),
|
|
ctx->uc_mcontext.regs[r], reset, r, r == 8 || r == 9 ? " " : "");
|
|
}
|
|
Append(b, "\n");
|
|
}
|
|
|
|
// PRINT VECTORS
|
|
vc = (struct fpsimd_context *)ctx->uc_mcontext.__reserved;
|
|
if (vc->head.magic == FPSIMD_MAGIC) {
|
|
int n = 16;
|
|
while (n && !vc->vregs[n - 1] && !vc->vregs[n - 2]) n -= 2;
|
|
for (i = 0; i * 2 < n; ++i) {
|
|
Append(b, " ");
|
|
for (j = 0; j < 2; ++j) {
|
|
int r = j + 2 * i;
|
|
if (j) Append(b, " ");
|
|
Append(b, "%016lx ..%s %016lx v%d%s", (long)(vc->vregs[r] >> 64),
|
|
!j ? "" : ".", (long)vc->vregs[r], r, r < 10 ? " " : "");
|
|
}
|
|
Append(b, "\n");
|
|
}
|
|
}
|
|
|
|
// PRINT CURRENT LOCATION
|
|
//
|
|
// We can get the address of the currently executing function by
|
|
// simply examining the program counter.
|
|
pc = ctx->uc_mcontext.pc;
|
|
Append(b, " %016lx sp %lx pc", ctx->uc_mcontext.sp, pc);
|
|
if (pc && st && (symbol = __get_symbol(st, pc))) {
|
|
addend = pc - st->addr_base;
|
|
addend -= st->symbols[symbol].x;
|
|
Append(b, " ");
|
|
if (!AppendFileLine(b, addr2line, debugbin, pc)) {
|
|
Append(b, "%s", GetSymbolName(st, symbol, &mem, &memsz));
|
|
if (addend) Append(b, "%+d", addend);
|
|
}
|
|
}
|
|
Append(b, "\n");
|
|
|
|
// PRINT LINKED LOCATION
|
|
//
|
|
// The x30 register can usually tell us the address of the parent
|
|
// function. This can help us determine the caller in cases where
|
|
// stack frames aren't being generated by the compiler; but if we
|
|
// have stack frames, then we need to ensure this won't duplicate
|
|
// the first element of the frame pointer backtrace below.
|
|
fp = (struct StackFrame *)ctx->uc_mcontext.regs[29];
|
|
if (IsCode((pc = ctx->uc_mcontext.regs[30]))) {
|
|
Append(b, " %016lx sp %lx lr", ctx->uc_mcontext.sp, pc);
|
|
if (pc && st && (symbol = __get_symbol(st, pc))) {
|
|
addend = pc - st->addr_base;
|
|
addend -= st->symbols[symbol].x;
|
|
Append(b, " ");
|
|
if (!AppendFileLine(b, addr2line, debugbin, pc)) {
|
|
Append(b, "%s", GetSymbolName(st, symbol, &mem, &memsz));
|
|
if (addend) Append(b, "%+d", addend);
|
|
}
|
|
}
|
|
Append(b, "\n");
|
|
if (fp && !kisdangerous(fp) && pc == fp->addr) {
|
|
fp = fp->next;
|
|
}
|
|
}
|
|
|
|
// PRINT FRAME POINTERS
|
|
//
|
|
// The prologues and epilogues of non-leaf functions should save
|
|
// the frame pointer (x29) and return address (x30) to the stack
|
|
// and then set x29 to sp, which is the address of the new frame
|
|
// effectively creating a daisy chain letting us trace back into
|
|
// the origin of execution, e.g. _start(), or sys_clone_linux().
|
|
for (i = 0; fp; fp = fp->next) {
|
|
if (kisdangerous(fp)) {
|
|
Append(b, " %016lx <dangerous fp>\n", fp);
|
|
break;
|
|
}
|
|
if (++i == 100) {
|
|
Append(b, " <truncated backtrace>\n");
|
|
break;
|
|
}
|
|
if (st && (pc = fp->addr)) {
|
|
if ((symbol = __get_symbol(st, pc))) {
|
|
addend = pc - st->addr_base;
|
|
addend -= st->symbols[symbol].x;
|
|
} else {
|
|
addend = 0;
|
|
}
|
|
} else {
|
|
symbol = 0;
|
|
addend = 0;
|
|
}
|
|
Append(b, " %016lx fp %lx lr ", fp, pc);
|
|
if (!AppendFileLine(b, addr2line, debugbin, pc) && st) {
|
|
Append(b, "%s", GetSymbolName(st, symbol, &mem, &memsz));
|
|
if (addend) Append(b, "%+d", addend);
|
|
}
|
|
Append(b, "\n");
|
|
}
|
|
free(mem);
|
|
}
|
|
b->p[b->n - 1] = '\n';
|
|
sys_write(2, b->p, MIN(b->i, b->n));
|
|
}
|
|
|
|
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
|
ucontext_t *ctx = arg;
|
|
BLOCK_CANCELLATIONS;
|
|
__oncrash_impl(sig, si, ctx);
|
|
ALLOW_CANCELLATIONS;
|
|
}
|
|
|
|
#endif /* __aarch64__ */
|