From 72f8bd10b7391f0dfa8cf454eabee19dce4b7fc7 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 28 May 2023 19:42:00 -0700 Subject: [PATCH] Perform some code cleanup --- libc/calls/err.c | 26 ---- libc/calls/umask.c | 19 +-- libc/intrin/asan.c | 53 +++---- libc/intrin/isdebuggerpresent.c | 8 +- libc/intrin/printsystemmappings.greg.c | 13 +- libc/intrin/restoretty.c | 7 +- libc/intrin/ubsan.c | 8 +- libc/log/libfatal.internal.h | 186 +------------------------ 8 files changed, 55 insertions(+), 265 deletions(-) delete mode 100644 libc/calls/err.c diff --git a/libc/calls/err.c b/libc/calls/err.c deleted file mode 100644 index 36498a3be..000000000 --- a/libc/calls/err.c +++ /dev/null @@ -1,26 +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 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/calls/calls.h" -#include "libc/errno.h" - -extern _Thread_local int hog; - -int dog(void) { - return hog; -} diff --git a/libc/calls/umask.c b/libc/calls/umask.c index 93daff6fb..a461a35fa 100644 --- a/libc/calls/umask.c +++ b/libc/calls/umask.c @@ -17,28 +17,11 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/sysv/consts/nr.h" -static inline unsigned sys_umask(unsigned newmask) { -#ifdef __x86_64__ - unsigned res; - asm volatile("syscall" - : "=a"(res) - : "0"(__NR_umask), "D"(newmask) - : "memory", "cc"); -#elif defined(__aarch64__) - // xnu m1 doesn't manage carry flag - register long r0 asm("x0") = newmask; - register long r8 asm("x8") = __NR_umask & 0x7ff; - register long r16 asm("x16") = __NR_umask & 0x7ff; - register unsigned res asm("x0"); - asm volatile("svc\t0" : "=r"(res) : "r"(r0), "r"(r8), "r"(r16) : "memory"); -#endif - return res; -} - /** * Sets file mode creation mask. * diff --git a/libc/intrin/asan.c b/libc/intrin/asan.c index ea0b00a60..f62f00064 100644 --- a/libc/intrin/asan.c +++ b/libc/intrin/asan.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/state.internal.h" +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/atomic.h" @@ -249,17 +250,13 @@ static void __asan_memset(void *p, char c, size_t n) { __builtin_memcpy(b + n - 8, &x, 8); break; default: - if (n <= 64) { - i = 0; - do { - __builtin_memcpy(b + i, &x, 8); - asm volatile("" ::: "memory"); - __builtin_memcpy(b + i + 8, &x, 8); - } while ((i += 16) + 16 <= n); - for (; i < n; ++i) b[i] = x; - } else { - __repstosb(p, c, n); - } + i = 0; + do { + __builtin_memcpy(b + i, &x, 8); + asm volatile("" ::: "memory"); + __builtin_memcpy(b + i + 8, &x, 8); + } while ((i += 16) + 16 <= n); + for (; i < n; ++i) b[i] = x; break; } } @@ -317,18 +314,14 @@ static void *__asan_mempcpy(void *dst, const void *src, size_t n) { __builtin_memcpy(d + n - 8, &b, 8); return d + n; default: - if (n <= 64) { - i = 0; - do { - __builtin_memcpy(&a, s + i, 8); - asm volatile("" ::: "memory"); - __builtin_memcpy(d + i, &a, 8); - } while ((i += 8) + 8 <= n); - for (; i < n; ++i) d[i] = s[i]; - return d + i; - } else { - return __repmovsb(d, s, n); - } + i = 0; + do { + __builtin_memcpy(&a, s + i, 8); + asm volatile("" ::: "memory"); + __builtin_memcpy(d + i, &a, 8); + } while ((i += 8) + 8 <= n); + for (; i < n; ++i) d[i] = s[i]; + return d + i; } } @@ -1426,8 +1419,14 @@ void __asan_map_shadow(uintptr_t p, size_t n) { __asan_unpoison((char *)p, n); } +static size_t __asan_strlen(const char *s) { + size_t i = 0; + while (s[i]) ++i; + return i; +} + static textstartup void __asan_shadow_string(char *s) { - __asan_map_shadow((intptr_t)s, __strlen(s) + 1); + __asan_map_shadow((intptr_t)s, __asan_strlen(s) + 1); } static textstartup void __asan_shadow_auxv(intptr_t *auxv) { @@ -1469,6 +1468,10 @@ static textstartup void __asan_shadow_existing_mappings(void) { __asan_poison((void *)GetStackAddr(), GUARDSIZE, kAsanStackOverflow); } +forceinline ssize_t __write_str(const char *s) { + return sys_write(2, s, __asan_strlen(s)); +} + void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) { static bool once; if (!_cmpxchg(&once, false, true)) return; @@ -1491,7 +1494,7 @@ void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) { __asan_map_shadow(0, 4096); __asan_poison(0, GUARDSIZE, kAsanNullPage); if (!IsWindows()) { - __sysv_mprotect((void *)0x7fff8000, 0x10000, PROT_READ); + sys_mprotect((void *)0x7fff8000, 0x10000, PROT_READ); } __asan_shadow_string_list(argv); __asan_shadow_string_list(envp); diff --git a/libc/intrin/isdebuggerpresent.c b/libc/intrin/isdebuggerpresent.c index 58885bd60..72096463e 100644 --- a/libc/intrin/isdebuggerpresent.c +++ b/libc/intrin/isdebuggerpresent.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/promises.internal.h" @@ -23,6 +24,7 @@ #include "libc/log/log.h" #include "libc/nt/struct/teb.h" #include "libc/runtime/runtime.h" +#include "libc/sysv/consts/at.h" #include "libc/sysv/consts/o.h" #define kBufSize 1024 @@ -48,15 +50,15 @@ int IsDebuggerPresent(bool force) { if (!PLEDGED(RPATH)) return false; res = 0; e = errno; - if ((fd = __sysv_open("/proc/self/status", O_RDONLY, 0)) >= 0) { - if ((got = __sysv_read(fd, buf, sizeof(buf) - 1)) > 0) { + if ((fd = __sys_openat(AT_FDCWD, "/proc/self/status", O_RDONLY, 0)) >= 0) { + if ((got = sys_read(fd, buf, sizeof(buf) - 1)) > 0) { buf[got] = '\0'; if ((p = __strstr(buf, kPid))) { p += sizeof(kPid) - 1; res = __atoul(p); } } - __sysv_close(fd); + sys_close(fd); } errno = e; return res; diff --git a/libc/intrin/printsystemmappings.greg.c b/libc/intrin/printsystemmappings.greg.c index b890efb3a..6104c71da 100644 --- a/libc/intrin/printsystemmappings.greg.c +++ b/libc/intrin/printsystemmappings.greg.c @@ -16,9 +16,10 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" -#include "libc/log/libfatal.internal.h" #include "libc/runtime/memtrack.internal.h" +#include "libc/sysv/consts/at.h" #include "libc/sysv/consts/o.h" privileged void PrintSystemMappings(int outfd) { @@ -26,12 +27,12 @@ privileged void PrintSystemMappings(int outfd) { ssize_t rc; char buf[64]; if (!IsWindows()) { - if ((infd = __sysv_open("/proc/self/maps", O_RDONLY, 0)) >= 0) { - __sysv_write(outfd, "\n", 1); - while ((rc = __sysv_read(infd, buf, sizeof(buf))) > 0) { - __sysv_write(outfd, buf, rc); + if ((infd = __sys_openat(AT_FDCWD, "/proc/self/maps", O_RDONLY, 0)) >= 0) { + sys_write(outfd, "\n", 1); + while ((rc = sys_read(infd, buf, sizeof(buf))) > 0) { + sys_write(outfd, buf, rc); } } - __sysv_close(infd); + sys_close(infd); } } diff --git a/libc/intrin/restoretty.c b/libc/intrin/restoretty.c index 4838b0706..afe274162 100644 --- a/libc/intrin/restoretty.c +++ b/libc/intrin/restoretty.c @@ -20,7 +20,6 @@ #include "libc/calls/syscall-sysv.internal.h" #include "libc/errno.h" #include "libc/log/internal.h" -#include "libc/log/libfatal.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/termios.h" @@ -39,6 +38,12 @@ static bool __isrestorable; static union metatermios __oldtermios; +static size_t __strlen(const char *s) { + size_t i = 0; + while (s[i]) ++i; + return i; +} + // called weakly by libc/calls/ioctl_tcsets.c to avoid pledge("tty") void __on_ioctl_tcsets(int fd) { int e; diff --git a/libc/intrin/ubsan.c b/libc/intrin/ubsan.c index ebaf529ea..a4e9a97d4 100644 --- a/libc/intrin/ubsan.c +++ b/libc/intrin/ubsan.c @@ -166,10 +166,16 @@ static char *__ubsan_itpcpy(char *p, struct UbsanTypeDescriptor *t, } } +static size_t __ubsan_strlen(const char *s) { + size_t i = 0; + while (s[i]) ++i; + return i; +} + static const char *__ubsan_dubnul(const char *s, unsigned i) { size_t n; while (i--) { - if ((n = __strlen(s))) { + if ((n = __ubsan_strlen(s))) { s += n + 1; } else { return NULL; diff --git a/libc/log/libfatal.internal.h b/libc/log/libfatal.internal.h index ee8849190..92e29ac03 100644 --- a/libc/log/libfatal.internal.h +++ b/libc/log/libfatal.internal.h @@ -1,6 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_LOG_LIBFATAL_INTERNAL_H_ #define COSMOPOLITAN_LIBC_LOG_LIBFATAL_INTERNAL_H_ #include "libc/calls/calls.h" +#include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/nt/runtime.h" @@ -10,191 +11,6 @@ COSMOPOLITAN_C_START_ #define __ToUpper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c)) -forceinline long __sysv_exit(long rc) { - long ax; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax) - : "0"(__NR_exit_group), "D"(rc) - : "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = rc; - register long r8 asm("x8") = __NR_exit_group & 0x7ff; - register long r16 asm("x16") = __NR_exit_group & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" : "=r"(res_x0) : "r"(r0), "r"(r8), "r"(r16) : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_exit_group & 0x7ff, rc); -#endif - return ax; -} - -forceinline int __sysv_close(long fd) { - long ax; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax) - : "0"(__NR_close), "D"(fd) - : "rdx", "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = fd; - register long r8 asm("x8") = __NR_close & 0x7ff; - register long r16 asm("x16") = __NR_close & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("mov\tx8,%1\n\t" - "svc\t0" - : "=r"(res_x0) - : "r"(r0), "r"(r8), "r"(r16) - : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_close & 0x7ff, fd); -#endif - return ax; -} - -forceinline int __sysv_open(const char *path, long flags, long mode) { - long ax, dx; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax), "=d"(dx) - : "0"(__NR_open), "D"(path), "S"(flags), "1"(mode) - : "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = -100; - register long r1 asm("x1") = (long)path; - register long r2 asm("x2") = (long)flags; - register long r3 asm("x3") = (long)mode; - register long r8 asm("x8") = (long)__NR_open & 0x7ff; - register long r16 asm("x16") = (long)__NR_open & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" - : "=r"(res_x0) - : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r8), "r"(r16) - : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_open & 0x7ff, path, flags, mode); -#endif - return ax; -} - -forceinline long __sysv_read(long fd, void *data, unsigned long size) { - long ax, dx; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax), "=d"(dx) - : "0"(__NR_read), "D"(fd), "S"(data), "1"(size) - : "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = (long)fd; - register long r1 asm("x1") = (long)data; - register long r2 asm("x2") = (long)size; - register long r8 asm("x8") = (long)__NR_read & 0x7ff; - register long r16 asm("x16") = (long)__NR_read & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" - : "=r"(res_x0) - : "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16) - : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_read & 0x7ff, fd, data, size); -#endif - return ax; -} - -forceinline long __sysv_write(long fd, const void *data, unsigned long size) { - long ax, dx; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax), "=d"(dx) - : "0"(__NR_write), "D"(fd), "S"(data), "1"(size) - : "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = (long)fd; - register long r1 asm("x1") = (long)data; - register long r2 asm("x2") = (long)size; - register long r8 asm("x8") = (long)__NR_write & 0x7ff; - register long r16 asm("x16") = (long)__NR_write & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" - : "=r"(res_x0) - : "i"(64), "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16) - : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_write & 0x7ff, fd, data, size); -#endif - return ax; -} - -forceinline long __sysv_mprotect(void *addr, size_t size, long prot) { - long ax, dx; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax), "=d"(dx) - : "0"(__NR_mprotect), "D"(addr), "S"(size), "1"(prot) - : "memory", "cc"); -#elif defined(__aarch64__) - register long r0 asm("x0") = (long)addr; - register long r1 asm("x1") = (long)size; - register long r2 asm("x2") = (long)prot; - register long r8 asm("x8") = (long)__NR_mprotect & 0x7ff; - register long r16 asm("x16") = (long)__NR_mprotect & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" - : "=r"(res_x0) - : "r"(r0), "r"(r1), "r"(r2), "r"(r8), "r"(r16) - : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_mprotect & 0x7ff, addr, size, prot); -#endif - return ax; -} - -forceinline int __sysv_getpid(void) { - long ax; -#if defined(__MNO_RED_ZONE__) && defined(__GNUC__) && !defined(__STRICT_ANSI__) - asm volatile("call\t__syscall__" - : "=a"(ax) - : "0"(__NR_getpid) - : "rdx", "memory", "cc"); -#elif defined(__aarch64__) - register long r8 asm("x8") = (long)__NR_getpid & 0x7ff; - register long r16 asm("x16") = (long)__NR_getpid & 0x7ff; - register long res_x0 asm("x0"); - asm volatile("svc\t0" : "=r"(res_x0) : "r"(r8), "r"(r16) : "memory"); - ax = res_x0; -#else - ax = syscall(__NR_getpid & 0x7ff); -#endif - return ax; -} - -forceinline ssize_t __write(const void *p, size_t n) { - uint32_t wrote; - if (!IsWindows()) { - return __sysv_write(2, p, n); - } else if (WriteFile(GetStdHandle(kNtStdErrorHandle), p, n, &wrote, 0)) { - return wrote; - } else { - return -GetLastError(); - } -} - -forceinline size_t __strlen(const char *s) { - size_t i = 0; - while (s[i]) ++i; - return i; -} - -forceinline ssize_t __write_str(const char *s) { - return __write(s, __strlen(s)); -} - forceinline int __strcmp(const char *l, const char *r) { size_t i = 0; while (l[i] == r[i] && r[i]) ++i;