2021-04-09 08:06:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2021-04-09 08:06:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2023-05-02 02:43:59 +00:00
|
|
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
2021-04-09 08:06:57 +00:00
|
|
|
│ │
|
|
|
|
│ 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-06-04 08:57:10 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2023-08-15 11:40:19 +00:00
|
|
|
#include "libc/calls/struct/rlimit.h"
|
2023-05-12 12:47:54 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-05-12 02:56:33 +00:00
|
|
|
#include "libc/dce.h"
|
2023-08-15 11:40:19 +00:00
|
|
|
#include "libc/errno.h"
|
2023-05-10 05:41:57 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-07-25 12:43:04 +00:00
|
|
|
#include "libc/limits.h"
|
2023-05-10 05:41:57 +00:00
|
|
|
#include "libc/macros.internal.h"
|
|
|
|
#include "libc/nexgen32e/rdtsc.h"
|
2023-05-09 09:35:05 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2023-05-10 05:41:57 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2023-05-02 02:43:59 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-06-03 15:12:13 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2023-05-19 02:05:08 +00:00
|
|
|
#include "libc/runtime/syslib.internal.h"
|
2023-08-15 11:40:19 +00:00
|
|
|
#include "libc/sysv/consts/auxv.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/nr.h"
|
2023-06-03 15:12:13 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2023-08-15 11:40:19 +00:00
|
|
|
#include "libc/sysv/consts/rlim.h"
|
|
|
|
#include "libc/sysv/consts/rlimit.h"
|
2023-06-04 08:57:10 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2023-05-10 05:41:57 +00:00
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
2023-07-25 12:43:04 +00:00
|
|
|
#include "third_party/lua/lunix.h"
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifndef __x86_64__
|
2021-04-09 08:06:57 +00:00
|
|
|
|
2023-06-04 08:57:10 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Cosmopolitan C Runtime, Second Edition
|
|
|
|
*/
|
|
|
|
|
2023-05-09 08:56:56 +00:00
|
|
|
int main(int, char **, char **) __attribute__((__weak__));
|
2023-05-02 02:43:59 +00:00
|
|
|
|
2023-05-09 09:35:05 +00:00
|
|
|
typedef int init_f(int argc, char **argv, char **envp, unsigned long *auxv);
|
|
|
|
|
2023-05-19 02:05:08 +00:00
|
|
|
extern long syscon_start[];
|
|
|
|
extern long syscon_end[];
|
2023-06-04 15:19:45 +00:00
|
|
|
extern char syscon_linux[];
|
|
|
|
extern char syscon_xnu[];
|
|
|
|
extern char syscon_freebsd[];
|
|
|
|
extern char syscon_openbsd[];
|
|
|
|
extern char syscon_netbsd[];
|
|
|
|
extern char syscon_windows[];
|
2023-05-09 09:35:05 +00:00
|
|
|
extern init_f __strace_init;
|
2023-05-19 02:05:08 +00:00
|
|
|
extern init_f *__preinit_array_start[] __attribute__((__weak__));
|
|
|
|
extern init_f *__preinit_array_end[] __attribute__((__weak__));
|
2023-05-09 09:35:05 +00:00
|
|
|
extern init_f *__init_array_start[] __attribute__((__weak__));
|
|
|
|
extern init_f *__init_array_end[] __attribute__((__weak__));
|
2023-06-03 15:12:13 +00:00
|
|
|
extern char ape_stack_prot[] __attribute__((__weak__));
|
2023-05-10 05:41:57 +00:00
|
|
|
extern pthread_mutex_t __mmi_lock_obj;
|
2023-05-19 02:05:08 +00:00
|
|
|
extern int hostos asm("__hostos");
|
2023-05-10 05:41:57 +00:00
|
|
|
|
2023-06-04 15:19:45 +00:00
|
|
|
static const char *DecodeMagnum(const char *p, long *r) {
|
|
|
|
int k = 0;
|
|
|
|
unsigned long c, x = 0;
|
|
|
|
do {
|
|
|
|
c = *p++;
|
|
|
|
x |= (c & 127) << k;
|
|
|
|
k += 7;
|
|
|
|
} while (c & 128);
|
|
|
|
return *r = x, p;
|
|
|
|
}
|
|
|
|
|
2023-12-30 04:11:23 +00:00
|
|
|
wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename,
|
2023-12-31 14:42:36 +00:00
|
|
|
int os, long *is_freebsd) {
|
2023-05-10 05:41:57 +00:00
|
|
|
|
|
|
|
// get startup timestamp as early as possible
|
|
|
|
// its used by --strace and also kprintf() %T
|
|
|
|
kStartTsc = rdtsc();
|
|
|
|
|
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
|
|
|
// enable enough tls to survive until it can be allocated properly
|
|
|
|
struct CosmoTib tib = {
|
|
|
|
.tib_self = &tib,
|
|
|
|
.tib_self2 = &tib,
|
|
|
|
.tib_sigmask = -1,
|
|
|
|
.tib_sigstack_size = 57344,
|
|
|
|
.tib_sigstack_addr = (char *)__builtin_frame_address(0) - 57344,
|
|
|
|
.tib_tid = 1,
|
|
|
|
};
|
|
|
|
__set_tls(&tib);
|
|
|
|
|
2023-12-30 04:11:23 +00:00
|
|
|
// check for freebsd
|
|
|
|
if (is_freebsd) {
|
|
|
|
sp = is_freebsd;
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:41:57 +00:00
|
|
|
// extracts arguments from old sysv stack abi
|
2023-08-15 11:40:19 +00:00
|
|
|
int argc = *sp;
|
|
|
|
char **argv = (char **)(sp + 1);
|
|
|
|
char **envp = (char **)(sp + 1 + argc + 1);
|
|
|
|
unsigned long *auxv = (unsigned long *)(sp + 1 + argc + 1);
|
2023-05-10 05:41:57 +00:00
|
|
|
while (*auxv++) donothing;
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
// set helpful globals
|
|
|
|
__argc = argc;
|
|
|
|
__argv = argv;
|
|
|
|
__envp = envp;
|
|
|
|
__auxv = auxv;
|
|
|
|
environ = envp;
|
Loader path security (#1012)
The ape loader now passes the program executable name directly as a
register. `x2` is used on aarch64, `%rdx` on x86_64. This is passed
as the third argument to `cosmo()` (M1) or `Launch` (non-M1) and is
assigned to the global `__program_executable_name`.
`GetProgramExecutableName` now returns this global's value, setting
it if it is initially null. `InitProgramExecutableName` first tries
exotic, secure methods: `KERN_PROC_PATHNAME` on FreeBSD/NetBSD, and
`/proc` on Linux. If those produce a reasonable response (i.e., not
`"/usr/bin/ape"`, which happens with the loader before this change),
that is used. Otherwise, if `issetugid()`, the empty string is used.
Otherwise, the old argv/envp parsing code is run.
The value returned from the loader is always the full absolute path
of the binary to be executed, having passed through `realpath`. For
the non-M1 loader, this necessitated writing `RealPath`, which uses
`readlinkat` of `"/proc/self/fd/[progfd]"` on Linux, `F_GETPATH` on
Xnu, and the `__realpath` syscall on OpenBSD. On FreeBSD/NetBSD, it
punts to `GetProgramExecutableName`, which is secure on those OSes.
With the loader, all platforms now have a secure program executable
name. With no loader or an old loader, everything still works as it
did, but setuid/setgid is not supported if the insecure pathfinding
code would have been needed.
Fixes #991.
2023-12-15 17:23:58 +00:00
|
|
|
__program_executable_name = exename;
|
2023-08-19 13:41:06 +00:00
|
|
|
program_invocation_name = argv[0];
|
|
|
|
__oldstack = (intptr_t)sp;
|
2023-12-31 14:42:36 +00:00
|
|
|
if (!(hostos = os)) {
|
|
|
|
if (SupportsFreebsd() && is_freebsd) {
|
|
|
|
hostos = _HOSTFREEBSD;
|
|
|
|
} else if (SupportsXnu() && m1) {
|
|
|
|
hostos = _HOSTXNU;
|
|
|
|
} else if (SupportsLinux()) {
|
|
|
|
hostos = _HOSTLINUX;
|
|
|
|
} else {
|
|
|
|
notpossible;
|
|
|
|
}
|
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
|
2023-09-02 03:49:13 +00:00
|
|
|
const char *magnums;
|
2023-12-31 14:42:36 +00:00
|
|
|
if (IsFreebsd()) {
|
2023-12-30 04:11:23 +00:00
|
|
|
magnums = syscon_freebsd;
|
2023-12-31 14:42:36 +00:00
|
|
|
} else if (IsXnu()) {
|
|
|
|
if (!(__syslib = m1)) {
|
|
|
|
notpossible;
|
|
|
|
}
|
2023-05-19 02:05:08 +00:00
|
|
|
magnums = syscon_xnu;
|
2023-12-31 14:42:36 +00:00
|
|
|
} else if (IsLinux()) {
|
2023-05-19 02:05:08 +00:00
|
|
|
magnums = syscon_linux;
|
2023-06-04 08:57:10 +00:00
|
|
|
} else {
|
|
|
|
notpossible;
|
2023-05-19 02:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup system magic numbers
|
2023-08-15 11:40:19 +00:00
|
|
|
for (long *mp = syscon_start; mp < syscon_end; ++mp) {
|
2023-06-04 15:19:45 +00:00
|
|
|
magnums = DecodeMagnum(magnums, mp);
|
2023-05-19 02:05:08 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 08:57:10 +00:00
|
|
|
// check system call abi compatibility
|
2024-02-01 11:39:46 +00:00
|
|
|
if (IsXnu() && __syslib->__version < SYSLIB_VERSION_MANDATORY) {
|
2023-06-04 08:57:10 +00:00
|
|
|
sys_write(2, "need newer ape loader\n", 22);
|
|
|
|
_Exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable enosys trapping
|
|
|
|
if (IsBsd()) {
|
|
|
|
void *act[6] = {SIG_IGN};
|
|
|
|
sys_sigaction(SIGSYS, act, 0, 8, 0);
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:41:57 +00:00
|
|
|
// needed by kisdangerous()
|
2023-05-12 12:47:54 +00:00
|
|
|
__pid = sys_getpid().ax;
|
2023-05-10 05:41:57 +00:00
|
|
|
|
2023-06-03 15:12:13 +00:00
|
|
|
// initialize memory manager
|
2023-08-15 11:40:19 +00:00
|
|
|
_mmi.i = 0;
|
2023-05-10 05:41:57 +00:00
|
|
|
_mmi.p = _mmi.s;
|
2023-08-15 11:40:19 +00:00
|
|
|
_mmi.n = ARRAYLEN(_mmi.s);
|
2023-06-09 08:23:18 +00:00
|
|
|
__virtualmax = -1;
|
2023-06-03 15:12:13 +00:00
|
|
|
|
2023-08-15 11:40:19 +00:00
|
|
|
// initialize file system
|
|
|
|
__init_fds(argc, argv, envp);
|
|
|
|
|
2024-01-01 15:23:23 +00:00
|
|
|
// prepend cwd to executable path
|
|
|
|
__init_program_executable_name();
|
|
|
|
|
2023-08-15 11:40:19 +00:00
|
|
|
__enable_tls();
|
|
|
|
|
2023-05-13 05:42:57 +00:00
|
|
|
#if 0
|
2023-05-12 02:56:33 +00:00
|
|
|
#if IsAsan()
|
2023-06-04 08:57:10 +00:00
|
|
|
// TODO(jart): Figure out ASAN data model on AARCH64.
|
2023-05-12 02:56:33 +00:00
|
|
|
__asan_init(argc, argv, envp, auxv);
|
|
|
|
#endif
|
2023-05-13 05:42:57 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-09 09:35:05 +00:00
|
|
|
_init();
|
2023-08-15 11:40:19 +00:00
|
|
|
// initialize program
|
2023-11-29 11:45:54 +00:00
|
|
|
#if SYSDEBUG
|
2023-05-19 02:05:08 +00:00
|
|
|
argc = __strace_init(argc, argv, envp, auxv);
|
|
|
|
#endif
|
2023-08-15 11:40:19 +00:00
|
|
|
for (init_f **fp = __init_array_end; fp-- > __init_array_start;) {
|
2023-05-09 09:35:05 +00:00
|
|
|
(*fp)(argc, argv, envp, auxv);
|
|
|
|
}
|
2023-06-06 06:35:31 +00:00
|
|
|
#ifdef FTRACE
|
|
|
|
argc = ftrace_init();
|
|
|
|
#endif
|
2023-05-10 05:41:57 +00:00
|
|
|
|
|
|
|
// run program
|
2023-05-02 02:43:59 +00:00
|
|
|
exit(main(argc, argv, envp));
|
2021-04-09 08:06:57 +00:00
|
|
|
}
|
2023-05-09 08:56:56 +00:00
|
|
|
|
2023-05-10 05:41:57 +00:00
|
|
|
#endif /* __x86_64__ */
|