Fix XNU / FreeBSD / OpenBSD / RHEL5 / NT bugs

For the first time ever, all tests in this codebase now pass, when
run automatically on macos, freebsd, openbsd, rhel5, rhel7, alpine
and windows via the network using the runit and runitd build tools

- Fix vfork exec path etc.
- Add XNU opendir() support
- Add OpenBSD opendir() support
- Add Linux history to syscalls.sh
- Use copy_file_range on FreeBSD 13+
- Fix system calls with 7+ arguments
- Fix Windows with greater than 16 FDs
- Fix RUNIT.COM and RUNITD.COM flakiness
- Fix OpenBSD munmap() when files are mapped
- Fix long double so it's actually long on Windows
- Fix OpenBSD truncate() and ftruncate() thunk typo
- Let Windows fcntl() be used on socket files descriptors
- Fix Windows fstat() which had an accidental printf statement
- Fix RHEL5 CLOCK_MONOTONIC by not aliasing to CLOCK_MONOTONIC_RAW

This is wonderful. I never could have dreamed it would be possible
to get it working so well on so many platforms with tiny binaries.

Fixes #31
Fixes #25
Fixes #14
This commit is contained in:
Justine Tunney 2021-01-25 13:08:05 -08:00
parent c20dad3534
commit 45b72485ad
1032 changed files with 6083 additions and 2348 deletions

View file

