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.
This commit is contained in:
Justine Tunney 2023-09-18 20:44:45 -07:00
parent c4eb838516
commit ec480f5aa0
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
638 changed files with 7925 additions and 8282 deletions

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_accept,0x81e81ea1d281e82b,2250,30,globl,hidden
.scall __sys_accept,0x81e81ea1d281e82b,2250,2078,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_connect,0x862862862286282a,2251,98,globl,hidden
.scall __sys_connect,0x862862862286282a,2251,2146,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_fcntl_cp,0x85c85c85c285c848,2073,92,globl,hidden
.scall __sys_fcntl_cp,0x85c85c85c285c848,2073,2140,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_openat,0x9d49419f329cf901,2104,463,globl,hidden
.scall __sys_openat,0x9d49419f329cf901,2104,2511,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_poll,0x8d18fc8d128e6807,4095,230,globl,hidden
.scall __sys_poll,0x8d18fc8d128e6807,4095,2278,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_wait4,0x9c180b807280783d,2308,7,globl,hidden
.scall __sys_wait4,0x9c180b807280783d,2308,2055,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_fdatasync,0x8f185fa2628bb84b,2131,187,globl,hidden
.scall sys_fdatasync,0x8f185fa2628bb84b,2131,2235,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_flock,0x8838838832883849,2080,131,globl,hidden
.scall sys_flock,0x8838838832883849,2080,2179,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_fstatfs,0x89e840a2c295a88a,2092,346,globl,hidden
.scall sys_fstatfs,0x89e840a2c295a88a,2092,2394,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_fsync,0x85f85f85f285f84a,2130,95,globl,hidden
.scall sys_fsync,0x85f85f85f285f84a,2130,2143,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_ftruncate,0x8c98a89e028c984d,2094,201,globl,hidden
.scall sys_ftruncate,0x8c98a89e028c984d,2094,2249,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_ioctl_cp,0x8368368362836810,2077,54,globl,hidden
.scall sys_ioctl_cp,0x8368368362836810,2077,2102,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_msgrcv,0x8e38e38e32905846,2236,261,globl
.scall sys_msgrcv,0x8e38e38e32905846,2236,2309,globl

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_msgsnd,0x8e28e28e22904845,2237,260,globl
.scall sys_msgsnd,0x8e28e28e22904845,2237,2308,globl

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_msync,0x915900841284181a,2275,65,globl,hidden
.scall sys_msync,0x915900841284181a,2275,2113,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_pread,0x8ad8a99db2899811,2115,153,globl,hidden
.scall sys_pread,0x8ad8a99db2899811,2115,2201,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_preadv,0x9218ab9212a1c927,2117,540,globl,hidden
.scall sys_preadv,0x9218ab9212a1c927,2117,2588,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_pselect,0x9b486ea0a298a90e,2120,394,globl,hidden
.scall sys_pselect,0x9b486ea0a298a90e,2120,2442,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_pwrite,0x8ae8aa9dc289a812,2116,154,globl,hidden
.scall sys_pwrite,0x8ae8aa9dc289a812,2116,2202,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_pwritev,0x9228ac9222a1d928,2118,541,globl,hidden
.scall sys_pwritev,0x9228ac9222a1d928,2118,2589,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_read,0x8038038032803800,2111,3,globl,hidden
.scall sys_read,0x8038038032803800,2111,2051,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_readv,0x8788788782878813,2113,120,globl,hidden
.scall sys_readv,0x8788788782878813,2113,2168,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_recvfrom,0x81d81d81d281d82d,2255,29,globl,hidden
.scall sys_recvfrom,0x81d81d81d281d82d,2255,2077,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_recvmsg,0x81b81b81b281b82f,2260,27,globl,hidden
.scall sys_recvmsg,0x81b81b81b281b82f,2260,2075,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_select,0x9a184785d285d817,4095,93,globl,hidden
.scall sys_select,0x9a184785d285d817,4095,2141,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_sem_wait,0x8fcfff992290ffff,4095,271,globl
.scall sys_sem_wait,0x8fcfff992290ffff,4095,2319,globl

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_sendmsg,0x81c81c81c281c82e,2259,28,globl,hidden
.scall sys_sendmsg,0x81c81c81c281c82e,2259,2076,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_sendto,0x885885885288582c,2254,133,globl,hidden
.scall sys_sendto,0x885885885288582c,2254,2181,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_sigsuspend,0x92686f955286f882,2181,111,globl,hidden
.scall sys_sigsuspend,0x92686f955286f882,2181,2159,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_statfs,0x89d83fa2b2959889,2091,345,globl,hidden
.scall sys_statfs,0x89d83fa2b2959889,2091,2393,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_truncate,0x8c88a79df28c884c,2093,200,globl,hidden
.scall sys_truncate,0x8c88a79df28c884c,2093,2248,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_waitid,0xfffffffff28ad8f7,95,173,globl
.scall sys_waitid,0xfffffffff28ad8f7,95,2221,globl

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_write,0x8048048042804801,2112,4,globl,hidden
.scall sys_write,0x8048048042804801,2112,2052,globl,hidden

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_writev,0x8798798792879814,2114,121,globl,hidden
.scall sys_writev,0x8798798792879814,2114,2169,globl,hidden

