Improve locks and signals

- Introduce fast spinlock API
- Double rand64() perf w/ spinlock
- Improve raise() on New Technology
- Support gettid() across platforms
- Implement SA_NODEFER on New Technology
- Move the lock intrinsics into LIBC_INTRIN
- Make SIGTRAP recoverable on New Technology
- Block SIGCHLD in wait4() on New Technology
- Add threading prototypes for XNU and FreeBSD
- Rewrite abort() fixing its minor bugs on XNU/NT
- Shave down a lot of the content in libc/bits/bits.h
- Let signal handlers modify CPU registers on New Technology
This commit is contained in:
Justine Tunney 2022-04-12 05:20:17 -07:00
parent f68f1789bd
commit 046c7ebd4a
110 changed files with 1514 additions and 876 deletions

View file

@ -1,61 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/dce.h"
#include "libc/runtime/internal.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/nr.h"
#include "libc/macros.internal.h"
.privileged
// Terminates program abnormally.
//
// This function first tries to trigger your SIGABRT handler. If
// there isn't one or execution resumes, then abort() terminates
// the program using an escalating variety methods of increasing
// brutality.
//
// @forcealignargpointer
// @asyncsignalsafe
// @noreturn
abort: push %rbp
mov %rsp,%rbp
and $-16,%rsp
sub $16,%rsp
#if SupportsWindows()
testb IsWindows()
jnz sys_abort_nt
#endif
mov SIG_SETMASK,%edi
mov %rsp,%rsi
push $0xffffffffffffffdf # all bits blocked but SIGABRT
push $0xffffffffffffffff # assumes von neum. arithmetic
pop 8(%rsi)
pop (%rsi)
xor %edx,%edx # don't care about old sigmask
pushpop 4*4,%r10 # sizeof(sigset_t) for systemd
mov __NR_sigprocmask,%eax # sys_sigprocmask is hookable
syscall
mov __NR_getpid,%eax
syscall
mov %eax,%edi
mov SIGABRT,%esi
mov __NR_kill,%eax
syscall # avoid hook and less bt noise
call _Exit
.endfn abort,globl,protected

View file

