Skip to content

Commit

Permalink
Remove some legacy cruft
Browse files Browse the repository at this point in the history
Function trace logs will report stack usage accurately. It won't include
the argv/environ block. Our clone() polyfill is now simpler and does not
use as much stack memory. Function call tracing on x86 is now faster too
  • Loading branch information
jart committed Jan 3, 2025
1 parent 8db646f commit a15958e
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 466 deletions.
12 changes: 6 additions & 6 deletions libc/dlopen/dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static bool elf_slurp(struct Loaded *l, int fd, const char *file) {
return true;
}

static dontinline bool elf_load(struct Loaded *l, const char *file, long pagesz,
dontinline static bool elf_load(struct Loaded *l, const char *file, long pagesz,
char *interp_path, size_t interp_size) {
int fd;
if ((fd = open(file, O_RDONLY | O_CLOEXEC)) == -1)
Expand All @@ -280,15 +280,15 @@ static long *push_strs(long *sp, char **list, int count) {
return sp;
}

static wontreturn dontinstrument void foreign_helper(void **p) {
wontreturn dontinstrument static void foreign_helper(void **p) {
__foreign.dlopen = p[0];
__foreign.dlsym = p[1];
__foreign.dlclose = p[2];
__foreign.dlerror = p[3];
_longjmp(__foreign.jb, 1);
}

static dontinline void elf_exec(const char *file, char **envp) {
dontinline static void elf_exec(const char *file, char **envp) {

// get microprocessor page size
long pagesz = __pagesize;
Expand Down Expand Up @@ -412,7 +412,7 @@ static char *dlerror_set(const char *str) {
return dlerror_buf;
}

static dontinline char *foreign_alloc_block(void) {
dontinline static char *foreign_alloc_block(void) {
char *p = 0;
size_t sz = 65536;
if (!IsWindows()) {
Expand All @@ -435,7 +435,7 @@ static dontinline char *foreign_alloc_block(void) {
return p;
}

static dontinline void *foreign_alloc(size_t n) {
dontinline static void *foreign_alloc(size_t n) {
void *res;
static char *block;
__dlopen_lock();
Expand Down Expand Up @@ -548,7 +548,7 @@ static void *foreign_thunk_nt(void *func) {
return code;
}

static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
dontinline static bool foreign_compile(char exe[hasatleast PATH_MAX]) {

// construct path
strlcpy(exe, get_tmp_dir(), PATH_MAX);
Expand Down
2 changes: 1 addition & 1 deletion libc/fmt/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
int __vcscanf(int (*)(void *), int (*)(int, void *), void *, const char *,
va_list);
int __fmt(void *, void *, const char *, va_list, int *);
__msabi char16_t *__itoa16(char16_t[21], uint64_t);
char16_t *__itoa16(char16_t[21], uint64_t) __msabi;

#endif /* COSMOPOLITAN_LIBC_FMT_STRTOL_H_ */
10 changes: 5 additions & 5 deletions libc/intrin/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ __msabi extern typeof(WriteFile) *const __imp_WriteFile;

extern pthread_mutex_t __sig_worker_lock;

HAIRY static bool __sig_ignored_by_default(int sig) {
textwindows static bool __sig_ignored_by_default(int sig) {
return sig == SIGURG || //
sig == SIGCONT || //
sig == SIGCHLD || //
sig == SIGWINCH;
}

HAIRY bool __sig_ignored(int sig) {
textwindows bool __sig_ignored(int sig) {
return __sighandrvas[sig] == (intptr_t)SIG_IGN ||
(__sighandrvas[sig] == (intptr_t)SIG_DFL &&
__sig_ignored_by_default(sig));
Expand Down Expand Up @@ -532,14 +532,14 @@ textwindows void __sig_generate(int sig, int sic) {
}
}

HAIRY static char *__sig_stpcpy(char *d, const char *s) {
textwindows static char *__sig_stpcpy(char *d, const char *s) {
size_t i;
for (i = 0;; ++i)
if (!(d[i] = s[i]))
return d + i;
}

HAIRY wontreturn static void __sig_death(int sig, const char *thing) {
textwindows wontreturn static void __sig_death(int sig, const char *thing) {
#ifndef TINY
intptr_t hStderr;
char sigbuf[21], s[128], *p;
Expand Down Expand Up @@ -810,7 +810,7 @@ HAIRY static uint32_t __sig_worker(void *arg) {
_pthread_mutex_unlock(&__sig_worker_lock);
Sleep(POLL_INTERVAL_MS);
}
return 0;
__builtin_unreachable();
}

__attribute__((__constructor__(10))) textstartup void __sig_init(void) {
Expand Down
8 changes: 5 additions & 3 deletions libc/intrin/sigproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "libc/nt/thunk/msabi.h"
#ifdef __x86_64__

#define ABI __msabi textwindows dontinstrument

// cut back on code size and avoid setting errno
// this code is a mandatory dependency of winmain
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
Expand All @@ -47,8 +49,8 @@ __msabi extern typeof(GetEnvironmentVariable)
*const __imp_GetEnvironmentVariableW;

// Generates C:\ProgramData\cosmo\sig\x\y.pid like path
__msabi textwindows dontinstrument char16_t *__sig_process_path(
char16_t *path, uint32_t pid, int create_directories) {
ABI char16_t *__sig_process_path(char16_t *path, uint32_t pid,
int create_directories) {
char16_t buf[3];
char16_t *p = path;
uint32_t vlen = __imp_GetEnvironmentVariableW(u"SYSTEMDRIVE", buf, 3);
Expand Down Expand Up @@ -100,7 +102,7 @@ __msabi textwindows dontinstrument char16_t *__sig_process_path(
return path;
}

__msabi textwindows atomic_ulong *__sig_map_process(int pid, int disposition) {
ABI atomic_ulong *__sig_map_process(int pid, int disposition) {
char16_t path[128];
__sig_process_path(path, pid, disposition == kNtOpenAlways);
intptr_t hand = __imp_CreateFileW(path, kNtGenericRead | kNtGenericWrite,
Expand Down
3 changes: 1 addition & 2 deletions libc/intrin/sigprocmask-sysv.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ int sys_sigprocmask(int how, const sigset_t *opt_set,
how, opt_set ? (sigset_t *)(intptr_t)(uint32_t)*opt_set : 0, 0, 0);
rc = 0;
}
if (rc != -1 && opt_out_oldset) {
if (rc != -1 && opt_out_oldset)
*opt_out_oldset = old[0];
}
return rc;
}
6 changes: 2 additions & 4 deletions libc/intrin/strsignal_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
privileged const char *strsignal_r(int sig, char buf[21]) {
char *p;
const char *s;
if (!sig) {
if (!sig)
return "0";
}
if ((s = GetMagnumStr(kSignalNames, sig))) {
if ((s = GetMagnumStr(kSignalNames, sig)))
return s;
}
if (SIGRTMIN <= sig && sig <= SIGRTMAX) {
sig -= SIGRTMIN;
buf[0] = 'S';
Expand Down
6 changes: 5 additions & 1 deletion libc/intrin/ulock.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ int ulock_wait(uint32_t operation, void *addr, uint64_t value,
// it could also mean another thread calling ulock on this address was
// configured (via operation) in an inconsistent way.
//
int ulock_wake(uint32_t operation, void *addr, uint64_t wake_value) {
// should be dontinstrument because SiliconThreadMain() calls this from
// a stack managed by apple libc.
//
dontinstrument int ulock_wake(uint32_t operation, void *addr,
uint64_t wake_value) {
int rc;
rc = __syscall3i(operation, (long)addr, wake_value, 0x2000000 | 516);
LOCKTRACE("ulock_wake(%#x, %p, %lx) → %s", operation, addr, wake_value,
Expand Down
5 changes: 2 additions & 3 deletions libc/log/backtrace3.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
* @param st is open symbol table for current executable
* @return -1 w/ errno if error happened
*/
dontinstrument int PrintBacktraceUsingSymbols(int fd,
const struct StackFrame *bp,
struct SymbolTable *st) {
int PrintBacktraceUsingSymbols(int fd, const struct StackFrame *bp,
struct SymbolTable *st) {
size_t gi;
char *cxxbuf;
intptr_t addr;
Expand Down
4 changes: 2 additions & 2 deletions libc/log/watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ static char __watch_last[4096];

void __watch_hook(void);

static dontinstrument inline void Copy(char *p, char *q, size_t n) {
dontinstrument static inline void Copy(char *p, char *q, size_t n) {
size_t i;
for (i = 0; i < n; ++i) {
p[i] = q[i];
}
}

static dontinstrument inline int Cmp(char *p, char *q, size_t n) {
dontinstrument static inline int Cmp(char *p, char *q, size_t n) {
if (n == 8)
return READ64LE(p) != READ64LE(q);
if (n == 4)
Expand Down
15 changes: 0 additions & 15 deletions libc/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,6 @@
.balign 4
.endm

// Loads address of errno into %rcx
.macro .errno
call __errno_location
.endm

// Post-Initialization Read-Only (PIRO) BSS section.
// @param ss is an optional string, for control image locality
.macro .piro ss
.ifnb \ss
.section .piro.sort.bss.\ss,"aw",@nobits
.else
.section .piro.bss,"aw",@nobits
.endif
.endm

// Helpers for Cosmopolitan _init() amalgamation magic.
// @param name should be consistent across macros for a module
// @see libc/runtime/_init.S
Expand Down
6 changes: 3 additions & 3 deletions libc/proc/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct Procs __proc = {
.lock = PTHREAD_MUTEX_INITIALIZER,
};

static textwindows void __proc_stats(int64_t h, struct rusage *ru) {
textwindows static void __proc_stats(int64_t h, struct rusage *ru) {
bzero(ru, sizeof(*ru));
struct NtProcessMemoryCountersEx memcount = {sizeof(memcount)};
GetProcessMemoryInfo(h, &memcount, sizeof(memcount));
Expand Down Expand Up @@ -137,7 +137,7 @@ textwindows int __proc_harvest(struct Proc *pr, bool iswait4) {
return sic;
}

static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
textwindows dontinstrument static uint32_t __proc_worker(void *arg) {
struct CosmoTib tls;
char *sp = __builtin_frame_address(0);
__bootstrap_tls(&tls, __builtin_frame_address(0));
Expand Down Expand Up @@ -246,7 +246,7 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
/**
* Lazy initializes process tracker data structures and worker.
*/
static textwindows void __proc_setup(void) {
textwindows static void __proc_setup(void) {
__proc.onbirth = CreateEvent(0, 0, 0, 0); // auto reset
__proc.haszombies = CreateEvent(0, 1, 0, 0); // manual reset
__proc.thread = CreateThread(0, STACK_SIZE, __proc_worker, 0,
Expand Down
9 changes: 1 addition & 8 deletions libc/runtime/clone-linux.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// @param rdx x2 is ptid
// @param rcx x3 is ctid
// @param r8 x4 is tls
// @param r9 x5 is func(void*,int)→int
// @param r9 x5 is func(void*)→int
// @param 8(rsp) x6 is arg
// @return tid of child on success, or -errno on error
sys_clone_linux:
Expand All @@ -45,16 +45,10 @@ sys_clone_linux:
ret
2: xor %ebp,%ebp // child thread
mov %rbx,%rdi // arg
mov %r10,%r15 // experiment
mov (%r10),%esi // tid
call *%r9 // func(arg,tid)
xchg %eax,%edi // func(arg,tid) → exitcode
mov (%r15),%eax // experiment
test %eax,%eax // experiment
jz 1f // experiment
mov $60,%eax // __NR_exit(exitcode)
syscall
1: hlt // ctid was corrupted by program!
#elif defined(__aarch64__)
stp x29,x30,[sp,#-16]!
mov x29,sp
Expand All @@ -69,7 +63,6 @@ sys_clone_linux:
2: mov x29,#0 // wipe backtrace
mov x28,x3 // set cosmo tls
mov x0,x6 // child thread
ldr w1,[x4] // arg2 = *ctid
blr x5
mov x8,#93 // __NR_exit
svc #0
Expand Down
Loading

0 comments on commit a15958e

Please sign in to comment.