Skip to content

Commit

Permalink
Make improvements
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jart committed Oct 3, 2023
1 parent b99512a commit ff77f2a
Show file tree
Hide file tree
Showing 226 changed files with 691 additions and 2,640 deletions.
57 changes: 16 additions & 41 deletions ape/ape-m1.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ struct Syslib {
long (*write)(int, const void *, size_t);
long (*read)(int, void *, size_t);
long (*sigaction)(int, const struct sigaction *, struct sigaction *);
long (*pselect)(int, fd_set *, fd_set *, fd_set *, const struct timespec *, const sigset_t *);
long (*pselect)(int, fd_set *, fd_set *, fd_set *, const struct timespec *,
const sigset_t *);
long (*mprotect)(void *, size_t, int);
};

Expand Down Expand Up @@ -187,10 +188,6 @@ struct ApeLoader {
char rando[16];
};

static int ToLower(int c) {
return 'A' <= c && c <= 'Z' ? c + ('a' - 'A') : c;
}

static unsigned long StrLen(const char *s) {
unsigned long n = 0;
while (*s++) ++n;
Expand Down Expand Up @@ -349,38 +346,14 @@ __attribute__((__noreturn__)) static void Pexit(const char *c, int failed,
_exit(127);
}

static char EndsWithIgnoreCase(const char *p, unsigned long n, const char *s) {
unsigned long i, m;
if (n >= (m = StrLen(s))) {
for (i = n - m; i < n; ++i) {
if (ToLower(p[i]) != *s++) {
return 0;
}
}
return 1;
} else {
return 0;
}
}

static char IsComPath(struct PathSearcher *ps) {
return EndsWithIgnoreCase(ps->name, ps->namelen, ".com") ||
EndsWithIgnoreCase(ps->name, ps->namelen, ".exe") ||
EndsWithIgnoreCase(ps->name, ps->namelen, ".com.dbg");
}

static char AccessCommand(struct PathSearcher *ps, const char *suffix,
unsigned long pathlen) {
unsigned long suffixlen;
suffixlen = StrLen(suffix);
if (pathlen + 1 + ps->namelen + suffixlen + 1 > sizeof(ps->path)) return 0;
static char AccessCommand(struct PathSearcher *ps, unsigned long pathlen) {
if (pathlen + 1 + ps->namelen + 1 > sizeof(ps->path)) return 0;
if (pathlen && ps->path[pathlen - 1] != '/') ps->path[pathlen++] = '/';
MemMove(ps->path + pathlen, ps->name, ps->namelen);
MemMove(ps->path + pathlen + ps->namelen, suffix, suffixlen + 1);
return !access(ps->path, X_OK);
}

static char SearchPath(struct PathSearcher *ps, const char *suffix) {
static char SearchPath(struct PathSearcher *ps) {
const char *p;
unsigned long i;
for (p = ps->syspath;;) {
Expand All @@ -389,7 +362,7 @@ static char SearchPath(struct PathSearcher *ps, const char *suffix) {
ps->path[i] = p[i];
}
}
if (AccessCommand(ps, suffix, i)) {
if (AccessCommand(ps, i)) {
return 1;
} else if (p[i] == ':') {
p += i + 1;
Expand All @@ -399,13 +372,13 @@ static char SearchPath(struct PathSearcher *ps, const char *suffix) {
}
}

static char FindCommand(struct PathSearcher *ps, const char *suffix) {
static char FindCommand(struct PathSearcher *ps) {
ps->path[0] = 0;

/* paths are always 100% taken literally when a slash exists
$ ape foo/bar.com arg1 arg2 */
if (MemChr(ps->name, '/', ps->namelen)) {
return AccessCommand(ps, suffix, 0);
return AccessCommand(ps, 0);
}

/* we don't run files in the current directory
Expand All @@ -416,20 +389,20 @@ static char FindCommand(struct PathSearcher *ps, const char *suffix) {
however we will execute this
$ ape - foo.com foo.com arg1 arg2
because cosmo's execve needs it */
if (ps->literally && AccessCommand(ps, suffix, 0)) {
if (ps->literally && AccessCommand(ps, 0)) {
return 1;
}

/* otherwise search for name on $PATH */
return SearchPath(ps, suffix);
return SearchPath(ps);
}

static char *Commandv(struct PathSearcher *ps, const char *name,
const char *syspath) {
ps->syspath = syspath ? syspath : "/bin:/usr/local/bin:/usr/bin";
if (!(ps->namelen = StrLen((ps->name = name)))) return 0;
if (ps->namelen + 1 > sizeof(ps->path)) return 0;
if (FindCommand(ps, "") || (!IsComPath(ps) && FindCommand(ps, ".com"))) {
if (FindCommand(ps)) {
return ps->path;
} else {
return 0;
Expand Down Expand Up @@ -843,12 +816,14 @@ static long sys_mmap(void *addr, size_t size, int prot, int flags, int fd,
return sysret((long)mmap(addr, size, prot, flags, fd, off));
}

static long sys_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
static long sys_sigaction(int sig, const struct sigaction *act,
struct sigaction *oact) {
return sysret(sigaction(sig, act, oact));
}

static long sys_pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
const struct timespec *timeout, const sigset_t *sigmask) {
static long sys_pselect(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *errorfds, const struct timespec *timeout,
const sigset_t *sigmask) {
return sysret(pselect(nfds, readfds, writefds, errorfds, timeout, sigmask));
}

Expand Down
46 changes: 9 additions & 37 deletions ape/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ EXTERN_C void Launch(void *, long, void *, int) __attribute__((__noreturn__));
extern char __executable_start[];
extern char _end[];

static int ToLower(int c) {
return 'A' <= c && c <= 'Z' ? c + ('a' - 'A') : c;
}

static unsigned long StrLen(const char *s) {
unsigned long n = 0;
while (*s++) ++n;
Expand Down Expand Up @@ -538,38 +534,14 @@ __attribute__((__noreturn__)) static void Pexit(int os, const char *c, int rc,
Exit(127, os);
}

static char EndsWithIgnoreCase(const char *p, unsigned long n, const char *s) {
unsigned long i, m;
if (n >= (m = StrLen(s))) {
for (i = n - m; i < n; ++i) {
if (ToLower(p[i]) != *s++) {
return 0;
}
}
return 1;
} else {
return 0;
}
}

static char IsComPath(struct PathSearcher *ps) {
return EndsWithIgnoreCase(ps->name, ps->namelen, ".com") ||
EndsWithIgnoreCase(ps->name, ps->namelen, ".exe") ||
EndsWithIgnoreCase(ps->name, ps->namelen, ".com.dbg");
}

static char AccessCommand(struct PathSearcher *ps, const char *suffix,
unsigned long pathlen) {
unsigned long suffixlen;
suffixlen = StrLen(suffix);
if (pathlen + 1 + ps->namelen + suffixlen + 1 > sizeof(ps->path)) return 0;
static char AccessCommand(struct PathSearcher *ps, unsigned long pathlen) {
if (pathlen + 1 + ps->namelen + 1 > sizeof(ps->path)) return 0;
if (pathlen && ps->path[pathlen - 1] != '/') ps->path[pathlen++] = '/';
MemMove(ps->path + pathlen, ps->name, ps->namelen);
MemMove(ps->path + pathlen + ps->namelen, suffix, suffixlen + 1);
return !Access(ps->path, X_OK, ps->os);
}

static char SearchPath(struct PathSearcher *ps, const char *suffix) {
static char SearchPath(struct PathSearcher *ps) {
const char *p;
unsigned long i;
for (p = ps->syspath;;) {
Expand All @@ -578,7 +550,7 @@ static char SearchPath(struct PathSearcher *ps, const char *suffix) {
ps->path[i] = p[i];
}
}
if (AccessCommand(ps, suffix, i)) {
if (AccessCommand(ps, i)) {
return 1;
} else if (p[i] == ':') {
p += i + 1;
Expand All @@ -588,13 +560,13 @@ static char SearchPath(struct PathSearcher *ps, const char *suffix) {
}
}

static char FindCommand(struct PathSearcher *ps, const char *suffix) {
static char FindCommand(struct PathSearcher *ps) {
ps->path[0] = 0;

/* paths are always 100% taken literally when a slash exists
$ ape foo/bar.com arg1 arg2 */
if (MemChr(ps->name, '/', ps->namelen)) {
return AccessCommand(ps, suffix, 0);
return AccessCommand(ps, 0);
}

/* we don't run files in the current directory
Expand All @@ -605,12 +577,12 @@ static char FindCommand(struct PathSearcher *ps, const char *suffix) {
however we will execute this
$ ape - foo.com foo.com arg1 arg2
because cosmo's execve needs it */
if (ps->literally && AccessCommand(ps, suffix, 0)) {
if (ps->literally && AccessCommand(ps, 0)) {
return 1;
}

/* otherwise search for name on $PATH */
return SearchPath(ps, suffix);
return SearchPath(ps);
}

static char *Commandv(struct PathSearcher *ps, int os, const char *name,
Expand All @@ -619,7 +591,7 @@ static char *Commandv(struct PathSearcher *ps, int os, const char *name,
ps->syspath = syspath ? syspath : "/bin:/usr/local/bin:/usr/bin";
if (!(ps->namelen = StrLen((ps->name = name)))) return 0;
if (ps->namelen + 1 > sizeof(ps->path)) return 0;
if (FindCommand(ps, "") || (!IsComPath(ps) && FindCommand(ps, ".com"))) {
if (FindCommand(ps)) {
return ps->path;
} else {
return 0;
Expand Down
1 change: 1 addition & 0 deletions dsp/mpeg/mpeg1.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "dsp/mpeg/idct.h"
#include "dsp/mpeg/mpeg.h"
#include "dsp/mpeg/video.h"
#include "libc/calls/struct/timespec.h"
#include "libc/fmt/conv.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
Expand Down
2 changes: 2 additions & 0 deletions libc/calls/calls.mk
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ o//libc/calls/statfs2cosmo.o: private \
# we always want -O2 because:
# division is expensive if not optimized
o/$(MODE)/libc/calls/clock.o \
o/$(MODE)/libc/calls/gettimeofday.o \
o/$(MODE)/libc/calls/clock_gettime-mono.o \
o/$(MODE)/libc/calls/timespec_tomillis.o \
o/$(MODE)/libc/calls/timespec_tomicros.o \
o/$(MODE)/libc/calls/timespec_totimeval.o \
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int64_t clock(void) {
struct rusage ru;
struct timespec ts;
e = errno;
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == -1) {
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) {
errno = e;
if (getrusage(RUSAGE_SELF, &ru) != -1) {
ts = timeval_totimespec(timeval_add(ru.ru_utime, ru.ru_stime));
Expand Down
1 change: 0 additions & 1 deletion libc/calls/clock_getres.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static int sys_clock_getres_xnu(int clock, struct timespec *ts) {
* @error EPERM if pledge() is in play without stdio promise
* @error EINVAL if `clock` isn't supported on this system
* @error EFAULT if `ts` points to bad memory
* @threadsafe
*/
int clock_getres(int clock, struct timespec *ts) {
int rc;
Expand Down
33 changes: 21 additions & 12 deletions libc/calls/clock_gettime-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/atomic.h"
#include "libc/calls/struct/timespec.h"
#include "libc/cosmo.h"
#include "libc/errno.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/errfuns.h"

/**
* @fileoverview Fast Monotonic Clock Polyfill for XNU/NT.
*/

static struct {
_Atomic(uint32_t) once;
atomic_uint once;
struct timespec base_wall;
uint64_t base_tick;
} g_mono;
Expand All @@ -37,13 +41,18 @@ static void sys_clock_gettime_mono_init(void) {
int sys_clock_gettime_mono(struct timespec *time) {
uint64_t nanos;
uint64_t cycles;
if (X86_HAVE(INVTSC)) {
cosmo_once(&g_mono.once, sys_clock_gettime_mono_init);
cycles = rdtsc() - g_mono.base_tick;
nanos = cycles / 3;
*time = timespec_add(g_mono.base_wall, timespec_fromnanos(nanos));
return 0;
} else {
return einval();
}
#ifdef __x86_64__
// intel architecture guarantees that a mapping exists between rdtsc &
// nanoseconds only if the cpu advertises invariant timestamps support
if (!X86_HAVE(INVTSC)) return -EINVAL;
#endif
cosmo_once(&g_mono.once, sys_clock_gettime_mono_init);
cycles = rdtsc() - g_mono.base_tick;
// this is a crude approximation, that's worked reasonably well so far
// only the kernel knows the actual mapping between rdtsc and nanosecs
// which we could attempt to measure ourselves using clock_gettime but
// we'd need to impose 100 ms of startup latency for a guess this good
nanos = cycles / 3;
*time = timespec_add(g_mono.base_wall, timespec_fromnanos(nanos));
return 0;
}
8 changes: 4 additions & 4 deletions libc/calls/clock_gettime-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/clock_gettime.internal.h"
#include "libc/fmt/conv.h"
#include "libc/calls/struct/timespec.internal.h"
#include "libc/errno.h"
#include "libc/fmt/wintime.internal.h"
#include "libc/nt/struct/filetime.h"
#include "libc/nt/synchronization.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/errfuns.h"

textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
struct NtFileTime ft;
Expand All @@ -32,6 +32,6 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
} else if (clock == CLOCK_MONOTONIC) {
return sys_clock_gettime_mono(ts);
} else {
return einval();
return -EINVAL;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2022 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 │
Expand All @@ -16,11 +16,10 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timespec.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/sysv/consts/nr.h"

int sys_clock_gettime_m1(int clock, struct timespec *ts) {
return _sysret(__syslib->__clock_gettime(clock, ts));
int sys_clock_gettime(int clock, struct timespec *ts) {
return __syscall2i(clock, (long)ts, __NR_clock_gettime);
}
Loading

0 comments on commit ff77f2a

Please sign in to comment.