View file

@ -18,6 +18,7 @@
╚────────────────────────────────────────────────────────────────'>/dev/null #*/
dir=libc/sysv/consts
. libc/sysv/gen.sh
#include "libc/sysv/consts/ss.h"
# The Fifth Bell System, Community Edition
# » catalogue of carnage
@ -1057,6 +1058,8 @@ syscon limits _ARG_MAX 128*1024 128*1024 1024*1024 1024*1024 512*1024 51
syscon limits _NAME_MAX 255 255 255 255 255 255 511 255 # probably higher on windows?
syscon limits _PATH_MAX 4096 4096 1024 1024 1024 1024 1024 260 #
syscon limits _NSIG 64 64 32 32 128 32 64 64 # _SIG_MAXSIG on FreeBSD
syscon limits _MINSIGSTKSZ 2048 2048 32768 32768 4096 12288 8192 2048 #
syscon limits _SIGSTKSZ 8192 2048 131072 131072 36864 28672 40960 8192 #
# unmount() flags
# a.k.a. umount2() on linux
@ -1549,8 +1552,8 @@ syscon vid KDFONTOP 0x4b72 0x4b72 0 0 0 0 0 0
syscon nr __NR_exit 0x003c 0x005d 0x2000169 0x0169 0x01af 0x012e 0x136 0xfff # __bsdthread_terminate() on XNU, thr_exit() on FreeBSD, __threxit() on OpenBSD, __lwp_exit() on NetBSD
syscon nr __NR_exit_group 0x00e7 0x005e 0x2000001 0x0001 0x0001 0x0001 0x001 0xfff
syscon nr __NR_read 0x0000 0x003f 0x2000003 0x0003 0x0003 0x0003 0x003 0xfff
syscon nr __NR_write 0x0001 0x0040 0x2000004 0x0004 0x0004 0x0004 0x004 0xfff
syscon nr __NR_open 0x0002 0x00b4 0x2000005 0x0005 0x0005 0x0005 0x005 0xfff
syscon nr __NR_write 0x0001 0x0040 0x200018d 0x0004 0x0004 0x0004 0x004 0xfff # write_nocancel() on XNU
syscon nr __NR_open 0x0002 0x00b4 0x200018e 0x0005 0x0005 0x0005 0x005 0xfff # open_nocancel() on XNU
syscon nr __NR_close 0x0003 0x0039 0x2000006 0x0006 0x0006 0x0006 0x006 0xfff
syscon nr __NR_stat 0x0004 0x004f 0x2000152 0x0152 0xfff 0x0026 0x1b7 0xfff
syscon nr __NR_fstat 0x0005 0x0050 0x2000153 0x0153 0x0227 0x0035 0x1b8 0xfff
@ -1574,7 +1577,7 @@ syscon nr __NR_pipe 0x0016 0x0fff 0x200002a 0x002a 0x021e 0x0107 0
syscon nr __NR_select 0x0017 0x0fff 0x200005d 0x005d 0x005d 0x0047 0x1a1 0xfff
syscon nr __NR_pselect 0xfff 0x0fff 0x200018a 0x018a 0x020a 0x006e 0x1b4 0xfff
syscon nr __NR_pselect6 0x010e 0x0048 0xfff 0xfff 0xfff 0xfff 0xfff 0xfff
syscon nr __NR_sched_yield 0x0018 0x007c 0x200005d 0x005d 0x014b 0x012a 0x15e 0xfff # select() on XNU (previously swtch() but removed in 12.4)
syscon nr __NR_sched_yield 0x0018 0x007c 0x2000197 0x005d 0x014b 0x012a 0x15e 0xfff # select_nocancel() on XNU (previously swtch() but removed in 12.4)
syscon nr __NR_mremap 0x0019 0x00d8 0xfff 0xfff 0xfff 0xfff 0x19b 0xfff
syscon nr __NR_mincore 0x001b 0x00e8 0x200004e 0x004e 0x004e 0x004e 0x04e 0xfff
syscon nr __NR_madvise 0x001c 0x00e9 0x200004b 0x004b 0x004b 0x004b 0x04b 0xfff
@ -1792,7 +1795,7 @@ syscon nr __NR_ioprio_get 0x00fc 0x001f 0xfff 0xfff 0xfff 0xfff 0
syscon nr __NR_inotify_init 0x00fd 0x0fff 0xfff 0xfff 0xfff 0xfff 0xfff 0xfff
syscon nr __NR_inotify_add_watch 0x00fe 0x0fff 0xfff 0xfff 0xfff 0xfff 0xfff 0xfff
syscon nr __NR_inotify_rm_watch 0x00ff 0x0fff 0xfff 0xfff 0xfff 0xfff 0xfff 0xfff
syscon nr __NR_openat 0x0101 0x0038 0x20001cf 0x01cf 0x01f3 0x0141 0x1d4 0xfff
syscon nr __NR_openat 0x0101 0x0038 0x20001d0 0x01cf 0x01f3 0x0141 0x1d4 0xfff # openat_nocancel() on XNU
syscon nr __NR_mkdirat 0x0102 0x0022 0x20001db 0x01db 0x01f0 0x013e 0x1cd 0xfff
syscon nr __NR_fchownat 0x0104 0x0036 0x20001d4 0x01d4 0x01eb 0x013b 0x1d0 0xfff
syscon nr __NR_utime 0x0084 0x0062 0xfff 0xfff 0xfff 0xfff 0xfff 0xfff

