Make improvements

- This change fixes a bug that allowed unbuffered printf() output (to
  streams like stderr) to be truncated. This regression was introduced
  some time between now and the last release.

- POSIX specifies all functions as thread safe by default. This change
  works towards cleaning up our use of the @threadsafe / @threadunsafe
  documentation annotations to reflect that. The goal is (1) to use
  @threadunsafe to document functions which POSIX say needn't be thread
  safe, and (2) use @threadsafe to document functions that we chose to
  implement as thread safe even though POSIX didn't mandate it.

- Tidy up the clock_gettime() implementation. We're now trying out a
  cleaner approach to system call support that aims to maintain the
  Linux errno convention as long as possible. This also fixes bugs that
  existed previously, where the vDSO errno wasn't being translated
  properly. The gettimeofday() system call is now a wrapper for
  clock_gettime(), which reduces bloat in apps that use both.

- The recently-introduced improvements to the execute bit on Windows has
  had bugs fixed. access(X_OK) on a directory on Windows now succeeds.
  fstat() will now perform the MZ/#! ReadFile() operation correctly.

- Windows.h is no longer included in libc/isystem/, because it confused
  PCRE's build system into thinking Cosmopolitan is a WIN32 platform.
  Cosmo's Windows.h polyfill was never even really that good, since it
  only defines a subset of the subset of WIN32 APIs that Cosmo defines.

- The setlongerjmp() / longerjmp() APIs are removed. While they're nice
  APIs that are superior to the standardized setjmp / longjmp functions,
  they weren't superior enough to not be dead code in the monorepo. If
  you use these APIs, please file an issue and they'll be restored.

- The .com appending magic has now been removed from APE Loader.
This commit is contained in:
Justine Tunney 2023-10-02 19:25:19 -07:00
parent b99512ac58
commit ff77f2a6af
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
226 changed files with 708 additions and 2657 deletions

View file

@ -1,2 +0,0 @@
#include "libc/sysv/macros.internal.h"
.scall sys_clock_gettime,0x1ab0570e8ffff0e4,113,4095,globl,hidden

View file

@ -1,2 +0,0 @@
#include "libc/sysv/macros.internal.h"
.scall sys_gettimeofday,0x1a20430742074060,169,116,globl,hidden

View file

@ -584,7 +584,7 @@ syscon clock CLOCK_MONOTONIC_PRECISE 1 1 1 6 11 3 3 1 #
syscon clock CLOCK_MONOTONIC_FAST 1 1 1 6 12 3 3 1 #
syscon clock CLOCK_MONOTONIC_COARSE 6 6 1 6 12 3 3 1 # Linux 2.6.32+; bsd consensus; not available on RHEL5
syscon clock CLOCK_MONOTONIC_RAW 4 4 127 4 127 127 127 127 # actually monotonic; not subject to NTP adjustments; Linux 2.6.28+; XNU/NT/FreeBSD/OpenBSD faked; not available on RHEL5
syscon clock CLOCK_PROCESS_CPUTIME_ID 2 2 127 12 15 2 0x40000000 127 #
syscon clock CLOCK_PROCESS_CPUTIME_ID 2 2 127 12 15 2 0x40000000 127 # NetBSD lets you bitwise a PID into clockid_t
syscon clock CLOCK_THREAD_CPUTIME_ID 3 3 127 16 14 4 0x20000000 127 #
syscon clock CLOCK_PROF 127 127 127 127 2 127 2 127 #
syscon clock CLOCK_BOOTTIME 7 7 127 127 127 6 127 127 #

View file

