Skip to content

Commit a15958e

Browse files
committed
Remove some legacy cruft
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
1 parent 8db646f commit a15958e

File tree

21 files changed

+290
-466
lines changed

21 files changed

+290
-466
lines changed

libc/dlopen/dlopen.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static bool elf_slurp(struct Loaded *l, int fd, const char *file) {
254254
return true;
255255
}
256256

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

283-
static wontreturn dontinstrument void foreign_helper(void **p) {
283+
wontreturn dontinstrument static void foreign_helper(void **p) {
284284
__foreign.dlopen = p[0];
285285
__foreign.dlsym = p[1];
286286
__foreign.dlclose = p[2];
287287
__foreign.dlerror = p[3];
288288
_longjmp(__foreign.jb, 1);
289289
}
290290

291-
static dontinline void elf_exec(const char *file, char **envp) {
291+
dontinline static void elf_exec(const char *file, char **envp) {
292292

293293
// get microprocessor page size
294294
long pagesz = __pagesize;
@@ -412,7 +412,7 @@ static char *dlerror_set(const char *str) {
412412
return dlerror_buf;
413413
}
414414

415-
static dontinline char *foreign_alloc_block(void) {
415+
dontinline static char *foreign_alloc_block(void) {
416416
char *p = 0;
417417
size_t sz = 65536;
418418
if (!IsWindows()) {
@@ -435,7 +435,7 @@ static dontinline char *foreign_alloc_block(void) {
435435
return p;
436436
}
437437

438-
static dontinline void *foreign_alloc(size_t n) {
438+
dontinline static void *foreign_alloc(size_t n) {
439439
void *res;
440440
static char *block;
441441
__dlopen_lock();
@@ -548,7 +548,7 @@ static void *foreign_thunk_nt(void *func) {
548548
return code;
549549
}
550550

551-
static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
551+
dontinline static bool foreign_compile(char exe[hasatleast PATH_MAX]) {
552552

553553
// construct path
554554
strlcpy(exe, get_tmp_dir(), PATH_MAX);

libc/fmt/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
int __vcscanf(int (*)(void *), int (*)(int, void *), void *, const char *,
5050
va_list);
5151
int __fmt(void *, void *, const char *, va_list, int *);
52-
__msabi char16_t *__itoa16(char16_t[21], uint64_t);
52+
char16_t *__itoa16(char16_t[21], uint64_t) __msabi;
5353

5454
#endif /* COSMOPOLITAN_LIBC_FMT_STRTOL_H_ */

libc/intrin/sig.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ __msabi extern typeof(WriteFile) *const __imp_WriteFile;
8989

9090
extern pthread_mutex_t __sig_worker_lock;
9191

92-
HAIRY static bool __sig_ignored_by_default(int sig) {
92+
textwindows static bool __sig_ignored_by_default(int sig) {
9393
return sig == SIGURG || //
9494
sig == SIGCONT || //
9595
sig == SIGCHLD || //
9696
sig == SIGWINCH;
9797
}
9898

99-
HAIRY bool __sig_ignored(int sig) {
99+
textwindows bool __sig_ignored(int sig) {
100100
return __sighandrvas[sig] == (intptr_t)SIG_IGN ||
101101
(__sighandrvas[sig] == (intptr_t)SIG_DFL &&
102102
__sig_ignored_by_default(sig));
@@ -532,14 +532,14 @@ textwindows void __sig_generate(int sig, int sic) {
532532
}
533533
}
534534

535-
HAIRY static char *__sig_stpcpy(char *d, const char *s) {
535+
textwindows static char *__sig_stpcpy(char *d, const char *s) {
536536
size_t i;
537537
for (i = 0;; ++i)
538538
if (!(d[i] = s[i]))
539539
return d + i;
540540
}
541541

542-
HAIRY wontreturn static void __sig_death(int sig, const char *thing) {
542+
textwindows wontreturn static void __sig_death(int sig, const char *thing) {
543543
#ifndef TINY
544544
intptr_t hStderr;
545545
char sigbuf[21], s[128], *p;
@@ -810,7 +810,7 @@ HAIRY static uint32_t __sig_worker(void *arg) {
810810
_pthread_mutex_unlock(&__sig_worker_lock);
811811
Sleep(POLL_INTERVAL_MS);
812812
}
813-
return 0;
813+
__builtin_unreachable();
814814
}
815815

816816
__attribute__((__constructor__(10))) textstartup void __sig_init(void) {

libc/intrin/sigproc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "libc/nt/thunk/msabi.h"
3535
#ifdef __x86_64__
3636

37+
#define ABI __msabi textwindows dontinstrument
38+
3739
// cut back on code size and avoid setting errno
3840
// this code is a mandatory dependency of winmain
3941
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
@@ -47,8 +49,8 @@ __msabi extern typeof(GetEnvironmentVariable)
4749
*const __imp_GetEnvironmentVariableW;
4850

4951
// Generates C:\ProgramData\cosmo\sig\x\y.pid like path
50-
__msabi textwindows dontinstrument char16_t *__sig_process_path(
51-
char16_t *path, uint32_t pid, int create_directories) {
52+
ABI char16_t *__sig_process_path(char16_t *path, uint32_t pid,
53+
int create_directories) {
5254
char16_t buf[3];
5355
char16_t *p = path;
5456
uint32_t vlen = __imp_GetEnvironmentVariableW(u"SYSTEMDRIVE", buf, 3);
@@ -100,7 +102,7 @@ __msabi textwindows dontinstrument char16_t *__sig_process_path(
100102
return path;
101103
}
102104

103-
__msabi textwindows atomic_ulong *__sig_map_process(int pid, int disposition) {
105+
ABI atomic_ulong *__sig_map_process(int pid, int disposition) {
104106
char16_t path[128];
105107
__sig_process_path(path, pid, disposition == kNtOpenAlways);
106108
intptr_t hand = __imp_CreateFileW(path, kNtGenericRead | kNtGenericWrite,

libc/intrin/sigprocmask-sysv.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ int sys_sigprocmask(int how, const sigset_t *opt_set,
3232
how, opt_set ? (sigset_t *)(intptr_t)(uint32_t)*opt_set : 0, 0, 0);
3333
rc = 0;
3434
}
35-
if (rc != -1 && opt_out_oldset) {
35+
if (rc != -1 && opt_out_oldset)
3636
*opt_out_oldset = old[0];
37-
}
3837
return rc;
3938
}

libc/intrin/strsignal_r.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
privileged const char *strsignal_r(int sig, char buf[21]) {
3737
char *p;
3838
const char *s;
39-
if (!sig) {
39+
if (!sig)
4040
return "0";
41-
}
42-
if ((s = GetMagnumStr(kSignalNames, sig))) {
41+
if ((s = GetMagnumStr(kSignalNames, sig)))
4342
return s;
44-
}
4543
if (SIGRTMIN <= sig && sig <= SIGRTMAX) {
4644
sig -= SIGRTMIN;
4745
buf[0] = 'S';

libc/intrin/ulock.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ int ulock_wait(uint32_t operation, void *addr, uint64_t value,
7979
// it could also mean another thread calling ulock on this address was
8080
// configured (via operation) in an inconsistent way.
8181
//
82-
int ulock_wake(uint32_t operation, void *addr, uint64_t wake_value) {
82+
// should be dontinstrument because SiliconThreadMain() calls this from
83+
// a stack managed by apple libc.
84+
//
85+
dontinstrument int ulock_wake(uint32_t operation, void *addr,
86+
uint64_t wake_value) {
8387
int rc;
8488
rc = __syscall3i(operation, (long)addr, wake_value, 0x2000000 | 516);
8589
LOCKTRACE("ulock_wake(%#x, %p, %lx) → %s", operation, addr, wake_value,

libc/log/backtrace3.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@
4848
* @param st is open symbol table for current executable
4949
* @return -1 w/ errno if error happened
5050
*/
51-
dontinstrument int PrintBacktraceUsingSymbols(int fd,
52-
const struct StackFrame *bp,
53-
struct SymbolTable *st) {
51+
int PrintBacktraceUsingSymbols(int fd, const struct StackFrame *bp,
52+
struct SymbolTable *st) {
5453
size_t gi;
5554
char *cxxbuf;
5655
intptr_t addr;

libc/log/watch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ static char __watch_last[4096];
3333

3434
void __watch_hook(void);
3535

36-
static dontinstrument inline void Copy(char *p, char *q, size_t n) {
36+
dontinstrument static inline void Copy(char *p, char *q, size_t n) {
3737
size_t i;
3838
for (i = 0; i < n; ++i) {
3939
p[i] = q[i];
4040
}
4141
}
4242

43-
static dontinstrument inline int Cmp(char *p, char *q, size_t n) {
43+
dontinstrument static inline int Cmp(char *p, char *q, size_t n) {
4444
if (n == 8)
4545
return READ64LE(p) != READ64LE(q);
4646
if (n == 4)

libc/macros.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,6 @@
291291
.balign 4
292292
.endm
293293

294-
// Loads address of errno into %rcx
295-
.macro .errno
296-
call __errno_location
297-
.endm
298-
299-
// Post-Initialization Read-Only (PIRO) BSS section.
300-
// @param ss is an optional string, for control image locality
301-
.macro .piro ss
302-
.ifnb \ss
303-
.section .piro.sort.bss.\ss,"aw",@nobits
304-
.else
305-
.section .piro.bss,"aw",@nobits
306-
.endif
307-
.endm
308-
309294
// Helpers for Cosmopolitan _init() amalgamation magic.
310295
// @param name should be consistent across macros for a module
311296
// @see libc/runtime/_init.S

libc/proc/proc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct Procs __proc = {
7171
.lock = PTHREAD_MUTEX_INITIALIZER,
7272
};
7373

74-
static textwindows void __proc_stats(int64_t h, struct rusage *ru) {
74+
textwindows static void __proc_stats(int64_t h, struct rusage *ru) {
7575
bzero(ru, sizeof(*ru));
7676
struct NtProcessMemoryCountersEx memcount = {sizeof(memcount)};
7777
GetProcessMemoryInfo(h, &memcount, sizeof(memcount));
@@ -137,7 +137,7 @@ textwindows int __proc_harvest(struct Proc *pr, bool iswait4) {
137137
return sic;
138138
}
139139

140-
static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
140+
textwindows dontinstrument static uint32_t __proc_worker(void *arg) {
141141
struct CosmoTib tls;
142142
char *sp = __builtin_frame_address(0);
143143
__bootstrap_tls(&tls, __builtin_frame_address(0));
@@ -246,7 +246,7 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
246246
/**
247247
* Lazy initializes process tracker data structures and worker.
248248
*/
249-
static textwindows void __proc_setup(void) {
249+
textwindows static void __proc_setup(void) {
250250
__proc.onbirth = CreateEvent(0, 0, 0, 0); // auto reset
251251
__proc.haszombies = CreateEvent(0, 1, 0, 0); // manual reset
252252
__proc.thread = CreateThread(0, STACK_SIZE, __proc_worker, 0,

libc/runtime/clone-linux.S

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// @param rdx x2 is ptid
2727
// @param rcx x3 is ctid
2828
// @param r8 x4 is tls
29-
// @param r9 x5 is func(void*,int)→int
29+
// @param r9 x5 is func(void*)→int
3030
// @param 8(rsp) x6 is arg
3131
// @return tid of child on success, or -errno on error
3232
sys_clone_linux:
@@ -45,16 +45,10 @@ sys_clone_linux:
4545
ret
4646
2: xor %ebp,%ebp // child thread
4747
mov %rbx,%rdi // arg
48-
mov %r10,%r15 // experiment
49-
mov (%r10),%esi // tid
5048
call *%r9 // func(arg,tid)
5149
xchg %eax,%edi // func(arg,tid) → exitcode
52-
mov (%r15),%eax // experiment
53-
test %eax,%eax // experiment
54-
jz 1f // experiment
5550
mov $60,%eax // __NR_exit(exitcode)
5651
syscall
57-
1: hlt // ctid was corrupted by program!
5852
#elif defined(__aarch64__)
5953
stp x29,x30,[sp,#-16]!
6054
mov x29,sp
@@ -69,7 +63,6 @@ sys_clone_linux:
6963
2: mov x29,#0 // wipe backtrace
7064
mov x28,x3 // set cosmo tls
7165
mov x0,x6 // child thread
72-
ldr w1,[x4] // arg2 = *ctid
7366
blr x5
7467
mov x8,#93 // __NR_exit
7568
svc #0

0 commit comments

Comments
 (0)