@ -311,6 +311,11 @@ static void *__asan_pvalloc(size_t n) {
return __asan_valloc(ROUNDUP(n, PAGESIZE));
}
static int __asan_malloc_trim(size_t pad) {
__asan_morgue_flush();
return dlmalloc_trim(pad);
}
void __asan_register_globals(struct AsanGlobal g[], int n) {
unsigned i;
for (i = 0; i < n; ++i) {
@ -382,6 +387,7 @@ void __asan_install_malloc_hooks(void) {
HOOK(hook$pvalloc, __asan_pvalloc);
HOOK(hook$realloc, __asan_realloc);
HOOK(hook$memalign, __asan_memalign);
HOOK(hook$malloc_trim, __asan_malloc_trim);
HOOK(hook$malloc_usable_size, __asan_malloc_usable_size);
}

View file

@ -18,7 +18,6 @@
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/fmt/fmt.h"
#include "libc/log/gdb.h"
#include "libc/log/log.h"
@ -52,12 +51,12 @@
* @note this is called via eponymous spinlock macro wrapper
*/
relegated int(attachdebugger)(intptr_t continuetoaddr) {
int ttyfd;
int pid, ttyfd;
struct StackFrame *bp;
char pidstr[11], breakcmd[40];
const char *se, *elf, *gdb, *rewind, *layout;
if (IsGenuineCosmo() || !(gdb = GetGdbPath()) ||
(ttyfd = open(_PATH_TTY, O_RDWR, 0)) == -1) {
(ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
return -1;
}
write(ttyfd, RESTORE_TTY, strlen(RESTORE_TTY));
@ -81,20 +80,24 @@ relegated int(attachdebugger)(intptr_t continuetoaddr) {
rewind = NULL;
breakcmd[0] = '\0';
}
return spawnve(0, (int[3]){ttyfd, ttyfd, STDERR_FILENO}, gdb,
(char *const[]){
"gdb", "--tui",
"-p", pidstr,
se, elf,
"-ex", "set osabi GNU/Linux",
"-ex", "set complaints 0",
"-ex", "set confirm off",
"-ex", layout,
"-ex", "layout reg",
"-ex", "set var g_gdbsync = 1",
"-q", rewind,
breakcmd, "-ex",
"c", NULL,
},
environ);
if (!(pid = vfork())) {
dup2(ttyfd, 0);
dup2(ttyfd, 1);
execv(gdb, (char *const[]){
"gdb", "--tui",
"-p", pidstr,
se, elf,
"-ex", "set osabi GNU/Linux",
"-ex", "set complaints 0",
"-ex", "set confirm off",
"-ex", layout,
"-ex", "layout reg",
"-ex", "set var g_gdbsync = 1",
"-q", rewind,
breakcmd, "-ex",
"c", NULL,
});
abort();
}
return pid;
}

View file

@ -21,7 +21,6 @@
#include "libc/bits/safemacros.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
@ -32,6 +31,7 @@
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#define kBacktraceMaxFrames 128
#define kBacktraceBufSize ((kBacktraceMaxFrames - 1) * (16 + 1))
@ -40,7 +40,7 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
ssize_t got;
intptr_t addr;
size_t i, j, gi;
int rc, pid, tubes[3];
int rc, pid, pipefds[2];
struct Garbages *garbage;
const struct StackFrame *frame;
const char *debugbin, *p1, *p2, *p3, *addr2line;
@ -67,13 +67,16 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
j += snprintf(&buf[j], 17, "%#x", addr - 1) + 1;
}
argv[i++] = NULL;
tubes[0] = STDIN_FILENO;
tubes[1] = -1;
tubes[2] = STDERR_FILENO;
if ((pid = spawnve(0, tubes, addr2line, argv, environ)) == -1) {
return -1;
pipe(pipefds);
if (!(pid = vfork())) {
dup2(pipefds[1], 1);
close(pipefds[0]);
close(pipefds[1]);
execvp(addr2line, argv);
abort();
}
while ((got = read(tubes[1], buf, kBacktraceBufSize)) > 0) {
close(pipefds[1]);
while ((got = read(pipefds[0], buf, kBacktraceBufSize)) > 0) {
for (p1 = buf; got;) {
/*
* remove racist output from gnu tooling, that can't be disabled
@ -95,7 +98,7 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
}
}
}
close(tubes[1]);
close(pipefds[0]);
if (waitpid(pid, &rc, 0) == -1) return -1;
if (WEXITSTATUS(rc) != 0) return -1;
return 0;

View file

@ -46,18 +46,22 @@ relegated void __check_fail(const char *suffix, const char *opstr,
size_t i;
va_list va;
char sufbuf[8];
char hostname[32];
int lasterr = errno;
__start_fatal(file, line);
if (!memccpy(sufbuf, suffix, '\0', sizeof(sufbuf))) strcpy(sufbuf, "?");
strtoupper(sufbuf);
strcpy(hostname, "unknown");
gethostname(hostname, sizeof(hostname));
(dprintf)(STDERR_FILENO,
"check failed\r\n"
"check failed on %s pid %d\r\n"
"\tCHECK_%s(%s, %s);\r\n"
"\t\t → %#lx (%s)\r\n"
"\t\t%s %#lx (%s)\r\n",
sufbuf, wantstr, gotstr, want, wantstr, opstr, got, gotstr);
hostname, getpid(), sufbuf, wantstr, gotstr, want, wantstr, opstr,
got, gotstr);
if (!isempty(fmt)) {
(dprintf)(STDERR_FILENO, "\t");

View file

@ -18,7 +18,6 @@
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/fmt/fmt.h"
#include "libc/log/gdb.h"
#include "libc/log/log.h"
@ -30,8 +29,8 @@
* Attachs GDB temporarilly, to do something like print a variable.
*/
int(gdbexec)(const char *cmd) {
int ttyin, ttyout;
struct StackFrame *bp;
int pid, ttyin, ttyout;
intptr_t continuetoaddr;
const char *se, *elf, *gdb;
char pidstr[11], breakcmd[40];
@ -45,33 +44,34 @@ int(gdbexec)(const char *cmd) {
}
bp = __builtin_frame_address(0);
continuetoaddr = bp->addr;
snprintf(breakcmd, sizeof(breakcmd), "%s *%#p", "break", bp->addr);
char *const args[] = {
"gdb",
"--nx",
"--nh",
"-p",
pidstr,
se,
elf,
"-ex",
"set osabi GNU/Linux",
"-ex",
"set complaints 0",
"-ex",
"set confirm off",
"-ex",
"set var g_gdbsync = 1",
"-q",
"-ex",
breakcmd,
"-ex",
cmd,
"-ex",
"quit",
NULL,
};
return SYNCHRONIZE_DEBUGGER(spawnve(0, NULL, gdb, args, environ));
sprintf(breakcmd, "%s *%#p", "break", bp->addr);
if (!(pid = vfork())) {
execv(gdb, (char *const[]){
"gdb",
"--nx",
"--nh",
"-p",
pidstr,
se,
elf,
"-ex",
"set osabi GNU/Linux",
"-ex",
"set complaints 0",
"-ex",
"set confirm off",
"-ex",
"set var g_gdbsync = 1",
"-q",
"-ex",
breakcmd,
"-ex",
cmd,
"-ex",
"quit",
NULL,
});
abort();
}
return SYNCHRONIZE_DEBUGGER(pid);
}

View file

@ -115,7 +115,11 @@ relegated static void ShowGeneralRegisters(int fd, ucontext_t *ctx) {
ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]]);
if (++j == 3) {
j = 0;
memcpy(&st, (char *)&ctx->fpustate.st[k], sizeof(st));
if (ctx->uc_mcontext.fpregs) {
memcpy(&st, (char *)&ctx->uc_mcontext.fpregs->st[k], sizeof(st));
} else {
memset(&st, 0, sizeof(st));
}
(dprintf)(fd, " %s(%zu) %Lf", "ST", k, st);
++k;
write(fd, "\r\n", 2);
@ -126,12 +130,15 @@ relegated static void ShowGeneralRegisters(int fd, ucontext_t *ctx) {
relegated static void ShowSseRegisters(int fd, ucontext_t *ctx) {
size_t i;
write(fd, "\r\n\r\n", 4);
for (i = 0; i < 8; ++i) {
(dprintf)(fd, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\r\n"),
"XMM", i + 0, ctx->fpustate.xmm[i + 0].u64[0],
ctx->fpustate.xmm[i + 0].u64[1], "XMM", i + 8,
ctx->fpustate.xmm[i + 8].u64[0], ctx->fpustate.xmm[i + 8].u64[1]);
if (ctx->uc_mcontext.fpregs) {
write(fd, "\r\n\r\n", 4);
for (i = 0; i < 8; ++i) {
(dprintf)(fd, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\r\n"),
"XMM", i + 0, ctx->uc_mcontext.fpregs->xmm[i + 0].u64[1],
ctx->uc_mcontext.fpregs->xmm[i + 0].u64[0], "XMM", i + 8,
ctx->uc_mcontext.fpregs->xmm[i + 8].u64[1],
ctx->uc_mcontext.fpregs->xmm[i + 8].u64[0]);
}
}
}
@ -153,10 +160,14 @@ relegated static void ShowMemoryMappings(int outfd) {
relegated static void ShowCrashReport(int err, int fd, int sig,
ucontext_t *ctx) {
int i;
char hostname[64];
struct utsname names;
(dprintf)(fd, VEIL("r", "\r\n%serror%s: Uncaught SIG%s\r\n %s\r\n %s\r\n"),
RED2, RESET, TinyStrSignal(sig), getauxval(AT_EXECFN),
strerror(err));
strcpy(hostname, "unknown");
gethostname(hostname, sizeof(hostname));
(dprintf)(
fd, VEIL("r", "\r\n%serror%s: Uncaught SIG%s on %s\r\n %s\r\n %s\r\n"),
RED2, RESET, TinyStrSignal(sig), hostname, getauxval(AT_EXECFN),
strerror(err));
if (uname(&names) != -1) {
(dprintf)(fd, VEIL("r", " %s %s %s %s\r\n"), names.sysname, names.nodename,
names.release, names.version);

View file

@ -86,7 +86,7 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
long double t2;
const char *prog;
int64_t secs, nsec, dots;
char timebuf[32], *timebufp;
char buf32[32], *buf32p;
if (!f) f = g_logfile;
if (fileno(f) == -1) return;
t2 = nowl();
@ -94,17 +94,17 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
nsec = rem1000000000int64(t2 * 1e9L);
if (secs > ts.tv_sec) {
localtime_r(&secs, &tm);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%S.", &tm);
timebufp = timebuf;
strftime(buf32, sizeof(buf32), "%Y-%m-%dT%H:%M:%S.", &tm);
buf32p = buf32;
dots = nsec;
} else {
timebufp = "--------------------";
buf32p = "--------------------";
dots = nsec - ts.tv_nsec;
}
ts.tv_sec = secs;
ts.tv_nsec = nsec;
prog = basename(program_invocation_name);
if ((fprintf)(f, "%c%s%06ld:%s:%d:%.*s:%d] ", loglevel2char(level), timebufp,
if ((fprintf)(f, "%c%s%06ld:%s:%d:%.*s:%d] ", loglevel2char(level), buf32p,
rem1000000int64(div1000int64(dots)), file, line,
strchrnul(prog, '.') - prog, prog, getpid()) <= 0) {
vflogf_onfail(f);
@ -114,7 +114,9 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
fputs("\n", f);
if (level == kLogFatal) {
__start_fatal(file, line);
(dprintf)(STDERR_FILENO, "fatal error see logfile\n");
strcpy(buf32, "unknown");
gethostname(buf32, sizeof(buf32));
(dprintf)(STDERR_FILENO, "fatality %s pid %d\n", buf32, getpid());
__die();
unreachable;
}