@ -1,7 +1,7 @@
/*-*- 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
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
@ -16,22 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/typedef/sigaction_f.h"
#include "libc/calls/calls.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
textwindows wontreturn void sys_abort_nt(void) {
int rva;
siginfo_t info;
bzero(&info, sizeof(info));
info.si_signo = SIGABRT;
rva = __sighandrvas[SIGABRT];
if (rva >= kSigactionMinRva) {
if (((sigaction_f)(_base + rva))) {
((sigaction_f)(_base + rva))(SIGABRT, &info, NULL);
}
}
/**
* Terminates program abnormally.
*
* This function first tries to trigger your SIGABRT handler. If
* there isn't one or execution resumes, then abort() terminates
* the program using an escalating variety methods of increasing
* brutality.
*
* @asyncsignalsafe
* @noreturn
*/
privileged void abort(void) {
sigset_t sm;
sigfillset(&sm);
sigdelset(&sm, SIGABRT);
sigprocmask(SIG_SETMASK, &sm, 0);
raise(SIGABRT);
__restorewintty();
_Exit(128 + SIGABRT);
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/asmflag.h"
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
@ -113,12 +114,14 @@ static int arch_prctl_freebsd(int code, int64_t addr) {
static privileged dontinline int arch_prctl_xnu(int code, int64_t addr) {
int ax;
bool failed;
switch (code) {
case ARCH_SET_GS:
asm volatile("syscall"
: "=a"(ax)
: "0"(0x3000003), "D"(addr - 0x8a0 /* wat */)
asm volatile(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(failed), "=a"(ax)
: "1"(0x3000003), "D"(addr - 0x8a0 /* wat */)
: "rcx", "r11", "memory", "cc");
if (failed) errno = ax, ax = -1;
return ax;
case ARCH_GET_FS:
case ARCH_SET_FS:
@ -130,21 +133,30 @@ static privileged dontinline int arch_prctl_xnu(int code, int64_t addr) {
}
static privileged dontinline int arch_prctl_openbsd(int code, int64_t addr) {
bool failed;
int64_t rax;
switch (code) {
case ARCH_GET_FS:
asm volatile("syscall"
: "=a"(rax)
: "0"(0x014a /* __get_tcb */)
asm volatile(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(failed), "=a"(rax)
: "1"(0x014a /* __get_tcb */)
: "rcx", "r11", "cc", "memory");
if (failed) {
errno = rax;
return -1;
}
*(int64_t *)addr = rax;
return 0;
case ARCH_SET_FS:
asm volatile("syscall"
: "=a"(rax)
: "0"(0x0149 /* __set_tcb */), "D"(addr)
asm volatile(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(failed), "=a"(rax)
: "1"(0x0149 /* __set_tcb */), "D"(addr)
: "rcx", "r11", "cc", "memory");
return 0;
if (failed) {
errno = rax;
rax = -1;
}
return rax;
case ARCH_GET_GS:
case ARCH_SET_GS:
return enosys();

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/intrin/lockxchg.h"
#include "libc/runtime/runtime.h"
/**

View file

@ -25,8 +25,10 @@
#include "libc/macros.internal.h"
#include "libc/mem/alloca.h"
#include "libc/nexgen32e/nt2sysv.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/ipc.h"
#include "libc/nt/memory.h"
@ -49,6 +51,7 @@ extern unsigned char __data_start[]; /* αpε */
extern unsigned char __data_end[]; /* αpε */
extern unsigned char __bss_start[]; /* αpε */
extern unsigned char __bss_end[]; /* αpε */
bool32 __onntconsoleevent_nt(uint32_t);
static textwindows char16_t *ParseInt(char16_t *p, int64_t *x) {
*x = 0;
@ -219,6 +222,9 @@ textwindows void WinMainForked(void) {
if (weaken(__wincrash_nt)) {
AddVectoredExceptionHandler(1, (void *)weaken(__wincrash_nt));
}
if (weaken(__onntconsoleevent_nt)) {
SetConsoleCtrlHandler(weaken(__onntconsoleevent_nt), 1);
}
longjmp(jb, 1);
}
@ -257,7 +263,8 @@ textwindows int sys_fork_nt(void) {
}
#endif
if (ntspawn(GetProgramExecutableName(), args, environ, forkvar,
&kNtIsInheritable, NULL, true, 0, NULL, &startinfo,
&kNtIsInheritable, NULL, true,
0 /* kNtCreateNewProcessGroup */, NULL, &startinfo,
&procinfo) != -1) {
CloseHandle(reader);
CloseHandle(procinfo.hThread);

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/intrin/lockxchg.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
@ -84,7 +84,7 @@ privileged noinstrument noasan noubsan void ftracer(void) {
uint64_t stamp;
static bool noreentry;
struct StackFrame *frame;
if (!cmpxchg(&noreentry, 0, 1)) return;
if (!_cmpxchg(&noreentry, 0, 1)) return;
if (ftrace_enabled && g_symbols) {
stamp = rdtsc();
frame = __builtin_frame_address(0);

View file

@ -12,7 +12,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern uint32_t __ntconsolemode[2];
extern uint32_t __ntconsolemode[3];
extern const char v_ntsubsystem[] __attribute__((__weak__));
extern const uintptr_t __fini_array_end[] __attribute__((__weak__));
extern const uintptr_t __fini_array_start[] __attribute__((__weak__));

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/errno.h"
#include "libc/intrin/lockcmpxchg.h"
#include "libc/runtime/runtime.h"
typedef void *pthread_t;
@ -25,7 +25,7 @@ typedef bool pthread_once_t;
typedef int pthread_mutex_t;
int pthread_once(pthread_once_t *once, void init(void)) {
if (lockcmpxchg(once, 0, 1)) init();
if (_lockcmpxchg(once, 0, 1)) init();
return 0;
}

View file

@ -80,15 +80,17 @@ struct WinArgs {
extern int __pid;
extern bool __nomultics;
extern uint32_t __winmainpid;
extern const char kConsoleHandles[2];
extern const char kConsoleHandles[3];
static const short kConsoleModes[2] = {
static const short kConsoleModes[3] = {
kNtEnableProcessedInput | kNtEnableLineInput | kNtEnableEchoInput |
kNtEnableMouseInput | kNtEnableQuickEditMode | kNtEnableExtendedFlags |
kNtEnableAutoPosition | kNtEnableInsertMode |
kNtEnableVirtualTerminalInput,
kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput |
kNtEnableVirtualTerminalProcessing,
kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput |
kNtEnableVirtualTerminalProcessing,
};
forceinline void MakeLongDoubleLongAgain(void) {
@ -128,7 +130,7 @@ static noasan textwindows wontreturn noinstrument void WinMainNew(
STRACE("SetConsoleCP(kNtCpUtf8) → %hhhd", rc);
rc = SetConsoleOutputCP(kNtCpUtf8);
STRACE("SetConsoleOutputCP(kNtCpUtf8) → %hhhd", rc);
for (i = 0; i < 2; ++i) {
for (i = 0; i < 3; ++i) {
hand = GetStdHandle(kConsoleHandles[i]);
rc = GetConsoleMode(hand, __ntconsolemode + i);
STRACE("GetConsoleMode(%p, [%#x]) → %hhhd", hand, __ntconsolemode[i], rc);