Elevate Windows production worthiness

- SQLite file locking now works on Windows
- SQLite will now use fdatasync() on non-Apple platforms
- Fix Ctrl-C handler on Windows to not crash with TLS
- Signals now work in multithreaded apps on Windows
- fcntl() will now accurately report EINVAL errors
- fcntl() now has excellent --strace logging
- Token bucket replenish now go 100x faster
- *NSYNC cancellations now work on Windows
- Support closefrom() on NetBSD
This commit is contained in:
Justine Tunney 2022-10-13 13:44:41 -07:00
parent d38700687a
commit 997ce29ddc
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
95 changed files with 959 additions and 418 deletions

View file

@ -43,6 +43,7 @@
#include "libc/thread/openbsd.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/xnu.internal.h"
#define __NR_thr_new 455
@ -97,11 +98,7 @@ WinThreadEntry(int rdi, // rcx
int rdx, // r8
struct CloneArgs *wt) { // r9
int rc;
if (wt->tls) {
asm("mov\t%1,%%gs:%0"
: "=m"(*((long *)0x1480 + __tls_index))
: "r"(wt->tls));
}
if (wt->tls) __set_tls_win32(wt->tls);
*wt->ptid = wt->tid;
*wt->ctid = wt->tid;
rc = WinThreadLaunch(wt->arg, wt->tid, wt->func, (intptr_t)wt & -16);

View file

@ -48,7 +48,7 @@
#define STATE_QUOTED_VAR 4
#define STATE_WHITESPACE 5
#define READ24LE(s) READ32LE(s "\0")
#define READ24(s) READ32LE(s "\0")
struct Env {
char *s;
@ -333,16 +333,16 @@ static int Test(void) {
if (n == 4) {
w = READ32LE(args[2]) & 0x00ffffff;
if ((w & 65535) == READ16LE("=")) return !!strcmp(args[1], args[3]);
if (w == READ24LE("==")) return !!strcmp(args[1], args[3]);
if (w == READ24LE("!=")) return !strcmp(args[1], args[3]);
if (w == READ24("==")) return !!strcmp(args[1], args[3]);
if (w == READ24("!=")) return !strcmp(args[1], args[3]);
} else if (n == 3) {
w = READ32LE(args[1]) & 0x00ffffff;
if (w == READ24LE("-n")) return !(strlen(args[2]) > 0);
if (w == READ24LE("-z")) return !(strlen(args[2]) == 0);
if (w == READ24LE("-e")) return !!stat(args[2], &st);
if (w == READ24LE("-f")) return !stat(args[2], &st) && S_ISREG(st.st_mode);
if (w == READ24LE("-d")) return !stat(args[2], &st) && S_ISDIR(st.st_mode);
if (w == READ24LE("-h")) return !stat(args[2], &st) && S_ISLNK(st.st_mode);
if (w == READ24("-n")) return !(strlen(args[2]) > 0);
if (w == READ24("-z")) return !(strlen(args[2]) == 0);
if (w == READ24("-e")) return !!stat(args[2], &st);
if (w == READ24("-f")) return !(!stat(args[2], &st) && S_ISREG(st.st_mode));
if (w == READ24("-d")) return !(!stat(args[2], &st) && S_ISDIR(st.st_mode));
if (w == READ24("-h")) return !(!stat(args[2], &st) && S_ISLNK(st.st_mode));
}
return 1;
}

View file

@ -19,17 +19,15 @@
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/thread/tls.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/tls.h"
extern int __threadcalls_end[];
extern int __threadcalls_start[];
#pragma weak __threadcalls_start
#pragma weak __threadcalls_end
privileged void __enable_threads(void) {
if (__threaded) return;
STRACE("__enable_threads()");
static privileged dontinline void FixupLocks(void) {
__morph_begin();
/*
* _NOPL("__threadcalls", func)
@ -55,5 +53,11 @@ privileged void __enable_threads(void) {
_base[*p + 2] = 0xe8;
}
__morph_end();
}
void __enable_threads(void) {
if (__threaded) return;
STRACE("__enable_threads()");
FixupLocks();
__threaded = gettid();
}

View file

@ -23,6 +23,7 @@
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/asancodes.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/weaken.h"
#include "libc/log/libfatal.internal.h"
@ -99,6 +100,7 @@ _Alignas(TLS_ALIGNMENT) static char __static_tls[5008];
* and your `errno` variable also won't be thread safe anymore.
*/
privileged void __enable_tls(void) {
int tid;
size_t siz;
struct CosmoTib *tib;
char *mem, *tls;
@ -133,12 +135,13 @@ privileged void __enable_tls(void) {
if (IsLinux()) {
// gnu/systemd guarantees pid==tid for the main thread so we can
// avoid issuing a superfluous system call at startup in program
tib->tib_tid = __pid;
tid = __pid;
} else {
tib->tib_tid = sys_gettid();
tid = sys_gettid();
}
atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed);
_pthread_main.tib = tib;
_pthread_main.tid = tib->tib_tid;
_pthread_main.tid = tid;
_pthread_main.flags = PT_MAINTHREAD;
__repmovsb(tls, _tdata_start, _TLDZ);

View file

@ -23,6 +23,7 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/process.h"
#include "libc/runtime/internal.h"
@ -56,7 +57,9 @@ int _fork(uint32_t dwCreationFlags) {
parent = __pid;
__pid = dx;
if (__tls_enabled) {
__get_tls()->tib_tid = IsLinux() ? dx : sys_gettid();
atomic_store_explicit(&__get_tls()->tib_tid,
IsLinux() ? dx : sys_gettid(),
memory_order_relaxed);
}
if (!IsWindows()) sys_sigprocmask(SIG_SETMASK, &old, 0);
STRACE("fork() → 0 (child of %d)", parent);