Mint APE Loader v1.5

This change ports APE Loader to Linux AARCH64, so that Raspberry Pi
users can run programs like redbean, without the executable needing
to modify itself. Progress has also slipped into this change on the
issue of making progress better conforming to user expectations and
industry standards regarding which symbols we're allowed to declare
This commit is contained in:
Justine Tunney 2023-07-26 13:54:49 -07:00
parent 6843150e0c
commit 7e0a09feec
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
510 changed files with 1783 additions and 1483 deletions

View file

@ -48,7 +48,7 @@ static void _pthread_purge(void) {
static void _pthread_onfork(int i) {
struct AtFork *a;
struct PosixThread *pt;
_unassert(0 <= i && i <= 2);
unassert(0 <= i && i <= 2);
if (!i) pthread_spin_lock(&_atforks.lock);
for (a = _atforks.list; a; a = a->p[!i]) {
if (a->f[i]) a->f[i]();

View file

@ -16,7 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/thread/thread.h"
/**
@ -36,7 +38,7 @@
errno_t pthread_attr_init(pthread_attr_t *attr) {
*attr = (pthread_attr_t){
.__stacksize = GetStackSize(),
.__guardsize = APE_GUARDSIZE,
.__guardsize = getauxval(AT_PAGESZ),
};
return 0;
}

View file

@ -75,7 +75,7 @@ static void ListenForSigThr(void) {
sa.sa_sigaction = OnSigThr;
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
memset(&sa.sa_mask, -1, sizeof(sa.sa_mask));
_npassert(!sigaction(SIGTHR, &sa, 0));
npassert(!sigaction(SIGTHR, &sa, 0));
}
/**

View file

@ -27,12 +27,14 @@
#include "libc/intrin/atomic.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -44,9 +46,9 @@
#include "libc/thread/tls.h"
#include "libc/thread/wait0.internal.h"
STATIC_YOINK("nsync_mu_lock");
STATIC_YOINK("nsync_mu_unlock");
STATIC_YOINK("_pthread_atfork");
__static_yoink("nsync_mu_lock");
__static_yoink("nsync_mu_unlock");
__static_yoink("_pthread_atfork");
#define MAP_ANON_OPENBSD 0x1000
#define MAP_STACK_OPENBSD 0x4000
@ -57,7 +59,7 @@ void _pthread_free(struct PosixThread *pt) {
if ((pt->flags & PT_OWNSTACK) && //
pt->attr.__stackaddr && //
pt->attr.__stackaddr != MAP_FAILED) {
_npassert(!munmap(pt->attr.__stackaddr, pt->attr.__stacksize));
npassert(!munmap(pt->attr.__stackaddr, pt->attr.__stacksize));
}
if (pt->altstack) {
free(pt->altstack);
@ -70,7 +72,7 @@ static int PosixThread(void *arg, int tid) {
struct sigaltstack ss;
struct PosixThread *pt = arg;
enum PosixThreadStatus status;
_unassert(__get_tls()->tib_tid > 0);
unassert(__get_tls()->tib_tid > 0);
if (pt->altstack) {
ss.ss_flags = 0;
ss.ss_size = SIGSTKSZ;
@ -85,10 +87,10 @@ static int PosixThread(void *arg, int tid) {
// set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->exiter)) {
__get_tls()->tib_pthread = (pthread_t)pt;
_npassert(!sigprocmask(SIG_SETMASK, (sigset_t *)pt->attr.__sigmask, 0));
npassert(!sigprocmask(SIG_SETMASK, (sigset_t *)pt->attr.__sigmask, 0));
rc = pt->start(pt->arg);
// ensure pthread_cleanup_pop(), and pthread_exit() popped cleanup
_npassert(!pt->cleanup);
npassert(!pt->cleanup);
// calling pthread_exit() will either jump back here, or call exit
pthread_exit(rc);
}
@ -170,15 +172,17 @@ static errno_t pthread_create_impl(pthread_t *thread,
// cosmo is managing the stack
// 1. in mono repo optimize for tiniest stack possible
// 2. in public world optimize to *work* regardless of memory
unsigned long default_guardsize;
default_guardsize = getauxval(AT_PAGESZ);
pt->flags = PT_OWNSTACK;
pt->attr.__stacksize = MAX(pt->attr.__stacksize, GetStackSize());
pt->attr.__stacksize = _roundup2pow(pt->attr.__stacksize);
pt->attr.__guardsize = ROUNDUP(pt->attr.__guardsize, APE_GUARDSIZE);
if (pt->attr.__guardsize + APE_GUARDSIZE >= pt->attr.__stacksize) {
pt->attr.__guardsize = ROUNDUP(pt->attr.__guardsize, default_guardsize);
if (pt->attr.__guardsize + default_guardsize >= pt->attr.__stacksize) {
_pthread_free(pt);
return EINVAL;
}
if (pt->attr.__guardsize == APE_GUARDSIZE) {
if (pt->attr.__guardsize == default_guardsize) {
// user is wisely using smaller stacks with default guard size
pt->attr.__stackaddr =
mmap(0, pt->attr.__stacksize, PROT_READ | PROT_WRITE,

View file

@ -103,7 +103,7 @@ wontreturn void pthread_exit(void *rc) {
tib = __get_tls();
pt = (struct PosixThread *)tib->tib_pthread;
_unassert(~pt->flags & PT_EXITING);
unassert(~pt->flags & PT_EXITING);
pt->flags |= PT_EXITING;
pt->rc = rc;

View file

@ -27,6 +27,6 @@
pthread_t pthread_self(void) {
pthread_t t;
t = __get_tls()->tib_pthread;
_unassert(t);
unassert(t);
return t;
}

View file

@ -58,7 +58,7 @@ errno_t pthread_timedjoin_np(pthread_t thread, void **value_ptr,
// "The behavior is undefined if the value specified by the thread
// argument to pthread_join() does not refer to a joinable thread."
// ──Quoth POSIX.1-2017
_unassert(status == kPosixThreadJoinable || status == kPosixThreadTerminated);
unassert(status == kPosixThreadJoinable || status == kPosixThreadTerminated);
if (!(rc = _wait0(&pt->tib->tib_tid, abstime))) {
pthread_spin_lock(&_pthread_lock);
dll_remove(&_pthread_list, &pt->list);

View file

@ -41,10 +41,10 @@
*/
int sem_destroy(sem_t *sem) {
int waiters;
_npassert(sem->sem_magic != SEM_MAGIC_NAMED);
npassert(sem->sem_magic != SEM_MAGIC_NAMED);
if (sem->sem_magic != SEM_MAGIC_UNNAMED) return einval();
waiters = atomic_load_explicit(&sem->sem_waiters, memory_order_relaxed);
_unassert(waiters >= 0);
unassert(waiters >= 0);
if (waiters) return ebusy();
atomic_store_explicit(&sem->sem_value, INT_MIN, memory_order_relaxed);
return 0;

View file

@ -85,9 +85,9 @@ static sem_t *sem_open_impl(const char *path, int oflag, unsigned mode,
if ((fd = openat(AT_FDCWD, path, oflag, mode)) == -1) {
return SEM_FAILED;
}
_npassert(!fstat(fd, &st));
npassert(!fstat(fd, &st));
if (st.st_size < PAGESIZE && ftruncate(fd, PAGESIZE) == -1) {
_npassert(!close(fd));
npassert(!close(fd));
return SEM_FAILED;
}
sem = mmap(0, PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
@ -100,7 +100,7 @@ static sem_t *sem_open_impl(const char *path, int oflag, unsigned mode,
} else {
sem = SEM_FAILED;
}
_npassert(!close(fd));
npassert(!close(fd));
return sem;
}
@ -256,23 +256,23 @@ int sem_close(sem_t *sem) {
int prefs;
bool unmap, delete;
struct Semaphore *s, **p;
_npassert(sem->sem_magic == SEM_MAGIC_NAMED);
npassert(sem->sem_magic == SEM_MAGIC_NAMED);
sem_open_init();
sem_open_lock();
_npassert((s = sem_open_get(sem, &p)));
npassert((s = sem_open_get(sem, &p)));
prefs = atomic_fetch_add_explicit(&sem->sem_prefs, -1, memory_order_acq_rel);
_npassert(s->refs > 0);
npassert(s->refs > 0);
if ((unmap = !--s->refs)) {
_npassert(prefs > 0);
npassert(prefs > 0);
delete = sem->sem_lazydelete && prefs == 1;
*p = s->next;
} else {
_npassert(prefs > 1);
npassert(prefs > 1);
delete = false;
}
sem_open_unlock();
if (unmap) {
_npassert(!munmap(sem, PAGESIZE));
npassert(!munmap(sem, PAGESIZE));
}
if (delete) {
unlink(s->path);

View file

@ -34,10 +34,10 @@
int sem_post(sem_t *sem) {
int rc, old, wakeups;
old = atomic_fetch_add_explicit(&sem->sem_value, 1, memory_order_acq_rel);
_unassert(old > INT_MIN);
unassert(old > INT_MIN);
if (old >= 0) {
wakeups = nsync_futex_wake_(&sem->sem_value, 1, true);
_npassert(wakeups >= 0);
npassert(wakeups >= 0);
rc = 0;
} else {
wakeups = 0;

View file

@ -35,8 +35,8 @@ static void sem_delay(int n) {
static void sem_timedwait_cleanup(void *arg) {
sem_t *sem = arg;
_unassert(atomic_fetch_add_explicit(&sem->sem_waiters, -1,
memory_order_acq_rel) > 0);
unassert(atomic_fetch_add_explicit(&sem->sem_waiters, -1,
memory_order_acq_rel) > 0);
}
/**
@ -69,8 +69,8 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime) {
}
BEGIN_CANCELLATION_POINT;
_unassert(atomic_fetch_add_explicit(&sem->sem_waiters, +1,
memory_order_acq_rel) >= 0);
unassert(atomic_fetch_add_explicit(&sem->sem_waiters, +1,
memory_order_acq_rel) >= 0);
pthread_cleanup_push(sem_timedwait_cleanup, sem);
do {
@ -82,20 +82,20 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime) {
} else if (rc == -EAGAIN || rc == -EWOULDBLOCK) {
rc = 0;
} else if (rc == -ETIMEDOUT) {
_npassert(abstime);
npassert(abstime);
if (timespec_cmp(*abstime, timespec_real()) <= 0) {
rc = etimedout();
} else {
rc = 0;
}
} else {
_npassert(!rc);
npassert(!rc);
rc = 0;
}
} else if (v > 0) {
rc = 0;
} else {
_unassert(v > INT_MIN);
unassert(v > INT_MIN);
rc = einval();
}
} while (!rc && (!v || !atomic_compare_exchange_weak_explicit(

View file

@ -36,7 +36,7 @@ int sem_trywait(sem_t *sem) {
int v;
v = atomic_load_explicit(&sem->sem_value, memory_order_relaxed);
do {
_unassert(v > INT_MIN);
unassert(v > INT_MIN);
if (!v) return eagain();
if (v < 0) return einval();
} while (!atomic_compare_exchange_weak_explicit(

View file

@ -153,7 +153,7 @@ int _join(struct spawn *th) {
int rc;
if (th->tib) {
// wait for ctid to become zero
_npassert(!_wait0(&th->tib->tib_tid, 0));
npassert(!_wait0(&th->tib->tib_tid, 0));
// free thread memory
free(th->tls);
rc = munmap(th->stk, GetStackSize());

View file

@ -12,7 +12,7 @@ COSMOPOLITAN_C_START_
* This should be favored over __get_tls() for .privileged code that
* can't be self-modified by __enable_tls().
*/
privileged static inline noasan struct CosmoTib *__get_tls_privileged(void) {
privileged static inline struct CosmoTib *__get_tls_privileged(void) {
char *tib, *lin = (char *)0x30;
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd() || IsMetal()) {
asm("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
@ -25,14 +25,14 @@ privileged static inline noasan struct CosmoTib *__get_tls_privileged(void) {
return (struct CosmoTib *)tib;
}
static noasan inline struct CosmoTib *__get_tls_win32(void) {
static dontasan inline struct CosmoTib *__get_tls_win32(void) {
char *tib, *lin = (char *)0x30;
asm("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
tib = *(char **)(tib + 0x1480 + __tls_index * 8);
return (struct CosmoTib *)tib;
}
static noasan inline void __set_tls_win32(void *tls) {
static dontasan inline void __set_tls_win32(void *tls) {
asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tls));
}

View file

@ -46,7 +46,7 @@ errno_t _wait0(const atomic_int *ctid, struct timespec *abstime) {
// "The behavior is undefined if the value specified by the thread
// argument to pthread_join() refers to the calling thread."
// ──Quoth POSIX.1-2017
_unassert(ctid != &__get_tls()->tib_tid);
unassert(ctid != &__get_tls()->tib_tid);
// "If the thread calling pthread_join() is canceled, then the target
// thread shall not be detached." ──Quoth POSIX.1-2017
if (!(rc = pthread_testcancel_np())) {