2022-03-16 20:33:13 +00:00
|
|
|
/*-*- 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 2021 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-04-27 03:45:01 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2023-05-10 13:17:33 +00:00
|
|
|
#include "ape/sections.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/fmt/divmod10.internal.h"
|
|
|
|
#include "libc/fmt/fmt.h"
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2023-06-09 08:23:18 +00:00
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
|
|
|
#include "libc/intrin/asancodes.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/intrin/asmflag.h"
|
2022-10-13 20:44:41 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-09-06 04:43:49 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/intrin/cmpxchg.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/intrin/getenv.internal.h"
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-09-06 04:43:49 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/intrin/nomultics.internal.h"
|
2022-09-06 04:43:49 +00:00
|
|
|
#include "libc/intrin/safemacros.internal.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-09-06 04:43:49 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/limits.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/log/internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/macros.internal.h"
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/mem/alloca.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/nexgen32e/rdtsc.h"
|
|
|
|
#include "libc/nexgen32e/uart.internal.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/nt/createfile.h"
|
|
|
|
#include "libc/nt/enum/accessmask.h"
|
|
|
|
#include "libc/nt/enum/creationdisposition.h"
|
|
|
|
#include "libc/nt/enum/fileflagandattributes.h"
|
|
|
|
#include "libc/nt/enum/filesharemode.h"
|
|
|
|
#include "libc/nt/errors.h"
|
|
|
|
#include "libc/nt/files.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/nt/process.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2023-05-13 05:42:57 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2023-06-10 16:15:19 +00:00
|
|
|
#include "libc/stdckdint.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/str/tab.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/str/utf16.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
2023-07-26 20:54:49 +00:00
|
|
|
#include "libc/sysv/consts/auxv.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
|
|
|
#include "libc/sysv/consts/fd.h"
|
|
|
|
#include "libc/sysv/consts/fileno.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/sysv/consts/nr.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/thread/posixthread.internal.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2023-09-02 03:49:13 +00:00
|
|
|
#include "libc/thread/tls2.internal.h"
|
2022-10-06 12:36:15 +00:00
|
|
|
#include "libc/vga/vga.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
#define STACK_ERROR "kprintf error: stack is about to overflow\n"
|
|
|
|
|
2023-05-10 05:41:57 +00:00
|
|
|
#define KGETINT(x, va, t, s) \
|
|
|
|
switch (t) { \
|
|
|
|
case -3: \
|
|
|
|
x = !!va_arg(va, int); \
|
|
|
|
break; \
|
|
|
|
case -2: \
|
|
|
|
if (s) { \
|
|
|
|
x = (signed char)va_arg(va, int); \
|
|
|
|
} else { \
|
|
|
|
x = (unsigned char)va_arg(va, int); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
case -1: \
|
|
|
|
if (s) { \
|
|
|
|
x = (signed short)va_arg(va, int); \
|
|
|
|
} else { \
|
|
|
|
x = (unsigned short)va_arg(va, int); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
case 0: \
|
|
|
|
default: \
|
|
|
|
if (s) { \
|
|
|
|
x = va_arg(va, int); \
|
|
|
|
} else { \
|
|
|
|
x = va_arg(va, unsigned int); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
case 1: \
|
|
|
|
if (s) { \
|
|
|
|
x = va_arg(va, long); \
|
|
|
|
} else { \
|
|
|
|
x = va_arg(va, unsigned long); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
case 2: \
|
|
|
|
if (s) { \
|
|
|
|
x = va_arg(va, long long); \
|
|
|
|
} else { \
|
|
|
|
x = va_arg(va, unsigned long long); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
// clang-format off
|
|
|
|
__msabi extern typeof(CreateFile) *const __imp_CreateFileW;
|
|
|
|
__msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle;
|
|
|
|
__msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariableW;
|
|
|
|
__msabi extern typeof(GetLastError) *const __imp_GetLastError;
|
|
|
|
__msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle;
|
|
|
|
__msabi extern typeof(SetLastError) *const __imp_SetLastError;
|
|
|
|
__msabi extern typeof(WriteFile) *const __imp_WriteFile;
|
|
|
|
// clang-format on
|
|
|
|
|
2023-07-24 15:31:54 +00:00
|
|
|
long __klog_handle;
|
2023-10-03 21:40:03 +00:00
|
|
|
extern struct SymbolTable *__symtab;
|
2022-05-19 23:57:49 +00:00
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline char *kadvance(char *p, char *e, long n) {
|
2022-03-16 20:33:13 +00:00
|
|
|
intptr_t t = (intptr_t)p;
|
2023-06-10 16:15:19 +00:00
|
|
|
if (ckd_add(&t, t, n)) t = (intptr_t)e;
|
2022-03-16 20:33:13 +00:00
|
|
|
return (char *)t;
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline char *kemitquote(char *p, char *e, signed char t, unsigned c) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (t) {
|
|
|
|
if (p < e) {
|
|
|
|
*p = t < 0 ? 'u' : 'L';
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (p < e) {
|
|
|
|
*p = c;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline bool kiskernelpointer(const void *p) {
|
2022-03-16 20:33:13 +00:00
|
|
|
return 0x7f0000000000 <= (intptr_t)p && (intptr_t)p < 0x800000000000;
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline bool kistextpointer(const void *p) {
|
2023-05-19 02:05:08 +00:00
|
|
|
return __executable_start <= (const unsigned char *)p &&
|
|
|
|
(const unsigned char *)p < _etext;
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline bool kisimagepointer(const void *p) {
|
2023-05-19 02:05:08 +00:00
|
|
|
return __executable_start <= (const unsigned char *)p &&
|
|
|
|
(const unsigned char *)p < _end;
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline bool kischarmisaligned(const char *p, signed char t) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (t == -1) return (intptr_t)p & 1;
|
|
|
|
if (t >= 1) return !!((intptr_t)p & 3);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
__funline bool kismemtrackhosed(void) {
|
2022-09-13 06:10:38 +00:00
|
|
|
return !((_weaken(_mmi)->i <= _weaken(_mmi)->n) &&
|
|
|
|
(_weaken(_mmi)->p == _weaken(_mmi)->s ||
|
|
|
|
_weaken(_mmi)->p == (struct MemoryInterval *)kMemtrackStart));
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
privileged static bool kismapped(int x) {
|
2022-04-24 16:59:22 +00:00
|
|
|
// xxx: we can't lock because no reentrant locks yet
|
2022-03-16 20:33:13 +00:00
|
|
|
size_t m, r, l = 0;
|
2022-09-13 06:10:38 +00:00
|
|
|
if (!_weaken(_mmi)) return true;
|
2022-03-16 20:33:13 +00:00
|
|
|
if (kismemtrackhosed()) return false;
|
2022-09-13 06:10:38 +00:00
|
|
|
r = _weaken(_mmi)->i;
|
2022-03-16 20:33:13 +00:00
|
|
|
while (l < r) {
|
2023-07-10 17:16:55 +00:00
|
|
|
m = (l & r) + ((l ^ r) >> 1); // floor((a+b)/2)
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(_mmi)->p[m].y < x) {
|
2022-03-16 20:33:13 +00:00
|
|
|
l = m + 1;
|
|
|
|
} else {
|
|
|
|
r = m;
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 06:10:38 +00:00
|
|
|
if (l < _weaken(_mmi)->i && x >= _weaken(_mmi)->p[l].x) {
|
|
|
|
return !!(_weaken(_mmi)->p[l].prot & PROT_READ);
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
privileged bool kisdangerous(const void *p) {
|
|
|
|
int frame;
|
|
|
|
if (kisimagepointer(p)) return false;
|
|
|
|
if (kiskernelpointer(p)) return false;
|
2023-05-13 05:42:57 +00:00
|
|
|
if (IsOldStack(p)) return false;
|
2022-03-16 20:33:13 +00:00
|
|
|
if (IsLegalPointer(p)) {
|
2023-05-13 05:42:57 +00:00
|
|
|
frame = (uintptr_t)p >> 16;
|
2022-03-16 20:33:13 +00:00
|
|
|
if (IsStackFrame(frame)) return false;
|
|
|
|
if (kismapped(frame)) return false;
|
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
if (GetStackAddr() + GetGuardSize() <= (uintptr_t)p &&
|
2023-05-13 05:42:57 +00:00
|
|
|
(uintptr_t)p < GetStackAddr() + GetStackSize()) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
privileged static void klogclose(long fd) {
|
|
|
|
#ifdef __x86_64__
|
|
|
|
long ax = __NR_close;
|
|
|
|
asm volatile("syscall"
|
|
|
|
: "+a"(ax), "+D"(fd)
|
|
|
|
: /* inputs already specified */
|
|
|
|
: "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long x0 asm("x0") = fd;
|
|
|
|
register int x8 asm("x8") = __NR_close;
|
|
|
|
register int x16 asm("x16") = __NR_close;
|
|
|
|
asm volatile("svc\t0" : "+r"(x0) : "r"(x8), "r"(x16) : "x9", "memory");
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
privileged static long klogfcntl(long fd, long cmd, long arg) {
|
|
|
|
#ifdef __x86_64__
|
|
|
|
char cf;
|
|
|
|
long ax = __NR_fcntl;
|
|
|
|
asm volatile("clc\n\tsyscall"
|
|
|
|
: CFLAG_CONSTRAINT(cf), "+a"(ax), "+D"(fd), "+S"(cmd), "+d"(arg)
|
|
|
|
: /* inputs already specified */
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
|
|
|
if (cf) ax = -ax;
|
|
|
|
return ax;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long x0 asm("x0") = fd;
|
|
|
|
register long x1 asm("x1") = cmd;
|
|
|
|
register long x2 asm("x2") = arg;
|
|
|
|
register int x8 asm("x8") = __NR_fcntl;
|
|
|
|
register int x16 asm("x16") = __NR_fcntl;
|
|
|
|
asm volatile("mov\tx9,0\n\t" // clear carry flag
|
|
|
|
"adds\tx9,x9,0\n\t" // clear carry flag
|
|
|
|
"svc\t0\n\t"
|
|
|
|
"bcs\t1f\n\t"
|
|
|
|
"b\t2f\n1:\t"
|
|
|
|
"neg\tx0,x0\n2:"
|
|
|
|
: "+r"(x0)
|
|
|
|
: "r"(x1), "r"(x2), "r"(x8), "r"(x16)
|
|
|
|
: "x9", "memory");
|
|
|
|
return x0;
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
privileged static long klogopen(const char *path) {
|
|
|
|
long dirfd = AT_FDCWD;
|
|
|
|
long flags = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
long mode = 0600;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
char cf;
|
|
|
|
long ax = __NR_openat;
|
|
|
|
register long r10 asm("r10") = mode;
|
|
|
|
asm volatile(CFLAG_ASM("clc\n\tsyscall")
|
|
|
|
: CFLAG_CONSTRAINT(cf), "+a"(ax), "+D"(dirfd), "+S"(path),
|
|
|
|
"+d"(flags), "+r"(r10)
|
|
|
|
: /* inputs already specified */
|
|
|
|
: "rcx", "r8", "r9", "r11", "memory");
|
|
|
|
if (cf) ax = -ax;
|
|
|
|
return ax;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long x0 asm("x0") = dirfd;
|
|
|
|
register long x1 asm("x1") = (long)path;
|
|
|
|
register long x2 asm("x2") = flags;
|
|
|
|
register long x3 asm("x3") = mode;
|
|
|
|
register int x8 asm("x8") = __NR_openat;
|
|
|
|
register int x16 asm("x16") = __NR_openat;
|
|
|
|
asm volatile("mov\tx9,0\n\t" // clear carry flag
|
|
|
|
"adds\tx9,x9,0\n\t" // clear carry flag
|
|
|
|
"svc\t0\n\t"
|
|
|
|
"bcs\t1f\n\t"
|
|
|
|
"b\t2f\n1:\t"
|
|
|
|
"neg\tx0,x0\n2:"
|
|
|
|
: "+r"(x0)
|
|
|
|
: "r"(x1), "r"(x2), "r"(x3), "r"(x8), "r"(x16)
|
|
|
|
: "x9", "memory");
|
|
|
|
return x0;
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns log handle or -1 if logging shouldn't happen
|
|
|
|
privileged long kloghandle(void) {
|
|
|
|
// kprintf() needs to own a file descriptor in case apps closes stderr
|
|
|
|
// our close() and dup() implementations will trigger this initializer
|
|
|
|
// to minimize a chance that the user accidentally closes their logger
|
|
|
|
// while at the same time, avoiding a mandatory initialization syscall
|
|
|
|
if (!__klog_handle) {
|
|
|
|
long hand;
|
|
|
|
// setting KPRINTF_LOG="/tmp/foo.log" will override stderr
|
|
|
|
// setting KPRINTF_LOG="INTEGER" logs to a file descriptor
|
|
|
|
// setting KPRINTF_LOG="" shall disable kprintf altogether
|
|
|
|
if (IsMetal()) {
|
|
|
|
hand = STDERR_FILENO;
|
|
|
|
} else if (IsWindows()) {
|
|
|
|
uint32_t e, n;
|
2023-09-02 03:49:13 +00:00
|
|
|
char16_t path[512];
|
2023-08-19 13:41:06 +00:00
|
|
|
e = __imp_GetLastError();
|
|
|
|
n = __imp_GetEnvironmentVariableW(u"KPRINTF_LOG", path, 512);
|
|
|
|
if (!n && __imp_GetLastError() == kNtErrorEnvvarNotFound) {
|
|
|
|
hand = __imp_GetStdHandle(kNtStdErrorHandle);
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
__imp_DuplicateHandle(-1, hand, -1, &hand, 0, false,
|
2023-08-19 13:41:06 +00:00
|
|
|
kNtDuplicateSameAccess);
|
|
|
|
} else if (n && n < 512) {
|
|
|
|
hand = __imp_CreateFileW(
|
2023-08-22 04:03:09 +00:00
|
|
|
path, kNtFileAppendData,
|
2023-09-10 15:12:43 +00:00
|
|
|
kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0,
|
|
|
|
kNtOpenAlways, kNtFileAttributeNormal, 0);
|
2023-08-19 13:41:06 +00:00
|
|
|
} else {
|
|
|
|
hand = -1; // KPRINTF_LOG was empty string or too long
|
|
|
|
}
|
|
|
|
__imp_SetLastError(e);
|
|
|
|
} else {
|
|
|
|
long fd, fd2;
|
|
|
|
bool closefd;
|
|
|
|
const char *path;
|
|
|
|
if (!__NR_write || !__envp) {
|
|
|
|
// it's too early in the initialization process for kprintf
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
path = __getenv(__envp, "KPRINTF_LOG").s;
|
|
|
|
closefd = false;
|
|
|
|
if (!path) {
|
|
|
|
fd = STDERR_FILENO;
|
|
|
|
} else if (*path) {
|
|
|
|
const char *p;
|
|
|
|
for (fd = 0, p = path; *p; ++p) {
|
|
|
|
if ('0' <= *p && *p <= '9') {
|
|
|
|
fd *= 10;
|
|
|
|
fd += *p - '0';
|
|
|
|
} else {
|
|
|
|
fd = klogopen(path);
|
|
|
|
closefd = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
if (fd >= 0) {
|
|
|
|
// avoid interfering with hard-coded assumptions about fds
|
|
|
|
if ((fd2 = klogfcntl(fd, F_DUPFD, 100)) >= 0) {
|
|
|
|
klogfcntl(fd2, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (closefd) {
|
|
|
|
klogclose(fd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// RLIMIT_NOFILE was probably too low for safe duplicate
|
|
|
|
fd2 = fd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd2 = -1;
|
|
|
|
}
|
|
|
|
hand = fd2;
|
|
|
|
}
|
|
|
|
__klog_handle = hand;
|
2023-06-05 11:16:15 +00:00
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
return __klog_handle;
|
2023-06-05 11:16:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 21:32:20 +00:00
|
|
|
privileged void _klog_serial(const char *b, size_t n) {
|
|
|
|
size_t i;
|
|
|
|
uint16_t dx;
|
|
|
|
unsigned char al;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
for (;;) {
|
|
|
|
dx = 0x3F8 + UART_LSR;
|
|
|
|
asm("inb\t%1,%0" : "=a"(al) : "dN"(dx));
|
|
|
|
if (al & UART_TTYTXR) break;
|
|
|
|
asm("pause");
|
|
|
|
}
|
|
|
|
dx = 0x3F8;
|
|
|
|
asm volatile("outb\t%0,%1"
|
|
|
|
: /* no inputs */
|
|
|
|
: "a"(b[i]), "dN"(dx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
privileged void klog(const char *b, size_t n) {
|
2023-05-02 02:43:59 +00:00
|
|
|
#ifdef __x86_64__
|
2022-03-16 20:33:13 +00:00
|
|
|
int e;
|
2023-06-05 11:16:15 +00:00
|
|
|
long h;
|
2022-03-16 20:33:13 +00:00
|
|
|
uint32_t wrote;
|
|
|
|
long rax, rdi, rsi, rdx;
|
2023-08-19 13:41:06 +00:00
|
|
|
if ((h = kloghandle()) == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
if (IsWindows()) {
|
|
|
|
e = __imp_GetLastError();
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
if (!__imp_WriteFile(h, b, n, &wrote, 0)) {
|
|
|
|
__imp_SetLastError(e);
|
|
|
|
__klog_handle = 0;
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
} else if (IsMetal()) {
|
2023-07-09 12:11:25 +00:00
|
|
|
if (_weaken(_klog_vga)) {
|
|
|
|
_weaken(_klog_vga)(b, n);
|
|
|
|
}
|
2023-10-25 21:32:20 +00:00
|
|
|
_klog_serial(b, n);
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
|
|
|
asm volatile("syscall"
|
|
|
|
: "=a"(rax), "=D"(rdi), "=S"(rsi), "=d"(rdx)
|
2023-06-05 11:16:15 +00:00
|
|
|
: "0"(__NR_write), "1"(h), "2"(b), "3"(n)
|
2022-03-16 20:33:13 +00:00
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
|
|
|
|
}
|
2023-05-10 13:17:33 +00:00
|
|
|
#elif defined(__aarch64__)
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
// this isn't a cancelation point because we don't acknowledge eintr
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
// on xnu we use the "nocancel" version of the system call for safety
|
2023-08-19 13:41:06 +00:00
|
|
|
register long r0 asm("x0") = kloghandle();
|
2023-05-02 02:43:59 +00:00
|
|
|
register long r1 asm("x1") = (long)b;
|
|
|
|
register long r2 asm("x2") = (long)n;
|
2023-06-04 08:57:10 +00:00
|
|
|
register long r8 asm("x8") = (long)__NR_write;
|
|
|
|
register long r16 asm("x16") = (long)__NR_write;
|
2023-05-02 02:43:59 +00:00
|
|
|
register long res_x0 asm("x0");
|
2023-05-13 05:42:57 +00:00
|
|
|
asm volatile("svc\t0"
|
2023-05-02 02:43:59 +00:00
|
|
|
: "=r"(res_x0)
|
2023-05-19 02:05:08 +00:00
|
|
|
: "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16)
|
2023-05-13 05:42:57 +00:00
|
|
|
: "memory");
|
2023-05-10 13:17:33 +00:00
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
2023-05-02 02:43:59 +00:00
|
|
|
#endif
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 16:37:17 +00:00
|
|
|
privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
|
|
|
va_list va) {
|
2023-09-02 03:49:13 +00:00
|
|
|
int si;
|
2022-03-16 20:33:13 +00:00
|
|
|
wint_t t, u;
|
|
|
|
const char *abet;
|
|
|
|
signed char type;
|
|
|
|
const char *s, *f;
|
2022-10-16 19:05:08 +00:00
|
|
|
struct CosmoTib *tib;
|
2022-03-16 20:33:13 +00:00
|
|
|
unsigned long long x;
|
|
|
|
unsigned i, j, m, rem, sign, hash, cols, prec;
|
2022-09-10 16:14:40 +00:00
|
|
|
char c, *p, *e, pdot, zero, flip, dang, base, quot, uppr, ansi, z[128];
|
2022-03-16 20:33:13 +00:00
|
|
|
if (kistextpointer(b) || kisdangerous(b)) n = 0;
|
|
|
|
if (!kistextpointer(fmt)) fmt = "!!WONTFMT";
|
|
|
|
p = b;
|
|
|
|
f = fmt;
|
2022-10-10 14:36:07 +00:00
|
|
|
e = p + n; // assume if n was negative e < p will be the case
|
2023-06-09 06:44:03 +00:00
|
|
|
tib = __tls_enabled ? __get_tls_privileged() : 0;
|
2022-03-16 20:33:13 +00:00
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
if (!(c = *f++) || c == '%') break;
|
|
|
|
EmitFormatByte:
|
|
|
|
if (p < e) *p = c;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (!c) break;
|
|
|
|
pdot = 0;
|
|
|
|
flip = 0;
|
|
|
|
dang = 0;
|
|
|
|
hash = 0;
|
|
|
|
sign = 0;
|
|
|
|
prec = 0;
|
|
|
|
quot = 0;
|
|
|
|
type = 0;
|
|
|
|
cols = 0;
|
|
|
|
zero = 0;
|
2022-04-18 15:54:42 +00:00
|
|
|
uppr = 0;
|
2023-06-08 11:37:05 +00:00
|
|
|
ansi = 0;
|
2022-03-16 20:33:13 +00:00
|
|
|
abet = "0123456789abcdef";
|
|
|
|
for (;;) {
|
|
|
|
switch ((c = *f++)) {
|
|
|
|
default:
|
|
|
|
goto EmitFormatByte;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '\0':
|
|
|
|
break;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '.':
|
|
|
|
pdot = 1;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '-':
|
|
|
|
flip = 1;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '#':
|
2022-05-16 20:20:08 +00:00
|
|
|
case '`':
|
2022-03-16 20:33:13 +00:00
|
|
|
hash = '0';
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '_':
|
|
|
|
case ',':
|
|
|
|
case '\'':
|
|
|
|
quot = c;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case ' ':
|
|
|
|
case '+':
|
|
|
|
sign = c;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-04-18 15:54:42 +00:00
|
|
|
case '^':
|
|
|
|
uppr = c;
|
|
|
|
continue;
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'h':
|
|
|
|
--type;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'j':
|
|
|
|
case 'l':
|
|
|
|
case 'z':
|
|
|
|
++type;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '!':
|
|
|
|
dang = 1;
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
si = pdot ? prec : cols;
|
|
|
|
si *= 10;
|
|
|
|
si += c - '0';
|
|
|
|
goto UpdateCols;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case '*':
|
|
|
|
si = va_arg(va, int);
|
|
|
|
UpdateCols:
|
|
|
|
if (pdot) {
|
|
|
|
if (si >= 0) {
|
|
|
|
prec = si;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (si < 0) {
|
|
|
|
flip = 1;
|
|
|
|
si = -si;
|
|
|
|
}
|
|
|
|
cols = si;
|
|
|
|
if (!cols) {
|
|
|
|
zero = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'T':
|
2022-09-06 04:43:49 +00:00
|
|
|
x = (rdtsc() - kStartTsc) / 3 % 86400000000000;
|
2022-03-16 20:33:13 +00:00
|
|
|
goto FormatUnsigned;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'P':
|
2023-10-03 21:47:20 +00:00
|
|
|
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
|
|
|
x = __pid;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
} else if (IsLinux()) {
|
|
|
|
asm volatile("syscall"
|
|
|
|
: "=a"(x)
|
|
|
|
: "0"(__NR_getpid)
|
|
|
|
: "rcx", "rdx", "r11", "memory");
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
x = 666;
|
|
|
|
}
|
|
|
|
if (!__nocolor && p + 7 <= e) {
|
|
|
|
*p++ = '\e';
|
|
|
|
*p++ = '[';
|
|
|
|
*p++ = '1';
|
|
|
|
*p++ = ';';
|
|
|
|
*p++ = '3';
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
*p++ = '0' + x % 7;
|
2023-10-03 21:47:20 +00:00
|
|
|
*p++ = 'm';
|
|
|
|
ansi = 1;
|
|
|
|
}
|
|
|
|
goto FormatDecimal;
|
|
|
|
|
|
|
|
case 'H':
|
2022-10-16 19:05:08 +00:00
|
|
|
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
|
|
|
if (tib) {
|
|
|
|
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
|
2022-04-20 16:56:53 +00:00
|
|
|
} else {
|
2023-06-06 06:35:31 +00:00
|
|
|
x = __pid;
|
2022-04-20 16:56:53 +00:00
|
|
|
}
|
2023-06-18 12:39:31 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
} else if (IsLinux()) {
|
|
|
|
asm volatile("syscall"
|
|
|
|
: "=a"(x)
|
|
|
|
: "0"(__NR_getpid)
|
|
|
|
: "rcx", "rdx", "r11", "memory");
|
|
|
|
#endif
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
2022-07-11 12:55:17 +00:00
|
|
|
x = 666;
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
2023-10-03 21:47:20 +00:00
|
|
|
if (!__nocolor && p + 7 <= e) {
|
|
|
|
// xnu thread ids are always divisible by 8
|
|
|
|
*p++ = '\e';
|
|
|
|
*p++ = '[';
|
|
|
|
*p++ = '1';
|
|
|
|
*p++ = ';';
|
|
|
|
*p++ = '3';
|
|
|
|
*p++ = '0' + x % 7;
|
|
|
|
*p++ = 'm';
|
|
|
|
ansi = 1;
|
|
|
|
}
|
2022-07-11 12:55:17 +00:00
|
|
|
goto FormatDecimal;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'u':
|
|
|
|
case 'd':
|
|
|
|
if (UNLIKELY(type <= -3)) {
|
|
|
|
s = va_arg(va, int) ? "true" : "false";
|
|
|
|
goto FormatString;
|
|
|
|
}
|
2023-05-10 05:41:57 +00:00
|
|
|
KGETINT(x, va, type, c == 'd');
|
2022-03-16 20:33:13 +00:00
|
|
|
FormatDecimal:
|
|
|
|
if ((long long)x < 0 && c != 'u') {
|
|
|
|
x = -x;
|
|
|
|
sign = '-';
|
|
|
|
}
|
|
|
|
FormatUnsigned:
|
|
|
|
if (x && hash) sign = hash;
|
|
|
|
for (i = j = 0;;) {
|
2023-08-11 18:56:35 +00:00
|
|
|
x = __divmod10(x, &rem);
|
2022-03-16 20:33:13 +00:00
|
|
|
z[i++ & 127] = '0' + rem;
|
|
|
|
if (pdot ? i >= prec : !x) break;
|
|
|
|
if (quot && ++j == 3) {
|
|
|
|
z[i++ & 127] = quot;
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EmitNumber:
|
|
|
|
if (flip || pdot) zero = 0;
|
|
|
|
while (zero && sign) {
|
|
|
|
if (p < e) *p = sign;
|
|
|
|
if (cols) --cols;
|
|
|
|
sign >>= 8;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
t = !!sign + !!(sign >> 8);
|
|
|
|
if (!flip && cols >= t) {
|
|
|
|
for (j = i; j < cols - t; ++j) {
|
|
|
|
if (p < e) {
|
|
|
|
*p++ = zero ? '0' : ' ';
|
|
|
|
} else {
|
|
|
|
p = kadvance(p, e, cols - t - j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sign) {
|
|
|
|
if (p < e) *p = sign;
|
|
|
|
sign >>= 8;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
for (j = i; j; ++p) {
|
|
|
|
--j;
|
|
|
|
if (p < e) {
|
|
|
|
*p = z[j & 127];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flip && cols >= t) {
|
|
|
|
for (j = i; j < cols - t; ++j) {
|
|
|
|
if (p < e) {
|
|
|
|
*p++ = ' ';
|
|
|
|
} else {
|
|
|
|
p = kadvance(p, e, cols - t - j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'b':
|
|
|
|
base = 1;
|
|
|
|
if (hash) hash = '0' | 'b' << 8;
|
|
|
|
BinaryNumber:
|
2023-05-10 05:41:57 +00:00
|
|
|
KGETINT(x, va, type, false);
|
2022-03-16 20:33:13 +00:00
|
|
|
FormatNumber:
|
|
|
|
i = 0;
|
|
|
|
m = (1 << base) - 1;
|
|
|
|
if (hash && x) sign = hash;
|
2022-07-18 10:33:32 +00:00
|
|
|
do z[i++ & 127] = abet[x & m];
|
2022-03-16 20:33:13 +00:00
|
|
|
while ((x >>= base) || (pdot && i < prec));
|
|
|
|
goto EmitNumber;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'X':
|
|
|
|
abet = "0123456789ABCDEF";
|
|
|
|
/* fallthrough */
|
|
|
|
case 'x':
|
|
|
|
base = 4;
|
|
|
|
if (hash) hash = '0' | 'x' << 8;
|
|
|
|
goto BinaryNumber;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'o':
|
|
|
|
base = 3;
|
|
|
|
goto BinaryNumber;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'p':
|
|
|
|
x = va_arg(va, intptr_t);
|
|
|
|
if (!x && pdot) pdot = 0;
|
|
|
|
if ((long)x == -1) {
|
|
|
|
pdot = 0;
|
|
|
|
goto FormatDecimal;
|
|
|
|
}
|
|
|
|
hash = '0' | 'x' << 8;
|
|
|
|
base = 4;
|
|
|
|
goto FormatNumber;
|
2022-03-25 14:11:44 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'C':
|
|
|
|
c = 'c';
|
|
|
|
type = 1;
|
2022-03-25 14:11:44 +00:00
|
|
|
// fallthrough
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'c':
|
|
|
|
i = 1;
|
|
|
|
j = 0;
|
|
|
|
x = 0;
|
|
|
|
s = (const char *)&x;
|
|
|
|
t = va_arg(va, int);
|
|
|
|
if (!type) t &= 255;
|
|
|
|
if (hash) {
|
|
|
|
quot = 1;
|
|
|
|
hash = '\'';
|
|
|
|
p = kemitquote(p, e, type, hash);
|
2022-03-25 14:11:44 +00:00
|
|
|
if (cols && type) --cols; // u/L
|
|
|
|
if (cols) --cols; // start quote
|
|
|
|
if (cols) --cols; // end quote
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
goto EmitChar;
|
2022-03-24 15:00:21 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
case 'm': {
|
2023-06-09 08:23:18 +00:00
|
|
|
int e;
|
|
|
|
if (!(e = tib ? tib->tib_errno : __errno) && sign == ' ') {
|
2022-03-16 20:33:13 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2022-04-15 06:39:48 +00:00
|
|
|
type = 0;
|
2023-08-19 13:41:06 +00:00
|
|
|
#if defined(SYSDEBUG) && _NTTRACE
|
|
|
|
strerror_r(e, z, sizeof(z));
|
|
|
|
s = z;
|
|
|
|
#else
|
2023-06-09 08:23:18 +00:00
|
|
|
s = _strerrno(e);
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
if (!s) {
|
2023-08-19 13:41:06 +00:00
|
|
|
FormatInt32(z, e);
|
|
|
|
s = z;
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
#endif
|
2023-06-09 08:23:18 +00:00
|
|
|
goto FormatString;
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2022-03-24 15:00:21 +00:00
|
|
|
|
2022-04-12 15:05:22 +00:00
|
|
|
case 'G':
|
|
|
|
x = va_arg(va, int);
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
s = strsignal_r(x, z);
|
|
|
|
goto FormatString;
|
2022-04-12 15:05:22 +00:00
|
|
|
|
2022-04-16 17:40:23 +00:00
|
|
|
case 't': {
|
2022-05-19 23:57:49 +00:00
|
|
|
// %t will print the &symbol associated with an address. this
|
|
|
|
// requires that some other code linked GetSymbolTable() and
|
|
|
|
// called it beforehand to ensure the symbol table is loaded.
|
|
|
|
// if the symbol table isn't linked or available, then this
|
|
|
|
// routine will display &hexaddr so objdump -dS foo.com.dbg
|
|
|
|
// can be manually consulted to look up the faulting code.
|
2022-04-16 17:40:23 +00:00
|
|
|
int idx;
|
|
|
|
x = va_arg(va, intptr_t);
|
2023-10-03 21:40:03 +00:00
|
|
|
if (_weaken(__symtab) && *_weaken(__symtab) &&
|
2022-09-13 06:10:38 +00:00
|
|
|
(idx = _weaken(__get_symbol)(0, x)) != -1) {
|
2022-04-16 17:40:23 +00:00
|
|
|
if (p + 1 <= e) *p++ = '&';
|
2023-10-03 21:40:03 +00:00
|
|
|
s = (*_weaken(__symtab))->name_base +
|
|
|
|
(*_weaken(__symtab))->names[idx];
|
2022-04-16 17:40:23 +00:00
|
|
|
goto FormatString;
|
|
|
|
}
|
|
|
|
base = 4;
|
|
|
|
hash = '&';
|
|
|
|
goto FormatNumber;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'n':
|
2022-03-24 15:00:21 +00:00
|
|
|
// nonstandard %n specifier
|
2022-03-16 20:33:13 +00:00
|
|
|
if (p < e) *p = '\n';
|
|
|
|
++p;
|
|
|
|
break;
|
2022-03-24 15:00:21 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 'r':
|
2022-03-24 15:00:21 +00:00
|
|
|
// undocumented %r specifier
|
|
|
|
// used for good carriage return
|
|
|
|
// helps integrate loggers with repls
|
2023-10-04 05:34:45 +00:00
|
|
|
if (!__ttyconf.replstderr || __nocolor) {
|
2022-03-16 20:33:13 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2022-05-12 13:43:59 +00:00
|
|
|
s = "\r\e[K";
|
2022-03-16 20:33:13 +00:00
|
|
|
goto FormatString;
|
|
|
|
}
|
2022-03-24 15:00:21 +00:00
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
case 's':
|
|
|
|
if (!(s = va_arg(va, const void *))) {
|
|
|
|
s = sign != ' ' ? "NULL" : "";
|
|
|
|
FormatString:
|
|
|
|
hash = 0;
|
2022-04-16 17:40:23 +00:00
|
|
|
type = 0;
|
2022-03-16 20:33:13 +00:00
|
|
|
} else if (!dang && (kisdangerous(s) || kischarmisaligned(s, type))) {
|
|
|
|
if (sign == ' ') {
|
|
|
|
if (p < e) *p = ' ';
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
x = (intptr_t)s;
|
|
|
|
base = 4;
|
|
|
|
hash = '!' | '!' << 8;
|
|
|
|
goto FormatNumber;
|
|
|
|
} else if (hash) {
|
|
|
|
quot = 1;
|
|
|
|
hash = '"';
|
2022-03-25 14:11:44 +00:00
|
|
|
if (cols && type) --cols; // u/L
|
|
|
|
if (cols) --cols; // start quote
|
|
|
|
if (cols) --cols; // end quote
|
2022-03-16 20:33:13 +00:00
|
|
|
p = kemitquote(p, e, type, hash);
|
|
|
|
}
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
if (sign == ' ' && (!pdot || prec) && s && *s) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (p < e) *p = ' ';
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
for (i = j = 0; !pdot || j < prec; ++j) {
|
2023-07-30 01:44:15 +00:00
|
|
|
if (UNLIKELY(!((intptr_t)s & 4095))) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (!dang && kisdangerous(s)) break;
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
if (!(t = *s++ & 255)) break;
|
|
|
|
if ((t & 0300) == 0200) goto ActuallyEmitByte;
|
|
|
|
++i;
|
|
|
|
EmitByte:
|
2022-04-18 15:54:42 +00:00
|
|
|
if (uppr && 'a' <= t && t <= 'z') {
|
|
|
|
t -= 'a' - 'A';
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
if (UNLIKELY(quot) && (t == '\\' || ((t == '"' && c == 's') ||
|
|
|
|
(t == '\'' && c == 'c')))) {
|
|
|
|
if (p + 2 <= e) {
|
|
|
|
p[0] = '\\';
|
|
|
|
p[1] = t;
|
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
i += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (pdot ||
|
|
|
|
(t != 0x7F && (t >= 0x20 || (t == '\n' || t == '\t' ||
|
|
|
|
t == '\r' || t == '\e')))) {
|
|
|
|
ActuallyEmitByte:
|
|
|
|
if (p < e) *p = t;
|
|
|
|
p += 1;
|
|
|
|
continue;
|
|
|
|
} else if (quot) {
|
|
|
|
if (p + 4 <= e) {
|
|
|
|
p[0] = '\\';
|
|
|
|
p[1] = '0' + ((t & 0300) >> 6);
|
|
|
|
p[2] = '0' + ((t & 0070) >> 3);
|
|
|
|
p[3] = '0' + ((t & 0007) >> 0);
|
|
|
|
}
|
|
|
|
p += 4;
|
|
|
|
i += 3;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Control Pictures
|
|
|
|
═══════════════════════════════════════════════════════
|
|
|
|
2400 │ 0 1 2 3 4 5 6 7 8 9 a b c d e f
|
|
|
|
───────────────────────────────────────────────────────
|
|
|
|
2400 │ ␀ ␁ ␂ ␃ ␄ ␅ ␆ ␇ ␈ ␉ ␊ ␋ ␌ ␍ ␎ ␏
|
|
|
|
2410 │ ␐ ␑ ␒ ␓ ␔ ␕ ␖ ␗ ␘ ␙ ␚ ␛ ␜ ␝ ␞ ␟
|
|
|
|
2420 │ ␠ ␡ ␢ ␣  ␥ ␦ */
|
|
|
|
if (t != 0x7F) {
|
|
|
|
t += 0x2400;
|
|
|
|
} else {
|
|
|
|
t = 0x2421;
|
|
|
|
}
|
|
|
|
goto EmitChar;
|
|
|
|
}
|
|
|
|
} else if (type < -1) {
|
2022-03-20 15:01:14 +00:00
|
|
|
if ((t = *s++ & 255) || prec) {
|
2022-03-16 20:33:13 +00:00
|
|
|
t = kCp437[t];
|
|
|
|
}
|
|
|
|
} else if (type < 0) {
|
|
|
|
t = *(const char16_t *)s;
|
|
|
|
s += sizeof(char16_t);
|
|
|
|
if (IsHighSurrogate(t)) {
|
|
|
|
if (!pdot || j + 1 < prec) {
|
2023-07-30 01:44:15 +00:00
|
|
|
if (UNLIKELY(!((intptr_t)s & 4095))) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (!dang && kisdangerous(s)) break;
|
|
|
|
}
|
|
|
|
u = *(const char16_t *)s;
|
|
|
|
if (IsLowSurrogate(u)) {
|
|
|
|
t = MergeUtf16(t, u);
|
|
|
|
s += sizeof(char16_t);
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!t) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = *(const wchar_t *)s;
|
|
|
|
s += sizeof(wchar_t);
|
|
|
|
}
|
|
|
|
if (!t) break;
|
|
|
|
++i;
|
|
|
|
EmitChar:
|
2022-04-18 15:54:42 +00:00
|
|
|
if (t <= 0x7f) goto EmitByte;
|
|
|
|
if (uppr) {
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(towupper)) {
|
|
|
|
t = _weaken(towupper)(t);
|
2022-04-18 15:54:42 +00:00
|
|
|
} else if (uppr && 'a' <= t && t <= 'z') {
|
|
|
|
t -= 'a' - 'A';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t <= 0x7ff) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (p + 2 <= e) {
|
|
|
|
p[0] = 0300 | (t >> 6);
|
|
|
|
p[1] = 0200 | (t & 077);
|
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
} else if (t <= 0xffff) {
|
|
|
|
if (UNLIKELY(IsSurrogate(t))) {
|
|
|
|
EncodeReplacementCharacter:
|
|
|
|
t = 0xfffd;
|
|
|
|
}
|
|
|
|
if (p + 3 <= e) {
|
|
|
|
p[0] = 0340 | (t >> 12);
|
|
|
|
p[1] = 0200 | ((t >> 6) & 077);
|
|
|
|
p[2] = 0200 | (t & 077);
|
|
|
|
}
|
|
|
|
p += 3;
|
|
|
|
} else if (~(t >> 18) & 007) {
|
|
|
|
if (p + 4 <= e) {
|
|
|
|
p[0] = 0360 | (t >> 18);
|
|
|
|
p[1] = 0200 | ((t >> 12) & 077);
|
|
|
|
p[2] = 0200 | ((t >> 6) & 077);
|
|
|
|
p[3] = 0200 | (t & 077);
|
|
|
|
}
|
|
|
|
p += 4;
|
|
|
|
} else {
|
|
|
|
goto EncodeReplacementCharacter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hash) {
|
|
|
|
if (p < e) *p = hash;
|
|
|
|
++p;
|
|
|
|
}
|
2022-05-20 11:46:42 +00:00
|
|
|
while (cols > i) {
|
|
|
|
if (p + 8 < e && cols - i > 8) {
|
|
|
|
WRITE64LE(p, 0x2020202020202020);
|
|
|
|
cols -= 8;
|
|
|
|
p += 8;
|
|
|
|
} else if (p < e) {
|
2022-03-16 20:33:13 +00:00
|
|
|
*p++ = ' ';
|
2022-05-20 11:46:42 +00:00
|
|
|
--cols;
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
|
|
|
p = kadvance(p, e, cols - i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-09-10 16:14:40 +00:00
|
|
|
if (ansi) {
|
|
|
|
if (p + 4 <= e) {
|
|
|
|
*p++ = '\e';
|
|
|
|
*p++ = '[';
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = 'm';
|
|
|
|
}
|
2023-06-08 11:37:05 +00:00
|
|
|
ansi = 0;
|
2022-09-10 16:14:40 +00:00
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p < e) {
|
|
|
|
*p = 0;
|
|
|
|
} else if (e > b) {
|
|
|
|
u = 0;
|
|
|
|
*--e = 0;
|
|
|
|
s = "\n...";
|
|
|
|
if (!(((f - fmt) >= 2 && f[-2] == '\n') ||
|
|
|
|
((f - fmt) >= 3 && f[-3] == '%' && f[-2] == 'n'))) {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
while ((t = *s++) && e > b) {
|
|
|
|
u = *--e;
|
|
|
|
*e = t;
|
|
|
|
}
|
|
|
|
if ((u & 0300) == 0200) {
|
|
|
|
while (e > b) {
|
|
|
|
u = *--e;
|
|
|
|
*e = '.';
|
|
|
|
if ((u & 0300) != 0200) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Privileged snprintf().
|
|
|
|
*
|
|
|
|
* @param b is buffer, and guaranteed a NUL-terminator if `n>0`
|
|
|
|
* @param n is number of bytes available in buffer
|
|
|
|
* @return length of output excluding NUL, which may exceed `n`
|
|
|
|
* @see kprintf() for documentation
|
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
|
|
|
*/
|
|
|
|
privileged size_t ksnprintf(char *b, size_t n, const char *fmt, ...) {
|
2022-04-12 12:20:17 +00:00
|
|
|
size_t m;
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
2022-06-12 16:37:17 +00:00
|
|
|
m = kformat(b, n, fmt, v);
|
2022-04-12 12:20:17 +00:00
|
|
|
va_end(v);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
/**
|
|
|
|
* Privileged vsnprintf().
|
|
|
|
*
|
|
|
|
* @param b is buffer, and guaranteed a NUL-terminator if `n>0`
|
|
|
|
* @param n is number of bytes available in buffer
|
|
|
|
* @return length of output excluding NUL, which may exceed `n`
|
|
|
|
* @see kprintf() for documentation
|
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
|
|
|
*/
|
|
|
|
privileged size_t kvsnprintf(char *b, size_t n, const char *fmt, va_list v) {
|
2022-06-12 16:37:17 +00:00
|
|
|
return kformat(b, n, fmt, v);
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Privileged vprintf.
|
|
|
|
*
|
|
|
|
* @see kprintf() for documentation
|
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
|
|
|
*/
|
|
|
|
privileged void kvprintf(const char *fmt, va_list v) {
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
2023-10-13 14:43:47 +00:00
|
|
|
long size = __get_safe_size(8000, 3000);
|
|
|
|
if (size < 80) {
|
|
|
|
klog(STACK_ERROR, sizeof(STACK_ERROR) - 1);
|
|
|
|
return;
|
|
|
|
}
|
Make improvements
- 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.
2023-09-19 03:44:45 +00:00
|
|
|
char *buf = alloca(size);
|
|
|
|
CheckLargeStackAllocation(buf, size);
|
|
|
|
#pragma GCC pop_options
|
|
|
|
size_t count = kformat(buf, size, fmt, v);
|
|
|
|
klog(buf, MIN(count, size - 1));
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Privileged printf().
|
|
|
|
*
|
|
|
|
* This function is intended for crash reporting. It's designed to be as
|
|
|
|
* unbreakable as possible, so that error messages can always be printed
|
|
|
|
* even when the rest of the runtime is broken. As such, it has continue
|
|
|
|
* on error semantics, doesn't support buffering between invocations and
|
|
|
|
* floating point is not supported. Output is also truncated if the line
|
|
|
|
* gets too long, but care is taken to preserve your newline characters.
|
|
|
|
* Your errno and GetLastError() state will not be clobbered, and ftrace
|
|
|
|
* and other runtime magic won't be invoked, since all the runtime magic
|
|
|
|
* depends on this function.
|
|
|
|
*
|
|
|
|
* Directives:
|
|
|
|
*
|
|
|
|
* %[FLAGS][WIDTH|*][.[PRECISION|*]][TYPE]SPECIFIER
|
|
|
|
*
|
|
|
|
* Specifiers:
|
|
|
|
*
|
|
|
|
* - `c` char
|
|
|
|
* - `o` octal
|
|
|
|
* - `b` binary
|
|
|
|
* - `s` string
|
2022-04-16 17:40:23 +00:00
|
|
|
* - `t` symbol
|
2022-03-16 20:33:13 +00:00
|
|
|
* - `p` pointer
|
|
|
|
* - `d` decimal
|
|
|
|
* - `n` newline
|
|
|
|
* - `u` unsigned
|
|
|
|
* - `r` carriage
|
|
|
|
* - `m` strerror
|
2022-04-12 15:05:22 +00:00
|
|
|
* - `G` strsignal
|
2022-03-16 20:33:13 +00:00
|
|
|
* - `X` uppercase
|
|
|
|
* - `T` timestamp
|
|
|
|
* - `x` hexadecimal
|
2023-10-03 21:47:20 +00:00
|
|
|
* - `P` process id
|
|
|
|
* - `H` thread id
|
2022-03-16 20:33:13 +00:00
|
|
|
*
|
|
|
|
* Types:
|
|
|
|
*
|
|
|
|
* - `hhh` bool
|
|
|
|
* - `hh` char or cp437
|
|
|
|
* - `h` short or char16_t
|
|
|
|
* - `l` long or wchar_t
|
|
|
|
* - `ll` long long
|
|
|
|
*
|
|
|
|
* Flags:
|
|
|
|
*
|
|
|
|
* - `0` zero padding
|
|
|
|
* - `-` flip alignment
|
|
|
|
* - `!` bypass memory safety
|
|
|
|
* - `,` thousands grouping w/ comma
|
|
|
|
* - `'` thousands grouping w/ apostrophe
|
|
|
|
* - `_` thousands grouping w/ underscore
|
|
|
|
* - `+` plus leftpad if positive (aligns w/ negatives)
|
|
|
|
* - ` ` space leftpad if positive (aligns w/ negatives)
|
|
|
|
* - `#` represent value with literal syntax, e.g. 0x, 0b, quotes
|
2022-04-18 15:54:42 +00:00
|
|
|
* - `^` uppercasing w/ towupper() if linked, otherwise toupper()
|
2022-03-16 20:33:13 +00:00
|
|
|
*
|
2022-04-15 06:39:48 +00:00
|
|
|
* Error numbers:
|
|
|
|
*
|
|
|
|
* - `%m` formats error (if strerror_wr if is linked)
|
|
|
|
* - `%m` formats errno number (if strerror_wr isn't linked)
|
|
|
|
* - `% m` formats error with leading space if errno isn't zero
|
|
|
|
* - `%lm` means favor WSAGetLastError() over GetLastError() if linked
|
|
|
|
*
|
2022-04-16 17:40:23 +00:00
|
|
|
* You need to link and load the symbol table before `%t` will work. You
|
|
|
|
* can do that by calling `GetSymbolTable()`. If that hasn't happened it
|
|
|
|
* will print `&hexnumber` instead.
|
|
|
|
*
|
2022-03-16 20:33:13 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
|
|
|
*/
|
|
|
|
privileged void kprintf(const char *fmt, ...) {
|
2023-08-19 13:41:06 +00:00
|
|
|
// system call support runtime depends on this function
|
|
|
|
// function tracing runtime depends on this function
|
|
|
|
// asan runtime depends on this function
|
2022-03-16 20:33:13 +00:00
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
kvprintf(fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
}
|
2023-10-25 21:32:20 +00:00
|
|
|
|
|
|
|
__weak_reference(kprintf, uprintf);
|
|
|
|
__weak_reference(kvprintf, uvprintf);
|