View file

@ -1,2 +1,5 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon errno,ENOEXEC,8,8,8,8,8,8,8,193
#ifdef __x86_64__
.yoink kDos2Errno.ENOEXEC
#endif

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon limits,_MINSIGSTKSZ,2048,2048,32768,32768,4096,12288,8192,2048

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon limits,_SIGSTKSZ,8192,2048,131072,131072,36864,28672,40960,8192

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon nr,__NR_open,0x0002,0x00b4,0x2000005,0x0005,0x0005,0x0005,0x005,0xfff
.syscon nr,__NR_open,0x0002,0x00b4,0x200018e,0x0005,0x0005,0x0005,0x005,0xfff

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon nr,__NR_openat,0x0101,0x0038,0x20001cf,0x01cf,0x01f3,0x0141,0x1d4,0xfff
.syscon nr,__NR_openat,0x0101,0x0038,0x20001d0,0x01cf,0x01f3,0x0141,0x1d4,0xfff

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon nr,__NR_sched_yield,0x0018,0x007c,0x200005d,0x005d,0x014b,0x012a,0x15e,0xfff
.syscon nr,__NR_sched_yield,0x0018,0x007c,0x2000197,0x005d,0x014b,0x012a,0x15e,0xfff

View file

@ -1,2 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon nr,__NR_write,0x0001,0x0040,0x2000004,0x0004,0x0004,0x0004,0x004,0xfff
.syscon nr,__NR_write,0x0001,0x0040,0x200018d,0x0004,0x0004,0x0004,0x004,0xfff

