Clean up more code

The *NSYNC linked list API is good enough that it deserves to be part of
the C libray, so this change writes an improved version of it which uses
that offsetof() trick from the Linux Kernel. We vendor all of the *NSYNC
tests in third_party which helped confirm the needed refactoring is safe

This change also deletes more old code that didn't pan out. My goal here
is to work towards a vision where the Cosmopolitan core libraries become
less experimental and more focused on curation. This better reflects the
current level of quality we've managed to achieve.
This commit is contained in:
Justine Tunney 2023-07-06 06:57:28 -07:00
parent 88612a2cd7
commit 0a24b4fc3c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
268 changed files with 632 additions and 8688 deletions

View file

@ -1,15 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_BUFFER_INTERNAL_H_
#define COSMOPOLITAN_LIBC_RUNTIME_BUFFER_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct GuardedBuffer {
void *p;
};
void *balloc(struct GuardedBuffer *, unsigned, size_t);
void bfree(struct GuardedBuffer *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_BUFFER_INTERNAL_H_ */

View file

@ -34,7 +34,6 @@
#include "libc/nt/synchronization.h"
#include "libc/nt/thread.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/clone.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"

View file

@ -1,11 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_CLONE_INTERNAL_H_
#define COSMOPOLITAN_LIBC_RUNTIME_CLONE_INTERNAL_H_
#include "libc/atomic.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int clone(void *, void *, size_t, int, void *, void *, void *, void *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_CLONE_INTERNAL_H_ */

View file

@ -72,7 +72,6 @@ static int exitstatus;
static char *envs[500];
static char *args[3000];
static const char *prog;
static char errbuf[512];
static char argbuf[ARG_MAX];
static bool unsupported[256];
@ -80,35 +79,12 @@ static ssize_t Write(int fd, const char *s) {
return write(fd, s, strlen(s));
}
static void Log(const char *s, ...) {
va_list va;
va_start(va, s);
errbuf[0] = 0;
do {
strlcat(errbuf, s, sizeof(errbuf));
} while ((s = va_arg(va, const char *)));
strlcat(errbuf, "\n", sizeof(errbuf));
Write(2, errbuf);
va_end(va);
}
static wontreturn void Wexit(int rc, const char *s, ...) {
va_list va;
va_start(va, s);
errbuf[0] = 0;
do {
strlcat(errbuf, s, sizeof(errbuf));
} while ((s = va_arg(va, const char *)));
Write(2, errbuf);
va_end(va);
_Exit(rc);
}
static wontreturn void UnsupportedSyntax(unsigned char c) {
char ibuf[13], cbuf[2] = {c};
FormatOctal32(ibuf, c, true);
Wexit(4, prog, ": unsupported syntax '", cbuf, "' (", ibuf, "): ", cmd, "\n",
0);
tinyprint(2, prog, ": unsupported syntax '", cbuf, "' (", ibuf, "): ", cmd,
"\n", 0);
_Exit(4);
}
static wontreturn void SysExit(int rc, const char *call, const char *thing) {
@ -119,7 +95,8 @@ static wontreturn void SysExit(int rc, const char *call, const char *thing) {
FormatInt32(ibuf, err);
estr = _strerdoc(err);
if (!estr) estr = "EUNKNOWN";
Wexit(rc, thing, ": ", call, "() failed: ", estr, " (", ibuf, ")\n", 0);
tinyprint(2, thing, ": ", call, "() failed: ", estr, " (", ibuf, ")\n", 0);
_Exit(rc);
}
static void Open(const char *path, int fd, int flags) {
@ -132,7 +109,10 @@ static void Open(const char *path, int fd, int flags) {
static wontreturn void Exec(void) {
_unassert(args[0][0]);
if (!n) Wexit(5, prog, ": error: too few args\n", 0);
if (!n) {
tinyprint(2, prog, ": error: too few args\n", 0);
_Exit(5);
}
execvpe(args[0], args, envs);
SysExit(127, "execve", args[0]);
}
@ -255,11 +235,11 @@ static int Cd(void) {
if (!chdir(s)) {
return 0;
} else {
Log("chdir: ", s, ": ", _strerdoc(errno), NULL);
tinyprint(2, "chdir: ", s, ": ", _strerdoc(errno), NULL);
return 1;
}
} else {
Log("chdir: missing argument", NULL);
tinyprint(2, "chdir: missing argument", NULL);
return 1;
}
}
@ -270,7 +250,7 @@ static int Mkdir(void) {
if (n >= 3 && !strcmp(args[1], "-p")) ++i, f = makedirs;
for (; i < n; ++i) {
if (f(args[i], 0755)) {
Log("mkdir: ", args[i], ": ", _strerdoc(errno), NULL);
tinyprint(2, "mkdir: ", args[i], ": ", _strerdoc(errno), NULL);
return errno;
}
}
@ -287,7 +267,7 @@ static int Kill(void) {
}
for (; i < n; ++i) {
if (kill(atoi(args[i]), sig)) {
Log("kill: ", args[i], ": ", _strerdoc(errno), NULL);
tinyprint(2, "kill: ", args[i], ": ", _strerdoc(errno), NULL);
rc = 1;
}
}
@ -345,7 +325,7 @@ static int Rm(void) {
if (n > 1 && args[1][0] != '-') {
for (i = 1; i < n; ++i) {
if (unlink(args[i])) {
Log("rm: ", args[i], ": ", _strerdoc(errno), NULL);
tinyprint(2, "rm: ", args[i], ": ", _strerdoc(errno), NULL);
return 1;
}
}
@ -360,7 +340,7 @@ static int Rmdir(void) {
if (n > 1 && args[1][0] != '-') {
for (i = 1; i < n; ++i) {
if (rmdir(args[i])) {
Log("rmdir: ", args[i], ": ", _strerdoc(errno), NULL);
tinyprint(2, "rmdir: ", args[i], ": ", _strerdoc(errno), NULL);
return 1;
}
}
@ -375,7 +355,7 @@ static int Touch(void) {
if (n > 1 && args[1][0] != '-') {
for (i = 1; i < n; ++i) {
if (touch(args[i], 0644)) {
Log("touch: ", args[i], ": ", _strerdoc(errno), NULL);
tinyprint(2, "touch: ", args[i], ": ", _strerdoc(errno), NULL);
return 1;
}
}
@ -614,7 +594,8 @@ static char *Tokenize(void) {
break;
UnterminatedString:
Wexit(6, "cmd: error: unterminated string\n", 0);
tinyprint(2, "cmd: error: unterminated string\n", 0);
_Exit(6);
case STATE_QUOTED_VAR:
if (!*p) goto UnterminatedString;
@ -661,7 +642,8 @@ static const char *GetRedirectArg(const char *prog, const char *arg, int n) {
} else if ((arg = Tokenize())) {
return arg;
} else {
Wexit(14, prog, ": error: redirect missing path\n", 0);
tinyprint(2, prog, ": error: redirect missing path\n", 0);
_Exit(14);
}
}
@ -693,16 +675,19 @@ int _cocmd(int argc, char **argv, char **envp) {
}
if (argc != 3) {
Wexit(10, prog, ": error: wrong number of args\n", 0);
tinyprint(2, prog, ": error: wrong number of args\n", 0);
_Exit(10);
}
if (strcmp(argv[1], "-c")) {
Wexit(11, prog, ": error: argv[1] should -c\n", 0);
tinyprint(2, prog, ": error: argv[1] should -c\n", 0);
_Exit(11);
}
p = cmd = argv[2];
if (strlen(cmd) >= ARG_MAX) {
Wexit(12, prog, ": error: cmd too long: ", cmd, "\n", 0);
tinyprint(2, prog, ": error: cmd too long: ", cmd, "\n", 0);
_Exit(12);
}
// copy environment variables
@ -747,7 +732,8 @@ int _cocmd(int argc, char **argv, char **envp) {
args[n++] = globTheBuilder.gl_pathv[globCount];
}
} else if (globrc != GLOB_NOMATCH) {
Wexit(16, prog, ": error: with glob\n", 0);
tinyprint(2, prog, ": error: with glob\n", 0);
_Exit(16);
}
globFlags |= GLOB_APPEND;
}
@ -757,7 +743,8 @@ int _cocmd(int argc, char **argv, char **envp) {
args[n] = 0;
}
} else {
Wexit(13, prog, ": error: too many args\n", 0);
tinyprint(2, prog, ": error: too many args\n", 0);
_Exit(13);
}
}

View file

@ -38,7 +38,6 @@
* @fileoverview Cosmopolitan C Runtime, Second Edition
*/
void __wipe(uintptr_t) _Hide;
int main(int, char **, char **) __attribute__((__weak__));
typedef int init_f(int argc, char **argv, char **envp, unsigned long *auxv);
@ -169,7 +168,6 @@ textstartup void cosmo(long *sp, struct Syslib *m1) {
#endif
// run program
if (!IsTiny()) __wipe(0);
exit(main(argc, argv, envp));
}

View file

@ -24,6 +24,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/asancodes.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/runtime/internal.h"
@ -38,7 +39,7 @@
extern unsigned char __tls_mov_nt_rax[];
extern unsigned char __tls_add_nt_rax[];
nsync_dll_list_ _pthread_list;
struct Dll *_pthread_list;
pthread_spinlock_t _pthread_lock;
static struct PosixThread _pthread_main;
_Alignas(TLS_ALIGNMENT) static char __static_tls[6016];
@ -205,7 +206,6 @@ textstartup void __enable_tls(void) {
_pthread_main.flags = PT_STATIC;
_pthread_main.list.prev = _pthread_main.list.next = //
_pthread_list = VEIL("r", &_pthread_main.list);
_pthread_main.list.container = &_pthread_main;
atomic_store_explicit(&_pthread_main.ptid, tid, memory_order_relaxed);
// copy in initialized data section

View file

@ -1,58 +0,0 @@
/*-*- 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 2020 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
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.
*/
#include "libc/calls/calls.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/libfatal.internal.h"
#include "libc/runtime/ezmap.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
// TODO(jart): DELETE
_Hide int MapFileRead(const char *filename, struct MappedFile *mf) {
mf->addr = MAP_FAILED;
if ((mf->fd = open(filename, O_RDONLY)) != -1 &&
(mf->size = lseek(mf->fd, 0, SEEK_END)) < INT_MAX &&
(mf->addr = mf->size ? mmap(NULL, mf->size, PROT_READ,
MAP_PRIVATE | MAP_POPULATE, mf->fd, 0)
: NULL) != MAP_FAILED) {
return 0;
} else {
UnmapFile(mf);
return -1;
}
}
_Hide int UnmapFile(struct MappedFile *mf) {
int rc;
rc = 0;
if (mf->addr && mf->addr != MAP_FAILED) {
rc |= munmap(mf->addr, mf->size);
mf->addr = MAP_FAILED;
}
if (mf->fd != -1) {
rc |= close(mf->fd);
mf->fd = -1;
}
mf->size = 0;
return rc;
}

View file

@ -1,17 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_EZMAP_INTERNAL_H_
#define COSMOPOLITAN_LIBC_RUNTIME_EZMAP_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct MappedFile {
int fd;
void *addr;
size_t size;
};
int MapFileRead(const char *, struct MappedFile *) _Hide;
int UnmapFile(struct MappedFile *) _Hide;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_EZMAP_INTERNAL_H_ */

View file

@ -3,7 +3,6 @@
#ifndef __STRICT_ANSI__
#include "libc/dce.h"
#include "libc/elf/struct/ehdr.h"
#include "libc/runtime/ezmap.internal.h"
#include "libc/runtime/runtime.h"
#define STACK_CEIL 0x700000000000ul
@ -40,7 +39,6 @@ void __asan_init(int, char **, char **, intptr_t *) _Hide;
void _jmpstack(void *, void *, ...) _Hide wontreturn;
long _setstack(void *, void *, ...) _Hide;
int GetDosArgv(const char16_t *, char *, size_t, char **, size_t);
Elf64_Ehdr *MapElfRead(const char *, struct MappedFile *) _Hide;
int GetDosEnviron(const char16_t *, char *, size_t, char **, size_t);
bool __intercept_flag(int *, char *[], const char *);
int sys_mprotect_nt(void *, size_t, int) _Hide;

View file

@ -1,45 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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
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.
*/
#include "ape/relocations.h"
#include "libc/macros.internal.h"
// Loads all pages from program image into memory.
_peekall:
.leafprologue
ezlea __executable_start,si
ezlea _etext,cx
add $0x1000,%rsi
0: xor (%rsi),%eax
add $PAGESIZE,%rsi
cmp %rcx,%rsi
jb 0b
ezlea _etext,si
ezlea _end,cx
add $0x1000,%rsi
0: incq (%rsi)
decq (%rsi)
add $PAGESIZE,%rsi
cmp %rcx,%rsi
jb 0b
.leafepilogue
.endfn _peekall,globl
.weak __executable_start
.weak _etext
.weak _end

View file

@ -122,7 +122,6 @@ void _weakfree(void *);
void *_mapanon(size_t) attributeallocsize((1)) mallocesque;
void *_mapshared(size_t) attributeallocsize((1)) mallocesque;
void __oom_hook(size_t);
void _peekall(void);
bool _isheap(void *);
/* portability */
int NtGetVersion(void) pureconst;

View file

@ -1,45 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 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
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.
*/
#include "libc/macros.internal.h"
// Switches stack.
//
// @param rdi is new rsp, passed as malloc(size) + size
// @param rsi is function to call in new stack space
// @param rdx,rcx,r8,r9 get passed as args to rsi
// @return rax and happens on original stack
_setstack:
push %rbp
mov %rsp,%rbp
push %rbx
mov %rsp,%rbx
mov %rdi,%rsp
push 16(%rbx)
push 8(%rbx)
mov %rsi,%rax
mov %rdx,%rdi
mov %rcx,%rsi
mov %r8,%rdx
mov %r9,%rcx
call *%rax
mov %rbx,%rsp
pop %rbx
pop %rbp
ret
.endfn _setstack,globl,hidden

View file

@ -16,16 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
#include "libc/runtime/ezmap.internal.h"
#include "libc/calls/calls.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
_Hide Elf64_Ehdr *MapElfRead(const char *filename, struct MappedFile *mf) {
if (MapFileRead(filename, mf) != -1 && IsElf64Binary(mf->addr, mf->size)) {
return mf->addr;
} else {
UnmapFile(mf);
return NULL;
}
__attribute__((__weak__)) void __stack_chk_fail(void) {
tinyprint(2, program_invocation_name, ": stack smashed\n", NULL);
__builtin_trap();
}

View file

@ -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 2023 Justine Alexandra Roberts Tunney
Copyright 2020 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
@ -16,4 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/runtime.h"
#include "libc/runtime/internal.h"
__attribute__((__weak__)) void __stack_chk_fail_local(void) {
__stack_chk_fail();
}

View file

@ -1,84 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
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
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.
*/
#include "libc/nexgen32e/x86feature.h"
#include "libc/macros.internal.h"
// Zeroes as many registers as possible.
//
// Each caller should declare an appropriate prototype.
//
// @param is return value
// @return is copied from parameter
__wipe:
#ifdef __x86_64__
mov %rdi,%rax
xor %edi,%edi
xor %esi,%esi
xor %edx,%edx
xor %ecx,%ecx
xor %r8d,%r8d
xor %r9d,%r9d
xor %r10d,%r10d
xor %r11d,%r11d
testb X86_HAVE(AVX)+kCpuids(%rip)
jz .Lsse
vpxor %xmm0,%xmm0,%xmm0
vpxor %xmm1,%xmm1,%xmm1
vpxor %xmm2,%xmm2,%xmm2
vpxor %xmm3,%xmm3,%xmm3
vpxor %xmm4,%xmm4,%xmm4
vpxor %xmm5,%xmm5,%xmm5
vpxor %xmm6,%xmm6,%xmm6
vpxor %xmm7,%xmm7,%xmm7
ret
.Lsse: xorps %xmm0,%xmm0
xorps %xmm1,%xmm1
xorps %xmm2,%xmm2
xorps %xmm3,%xmm3
xorps %xmm4,%xmm4
xorps %xmm5,%xmm5
xorps %xmm6,%xmm6
xorps %xmm7,%xmm7
ret
#elif defined(__aarch64__)
mov x1,#0
mov x2,#0
mov x3,#0
mov x4,#0
mov x5,#0
mov x6,#0
mov x7,#0
mov x9,#0
mov x10,#0
mov x11,#0
mov x12,#0
mov x13,#0
mov x14,#0
mov x15,#0
movi v0.16b,#0
movi v1.16b,#0
movi v2.16b,#0
movi v3.16b,#0
movi v4.16b,#0
movi v5.16b,#0
movi v6.16b,#0
movi v7.16b,#0
ret
#endif
.endfn __wipe,globl,hidden