@ -1,2 +1,5 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon errno,ENOBUFS,105,105,55,55,55,55,55,10055
#ifdef __x86_64__
.yoink kDos2Errno.ENOBUFS
#endif

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,70 +16,35 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
.privileged
// Performs raw System Five system call.
// Invokes system call w/ arity of two.
//
// This function provides a direct path into system call support
// that's friendly to C code, since it doesn't need an intermediate
// thunk. It only supports arities up to six, since there's no way
// to do more safely; this isn't a problem with Linux, although
// certain BSD calls may not be available.
// This function has three parameters. The first two are for
// args passed along to the system call. The third's for the
// the magic number, indicating which system call is called.
//
// @param %rdi is system call ordinal, which isn't translated,
// and must be correct for the underlying host system
// @param %rsi,%rdx,%rcx,%r8,%r9 may supply parameters 1 through 5
// @param sixth is optionally pushed on the stack before call
// @return %rax has result, or -1 w/ errno on failure
.ftrace1
syscall:
.ftrace2
push %rbp
mov %rsp,%rbp
// slide arguments into their right places
mov %rdi,%rax // nr
mov %rsi,%rdi // arg 1
mov %rdx,%rsi // arg 2
mov %rcx,%rdx // arg 3
mov %r8,%rcx // arg 4
mov %r9,%r8 // arg 5
mov 16(%rbp),%r9 // arg 6
push 32(%rbp) // arg 8
push 24(%rbp) // arg 7
// convert from consts.sh to syscalls.sh encoding
push %rcx
mov __hostos(%rip),%cl
test $_HOSTLINUX,%cl
jnz 2f
1: test $_HOSTFREEBSD,%cl
jz 1f
shl $4*7,%rax
jmp 2f
1: test $_HOSTOPENBSD,%cl
jz 1f
shl $4*10,%rax
jmp 2f
1: test $_HOSTNETBSD,%cl
jz 1f
shl $4*13,%rax
jmp 2f
1: test $_HOSTXNU,%cl
jz 2f
mov %eax,%ecx
and $0x0f000000,%ecx
and $0x00000fff,%eax
shl $4*3,%eax
or %ecx,%eax
2: pop %rcx
// trigger the system call
call *__systemfive(%rip)
// clean up stack and return
leave
// The returned value follows the Linux kernel convention ie
// errors are returned as `-errno`, rather than -1 w/ errno.
__syscall2:
#ifdef __aarch64__
mov x8,x2 // syscall number (linux)
mov x16,x2 // syscall number (xnu)
mov x9,0 // clear carry flag
adds x9,x9,0 // clear carry flag
svc 0
bcs 1f
ret
.endfn syscall,globl
1: neg x0,x0
ret
#elif defined(__x86_64__)
mov %edx,%eax // arg3 -> syscall number
clc // linux saves carry flag
syscall // bsds set carry on errs
jnc 1f
neg %rax // normalizes to system v
1: ret
#else
#error "unsupported architecture"
#endif
.endfn __syscall2,globl

50
libc/sysv/syscall3.S Normal file
View file

@ -0,0 +1,50 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
// Invokes system call w/ arity of three.
//
// This function takes four params. The first three are for
// args passed along to the system call. The 4th is for the
// the magic number, indicating which system call is called
//
// The return value follows the Linux Kernel (System V) ABI
// where -errno is returned, rather than doing -1 w/ errno.
__syscall3:
#ifdef __aarch64__
mov x8,x3 // syscall number (linux)
mov x16,x3 // syscall number (xnu)
mov x9,0 // clear carry flag
adds x9,x9,0 // clear carry flag
svc 0
bcs 1f
ret
1: neg x0,x0
ret
#elif defined(__x86_64__)
mov %ecx,%eax // arg4 -> syscall number
clc // linux saves carry flag
syscall // bsds set carry on errs
jnc 1f
neg %rax // normalizes to system v
1: ret
#else
#error "unsupported architecture"
#endif
.endfn __syscall3,globl

View file

@ -127,7 +127,6 @@ scall sys_unlink 0x00a00a00a200a057 0x0b5 globl hidden
scall sys_fchmod 0x07c07c07c207c05b 0x034 globl hidden
scall sys_fchown 0x07b07b07b207b05d 0x037 globl hidden # @asyncsignalsafe
scall sys_umask 0x03c03c03c203c05f 0x0a6 globl hidden
scall sys_gettimeofday 0x1a20430742074060 0x0a9 globl hidden # xnu esi/edx=0
scall sys_getrlimit 0x0c20c20c220c2061 0x0a3 globl hidden
scall __sys_getrusage 0x1bd0130752075062 0x0a5 globl hidden
scall sys_sysinfo 0xfffffffffffff063 0x0b3 globl hidden
@ -251,7 +250,6 @@ scall sys_ktimer_getoverrun 0xffffff0effffffff 0xfff globl # no wrapper
scall sys_ktimer_gettime 0xffffff0eefffffff 0xfff globl # no wrapper
scall sys_ktimer_settime 0xffffff0edfffffff 0xfff globl # no wrapper
scall sys_clock_settime 0x1ac0580e9ffff0e3 0x070 globl hidden # no wrapper
scall sys_clock_gettime 0x1ab0570e8ffff0e4 0x071 globl hidden # Linux 2.6+ (c. 2003); XNU uses magic address
scall sys_clock_getres 0x1ad0590eaffff0e5 0x072 globl hidden
scall sys_mbind 0xfffffffffffff0ed 0x0eb globl # no wrapper; numa numa yeah
scall set_mempolicy 0xfffffffffffff0ee 0x0ed globl

View file

@ -38,7 +38,8 @@ LIBC_SYSV_A_FILES := \
libc/sysv/syslib.S \
libc/sysv/syscount.S \
libc/sysv/restorert.S \
libc/sysv/syscall.S \
libc/sysv/syscall2.S \
libc/sysv/syscall3.S \
libc/sysv/systemfive.S \
libc/sysv/sysret.c \
libc/sysv/sysv.c \
@ -165,6 +166,10 @@ o/$(MODE)/libc/sysv/syslib.o: libc/sysv/syslib.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/syscount.o: libc/sysv/syscount.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/syscall2.o: libc/sysv/syscall2.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/syscall3.o: libc/sysv/syscall3.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/restorert.o: libc/sysv/restorert.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
o/$(MODE)/libc/sysv/calls/%.o: libc/sysv/calls/%.S