View file

@ -4,6 +4,8 @@
COSMOPOLITAN_C_START_
extern const int SS_DISABLE;
extern const int _SIGSTKSZ;
extern const int _MINSIGSTKSZ;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
@ -13,5 +15,4 @@ COSMOPOLITAN_C_END_
#define SS_ONSTACK 1
#define SS_DISABLE SS_DISABLE
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_SS_H_ */

View file

@ -33,7 +33,7 @@ scall() {
arm_linux=$(($3 + 0))
arm_xnu=$((($amd & 0xfff000) >> 12))
if [ $arm_xnu != 4095 ]; then
arm_xnu=$(($arm_xnu & 0x7ff))
arm_xnu=$(($arm_xnu & 0xfff))
fi
shift 3
set -- "$name" "$amd" "$arm_linux" "$arm_xnu" "$*"

View file

@ -149,8 +149,8 @@ systemfive_cancellable_end: // i/o calls park here for long time
jmp 4f // now we are in fact cancelled
systemfive_cancel: // SIGTHR will jump here too
pop %rbp
4: jmp _pthread_cancel_sys // tail call
.weak _pthread_cancel_sys // must be linked if we're cancelled
4: jmp _pthread_cancel_ack // tail call
.weak _pthread_cancel_ack // must be linked if we're cancelled
#if IsModeDbg()
not_a_cancellation_point: // need BEGIN/END_CANCELLATION_POINT
nop

View file

@ -39,7 +39,7 @@ register long cosmo_tls_register asm("x28");
void report_cancellation_point(void);
dontinline long systemfive_cancel(void) {
return _pthread_cancel_sys();
return _weaken(_pthread_cancel_ack)();
}
// special region of executable memory where cancellation is safe
@ -49,16 +49,16 @@ dontinline long systemfive_cancellable(void) {
// plus (2) cancellations aren't disabled
struct PosixThread *pth = 0;
struct CosmoTib *tib = __get_tls();
if (cosmo_tls_register && //
_weaken(_pthread_cancel_sys) && //
!(tib->tib_flags & PT_NOCANCEL) && //
if (cosmo_tls_register && //
_weaken(_pthread_cancel_ack) && //
(pth = (struct PosixThread *)tib->tib_pthread)) {
// check if cancellation is already pending
if (atomic_load_explicit(&pth->cancelled, memory_order_acquire)) {
if (!(pth->pt_flags & PT_NOCANCEL) &&
atomic_load_explicit(&pth->cancelled, memory_order_acquire)) {
return systemfive_cancel();
}
#if IsModeDbg()
if (!(tib->tib_flags & PT_INCANCEL)) {
if (!(pth->flags & PT_INCANCEL)) {
if (_weaken(report_cancellation_point)) {
_weaken(report_cancellation_point)();
}
@ -87,7 +87,7 @@ dontinline long systemfive_cancellable(void) {
}
// check if i/o call was interrupted by sigthr
if (pth && x0 == -EINTR &&
if (pth && x0 == -EINTR && !(pth->pt_flags & PT_NOCANCEL) &&
atomic_load_explicit(&pth->cancelled, memory_order_acquire)) {
return systemfive_cancel();
}

View file

@ -80,7 +80,9 @@ o/$(MODE)/libc/sysv/sysret.o \
o/$(MODE)/libc/sysv/errfun2.o \
o/$(MODE)/libc/sysv/sysret.o: private \
CFLAGS += \
$(NO_MAGIC)
-ffreestanding \
-fno-stack-protector \
-fno-sanitize=all
ifeq ($(ARCH),aarch64)
o/$(MODE)/libc/sysv/sysv.o: private \