path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/libc/runtime/stackchkfail.c
/*-*- 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/intrin/kprintf.h" #include "libc/runtime/internal.h" privileged noasan noinstrument void __stack_chk_fail(void) { kprintf("stack smashed\n"); _Exitr(207); }
2,013
26
jart/cosmopolitan
false
cosmopolitan/libc/runtime/dlfcn.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_ #define COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_ #define RTLD_LOCAL 0 #define RTLD_LAZY 1 #define RTLD_NOW 2 #define RTLD_GLOBAL 256 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define RTLD_NEXT ((void *)-1) #define RTLD_DEFAULT ((void *)0) char *dlerror(void); void *dlopen(const char *, int); void *dlsym(void *, const char *); int dlclose(void *); int dl_iterate_phdr(int (*)(void *, size_t, void *), void *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_ */
595
24
jart/cosmopolitan
false
cosmopolitan/libc/runtime/memtrack.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_MEMTRACK_H_ #define COSMOPOLITAN_LIBC_RUNTIME_MEMTRACK_H_ #include "ape/sections.internal.h" #include "libc/dce.h" #include "libc/intrin/nopl.internal.h" #include "libc/macros.internal.h" #include "libc/nt/version.h" #include "libc/runtime/stack.h" #include "libc/sysv/consts/ss.h" #include "libc/thread/tls.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define kAutomapStart 0x100080040000 #define kAutomapSize (kMemtrackStart - kAutomapStart) #define kMemtrackStart 0x1fe7fffc0000 #define kMemtrackSize (0x1ffffffc0000 - kMemtrackStart) #define kFixedmapStart 0x300000040000 #define kFixedmapSize (0x400000040000 - kFixedmapStart) #define kMemtrackFdsStart 0x6fe000040000 #define kMemtrackFdsSize (0x6feffffc0000 - kMemtrackFdsStart) #define kMemtrackZiposStart 0x6fd000040000 #define kMemtrackZiposSize (0x6fdffffc0000 - kMemtrackZiposStart) #define kMemtrackKmallocStart 0x6fc000040000 #define kMemtrackKmallocSize (0x6fcffffc0000 - kMemtrackKmallocStart) #define kMemtrackGran (!IsAsan() ? FRAMESIZE : FRAMESIZE * 8) struct MemoryInterval { int x; int y; long h; long size; long offset; int flags; char prot; bool iscow; bool readonlyfile; }; struct MemoryIntervals { size_t i, n; struct MemoryInterval *p; struct MemoryInterval s[OPEN_MAX]; }; extern struct MemoryIntervals _mmi; void __mmi_init(void); void __mmi_lock(void); void __mmi_unlock(void); void __mmi_funlock(void); bool IsMemtracked(int, int); void PrintSystemMappings(int); unsigned FindMemoryInterval(const struct MemoryIntervals *, int) nosideeffect; bool AreMemoryIntervalsOk(const struct MemoryIntervals *) nosideeffect; void PrintMemoryIntervals(int, const struct MemoryIntervals *); int TrackMemoryInterval(struct MemoryIntervals *, int, int, long, int, int, bool, bool, long, long); int ReleaseMemoryIntervals(struct MemoryIntervals *, int, int, void (*)(struct MemoryIntervals *, int, int)); void ReleaseMemoryNt(struct MemoryIntervals *, int, int); int UntrackMemoryIntervals(void *, size_t); size_t GetMemtrackSize(struct MemoryIntervals *); #ifdef _NOPL0 #define __mmi_lock() _NOPL0("__threadcalls", __mmi_lock) #define __mmi_unlock() _NOPL0("__threadcalls", __mmi_unlock) #else #define __mmi_lock() (__threaded ? __mmi_lock() : 0) #define __mmi_unlock() (__threaded ? __mmi_unlock() : 0) #endif #ifdef __x86_64__ /* * AMD64 has 48-bit signed pointers (PML4T) * AMD64 is trying to go bigger, i.e. 57-bit (PML5T) * LINUX forbids userspace from leveraging negative pointers * Q-EMU may impose smaller vaspaces emulating AMD on non-AMD * * Having "signed pointers" means these top sixteen bits * * 0x0000000000000000 * ^^^^ * * must be * * - 0000 for positive pointers * - FFFF for negative pointers * * otherwise the instruction using the faulty pointer will fault. */ #define IsLegalPointer(p) \ (-0x800000000000 <= (intptr_t)(p) && (intptr_t)(p) <= 0x7fffffffffff) #define ADDR_32_TO_48(x) (intptr_t)((uint64_t)(int)(x) << 16) #elif defined(__aarch64__) /* * ARM64 has 48-bit unsigned pointers (Armv8.0-A) * ARM64 can possibly go bigger, i.e. 52-bit (Armv8.2-A) * ARM64 can impose arbitrarily smaller vaspaces, e.g. 40/44-bit * APPLE in their limitless authoritarianism forbids 32-bit pointers */ #define IsLegalPointer(p) ((uintptr_t)(p) <= 0xffffffffffff) #define ADDR_32_TO_48(x) (uintptr_t)((uint64_t)(uint32_t)(x) << 16) #else /* RISC-V Sipeed Nezha has 39-bit vaspace */ #error "unsupported architecture" #endif forceinline pureconst bool IsLegalSize(uint64_t n) { /* subtract frame size so roundup is safe */ return n <= 0x800000000000 - FRAMESIZE; } forceinline pureconst bool IsAutoFrame(int x) { return (int)(kAutomapStart >> 16) <= x && x <= (int)((kAutomapStart + kAutomapSize - 1) >> 16); } forceinline pureconst bool IsMemtrackFrame(int x) { return (int)(kAutomapStart >> 16) <= x && x <= (int)((kAutomapStart + kAutomapSize - 1) >> 16); } forceinline pureconst bool IsGfdsFrame(int x) { return (int)(kMemtrackFdsStart >> 16) <= x && x <= (int)((kMemtrackFdsStart + kMemtrackFdsSize - 1) >> 16); } forceinline pureconst bool IsZiposFrame(int x) { return (int)(kMemtrackZiposStart >> 16) <= x && x <= (int)((kMemtrackZiposStart + kMemtrackZiposSize - 1) >> 16); } forceinline pureconst bool IsKmallocFrame(int x) { return (int)(kMemtrackKmallocStart >> 16) <= x && x <= (int)((kMemtrackKmallocStart + kMemtrackKmallocSize - 1) >> 16); } forceinline pureconst bool IsShadowFrame(int x) { return 0x7fff <= x && x < 0x10008000; } forceinline pureconst bool IsArenaFrame(int x) { return 0x5004 <= x && x <= 0x7ffb; } forceinline pureconst bool IsStaticStackFrame(int x) { intptr_t stack = GetStaticStackAddr(0); return (int)(stack >> 16) <= x && x <= (int)((stack + (GetStackSize() - FRAMESIZE)) >> 16); } forceinline pureconst bool IsStackFrame(int x) { intptr_t stack = GetStackAddr(); return (int)(stack >> 16) <= x && x <= (int)((stack + (GetStackSize() - FRAMESIZE)) >> 16); } forceinline pureconst bool IsOldStack(const void *x) { /* openbsd uses 4mb stack by default */ /* freebsd uses 512mb stack by default */ /* most systems use 8mb stack by default */ size_t foss_stack_size = 1ul * 1024 * 1024; uintptr_t top = ROUNDUP(__oldstack + 1, foss_stack_size); uintptr_t bot = ROUNDDOWN(__oldstack, foss_stack_size); return bot <= (uintptr_t)x && (uintptr_t)x < top; } forceinline pureconst bool IsOldStackFrame(int x) { size_t foss_stack_size = 1ul * 1024 * 1024; uintptr_t top = ROUNDUP(__oldstack + 1, foss_stack_size); uintptr_t bot = ROUNDDOWN(__oldstack, foss_stack_size); return (int)(bot >> 16) <= x && x <= (int)((top >> 16) - 1); } forceinline pureconst bool IsFixedFrame(int x) { return (kFixedmapStart >> 16) <= x && x <= ((kFixedmapStart + (kFixedmapSize - 1)) >> 16); } forceinline pureconst bool OverlapsImageSpace(const void *p, size_t n) { const unsigned char *BegA, *EndA, *BegB, *EndB; if (n) { BegA = p; EndA = BegA + (n - 1); BegB = __executable_start; EndB = _end - 1; return MAX(BegA, BegB) < MIN(EndA, EndB); } else { return 0; } } forceinline pureconst bool OverlapsArenaSpace(const void *p, size_t n) { intptr_t BegA, EndA, BegB, EndB; if (n) { BegA = (intptr_t)p; EndA = BegA + (n - 1); BegB = 0x50000000; EndB = 0x7ffdffff; return MAX(BegA, BegB) < MIN(EndA, EndB); } else { return 0; } } forceinline pureconst bool OverlapsShadowSpace(const void *p, size_t n) { intptr_t BegA, EndA, BegB, EndB; if (n) { BegA = (intptr_t)p; EndA = BegA + (n - 1); BegB = 0x7fff0000; EndB = 0x10007fffffff; return MAX(BegA, BegB) < MIN(EndA, EndB); } else { return 0; } } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_MEMTRACK_H_ */
7,075
223
jart/cosmopolitan
false
cosmopolitan/libc/runtime/winargs.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_WINARGS_INTERNAL_H_ #define COSMOPOLITAN_LIBC_RUNTIME_WINARGS_INTERNAL_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct WinArgs { char *argv[4096]; char *envp[4092]; intptr_t auxv[2][2]; char argblock[ARG_MAX / 2]; char envblock[ARG_MAX / 2]; }; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_WINARGS_INTERNAL_H_ */
439
17
jart/cosmopolitan
false
cosmopolitan/libc/runtime/utmp.c
/*-*- 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/runtime/utmp.h" #include "libc/errno.h" #include "libc/runtime/utmpx.h" void setutent(void) { } void endutent(void) { } void endutxent(void) { } struct utmp *getutent(void) { return 0; } void updwtmp(const char *x, const struct utmp *y) { } void updwtmpx(const char *x, const struct utmpx *y) { } void setutxent(void) { } struct utmp *getutid(const struct utmp *x) { return 0; } struct utmpx *getutxent(void) { return 0; } struct utmpx *getutxid(const struct utmpx *x) { return 0; } struct utmpx *getutxline(const struct utmpx *x) { return 0; } int __utmpxname() { errno = ENOTSUP; return -1; } int utmpname(const char *x) { return __utmpxname(); } int utmpxname(const char *x) { return __utmpxname(); }
2,587
73
jart/cosmopolitan
false
cosmopolitan/libc/runtime/memtracknt.c
/*-*- 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/assert.h" #include "libc/intrin/strace.internal.h" #include "libc/nt/memory.h" #include "libc/nt/runtime.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" static inline noasan void *GetFrameAddr(int f) { intptr_t a; a = f; a *= FRAMESIZE; return (void *)a; } noasan void ReleaseMemoryNt(struct MemoryIntervals *mm, int l, int r) { int i, ok; size_t size; char *addr, *last; for (i = l; i <= r; ++i) { addr = GetFrameAddr(mm->p[i].x); last = GetFrameAddr(mm->p[i].y); UnmapViewOfFile(addr); CloseHandle(mm->p[i].h); } }
2,437
44
jart/cosmopolitan
false
cosmopolitan/libc/runtime/valist.c
/*-*- 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/macros.internal.h" /* <sync libc/integral/lp64arg.inc> */ struct __va_list { uint32_t gp_offset; uint32_t fp_offset; void *overflow_arg_area; void *reg_save_area; }; /* </sync libc/integral/lp64arg.inc> */ static void *__va_arg_mem(struct __va_list *ap, size_t sz, size_t align) { void *r = (void *)ROUNDUP((intptr_t)ap->overflow_arg_area, align); ap->overflow_arg_area = (void *)ROUNDUP((intptr_t)r + sz, 8); return r; } void *__va_arg(struct __va_list *ap, size_t sz, unsigned align, unsigned k) { void *r; switch (k) { case 0: if (ap->gp_offset < 48) { r = (char *)ap->reg_save_area + ap->gp_offset; ap->gp_offset += 8; return r; } else { return __va_arg_mem(ap, sz, align); } case 1: if (ap->fp_offset < 112) { r = (char *)ap->reg_save_area + ap->fp_offset; ap->fp_offset += 8; return r; } else { return __va_arg_mem(ap, sz, align); } default: return __va_arg_mem(ap, sz, align); } }
2,880
59
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getdosenviron.c
/*-*- 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/intrin/bits.h" #include "libc/intrin/tpenc.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/str/utf16.h" #define ToUpper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c)) forceinline int IsAlpha(int c) { return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); } forceinline char *MemChr(const char *s, unsigned char c, unsigned long n) { for (; n; --n, ++s) { if ((*s & 255) == c) { return s; } } return 0; } static textwindows noasan noinstrument axdx_t Recode16to8(char *dst, size_t dstsize, const char16_t *src) { bool v; axdx_t r; uint64_t w; wint_t x, y; for (v = r.ax = 0, r.dx = 0;;) { if (!(x = src[r.dx++])) break; if (!IsUcs2(x)) { y = src[r.dx++]; x = MergeUtf16(x, y); } if (!v) { if (x == '=') { v = true; } else { x = ToUpper(x); } } w = _tpenc(x); do { if (r.ax + 1 < dstsize) { dst[r.ax++] = w; } else { break; } } while ((w >>= 8)); } if (r.ax < dstsize) { dst[r.ax] = 0; } return r; } textwindows noinstrument noasan void FixPath(char *path) { char *p; size_t i; // turn backslash into slash for (p = path; *p; ++p) { if (*p == '\\') { *p = '/'; } } // turn c:/... into /c/... p = path; if (IsAlpha(p[0]) && p[1] == ':' && p[2] == '/') { p[1] = p[0]; p[0] = '/'; } for (; *p; ++p) { if (p[0] == ';' && IsAlpha(p[1]) && p[2] == ':' && p[3] == '/') { p[2] = p[1]; p[1] = '/'; } } // turn semicolon into colon for (p = path; *p; ++p) { if (*p == ';') { *p = ':'; } } } // Transcodes NT environment variable block from UTF-16 to UTF-8. // // @param env is a double NUL-terminated block of key=values // @param buf is the new environment which gets double-nul'd // @param size is the byte capacity of buf // @param envp stores NULL-terminated string pointer list (optional) // @param max is the pointer count capacity of envp // @return number of variables decoded, excluding NULL-terminator textwindows noasan noinstrument int GetDosEnviron(const char16_t *env, char *buf, size_t size, char **envp, size_t max) { int i; char *p; axdx_t r; i = 0; --size; while (*env) { if (i + 1 < max) envp[i++] = buf; r = Recode16to8(buf, size, env); if ((p = MemChr(buf, '=', r.ax)) && IsAlpha(p[1]) && p[2] == ':' && (p[3] == '\\' || p[3] == '/')) { FixPath(p + 1); } size -= r.ax + 1; buf += r.ax + 1; env += r.dx; } return i; }
4,639
136
jart/cosmopolitan
false
cosmopolitan/libc/runtime/brk.c
/*-*- 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 2021 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/assert.h" #include "libc/calls/calls.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/directmap.internal.h" #include "libc/intrin/nopl.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/runtime/brk.internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" #include "libc/thread/thread.h" #include "libc/thread/tls.h" struct Brk __brk; static bool OverlapsMmappedMemory(unsigned char *p, size_t n) { int a, b, i; _unassert(n); a = (intptr_t)p >> 16; b = (intptr_t)(p + n - 1) >> 16; i = FindMemoryInterval(&_mmi, a); if (i < _mmi.i) { if (a <= _mmi.p[i].x && _mmi.p[i].x <= b) return true; if (a <= _mmi.p[i].y && _mmi.p[i].y <= b) return true; if (_mmi.p[i].x <= a && b <= _mmi.p[i].y) return true; } return false; } static unsigned char *brk_unlocked(unsigned char *p) { int rc; struct DirectMap dm; _unassert(!((intptr_t)__brk.p & (PAGESIZE - 1))); if (p >= __brk.p) { p = (unsigned char *)ROUNDUP((intptr_t)p, PAGESIZE); } else { p = (unsigned char *)ROUNDDOWN((intptr_t)p, PAGESIZE); } if (IsWindows()) { rc = enosys(); } else if (p < _end) { rc = einval(); } else if (p > __brk.p) { if (!OverlapsMmappedMemory(__brk.p, p - __brk.p)) { // we always polyfill this system call because // 1. Linux has brk() but its behavior is poorly documented // 2. FreeBSD has sbrk(int):int but it's foreseeable it could go away // 3. XNU/OpenBSD/NetBSD have all deleted this interface in the kernel dm = sys_mmap(__brk.p, p - __brk.p, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); rc = (int)(intptr_t)dm.addr; // safe b/c __brk.p is page-aligned } else { rc = eexist(); } } else if (p < __brk.p) { rc = sys_munmap(p, __brk.p - p); } else { rc = 0; } if (rc != -1) { __brk.p = p; return 0; } else { return (unsigned char *)-1; } } void brk_lock(void) { pthread_mutex_lock(&__brk.m); } void brk_unlock(void) { pthread_mutex_unlock(&__brk.m); } void brk_funlock(void) { pthread_mutex_init(&__brk.m, 0); } __attribute__((__constructor__)) static void brk_init(void) { brk_funlock(); pthread_atfork(brk_lock, brk_unlock, brk_funlock); } #ifdef _NOPL0 #define brk_lock() _NOPL0("__threadcalls", brk_lock) #define brk_unlock() _NOPL0("__threadcalls", brk_unlock) #endif /** * Sets end of data section. * * Your program break starts right after end of `.bss` as defined * by the external linker-defined variable `end`. Setting it to a * higher address will allocate more memory. After using this you * may dealocate memory by specifying it back to a lower address. * * The only virtue of brk(), and sbrk(), aside from compatibility * with legacy software, is it's tinier than mmap() because since * this API only supports Unix, we don't bother doing the complex * memory interval tracking that mmap() does. * * @param neu is the new end address of data segment, which shall * be rounded automatically to a 4096-byte granularity * @return 0 on success, or -1 w/ errno * @raise EINVAL if `neu` is less than the `end` of `.bss` * @raise EEXIST if expanded break would overlap existing mmap() memory * @raise ENOMEM if `RLIMIT_DATA` / `RLIMIT_AS` / `RLIMIT_RSS` is exceeded * @raise ENOSYS on Windows because WIN32 puts random stuff after your break * @threadsafe */ int brk(void *neu) { unsigned char *rc; brk_lock(); if (!__brk.p) __brk.p = _end; rc = brk_unlocked(neu); brk_unlock(); STRACE("brk(%p) → %d% m", neu, rc); return (int)(intptr_t)rc; } /** * Adjusts end of data section. * * This shrinks or increases the program break by delta bytes. On * success, the previous program break is returned. It's possible * to pass 0 to this function to obtain the current program break * which is initially set to the linker-defined external variable * `end` which is the end of the `.bss` segment. Your allocations * are rounded automatically to a 4096-byte granularity. * * The only virtue of sbrk(), and brk(), aside from compatibility * with legacy software, is it's tinier than mmap() because since * this API only supports Unix, we don't bother doing the complex * memory interval tracking that mmap() does. * * @param delta is the number of bytes to allocate (or free if negative) * noting that your delta may be tuned to a number further from zero * to accommodate the page size granularity of this allocator * @return previous break on success, or `(void *)-1` w/ errno * @raise EINVAL if new break would be less than the `end` of `.bss` * @raise EEXIST if expanded break would overlap existing mmap() memory * @raise EOVERFLOW if `delta` added to break overflows the address space * @raise ENOMEM if `RLIMIT_DATA` / `RLIMIT_AS` / `RLIMIT_RSS` is exceeded * @raise ENOSYS on Windows because WIN32 puts random stuff after your break * @threadsafe */ void *sbrk(intptr_t delta) { intptr_t neu; unsigned char *rc, *old; brk_lock(); if (!__brk.p) __brk.p = _end; old = __brk.p; if (!__builtin_add_overflow((intptr_t)__brk.p, delta, &neu) && IsLegalPointer((unsigned char *)neu)) { rc = brk_unlocked((unsigned char *)neu); if (!rc) rc = old; } else { rc = (void *)eoverflow(); } brk_unlock(); STRACE("sbrk(%'ld) → %p% m", delta, rc); return rc; }
7,437
187
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ftrace-hook.S
/*-*- 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 2021 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" .privileged ftrace_hook: #ifdef __x86_64__ cmp $0,__ftrace(%rip) jg 1f ret 1: push %rbp mov %rsp,%rbp and $-16,%rsp sub $0x80,%rsp movaps %xmm0,0x00(%rsp) movaps %xmm1,0x10(%rsp) movaps %xmm2,0x20(%rsp) movaps %xmm3,0x30(%rsp) movaps %xmm4,0x40(%rsp) movaps %xmm5,0x50(%rsp) movaps %xmm6,0x60(%rsp) movaps %xmm7,0x70(%rsp) push %rax push %rax push %rdi push %rsi push %rdx push %rcx push %r8 push %r9 push %r10 push %r11 call ftracer pop %r11 pop %r10 pop %r9 pop %r8 pop %rcx pop %rdx pop %rsi pop %rdi pop %rax pop %rax movaps 0x00(%rsp),%xmm0 movaps 0x10(%rsp),%xmm1 movaps 0x20(%rsp),%xmm2 movaps 0x30(%rsp),%xmm3 movaps 0x40(%rsp),%xmm4 movaps 0x50(%rsp),%xmm5 movaps 0x60(%rsp),%xmm6 movaps 0x70(%rsp),%xmm7 leave #elif defined(__aarch64__) stp x0,x1,[sp,#-16]! stp x2,x3,[sp,#-16]! stp x4,x5,[sp,#-16]! stp x6,x7,[sp,#-16]! stp x8,x9,[sp,#-16]! stp x10,x11,[sp,#-16]! stp x12,x13,[sp,#-16]! stp x14,x15,[sp,#-16]! stp x16,x17,[sp,#-16]! stp x18,x19,[sp,#-16]! stp x20,x21,[sp,#-16]! stp x22,x23,[sp,#-16]! stp x24,x25,[sp,#-16]! stp x26,x27,[sp,#-16]! stp x28,x29,[sp,#-16]! str x30,[sp,#-16]! bl ftracer ldr x30,[sp,#16]! ldp x28,x29,[sp,#16]! ldp x26,x27,[sp,#16]! ldp x24,x25,[sp,#16]! ldp x22,x23,[sp,#16]! ldp x20,x21,[sp,#16]! ldp x18,x19,[sp,#16]! ldp x16,x17,[sp,#16]! ldp x14,x15,[sp,#16]! ldp x12,x13,[sp,#16]! ldp x10,x11,[sp,#16]! ldp x8,x9,[sp,#16]! ldp x6,x7,[sp,#16]! ldp x4,x5,[sp,#16]! ldp x2,x3,[sp,#16]! ldp x0,x1,[sp,#16]! #endif /* __x86_64__ */ ret .endfn ftrace_hook,globl
3,457
110
jart/cosmopolitan
false
cosmopolitan/libc/runtime/closesymboltable.c
/*-*- 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/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" /** * Frees symbol table. * @return 0 on success or -1 on system error */ int CloseSymbolTable(struct SymbolTable **table) { struct SymbolTable *t; if (!*table) return 0; t = *table; *table = 0; return munmap(t, t->mapsize); }
2,154
33
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mapstack.c
/*-*- 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 2022 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/dce.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/asancodes.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" /** * Allocates stack. * * The size of your returned stack is always GetStackSize(). * * The bottom 4096 bytes of your stack can't be used, since it's always * reserved for a read-only guard page. With ASAN it'll be poisoned too. * * The top 16 bytes of a stack can't be used due to openbsd:stackbound * and those bytes are also poisoned under ASAN build modes. * * @return stack bottom address on success, or null w/ errno */ void *_mapstack(void) { char *p; if ((p = mmap(0, GetStackSize(), PROT_READ | PROT_WRITE, MAP_STACK | MAP_ANONYMOUS, -1, 0)) != MAP_FAILED) { if (IsAsan()) { __asan_poison(p + GetStackSize() - 16, 16, kAsanStackOverflow); __asan_poison(p, 4096, kAsanStackOverflow); } return p; } else { return 0; } } /** * Frees stack. * * @param stk was allocated by _mapstack() */ int _freestack(void *stk) { return munmap(stk, GetStackSize()); }
3,073
64
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getargmax.c
/*-*- 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 2022 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/dce.h" #include "libc/runtime/runtime.h" /** * Returns `ARG_MAX` for host platform. */ int __arg_max(void) { if (IsWindows()) return 32767; if (IsLinux()) return 128 * 1024; if (IsNetbsd()) return 256 * 1024; if (IsFreebsd()) return 512 * 1024; if (IsOpenbsd()) return 512 * 1024; if (IsXnu()) return 1024 * 1024; return ARG_MAX; }
2,200
34
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getsymbolname.c
/*-*- 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 2021 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/runtime/symbols.internal.h" privileged noinstrument noasan noubsan char *__get_symbol_name( struct SymbolTable *t, int s) { /* asan runtime depends on this function */ if (t && s != -1) { return t->name_base + t->names[s]; } else { return 0; } }
2,118
30
jart/cosmopolitan
false
cosmopolitan/libc/runtime/clone-linux.S
/*-*- 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 2022 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" .privileged // Invokes clone() system call on GNU/Systemd. // // @param rdi x0 is flags // @param rsi x1 is top of stack // @param rdx x2 is ptid // @param rcx x3 is ctid // @param r8 x4 is tls // @param r9 x5 is func(void*,int)→int // @param 8(rsp) x6 is arg // @return tid of child on success, or -errno on error sys_clone_linux: #ifdef __x86_64__ push %rbp mov %rsp,%rbp push %rbx mov %rcx,%r10 mov 16(%rbp),%rbx mov $56,%eax // __NR_clone syscall test %rax,%rax jz 2f 0: pop %rbx pop %rbp 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 mov x8,x3 // swap x3 and x4 mov x3,x4 // swap x3 and x4 mov x4,x8 // swap x3 and x4 mov x8,#220 // __NR_clone svc #0 cbz x0,2f ldp x29,x30,[sp],#16 ret 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 #else #error "unsupported architecture" #endif .endfn sys_clone_linux,globl,hidden
3,231
80
jart/cosmopolitan
false
cosmopolitan/libc/runtime/setstack.S
/*-*- 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
2,360
46
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getmaxfd.c
/*-*- 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 2021 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/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/rlimit.h" #define F_MAXFD 11 /** * Returns maximum number of open files. */ long _GetMaxFd(void) { int rc; if (IsNetbsd()) { if ((rc = __sys_fcntl(0, F_MAXFD, 0)) != -1) { return rc; } } return _GetResourceLimit(RLIMIT_NOFILE); }
2,221
38
jart/cosmopolitan
false
cosmopolitan/libc/runtime/buffer.internal.h
#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_ */
423
16
jart/cosmopolitan
false
cosmopolitan/libc/runtime/sysconf.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_ #define COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_ #define _SC_ARG_MAX 0 #define _SC_CHILD_MAX 1 #define _SC_CLK_TCK 2 #define _SC_NGROUPS_MAX 3 #define _SC_OPEN_MAX 4 #define _SC_STREAM_MAX 5 #define _SC_TZNAME_MAX 6 #define _SC_JOB_CONTROL 7 #define _SC_SAVED_IDS 8 #define _SC_REALTIME_SIGNALS 9 #define _SC_PRIORITY_SCHEDULING 10 #define _SC_TIMERS 11 #define _SC_ASYNCHRONOUS_IO 12 #define _SC_PRIORITIZED_IO 13 #define _SC_SYNCHRONIZED_IO 14 #define _SC_FSYNC 15 #define _SC_MAPPED_FILES 16 #define _SC_MEMLOCK 17 #define _SC_MEMLOCK_RANGE 18 #define _SC_MEMORY_PROTECTION 19 #define _SC_MESSAGE_PASSING 20 #define _SC_SEMAPHORES 21 #define _SC_SHARED_MEMORY_OBJECTS 22 #define _SC_AIO_LISTIO_MAX 23 #define _SC_AIO_MAX 24 #define _SC_AIO_PRIO_DELTA_MAX 25 #define _SC_DELAYTIMER_MAX 26 #define _SC_MQ_OPEN_MAX 27 #define _SC_MQ_PRIO_MAX 28 #define _SC_VERSION 29 #define _SC_PAGE_SIZE 30 #define _SC_PAGESIZE 30 /* !! */ #define _SC_RTSIG_MAX 31 #define _SC_SEM_NSEMS_MAX 32 #define _SC_SEM_VALUE_MAX 33 #define _SC_SIGQUEUE_MAX 34 #define _SC_TIMER_MAX 35 #define _SC_BC_BASE_MAX 36 #define _SC_BC_DIM_MAX 37 #define _SC_BC_SCALE_MAX 38 #define _SC_BC_STRING_MAX 39 #define _SC_COLL_WEIGHTS_MAX 40 #define _SC_EXPR_NEST_MAX 42 #define _SC_LINE_MAX 43 #define _SC_RE_DUP_MAX 44 #define _SC_2_VERSION 46 #define _SC_2_C_BIND 47 #define _SC_2_C_DEV 48 #define _SC_2_FORT_DEV 49 #define _SC_2_FORT_RUN 50 #define _SC_2_SW_DEV 51 #define _SC_2_LOCALEDEF 52 #define _SC_UIO_MAXIOV 60 /* !! */ #define _SC_IOV_MAX 60 #define _SC_THREADS 67 #define _SC_THREAD_SAFE_FUNCTIONS 68 #define _SC_GETGR_R_SIZE_MAX 69 #define _SC_GETPW_R_SIZE_MAX 70 #define _SC_LOGIN_NAME_MAX 71 #define _SC_TTY_NAME_MAX 72 #define _SC_THREAD_DESTRUCTOR_ITERATIONS 73 #define _SC_THREAD_KEYS_MAX 74 #define _SC_THREAD_STACK_MIN 75 #define _SC_THREAD_THREADS_MAX 76 #define _SC_THREAD_ATTR_STACKADDR 77 #define _SC_THREAD_ATTR_STACKSIZE 78 #define _SC_THREAD_PRIORITY_SCHEDULING 79 #define _SC_THREAD_PRIO_INHERIT 80 #define _SC_THREAD_PRIO_PROTECT 81 #define _SC_THREAD_PROCESS_SHARED 82 #define _SC_NPROCESSORS_CONF 83 #define _SC_NPROCESSORS_ONLN 84 #define _SC_PHYS_PAGES 85 #define _SC_AVPHYS_PAGES 86 #define _SC_ATEXIT_MAX 87 #define _SC_PASS_MAX 88 #define _SC_XOPEN_VERSION 89 #define _SC_XOPEN_XCU_VERSION 90 #define _SC_XOPEN_UNIX 91 #define _SC_XOPEN_CRYPT 92 #define _SC_XOPEN_ENH_I18N 93 #define _SC_XOPEN_SHM 94 #define _SC_2_CHAR_TERM 95 #define _SC_2_UPE 97 #define _SC_XOPEN_XPG2 98 #define _SC_XOPEN_XPG3 99 #define _SC_XOPEN_XPG4 100 #define _SC_NZERO 109 #define _SC_XBS5_ILP32_OFF32 125 #define _SC_XBS5_ILP32_OFFBIG 126 #define _SC_XBS5_LP64_OFF64 127 #define _SC_XBS5_LPBIG_OFFBIG 128 #define _SC_XOPEN_LEGACY 129 #define _SC_XOPEN_REALTIME 130 #define _SC_XOPEN_REALTIME_THREADS 131 #define _SC_ADVISORY_INFO 132 #define _SC_BARRIERS 133 #define _SC_CLOCK_SELECTION 137 #define _SC_CPUTIME 138 #define _SC_THREAD_CPUTIME 139 #define _SC_MONOTONIC_CLOCK 149 #define _SC_READER_WRITER_LOCKS 153 #define _SC_SPIN_LOCKS 154 #define _SC_REGEXP 155 #define _SC_SHELL 157 #define _SC_SPAWN 159 #define _SC_SPORADIC_SERVER 160 #define _SC_THREAD_SPORADIC_SERVER 161 #define _SC_TIMEOUTS 164 #define _SC_TYPED_MEMORY_OBJECTS 165 #define _SC_2_PBS 168 #define _SC_2_PBS_ACCOUNTING 169 #define _SC_2_PBS_LOCATE 170 #define _SC_2_PBS_MESSAGE 171 #define _SC_2_PBS_TRACK 172 #define _SC_SYMLOOP_MAX 173 #define _SC_STREAMS 174 #define _SC_2_PBS_CHECKPOINT 175 #define _SC_V6_ILP32_OFF32 176 #define _SC_V6_ILP32_OFFBIG 177 #define _SC_V6_LP64_OFF64 178 #define _SC_V6_LPBIG_OFFBIG 179 #define _SC_HOST_NAME_MAX 180 #define _SC_TRACE 181 #define _SC_TRACE_EVENT_FILTER 182 #define _SC_TRACE_INHERIT 183 #define _SC_TRACE_LOG 184 #define _SC_IPV6 235 #define _SC_RAW_SOCKETS 236 #define _SC_V7_ILP32_OFF32 237 #define _SC_V7_ILP32_OFFBIG 238 #define _SC_V7_LP64_OFF64 239 #define _SC_V7_LPBIG_OFFBIG 240 #define _SC_SS_REPL_MAX 241 #define _SC_TRACE_EVENT_NAME_MAX 242 #define _SC_TRACE_NAME_MAX 243 #define _SC_TRACE_SYS_MAX 244 #define _SC_TRACE_USER_EVENT_MAX 245 #define _SC_XOPEN_STREAMS 246 #define _SC_THREAD_ROBUST_PRIO_INHERIT 247 #define _SC_THREAD_ROBUST_PRIO_PROTECT 248 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ long sysconf(int); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_ */
6,557
154
jart/cosmopolitan
false
cosmopolitan/libc/runtime/enable_tls.c
/*-*- 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 2022 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/sections.internal.h" #include "libc/assert.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/asancodes.h" #include "libc/intrin/atomic.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/thread/posixthread.internal.h" #include "libc/thread/thread.h" #include "libc/thread/tls.h" #define I(x) ((uintptr_t)x) extern unsigned char __tls_mov_nt_rax[]; extern unsigned char __tls_add_nt_rax[]; nsync_dll_list_ _pthread_list; pthread_spinlock_t _pthread_lock; static struct PosixThread _pthread_main; _Alignas(TLS_ALIGNMENT) static char __static_tls[6016]; /** * Enables thread local storage for main process. * * Here's the TLS memory layout on x86_64: * * __get_tls() * │ * %fs Linux/BSDs * _Thread_local │ * ┌───┬──────────┬──────────┼───┐ * │pad│ .tdata │ .tbss │tib│ * └───┴──────────┴──────────┼───┘ * │ * Windows/Mac %gs * * Here's the TLS memory layout on aarch64: * * x28 * %tpidr_el0 * │ * │ _Thread_local * ┌───┼───┬──────────┬──────────┐ * │tib│dtv│ .tdata │ .tbss │ * ├───┴───┴──────────┴──────────┘ * │ * __get_tls() * * This function is always called by the core runtime to guarantee TLS * is always available to your program. You must build your code using * -mno-tls-direct-seg-refs if you want to use _Thread_local. * * You can use __get_tls() to get the linear address of your tib. When * accessing TLS via privileged code you must use __get_tls_privileged * because we need code morphing to support The New Technology and XNU * * On XNU and The New Technology, this function imposes 1ms of latency * during startup for larger binaries like Python. * * If you don't want TLS and you're sure you're not using it, then you * can disable it as follows: * * int main() { * __tls_enabled_set(false); * // do stuff * } * * This is useful if you want to wrestle back control of %fs using the * arch_prctl() function. However, such programs might not be portable * and your `errno` variable also won't be thread safe anymore. */ textstartup void __enable_tls(void) { int tid; size_t hiz, siz; char *mem, *tls; struct CosmoTib *tib; // Here's the layout we're currently using: // // .balign PAGESIZE // _tdata_start: // .tdata // _tdata_size = . - _tdata_start // .balign PAGESIZE // _tbss_start: // _tdata_start + _tbss_offset: // .tbss // .balign TLS_ALIGNMENT // _tbss_size = . - _tbss_start // _tbss_end: // _tbss_start + _tbss_size: // _tdata_start + _tls_size: // _unassert(_tbss_start == _tdata_start + I(_tbss_offset)); _unassert(_tbss_start + I(_tbss_size) == _tdata_start + I(_tls_size)); #ifdef __x86_64__ siz = ROUNDUP(I(_tls_size) + sizeof(*tib), _Alignof(__static_tls)); if (siz <= sizeof(__static_tls)) { // if tls requirement is small then use the static tls block // which helps avoid a system call for appes with little tls // this is crucial to keeping life.com 16 kilobytes in size! mem = __static_tls; } else { // if this binary needs a hefty tls block then we'll bank on // malloc() being linked, which links _mapanon(). otherwise // if you exceed this, you need to STATIC_YOINK("_mapanon"). // please note that it's probably too early to call calloc() _npassert(_weaken(_mapanon)); siz = ROUNDUP(siz, FRAMESIZE); mem = _weaken(_mapanon)(siz); _npassert(mem); } if (IsAsan()) { // poison the space between .tdata and .tbss __asan_poison(mem + I(_tdata_size), I(_tbss_offset) - I(_tdata_size), kAsanProtected); } tib = (struct CosmoTib *)(mem + siz - sizeof(*tib)); tls = mem + siz - sizeof(*tib) - I(_tls_size); #elif defined(__aarch64__) hiz = ROUNDUP(sizeof(*tib) + 2 * sizeof(void *), I(_tls_align)); siz = hiz + I(_tls_size); if (siz <= sizeof(__static_tls)) { mem = __static_tls; } else { _npassert(_weaken(_mapanon)); siz = ROUNDUP(siz, FRAMESIZE); mem = _weaken(_mapanon)(siz); _npassert(mem); } if (IsAsan()) { // there's a roundup(pagesize) gap between .tdata and .tbss // poison that empty space __asan_poison(mem + hiz + I(_tdata_size), I(_tbss_offset) - I(_tdata_size), kAsanProtected); } tib = (struct CosmoTib *)mem; tls = mem + hiz; // Set the DTV. // // We don't support dynamic shared objects at the moment. The APE // linker script will only produce a single PT_TLS program header // therefore our job is relatively simple. // // @see musl/src/env/__init_tls.c // @see https://chao-tic.github.io/blog/2018/12/25/tls ((uintptr_t *)tls)[-2] = 1; ((void **)tls)[-1] = tls; #else #error "unsupported architecture" #endif /* __x86_64__ */ // initialize main thread tls memory tib->tib_self = tib; tib->tib_self2 = tib; tib->tib_errno = __errno; tib->tib_strace = __strace; tib->tib_ftrace = __ftrace; tib->tib_pthread = (pthread_t)&_pthread_main; if (IsLinux() || IsXnuSilicon()) { // gnu/systemd guarantees pid==tid for the main thread so we can // avoid issuing a superfluous system call at startup in program tid = __pid; } else { tid = sys_gettid(); } atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed); // initialize posix threads _pthread_main.tib = tib; _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 if (I(_tdata_size)) { memcpy(tls, _tdata_start, I(_tdata_size)); } // ask the operating system to change the x86 segment register __set_tls(tib); #ifdef __x86_64__ // rewrite the executable tls opcodes in memory __morph_tls(); #endif // we are now allowed to use tls __tls_enabled_set(true); }
8,562
226
jart/cosmopolitan
false
cosmopolitan/libc/runtime/msync-nt.c
/*-*- 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/macros.internal.h" #include "libc/nt/files.h" #include "libc/nt/memory.h" #include "libc/runtime/memtrack.internal.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/msync.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" noasan textwindows int sys_msync_nt(char *addr, size_t size, int flags) { int i, rc = 0; char *a, *b, *x, *y; __mmi_lock(); for (i = FindMemoryInterval(&_mmi, (intptr_t)addr >> 16); i < _mmi.i; ++i) { x = (char *)ADDR_32_TO_48(_mmi.p[i].x); y = x + _mmi.p[i].size; if ((x <= addr && addr < y) || (x < addr + size && addr + size <= y) || (addr < x && y < addr + size)) { a = MIN(MAX(addr, x), y); b = MAX(MIN(addr + size, y), x); if (!FlushViewOfFile(a, b - a)) { rc = -1; break; } // TODO(jart): FlushFileBuffers too on g_fds handle if MS_SYNC? } else { break; } } __mmi_unlock(); return rc; }
2,795
51
jart/cosmopolitan
false
cosmopolitan/libc/runtime/sysconf.c
/*-*- 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/runtime/sysconf.h" #include "libc/runtime/clktck.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/limits.h" #include "libc/sysv/consts/rlimit.h" /** * Returns configuration value about system. * * The following parameters are supported: * * - `_SC_CLK_TCK` returns number of clock ticks per second * - `_SC_ARG_MAX` currently always returns 32768 due to Windows * - `_SC_PAGESIZE` currently always returns 65536 due to Windows * - `_SC_NPROCESSORS_ONLN` returns number of CPUs in the system * - `_SC_OPEN_MAX` returns maximum number of open files * - `_SC_CHILD_MAX` returns maximum number of processes * */ long sysconf(int name) { int n; switch (name) { case _SC_ARG_MAX: return _ARG_MAX; case _SC_CHILD_MAX: return _GetResourceLimit(RLIMIT_NPROC); case _SC_CLK_TCK: return CLK_TCK; case _SC_OPEN_MAX: return _GetMaxFd(); case _SC_PAGESIZE: return FRAMESIZE; case _SC_NPROCESSORS_ONLN: n = _getcpucount(); return n > 0 ? n : -1; default: return -1; } }
2,916
58
jart/cosmopolitan
false
cosmopolitan/libc/runtime/arch_prctl.c
/*-*- 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/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/asmflag.h" #include "libc/intrin/bits.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/nexgen32e/msr.internal.h" #include "libc/nexgen32e/x86feature.h" #include "libc/runtime/pc.internal.h" #include "libc/sysv/consts/sig.h" #include "libc/sysv/errfuns.h" #ifdef __x86_64__ #define rdmsr(msr) \ ({ \ uint32_t lo, hi; \ asm volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr)); \ (uint64_t) hi << 32 | lo; \ }) #define wrmsr(msr, val) \ do { \ uint64_t val_ = (val); \ asm volatile("wrmsr" \ : /* no outputs */ \ : "c"(msr), "a"((uint32_t)val_), \ "d"((uint32_t)(val_ >> 32))); \ } while (0) int sys_set_tls(); static int arch_prctl_msr(int code, int64_t addr) { switch (code) { case ARCH_SET_GS: wrmsr(MSR_IA32_GS_BASE, addr); return 0; case ARCH_SET_FS: wrmsr(MSR_IA32_FS_BASE, addr); return 0; case ARCH_GET_GS: *(int64_t *)addr = rdmsr(MSR_IA32_GS_BASE); return 0; case ARCH_GET_FS: *(int64_t *)addr = rdmsr(MSR_IA32_FS_BASE); return 0; default: return einval(); } } static int arch_prctl_freebsd(int code, int64_t addr) { switch (code) { case ARCH_GET_FS: // sysarch(AMD64_GET_FSBASE) return sys_arch_prctl(128, addr); case ARCH_SET_FS: // sysarch(AMD64_SET_FSBASE) return sys_arch_prctl(129, (intptr_t)&addr); case ARCH_GET_GS: // sysarch(AMD64_GET_GSBASE) return sys_arch_prctl(130, addr); case ARCH_SET_GS: // sysarch(AMD64_SET_GSBASE) return sys_arch_prctl(131, (intptr_t)&addr); default: return einval(); } } static int arch_prctl_netbsd(int code, int64_t addr) { switch (code) { case ARCH_GET_FS: // sysarch(X86_GET_FSBASE) return sys_arch_prctl(15, addr); case ARCH_SET_FS: // we use _lwp_setprivate() instead of sysarch(X86_SET_FSBASE) // because the latter has a bug where signal handlers cause it // to be clobbered. please note, this doesn't apply to %gs :-) return sys_set_tls(addr); case ARCH_GET_GS: // sysarch(X86_GET_GSBASE) return sys_arch_prctl(14, addr); case ARCH_SET_GS: // sysarch(X86_SET_GSBASE) return sys_arch_prctl(16, (intptr_t)&addr); default: return einval(); } } static int arch_prctl_xnu(int code, int64_t addr) { int e; switch (code) { case ARCH_SET_GS: // thread_fast_set_cthread_self has a weird ABI e = errno; sys_set_tls(addr); errno = e; return 0; case ARCH_GET_FS: case ARCH_SET_FS: case ARCH_GET_GS: return enosys(); default: return einval(); } } static privileged dontinline int arch_prctl_openbsd(int code, int64_t addr) { bool failed; int64_t rax; switch (code) { case ARCH_GET_FS: asm volatile(CFLAG_ASM("syscall") : CFLAG_CONSTRAINT(failed), "=a"(rax) : "1"(0x014a /* __get_tcb */) : "rcx", "rdx", "rdi", "rsi", "r8", "r9", "r10", "r11", "cc", "memory"); if (failed) { errno = rax; return -1; } *(int64_t *)addr = rax; return 0; case ARCH_SET_FS: asm volatile(CFLAG_ASM("syscall") : CFLAG_CONSTRAINT(failed), "=a"(rax) : "1"(0x0149 /* __set_tcb */), "D"(addr) : "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "cc", "memory"); if (failed) { errno = rax; rax = -1; } return rax; case ARCH_GET_GS: case ARCH_SET_GS: return enosys(); default: return einval(); } } /** * Changes x86 segment registers. * * Support for segment registers is spotty across platforms. See the * table of tested combinations below. * * This wrapper has the same weird ABI as the Linux Kernel. The type * Cosmopolitan type signature of the prototype for this function is * variadic, so no value safety checking will be performed w/o asan. * * Cosmopolitan Libc initializes your process by default, to use the * segment register %fs, for thread-local storage. To safely disable * this TLS you need to either set `__tls_enabled` to 0, or you must * follow the same memory layout assumptions as your C library. When * TLS is disabled you can't use threads unless you call clone() and * that's not really a good idea since `errno` won't be thread-safe. * * Please note if you're only concerned about running on Linux, then * consider using Cosmopolitan's fsgsbase macros which don't need to * issue system calls to change %fs and %gs. See _have_fsgsbase() to * learn more. * * @param code may be * - `ARCH_SET_FS` works on Linux, FreeBSD, NetBSD, OpenBSD, Metal * - `ARCH_GET_FS` works on Linux, FreeBSD, NetBSD, OpenBSD, Metal * - `ARCH_SET_GS` works on Linux, FreeBSD, NetBSD, XNU, Metal * - `ARCH_GET_GS` works on Linux, FreeBSD, NetBSD, Metal * @param addr is treated as `intptr_t` when setting a register, and * is an output parameter (i.e. `intptr_t *`) when reading one * @raise ENOSYS if operating system didn't support changing `code` * @raise EINVAL if `code` wasn't valid * @raise EFAULT if `ARCH_SET_FS` or `ARCH_SET_GS` was used and memory * pointed to by `addr` was invalid * @see _have_fsgsbase() */ int arch_prctl(int code, int64_t addr) { int rc; if (IsAsan() && // (code == ARCH_GET_FS || // code == ARCH_GET_GS) && // !__asan_is_valid((int64_t *)addr, 8)) { rc = efault(); } else { switch (__hostos) { case _HOSTMETAL: rc = arch_prctl_msr(code, addr); break; case _HOSTFREEBSD: rc = arch_prctl_freebsd(code, addr); break; case _HOSTNETBSD: rc = arch_prctl_netbsd(code, addr); break; case _HOSTOPENBSD: rc = arch_prctl_openbsd(code, addr); break; case _HOSTLINUX: rc = sys_arch_prctl(code, addr); break; case _HOSTXNU: rc = arch_prctl_xnu(code, addr); break; default: rc = enosys(); break; } } #ifdef SYSDEBUG if (!rc && (code == ARCH_GET_FS || code == ARCH_GET_GS)) { STRACE("arch_prctl(%s, [%p]) → %d% m", DescribeArchPrctlCode(code), *(int64_t *)addr, rc); } else { STRACE("arch_prctl(%s, %p) → %d% m", DescribeArchPrctlCode(code), addr, rc); } #endif return rc; } #endif /* __x86_64__ */
8,878
244
jart/cosmopolitan
false
cosmopolitan/libc/runtime/morph.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_ #define COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_ #include "libc/calls/struct/sigset.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ void __morph_begin(sigset_t *); void __morph_end(sigset_t *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_MORPH_H_ */
363
13
jart/cosmopolitan
false
cosmopolitan/libc/runtime/exit2.c
/*-*- 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 2021 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/runtime/runtime.h" /** * Terminates process, ignoring destructors and atexit() handlers. * * When running on bare metal, this function will reboot your computer * by hosing the interrupt descriptors and triple faulting the system. * * @param exitcode is masked with 255 * @asyncsignalsafe * @vforksafe * @noreturn */ wontreturn void _exit(int exitcode) { _Exit(exitcode); }
2,238
35
jart/cosmopolitan
false
cosmopolitan/libc/runtime/login_tty.c
/*-*- 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 2022 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/calls/ioctl.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/utmp.h" #include "libc/sysv/consts/termios.h" #include "libc/sysv/errfuns.h" /** * Prepares terminal for login. * * @return 0 on success, or -1 w/ errno * @raise ENOSYS on Windows and Metal * @raise EPERM if terminal is already controlling another sid */ int login_tty(int fd) { int i, rc; if (IsLinux() || IsBsd()) { setsid(); if (!sys_ioctl(fd, TIOCSCTTY, 0)) { for (i = 0; i < 3; ++i) dup2(fd, i); if (fd > 2) close(fd); rc = 0; } else { rc = -1; } } else { rc = enosys(); } STRACE("login_tty(%d) → %d% m", fd, rc); return rc; }
2,659
53
jart/cosmopolitan
false
cosmopolitan/libc/runtime/weakfree.c
/*-*- 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 2021 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/intrin/weaken.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" /** * Thunks free() if it's linked, otherwise do nothing. */ void _weakfree(void *p) { if (_weaken(free)) { _weaken(free)(p); } }
2,069
31
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ismemtracked.greg.c
/*-*- 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 2022 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/runtime/memtrack.internal.h" static inline bool IsMemtrackedImpl(int x, int y) { unsigned i; i = FindMemoryInterval(&_mmi, x); if (i == _mmi.i) return false; if (x < _mmi.p[i].x) return false; for (;;) { if (y <= _mmi.p[i].y) return true; if (++i == _mmi.i) return false; if (_mmi.p[i].x != _mmi.p[i - 1].y + 1) return false; } } bool IsMemtracked(int x, int y) { /* assumes __mmi_lock() is held */ bool res; res = IsMemtrackedImpl(x, y); return res; }
2,338
39
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ldso.c
/*-*- 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 2021 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/runtime/dlfcn.h" char *dlerror(void) { return "cosmopolitan doesn't support dsos"; } void *dlopen(const char *file, int mode) { return NULL; } void *dlsym(void *handle, const char *name) { return NULL; } int dlclose(void *handle) { return -1; } int dl_iterate_phdr(int callback(void *info, size_t size, void *data), void *data) { return -1; }
2,229
41
jart/cosmopolitan
false
cosmopolitan/libc/runtime/fenv.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_FENV_H_ #define COSMOPOLITAN_LIBC_RUNTIME_FENV_H_ #ifdef __x86_64__ #define FE_INVALID 1 #define __FE_DENORM 2 #define FE_DIVBYZERO 4 #define FE_OVERFLOW 8 #define FE_UNDERFLOW 16 #define FE_INEXACT 32 #define FE_ALL_EXCEPT 63 #define FE_TONEAREST 0x0000 #define FE_DOWNWARD 0x0400 #define FE_UPWARD 0x0800 #define FE_TOWARDZERO 0x0c00 #define FE_DFL_ENV ((const fenv_t *)-1) typedef void *fenv_t; typedef uint16_t fexcept_t; #elif defined(__aarch64__) #define FE_INVALID 1 #define FE_DIVBYZERO 2 #define FE_OVERFLOW 4 #define FE_UNDERFLOW 8 #define FE_INEXACT 16 #define FE_ALL_EXCEPT 31 #define FE_TONEAREST 0 #define FE_DOWNWARD 0x800000 #define FE_UPWARD 0x400000 #define FE_TOWARDZERO 0xc00000 #define FE_DFL_ENV ((const fenv_t *)-1) typedef void *fenv_t; typedef uint32_t fexcept_t; #elif defined(__powerpc64__) #define FE_TONEAREST 0 #define FE_TOWARDZERO 1 #define FE_UPWARD 2 #define FE_DOWNWARD 3 #define FE_INEXACT 0x02000000 #define FE_DIVBYZERO 0x04000000 #define FE_UNDERFLOW 0x08000000 #define FE_OVERFLOW 0x10000000 #define FE_INVALID 0x20000000 #define FE_ALL_EXCEPT 0x3e000000 #define FE_INVALID_SNAN 0x01000000 #define FE_INVALID_ISI 0x00800000 #define FE_INVALID_IDI 0x00400000 #define FE_INVALID_ZDZ 0x00200000 #define FE_INVALID_IMZ 0x00100000 #define FE_INVALID_COMPARE 0x00080000 #define FE_INVALID_SOFTWARE 0x00000400 #define FE_INVALID_SQRT 0x00000200 #define FE_INVALID_INTEGER_CONVERSION 0x00000100 #define FE_ALL_INVALID 0x01f80700 #define FE_DFL_ENV ((const fenv_t *)-1) typedef unsigned fexcept_t; typedef double fenv_t; #endif /* __x86_64__ */ #ifdef __FLT_EVAL_METHOD__ #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ #else #define FLT_EVAL_METHOD 0 #endif #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define FLT_ROUNDS (__flt_rounds()) int feclearexcept(int); int fegetenv(fenv_t *); int fegetexceptflag(fexcept_t *, int); int fegetround(void); int feholdexcept(fenv_t *); int feraiseexcept(int); int fesetenv(const fenv_t *); int fesetexceptflag(const fexcept_t *, int); int fesetround(int); int fetestexcept(int); int feupdateenv(const fenv_t *); int __flt_rounds(void); int __fesetround(int); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_FENV_H_ */
2,661
90
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mapanon.c
/*-*- 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/errno.h" #include "libc/intrin/weaken.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" /** * Helper function for allocating anonymous mapping. * * This function is equivalent to: * * mmap(NULL, mapsize, PROT_READ | PROT_WRITE, * MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); * * If mmap() fails, possibly because the parent process did this: * * if (!vfork()) { * setrlimit(RLIMIT_AS, &(struct rlimit){maxbytes, maxbytes}); * execv(prog, (char *const[]){prog, 0}); * } * wait(0); * * Then this function will call: * * __oom_hook(size); * * If it's linked. The LIBC_TESTLIB library provides an implementation, * which can be installed as follows: * * int main() { * InstallQuotaHandlers(); * // ... * } * * That is performed automatically for unit test executables. * * @return memory map address on success, or null w/ errno */ void *_mapanon(size_t size) { void *m; m = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (m != MAP_FAILED) { return m; } if (errno == ENOMEM && _weaken(__oom_hook)) { _weaken(__oom_hook)(size); } return 0; }
3,147
70
jart/cosmopolitan
false
cosmopolitan/libc/runtime/interceptflag.greg.c
/*-*- 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 2021 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/log/libfatal.internal.h" #include "libc/runtime/internal.h" #include "libc/str/str.h" textstartup bool __intercept_flag(int *argc, char *argv[], const char *flag) { /* asan isn't initialized yet at runlevel 300 */ char *a; int i, j; bool found; found = false; for (j = i = 1; i <= *argc;) { a = argv[j++]; if (a && !__strcmp(a, flag)) { found = true; --*argc; } else { /* * e.g. turns ----strace → --strace for execve. * todo: update this to allow ------strace etc. */ if (a && a[0] == '-' && a[1] == '-' && !__strcmp(a + 2, flag)) { a = flag; } argv[i++] = a; } } return found; }
2,532
47
jart/cosmopolitan
false
cosmopolitan/libc/runtime/pathconf.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_PATHCONF_H_ #define COSMOPOLITAN_LIBC_RUNTIME_PATHCONF_H_ #define _PC_LINK_MAX 0 #define _PC_MAX_CANON 1 #define _PC_MAX_INPUT 2 #define _PC_NAME_MAX 3 #define _PC_PATH_MAX 4 #define _PC_PIPE_BUF 5 #define _PC_CHOWN_RESTRICTED 6 #define _PC_NO_TRUNC 7 #define _PC_VDISABLE 8 #define _PC_SYNC_IO 9 #define _PC_ASYNC_IO 10 #define _PC_PRIO_IO 11 #define _PC_SOCK_MAXBUF 12 #define _PC_FILESIZEBITS 13 #define _PC_REC_INCR_XFER_SIZE 14 #define _PC_REC_MAX_XFER_SIZE 15 #define _PC_REC_MIN_XFER_SIZE 16 #define _PC_REC_XFER_ALIGN 17 #define _PC_ALLOC_SIZE_MIN 18 #define _PC_SYMLINK_MAX 19 #define _PC_2_SYMLINKS 20 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ long fpathconf(int, int); long pathconf(const char *, int); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_PATHCONF_H_ */
1,038
35
jart/cosmopolitan
false
cosmopolitan/libc/runtime/printmaps.c
/*-*- 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 2022 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/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" /** * Prints memory mappings to stderr. */ void __print_maps(void) { __mmi_lock(); PrintMemoryIntervals(2, &_mmi); __mmi_unlock(); }
2,056
30
jart/cosmopolitan
false
cosmopolitan/libc/runtime/brk.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_BRK_INTERNAL_H_ #define COSMOPOLITAN_LIBC_RUNTIME_BRK_INTERNAL_H_ #include "libc/thread/thread.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct Brk { unsigned char *p; pthread_mutex_t m; }; extern struct Brk __brk; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_BRK_INTERNAL_H_ */
399
17
jart/cosmopolitan
false
cosmopolitan/libc/runtime/fltrounds.c
/*-*- 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 2021 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/runtime/fenv.h" int __flt_rounds() { switch (fegetround()) { case FE_TOWARDZERO: return 0; case FE_TONEAREST: return 1; case FE_UPWARD: return 2; case FE_DOWNWARD: return 3; default: return -1; } }
2,104
35
jart/cosmopolitan
false
cosmopolitan/libc/runtime/openexecutable.S
/*-*- 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 2021 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/dce.h" #include "libc/macros.internal.h" #include "libc/sysv/consts/auxv.h" #include "libc/sysv/consts/prot.h" .privileged // Opens executable in O_RDWR mode. // // To avoid ETXTBSY we need to unmap the running executable first, // then open the file, and finally load the code back into memory. // // @return file descriptor // @note only works on .com binary (not .com.dbg) // @note only supports linux, freebsd, openbsd, and netbsd _OpenExecutable: push %rbp mov %rsp,%rbp pushq __NR_open(%rip) // -0x08(%rbp) pushq __NR_mmap(%rip) // -0x10(%rbp) pushq __NR_munmap(%rip) // -0x18(%rbp) pushq O_RDWR(%rip) // -0x20(%rbp) pushq MAP_ANONYMOUS(%rip) // -0x28(%rbp) pushq MAP_PRIVATE(%rip) // -0x30(%rbp) pushq MAP_FIXED(%rip) // -0x38(%rbp) pushq __NR_mprotect(%rip) // -0x40(%rbp) pushq O_RDONLY(%rip) // -0x48(%rbp) push %rbx // code buffer push %r12 // data buffer push %r14 // filename push %r15 // fd // Get filename. lea program_executable_name(%rip),%r14 // Allocate code buffer. mov -0x10(%rbp),%eax // __NR_mmap xor %edi,%edi mov $PAGESIZE,%esi mov $PROT_READ|PROT_WRITE,%edx mov -0x28(%rbp),%r10d // MAP_ANONYMOUS or -0x30(%rbp),%r10d // MAP_PRIVATE mov $-1,%r8 mov $0,%r9 push %r9 // openbsd:pad push %r9 // openbsd:align syscall pop %r9 pop %r9 mov %rax,%rbx // Allocate data buffer. mov -0x10(%rbp),%eax // __NR_mmap xor %edi,%edi mov $ape_ram_filesz,%esi mov $PROT_READ|PROT_WRITE,%edx mov -0x28(%rbp),%r10d // MAP_ANONYMOUS or -0x30(%rbp),%r10d // MAP_PRIVATE mov $-1,%r8 mov $0,%r9 push %r9 // openbsd:pad push %r9 // openbsd:align syscall pop %r9 pop %r9 mov %rax,%r12 // Move data. mov %r12,%rdi mov $ape_ram_vaddr,%esi mov $ape_ram_filesz,%ecx rep movsb // Move code. mov %rbx,%rdi mov $8f,%esi mov $9f-8f,%ecx rep movsb // Change protection. mov -0x40(%rbp),%eax // __NR_mprotect mov %rbx,%rdi mov $PAGESIZE,%esi mov $PROT_READ|PROT_EXEC,%edx syscall jmp *%rbx // <LIMBO> // Unmap code segment. 8: mov -0x18(%rbp),%eax // __NR_munmap mov $ape_rom_vaddr,%edi mov $ape_rom_filesz,%esi syscall // Unmap data segment. mov -0x18(%rbp),%eax // __NR_munmap mov $ape_ram_vaddr,%edi mov $ape_ram_filesz,%esi syscall // Open executable in read-write mode. mov -0x08(%rbp),%eax // __NR_open mov %r14,%rdi mov -0x20(%rbp),%esi // O_RDWR clc // clear carry flag syscall jc .Lohno // bsd error cmp $-4095,%eax jae .Lohno // linux error jmp .Lok // Open executable in read-only mode. .Lohno: mov -0x08(%rbp),%eax // __NR_open mov %r14,%rdi mov -0x48(%rbp),%esi // O_RDONLY syscall .Lok: mov %eax,%r15d // Map code segment. mov -0x10(%rbp),%eax // __NR_mmap mov $ape_rom_vaddr,%edi mov $ape_rom_filesz,%esi mov $PROT_READ|PROT_EXEC,%edx mov -0x38(%rbp),%r10d // MAP_FIXED or -0x30(%rbp),%r10d // MAP_PRIVATE mov %r15d,%r8d mov $ape_rom_offset,%r9d push %r9 // openbsd:pad push %r9 // openbsd:align syscall pop %r9 pop %r9 // Allocate data segment. mov -0x10(%rbp),%eax // __NR_mmap mov $ape_ram_vaddr,%edi mov $ape_ram_filesz,%esi mov $PROT_READ|PROT_WRITE,%edx mov -0x38(%rbp),%r10d // MAP_FIXED or -0x30(%rbp),%r10d // MAP_PRIVATE or -0x28(%rbp),%r10d // MAP_ANONYMOUS mov $-1,%r8 mov $0,%r9 push %r9 // openbsd:pad push %r9 // openbsd:align syscall pop %r9 pop %r9 // Put data back. mov $ape_ram_vaddr,%edi mov %r12,%rsi mov $ape_ram_filesz,%ecx rep movsb // Jump back. mov $9f,%eax jmp *%rax // </LIMBO> // Deallocate code buffer. 9: mov __NR_munmap,%eax mov %rbx,%rdi mov $PAGESIZE,%esi syscall // Deallocate data buffer. mov __NR_munmap,%eax mov %r12,%rdi mov $ape_ram_filesz,%esi syscall mov %r15d,%eax pop %r15 pop %r14 pop %r12 pop %rbx leave ret 9: .endfn _OpenExecutable,globl .weak ape_rom_vaddr .weak ape_rom_filesz .weak ape_rom_offset .weak ape_ram_vaddr .weak ape_ram_filesz
5,980
208
jart/cosmopolitan
false
cosmopolitan/libc/runtime/clone-openbsd.S
/*-*- 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 2022 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" #define SYS___tfork 8 // Creates thread on OpenBSD. // // @param rdi is params // @param rsi is size of params // @param rdx is start function // @param rcx is start parameter // @return thread id or negative errno __tfork_thread: mov %rdx,%r8 mov %rcx,%r9 mov $SYS___tfork,%eax syscall jc 2f test %eax,%eax jz 1f ret 1: xor %ebp,%ebp mov %r9,%rdi jmp *%r8 .unreachable 2: neg %eax ret .endfn __tfork_thread,globl
2,299
46
jart/cosmopolitan
false
cosmopolitan/libc/runtime/__sigsetjmp_tail.c
/*-*- 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 2022 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/assert.h" #include "libc/calls/struct/sigset.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/sig.h" #ifdef __x86_64__ // kudos rich felker for the brilliant design _Hide int __sigsetjmp_tail(sigjmp_buf jb, int rc) { _Static_assert( sizeof(sigjmp_buf) == sizeof(jmp_buf) + 8 + 8 + sizeof(sigset_t), "please recompute sigjmp_buf w.r.t. sigset_t"); void *p = (char *)jb + sizeof(jmp_buf) + 8 + 8; _npassert(!sigprocmask(SIG_SETMASK, rc ? p : 0, rc ? 0 : p)); return rc; } #endif /* __x86_64__ */
2,382
36
jart/cosmopolitan
false
cosmopolitan/libc/runtime/set_tls.c
/*-*- 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 2022 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/assert.h" #include "libc/calls/calls.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/nexgen32e/msr.internal.h" #include "libc/nt/thread.h" #include "libc/thread/tls.h" #include "libc/thread/tls2.h" int sys_set_tls(); textstartup void __set_tls(struct CosmoTib *tib) { tib = __adj_tls(tib); #ifdef __x86_64__ // ask the operating system to change the x86 segment register int ax, dx; if (IsWindows()) { __tls_index = TlsAlloc(); _npassert(0 <= __tls_index && __tls_index < 64); asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tib)); } else if (IsFreebsd()) { sys_set_tls(129 /*AMD64_SET_FSBASE*/, tib); } else if (IsLinux()) { sys_set_tls(ARCH_SET_FS, tib); } else if (IsNetbsd()) { // netbsd has sysarch(X86_SET_FSBASE) but we can't use that because // signal handlers will cause it to be reset due to not setting the // _mc_tlsbase field in struct mcontext_netbsd. sys_set_tls(tib); } else if (IsOpenbsd()) { sys_set_tls(tib); } else if (IsXnu()) { // thread_fast_set_cthread_self has a weird ABI int e = errno; sys_set_tls((intptr_t)tib - 0x30); errno = e; } else { uint64_t val = (uint64_t)tib; asm volatile("wrmsr" : /* no outputs */ : "c"(MSR_IA32_FS_BASE), "a"((uint32_t)val), "d"((uint32_t)(val >> 32))); } #else register long x28 asm("x28") = (long)tib; asm volatile("" : "+r"(x28)); #endif }
3,379
68
jart/cosmopolitan
false
cosmopolitan/libc/runtime/clone-nt.S
/*-*- 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 2022 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" .text.windows // Used by clone() on Windows to launch thread. // // Windows owns the stack memory when we initially enter threads. // This function switches us over, so that we can start using the // runtime facilities. // // @param %rdi is arg // @param %rsi is tid // @param %rdx is func // @param %rcx is stack // @return %rax is exit code // @see clone() WinThreadLaunch: push %rbx push %r15 mov %rbp,%r15 mov %rsp,%rbx mov %rcx,%rsp xor %rbp,%rbp call *%rdx mov %r15,%rbp mov %rbx,%rsp pop %r15 pop %rbx ret .endfn WinThreadLaunch,globl,hidden
2,433
48
jart/cosmopolitan
false
cosmopolitan/libc/runtime/inflate.c
/*-*- 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 2022 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/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "third_party/puff/puff.h" #include "third_party/zlib/zlib.h" /** * Decompresses raw deflate data. * * This uses puff by default since it has a 2kb footprint. If zlib * proper is linked, then we favor that instead, since it's faster. * * @param outsize needs to be known ahead of time by some other means * @return 0 on success or nonzero on failure */ int __inflate(void *out, size_t outsize, const void *in, size_t insize) { int rc; z_stream zs; if (_weaken(inflateInit2) && _weaken(inflate) && _weaken(inflateEnd)) { zs.next_in = in; zs.avail_in = insize; zs.total_in = insize; zs.next_out = out; zs.avail_out = outsize; zs.total_out = outsize; zs.zalloc = Z_NULL; zs.zfree = Z_NULL; if ((rc = _weaken(inflateInit2)(&zs, -MAX_WBITS)) == Z_OK && (rc = _weaken(inflate)(&zs, Z_FINISH)) == Z_STREAM_END && (rc = _weaken(inflateEnd)(&zs)) == Z_OK) { rc = 0; } else if (rc == Z_OK) { rc = Z_STREAM_END; // coerce to nonzero } else { rc = rc; } } else { rc = _puff(out, &outsize, in, &insize); } STRACE("inflate([%#.*hhs%s], %'zu, %#.*hhs%s, %'zu) → %d", MIN(40, outsize), out, outsize > 40 ? "..." : "", outsize, MIN(40, insize), in, insize > 40 ? "..." : "", insize, rc); return rc; }
3,299
64
jart/cosmopolitan
false
cosmopolitan/libc/runtime/clone.internal.h
#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_ */
387
12
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ftraceinit.greg.c
/*-*- 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 2021 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/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/thread/tls.h" /** * Enables plaintext function tracing if `--ftrace` flag is passed. * * The `--ftrace` CLI arg is removed before main() is called. This code * is intended for diagnostic purposes and assumes binaries are * trustworthy and stack isn't corrupted. Logging plain text allows * program structure to easily be visualized and hotspots identified w/ * `sed | sort | uniq -c | sort`. A compressed trace can be made by * appending `--ftrace 2>&1 | gzip -4 >trace.gz` to the CLI arguments. * * @see libc/runtime/_init.S for documentation */ textstartup int ftrace_init(void) { if (__intercept_flag(&__argc, __argv, "--ftrace")) { ftrace_install(); ftrace_enabled(+1); } return __argc; }
2,665
43
jart/cosmopolitan
false
cosmopolitan/libc/runtime/enable_threads.c
/*-*- 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 2022 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/sections.internal.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/struct/sigset.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/runtime/morph.h" #include "libc/runtime/runtime.h" #include "libc/thread/tls.h" extern int __threadcalls_end[]; extern int __threadcalls_start[]; #pragma weak __threadcalls_start #pragma weak __threadcalls_end static privileged dontinline void FixupLockNops(void) { sigset_t mask; __morph_begin(&mask); /* * _NOPL("__threadcalls", func) * * The big ugly macro above is used by Cosmopolitan Libc to unser * locking primitive (e.g. flockfile, funlockfile) have zero impact on * performance and binary size when threads aren't actually in play. * * we have this * * 0f 1f 05 b1 19 00 00 nopl func(%rip) * * we're going to turn it into this * * 67 67 e8 b1 19 00 00 addr32 addr32 call func * * This is cheap and fast because the big ugly macro stored in the * binary the offsets of all the instructions we need to change. */ for (int *p = __threadcalls_start; p < __threadcalls_end; ++p) { __executable_start[*p + 0] = 0x67; __executable_start[*p + 1] = 0x67; __executable_start[*p + 2] = 0xe8; } __morph_end(&mask); } void __enable_threads(void) { if (__threaded) return; #ifdef __x86_64__ STRACE("__enable_threads()"); FixupLockNops(); #endif __threaded = __tls_enabled ? __get_tls()->tib_tid : sys_gettid(); }
3,371
71
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mapshared.c
/*-*- 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 2022 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" #include "libc/intrin/weaken.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" /** * Creates anonymous shared memory mapping. * * @return memory map address on success, or null w/ errno */ void *_mapshared(size_t size) { void *m; m = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (m != MAP_FAILED) { return m; } if (errno == ENOMEM && _weaken(__oom_hook)) { _weaken(__oom_hook)(size); } return 0; }
2,403
42
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getlogin_r.c
/*-*- 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 2022 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/syscall_support-nt.internal.h" #include "libc/dce.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/nt/accounting.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/errfuns.h" int sys_getlogin(char *, uint32_t); /** * Returns login name. */ int getlogin_r(char *buf, size_t size) { int rc; uint32_t n32; const char *p; char16_t buf16[257]; if (IsBsd()) { rc = sys_getlogin(buf, size); } else if (IsWindows()) { n32 = ARRAYLEN(buf16); if (GetUserName(&buf16, &n32)) { tprecode16to8(buf, sizeof(size), buf16); rc = 0; } else { rc = __winerr(); } } else if ((p = getenv("LOGNAME"))) { if (strlen(p) < size) { strcpy(buf, p); rc = 0; } else { rc = enomem(); } } else { rc = enoent(); } STRACE("getlogin_r([%#s], %'zu) → %d% m", rc != -1 ? buf : "n/a", size, rc); return rc; }
2,837
62
jart/cosmopolitan
false
cosmopolitan/libc/runtime/findcombinary.c
/*-*- 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/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/auxv.h" struct FindComBinary { bool once; const char *res; char buf[PATH_MAX]; }; static struct FindComBinary g_findcombinary; /** * Returns path of binary without debug information, or null. * * @return path to non-debug binary, or -1 w/ errno */ const char *FindComBinary(void) { size_t len; const char *p; if (!g_findcombinary.once) { g_findcombinary.once = true; if ((p = GetProgramExecutableName()) && (len = strlen(p)) < ARRAYLEN(g_findcombinary.buf)) { g_findcombinary.res = memcpy(g_findcombinary.buf, p, len + 1); if (len > 4 && memcmp(&g_findcombinary.buf[len - 4], ".dbg", 4) == 0) { g_findcombinary.buf[len - 4] = '\0'; } } } return g_findcombinary.res; }
2,694
52
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ftracer.c
/*-*- 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/errno.h" #include "libc/fmt/itoa.h" #include "libc/intrin/cmpxchg.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/nopl.internal.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/stackframe.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/runtime/symbols.internal.h" #include "libc/thread/tls.h" #include "libc/thread/tls2.h" #define MAX_NESTING 512 /** * @fileoverview Plain-text function call logging. * * Able to log ~2 million function calls per second, which is mostly * bottlenecked by system call overhead. Log size is reasonable if piped * into gzip. */ void ftrace_hook(void); static int g_stackdigs; static struct CosmoFtrace g_ftrace; static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) { int nesting = -2; while (frame) { ++nesting; frame = frame->next; } return MAX(0, nesting); } static privileged inline int GetNestingLevel(struct CosmoFtrace *ft, struct StackFrame *sf) { int nesting; nesting = GetNestingLevelImpl(sf); if (nesting < ft->ft_skew) ft->ft_skew = nesting; nesting -= ft->ft_skew; return MIN(MAX_NESTING, nesting); } /** * Prints name of function being called. * * We insert CALL instructions that point to this function, in the * prologues of other functions. We assume those functions behave * according to the System Five NexGen32e ABI. */ privileged void ftracer(void) { long stackuse; struct CosmoTib *tib; struct StackFrame *sf; struct CosmoFtrace *ft; if (__ftrace <= 0) return; if (__tls_enabled) { tib = __get_tls_privileged(); if (tib->tib_ftrace <= 0) return; ft = &tib->tib_ftracer; } else { ft = &g_ftrace; } if (_cmpxchg(&ft->ft_once, false, true)) { ft->ft_lastaddr = -1; ft->ft_skew = GetNestingLevelImpl(__builtin_frame_address(0)); } if (_cmpxchg(&ft->ft_noreentry, false, true)) { sf = __builtin_frame_address(0); sf = sf->next; if (sf->addr != ft->ft_lastaddr) { stackuse = GetStackAddr() + GetStackSize() - (intptr_t)sf; kprintf("%rFUN %6P %'13T %'*ld %*s%t\n", g_stackdigs, stackuse, GetNestingLevel(ft, sf) * 2, "", sf->addr); ft->ft_lastaddr = sf->addr; } ft->ft_noreentry = false; } } textstartup int ftrace_install(void) { if (GetSymbolTable()) { g_stackdigs = LengthInt64Thousands(GetStackSize()); return __hook(ftrace_hook, GetSymbolTable()); } else { kprintf("error: --ftrace failed to open symbol table\n"); return -1; } }
4,490
113
jart/cosmopolitan
false
cosmopolitan/libc/runtime/clktck.c
/*-*- 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 2021 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/runtime/clktck.h" #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/fmt/conv.h" #include "libc/intrin/_getauxval.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/auxv.h" struct clockinfo_netbsd { int32_t hz; // number of clock ticks per second int32_t tick; // µs per tick int32_t tickadj; // skew rate for adjtime() int32_t stathz; // statistics clock frequency int32_t profhz; // profiling clock frequency }; static int clk_tck; static dontinline int __clk_tck_init(void) { int x; int cmd[2]; size_t len; struct clockinfo_netbsd clock; if (IsWindows()) { x = HECTONANOSECONDS; } else if (IsXnu() || IsOpenbsd()) { x = 100; } else if (IsFreebsd()) { x = 128; } else if (IsNetbsd()) { cmd[0] = 1; // CTL_KERN cmd[1] = 12; // KERN_CLOCKRATE len = sizeof(clock); if (sys_sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) { x = clock.hz; } else { x = -1; } } else { x = _getauxval(AT_CLKTCK).value; } if (x < 1) x = 100; clk_tck = x; return x; } /** * Returns system clock ticks per second. * * The returned value is memoized. This function is intended to be * used via the `CLK_TCK` macro wrapper. * * The returned value is always greater than zero. It's usually 100 * hertz which means each clock tick is 10 milliseconds long. */ int __clk_tck(void) { if (clk_tck) { return clk_tck; } else { return __clk_tck_init(); } }
3,340
81
jart/cosmopolitan
false
cosmopolitan/libc/runtime/straceinit.greg.c
/*-*- 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 2021 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/dce.h" #include "libc/intrin/atomic.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" /** * Enables plaintext system call logging if `--strace` flag is passed. */ textstartup int __strace_init(int argc, char **argv, char **envp, long *auxv) { /* asan isn't initialized yet at runlevel 300 */ if (__intercept_flag(&argc, argv, "--strace") || __atoul(nulltoempty(__getenv(envp, "STRACE")))) { strace_enabled(+1); } return (__argc = argc); }
2,460
38
jart/cosmopolitan
false
cosmopolitan/libc/runtime/printargs.c
/*-*- 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 2021 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/calls/struct/rlimit.h" #include "libc/calls/struct/sched_param.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/sigset.h" #include "libc/calls/struct/termios.h" #include "libc/calls/struct/utsname.h" #include "libc/calls/termios.h" #include "libc/calls/ttydefaults.h" #include "libc/dce.h" #include "libc/dns/dns.h" #include "libc/errno.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/promises.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/cpuid4.internal.h" #include "libc/nexgen32e/kcpuids.h" #include "libc/nexgen32e/x86feature.h" #include "libc/nexgen32e/x86info.h" #include "libc/nt/enum/startf.h" #include "libc/nt/runtime.h" #include "libc/nt/startupinfo.h" #include "libc/nt/struct/ldrdatatableentry.h" #include "libc/nt/struct/startupinfo.h" #include "libc/nt/struct/teb.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/sock/internal.h" #include "libc/sock/sock.h" #include "libc/sock/struct/pollfd.h" #include "libc/str/str.h" #include "libc/sysv/consts/auxv.h" #include "libc/sysv/consts/cap.h" #include "libc/sysv/consts/f.h" #include "libc/sysv/consts/poll.h" #include "libc/sysv/consts/pr.h" #include "libc/sysv/consts/prio.h" #include "libc/sysv/consts/rlim.h" #include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/termios.h" #include "tool/decode/lib/idname.h" #include "tool/decode/lib/x86idnames.h" STATIC_YOINK("strerror"); // for kprintf() STATIC_YOINK("strsignal"); // for kprintf() #define PRINT(FMT, ...) \ do { \ kprintf(prologue); \ kprintf(FMT "\n", ##__VA_ARGS__); \ } while (0) static const struct AuxiliaryValue { const char *fmt; long *id; const char *name; } kAuxiliaryValues[] = { {"%-14p", &AT_EXECFD, "AT_EXECFD"}, {"%-14p", &AT_PHDR, "AT_PHDR"}, {"%-14p", &AT_PHENT, "AT_PHENT"}, {"%-14p", &AT_PHNUM, "AT_PHNUM"}, {"%-14p", &AT_PAGESZ, "AT_PAGESZ"}, {"%-14p", &AT_BASE, "AT_BASE"}, {"%-14p", &AT_ENTRY, "AT_ENTRY"}, {"%-14p", &AT_NOTELF, "AT_NOTELF"}, {"%-14d", &AT_UID, "AT_UID"}, {"%-14d", &AT_EUID, "AT_EUID"}, {"%-14d", &AT_GID, "AT_GID"}, {"%-14d", &AT_EGID, "AT_EGID"}, {"%-14d", &AT_CLKTCK, "AT_CLKTCK"}, {"%-14d", &AT_OSRELDATE, "AT_OSRELDATE"}, {"%-14p", &AT_PLATFORM, "AT_PLATFORM"}, {"%-14p", &AT_DCACHEBSIZE, "AT_DCACHEBSIZE"}, {"%-14p", &AT_ICACHEBSIZE, "AT_ICACHEBSIZE"}, {"%-14p", &AT_UCACHEBSIZE, "AT_UCACHEBSIZE"}, {"%-14p", &AT_SECURE, "AT_SECURE"}, {"%-14s", &AT_BASE_PLATFORM, "AT_BASE_PLATFORM"}, {"%-14p", &AT_RANDOM, "AT_RANDOM"}, {"%-14s", &AT_EXECFN, "AT_EXECFN"}, {"%-14p", &AT_SYSINFO_EHDR, "AT_SYSINFO_EHDR"}, {"%-14p", &AT_FLAGS, "AT_FLAGS"}, {"%-14p", &AT_HWCAP, "AT_HWCAP"}, {"%-14p", &AT_HWCAP2, "AT_HWCAP2"}, {"%-14p", &AT_STACKBASE, "AT_STACKBASE"}, {"%-14p", &AT_CANARY, "AT_CANARY"}, {"%-14p", &AT_CANARYLEN, "AT_CANARYLEN"}, {"%-14ld", &AT_NCPUS, "AT_NCPUS"}, {"%-14p", &AT_PAGESIZES, "AT_PAGESIZES"}, {"%-14d", &AT_PAGESIZESLEN, "AT_PAGESIZESLEN"}, {"%-14p", &AT_TIMEKEEP, "AT_TIMEKEEP"}, {"%-14p", &AT_STACKPROT, "AT_STACKPROT"}, {"%-14p", &AT_EHDRFLAGS, "AT_EHDRFLAGS"}, {"%-14d", &AT_MINSIGSTKSZ, "AT_MINSIGSTKSZ"}, }; static const char *FindNameById(const struct IdName *names, unsigned long id) { for (; names->name; names++) { if (names->id == id) { return names->name; } } return NULL; } static const struct AuxiliaryValue *DescribeAuxv(unsigned long x) { int i; for (i = 0; i < ARRAYLEN(kAuxiliaryValues); ++i) { if (*kAuxiliaryValues[i].id && x == *kAuxiliaryValues[i].id) { return kAuxiliaryValues + i; } } return NULL; } static noasan void PrintDependencies(const char *prologue) { struct NtLinkedList *head = &NtGetPeb()->Ldr->InLoadOrderModuleList; struct NtLinkedList *ldr = head->Next; do { const struct NtLdrDataTableEntry *dll = (const struct NtLdrDataTableEntry *)ldr; PRINT(" ☼ %.*!hs (%'zukb @ %p)", dll->FullDllName.Length, dll->FullDllName.Data, dll->SizeOfImage / 1024, dll->DllBase); } while ((ldr = ldr->Next) && ldr != head); } static noasan void Print(const char *prologue) { } /** * Prints lots of information about this process, e.g. * * __printargs(""); * * This is called automatically in MODE=dbg if `--strace` is used. * * @param prologue needs to be a .rodata kprintf string */ textstartup void __printargs(const char *prologue) { long key; char **env; sigset_t ss; bool gotsome; unsigned i, n; int e, x, flags; uintptr_t *auxp; struct rlimit rlim; struct utsname uts; struct sigaction sa; struct sched_param sp; struct termios termios; struct AuxiliaryValue *auxinfo; union { char path[PATH_MAX]; struct pollfd pfds[128]; } u; if (!PLEDGED(STDIO)) return; ftrace_enabled(-1); strace_enabled(-1); e = errno; PRINT(""); PRINT("SYSTEM"); if (!uname(&uts)) { kprintf(prologue); kprintf(" %s", uts.nodename); if (*uts.sysname) { kprintf(" on %s", uts.sysname); if (*uts.release) { kprintf(" %s", uts.release); } } kprintf("\n"); } else { PRINT(" uname() failed %m"); } PRINT(""); PRINT("MICROPROCESSOR"); kprintf(prologue); #ifdef __x86_64__ kprintf(" %.*s%.*s%.*s", 4, &KCPUIDS(0H, EBX), 4, &KCPUIDS(0H, EDX), 4, &KCPUIDS(0H, ECX)); if (getx86processormodel(kX86ProcessorModelKey)) { kprintf(" %s", FindNameById(kX86MarchNames, getx86processormodel(kX86ProcessorModelKey)->march)); } if (getx86processormodel(kX86ProcessorModelKey)) { kprintf(" (%s Grade)", FindNameById(kX86GradeNames, getx86processormodel(kX86ProcessorModelKey)->grade)); } kprintf("\n"); if ((x = KCPUIDS(16H, EAX) & 0x7fff)) { kprintf(prologue); kprintf(" %dmhz %s", x, "freq"); if ((x = KCPUIDS(16H, EBX) & 0x7fff)) { kprintf(" / %dmhz %s", x, "turbo"); } if ((x = KCPUIDS(16H, ECX) & 0x7fff)) { kprintf(" / %dmhz %s", x, "bus"); } kprintf("\n"); } if (X86_HAVE(HYPERVISOR)) { unsigned eax, ebx, ecx, edx; asm("push\t%%rbx\n\t" "cpuid\n\t" "mov\t%%ebx,%1\n\t" "pop\t%%rbx" : "=a"(eax), "=rm"(ebx), "=c"(ecx), "=d"(edx) : "0"(0x40000000), "2"(0L)); PRINT(" Running inside %.4s%.4s%.4s (eax=%#x)", &ebx, &ecx, &edx, eax); } CPUID4_ITERATE(i, { PRINT(" L%d%s%s %u-way %,u byte cache w/%s " "%,u sets of %,u byte lines shared across %u threads%s", CPUID4_CACHE_LEVEL, CPUID4_CACHE_TYPE == 1 ? " data" : CPUID4_CACHE_TYPE == 2 ? " code" : "", CPUID4_IS_FULLY_ASSOCIATIVE ? " fully-associative" : "", CPUID4_WAYS_OF_ASSOCIATIVITY, CPUID4_CACHE_SIZE_IN_BYTES, CPUID4_PHYSICAL_LINE_PARTITIONS > 1 ? " physically partitioned" : "", CPUID4_NUMBER_OF_SETS, CPUID4_SYSTEM_COHERENCY_LINE_SIZE, CPUID4_MAX_THREADS_SHARING_CACHE, CPUID4_COMPLEX_INDEXING ? " complexly-indexed" : ""); }); kprintf(prologue); kprintf(" "); if (X86_HAVE(SSE3)) kprintf(" SSE3"); if (X86_HAVE(SSSE3)) kprintf(" SSSE3"); if (X86_HAVE(SSE4_2)) kprintf(" SSE4_2"); if (X86_HAVE(POPCNT)) kprintf(" POPCNT"); if (X86_HAVE(AVX)) kprintf(" AVX"); if (X86_HAVE(AVX2)) kprintf(" AVX2"); if (X86_HAVE(FMA)) kprintf(" FMA"); if (X86_HAVE(BMI)) kprintf(" BMI"); if (X86_HAVE(BMI2)) kprintf(" BMI2"); if (X86_HAVE(ADX)) kprintf(" ADX"); if (X86_HAVE(F16C)) kprintf(" F16C"); if (X86_HAVE(SHA)) kprintf(" SHA"); if (X86_HAVE(AES)) kprintf(" AES"); if (X86_HAVE(RDRND)) kprintf(" RDRND"); if (X86_HAVE(RDSEED)) kprintf(" RDSEED"); if (X86_HAVE(RDTSCP)) kprintf(" RDTSCP"); if (X86_HAVE(RDPID)) kprintf(" RDPID"); if (X86_HAVE(LA57)) kprintf(" LA57"); if (X86_HAVE(FSGSBASE)) kprintf(" FSGSBASE"); #elif defined(__aarch64__) PRINT(" AARCH64"); #endif PRINT(""); PRINT("FILE DESCRIPTORS"); for (i = 0; i < ARRAYLEN(u.pfds); ++i) { u.pfds[i].fd = i; u.pfds[i].events = POLLIN; } if ((n = poll(u.pfds, ARRAYLEN(u.pfds), 0)) != -1) { for (i = 0; i < ARRAYLEN(u.pfds); ++i) { if (i && (u.pfds[i].revents & POLLNVAL)) continue; PRINT(" ☼ %d (revents=%#hx fcntl(F_GETFL)=%#x isatty()=%hhhd)", i, u.pfds[i].revents, fcntl(i, F_GETFL), isatty(i)); } } else { PRINT(" poll() returned %d %m", n); } if (!sigprocmask(SIG_BLOCK, 0, &ss)) { PRINT(""); PRINT("SIGNAL MASK {%#lx, %#lx}", ss.__bits[0], ss.__bits[1]); if (ss.__bits[0] || ss.__bits[1]) { for (i = 0; i < 32; ++i) { if (ss.__bits[0] & (1u << i)) { PRINT(" ☼ %G (%d) is masked", i + 1, i + 1); } } } else { PRINT(" no signals blocked"); } } else { PRINT(""); PRINT("SIGNALS"); PRINT(" error: sigprocmask() failed %m"); } PRINT(""); PRINT("SIGNALS"); for (gotsome = 0, i = 1; i <= 64; ++i) { if (!sigaction(i, 0, &sa)) { if (sa.sa_handler == SIG_IGN) { PRINT(" ☼ %G is SIG_IGN", i); gotsome = 1; } else if (sa.sa_handler != SIG_DFL) { PRINT(" ☼ %G is %p", i, sa.sa_handler); gotsome = 1; } } } if (!gotsome) { PRINT(" ☼ SIG_DFL"); } if (PLEDGED(PROC)) { PRINT(""); PRINT("SCHEDULER"); errno = 0; PRINT(" ☼ getpriority(PRIO_PROCESS) → %d% m", getpriority(PRIO_PROCESS, 0)); errno = 0; PRINT(" ☼ getpriority(PRIO_PGRP) → %d% m", getpriority(PRIO_PGRP, 0)); errno = 0; PRINT(" ☼ getpriority(PRIO_USER) → %d% m", getpriority(PRIO_USER, 0)); errno = 0; PRINT(" ☼ sched_getscheduler() → %s% m", DescribeSchedPolicy(sched_getscheduler(0))); errno = 0; if (sched_getparam(0, &sp) != -1) { PRINT(" ☼ sched_getparam() → %d% m", sp.sched_priority); } } if (IsLinux()) { PRINT(""); PRINT("CAPABILITIES"); if (prctl(PR_CAPBSET_READ, 0) != -1) { for (gotsome = i = 0; i <= CAP_LAST_CAP; ++i) { if (prctl(PR_CAPBSET_READ, i) == 1) { char buf[64]; PRINT(" ☼ %s", (DescribeCapability)(buf, i)); gotsome = true; } } if (!gotsome) { PRINT(" ☼ %s", "none"); } } else { PRINT(" ☼ %s", strerror(errno)); } } PRINT(""); PRINT("RESOURCE LIMITS"); for (gotsome = i = 0; i < RLIM_NLIMITS; ++i) { if (!getrlimit(i, &rlim)) { char buf[20]; if (rlim.rlim_cur == RLIM_INFINITY) rlim.rlim_cur = -1; if (rlim.rlim_max == RLIM_INFINITY) rlim.rlim_max = -1; PRINT(" ☼ %-20s %,16ld %,16ld", (DescribeRlimitName)(buf, i), rlim.rlim_cur, rlim.rlim_max); gotsome = true; } } if (!gotsome) { PRINT(" ☼ %s", "none"); } PRINT(""); PRINT("STACK"); size_t foss_stack_size = 4ul * 1024 * 1024; PRINT(" ☼ %p __oldstack top", ROUNDUP(__oldstack + 1, foss_stack_size)); PRINT(" ☼ %p __oldstack ptr", __oldstack); PRINT(" ☼ %p __oldstack bot", ROUNDDOWN(__oldstack, foss_stack_size)); PRINT(" ☼ %p __builtin_frame_address(0)", __builtin_frame_address(0)); PRINT(""); PRINT("ARGUMENTS (%p)", __argv); if (*__argv) { for (i = 0; i < __argc; ++i) { PRINT(" ☼ %p %s", __argv[i], __argv[i]); } } else { PRINT(" none"); } PRINT(""); PRINT("ENVIRONMENT (%p)", __envp); if (*__envp) { for (env = __envp; *env; ++env) { PRINT(" ☼ %p %s", *env, *env); } } else { PRINT(" none"); } PRINT(""); PRINT("AUXILIARY (%p)", __auxv); if (*__auxv) { if (*__auxv) { for (auxp = __auxv; *auxp; auxp += 2) { if ((auxinfo = DescribeAuxv(auxp[0]))) { ksnprintf(u.path, sizeof(u.path), auxinfo->fmt, auxp[1]); PRINT(" ☼ %p %16s[%4ld] = %s", auxp, auxinfo->name, auxp[0], u.path); } else { PRINT(" ☼ %p %16s[%4ld] = %014p", auxp, "unknown", auxp[0], auxp[1]); } } } } else { PRINT(" none"); } PRINT(""); PRINT("SPECIALS"); umask((i = umask(022))); PRINT(" ☼ %s = %#o", "umask()", i); PRINT(" ☼ %s = %d", "getpid()", getpid()); PRINT(" ☼ %s = %d", "getppid()", getppid()); PRINT(" ☼ %s = %d", "getpgrp()", getpgrp()); PRINT(" ☼ %s = %d", "getsid()", getsid(0)); PRINT(" ☼ %s = %d", "getuid()", getuid()); PRINT(" ☼ %s = %d", "geteuid()", geteuid()); PRINT(" ☼ %s = %d", "getgid()", getgid()); PRINT(" ☼ %s = %d", "getegid()", getegid()); PRINT(" ☼ %s = %#s", "kTmpPath", kTmpPath); #ifdef __x86_64__ PRINT(" ☼ %s = %#s", "kNtSystemDirectory", kNtSystemDirectory); PRINT(" ☼ %s = %#s", "kNtWindowsDirectory", kNtWindowsDirectory); #endif PRINT(" ☼ %s = %#s", "GetProgramExecutableName", GetProgramExecutableName()); PRINT(" ☼ %s = %#s", "GetInterpreterExecutableName", GetInterpreterExecutableName(u.path, sizeof(u.path))); PRINT(" ☼ %s = %p", "RSP", __builtin_frame_address(0)); PRINT(" ☼ %s = %p", "GetStackAddr()", GetStackAddr()); PRINT(" ☼ %s = %p", "GetStaticStackAddr(0)", GetStaticStackAddr(0)); PRINT(" ☼ %s = %p", "GetStackSize()", GetStackSize()); PRINT(""); PRINT("MEMTRACK"); __print_maps(); PRINT(""); PRINT("TERMIOS"); for (i = 0; i <= 2; ++i) { if (!tcgetattr(i, &termios)) { struct winsize ws; if (i == 0) { PRINT(" - stdin"); } else if (i == 1) { PRINT(" - stdout"); } else { PRINT(" - stderr"); } kprintf(prologue); if (!tcgetwinsize(i, &ws)) { kprintf(" ws_row = %d\n", ws.ws_row); kprintf(" ws_col = %d\n", ws.ws_col); } kprintf(" c_iflag ="); if (termios.c_iflag & IGNBRK) kprintf(" IGNBRK"); if (termios.c_iflag & BRKINT) kprintf(" BRKINT"); if (termios.c_iflag & IGNPAR) kprintf(" IGNPAR"); if (termios.c_iflag & PARMRK) kprintf(" PARMRK"); if (termios.c_iflag & INPCK) kprintf(" INPCK"); if (termios.c_iflag & ISTRIP) kprintf(" ISTRIP"); if (termios.c_iflag & INLCR) kprintf(" INLCR"); if (termios.c_iflag & IGNCR) kprintf(" IGNCR"); if (termios.c_iflag & ICRNL) kprintf(" ICRNL"); if (termios.c_iflag & IXON) kprintf(" IXON"); if (termios.c_iflag & IXANY) kprintf(" IXANY"); if (termios.c_iflag & IXOFF) kprintf(" IXOFF"); if (termios.c_iflag & IMAXBEL) kprintf(" IMAXBEL"); if (termios.c_iflag & IUTF8) kprintf(" IUTF8"); if (termios.c_iflag & IUCLC) kprintf(" IUCLC"); kprintf("\n"); kprintf(prologue); kprintf(" c_oflag ="); if (termios.c_oflag & OPOST) kprintf(" OPOST"); if (termios.c_oflag & ONLCR) kprintf(" ONLCR"); if (termios.c_oflag & OCRNL) kprintf(" OCRNL"); if (termios.c_oflag & ONOCR) kprintf(" ONOCR"); if (termios.c_oflag & ONLRET) kprintf(" ONLRET"); if (termios.c_oflag & OFILL) kprintf(" OFILL"); if (termios.c_oflag & OFDEL) kprintf(" OFDEL"); if (termios.c_oflag & OLCUC) kprintf(" OLCUC"); if ((termios.c_oflag & NLDLY) == NL0) { kprintf(" NL0"); } else if ((termios.c_oflag & NLDLY) == NL1) { kprintf(" NL1"); } else if ((termios.c_oflag & NLDLY) == NL2) { kprintf(" NL2"); } else if ((termios.c_oflag & NLDLY) == NL3) { kprintf(" NL3"); } if ((termios.c_oflag & CRDLY) == CR0) { kprintf(" CR0"); } else if ((termios.c_oflag & CRDLY) == CR1) { kprintf(" CR1"); } else if ((termios.c_oflag & CRDLY) == CR2) { kprintf(" CR2"); } else if ((termios.c_oflag & CRDLY) == CR3) { kprintf(" CR3"); } if ((termios.c_oflag & TABDLY) == TAB0) { kprintf(" TAB0"); } else if ((termios.c_oflag & TABDLY) == TAB1) { kprintf(" TAB1"); } else if ((termios.c_oflag & TABDLY) == TAB2) { kprintf(" TAB2"); } else if ((termios.c_oflag & TABDLY) == TAB3) { kprintf(" TAB3"); } if ((termios.c_oflag & BSDLY) == BS0) { kprintf(" BS0"); } else if ((termios.c_oflag & BSDLY) == BS1) { kprintf(" BS1"); } if ((termios.c_oflag & VTDLY) == VT0) { kprintf(" VT0"); } else if ((termios.c_oflag & VTDLY) == VT1) { kprintf(" VT1"); } if ((termios.c_oflag & FFDLY) == FF0) { kprintf(" FF0"); } else if ((termios.c_oflag & FFDLY) == FF1) { kprintf(" FF1"); } kprintf("\n"); kprintf(prologue); kprintf(" c_cflag ="); if (termios.c_cflag & PARENB) kprintf(" PARENB"); if (termios.c_cflag & PARODD) kprintf(" PARODD"); if (termios.c_cflag & CSTOPB) kprintf(" CSTOPB"); if (termios.c_cflag & PARODD) kprintf(" PARODD"); if (termios.c_cflag & HUPCL) kprintf(" HUPCL"); if (termios.c_cflag & CREAD) kprintf(" CREAD"); if (termios.c_cflag & CLOCAL) kprintf(" CLOCAL"); if ((termios.c_cflag & CSIZE) == CS5) { kprintf(" CS5"); } else if ((termios.c_cflag & CSIZE) == CS6) { kprintf(" CS6"); } else if ((termios.c_cflag & CSIZE) == CS7) { kprintf(" CS7"); } else if ((termios.c_cflag & CSIZE) == CS8) { kprintf(" CS8"); } if ((termios.c_cflag & CBAUD) == B0) { kprintf(" B0"); } else if ((termios.c_cflag & CBAUD) == B50) { kprintf(" B50"); } else if ((termios.c_cflag & CBAUD) == B75) { kprintf(" B75"); } else if ((termios.c_cflag & CBAUD) == B110) { kprintf(" B110"); } else if ((termios.c_cflag & CBAUD) == B134) { kprintf(" B134"); } else if ((termios.c_cflag & CBAUD) == B150) { kprintf(" B150"); } else if ((termios.c_cflag & CBAUD) == B200) { kprintf(" B200"); } else if ((termios.c_cflag & CBAUD) == B300) { kprintf(" B300"); } else if ((termios.c_cflag & CBAUD) == B600) { kprintf(" B600"); } else if ((termios.c_cflag & CBAUD) == B1200) { kprintf(" B1200"); } else if ((termios.c_cflag & CBAUD) == B1800) { kprintf(" B1800"); } else if ((termios.c_cflag & CBAUD) == B2400) { kprintf(" B2400"); } else if ((termios.c_cflag & CBAUD) == B4800) { kprintf(" B4800"); } else if ((termios.c_cflag & CBAUD) == B9600) { kprintf(" B9600"); } else if ((termios.c_cflag & CBAUD) == B19200) { kprintf(" B19200"); } else if ((termios.c_cflag & CBAUD) == B38400) { kprintf(" B38400"); } else if ((termios.c_cflag & CBAUD) == B57600) { kprintf(" B57600"); } else if ((termios.c_cflag & CBAUD) == B115200) { kprintf(" B115200"); } else if ((termios.c_cflag & CBAUD) == B230400) { kprintf(" B230400"); } else if ((termios.c_cflag & CBAUD) == B500000) { kprintf(" B500000"); } else if ((termios.c_cflag & CBAUD) == B576000) { kprintf(" B576000"); } else if ((termios.c_cflag & CBAUD) == B1000000) { kprintf(" B1000000"); } else if ((termios.c_cflag & CBAUD) == B1152000) { kprintf(" B1152000"); } else if ((termios.c_cflag & CBAUD) == B1500000) { kprintf(" B1500000"); } else if ((termios.c_cflag & CBAUD) == B2000000) { kprintf(" B2000000"); } else if ((termios.c_cflag & CBAUD) == B2500000) { kprintf(" B2500000"); } else if ((termios.c_cflag & CBAUD) == B3000000) { kprintf(" B3000000"); } else if ((termios.c_cflag & CBAUD) == B3500000) { kprintf(" B3500000"); } else if ((termios.c_cflag & CBAUD) == B4000000) { kprintf(" B4000000"); } kprintf("\n"); kprintf(prologue); kprintf(" c_lflag ="); if (termios.c_lflag & ISIG) kprintf(" ISIG"); if (termios.c_lflag & ICANON) kprintf(" ICANON"); if (termios.c_lflag & ECHO) kprintf(" ECHO"); if (termios.c_lflag & ECHOE) kprintf(" ECHOE"); if (termios.c_lflag & ECHOK) kprintf(" ECHOK"); if (termios.c_lflag & ECHONL) kprintf(" ECHONL"); if (termios.c_lflag & NOFLSH) kprintf(" NOFLSH"); if (termios.c_lflag & TOSTOP) kprintf(" TOSTOP"); if (termios.c_lflag & IEXTEN) kprintf(" IEXTEN"); if (termios.c_lflag & ECHOCTL) kprintf(" ECHOCTL"); if (termios.c_lflag & ECHOPRT) kprintf(" ECHOPRT"); if (termios.c_lflag & ECHOKE) kprintf(" ECHOKE"); if (termios.c_lflag & FLUSHO) kprintf(" FLUSHO"); if (termios.c_lflag & PENDIN) kprintf(" PENDIN"); if (termios.c_lflag & XCASE) kprintf(" XCASE"); kprintf("\n"); PRINT(" c_ispeed = %u", cfgetispeed(&termios)); PRINT(" c_ospeed = %u", cfgetospeed(&termios)); PRINT(" c_cc[VMIN] = %d", termios.c_cc[VMIN]); PRINT(" c_cc[VTIME] = %d", termios.c_cc[VTIME]); PRINT(" c_cc[VINTR] = CTRL-%c", CTRL(termios.c_cc[VINTR])); PRINT(" c_cc[VQUIT] = CTRL-%c", CTRL(termios.c_cc[VQUIT])); PRINT(" c_cc[VERASE] = CTRL-%c", CTRL(termios.c_cc[VERASE])); PRINT(" c_cc[VKILL] = CTRL-%c", CTRL(termios.c_cc[VKILL])); PRINT(" c_cc[VEOF] = CTRL-%c", CTRL(termios.c_cc[VEOF])); PRINT(" c_cc[VSTART] = CTRL-%c", CTRL(termios.c_cc[VSTART])); PRINT(" c_cc[VSTOP] = CTRL-%c", CTRL(termios.c_cc[VSTOP])); PRINT(" c_cc[VSUSP] = CTRL-%c", CTRL(termios.c_cc[VSUSP])); PRINT(" c_cc[VEOL] = CTRL-%c", CTRL(termios.c_cc[VEOL])); PRINT(" c_cc[VSWTC] = CTRL-%c", CTRL(termios.c_cc[VSWTC])); PRINT(" c_cc[VREPRINT] = CTRL-%c", CTRL(termios.c_cc[VREPRINT])); PRINT(" c_cc[VDISCARD] = CTRL-%c", CTRL(termios.c_cc[VDISCARD])); PRINT(" c_cc[VWERASE] = CTRL-%c", CTRL(termios.c_cc[VWERASE])); PRINT(" c_cc[VLNEXT] = CTRL-%c", CTRL(termios.c_cc[VLNEXT])); PRINT(" c_cc[VEOL2] = CTRL-%c", CTRL(termios.c_cc[VEOL2])); } else { PRINT(" - tcgetattr(%d) failed %m", i); } } if (IsWindows()) { struct NtStartupInfo startinfo; GetStartupInfo(&startinfo); PRINT(""); PRINT("GETSTARTUPINFO"); if (startinfo.lpDesktop) PRINT(" ☼ %s = %#!hs", "lpDesktop", startinfo.lpDesktop); if (startinfo.lpTitle) PRINT(" ☼ %s = %#!hs", "lpTitle", startinfo.lpTitle); if (startinfo.dwX) PRINT(" ☼ %s = %u", "dwX", startinfo.dwX); if (startinfo.dwY) PRINT(" ☼ %s = %u", "dwY", startinfo.dwY); if (startinfo.dwXSize) PRINT(" ☼ %s = %u", "dwXSize", startinfo.dwXSize); if (startinfo.dwYSize) PRINT(" ☼ %s = %u", "dwYSize", startinfo.dwYSize); if (startinfo.dwXCountChars) PRINT(" ☼ %s = %u", "dwXCountChars", startinfo.dwXCountChars); if (startinfo.dwYCountChars) PRINT(" ☼ %s = %u", "dwYCountChars", startinfo.dwYCountChars); if (startinfo.dwFillAttribute) PRINT(" ☼ %s = %u", "dwFillAttribute", startinfo.dwFillAttribute); if (startinfo.dwFlags) PRINT(" ☼ %s = %s", "dwFlags", DescribeNtStartFlags(startinfo.dwFlags)); if (startinfo.wShowWindow) PRINT(" ☼ %s = %hu", "wShowWindow", startinfo.wShowWindow); if (startinfo.cbReserved2) PRINT(" ☼ %s = %hu", "cbReserved2", startinfo.cbReserved2); if (startinfo.hStdInput) PRINT(" ☼ %s = %ld", "hStdInput", startinfo.hStdInput); if (startinfo.hStdOutput) PRINT(" ☼ %s = %ld", "hStdOutput", startinfo.hStdOutput); if (startinfo.hStdError) PRINT(" ☼ %s = %ld", "hStdError", startinfo.hStdError); PRINT(""); PRINT("STANDARD HANDLES"); PRINT(" ☼ %s = %ld", "GetStdHandle(kNtStdInputHandle)", GetStdHandle(kNtStdInputHandle)); PRINT(" ☼ %s = %ld", "GetStdHandle(kNtStdOutputHandle)", GetStdHandle(kNtStdOutputHandle)); PRINT(" ☼ %s = %ld", "GetStdHandle(kNtStdErrorHandle)", GetStdHandle(kNtStdErrorHandle)); PRINT(""); PRINT("TEB"); PRINT(" ☼ gs:0x%02x %s = %p", 0x00, "NtGetSeh()", _NtGetSeh()); PRINT(" ☼ gs:0x%02x %s = %p", 0x08, "NtGetStackHigh()", _NtGetStackHigh()); PRINT(" ☼ gs:0x%02x %s = %p", 0x10, "NtGetStackLow()", _NtGetStackLow()); PRINT(" ☼ gs:0x%02x %s = %p", 0x18, "_NtGetSubsystemTib()", _NtGetSubsystemTib()); PRINT(" ☼ gs:0x%02x %s = %p", 0x20, "NtGetFib()", _NtGetFib()); PRINT(" ☼ gs:0x%02x %s = %p", 0x30, "NtGetTeb()", NtGetTeb()); PRINT(" ☼ gs:0x%02x %s = %p", 0x38, "NtGetEnv()", _NtGetEnv()); PRINT(" ☼ gs:0x%02x %s = %p", 0x40, "NtGetPid()", NtGetPid()); PRINT(" ☼ gs:0x%02x %s = %p", 0x48, "NtGetTid()", NtGetTid()); PRINT(" ☼ gs:0x%02x %s = %p", 0x50, "NtGetRpc()", _NtGetRpc()); PRINT(" ☼ gs:0x%02x %s = %p", 0x58, "NtGetTls()", _NtGetTls()); PRINT(" ☼ gs:0x%02x %s = %p", 0x60, "NtGetPeb()", NtGetPeb()); PRINT(" ☼ gs:0x%02x %s = %p", 0x68, "NtGetErr()", NtGetErr()); PRINT(""); PRINT("DEPENDENCIES"); PrintDependencies(prologue); } PRINT(""); strace_enabled(+1); ftrace_enabled(+1); errno = e; }
27,273
731
jart/cosmopolitan
false
cosmopolitan/libc/runtime/unwind.c
/*-*- 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 2022 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/runtime/unwind.h" // temporary stubs for libunwind uintptr_t _Unwind_GetCFA(struct _Unwind_Context *ctx) { return 0; } uintptr_t _Unwind_GetIP(struct _Unwind_Context *ctx) { return 0; } _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn fn, void *arg) { return _URC_NORMAL_STOP; } void *_Unwind_FindEnclosingFunction(void *arg) { return NULL; }
2,213
38
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getresourcelimit.c
/*-*- 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 2021 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/struct/rlimit.h" #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/sysv/consts/rlim.h" long _GetResourceLimit(int resource) { struct rlimit rl; getrlimit(resource, &rl); if (rl.rlim_cur == RLIM_INFINITY) return -1; return MIN(rl.rlim_cur, LONG_MAX); }
2,142
30
jart/cosmopolitan
false
cosmopolitan/libc/runtime/memtrack64.txt
# -*- conf -*- # Cosmopolitan Libc Memory Plan 00000000-0000001f 2048kb null 00000020-0000003f 2048kb loader 00000040-00004ffb 1276mb image 00005004-00007ffb 768mb arena 00007ffe-00007ffe 64kb free # address sanitizer shadow memory 00007fff-000fffff 62gb asan 00100000-001fffff 64gb asan 00200000-002fffff 64gb asan 00300000-003fffff 64gb asan 00400000-004fffff 64gb asan 00500000-005fffff 64gb asan 00600000-006fffff 64gb asan 00700000-007fffff 64gb asan 00800000-008fffff 64gb asan 00900000-009fffff 64gb asan 00a00000-00afffff 64gb asan 00b00000-00bfffff 64gb asan 00c00000-00cfffff 64gb asan 00d00000-00dfffff 64gb asan 00e00000-00efffff 64gb asan 00f00000-00ffffff 64gb asan 01000000-010fffff 64gb asan 01100000-011fffff 64gb asan 01200000-012fffff 64gb asan 01300000-013fffff 64gb asan 01400000-014fffff 64gb asan 01500000-015fffff 64gb asan 01600000-016fffff 64gb asan 01700000-017fffff 64gb asan 01800000-018fffff 64gb asan 01900000-019fffff 64gb asan 01a00000-01afffff 64gb asan 01b00000-01bfffff 64gb asan 01c00000-01cfffff 64gb asan 01d00000-01dfffff 64gb asan 01e00000-01efffff 64gb asan 01f00000-01ffffff 64gb asan 02000000-020fffff 64gb asan 02100000-021fffff 64gb asan 02200000-022fffff 64gb asan 02300000-023fffff 64gb asan 02400000-024fffff 64gb asan 02500000-025fffff 64gb asan 02600000-026fffff 64gb asan 02700000-027fffff 64gb asan 02800000-028fffff 64gb asan 02900000-029fffff 64gb asan 02a00000-02afffff 64gb asan 02b00000-02bfffff 64gb asan 02c00000-02cfffff 64gb asan 02d00000-02dfffff 64gb asan 02e00000-02efffff 64gb asan 02f00000-02ffffff 64gb asan 03000000-030fffff 64gb asan 03100000-031fffff 64gb asan 03200000-032fffff 64gb asan 03300000-033fffff 64gb asan 03400000-034fffff 64gb asan 03500000-035fffff 64gb asan 03600000-036fffff 64gb asan 03700000-037fffff 64gb asan 03800000-038fffff 64gb asan 03900000-039fffff 64gb asan 03a00000-03afffff 64gb asan 03b00000-03bfffff 64gb asan 03c00000-03cfffff 64gb asan 03d00000-03dfffff 64gb asan 03e00000-03efffff 64gb asan 03f00000-03ffffff 64gb asan 04000000-040fffff 64gb asan 04100000-041fffff 64gb asan 04200000-042fffff 64gb asan 04300000-043fffff 64gb asan 04400000-044fffff 64gb asan 04500000-045fffff 64gb asan 04600000-046fffff 64gb asan 04700000-047fffff 64gb asan 04800000-048fffff 64gb asan 04900000-049fffff 64gb asan 04a00000-04afffff 64gb asan 04b00000-04bfffff 64gb asan 04c00000-04cfffff 64gb asan 04d00000-04dfffff 64gb asan 04e00000-04efffff 64gb asan 04f00000-04ffffff 64gb asan 05000000-050fffff 64gb asan 05100000-051fffff 64gb asan 05200000-052fffff 64gb asan 05300000-053fffff 64gb asan 05400000-054fffff 64gb asan 05500000-055fffff 64gb asan 05600000-056fffff 64gb asan 05700000-057fffff 64gb asan 05800000-058fffff 64gb asan 05900000-059fffff 64gb asan 05a00000-05afffff 64gb asan 05b00000-05bfffff 64gb asan 05c00000-05cfffff 64gb asan 05d00000-05dfffff 64gb asan 05e00000-05efffff 64gb asan 05f00000-05ffffff 64gb asan 06000000-060fffff 64gb asan 06100000-061fffff 64gb asan 06200000-062fffff 64gb asan 06300000-063fffff 64gb asan 06400000-064fffff 64gb asan 06500000-065fffff 64gb asan 06600000-066fffff 64gb asan 06700000-067fffff 64gb asan 06800000-068fffff 64gb asan 06900000-069fffff 64gb asan 06a00000-06afffff 64gb asan 06b00000-06bfffff 64gb asan 06c00000-06cfffff 64gb asan 06d00000-06dfffff 64gb asan 06e00000-06efffff 64gb asan 06f00000-06ffffff 64gb asan 07000000-070fffff 64gb asan 07100000-071fffff 64gb asan 07200000-072fffff 64gb asan 07300000-073fffff 64gb asan 07400000-074fffff 64gb asan 07500000-075fffff 64gb asan 07600000-076fffff 64gb asan 07700000-077fffff 64gb asan 07800000-078fffff 64gb asan 07900000-079fffff 64gb asan 07a00000-07afffff 64gb asan 07b00000-07bfffff 64gb asan 07c00000-07cfffff 64gb asan 07d00000-07dfffff 64gb asan 07e00000-07efffff 64gb asan 07f00000-07ffffff 64gb asan 08000000-080fffff 64gb asan 08100000-081fffff 64gb asan 08200000-082fffff 64gb asan 08300000-083fffff 64gb asan 08400000-084fffff 64gb asan 08500000-085fffff 64gb asan 08600000-086fffff 64gb asan 08700000-087fffff 64gb asan 08800000-088fffff 64gb asan 08900000-089fffff 64gb asan 08a00000-08afffff 64gb asan 08b00000-08bfffff 64gb asan 08c00000-08cfffff 64gb asan 08d00000-08dfffff 64gb asan 08e00000-08efffff 64gb asan 08f00000-08ffffff 64gb asan 09000000-090fffff 64gb asan 09100000-091fffff 64gb asan 09200000-092fffff 64gb asan 09300000-093fffff 64gb asan 09400000-094fffff 64gb asan 09500000-095fffff 64gb asan 09600000-096fffff 64gb asan 09700000-097fffff 64gb asan 09800000-098fffff 64gb asan 09900000-099fffff 64gb asan 09a00000-09afffff 64gb asan 09b00000-09bfffff 64gb asan 09c00000-09cfffff 64gb asan 09d00000-09dfffff 64gb asan 09e00000-09efffff 64gb asan 09f00000-09ffffff 64gb asan 0a000000-0a0fffff 64gb asan 0a100000-0a1fffff 64gb asan 0a200000-0a2fffff 64gb asan 0a300000-0a3fffff 64gb asan 0a400000-0a4fffff 64gb asan 0a500000-0a5fffff 64gb asan 0a600000-0a6fffff 64gb asan 0a700000-0a7fffff 64gb asan 0a800000-0a8fffff 64gb asan 0a900000-0a9fffff 64gb asan 0aa00000-0aafffff 64gb asan 0ab00000-0abfffff 64gb asan 0ac00000-0acfffff 64gb asan 0ad00000-0adfffff 64gb asan 0ae00000-0aefffff 64gb asan 0af00000-0affffff 64gb asan 0b000000-0b0fffff 64gb asan 0b100000-0b1fffff 64gb asan 0b200000-0b2fffff 64gb asan 0b300000-0b3fffff 64gb asan 0b400000-0b4fffff 64gb asan 0b500000-0b5fffff 64gb asan 0b600000-0b6fffff 64gb asan 0b700000-0b7fffff 64gb asan 0b800000-0b8fffff 64gb asan 0b900000-0b9fffff 64gb asan 0ba00000-0bafffff 64gb asan 0bb00000-0bbfffff 64gb asan 0bc00000-0bcfffff 64gb asan 0bd00000-0bdfffff 64gb asan 0be00000-0befffff 64gb asan 0bf00000-0bffffff 64gb asan 0c000000-0c0fffff 64gb asan 0c100000-0c1fffff 64gb asan 0c200000-0c2fffff 64gb asan 0c300000-0c3fffff 64gb asan 0c400000-0c4fffff 64gb asan 0c500000-0c5fffff 64gb asan 0c600000-0c6fffff 64gb asan 0c700000-0c7fffff 64gb asan 0c800000-0c8fffff 64gb asan 0c900000-0c9fffff 64gb asan 0ca00000-0cafffff 64gb asan 0cb00000-0cbfffff 64gb asan 0cc00000-0ccfffff 64gb asan 0cd00000-0cdfffff 64gb asan 0ce00000-0cefffff 64gb asan 0cf00000-0cffffff 64gb asan 0d000000-0d0fffff 64gb asan 0d100000-0d1fffff 64gb asan 0d200000-0d2fffff 64gb asan 0d300000-0d3fffff 64gb asan 0d400000-0d4fffff 64gb asan 0d500000-0d5fffff 64gb asan 0d600000-0d6fffff 64gb asan 0d700000-0d7fffff 64gb asan 0d800000-0d8fffff 64gb asan 0d900000-0d9fffff 64gb asan 0da00000-0dafffff 64gb asan 0db00000-0dbfffff 64gb asan 0dc00000-0dcfffff 64gb asan 0dd00000-0ddfffff 64gb asan 0de00000-0defffff 64gb asan 0df00000-0dffffff 64gb asan 0e000000-0e0fffff 64gb asan 0e100000-0e1fffff 64gb asan 0e200000-0e2fffff 64gb asan 0e300000-0e3fffff 64gb asan 0e400000-0e4fffff 64gb asan 0e500000-0e5fffff 64gb asan 0e600000-0e6fffff 64gb asan 0e700000-0e7fffff 64gb asan 0e800000-0e8fffff 64gb asan 0e900000-0e9fffff 64gb asan 0ea00000-0eafffff 64gb asan 0eb00000-0ebfffff 64gb asan 0ec00000-0ecfffff 64gb asan 0ed00000-0edfffff 64gb asan 0ee00000-0eefffff 64gb asan 0ef00000-0effffff 64gb asan 0f000000-0f0fffff 64gb asan 0f100000-0f1fffff 64gb asan 0f200000-0f2fffff 64gb asan 0f300000-0f3fffff 64gb asan 0f400000-0f4fffff 64gb asan 0f500000-0f5fffff 64gb asan 0f600000-0f6fffff 64gb asan 0f700000-0f7fffff 64gb asan 0f800000-0f8fffff 64gb asan 0f900000-0f9fffff 64gb asan 0fa00000-0fafffff 64gb asan 0fb00000-0fbfffff 64gb asan 0fc00000-0fcfffff 64gb asan 0fd00000-0fdfffff 64gb asan 0fe00000-0fefffff 64gb asan 0ff00000-0fffffff 64gb asan 10000000-10008003 2048mb asan # memory dedicated to mmap(NULL, ...) automation, e.g. malloc() 10008004-100fffff 62gb automap 10100000-101fffff 64gb automap 10200000-102fffff 64gb automap 10300000-103fffff 64gb automap 10400000-104fffff 64gb automap 10500000-105fffff 64gb automap 10600000-106fffff 64gb automap 10700000-107fffff 64gb automap 10800000-108fffff 64gb automap 10900000-109fffff 64gb automap 10a00000-10afffff 64gb automap 10b00000-10bfffff 64gb automap 10c00000-10cfffff 64gb automap 10d00000-10dfffff 64gb automap 10e00000-10efffff 64gb automap 10f00000-10ffffff 64gb automap 11000000-110fffff 64gb automap 11100000-111fffff 64gb automap 11200000-112fffff 64gb automap 11300000-113fffff 64gb automap 11400000-114fffff 64gb automap 11500000-115fffff 64gb automap 11600000-116fffff 64gb automap 11700000-117fffff 64gb automap 11800000-118fffff 64gb automap 11900000-119fffff 64gb automap 11a00000-11afffff 64gb automap 11b00000-11bfffff 64gb automap 11c00000-11cfffff 64gb automap 11d00000-11dfffff 64gb automap 11e00000-11efffff 64gb automap 11f00000-11ffffff 64gb automap 12000000-120fffff 64gb automap 12100000-121fffff 64gb automap 12200000-122fffff 64gb automap 12300000-123fffff 64gb automap 12400000-124fffff 64gb automap 12500000-125fffff 64gb automap 12600000-126fffff 64gb automap 12700000-127fffff 64gb automap 12800000-128fffff 64gb automap 12900000-129fffff 64gb automap 12a00000-12afffff 64gb automap 12b00000-12bfffff 64gb automap 12c00000-12cfffff 64gb automap 12d00000-12dfffff 64gb automap 12e00000-12efffff 64gb automap 12f00000-12ffffff 64gb automap 13000000-130fffff 64gb automap 13100000-131fffff 64gb automap 13200000-132fffff 64gb automap 13300000-133fffff 64gb automap 13400000-134fffff 64gb automap 13500000-135fffff 64gb automap 13600000-136fffff 64gb automap 13700000-137fffff 64gb automap 13800000-138fffff 64gb automap 13900000-139fffff 64gb automap 13a00000-13afffff 64gb automap 13b00000-13bfffff 64gb automap 13c00000-13cfffff 64gb automap 13d00000-13dfffff 64gb automap 13e00000-13efffff 64gb automap 13f00000-13ffffff 64gb automap 14000000-140fffff 64gb automap 14100000-141fffff 64gb automap 14200000-142fffff 64gb automap 14300000-143fffff 64gb automap 14400000-144fffff 64gb automap 14500000-145fffff 64gb automap 14600000-146fffff 64gb automap 14700000-147fffff 64gb automap 14800000-148fffff 64gb automap 14900000-149fffff 64gb automap 14a00000-14afffff 64gb automap 14b00000-14bfffff 64gb automap 14c00000-14cfffff 64gb automap 14d00000-14dfffff 64gb automap 14e00000-14efffff 64gb automap 14f00000-14ffffff 64gb automap 15000000-150fffff 64gb automap 15100000-151fffff 64gb automap 15200000-152fffff 64gb automap 15300000-153fffff 64gb automap 15400000-154fffff 64gb automap 15500000-155fffff 64gb automap 15600000-156fffff 64gb automap 15700000-157fffff 64gb automap 15800000-158fffff 64gb automap 15900000-159fffff 64gb automap 15a00000-15afffff 64gb automap 15b00000-15bfffff 64gb automap 15c00000-15cfffff 64gb automap 15d00000-15dfffff 64gb automap 15e00000-15efffff 64gb automap 15f00000-15ffffff 64gb automap 16000000-160fffff 64gb automap 16100000-161fffff 64gb automap 16200000-162fffff 64gb automap 16300000-163fffff 64gb automap 16400000-164fffff 64gb automap 16500000-165fffff 64gb automap 16600000-166fffff 64gb automap 16700000-167fffff 64gb automap 16800000-168fffff 64gb automap 16900000-169fffff 64gb automap 16a00000-16afffff 64gb automap 16b00000-16bfffff 64gb automap 16c00000-16cfffff 64gb automap 16d00000-16dfffff 64gb automap 16e00000-16efffff 64gb automap 16f00000-16ffffff 64gb automap 17000000-170fffff 64gb automap 17100000-171fffff 64gb automap 17200000-172fffff 64gb automap 17300000-173fffff 64gb automap 17400000-174fffff 64gb automap 17500000-175fffff 64gb automap 17600000-176fffff 64gb automap 17700000-177fffff 64gb automap 17800000-178fffff 64gb automap 17900000-179fffff 64gb automap 17a00000-17afffff 64gb automap 17b00000-17bfffff 64gb automap 17c00000-17cfffff 64gb automap 17d00000-17dfffff 64gb automap 17e00000-17efffff 64gb automap 17f00000-17ffffff 64gb automap 18000000-180fffff 64gb automap 18100000-181fffff 64gb automap 18200000-182fffff 64gb automap 18300000-183fffff 64gb automap 18400000-184fffff 64gb automap 18500000-185fffff 64gb automap 18600000-186fffff 64gb automap 18700000-187fffff 64gb automap 18800000-188fffff 64gb automap 18900000-189fffff 64gb automap 18a00000-18afffff 64gb automap 18b00000-18bfffff 64gb automap 18c00000-18cfffff 64gb automap 18d00000-18dfffff 64gb automap 18e00000-18efffff 64gb automap 18f00000-18ffffff 64gb automap 19000000-190fffff 64gb automap 19100000-191fffff 64gb automap 19200000-192fffff 64gb automap 19300000-193fffff 64gb automap 19400000-194fffff 64gb automap 19500000-195fffff 64gb automap 19600000-196fffff 64gb automap 19700000-197fffff 64gb automap 19800000-198fffff 64gb automap 19900000-199fffff 64gb automap 19a00000-19afffff 64gb automap 19b00000-19bfffff 64gb automap 19c00000-19cfffff 64gb automap 19d00000-19dfffff 64gb automap 19e00000-19efffff 64gb automap 19f00000-19ffffff 64gb automap 1a000000-1a0fffff 64gb automap 1a100000-1a1fffff 64gb automap 1a200000-1a2fffff 64gb automap 1a300000-1a3fffff 64gb automap 1a400000-1a4fffff 64gb automap 1a500000-1a5fffff 64gb automap 1a600000-1a6fffff 64gb automap 1a700000-1a7fffff 64gb automap 1a800000-1a8fffff 64gb automap 1a900000-1a9fffff 64gb automap 1aa00000-1aafffff 64gb automap 1ab00000-1abfffff 64gb automap 1ac00000-1acfffff 64gb automap 1ad00000-1adfffff 64gb automap 1ae00000-1aefffff 64gb automap 1af00000-1affffff 64gb automap 1b000000-1b0fffff 64gb automap 1b100000-1b1fffff 64gb automap 1b200000-1b2fffff 64gb automap 1b300000-1b3fffff 64gb automap 1b400000-1b4fffff 64gb automap 1b500000-1b5fffff 64gb automap 1b600000-1b6fffff 64gb automap 1b700000-1b7fffff 64gb automap 1b800000-1b8fffff 64gb automap 1b900000-1b9fffff 64gb automap 1ba00000-1bafffff 64gb automap 1bb00000-1bbfffff 64gb automap 1bc00000-1bcfffff 64gb automap 1bd00000-1bdfffff 64gb automap 1be00000-1befffff 64gb automap 1bf00000-1bffffff 64gb automap 1c000000-1c0fffff 64gb automap 1c100000-1c1fffff 64gb automap 1c200000-1c2fffff 64gb automap 1c300000-1c3fffff 64gb automap 1c400000-1c4fffff 64gb automap 1c500000-1c5fffff 64gb automap 1c600000-1c6fffff 64gb automap 1c700000-1c7fffff 64gb automap 1c800000-1c8fffff 64gb automap 1c900000-1c9fffff 64gb automap 1ca00000-1cafffff 64gb automap 1cb00000-1cbfffff 64gb automap 1cc00000-1ccfffff 64gb automap 1cd00000-1cdfffff 64gb automap 1ce00000-1cefffff 64gb automap 1cf00000-1cffffff 64gb automap 1d000000-1d0fffff 64gb automap 1d100000-1d1fffff 64gb automap 1d200000-1d2fffff 64gb automap 1d300000-1d3fffff 64gb automap 1d400000-1d4fffff 64gb automap 1d500000-1d5fffff 64gb automap 1d600000-1d6fffff 64gb automap 1d700000-1d7fffff 64gb automap 1d800000-1d8fffff 64gb automap 1d900000-1d9fffff 64gb automap 1da00000-1dafffff 64gb automap 1db00000-1dbfffff 64gb automap 1dc00000-1dcfffff 64gb automap 1dd00000-1ddfffff 64gb automap 1de00000-1defffff 64gb automap 1df00000-1dffffff 64gb automap 1e000000-1e0fffff 64gb automap 1e100000-1e1fffff 64gb automap 1e200000-1e2fffff 64gb automap 1e300000-1e3fffff 64gb automap 1e400000-1e4fffff 64gb automap 1e500000-1e5fffff 64gb automap 1e600000-1e6fffff 64gb automap 1e700000-1e7fffff 64gb automap 1e800000-1e8fffff 64gb automap 1e900000-1e9fffff 64gb automap 1ea00000-1eafffff 64gb automap 1eb00000-1ebfffff 64gb automap 1ec00000-1ecfffff 64gb automap 1ed00000-1edfffff 64gb automap 1ee00000-1eefffff 64gb automap 1ef00000-1effffff 64gb automap 1f000000-1f0fffff 64gb automap 1f100000-1f1fffff 64gb automap 1f200000-1f2fffff 64gb automap 1f300000-1f3fffff 64gb automap 1f400000-1f4fffff 64gb automap 1f500000-1f5fffff 64gb automap 1f600000-1f6fffff 64gb automap 1f700000-1f7fffff 64gb automap 1f800000-1f8fffff 64gb automap 1f900000-1f9fffff 64gb automap 1fa00000-1fafffff 64gb automap 1fb00000-1fbfffff 64gb automap 1fc00000-1fcfffff 64gb automap 1fd00000-1fdfffff 64gb automap 1fe00000-1fe7fffb 32gb automap 1fe7fffc-1fefffff 32gb _mmi 1ff00000-1ffffffb 64gb _mmi 1ffffffc-20000003 256kb free 20000004-200fffff 64gb free 20100000-201fffff 64gb free 20200000-202fffff 64gb free 20300000-203fffff 64gb free 20400000-204fffff 64gb free 20500000-205fffff 64gb free 20600000-206fffff 64gb free 20700000-207fffff 64gb free 20800000-208fffff 64gb free 20900000-209fffff 64gb free 20a00000-20afffff 64gb free 20b00000-20bfffff 64gb free 20c00000-20cfffff 64gb free 20d00000-20dfffff 64gb free 20e00000-20efffff 64gb free 20f00000-20ffffff 64gb free 21000000-210fffff 64gb free 21100000-211fffff 64gb free 21200000-212fffff 64gb free 21300000-213fffff 64gb free 21400000-214fffff 64gb free 21500000-215fffff 64gb free 21600000-216fffff 64gb free 21700000-217fffff 64gb free 21800000-218fffff 64gb free 21900000-219fffff 64gb free 21a00000-21afffff 64gb free 21b00000-21bfffff 64gb free 21c00000-21cfffff 64gb free 21d00000-21dfffff 64gb free 21e00000-21efffff 64gb free 21f00000-21ffffff 64gb free 22000000-220fffff 64gb free 22100000-221fffff 64gb free 22200000-222fffff 64gb free 22300000-223fffff 64gb free 22400000-224fffff 64gb free 22500000-225fffff 64gb free 22600000-226fffff 64gb free 22700000-227fffff 64gb free 22800000-228fffff 64gb free 22900000-229fffff 64gb free 22a00000-22afffff 64gb free 22b00000-22bfffff 64gb free 22c00000-22cfffff 64gb free 22d00000-22dfffff 64gb free 22e00000-22efffff 64gb free 22f00000-22ffffff 64gb free 23000000-230fffff 64gb free 23100000-231fffff 64gb free 23200000-232fffff 64gb free 23300000-233fffff 64gb free 23400000-234fffff 64gb free 23500000-235fffff 64gb free 23600000-236fffff 64gb free 23700000-237fffff 64gb free 23800000-238fffff 64gb free 23900000-239fffff 64gb free 23a00000-23afffff 64gb free 23b00000-23bfffff 64gb free 23c00000-23cfffff 64gb free 23d00000-23dfffff 64gb free 23e00000-23efffff 64gb free 23f00000-23ffffff 64gb free 24000000-240fffff 64gb free 24100000-241fffff 64gb free 24200000-242fffff 64gb free 24300000-243fffff 64gb free 24400000-244fffff 64gb free 24500000-245fffff 64gb free 24600000-246fffff 64gb free 24700000-247fffff 64gb free 24800000-248fffff 64gb free 24900000-249fffff 64gb free 24a00000-24afffff 64gb free 24b00000-24bfffff 64gb free 24c00000-24cfffff 64gb free 24d00000-24dfffff 64gb free 24e00000-24efffff 64gb free 24f00000-24ffffff 64gb free 25000000-250fffff 64gb free 25100000-251fffff 64gb free 25200000-252fffff 64gb free 25300000-253fffff 64gb free 25400000-254fffff 64gb free 25500000-255fffff 64gb free 25600000-256fffff 64gb free 25700000-257fffff 64gb free 25800000-258fffff 64gb free 25900000-259fffff 64gb free 25a00000-25afffff 64gb free 25b00000-25bfffff 64gb free 25c00000-25cfffff 64gb free 25d00000-25dfffff 64gb free 25e00000-25efffff 64gb free 25f00000-25ffffff 64gb free 26000000-260fffff 64gb free 26100000-261fffff 64gb free 26200000-262fffff 64gb free 26300000-263fffff 64gb free 26400000-264fffff 64gb free 26500000-265fffff 64gb free 26600000-266fffff 64gb free 26700000-267fffff 64gb free 26800000-268fffff 64gb free 26900000-269fffff 64gb free 26a00000-26afffff 64gb free 26b00000-26bfffff 64gb free 26c00000-26cfffff 64gb free 26d00000-26dfffff 64gb free 26e00000-26efffff 64gb free 26f00000-26ffffff 64gb free 27000000-270fffff 64gb free 27100000-271fffff 64gb free 27200000-272fffff 64gb free 27300000-273fffff 64gb free 27400000-274fffff 64gb free 27500000-275fffff 64gb free 27600000-276fffff 64gb free 27700000-277fffff 64gb free 27800000-278fffff 64gb free 27900000-279fffff 64gb free 27a00000-27afffff 64gb free 27b00000-27bfffff 64gb free 27c00000-27cfffff 64gb free 27d00000-27dfffff 64gb free 27e00000-27efffff 64gb free 27f00000-27ffffff 64gb free 28000000-280fffff 64gb free 28100000-281fffff 64gb free 28200000-282fffff 64gb free 28300000-283fffff 64gb free 28400000-284fffff 64gb free 28500000-285fffff 64gb free 28600000-286fffff 64gb free 28700000-287fffff 64gb free 28800000-288fffff 64gb free 28900000-289fffff 64gb free 28a00000-28afffff 64gb free 28b00000-28bfffff 64gb free 28c00000-28cfffff 64gb free 28d00000-28dfffff 64gb free 28e00000-28efffff 64gb free 28f00000-28ffffff 64gb free 29000000-290fffff 64gb free 29100000-291fffff 64gb free 29200000-292fffff 64gb free 29300000-293fffff 64gb free 29400000-294fffff 64gb free 29500000-295fffff 64gb free 29600000-296fffff 64gb free 29700000-297fffff 64gb free 29800000-298fffff 64gb free 29900000-299fffff 64gb free 29a00000-29afffff 64gb free 29b00000-29bfffff 64gb free 29c00000-29cfffff 64gb free 29d00000-29dfffff 64gb free 29e00000-29efffff 64gb free 29f00000-29ffffff 64gb free 2a000000-2a0fffff 64gb free 2a100000-2a1fffff 64gb free 2a200000-2a2fffff 64gb free 2a300000-2a3fffff 64gb free 2a400000-2a4fffff 64gb free 2a500000-2a5fffff 64gb free 2a600000-2a6fffff 64gb free 2a700000-2a7fffff 64gb free 2a800000-2a8fffff 64gb free 2a900000-2a9fffff 64gb free 2aa00000-2aafffff 64gb free 2ab00000-2abfffff 64gb free 2ac00000-2acfffff 64gb free 2ad00000-2adfffff 64gb free 2ae00000-2aefffff 64gb free 2af00000-2affffff 64gb free 2b000000-2b0fffff 64gb free 2b100000-2b1fffff 64gb free 2b200000-2b2fffff 64gb free 2b300000-2b3fffff 64gb free 2b400000-2b4fffff 64gb free 2b500000-2b5fffff 64gb free 2b600000-2b6fffff 64gb free 2b700000-2b7fffff 64gb free 2b800000-2b8fffff 64gb free 2b900000-2b9fffff 64gb free 2ba00000-2bafffff 64gb free 2bb00000-2bbfffff 64gb free 2bc00000-2bcfffff 64gb free 2bd00000-2bdfffff 64gb free 2be00000-2befffff 64gb free 2bf00000-2bffffff 64gb free 2c000000-2c0fffff 64gb free 2c100000-2c1fffff 64gb free 2c200000-2c2fffff 64gb free 2c300000-2c3fffff 64gb free 2c400000-2c4fffff 64gb free 2c500000-2c5fffff 64gb free 2c600000-2c6fffff 64gb free 2c700000-2c7fffff 64gb free 2c800000-2c8fffff 64gb free 2c900000-2c9fffff 64gb free 2ca00000-2cafffff 64gb free 2cb00000-2cbfffff 64gb free 2cc00000-2ccfffff 64gb free 2cd00000-2cdfffff 64gb free 2ce00000-2cefffff 64gb free 2cf00000-2cffffff 64gb free 2d000000-2d0fffff 64gb free 2d100000-2d1fffff 64gb free 2d200000-2d2fffff 64gb free 2d300000-2d3fffff 64gb free 2d400000-2d4fffff 64gb free 2d500000-2d5fffff 64gb free 2d600000-2d6fffff 64gb free 2d700000-2d7fffff 64gb free 2d800000-2d8fffff 64gb free 2d900000-2d9fffff 64gb free 2da00000-2dafffff 64gb free 2db00000-2dbfffff 64gb free 2dc00000-2dcfffff 64gb free 2dd00000-2ddfffff 64gb free 2de00000-2defffff 64gb free 2df00000-2dffffff 64gb free 2e000000-2e0fffff 64gb free 2e100000-2e1fffff 64gb free 2e200000-2e2fffff 64gb free 2e300000-2e3fffff 64gb free 2e400000-2e4fffff 64gb free 2e500000-2e5fffff 64gb free 2e600000-2e6fffff 64gb free 2e700000-2e7fffff 64gb free 2e800000-2e8fffff 64gb free 2e900000-2e9fffff 64gb free 2ea00000-2eafffff 64gb free 2eb00000-2ebfffff 64gb free 2ec00000-2ecfffff 64gb free 2ed00000-2edfffff 64gb free 2ee00000-2eefffff 64gb free 2ef00000-2effffff 64gb free 2f000000-2f0fffff 64gb free 2f100000-2f1fffff 64gb free 2f200000-2f2fffff 64gb free 2f300000-2f3fffff 64gb free 2f400000-2f4fffff 64gb free 2f500000-2f5fffff 64gb free 2f600000-2f6fffff 64gb free 2f700000-2f7fffff 64gb free 2f800000-2f8fffff 64gb free 2f900000-2f9fffff 64gb free 2fa00000-2fafffff 64gb free 2fb00000-2fbfffff 64gb free 2fc00000-2fcfffff 64gb free 2fd00000-2fdfffff 64gb free 2fe00000-2fefffff 64gb free 2ff00000-30000003 64gb free # memory recommended for application MAP_FIXED usage 30000004-300fffff 64gb fixedmap 30100000-301fffff 64gb fixedmap 30200000-302fffff 64gb fixedmap 30300000-303fffff 64gb fixedmap 30400000-304fffff 64gb fixedmap 30500000-305fffff 64gb fixedmap 30600000-306fffff 64gb fixedmap 30700000-307fffff 64gb fixedmap 30800000-308fffff 64gb fixedmap 30900000-309fffff 64gb fixedmap 30a00000-30afffff 64gb fixedmap 30b00000-30bfffff 64gb fixedmap 30c00000-30cfffff 64gb fixedmap 30d00000-30dfffff 64gb fixedmap 30e00000-30efffff 64gb fixedmap 30f00000-30ffffff 64gb fixedmap 31000000-310fffff 64gb fixedmap 31100000-311fffff 64gb fixedmap 31200000-312fffff 64gb fixedmap 31300000-313fffff 64gb fixedmap 31400000-314fffff 64gb fixedmap 31500000-315fffff 64gb fixedmap 31600000-316fffff 64gb fixedmap 31700000-317fffff 64gb fixedmap 31800000-318fffff 64gb fixedmap 31900000-319fffff 64gb fixedmap 31a00000-31afffff 64gb fixedmap 31b00000-31bfffff 64gb fixedmap 31c00000-31cfffff 64gb fixedmap 31d00000-31dfffff 64gb fixedmap 31e00000-31efffff 64gb fixedmap 31f00000-31ffffff 64gb fixedmap 32000000-320fffff 64gb fixedmap 32100000-321fffff 64gb fixedmap 32200000-322fffff 64gb fixedmap 32300000-323fffff 64gb fixedmap 32400000-324fffff 64gb fixedmap 32500000-325fffff 64gb fixedmap 32600000-326fffff 64gb fixedmap 32700000-327fffff 64gb fixedmap 32800000-328fffff 64gb fixedmap 32900000-329fffff 64gb fixedmap 32a00000-32afffff 64gb fixedmap 32b00000-32bfffff 64gb fixedmap 32c00000-32cfffff 64gb fixedmap 32d00000-32dfffff 64gb fixedmap 32e00000-32efffff 64gb fixedmap 32f00000-32ffffff 64gb fixedmap 33000000-330fffff 64gb fixedmap 33100000-331fffff 64gb fixedmap 33200000-332fffff 64gb fixedmap 33300000-333fffff 64gb fixedmap 33400000-334fffff 64gb fixedmap 33500000-335fffff 64gb fixedmap 33600000-336fffff 64gb fixedmap 33700000-337fffff 64gb fixedmap 33800000-338fffff 64gb fixedmap 33900000-339fffff 64gb fixedmap 33a00000-33afffff 64gb fixedmap 33b00000-33bfffff 64gb fixedmap 33c00000-33cfffff 64gb fixedmap 33d00000-33dfffff 64gb fixedmap 33e00000-33efffff 64gb fixedmap 33f00000-33ffffff 64gb fixedmap 34000000-340fffff 64gb fixedmap 34100000-341fffff 64gb fixedmap 34200000-342fffff 64gb fixedmap 34300000-343fffff 64gb fixedmap 34400000-344fffff 64gb fixedmap 34500000-345fffff 64gb fixedmap 34600000-346fffff 64gb fixedmap 34700000-347fffff 64gb fixedmap 34800000-348fffff 64gb fixedmap 34900000-349fffff 64gb fixedmap 34a00000-34afffff 64gb fixedmap 34b00000-34bfffff 64gb fixedmap 34c00000-34cfffff 64gb fixedmap 34d00000-34dfffff 64gb fixedmap 34e00000-34efffff 64gb fixedmap 34f00000-34ffffff 64gb fixedmap 35000000-350fffff 64gb fixedmap 35100000-351fffff 64gb fixedmap 35200000-352fffff 64gb fixedmap 35300000-353fffff 64gb fixedmap 35400000-354fffff 64gb fixedmap 35500000-355fffff 64gb fixedmap 35600000-356fffff 64gb fixedmap 35700000-357fffff 64gb fixedmap 35800000-358fffff 64gb fixedmap 35900000-359fffff 64gb fixedmap 35a00000-35afffff 64gb fixedmap 35b00000-35bfffff 64gb fixedmap 35c00000-35cfffff 64gb fixedmap 35d00000-35dfffff 64gb fixedmap 35e00000-35efffff 64gb fixedmap 35f00000-35ffffff 64gb fixedmap 36000000-360fffff 64gb fixedmap 36100000-361fffff 64gb fixedmap 36200000-362fffff 64gb fixedmap 36300000-363fffff 64gb fixedmap 36400000-364fffff 64gb fixedmap 36500000-365fffff 64gb fixedmap 36600000-366fffff 64gb fixedmap 36700000-367fffff 64gb fixedmap 36800000-368fffff 64gb fixedmap 36900000-369fffff 64gb fixedmap 36a00000-36afffff 64gb fixedmap 36b00000-36bfffff 64gb fixedmap 36c00000-36cfffff 64gb fixedmap 36d00000-36dfffff 64gb fixedmap 36e00000-36efffff 64gb fixedmap 36f00000-36ffffff 64gb fixedmap 37000000-370fffff 64gb fixedmap 37100000-371fffff 64gb fixedmap 37200000-372fffff 64gb fixedmap 37300000-373fffff 64gb fixedmap 37400000-374fffff 64gb fixedmap 37500000-375fffff 64gb fixedmap 37600000-376fffff 64gb fixedmap 37700000-377fffff 64gb fixedmap 37800000-378fffff 64gb fixedmap 37900000-379fffff 64gb fixedmap 37a00000-37afffff 64gb fixedmap 37b00000-37bfffff 64gb fixedmap 37c00000-37cfffff 64gb fixedmap 37d00000-37dfffff 64gb fixedmap 37e00000-37efffff 64gb fixedmap 37f00000-37ffffff 64gb fixedmap 38000000-380fffff 64gb fixedmap 38100000-381fffff 64gb fixedmap 38200000-382fffff 64gb fixedmap 38300000-383fffff 64gb fixedmap 38400000-384fffff 64gb fixedmap 38500000-385fffff 64gb fixedmap 38600000-386fffff 64gb fixedmap 38700000-387fffff 64gb fixedmap 38800000-388fffff 64gb fixedmap 38900000-389fffff 64gb fixedmap 38a00000-38afffff 64gb fixedmap 38b00000-38bfffff 64gb fixedmap 38c00000-38cfffff 64gb fixedmap 38d00000-38dfffff 64gb fixedmap 38e00000-38efffff 64gb fixedmap 38f00000-38ffffff 64gb fixedmap 39000000-390fffff 64gb fixedmap 39100000-391fffff 64gb fixedmap 39200000-392fffff 64gb fixedmap 39300000-393fffff 64gb fixedmap 39400000-394fffff 64gb fixedmap 39500000-395fffff 64gb fixedmap 39600000-396fffff 64gb fixedmap 39700000-397fffff 64gb fixedmap 39800000-398fffff 64gb fixedmap 39900000-399fffff 64gb fixedmap 39a00000-39afffff 64gb fixedmap 39b00000-39bfffff 64gb fixedmap 39c00000-39cfffff 64gb fixedmap 39d00000-39dfffff 64gb fixedmap 39e00000-39efffff 64gb fixedmap 39f00000-39ffffff 64gb fixedmap 3a000000-3a0fffff 64gb fixedmap 3a100000-3a1fffff 64gb fixedmap 3a200000-3a2fffff 64gb fixedmap 3a300000-3a3fffff 64gb fixedmap 3a400000-3a4fffff 64gb fixedmap 3a500000-3a5fffff 64gb fixedmap 3a600000-3a6fffff 64gb fixedmap 3a700000-3a7fffff 64gb fixedmap 3a800000-3a8fffff 64gb fixedmap 3a900000-3a9fffff 64gb fixedmap 3aa00000-3aafffff 64gb fixedmap 3ab00000-3abfffff 64gb fixedmap 3ac00000-3acfffff 64gb fixedmap 3ad00000-3adfffff 64gb fixedmap 3ae00000-3aefffff 64gb fixedmap 3af00000-3affffff 64gb fixedmap 3b000000-3b0fffff 64gb fixedmap 3b100000-3b1fffff 64gb fixedmap 3b200000-3b2fffff 64gb fixedmap 3b300000-3b3fffff 64gb fixedmap 3b400000-3b4fffff 64gb fixedmap 3b500000-3b5fffff 64gb fixedmap 3b600000-3b6fffff 64gb fixedmap 3b700000-3b7fffff 64gb fixedmap 3b800000-3b8fffff 64gb fixedmap 3b900000-3b9fffff 64gb fixedmap 3ba00000-3bafffff 64gb fixedmap 3bb00000-3bbfffff 64gb fixedmap 3bc00000-3bcfffff 64gb fixedmap 3bd00000-3bdfffff 64gb fixedmap 3be00000-3befffff 64gb fixedmap 3bf00000-3bffffff 64gb fixedmap 3c000000-3c0fffff 64gb fixedmap 3c100000-3c1fffff 64gb fixedmap 3c200000-3c2fffff 64gb fixedmap 3c300000-3c3fffff 64gb fixedmap 3c400000-3c4fffff 64gb fixedmap 3c500000-3c5fffff 64gb fixedmap 3c600000-3c6fffff 64gb fixedmap 3c700000-3c7fffff 64gb fixedmap 3c800000-3c8fffff 64gb fixedmap 3c900000-3c9fffff 64gb fixedmap 3ca00000-3cafffff 64gb fixedmap 3cb00000-3cbfffff 64gb fixedmap 3cc00000-3ccfffff 64gb fixedmap 3cd00000-3cdfffff 64gb fixedmap 3ce00000-3cefffff 64gb fixedmap 3cf00000-3cffffff 64gb fixedmap 3d000000-3d0fffff 64gb fixedmap 3d100000-3d1fffff 64gb fixedmap 3d200000-3d2fffff 64gb fixedmap 3d300000-3d3fffff 64gb fixedmap 3d400000-3d4fffff 64gb fixedmap 3d500000-3d5fffff 64gb fixedmap 3d600000-3d6fffff 64gb fixedmap 3d700000-3d7fffff 64gb fixedmap 3d800000-3d8fffff 64gb fixedmap 3d900000-3d9fffff 64gb fixedmap 3da00000-3dafffff 64gb fixedmap 3db00000-3dbfffff 64gb fixedmap 3dc00000-3dcfffff 64gb fixedmap 3dd00000-3ddfffff 64gb fixedmap 3de00000-3defffff 64gb fixedmap 3df00000-3dffffff 64gb fixedmap 3e000000-3e0fffff 64gb fixedmap 3e100000-3e1fffff 64gb fixedmap 3e200000-3e2fffff 64gb fixedmap 3e300000-3e3fffff 64gb fixedmap 3e400000-3e4fffff 64gb fixedmap 3e500000-3e5fffff 64gb fixedmap 3e600000-3e6fffff 64gb fixedmap 3e700000-3e7fffff 64gb fixedmap 3e800000-3e8fffff 64gb fixedmap 3e900000-3e9fffff 64gb fixedmap 3ea00000-3eafffff 64gb fixedmap 3eb00000-3ebfffff 64gb fixedmap 3ec00000-3ecfffff 64gb fixedmap 3ed00000-3edfffff 64gb fixedmap 3ee00000-3eefffff 64gb fixedmap 3ef00000-3effffff 64gb fixedmap 3f000000-3f0fffff 64gb fixedmap 3f100000-3f1fffff 64gb fixedmap 3f200000-3f2fffff 64gb fixedmap 3f300000-3f3fffff 64gb fixedmap 3f400000-3f4fffff 64gb fixedmap 3f500000-3f5fffff 64gb fixedmap 3f600000-3f6fffff 64gb fixedmap 3f700000-3f7fffff 64gb fixedmap 3f800000-3f8fffff 64gb fixedmap 3f900000-3f9fffff 64gb fixedmap 3fa00000-3fafffff 64gb fixedmap 3fb00000-3fbfffff 64gb fixedmap 3fc00000-3fcfffff 64gb fixedmap 3fd00000-3fdfffff 64gb fixedmap 3fe00000-3fefffff 64gb fixedmap 3ff00000-40000003 64gb fixedmap 40000004-400fffff 64gb free 40100000-401fffff 64gb free 40200000-402fffff 64gb free 40300000-403fffff 64gb free 40400000-404fffff 64gb free 40500000-405fffff 64gb free 40600000-406fffff 64gb free 40700000-407fffff 64gb free 40800000-408fffff 64gb free 40900000-409fffff 64gb free 40a00000-40afffff 64gb free 40b00000-40bfffff 64gb free 40c00000-40cfffff 64gb free 40d00000-40dfffff 64gb free 40e00000-40efffff 64gb free 40f00000-40ffffff 64gb free 41000000-410fffff 64gb free 41100000-411fffff 64gb free 41200000-412fffff 64gb free 41300000-413fffff 64gb free 41400000-414fffff 64gb free 41500000-415fffff 64gb free 41600000-416fffff 64gb free 41700000-417fffff 64gb free 41800000-418fffff 64gb free 41900000-419fffff 64gb free 41a00000-41afffff 64gb free 41b00000-41bfffff 64gb free 41c00000-41cfffff 64gb free 41d00000-41dfffff 64gb free 41e00000-41efffff 64gb free 41f00000-41ffffff 64gb free 42000000-420fffff 64gb free 42100000-421fffff 64gb free 42200000-422fffff 64gb free 42300000-423fffff 64gb free 42400000-424fffff 64gb free 42500000-425fffff 64gb free 42600000-426fffff 64gb free 42700000-427fffff 64gb free 42800000-428fffff 64gb free 42900000-429fffff 64gb free 42a00000-42afffff 64gb free 42b00000-42bfffff 64gb free 42c00000-42cfffff 64gb free 42d00000-42dfffff 64gb free 42e00000-42efffff 64gb free 42f00000-42ffffff 64gb free 43000000-430fffff 64gb free 43100000-431fffff 64gb free 43200000-432fffff 64gb free 43300000-433fffff 64gb free 43400000-434fffff 64gb free 43500000-435fffff 64gb free 43600000-436fffff 64gb free 43700000-437fffff 64gb free 43800000-438fffff 64gb free 43900000-439fffff 64gb free 43a00000-43afffff 64gb free 43b00000-43bfffff 64gb free 43c00000-43cfffff 64gb free 43d00000-43dfffff 64gb free 43e00000-43efffff 64gb free 43f00000-43ffffff 64gb free 44000000-440fffff 64gb free 44100000-441fffff 64gb free 44200000-442fffff 64gb free 44300000-443fffff 64gb free 44400000-444fffff 64gb free 44500000-445fffff 64gb free 44600000-446fffff 64gb free 44700000-447fffff 64gb free 44800000-448fffff 64gb free 44900000-449fffff 64gb free 44a00000-44afffff 64gb free 44b00000-44bfffff 64gb free 44c00000-44cfffff 64gb free 44d00000-44dfffff 64gb free 44e00000-44efffff 64gb free 44f00000-44ffffff 64gb free 45000000-450fffff 64gb free 45100000-451fffff 64gb free 45200000-452fffff 64gb free 45300000-453fffff 64gb free 45400000-454fffff 64gb free 45500000-455fffff 64gb free 45600000-456fffff 64gb free 45700000-457fffff 64gb free 45800000-458fffff 64gb free 45900000-459fffff 64gb free 45a00000-45afffff 64gb free 45b00000-45bfffff 64gb free 45c00000-45cfffff 64gb free 45d00000-45dfffff 64gb free 45e00000-45efffff 64gb free 45f00000-45ffffff 64gb free 46000000-460fffff 64gb free 46100000-461fffff 64gb free 46200000-462fffff 64gb free 46300000-463fffff 64gb free 46400000-464fffff 64gb free 46500000-465fffff 64gb free 46600000-466fffff 64gb free 46700000-467fffff 64gb free 46800000-468fffff 64gb free 46900000-469fffff 64gb free 46a00000-46afffff 64gb free 46b00000-46bfffff 64gb free 46c00000-46cfffff 64gb free 46d00000-46dfffff 64gb free 46e00000-46efffff 64gb free 46f00000-46ffffff 64gb free 47000000-470fffff 64gb free 47100000-471fffff 64gb free 47200000-472fffff 64gb free 47300000-473fffff 64gb free 47400000-474fffff 64gb free 47500000-475fffff 64gb free 47600000-476fffff 64gb free 47700000-477fffff 64gb free 47800000-478fffff 64gb free 47900000-479fffff 64gb free 47a00000-47afffff 64gb free 47b00000-47bfffff 64gb free 47c00000-47cfffff 64gb free 47d00000-47dfffff 64gb free 47e00000-47efffff 64gb free 47f00000-47ffffff 64gb free 48000000-480fffff 64gb free 48100000-481fffff 64gb free 48200000-482fffff 64gb free 48300000-483fffff 64gb free 48400000-484fffff 64gb free 48500000-485fffff 64gb free 48600000-486fffff 64gb free 48700000-487fffff 64gb free 48800000-488fffff 64gb free 48900000-489fffff 64gb free 48a00000-48afffff 64gb free 48b00000-48bfffff 64gb free 48c00000-48cfffff 64gb free 48d00000-48dfffff 64gb free 48e00000-48efffff 64gb free 48f00000-48ffffff 64gb free 49000000-490fffff 64gb free 49100000-491fffff 64gb free 49200000-492fffff 64gb free 49300000-493fffff 64gb free 49400000-494fffff 64gb free 49500000-495fffff 64gb free 49600000-496fffff 64gb free 49700000-497fffff 64gb free 49800000-498fffff 64gb free 49900000-499fffff 64gb free 49a00000-49afffff 64gb free 49b00000-49bfffff 64gb free 49c00000-49cfffff 64gb free 49d00000-49dfffff 64gb free 49e00000-49efffff 64gb free 49f00000-49ffffff 64gb free 4a000000-4a0fffff 64gb free 4a100000-4a1fffff 64gb free 4a200000-4a2fffff 64gb free 4a300000-4a3fffff 64gb free 4a400000-4a4fffff 64gb free 4a500000-4a5fffff 64gb free 4a600000-4a6fffff 64gb free 4a700000-4a7fffff 64gb free 4a800000-4a8fffff 64gb free 4a900000-4a9fffff 64gb free 4aa00000-4aafffff 64gb free 4ab00000-4abfffff 64gb free 4ac00000-4acfffff 64gb free 4ad00000-4adfffff 64gb free 4ae00000-4aefffff 64gb free 4af00000-4affffff 64gb free 4b000000-4b0fffff 64gb free 4b100000-4b1fffff 64gb free 4b200000-4b2fffff 64gb free 4b300000-4b3fffff 64gb free 4b400000-4b4fffff 64gb free 4b500000-4b5fffff 64gb free 4b600000-4b6fffff 64gb free 4b700000-4b7fffff 64gb free 4b800000-4b8fffff 64gb free 4b900000-4b9fffff 64gb free 4ba00000-4bafffff 64gb free 4bb00000-4bbfffff 64gb free 4bc00000-4bcfffff 64gb free 4bd00000-4bdfffff 64gb free 4be00000-4befffff 64gb free 4bf00000-4bffffff 64gb free 4c000000-4c0fffff 64gb free 4c100000-4c1fffff 64gb free 4c200000-4c2fffff 64gb free 4c300000-4c3fffff 64gb free 4c400000-4c4fffff 64gb free 4c500000-4c5fffff 64gb free 4c600000-4c6fffff 64gb free 4c700000-4c7fffff 64gb free 4c800000-4c8fffff 64gb free 4c900000-4c9fffff 64gb free 4ca00000-4cafffff 64gb free 4cb00000-4cbfffff 64gb free 4cc00000-4ccfffff 64gb free 4cd00000-4cdfffff 64gb free 4ce00000-4cefffff 64gb free 4cf00000-4cffffff 64gb free 4d000000-4d0fffff 64gb free 4d100000-4d1fffff 64gb free 4d200000-4d2fffff 64gb free 4d300000-4d3fffff 64gb free 4d400000-4d4fffff 64gb free 4d500000-4d5fffff 64gb free 4d600000-4d6fffff 64gb free 4d700000-4d7fffff 64gb free 4d800000-4d8fffff 64gb free 4d900000-4d9fffff 64gb free 4da00000-4dafffff 64gb free 4db00000-4dbfffff 64gb free 4dc00000-4dcfffff 64gb free 4dd00000-4ddfffff 64gb free 4de00000-4defffff 64gb free 4df00000-4dffffff 64gb free 4e000000-4e0fffff 64gb free 4e100000-4e1fffff 64gb free 4e200000-4e2fffff 64gb free 4e300000-4e3fffff 64gb free 4e400000-4e4fffff 64gb free 4e500000-4e5fffff 64gb free 4e600000-4e6fffff 64gb free 4e700000-4e7fffff 64gb free 4e800000-4e8fffff 64gb free 4e900000-4e9fffff 64gb free 4ea00000-4eafffff 64gb free 4eb00000-4ebfffff 64gb free 4ec00000-4ecfffff 64gb free 4ed00000-4edfffff 64gb free 4ee00000-4eefffff 64gb free 4ef00000-4effffff 64gb free 4f000000-4f0fffff 64gb free 4f100000-4f1fffff 64gb free 4f200000-4f2fffff 64gb free 4f300000-4f3fffff 64gb free 4f400000-4f4fffff 64gb free 4f500000-4f5fffff 64gb free 4f600000-4f6fffff 64gb free 4f700000-4f7fffff 64gb free 4f800000-4f8fffff 64gb free 4f900000-4f9fffff 64gb free 4fa00000-4fafffff 64gb free 4fb00000-4fbfffff 64gb free 4fc00000-4fcfffff 64gb free 4fd00000-4fdfffff 64gb free 4fe00000-4fefffff 64gb free 4ff00000-4fffffff 64gb free 50000000-500fffff 64gb free 50100000-501fffff 64gb free 50200000-502fffff 64gb free 50300000-503fffff 64gb free 50400000-504fffff 64gb free 50500000-505fffff 64gb free 50600000-506fffff 64gb free 50700000-507fffff 64gb free 50800000-508fffff 64gb free 50900000-509fffff 64gb free 50a00000-50afffff 64gb free 50b00000-50bfffff 64gb free 50c00000-50cfffff 64gb free 50d00000-50dfffff 64gb free 50e00000-50efffff 64gb free 50f00000-50ffffff 64gb free 51000000-510fffff 64gb free 51100000-511fffff 64gb free 51200000-512fffff 64gb free 51300000-513fffff 64gb free 51400000-514fffff 64gb free 51500000-515fffff 64gb free 51600000-516fffff 64gb free 51700000-517fffff 64gb free 51800000-518fffff 64gb free 51900000-519fffff 64gb free 51a00000-51afffff 64gb free 51b00000-51bfffff 64gb free 51c00000-51cfffff 64gb free 51d00000-51dfffff 64gb free 51e00000-51efffff 64gb free 51f00000-51ffffff 64gb free 52000000-520fffff 64gb free 52100000-521fffff 64gb free 52200000-522fffff 64gb free 52300000-523fffff 64gb free 52400000-524fffff 64gb free 52500000-525fffff 64gb free 52600000-526fffff 64gb free 52700000-527fffff 64gb free 52800000-528fffff 64gb free 52900000-529fffff 64gb free 52a00000-52afffff 64gb free 52b00000-52bfffff 64gb free 52c00000-52cfffff 64gb free 52d00000-52dfffff 64gb free 52e00000-52efffff 64gb free 52f00000-52ffffff 64gb free 53000000-530fffff 64gb free 53100000-531fffff 64gb free 53200000-532fffff 64gb free 53300000-533fffff 64gb free 53400000-534fffff 64gb free 53500000-535fffff 64gb free 53600000-536fffff 64gb free 53700000-537fffff 64gb free 53800000-538fffff 64gb free 53900000-539fffff 64gb free 53a00000-53afffff 64gb free 53b00000-53bfffff 64gb free 53c00000-53cfffff 64gb free 53d00000-53dfffff 64gb free 53e00000-53efffff 64gb free 53f00000-53ffffff 64gb free 54000000-540fffff 64gb free 54100000-541fffff 64gb free 54200000-542fffff 64gb free 54300000-543fffff 64gb free 54400000-544fffff 64gb free 54500000-545fffff 64gb free 54600000-546fffff 64gb free 54700000-547fffff 64gb free 54800000-548fffff 64gb free 54900000-549fffff 64gb free 54a00000-54afffff 64gb free 54b00000-54bfffff 64gb free 54c00000-54cfffff 64gb free 54d00000-54dfffff 64gb free 54e00000-54efffff 64gb free 54f00000-54ffffff 64gb free 55000000-550fffff 64gb free 55100000-551fffff 64gb free 55200000-552fffff 64gb free 55300000-553fffff 64gb free 55400000-554fffff 64gb free 55500000-555fffff 64gb free 55600000-556fffff 64gb free 55700000-557fffff 64gb free 55800000-558fffff 64gb free 55900000-559fffff 64gb free 55a00000-55afffff 64gb free 55b00000-55bfffff 64gb free 55c00000-55cfffff 64gb free 55d00000-55dfffff 64gb free 55e00000-55efffff 64gb free 55f00000-55ffffff 64gb free 56000000-560fffff 64gb free 56100000-561fffff 64gb free 56200000-562fffff 64gb free 56300000-563fffff 64gb free 56400000-564fffff 64gb free 56500000-565fffff 64gb free 56600000-566fffff 64gb free 56700000-567fffff 64gb free 56800000-568fffff 64gb free 56900000-569fffff 64gb free 56a00000-56afffff 64gb free 56b00000-56bfffff 64gb free 56c00000-56cfffff 64gb free 56d00000-56dfffff 64gb free 56e00000-56efffff 64gb free 56f00000-56ffffff 64gb free 57000000-570fffff 64gb free 57100000-571fffff 64gb free 57200000-572fffff 64gb free 57300000-573fffff 64gb free 57400000-574fffff 64gb free 57500000-575fffff 64gb free 57600000-576fffff 64gb free 57700000-577fffff 64gb free 57800000-578fffff 64gb free 57900000-579fffff 64gb free 57a00000-57afffff 64gb free 57b00000-57bfffff 64gb free 57c00000-57cfffff 64gb free 57d00000-57dfffff 64gb free 57e00000-57efffff 64gb free 57f00000-57ffffff 64gb free 58000000-580fffff 64gb free 58100000-581fffff 64gb free 58200000-582fffff 64gb free 58300000-583fffff 64gb free 58400000-584fffff 64gb free 58500000-585fffff 64gb free 58600000-586fffff 64gb free 58700000-587fffff 64gb free 58800000-588fffff 64gb free 58900000-589fffff 64gb free 58a00000-58afffff 64gb free 58b00000-58bfffff 64gb free 58c00000-58cfffff 64gb free 58d00000-58dfffff 64gb free 58e00000-58efffff 64gb free 58f00000-58ffffff 64gb free 59000000-590fffff 64gb free 59100000-591fffff 64gb free 59200000-592fffff 64gb free 59300000-593fffff 64gb free 59400000-594fffff 64gb free 59500000-595fffff 64gb free 59600000-596fffff 64gb free 59700000-597fffff 64gb free 59800000-598fffff 64gb free 59900000-599fffff 64gb free 59a00000-59afffff 64gb free 59b00000-59bfffff 64gb free 59c00000-59cfffff 64gb free 59d00000-59dfffff 64gb free 59e00000-59efffff 64gb free 59f00000-59ffffff 64gb free 5a000000-5a0fffff 64gb free 5a100000-5a1fffff 64gb free 5a200000-5a2fffff 64gb free 5a300000-5a3fffff 64gb free 5a400000-5a4fffff 64gb free 5a500000-5a5fffff 64gb free 5a600000-5a6fffff 64gb free 5a700000-5a7fffff 64gb free 5a800000-5a8fffff 64gb free 5a900000-5a9fffff 64gb free 5aa00000-5aafffff 64gb free 5ab00000-5abfffff 64gb free 5ac00000-5acfffff 64gb free 5ad00000-5adfffff 64gb free 5ae00000-5aefffff 64gb free 5af00000-5affffff 64gb free 5b000000-5b0fffff 64gb free 5b100000-5b1fffff 64gb free 5b200000-5b2fffff 64gb free 5b300000-5b3fffff 64gb free 5b400000-5b4fffff 64gb free 5b500000-5b5fffff 64gb free 5b600000-5b6fffff 64gb free 5b700000-5b7fffff 64gb free 5b800000-5b8fffff 64gb free 5b900000-5b9fffff 64gb free 5ba00000-5bafffff 64gb free 5bb00000-5bbfffff 64gb free 5bc00000-5bcfffff 64gb free 5bd00000-5bdfffff 64gb free 5be00000-5befffff 64gb free 5bf00000-5bffffff 64gb free 5c000000-5c0fffff 64gb free 5c100000-5c1fffff 64gb free 5c200000-5c2fffff 64gb free 5c300000-5c3fffff 64gb free 5c400000-5c4fffff 64gb free 5c500000-5c5fffff 64gb free 5c600000-5c6fffff 64gb free 5c700000-5c7fffff 64gb free 5c800000-5c8fffff 64gb free 5c900000-5c9fffff 64gb free 5ca00000-5cafffff 64gb free 5cb00000-5cbfffff 64gb free 5cc00000-5ccfffff 64gb free 5cd00000-5cdfffff 64gb free 5ce00000-5cefffff 64gb free 5cf00000-5cffffff 64gb free 5d000000-5d0fffff 64gb free 5d100000-5d1fffff 64gb free 5d200000-5d2fffff 64gb free 5d300000-5d3fffff 64gb free 5d400000-5d4fffff 64gb free 5d500000-5d5fffff 64gb free 5d600000-5d6fffff 64gb free 5d700000-5d7fffff 64gb free 5d800000-5d8fffff 64gb free 5d900000-5d9fffff 64gb free 5da00000-5dafffff 64gb free 5db00000-5dbfffff 64gb free 5dc00000-5dcfffff 64gb free 5dd00000-5ddfffff 64gb free 5de00000-5defffff 64gb free 5df00000-5dffffff 64gb free 5e000000-5e0fffff 64gb free 5e100000-5e1fffff 64gb free 5e200000-5e2fffff 64gb free 5e300000-5e3fffff 64gb free 5e400000-5e4fffff 64gb free 5e500000-5e5fffff 64gb free 5e600000-5e6fffff 64gb free 5e700000-5e7fffff 64gb free 5e800000-5e8fffff 64gb free 5e900000-5e9fffff 64gb free 5ea00000-5eafffff 64gb free 5eb00000-5ebfffff 64gb free 5ec00000-5ecfffff 64gb free 5ed00000-5edfffff 64gb free 5ee00000-5eefffff 64gb free 5ef00000-5effffff 64gb free 5f000000-5f0fffff 64gb free 5f100000-5f1fffff 64gb free 5f200000-5f2fffff 64gb free 5f300000-5f3fffff 64gb free 5f400000-5f4fffff 64gb free 5f500000-5f5fffff 64gb free 5f600000-5f6fffff 64gb free 5f700000-5f7fffff 64gb free 5f800000-5f8fffff 64gb free 5f900000-5f9fffff 64gb free 5fa00000-5fafffff 64gb free 5fb00000-5fbfffff 64gb free 5fc00000-5fcfffff 64gb free 5fd00000-5fdfffff 64gb free 5fe00000-5fefffff 64gb free 5ff00000-5fffffff 64gb free 60000000-600fffff 64gb free 60100000-601fffff 64gb free 60200000-602fffff 64gb free 60300000-603fffff 64gb free 60400000-604fffff 64gb free 60500000-605fffff 64gb free 60600000-606fffff 64gb free 60700000-607fffff 64gb free 60800000-608fffff 64gb free 60900000-609fffff 64gb free 60a00000-60afffff 64gb free 60b00000-60bfffff 64gb free 60c00000-60cfffff 64gb free 60d00000-60dfffff 64gb free 60e00000-60efffff 64gb free 60f00000-60ffffff 64gb free 61000000-610fffff 64gb free 61100000-611fffff 64gb free 61200000-612fffff 64gb free 61300000-613fffff 64gb free 61400000-614fffff 64gb free 61500000-615fffff 64gb free 61600000-616fffff 64gb free 61700000-617fffff 64gb free 61800000-618fffff 64gb free 61900000-619fffff 64gb free 61a00000-61afffff 64gb free 61b00000-61bfffff 64gb free 61c00000-61cfffff 64gb free 61d00000-61dfffff 64gb free 61e00000-61efffff 64gb free 61f00000-61ffffff 64gb free 62000000-620fffff 64gb free 62100000-621fffff 64gb free 62200000-622fffff 64gb free 62300000-623fffff 64gb free 62400000-624fffff 64gb free 62500000-625fffff 64gb free 62600000-626fffff 64gb free 62700000-627fffff 64gb free 62800000-628fffff 64gb free 62900000-629fffff 64gb free 62a00000-62afffff 64gb free 62b00000-62bfffff 64gb free 62c00000-62cfffff 64gb free 62d00000-62dfffff 64gb free 62e00000-62efffff 64gb free 62f00000-62ffffff 64gb free 63000000-630fffff 64gb free 63100000-631fffff 64gb free 63200000-632fffff 64gb free 63300000-633fffff 64gb free 63400000-634fffff 64gb free 63500000-635fffff 64gb free 63600000-636fffff 64gb free 63700000-637fffff 64gb free 63800000-638fffff 64gb free 63900000-639fffff 64gb free 63a00000-63afffff 64gb free 63b00000-63bfffff 64gb free 63c00000-63cfffff 64gb free 63d00000-63dfffff 64gb free 63e00000-63efffff 64gb free 63f00000-63ffffff 64gb free 64000000-640fffff 64gb free 64100000-641fffff 64gb free 64200000-642fffff 64gb free 64300000-643fffff 64gb free 64400000-644fffff 64gb free 64500000-645fffff 64gb free 64600000-646fffff 64gb free 64700000-647fffff 64gb free 64800000-648fffff 64gb free 64900000-649fffff 64gb free 64a00000-64afffff 64gb free 64b00000-64bfffff 64gb free 64c00000-64cfffff 64gb free 64d00000-64dfffff 64gb free 64e00000-64efffff 64gb free 64f00000-64ffffff 64gb free 65000000-650fffff 64gb free 65100000-651fffff 64gb free 65200000-652fffff 64gb free 65300000-653fffff 64gb free 65400000-654fffff 64gb free 65500000-655fffff 64gb free 65600000-656fffff 64gb free 65700000-657fffff 64gb free 65800000-658fffff 64gb free 65900000-659fffff 64gb free 65a00000-65afffff 64gb free 65b00000-65bfffff 64gb free 65c00000-65cfffff 64gb free 65d00000-65dfffff 64gb free 65e00000-65efffff 64gb free 65f00000-65ffffff 64gb free 66000000-660fffff 64gb free 66100000-661fffff 64gb free 66200000-662fffff 64gb free 66300000-663fffff 64gb free 66400000-664fffff 64gb free 66500000-665fffff 64gb free 66600000-666fffff 64gb free 66700000-667fffff 64gb free 66800000-668fffff 64gb free 66900000-669fffff 64gb free 66a00000-66afffff 64gb free 66b00000-66bfffff 64gb free 66c00000-66cfffff 64gb free 66d00000-66dfffff 64gb free 66e00000-66efffff 64gb free 66f00000-66ffffff 64gb free 67000000-670fffff 64gb free 67100000-671fffff 64gb free 67200000-672fffff 64gb free 67300000-673fffff 64gb free 67400000-674fffff 64gb free 67500000-675fffff 64gb free 67600000-676fffff 64gb free 67700000-677fffff 64gb free 67800000-678fffff 64gb free 67900000-679fffff 64gb free 67a00000-67afffff 64gb free 67b00000-67bfffff 64gb free 67c00000-67cfffff 64gb free 67d00000-67dfffff 64gb free 67e00000-67efffff 64gb free 67f00000-67ffffff 64gb free 68000000-680fffff 64gb free 68100000-681fffff 64gb free 68200000-682fffff 64gb free 68300000-683fffff 64gb free 68400000-684fffff 64gb free 68500000-685fffff 64gb free 68600000-686fffff 64gb free 68700000-687fffff 64gb free 68800000-688fffff 64gb free 68900000-689fffff 64gb free 68a00000-68afffff 64gb free 68b00000-68bfffff 64gb free 68c00000-68cfffff 64gb free 68d00000-68dfffff 64gb free 68e00000-68efffff 64gb free 68f00000-68ffffff 64gb free 69000000-690fffff 64gb free 69100000-691fffff 64gb free 69200000-692fffff 64gb free 69300000-693fffff 64gb free 69400000-694fffff 64gb free 69500000-695fffff 64gb free 69600000-696fffff 64gb free 69700000-697fffff 64gb free 69800000-698fffff 64gb free 69900000-699fffff 64gb free 69a00000-69afffff 64gb free 69b00000-69bfffff 64gb free 69c00000-69cfffff 64gb free 69d00000-69dfffff 64gb free 69e00000-69efffff 64gb free 69f00000-69ffffff 64gb free 6a000000-6a0fffff 64gb free 6a100000-6a1fffff 64gb free 6a200000-6a2fffff 64gb free 6a300000-6a3fffff 64gb free 6a400000-6a4fffff 64gb free 6a500000-6a5fffff 64gb free 6a600000-6a6fffff 64gb free 6a700000-6a7fffff 64gb free 6a800000-6a8fffff 64gb free 6a900000-6a9fffff 64gb free 6aa00000-6aafffff 64gb free 6ab00000-6abfffff 64gb free 6ac00000-6acfffff 64gb free 6ad00000-6adfffff 64gb free 6ae00000-6aefffff 64gb free 6af00000-6affffff 64gb free 6b000000-6b0fffff 64gb free 6b100000-6b1fffff 64gb free 6b200000-6b2fffff 64gb free 6b300000-6b3fffff 64gb free 6b400000-6b4fffff 64gb free 6b500000-6b5fffff 64gb free 6b600000-6b6fffff 64gb free 6b700000-6b7fffff 64gb free 6b800000-6b8fffff 64gb free 6b900000-6b9fffff 64gb free 6ba00000-6bafffff 64gb free 6bb00000-6bbfffff 64gb free 6bc00000-6bcfffff 64gb free 6bd00000-6bdfffff 64gb free 6be00000-6befffff 64gb free 6bf00000-6bffffff 64gb free 6c000000-6c0fffff 64gb free 6c100000-6c1fffff 64gb free 6c200000-6c2fffff 64gb free 6c300000-6c3fffff 64gb free 6c400000-6c4fffff 64gb free 6c500000-6c5fffff 64gb free 6c600000-6c6fffff 64gb free 6c700000-6c7fffff 64gb free 6c800000-6c8fffff 64gb free 6c900000-6c9fffff 64gb free 6ca00000-6cafffff 64gb free 6cb00000-6cbfffff 64gb free 6cc00000-6ccfffff 64gb free 6cd00000-6cdfffff 64gb free 6ce00000-6cefffff 64gb free 6cf00000-6cffffff 64gb free 6d000000-6d0fffff 64gb free 6d100000-6d1fffff 64gb free 6d200000-6d2fffff 64gb free 6d300000-6d3fffff 64gb free 6d400000-6d4fffff 64gb free 6d500000-6d5fffff 64gb free 6d600000-6d6fffff 64gb free 6d700000-6d7fffff 64gb free 6d800000-6d8fffff 64gb free 6d900000-6d9fffff 64gb free 6da00000-6dafffff 64gb free 6db00000-6dbfffff 64gb free 6dc00000-6dcfffff 64gb free 6dd00000-6ddfffff 64gb free 6de00000-6defffff 64gb free 6df00000-6dffffff 64gb free 6e000000-6e0fffff 64gb free 6e100000-6e1fffff 64gb free 6e200000-6e2fffff 64gb free 6e300000-6e3fffff 64gb free 6e400000-6e4fffff 64gb free 6e500000-6e5fffff 64gb free 6e600000-6e6fffff 64gb free 6e700000-6e7fffff 64gb free 6e800000-6e8fffff 64gb free 6e900000-6e9fffff 64gb free 6ea00000-6eafffff 64gb free 6eb00000-6ebfffff 64gb free 6ec00000-6ecfffff 64gb free 6ed00000-6edfffff 64gb free 6ee00000-6eefffff 64gb free 6ef00000-6effffff 64gb free 6f000000-6f0fffff 64gb free 6f100000-6f1fffff 64gb free 6f200000-6f2fffff 64gb free 6f300000-6f3fffff 64gb free 6f400000-6f4fffff 64gb free 6f500000-6f5fffff 64gb free 6f600000-6f6fffff 64gb free 6f700000-6f7fffff 64gb free 6f800000-6f8fffff 64gb free 6f900000-6f9fffff 64gb free 6fa00000-6fafffff 64gb free 6fb00000-6fbfffff 64gb free 6fc00004-6fcffffb 64gb kmalloc 6fd00004-6fdffffb 64gb zipos 6fe00004-6feffffb 64gb g_fds 6ff00004-70000003 64gb free 70000004-70000004 64kb stack 70000005-70000006 128kb winargs 70000005-700fffff 64gb free 70100000-701fffff 64gb free 70200000-702fffff 64gb free 70300000-703fffff 64gb free 70400000-704fffff 64gb free 70500000-705fffff 64gb free 70600000-706fffff 64gb free 70700000-707fffff 64gb free 70800000-708fffff 64gb free 70900000-709fffff 64gb free 70a00000-70afffff 64gb free 70b00000-70bfffff 64gb free 70c00000-70cfffff 64gb free 70d00000-70dfffff 64gb free 70e00000-70efffff 64gb free 70f00000-70ffffff 64gb free 71000000-710fffff 64gb free 71100000-711fffff 64gb free 71200000-712fffff 64gb free 71300000-713fffff 64gb free 71400000-714fffff 64gb free 71500000-715fffff 64gb free 71600000-716fffff 64gb free 71700000-717fffff 64gb free 71800000-718fffff 64gb free 71900000-719fffff 64gb free 71a00000-71afffff 64gb free 71b00000-71bfffff 64gb free 71c00000-71cfffff 64gb free 71d00000-71dfffff 64gb free 71e00000-71efffff 64gb free 71f00000-71ffffff 64gb free 72000000-720fffff 64gb free 72100000-721fffff 64gb free 72200000-722fffff 64gb free 72300000-723fffff 64gb free 72400000-724fffff 64gb free 72500000-725fffff 64gb free 72600000-726fffff 64gb free 72700000-727fffff 64gb free 72800000-728fffff 64gb free 72900000-729fffff 64gb free 72a00000-72afffff 64gb free 72b00000-72bfffff 64gb free 72c00000-72cfffff 64gb free 72d00000-72dfffff 64gb free 72e00000-72efffff 64gb free 72f00000-72ffffff 64gb free 73000000-730fffff 64gb free 73100000-731fffff 64gb free 73200000-732fffff 64gb free 73300000-733fffff 64gb free 73400000-734fffff 64gb free 73500000-735fffff 64gb free 73600000-736fffff 64gb free 73700000-737fffff 64gb free 73800000-738fffff 64gb free 73900000-739fffff 64gb free 73a00000-73afffff 64gb free 73b00000-73bfffff 64gb free 73c00000-73cfffff 64gb free 73d00000-73dfffff 64gb free 73e00000-73efffff 64gb free 73f00000-73ffffff 64gb free 74000000-740fffff 64gb free 74100000-741fffff 64gb free 74200000-742fffff 64gb free 74300000-743fffff 64gb free 74400000-744fffff 64gb free 74500000-745fffff 64gb free 74600000-746fffff 64gb free 74700000-747fffff 64gb free 74800000-748fffff 64gb free 74900000-749fffff 64gb free 74a00000-74afffff 64gb free 74b00000-74bfffff 64gb free 74c00000-74cfffff 64gb free 74d00000-74dfffff 64gb free 74e00000-74efffff 64gb free 74f00000-74ffffff 64gb free 75000000-750fffff 64gb free 75100000-751fffff 64gb free 75200000-752fffff 64gb free 75300000-753fffff 64gb free 75400000-754fffff 64gb free 75500000-755fffff 64gb free 75600000-756fffff 64gb free 75700000-757fffff 64gb free 75800000-758fffff 64gb free 75900000-759fffff 64gb free 75a00000-75afffff 64gb free 75b00000-75bfffff 64gb free 75c00000-75cfffff 64gb free 75d00000-75dfffff 64gb free 75e00000-75efffff 64gb free 75f00000-75ffffff 64gb free 76000000-760fffff 64gb free 76100000-761fffff 64gb free 76200000-762fffff 64gb free 76300000-763fffff 64gb free 76400000-764fffff 64gb free 76500000-765fffff 64gb free 76600000-766fffff 64gb free 76700000-767fffff 64gb free 76800000-768fffff 64gb free 76900000-769fffff 64gb free 76a00000-76afffff 64gb free 76b00000-76bfffff 64gb free 76c00000-76cfffff 64gb free 76d00000-76dfffff 64gb free 76e00000-76efffff 64gb free 76f00000-76ffffff 64gb free 77000000-770fffff 64gb free 77100000-771fffff 64gb free 77200000-772fffff 64gb free 77300000-773fffff 64gb free 77400000-774fffff 64gb free 77500000-775fffff 64gb free 77600000-776fffff 64gb free 77700000-777fffff 64gb free 77800000-778fffff 64gb free 77900000-779fffff 64gb free 77a00000-77afffff 64gb free 77b00000-77bfffff 64gb free 77c00000-77cfffff 64gb free 77d00000-77dfffff 64gb free 77e00000-77efffff 64gb free 77f00000-77ffffff 64gb free 78000000-780fffff 64gb free 78100000-781fffff 64gb free 78200000-782fffff 64gb free 78300000-783fffff 64gb free 78400000-784fffff 64gb free 78500000-785fffff 64gb free 78600000-786fffff 64gb free 78700000-787fffff 64gb free 78800000-788fffff 64gb free 78900000-789fffff 64gb free 78a00000-78afffff 64gb free 78b00000-78bfffff 64gb free 78c00000-78cfffff 64gb free 78d00000-78dfffff 64gb free 78e00000-78efffff 64gb free 78f00000-78ffffff 64gb free 79000000-790fffff 64gb free 79100000-791fffff 64gb free 79200000-792fffff 64gb free 79300000-793fffff 64gb free 79400000-794fffff 64gb free 79500000-795fffff 64gb free 79600000-796fffff 64gb free 79700000-797fffff 64gb free 79800000-798fffff 64gb free 79900000-799fffff 64gb free 79a00000-79afffff 64gb free 79b00000-79bfffff 64gb free 79c00000-79cfffff 64gb free 79d00000-79dfffff 64gb free 79e00000-79efffff 64gb free 79f00000-79ffffff 64gb free 7a000000-7a0fffff 64gb free 7a100000-7a1fffff 64gb free 7a200000-7a2fffff 64gb free 7a300000-7a3fffff 64gb free 7a400000-7a4fffff 64gb free 7a500000-7a5fffff 64gb free 7a600000-7a6fffff 64gb free 7a700000-7a7fffff 64gb free 7a800000-7a8fffff 64gb free 7a900000-7a9fffff 64gb free 7aa00000-7aafffff 64gb free 7ab00000-7abfffff 64gb free 7ac00000-7acfffff 64gb free 7ad00000-7adfffff 64gb free 7ae00000-7aefffff 64gb free 7af00000-7affffff 64gb free 7b000000-7b0fffff 64gb free 7b100000-7b1fffff 64gb free 7b200000-7b2fffff 64gb free 7b300000-7b3fffff 64gb free 7b400000-7b4fffff 64gb free 7b500000-7b5fffff 64gb free 7b600000-7b6fffff 64gb free 7b700000-7b7fffff 64gb free 7b800000-7b8fffff 64gb free 7b900000-7b9fffff 64gb free 7ba00000-7bafffff 64gb free 7bb00000-7bbfffff 64gb free 7bc00000-7bcfffff 64gb free 7bd00000-7bdfffff 64gb free 7be00000-7befffff 64gb free 7bf00000-7bffffff 64gb free 7c000000-7c0fffff 64gb free 7c100000-7c1fffff 64gb free 7c200000-7c2fffff 64gb free 7c300000-7c3fffff 64gb free 7c400000-7c4fffff 64gb free 7c500000-7c5fffff 64gb free 7c600000-7c6fffff 64gb free 7c700000-7c7fffff 64gb free 7c800000-7c8fffff 64gb free 7c900000-7c9fffff 64gb free 7ca00000-7cafffff 64gb free 7cb00000-7cbfffff 64gb free 7cc00000-7ccfffff 64gb free 7cd00000-7cdfffff 64gb free 7ce00000-7cefffff 64gb free 7cf00000-7cffffff 64gb free 7d000000-7d0fffff 64gb free 7d100000-7d1fffff 64gb free 7d200000-7d2fffff 64gb free 7d300000-7d3fffff 64gb free 7d400000-7d4fffff 64gb free 7d500000-7d5fffff 64gb free 7d600000-7d6fffff 64gb free 7d700000-7d7fffff 64gb free 7d800000-7d8fffff 64gb free 7d900000-7d9fffff 64gb free 7da00000-7dafffff 64gb free 7db00000-7dbfffff 64gb free 7dc00000-7dcfffff 64gb free 7dd00000-7ddfffff 64gb free 7de00000-7defffff 64gb free 7df00000-7dffffff 64gb free 7e000000-7e0fffff 64gb free 7e100000-7e1fffff 64gb free 7e200000-7e2fffff 64gb free 7e300000-7e3fffff 64gb free 7e400000-7e4fffff 64gb free 7e500000-7e5fffff 64gb free 7e600000-7e6fffff 64gb free 7e700000-7e7fffff 64gb free 7e800000-7e8fffff 64gb free 7e900000-7e9fffff 64gb free 7ea00000-7eafffff 64gb free 7eb00000-7ebfffff 64gb free 7ec00000-7ecfffff 64gb free 7ed00000-7edfffff 64gb free 7ee00000-7eefffff 64gb free 7ef00000-7effffff 64gb free 7f000000-7f0fffff 64gb kernel 7f100000-7f1fffff 64gb kernel 7f200000-7f2fffff 64gb kernel 7f300000-7f3fffff 64gb kernel 7f400000-7f4fffff 64gb kernel 7f500000-7f5fffff 64gb kernel 7f600000-7f6fffff 64gb kernel 7f700000-7f7fffff 64gb kernel 7f800000-7f8fffff 64gb kernel 7f900000-7f9fffff 64gb kernel 7fa00000-7fafffff 64gb kernel 7fb00000-7fbfffff 64gb kernel 7fc00000-7fcfffff 64gb kernel 7fd00000-7fdfffff 64gb kernel 7fe00000-7fefffff 64gb kernel 7ff00000-7fffffff 64gb kernel
63,782
2,074
jart/cosmopolitan
false
cosmopolitan/libc/runtime/winmain.greg.c
/*-*- 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/state.internal.h" #include "libc/calls/syscall_support-nt.internal.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/log/libfatal.internal.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/rdtsc.h" #include "libc/nt/console.h" #include "libc/nt/enum/consolemodeflags.h" #include "libc/nt/enum/filemapflags.h" #include "libc/nt/enum/pageflags.h" #include "libc/nt/memory.h" #include "libc/nt/pedef.internal.h" #include "libc/nt/process.h" #include "libc/nt/runtime.h" #include "libc/nt/signals.h" #include "libc/nt/struct/ntexceptionpointers.h" #include "libc/nt/struct/teb.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/stack.h" #include "libc/runtime/winargs.internal.h" #include "libc/sock/internal.h" #ifdef __x86_64__ #if IsTiny() __msabi extern typeof(CreateFileMapping) *const __imp_CreateFileMappingW; __msabi extern typeof(MapViewOfFileEx) *const __imp_MapViewOfFileEx; __msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect; #define CreateFileMapping __imp_CreateFileMappingW #define MapViewOfFileEx __imp_MapViewOfFileEx #define VirtualProtect __imp_VirtualProtect #endif #define AT_EXECFN 31L #define MAP_ANONYMOUS 32 #define MAP_PRIVATE 2 #define PROT_EXEC 4 #define PROT_READ 1 #define PROT_WRITE 2 /* * TODO: Why can't we allocate addresses above 4GB on Windows 7 x64? * TODO: How can we ensure we never overlap with KERNEL32.DLL? */ extern int64_t __wincrashearly; extern const char kConsoleHandles[3]; extern void cosmo(int, char **, char **, long (*)[2]) _Hide wontreturn; static const short kConsoleModes[3] = { kNtEnableProcessedInput | kNtEnableLineInput | kNtEnableEchoInput | kNtEnableMouseInput | kNtEnableQuickEditMode | kNtEnableExtendedFlags | kNtEnableAutoPosition | kNtEnableInsertMode | kNtEnableVirtualTerminalInput, kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput | kNtEnableVirtualTerminalProcessing, kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput | kNtEnableVirtualTerminalProcessing, }; // https://nullprogram.com/blog/2022/02/18/ static inline char16_t *MyCommandLine(void) { void *cmd; asm("mov\t%%gs:(0x60),%0\n" "mov\t0x20(%0),%0\n" "mov\t0x78(%0),%0\n" : "=r"(cmd)); return cmd; } static inline size_t StrLen16(const char16_t *s) { size_t n; for (n = 0;; ++n) { if (!s[n]) { return n; } } } __msabi static textwindows int OnEarlyWinCrash(struct NtExceptionPointers *ep) { uint32_t wrote; char buf[64], *p = buf; *p++ = 'c'; *p++ = 'r'; *p++ = 'a'; *p++ = 's'; *p++ = 'h'; *p++ = ' '; *p++ = '0'; *p++ = 'x'; p = __fixcpy(p, ep->ExceptionRecord->ExceptionCode, 32); *p++ = ' '; *p++ = 'r'; *p++ = 'i'; *p++ = 'p'; *p++ = ' '; p = __fixcpy(p, ep->ContextRecord ? ep->ContextRecord->Rip : -1, 32); *p++ = '\r'; *p++ = '\n'; WriteFile(GetStdHandle(kNtStdErrorHandle), buf, p - buf, &wrote, 0); ExitProcess(200); } __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) { bool32 rc; int64_t h, hand; uint32_t oldprot; struct WinArgs *wa; char inflagsbuf[256]; char outflagsbuf[128]; const char16_t *env16; int i, prot, count, version; size_t allocsize, stacksize; intptr_t stackaddr, allocaddr; version = NtGetPeb()->OSMajorVersion; __oldstack = (intptr_t)__builtin_frame_address(0); if ((intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui && version >= 10) { rc = SetConsoleCP(kNtCpUtf8); NTTRACE("SetConsoleCP(kNtCpUtf8) → %hhhd", rc); rc = SetConsoleOutputCP(kNtCpUtf8); NTTRACE("SetConsoleOutputCP(kNtCpUtf8) → %hhhd", rc); for (i = 0; i < 3; ++i) { hand = GetStdHandle(kConsoleHandles[i]); rc = GetConsoleMode(hand, __ntconsolemode + i); NTTRACE("GetConsoleMode(%p, [%s]) → %hhhd", hand, i ? (DescribeNtConsoleOutFlags)(outflagsbuf, __ntconsolemode[i]) : (DescribeNtConsoleInFlags)(inflagsbuf, __ntconsolemode[i]), rc); rc = SetConsoleMode(hand, kConsoleModes[i]); NTTRACE("SetConsoleMode(%p, %s) → %hhhd", hand, i ? (DescribeNtConsoleOutFlags)(outflagsbuf, kConsoleModes[i]) : (DescribeNtConsoleInFlags)(inflagsbuf, kConsoleModes[i]), rc); } } _Static_assert(sizeof(struct WinArgs) % FRAMESIZE == 0, ""); _mmi.p = _mmi.s; _mmi.n = ARRAYLEN(_mmi.s); stackaddr = GetStaticStackAddr(0); stacksize = GetStackSize(); allocaddr = stackaddr; allocsize = stacksize + sizeof(struct WinArgs); NTTRACE("WinMainNew() mapping %'zu byte stack at %p", allocsize, allocaddr); MapViewOfFileEx( (_mmi.p[0].h = CreateFileMapping(-1, &kNtIsInheritable, kNtPageExecuteReadwrite, allocsize >> 32, allocsize, NULL)), kNtFileMapWrite | kNtFileMapExecute, 0, 0, allocsize, (void *)allocaddr); prot = (intptr_t)ape_stack_prot; if (~prot & PROT_EXEC) { VirtualProtect((void *)allocaddr, allocsize, kNtPageReadwrite, &oldprot); } _mmi.p[0].x = allocaddr >> 16; _mmi.p[0].y = (allocaddr >> 16) + ((allocsize - 1) >> 16); _mmi.p[0].prot = prot; _mmi.p[0].flags = 0x00000026; // stack+anonymous _mmi.p[0].size = allocsize; _mmi.i = 1; wa = (struct WinArgs *)(allocaddr + stacksize); NTTRACE("WinMainNew() loading arg block"); count = GetDosArgv(cmdline, wa->argblock, ARRAYLEN(wa->argblock), wa->argv, ARRAYLEN(wa->argv)); for (i = 0; wa->argv[0][i]; ++i) { if (wa->argv[0][i] == '\\') { wa->argv[0][i] = '/'; } } env16 = GetEnvironmentStrings(); NTTRACE("WinMainNew() loading environment"); GetDosEnviron(env16, wa->envblock, ARRAYLEN(wa->envblock) - 8, wa->envp, ARRAYLEN(wa->envp) - 1); FreeEnvironmentStrings(env16); NTTRACE("WinMainNew() switching stacks"); _jmpstack((char *)(stackaddr + stacksize - (intptr_t)ape_stack_align), cosmo, count, wa->argv, wa->envp, wa->auxv); } /** * Main function on Windows NT. * * The Cosmopolitan Runtime provides the following services, which aim * to bring Windows NT behavior closer in harmony with System Five: * * 1. We configure CMD.EXE for UTF-8 and enable ANSI colors on Win10. * * 2. Command line arguments are passed as a blob of UTF-16 text. We * chop them up into an char *argv[] UTF-8 data structure, in * accordance with the DOS conventions for argument quoting. * * 3. Environment variables are passed to us as a sorted UTF-16 double * NUL terminated list. We translate this to char ** using UTF-8. * * 4. Allocates new stack at a high address. NT likes to choose a * stack address that's beneath the program image. We want to be * able to assume that stack addresses are located at higher * addresses than heap and program memory. * * 5. Reconfigure x87 FPU so long double is actually long (80 bits). * * 6. Finally, we need fork. Since disagreeing with fork is axiomatic to * Microsoft's engineering culture, we need to go to great lengths to * have it anyway without breaking Microsoft's rules: using the WIN32 * API (i.e. not NTDLL) to copy MAP_PRIVATE pages via a pipe. It'd go * faster if the COW pages CreateFileMappingNuma claims to have turns * out to be true. Until then we have a "PC Scale" and entirely legal * workaround that they hopefully won't block using Windows Defender. * * @param hInstance call GetModuleHandle(NULL) from main if you need it */ __msabi textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance, const char *lpCmdLine, int64_t nCmdShow) { const char16_t *cmdline; extern char os asm("__hostos"); os = _HOSTWINDOWS; /* madness https://news.ycombinator.com/item?id=21019722 */ kStartTsc = rdtsc(); __pid = GetCurrentProcessId(); #if !IsTiny() __wincrashearly = AddVectoredExceptionHandler(1, (void *)OnEarlyWinCrash); #endif cmdline = MyCommandLine(); #ifdef SYSDEBUG /* sloppy flag-only check for early initialization */ if (__strstr16(cmdline, u"--strace")) ++__strace; #endif NTTRACE("WinMain()"); if (_weaken(WinSockInit)) _weaken(WinSockInit)(); if (_weaken(WinMainForked)) _weaken(WinMainForked)(); WinMainNew(cmdline); } #endif /* __x86_64__ */
10,259
253
jart/cosmopolitan
false
cosmopolitan/libc/runtime/metalprintf.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_METALPRINTF_H_ #define COSMOPOLITAN_LIBC_RUNTIME_METALPRINTF_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ void MetalPrintf(const char *, ...); #define MetalPrintf(FMT, ...) \ do { \ const char StackFmt[] = FMT; \ MetalPrintf(StackFmt, ##__VA_ARGS__); \ } while (0) COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_METALPRINTF_H_ */
509
17
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mprotect-nt.greg.c
/*-*- 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 2022 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/intrin/directmap.internal.h" #include "libc/nt/memory.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" textwindows int sys_mprotect_nt(void *addr, size_t size, int prot) { int rc = 0; unsigned i; uint32_t op; char *a, *b, *x, *y, *p; __mmi_lock(); p = addr; i = FindMemoryInterval(&_mmi, (intptr_t)p >> 16); if (i == _mmi.i || (!i && p + size <= (char *)ADDR_32_TO_48(_mmi.p[0].x))) { // memory isn't in memtrack // let's just trust the user then // it's probably part of the executable if (!VirtualProtect(addr, size, __prot2nt(prot, false), &op)) { rc = -1; } } else { // memory is in memtrack, so use memtrack, to do dimensioning // we unfortunately must do something similar to this for cow for (; i < _mmi.i; ++i) { x = (char *)ADDR_32_TO_48(_mmi.p[i].x); y = x + _mmi.p[i].size; if ((x <= p && p < y) || (x < p + size && p + size <= y) || (p < x && y < p + size)) { a = MIN(MAX(p, x), y); b = MAX(MIN(p + size, y), x); if (!VirtualProtect(a, b - a, __prot2nt(prot, _mmi.p[i].iscow), &op)) { rc = -1; break; } } else { break; } } } __mmi_unlock(); return rc; }
3,117
61
jart/cosmopolitan
false
cosmopolitan/libc/runtime/munmap.c
/*-*- 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/assert.h" #include "libc/calls/state.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/directmap.internal.h" #include "libc/intrin/likely.h" #include "libc/intrin/strace.internal.h" #include "libc/log/backtrace.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/errfuns.h" #define IP(X) (intptr_t)(X) #define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1))) #define FRAME(x) ((int)((intptr_t)(x) >> 16)) static noasan void MunmapShadow(char *p, size_t n) { intptr_t a, b, x, y; KERNTRACE("MunmapShadow(%p, %'zu)", p, n); a = ((intptr_t)p >> 3) + 0x7fff8000; b = a + (n >> 3); if (IsMemtracked(FRAME(a), FRAME(b - 1))) { x = ROUNDUP(a, FRAMESIZE); y = ROUNDDOWN(b, FRAMESIZE); if (0 && x < y) { // delete shadowspace if unmapping ≥512kb. in practice it has // to be >1mb since we can only unmap it if it's aligned, and // as such we poison the edges if there are any. __repstosb((void *)a, kAsanUnmapped, x - a); _Munmap((void *)x, y - x); __repstosb((void *)y, kAsanUnmapped, b - y); } else { // otherwise just poison and assume reuse __repstosb((void *)a, kAsanUnmapped, b - a); } } else { STRACE("unshadow(%.12p, %p) EFAULT", a, b - a); } } // our api supports doing things like munmap(0, 0x7fffffffffff) but some // platforms (e.g. openbsd) require that we know the specific intervals // or else it returns EINVAL. so we munmap a piecewise. static noasan void MunmapImpl(char *p, size_t n) { char *q; size_t m; intptr_t a, b, c; int i, l, r, rc, beg, end; KERNTRACE("MunmapImpl(%p, %'zu)", p, n); l = FRAME(p); r = FRAME(p + n - 1); i = FindMemoryInterval(&_mmi, l); for (; i < _mmi.i && r >= _mmi.p[i].x; ++i) { if (l >= _mmi.p[i].x && r <= _mmi.p[i].y) { // it's contained within the entry beg = l; end = r; } else if (l <= _mmi.p[i].x && r >= _mmi.p[i].x) { // it overlaps with the lefthand side of the entry beg = _mmi.p[i].x; end = MIN(r, _mmi.p[i].y); } else if (l <= _mmi.p[i].y && r >= _mmi.p[i].y) { // it overlaps with the righthand side of the entry beg = MAX(_mmi.p[i].x, l); end = _mmi.p[i].y; } else { unreachable; } // openbsd even requires that if we mapped, for instance a 5 byte // file, that we be sure to call munmap(file, 5). let's abstract! a = ADDR_32_TO_48(beg); b = ADDR_32_TO_48(end) + FRAMESIZE; c = ADDR_32_TO_48(_mmi.p[i].x) + _mmi.p[i].size; q = (char *)a; m = MIN(b, c) - a; if (!IsWindows()) { _npassert(!sys_munmap(q, m)); } else { // Handled by UntrackMemoryIntervals() on Windows } if (IsAsan() && !OverlapsShadowSpace(p, n)) { MunmapShadow(q, m); } } } noasan int _Munmap(char *p, size_t n) { unsigned i; char poison; intptr_t a, b, x, y; _unassert(!__vforked); if (UNLIKELY(!n)) { STRACE("munmap n is 0"); return einval(); } if (UNLIKELY(!IsLegalSize(n))) { STRACE("munmap n isn't 48-bit"); return einval(); } if (UNLIKELY(!IsLegalPointer(p))) { STRACE("munmap p isn't 48-bit"); return einval(); } if (UNLIKELY(!IsLegalPointer(p + (n - 1)))) { STRACE("munmap p+(n-1) isn't 48-bit"); return einval(); } if (UNLIKELY(!ALIGNED(p))) { STRACE("munmap(%p) isn't 64kb aligned", p); return einval(); } MunmapImpl(p, n); return UntrackMemoryIntervals(p, n); } /** * Releases memory pages. * * @param p is the beginning of the memory region to unmap * @param n is the number of bytes to be unmapped * @return 0 on success, or -1 w/ errno * @raises EINVAL if `n == 0` * @raises EINVAL if `n` isn't 48-bit * @raises EINVAL if `p+(n-1)` isn't 48-bit * @raises EINVAL if `p` isn't 65536-byte aligned */ int munmap(void *p, size_t n) { int rc; size_t toto; __mmi_lock(); rc = _Munmap(p, n); #if SYSDEBUG toto = __strace > 0 ? GetMemtrackSize(&_mmi) : 0; #endif __mmi_unlock(); STRACE("munmap(%.12p, %'zu) → %d% m (%'zu bytes total)", p, n, rc, toto); return rc; }
6,235
167
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mremap.c
/*-*- 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/sysv/consts/mremap.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/directmap.internal.h" #include "libc/intrin/likely.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/nt/runtime.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/errfuns.h" #define IP(X) (intptr_t)(X) #define VIP(X) (void *)IP(X) #define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1))) #define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000) #define FRAME(x) ((int)((intptr_t)(x) >> 16)) static size_t GetMapSize(size_t i, size_t *j) { size_t n; n = (size_t)(_mmi.p[i].y - _mmi.p[i].x + 1) << 16; while (i + 1 < _mmi.i) { if (_mmi.p[i + 1].x != _mmi.p[i].y + 1) break; ++i; n += (size_t)(_mmi.p[i].y - _mmi.p[i].x + 1) << 16; } *j = i; return n; } static bool MustMoveMap(intptr_t y, size_t j) { return ADDR_32_TO_48(_mmi.p[j].y) + FRAMESIZE > y || (j + 1 < _mmi.i && ADDR_32_TO_48(_mmi.p[j + 1].x) < y); } /** * Extends and/or relocates memory pages. * * @param p is old address * @param n is old size * @param m is new size * @param f should have MREMAP_MAYMOVE and may have MAP_FIXED * @param q is new address */ void *mremap(void *p, size_t n, size_t m, int f, ... /* void *q */) { enosys(); return MAP_FAILED; #if 0 va_list va; void *res, *q; if (f & MREMAP_FIXED) { va_start(va, f); q = va_arg(va, void *); va_end(va); } else { q = 0; } enosys(); res = MAP_FAILED; STRACE("mremap(%p, %'zu, %'zu, %s, %p) → %p% m", p, n, m, DescribeRemapFlags(f), q, res); return res; // TODO(jart): perhaps some day? // probably not a big perf gain at this point :| size_t i, j, k; struct DirectMap dm; int a, b, prot, flags; _unassert(!__vforked); if (UNLIKELY(!m)) { STRACE("m=0"); return VIP(einval()); } else if (UNLIKELY(!n)) { STRACE("n=0"); return VIP(eopnotsupp()); } else if (UNLIKELY(!ALIGNED(n))) { STRACE("n align"); return VIP(eopnotsupp()); } else if (UNLIKELY(!ALIGNED(m))) { STRACE("n align"); return VIP(eopnotsupp()); } else if (UNLIKELY(!ALIGNED(p))) { STRACE("64kb align"); return VIP(einval()); } else if (UNLIKELY(!IsLegalSize(n))) { STRACE("n too big"); return VIP(enomem()); } else if (UNLIKELY(!IsLegalSize(m))) { STRACE("m too big"); return VIP(enomem()); } else if (f & ~(MREMAP_MAYMOVE | MREMAP_FIXED)) { STRACE("bad flag"); return VIP(einval()); } else if (!IsMemtracked(FRAME(p), FRAME((intptr_t)p + (n - 1)))) { STRACE("interval not tracked"); return VIP(efault()); } STRACE("mremap(%p, %'zu, %'zu, %#b)", p, n, m, f); i = FindMemoryInterval(&_mmi, FRAME(p)); if (i >= _mmi.i) return VIP(efault()); flags = _mmi.p[i].flags; if (!(flags & MAP_ANONYMOUS)) { return VIP(eopnotsupp()); /* TODO */ } if (f & MREMAP_FIXED) { if (!ALIGNED(q)) return VIP(einval()); return VIP(eopnotsupp()); /* TODO */ } prot = _mmi.p[i].prot; for (k = i + 1; k <= j; ++k) { prot |= _mmi.p[k].prot; if (_mmi.p[k].flags != flags) { return VIP(enomem()); } } if (m == n) { return p; } else if (m < n) { if (munmap((char *)p + n, m - n) != -1) { return p; } else { return MAP_FAILED; } } else if (!MustMoveMap(j, (intptr_t)p + n)) { dm = sys_mmap((char *)p + n, m - n, prot, flags | MAP_FIXED, -1, 0); if (dm.addr == MAP_FAILED) return 0; if (TrackMemoryInterval(&_mmi, ((uintptr_t)p + n) >> 16, ((uintptr_t)p + m - FRAMESIZE) >> 16, dm.maphandle, prot, flags, false, false, 0, m - n) != -1) { if (_weaken(__asan_map_shadow)) { _weaken(__asan_map_shadow)((uintptr_t)dm.addr, m - n); } return p; } else { abort(); } } else if (!(f & MREMAP_MAYMOVE)) { return VIP(enomem()); } else if (IsLinux()) { a = (uint32_t)(kAutomapStart >> 16); i = FindMemoryInterval(&_mmi, a); if (i < _mmi.i) { for (; i + 1 < _mmi.i; ++i) { if (((size_t)(_mmi.p[i + 1].x - _mmi.p[i].y - 1) << 16) >= m) { break; } } if (__builtin_add_overflow(_mmi.p[i].y, 1, &a)) { return VIP(enomem()); } } if (__builtin_add_overflow(a, (int)((m >> 16) - 1), &b)) { return VIP(enomem()); } q = sys_mremap((void *)p, n, m, MREMAP_MAYMOVE | MREMAP_FIXED, (void *)ADDR_32_TO_48(a)); KERNTRACE("sys_mremap(%p, %'zu, %'zu, %#b, %p) → %p", p, n, m, MREMAP_MAYMOVE | MREMAP_FIXED, ADDR_32_TO_48(a), q); if (q == MAP_FAILED) return 0; if (ReleaseMemoryIntervals(&_mmi, (uintptr_t)p >> 16, ((uintptr_t)p + n - FRAMESIZE) >> 16, 0) != -1 && TrackMemoryInterval(&_mmi, a, b, -1, prot, flags, false, false, 0, m) != -1) { if (_weaken(__asan_poison)) { if (!OverlapsShadowSpace(p, n)) { _weaken(__asan_poison)((intptr_t)p, n, kAsanUnmapped); } if (!OverlapsShadowSpace(q, m)) { _weaken(__asan_map_shadow)((intptr_t)q, m); } } return (void *)ADDR_32_TO_48(a); } else { abort(); } } else if ((q = mmap(0, m, prot, flags, -1, 0)) != MAP_FAILED) { memcpy(q, p, n); munmap(p, n); return q; } else { return q; } #endif }
7,485
209
jart/cosmopolitan
false
cosmopolitan/libc/runtime/fpreset.S
/*-*- 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" // Re-initializes FPU. fpreset: _fpreset: .leafprologue .profilable finit .leafepilogue .endfn _fpreset,globl .endfn fpreset,globl
2,008
30
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getsymboltable.c
/*-*- 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/assert.h" #include "libc/intrin/bits.h" #include "libc/intrin/promises.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/str/str.h" #include "libc/thread/thread.h" #include "libc/x/x.h" #include "libc/zip.h" #include "libc/zipos/zipos.internal.h" #include "third_party/puff/puff.h" static pthread_spinlock_t g_lock; _Hide struct SymbolTable *__symtab; // for kprintf /** * Looks for `.symtab` in zip central directory. */ static ssize_t FindSymtabInZip(struct Zipos *zipos) { size_t i, n, c; c = GetZipCdirOffset(zipos->cdir); n = GetZipCdirRecords(zipos->cdir); for (i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(zipos->map + c)) { if (ZIP_CFILE_NAMESIZE(zipos->map + c) == 7 && READ32LE(ZIP_CFILE_NAME(zipos->map + c + 0)) == READ32LE(".sym") && READ16LE(ZIP_CFILE_NAME(zipos->map + c + 4)) == READ16LE("ta") && *ZIP_CFILE_NAME(zipos->map + c + 6) == 'b') { return c; } } return -1; } /** * Reads symbol table from zip directory. * @note This code can't depend on dlmalloc() */ static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) { size_t size, size2; ssize_t rc, cf, lf; struct SymbolTable *res = 0; if ((cf = FindSymtabInZip(zipos)) != -1) { lf = GetZipCfileOffset(zipos->map + cf); size = GetZipLfileUncompressedSize(zipos->map + lf); size2 = ROUNDUP(size, FRAMESIZE); if ((res = _mapanon(size2))) { switch (ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf)) { case kZipCompressionNone: memcpy(res, (void *)ZIP_LFILE_CONTENT(zipos->map + lf), size); break; case kZipCompressionDeflate: if (__inflate((void *)res, size, (void *)ZIP_LFILE_CONTENT(zipos->map + lf), GetZipLfileCompressedSize(zipos->map + lf))) { munmap(res, size2); res = 0; } break; default: munmap(res, size2); res = 0; break; } } } STRACE("GetSymbolTableFromZip() → %p", res); return res; } /** * Reads symbol table from .com.dbg file. * @note This code can't depend on dlmalloc() */ static struct SymbolTable *GetSymbolTableFromElf(void) { int e; const char *s; if (PLEDGED(RPATH) && (s = FindDebugBinary())) { return OpenSymbolTable(s); } else { return 0; } } /** * Returns symbol table singleton. * * This uses multiple strategies to find the symbol table. The first * strategy, depends on whether or not the following is linked: * * STATIC_YOINK("__zipos_get"); * * In that case, the symbol table may be read from `/zip/.symtab` which * is generated by `o//tool/build/symtab.com`. The second strategy is to * look for the concomitant `.com.dbg` executable, which may very well * be the one currently executing, or it could be placed in the same * folder as your `.com` binary, or lastly, it could be explicitly * specified via the `COMDBG` environment variable. * * Function tracing is disabled throughout the duration of this call. * Backtraces and other core runtime functionality depend on this. * * @return symbol table, or NULL if not found */ struct SymbolTable *GetSymbolTable(void) { struct Zipos *z; if (pthread_spin_trylock(&g_lock)) return 0; if (!__symtab && !__isworker) { if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) { if ((__symtab = GetSymbolTableFromZip(z))) { __symtab->names = (uint32_t *)((char *)__symtab + __symtab->names_offset); __symtab->name_base = (char *)((char *)__symtab + __symtab->name_base_offset); } } if (!__symtab) { __symtab = GetSymbolTableFromElf(); } } pthread_spin_unlock(&g_lock); return __symtab; } /** * Returns low index into symbol table for address. * * @param t if null will be auto-populated only if already open * @return index or -1 if nothing found */ noinstrument privileged int __get_symbol(struct SymbolTable *t, intptr_t a) { // we need privileged because: // kprintf is privileged and it depends on this // we don't want function tracing because: // function tracing depends on this function via kprintf unsigned l, m, r, n, k; if (!t && __symtab) { t = __symtab; } if (t) { l = 0; r = n = t->count; k = a - t->addr_base; while (l < r) { m = (l + r) >> 1; if (t->symbols[m].y < k) { l = m + 1; } else { r = m; } } if (l < n && t->symbols[l].x <= k && k <= t->symbols[l].y) { return l; } } return -1; }
6,627
179
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mapelfread.c
/*-*- 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/elf/def.h" #include "libc/elf/elf.h" #include "libc/runtime/ezmap.internal.h" #include "libc/runtime/internal.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; } }
2,193
32
jart/cosmopolitan
false
cosmopolitan/libc/runtime/untrackmemoryintervals.c
/*-*- 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/assert.h" #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/runtime/memtrack.internal.h" int UntrackMemoryIntervals(void *addr, size_t size) { int a, b; _unassert(size > 0); a = ROUNDDOWN((intptr_t)addr, FRAMESIZE) >> 16; b = ROUNDDOWN((intptr_t)addr + size - 1, FRAMESIZE) >> 16; return ReleaseMemoryIntervals(&_mmi, a, b, SupportsWindows() ? ReleaseMemoryNt : 0); }
2,283
32
jart/cosmopolitan
false
cosmopolitan/libc/runtime/msync.c
/*-*- 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/sysv/consts/msync.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/cp.internal.h" #include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" /** * Synchronize memory mapping changes to disk. * * Without this, there's no guarantee memory is written back to disk. * Particularly on RHEL5, OpenBSD, and Windows NT. * * @param addr needs to be 4096-byte page aligned * @param flags needs MS_ASYNC or MS_SYNC and can have MS_INVALIDATE * @return 0 on success or -1 w/ errno * @raise ECANCELED if thread was cancelled in masked mode * @raise EINTR if we needed to block and a signal was delivered instead * @cancellationpoint */ int msync(void *addr, size_t size, int flags) { int rc; BEGIN_CANCELLATION_POINT; _unassert(((flags & MS_SYNC) ^ (flags & MS_ASYNC)) || !(MS_SYNC && MS_ASYNC)); if (!IsWindows()) { rc = sys_msync(addr, size, flags); } else { rc = sys_msync_nt(addr, size, flags); } END_CANCELLATION_POINT; STRACE("msync(%p, %'zu, %#x) → %d% m", addr, size, flags, rc); return rc; }
3,040
57
jart/cosmopolitan
false
cosmopolitan/libc/runtime/e820.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_E820_H_ #define COSMOPOLITAN_LIBC_RUNTIME_E820_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct SmapEntry { uint64_t addr; uint64_t size; enum { kMemoryUsable = 1, kMemoryUnusable = 2, kMemoryAcpiReclaimable = 3, kMemoryAcpiNvs = 4, kMemoryBad = 5 } type; uint32_t acpi3; }; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_E820_H_ */
476
22
jart/cosmopolitan
false
cosmopolitan/libc/runtime/morph.greg.c
/*-*- 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 2022 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. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define ShouldUseMsabiAttribute() 1 #include "ape/sections.internal.h" #include "libc/assert.h" #include "libc/calls/internal.h" #include "libc/calls/struct/sigset.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asmflag.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/strace.internal.h" #include "libc/nt/enum/pageflags.h" #include "libc/nt/memory.h" #include "libc/nt/runtime.h" #include "libc/nt/thunk/msabi.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/nr.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/sig.h" __msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect; static inline int __morph_rt_sigprocmask(int h, const sigset_t *s, sigset_t *o, size_t c) { #ifdef __aarch64__ register long r0 asm("x0") = (long)h; register long r1 asm("x1") = (long)s; register long r2 asm("x2") = (long)o; register long r3 asm("x3") = (long)c; register long r8 asm("x8") = (long)__NR_sigprocmask; register long res_x0 asm("x0"); asm volatile("svc\t0" : "=r"(res_x0) : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r8) : "memory"); return res_x0; #else return 0; #endif } static inline int __morph_sigprocmask(int how, const sigset_t *set, sigset_t *oldset) { return __morph_rt_sigprocmask(how, set, oldset, sizeof(*set)); } static privileged void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) { #ifdef __x86_64__ bool cf; int ax, dx; uint32_t op; if (!IsWindows()) { asm volatile(CFLAG_ASM("clc\n\t" "syscall") : CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx) : "1"(__NR_mprotect), "D"(addr), "S"(size), "2"(prot) : "rcx", "r8", "r9", "r10", "r11", "memory"); #ifndef NDEBUG if (cf) ax = -ax; if (ax == -EPERM) { kprintf("error: need pledge(prot_exec) permission to code morph\n"); _Exit(26); } #endif _npassert(!ax); } else { __imp_VirtualProtect(addr, size, ntprot, &op); } #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; register long res_x0 asm("x0"); asm volatile("svc\t0" : "=r"(res_x0) : "r"(r0), "r"(r1), "r"(r2), "r"(r8) : "memory"); _npassert(!res_x0); #endif } /** * Begins code morphing executable. * * @return 0 on success, or -1 w/ errno */ privileged void __morph_begin(sigset_t *save) { int ax; bool cf; intptr_t dx; sigset_t ss = {{-1, -1}}; STRACE("__morph_begin()"); #ifdef __x86_64__ if (IsOpenbsd()) { asm volatile(CFLAG_ASM("syscall") : CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx) : "1"(__NR_sigprocmask), "D"(SIG_BLOCK), "S"(-1u) : "rcx", "r8", "r9", "r10", "r11", "memory"); save->__bits[0] = ax & 0xffffffff; _npassert(!cf); } else if (!IsWindows() && !IsMetal()) { asm volatile("mov\t$8,%%r10d\n\t" "syscall" : "=a"(ax), "=d"(dx) : "0"(__NR_sigprocmask), "D"(SIG_BLOCK), "S"(&ss), "1"(save) : "rcx", "r8", "r9", "r10", "r11", "memory", "cc"); _npassert(!ax); } #else __morph_sigprocmask(SIG_BLOCK, &ss, save); #endif __morph_mprotect(__executable_start, __privileged_addr - __executable_start, PROT_READ | PROT_WRITE, kNtPageWritecopy); } /** * Begins code morphing executable. */ privileged void __morph_end(sigset_t *save) { int ax; long dx; bool cf; __morph_mprotect(__executable_start, __privileged_addr - __executable_start, PROT_READ | PROT_EXEC, kNtPageExecuteRead); #ifdef __x86_64__ if (IsOpenbsd()) { asm volatile(CFLAG_ASM("syscall") : CFLAG_CONSTRAINT(cf), "=a"(ax), "=d"(dx) : "1"(__NR_sigprocmask), "D"(SIG_SETMASK), "S"(save->__bits[0]) : "rcx", "r8", "r9", "r10", "r11", "memory"); _npassert(!cf); } else if (!IsWindows() && !IsMetal()) { asm volatile("mov\t$8,%%r10d\n\t" "syscall" : "=a"(ax), "=d"(dx) : "0"(__NR_sigprocmask), "D"(SIG_SETMASK), "S"(save), "1"(0L) : "rcx", "r8", "r9", "r10", "r11", "memory", "cc"); _npassert(!ax); } #else __morph_sigprocmask(SIG_SETMASK, save, 0); #endif STRACE("__morph_end()"); }
6,486
165
jart/cosmopolitan
false
cosmopolitan/libc/runtime/utmpx.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_UTMPX_H_ #define COSMOPOLITAN_LIBC_RUNTIME_UTMPX_H_ #include "libc/calls/struct/timeval.h" #include "libc/calls/weirdtypes.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct utmpx { short ut_type; pid_t ut_pid; char ut_line[32]; char ut_id[4]; char ut_user[32]; char ut_host[256]; struct { short __e_termination; short __e_exit; } ut_exit; long ut_session; struct timeval ut_tv; unsigned ut_addr_v6[4]; char __unused[20]; }; void endutxent(void); struct utmpx *getutxent(void); struct utmpx *getutxid(const struct utmpx *); struct utmpx *getutxline(const struct utmpx *); struct utmpx *pututxline(const struct utmpx *); void setutxent(void); #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) #define e_exit __e_exit #define e_termination __e_termination void updwtmpx(const char *, const struct utmpx *); int utmpxname(const char *); #endif #define EMPTY 0 #define RUN_LVL 1 #define BOOT_TIME 2 #define NEW_TIME 3 #define OLD_TIME 4 #define INIT_PROCESS 5 #define LOGIN_PROCESS 6 #define USER_PROCESS 7 #define DEAD_PROCESS 8 COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_UTMPX_H_ */
1,268
52
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getlogin.c
/*-*- 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 2022 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/syscall_support-nt.internal.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/nt/accounting.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/errfuns.h" int sys_getlogin(char *, uint32_t); /** * Returns login name. */ char *getlogin(void) { uint32_t size; const char *p, *res; char16_t buf16[257]; static char login[128]; if (IsBsd()) { if (sys_getlogin(login, sizeof(login)) != -1) { res = login; } else { res = 0; } } else if (IsWindows()) { size = ARRAYLEN(buf16); if (GetUserName(&buf16, &size)) { tprecode16to8(login, sizeof(login), buf16); res = login; } else { __winerr(); res = 0; } } else if ((p = getenv("LOGNAME"))) { res = p; } else { enoent(); res = 0; } STRACE("getlogin() → %#s% m", res); return res; }
2,777
62
jart/cosmopolitan
false
cosmopolitan/libc/runtime/init.S
/*-*- 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" #include "libc/runtime/internal.h" #include "libc/sysv/consts/prot.h" #include "libc/dce.h" // Decentralized function for process initialization. // // Modules may inject cheap data structure initialization code into // this function using the .init.start and .init.end macros. That // code can use the LODS and STOS instructions to initialize memory // that's restricted to read-only after initialization by PIRO. // // This is fast, since the linker is able to roll-up initialization // for large codebases comprised of many modules, into a perfectly // linear order. It also enables a common pattern we use, which we // call “Referencing Is Initialization” (RII). // // C/C++ code should favor using ordinary constructors, since under // normal circumstances the compiler will clobber RDI and RSI which // are granted special meanings within this function. // // @param r12 is argc (still callee saved) // @param r13 is argv (still callee saved) // @param r14 is envp (still callee saved) // @param r15 is envp (still callee saved) // @note rdi is __init_bss_start (callee monotonic lockstep) // @note rsi is __init_rodata_start (callee monotonic lockstep) // @see .init.start & .init.end (libc/macros.internal.h) // @see ape/ape.lds .section .initprologue,"ax",@progbits .type _init,@function .globl _init _init: #ifdef __x86_64__ push %rbp mov %rsp,%rbp .profilable ezlea __init_bss_start,di ezlea __init_rodata_start,si #elif defined(__aarch64__) stp x29,x30,[sp,-16]! mov x29,sp #endif .previous/* ... decentralized content ... */.section .initepilogue,"ax",@progbits #ifdef __x86_64__ #if IsModeDbg() _init_check_rdi_rsi: jmp 2f 1: call abort 2: ezlea __init_bss_end,ax cmp %rax,%rdi jne 1b ezlea __init_rodata_end,ax cmp %rax,%rsi jne 1b 3: .endfn _init_check_rdi_rsi #endif _woot: leave #elif defined(__aarch64__) ldp x29,x30,[sp],#16 #endif ret .previous #ifdef __x86_64__ // Decentralized section for packed data structures & initializers. // // @see .initro (libc/macros.internal.h) // @see ape/ape.lds .section .initroprologue,"a",@progbits .type __init_rodata_start,@object .type __init_rodata_end,@object .globl __init_rodata_start,__init_rodata_end .hidden __init_rodata_start,__init_rodata_end .balign __SIZEOF_POINTER__ .underrun __init_rodata_start: .previous/* ... decentralized content ... */.section .initroepilogue,"a",@progbits __init_rodata_end: .byte 0x90 .overrun .previous // Decentralized section for unpacked data structures. // // Data in this section becomes read-only after initialization. // // @see .piro.bss.init (libc/macros.internal.h) // @see libc/runtime/piro.c // @see ape/ape.lds .section .piro.bss.init.1,"aw",@nobits .type __init_bss_start,@object .type __init_bss_end,@object .globl __init_bss_start,__init_bss_end .hidden __init_bss_start,__init_bss_end .balign __SIZEOF_POINTER__ .underrun __init_bss_start: .previous/* ... decentralized content ... */.section .piro.bss.init.3,"aw",@nobits __init_bss_end: .byte 0 .overrun .previous // Special area for Windows NT support code. // // Isolating this code adds value for Windows users by minimizing // page faults through improved locality. On System Five the PIRO // runtime can unmap these pages. // // @see libc/runtime/piro.c // @see ape/ape.lds .section .textwindowsprologue,"ax",@progbits .type __text_windows_start,@object .type __text_windows_end,@object .globl __text_windows_start,__text_windows_end .hidden __text_windows_start,__text_windows_end int3 __text_windows_start: .previous/* ... decentralized content ... */.section .textwindowsepilogue,"ax",@progbits __text_windows_end: int3 .previous #endif /* __x86_64__ */
5,577
161
jart/cosmopolitan
false
cosmopolitan/libc/runtime/dsohandle.S
/*-*- 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" #include "libc/notice.inc" .underrun // Uniquely identifies each artifact linked in an address space. __dso_handle: .quad __dso_handle .endobj __dso_handle,globl,hidden .overrun
2,053
28
jart/cosmopolitan
false
cosmopolitan/libc/runtime/metalprintf.greg.c
/*-*- 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 2021 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/uart.internal.h" #include "libc/runtime/pc.internal.h" #include "libc/runtime/runtime.h" #ifdef __x86_64__ #define PUTC(C) \ do { \ while (!(inb(0x3F8 + UART_LSR) & UART_TTYTXR)) { \ __builtin_ia32_pause(); \ } \ outb(0x3F8, C); \ } while (0) /** * Prints string to serial port. * * This only supports %d and %s. It'll will work even if .rodata hasn't * been loaded into memory yet. */ _Hide textreal void(MetalPrintf)(const char *fmt, ...) { int i; char c; unsigned u; va_list va; const char *s; unsigned long d; va_start(va, fmt); for (;;) { switch ((c = *fmt++)) { case '\0': va_end(va); return; case '%': switch ((c = *fmt++)) { case 's': for (s = va_arg(va, const char *); s && *s; ++s) { PUTC(*s); } break; case 'd': d = va_arg(va, unsigned long); for (i = 16; i--;) { u = (d >> (i * 4)) & 0xf; if (u < 10) { c = '0' + u; } else { u -= 10; c = 'a' + u; } PUTC(c); } break; default: PUTC(c); break; } break; default: PUTC(c); break; } } } #endif /* __x86_64__ */
3,416
85
jart/cosmopolitan
false
cosmopolitan/libc/runtime/efipostboot.S
/*-*- 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 2022 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/dce.h" #include "libc/macros.internal.h" #include "libc/runtime/pc.internal.h" .real // Start the Cosmopolitan runtime after exiting UEFI Boot Services. // // @param rdi is mm // @param rsi is new pml4t // @param rdx is argc // @param rcx is argv // @see libc/runtime/efimain.greg.c _EfiPostboot: cli // Define handy mnemonics for parameters & constants stored in // call-saved registers. #define rMm %r12 #define rArgc %r13 #define rArgv %r14 #define rBane %r15 movabs $BANE,rBane mov %rdi,rMm mov %rdx,rArgc lea (rBane,%rcx),rArgv mov $PHYSICAL(.Ltmpstk),%rax // switch to temporary stack and $-16,%al // in physical space xchg %rax,%rsp mov $PHYSICAL(0f),%eax // resume execution in copied jmp *%rax // image 0: mov $EFER,%ecx // enable syscall/sysret & nx rdmsr or $EFER_SCE|EFER_NXE,%eax wrmsr mov %rsi,%cr3 // load new page table add rBane,%rsp // we can now switch stack to add rBane,rMm // negative address space mov $1024*1024,%edx // set up virtual memory mov $1024*1024+_end,%ecx // mapping sub $__executable_start,%ecx call __map_phdrs mov $1f,%eax // switch rip to virtual jmp *%rax // address space 1: push $0x037f fldcw (%rsp) .weak _gdtr lgdt _gdtr // switch to our own GDT mov $GDT_LONG_DATA,%ax mov %ax,%ds mov %ax,%ss mov %ax,%es mov %ax,%fs mov %ax,%gs .weak ape_stack_vaddr .weak ape_stack_memsz movabs $ape_stack_vaddr,%rsp // switch to final stack in add $ape_stack_memsz,%rsp // virtual address space movl $0,0x7b000 // unmap null 2mb mov rMm,%rdi xor %esi,%esi // free up now-unused pages xor %edx,%edx call __reclaim_boot_pages push .Lenv0(%rip) // envp[0][0] mov %rsp,%rbp push $0 // auxv[1][1] push $0 // auxv[1][0] mov (rArgv),%rax add rBane,%rax push %rax // auxv[0][1] push $31 // auxv[0][0] AT_EXECFN push $0 // envp[1] push %rbp // envp[0] push $0 // argv[argc] NULL lea -8(rArgv,rArgc,8),%rsi // push rest of argv, & mov rArgc,%rcx // adjust pointers to point to std // negative space 2: lodsq add rBane,%rax push %rax loop 2b cld push rArgc // argc pushpop _HOSTMETAL,%rcx // sets __hostos in crt.S xor %ebp,%ebp xor %eax,%eax xor %ebx,%ebx xor %edx,%edx xor %edi,%edi xor %esi,%esi xor %r8d,%r8d xor %r9d,%r9d xor %r10d,%r10d xor %r11d,%r11d xor %r12d,%r12d xor %r13d,%r13d xor %r14d,%r14d xor %r15d,%r15d push $GDT_LONG_CODE .weak _start push $_start lretq .endfn _EfiPostboot,globl,hidden .rodata .Lenv0: .asciz "METAL=1" .bss .space 0x1000 .Ltmpstk: .previous .weak __executable_start .weak _end
4,470
130
jart/cosmopolitan
false
cosmopolitan/libc/runtime/symbols.c
/*-*- 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 2022 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/dce.h" #include "libc/intrin/weaken.h" #include "libc/log/backtrace.internal.h" #include "libc/log/log.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" void __init_symbols(void) { if (__strace > 0 || (IsAsan() && _weaken(__die))) { GetSymbolTable(); } }
2,145
31
jart/cosmopolitan
false
cosmopolitan/libc/runtime/cosmo2.c
/*-*- 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/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/rdtsc.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/runtime/syslib.internal.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/sig.h" #include "libc/thread/thread.h" #include "libc/thread/tls.h" #ifndef __x86_64__ /** * @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); extern long syscon_start[]; extern long syscon_end[]; extern char syscon_linux[]; extern char syscon_xnu[]; extern char syscon_freebsd[]; extern char syscon_openbsd[]; extern char syscon_netbsd[]; extern char syscon_windows[]; extern init_f __strace_init; extern init_f *__preinit_array_start[] __attribute__((__weak__)); extern init_f *__preinit_array_end[] __attribute__((__weak__)); extern init_f *__init_array_start[] __attribute__((__weak__)); extern init_f *__init_array_end[] __attribute__((__weak__)); extern char ape_stack_vaddr[] __attribute__((__weak__)); extern char ape_stack_memsz[] __attribute__((__weak__)); extern char ape_stack_prot[] __attribute__((__weak__)); extern pthread_mutex_t __mmi_lock_obj; extern int hostos asm("__hostos"); 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; } textstartup void cosmo(long *sp, struct Syslib *m1) { int argc; long *mp; init_f **fp; uintptr_t *pp; unsigned long *auxv; char **argv, **envp, *magnums; // get startup timestamp as early as possible // its used by --strace and also kprintf() %T kStartTsc = rdtsc(); // extracts arguments from old sysv stack abi argc = *sp; argv = (char **)(sp + 1); envp = (char **)(sp + 1 + argc + 1); auxv = (unsigned long *)(sp + 1 + argc + 1); while (*auxv++) donothing; // detect apple m1 environment if (SupportsXnu() && (__syslib = m1)) { hostos = _HOSTXNU; magnums = syscon_xnu; } else if (SupportsLinux()) { hostos = _HOSTLINUX; magnums = syscon_linux; } else { notpossible; } // setup system magic numbers for (mp = syscon_start; mp < syscon_end; ++mp) { magnums = DecodeMagnum(magnums, mp); } // check system call abi compatibility if (SupportsXnu() && __syslib && __syslib->version < SYSLIB_VERSION) { 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); } // needed by kisdangerous() __oldstack = (intptr_t)sp; __pid = sys_getpid().ax; // initialize memory manager _mmi.n = ARRAYLEN(_mmi.s); _mmi.p = _mmi.s; __mmi_lock_obj._type = PTHREAD_MUTEX_RECURSIVE; // record provided stack to memory manager _mmi.i = 1; _mmi.p->x = (uintptr_t)GetStackAddr() >> 16; _mmi.p->y = (uintptr_t)(GetStackAddr() + (GetStackSize() - FRAMESIZE)) >> 16; _mmi.p->size = GetStackSize(); _mmi.p->prot = PROT_READ | PROT_WRITE; #if 0 #if IsAsan() // TODO(jart): Figure out ASAN data model on AARCH64. __asan_init(argc, argv, envp, auxv); #endif #endif // initialize file system InitializeFileDescriptors(); // set helpful globals __argc = argc; __argv = argv; __envp = envp; __auxv = auxv; environ = envp; if (argc) program_invocation_name = argv[0]; // run initialization callbacks _init(); __enable_tls(); #ifdef SYSDEBUG argc = __strace_init(argc, argv, envp, auxv); #endif for (fp = __init_array_end; fp-- > __init_array_start;) { (*fp)(argc, argv, envp, auxv); } // run program if (!IsTiny()) __wipe(0); exit(main(argc, argv, envp)); } #endif /* __x86_64__ */
5,934
173
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mmap.c
/*-*- 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/assert.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/asancodes.h" #include "libc/intrin/bits.h" #include "libc/intrin/bsr.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/directmap.internal.h" #include "libc/intrin/likely.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/limits.h" #include "libc/log/backtrace.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/nt/process.h" #include "libc/nt/runtime.h" #include "libc/nt/struct/processmemorycounters.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/stdio/rand.h" #include "libc/str/str.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" #include "libc/thread/thread.h" #include "libc/zipos/zipos.internal.h" #define MAP_ANONYMOUS_linux 0x00000020 #define MAP_ANONYMOUS_openbsd 0x00001000 #define MAP_GROWSDOWN_linux 0x00000100 #define MAP_STACK_freebsd 0x00000400 #define MAP_STACK_openbsd 0x00004000 #define IP(X) (intptr_t)(X) #define VIP(X) (void *)IP(X) #define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1))) #define SHADE(x) (((intptr_t)(x) >> 3) + 0x7fff8000) #define FRAME(x) ((int)((intptr_t)(x) >> 16)) static pureconst unsigned long RoundDownTwoPow(unsigned long x) { return x ? 1ul << _bsrl(x) : 0; } static wontreturn void OnUnrecoverableMmapError(const char *s) { if (_weaken(__die)) _weaken(__die)(); STRACE("%s %m", s); _Exitr(199); } static noasan inline bool OverlapsExistingMapping(char *p, size_t n) { int a, b, i; _unassert(n > 0); a = FRAME(p); b = FRAME(p + (n - 1)); i = FindMemoryInterval(&_mmi, a); if (i < _mmi.i) { if (a <= _mmi.p[i].x && _mmi.p[i].x <= b) return true; if (a <= _mmi.p[i].y && _mmi.p[i].y <= b) return true; if (_mmi.p[i].x <= a && b <= _mmi.p[i].y) return true; } return false; } static noasan bool ChooseMemoryInterval(int x, int n, int align, int *res) { // TODO: improve performance int i, start, end; _unassert(align > 0); if (_mmi.i) { // find the start of the automap memory region i = FindMemoryInterval(&_mmi, x); if (i < _mmi.i) { // check to see if there's space available before the first entry if (!__builtin_add_overflow(x, align - 1, &start)) { start &= -align; if (!__builtin_add_overflow(start, n - 1, &end)) { if (end < _mmi.p[i].x) { *res = start; return true; } } } // check to see if there's space available between two entries while (++i < _mmi.i) { if (!__builtin_add_overflow(_mmi.p[i - 1].y, 1, &start) && !__builtin_add_overflow(start, align - 1, &start)) { start &= -align; if (!__builtin_add_overflow(start, n - 1, &end)) { if (end < _mmi.p[i].x) { *res = start; return true; } } } } } // otherwise append after the last entry if space is available if (!__builtin_add_overflow(_mmi.p[i - 1].y, 1, &start) && !__builtin_add_overflow(start, align - 1, &start)) { start &= -align; if (!__builtin_add_overflow(start, n - 1, &end)) { *res = start; return true; } } } else { // if memtrack is empty, then just assign the requested address // assuming it doesn't overflow if (!__builtin_add_overflow(x, align - 1, &start)) { start &= -align; if (!__builtin_add_overflow(start, n - 1, &end)) { *res = start; return true; } } } return false; } noasan static bool Automap(int count, int align, int *res) { return ChooseMemoryInterval(FRAME(kAutomapStart), count, align, res) && *res + count <= FRAME(kAutomapStart + (kAutomapSize - 1)); } static noasan void *FinishMemory(void *addr, size_t size, int prot, int flags, int fd, int64_t off, int f, int x, int n, struct DirectMap dm) { if (!IsWindows() && (flags & MAP_FIXED)) { if (UntrackMemoryIntervals(addr, size)) { OnUnrecoverableMmapError("FIXED UNTRACK FAILED"); } } if (TrackMemoryInterval(&_mmi, x, x + (n - 1), dm.maphandle, prot, flags, false, false, off, size)) { if (sys_munmap(addr, n) == -1) { OnUnrecoverableMmapError("TRACK MUNMAP FAILED"); } return MAP_FAILED; } if (_weaken(__asan_map_shadow) && !OverlapsShadowSpace(addr, size)) { _weaken(__asan_map_shadow)((intptr_t)addr, size); } return addr; } static noasan void *MapMemory(void *addr, size_t size, int prot, int flags, int fd, int64_t off, int f, int x, int n) { struct DirectMap dm; dm = sys_mmap(addr, size, prot, f, fd, off); if (VERY_UNLIKELY(dm.addr == MAP_FAILED)) { if (IsWindows() && (flags & MAP_FIXED)) { OnUnrecoverableMmapError( "can't recover from MAP_FIXED errors on Windows"); } return MAP_FAILED; } if (VERY_UNLIKELY(dm.addr != addr)) { OnUnrecoverableMmapError("KERNEL DIDN'T RESPECT MAP_FIXED"); } return FinishMemory(addr, size, prot, flags, fd, off, f, x, n, dm); } /** * Maps memory from system, one frame at a time. * * This is useful on Windows since it allows us to partially unmap or * punch holes into existing mappings. */ static textwindows dontinline noasan void *MapMemories(char *addr, size_t size, int prot, int flags, int fd, int64_t off, int f, int x, int n) { size_t i, m; int64_t oi, sz; struct DirectMap dm; bool iscow, readonlyfile; m = (size_t)(n - 1) << 16; _unassert(m < size); _unassert(m + FRAMESIZE >= size); oi = fd == -1 ? 0 : off + m; sz = size - m; dm = sys_mmap(addr + m, sz, prot, f, fd, oi); if (dm.addr == MAP_FAILED) return MAP_FAILED; iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1; readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 && (g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY; if (TrackMemoryInterval(&_mmi, x + (n - 1), x + (n - 1), dm.maphandle, prot, flags, readonlyfile, iscow, oi, sz) == -1) { OnUnrecoverableMmapError("MapMemories unrecoverable #1"); } for (i = 0; i < m; i += FRAMESIZE) { oi = fd == -1 ? 0 : off + i; sz = FRAMESIZE; dm = sys_mmap(addr + i, sz, prot, f, fd, oi); if (dm.addr == MAP_FAILED || TrackMemoryInterval(&_mmi, x + i / FRAMESIZE, x + i / FRAMESIZE, dm.maphandle, prot, flags, readonlyfile, iscow, oi, sz) == -1) { OnUnrecoverableMmapError("MapMemories unrecoverable #2"); } } if (_weaken(__asan_map_shadow) && !OverlapsShadowSpace(addr, size)) { _weaken(__asan_map_shadow)((intptr_t)addr, size); } return addr; } noasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) { char *p = addr; struct DirectMap dm; int a, b, i, f, m, n, x; bool needguard, clashes; size_t virtualused, virtualneed; if (VERY_UNLIKELY(!size)) { STRACE("can't mmap zero bytes"); return VIP(einval()); } if (VERY_UNLIKELY(!ALIGNED(p))) { STRACE("cosmo mmap is 64kb aligned"); return VIP(einval()); } if (VERY_UNLIKELY(!IsLegalSize(size))) { STRACE("mmap size isn't legal"); return VIP(einval()); } if (VERY_UNLIKELY(!IsLegalPointer(p))) { STRACE("mmap addr isn't 48-bit"); return VIP(einval()); } if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE)) { flags = MAP_SHARED; // cf. MAP_SHARED_VALIDATE } if (flags & MAP_ANONYMOUS) { fd = -1; off = 0; size = ROUNDUP(size, FRAMESIZE); if (IsWindows()) prot |= PROT_WRITE; // kludge if ((flags & MAP_TYPE) == MAP_FILE) { STRACE("need MAP_PRIVATE or MAP_SHARED"); return VIP(einval()); } } else if (__isfdkind(fd, kFdZip)) { STRACE("mmap fd is zipos handle"); return VIP(einval()); } else if (VERY_UNLIKELY(off < 0)) { STRACE("mmap negative offset"); return VIP(einval()); } else if (VERY_UNLIKELY(!ALIGNED(off))) { STRACE("mmap off isn't 64kb aligned"); return VIP(einval()); } else if (VERY_UNLIKELY(INT64_MAX - size < off)) { STRACE("mmap too large"); return VIP(einval()); } if (__virtualmax < LONG_MAX && (__builtin_add_overflow((virtualused = GetMemtrackSize(&_mmi)), size, &virtualneed) || virtualneed > __virtualmax)) { STRACE("mmap %'zu size + %'zu inuse exceeds virtual memory limit %'zu", size, virtualused, __virtualmax); return VIP(enomem()); } clashes = OverlapsImageSpace(p, size) || OverlapsExistingMapping(p, size); if ((flags & MAP_FIXED_NOREPLACE) == MAP_FIXED_NOREPLACE && clashes) { STRACE("mmap noreplace overlaps existing"); return VIP(eexist()); } if (__builtin_add_overflow((int)(size >> 16), (int)!!(size & (FRAMESIZE - 1)), &n)) { STRACE("mmap range overflows"); return VIP(einval()); } a = MAX(1, RoundDownTwoPow(size) >> 16); f = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; if (flags & MAP_FIXED) { x = FRAME(p); if (IsWindows()) { if (UntrackMemoryIntervals(p, size)) { OnUnrecoverableMmapError("FIXED UNTRACK FAILED"); } } } else if (p && !clashes && !OverlapsArenaSpace(p, size) && !OverlapsShadowSpace(p, size)) { x = FRAME(p); } else if (!Automap(n, a, &x)) { STRACE("automap has no room for %d frames with %d alignment", n, a); return VIP(enomem()); } needguard = false; p = (char *)ADDR_32_TO_48(x); if ((f & MAP_TYPE) == MAP_STACK) { if (~f & MAP_ANONYMOUS) { STRACE("MAP_STACK must be anonymous"); return VIP(einval()); } f &= ~MAP_TYPE; f |= MAP_PRIVATE; if (IsOpenbsd()) { // openbsd:dubstack // on openbsd this is less about scalability of threads, and more // about defining the legal intervals for the RSP register. sadly // openbsd doesn't let us create a new fixed stack mapping. but.. // openbsd does allow us to overwrite existing fixed mappings, to // authorize its usage as a stack. if (sys_mmap(p, size, prot, f, fd, off).addr == MAP_FAILED) { return MAP_FAILED; } f |= MAP_STACK_openbsd; needguard = true; } else if (IsLinux()) { // make sure there's no existing stuff existing between our stack // starting page and the bottom guard page, since that would stop // our stack page from growing down. _npassert(!sys_munmap(p, size)); // by default MAP_GROWSDOWN will auto-allocate 10mb of pages. it's // supposed to stop growing if an adjacent allocation exists, to // prevent your stacks from overlapping on each other. we're not // able to easily assume a mapping beneath this one exists. even // if we could, the linux kernel requires for muh security reasons // that stacks be at least 1mb away from each other, so it's not // possible to avoid this call if our goal is to have 60kb stacks // with 4kb guards like a sane multithreaded production system. // however this 1mb behavior oddly enough is smart enough to not // apply if the mapping is a manually-created guard page. int e = errno; if ((dm = sys_mmap(p + size - GUARDSIZE, GUARDSIZE, prot, f | MAP_GROWSDOWN_linux, fd, off)) .addr != MAP_FAILED) { _npassert(sys_mmap(p, GUARDSIZE, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) .addr == p); dm.addr = p; return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm); } else if (errno == ENOTSUP) { // WSL doesn't support MAP_GROWSDOWN needguard = true; errno = e; } } else { if (IsFreebsd()) { f |= MAP_STACK_freebsd; } needguard = true; } } if (!IsWindows()) { p = MapMemory(p, size, prot, flags, fd, off, f, x, n); } else { p = MapMemories(p, size, prot, flags, fd, off, f, x, n); } if (p != MAP_FAILED) { if (needguard) { if (!IsWindows()) { // make windows fork() code simpler mprotect(p, GUARDSIZE, PROT_NONE); } if (IsAsan()) { __repstosb((void *)(((intptr_t)p >> 3) + 0x7fff8000), kAsanStackOverflow, GUARDSIZE / 8); } } } return p; } /** * Beseeches system for page-table entries, e.g. * * char *m; * m = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE, * MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); * munmap(m, FRAMESIZE); * * @param addr should be 0 to let your memory manager choose address; * unless MAP_FIXED or MAP_FIXED_NOREPLACE are specified in flags * in which case this function will do precicely as you ask, even * if p=0 (in which you need -fno-delete-null-pointer-checks); it * needs to be 64kb aligned because it's a wise choice that sadly * needs to be made mandatory because of Windows although you can * use __sys_mmap() to circumvent it on System Five in which case * runtime support services, e.g. asan memory safety, could break * @param size must be >0 and needn't be a multiple of FRAMESIZE, but * will be rounded up to FRAMESIZE automatically if MAP_ANONYMOUS * is specified * @param prot can have PROT_READ/PROT_WRITE/PROT_EXEC/PROT_NONE/etc. * @param flags should have one of the following masked by `MAP_TYPE` * - `MAP_FILE` in which case `MAP_ANONYMOUS` shouldn't be used * - `MAP_PRIVATE` for copy-on-write behavior of writeable pages * - `MAP_SHARED` to create shared memory between processes * - `MAP_STACK` to create a grows-down alloc, where a guard page * is automatically protected at the bottom: FreeBSD's behavior * is polyfilled across platforms; uses MAP_GROWSDOWN on Linux * too for extra oomph (do not use MAP_GROWSDOWN!) and this is * completely mandatory on OpenBSD but helps perf elsewhere if * you need to create 10,000 threads. This flag is the reason * why `STACK_FRAME_UNLIMITED` toil is important, because this * only allocates a 4096-byte guard page, thus we need the GCC * compile-time checks to ensure some char[8192] vars will not * create an undetectable overflow into another thread's stack * Your `flags` may optionally bitwise or any of the following: * - `MAP_ANONYMOUS` in which case `fd` and `off` are ignored * - `MAP_FIXED` in which case `addr` becomes more than a hint * - `MAP_FIXED_NOREPLACE` to protect existing mappings; this is * always polyfilled by mmap() which tracks its own memory and * removed before passing to the kernel, in order to support * old versions; if you believe mappings exist which only the * kernel knows, then this flag may be passed to sys_mmap() on * Linux 4.17+ and FreeBSD (where it has multiple bits) * - `MAP_CONCEAL` is FreeBSD/NetBSD/OpenBSD-only * - `MAP_NORESERVE` is Linux/XNU/NetBSD-only * - `MAP_POPULATE` is Linux/FreeBSD-only * - `MAP_NONBLOCK` is Linux-only * - `MAP_NOSYNC` is FreeBSD-only * - `MAP_INHERIT` is NetBSD-only * - `MAP_LOCKED` is Linux-only * @param fd is an open()'d file descriptor, whose contents shall be * made available w/ automatic reading at the chosen address * @param off specifies absolute byte index of fd's file for mapping, * should be zero if MAP_ANONYMOUS is specified, and sadly needs * to be 64kb aligned too * @return virtual base address of new mapping, or MAP_FAILED w/ errno */ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) { void *res; size_t toto = 0; #if defined(SYSDEBUG) && (_KERNTRACE || _NTTRACE) if (IsWindows()) { STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → ...", addr, size, DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off); } #endif __mmi_lock(); if (!__isfdkind(fd, kFdZip)) { res = _Mmap(addr, size, prot, flags, fd, off); } else { res = _weaken(__zipos_Mmap)( addr, size, prot, flags, (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, off); } #if SYSDEBUG toto = __strace > 0 ? GetMemtrackSize(&_mmi) : 0; #endif __mmi_unlock(); STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → %p% m (%'zu bytes total)", addr, size, DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off, res, toto); return res; }
19,079
496
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getinterpreterexecutablename.c
/*-*- 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 2021 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/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/at.h" #include "libc/sysv/errfuns.h" /** * Returns path of executable interpreter. * * Unlike `program_executable_name` which is designed to figure out the * absolute path of the first argument passed to `execve()`, what we do * here is probe things like `/proc` and `sysctl()` to figure out if we * were launched by something like `ape-loader`, and then we return its * path. If we can't determine that path, possibly because we're on XNU * or OpenBSD, then we return -1 with an error code. * * @param p receives utf8 output * @param n is byte size of res buffer * @return p on success or null w/ errno if out of buf or error * @see program_invocation_short_name * @see program_invocation_name * @see program_executable_name */ char *GetInterpreterExecutableName(char *p, size_t n) { int e; size_t m; int cmd[4]; ssize_t rc; char *r, *t; e = errno; if (n < 2) { errno = ENAMETOOLONG; } else if (IsWindows() || IsXnu()) { // TODO(jart): Does XNU guarantee argv[0] is legit? // Otherwise we should return NULL. // What about OpenBSD? if (strlen(GetProgramExecutableName()) < n) { strcpy(p, GetProgramExecutableName()); return p; } errno = ENAMETOOLONG; } else if ((rc = sys_readlinkat(AT_FDCWD, "/proc/self/exe", p, n - 1)) > 0) { p[rc] = 0; return p; } else if ((rc = sys_readlinkat(AT_FDCWD, "/proc/curproc/file", p, n - 1)) > 0) { errno = e; p[rc] = 0; return p; } else if (IsFreebsd() || IsNetbsd()) { cmd[0] = 1; // CTL_KERN cmd[1] = 14; // KERN_PROC if (IsFreebsd()) { // cmd[2] = 12; // KERN_PROC_PATHNAME } else { // cmd[2] = 5; // KERN_PROC_PATHNAME } // cmd[3] = -1; // current process if (sys_sysctl(cmd, ARRAYLEN(cmd), p, &n, 0, 0) != -1) { errno = e; return p; } } return 0; }
4,027
88
jart/cosmopolitan
false
cosmopolitan/libc/runtime/fork.c
/*-*- 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/assert.h" #include "libc/calls/blocksigs.internal.h" #include "libc/calls/calls.h" #include "libc/calls/struct/sigset.h" #include "libc/calls/struct/sigset.internal.h" #include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/intrin/atomic.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/nt/process.h" #include "libc/runtime/internal.h" #include "libc/sysv/consts/sig.h" #include "libc/thread/posixthread.internal.h" #include "libc/thread/tls.h" int _fork(uint32_t dwCreationFlags) { struct CosmoTib *tib; struct PosixThread *pt; int ax, dx, tid, parent; BLOCK_SIGNALS; if (__threaded && _weaken(_pthread_onfork_prepare)) { _weaken(_pthread_onfork_prepare)(); } if (!IsWindows()) { ax = sys_fork(); } else { ax = sys_fork_nt(dwCreationFlags); } if (!ax) { if (!IsWindows()) { dx = sys_getpid().ax; } else { dx = GetCurrentProcessId(); } parent = __pid; __pid = dx; if (__tls_enabled) { tib = __get_tls(); tid = IsLinux() || IsXnuSilicon() ? dx : sys_gettid(); atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed); if ((pt = (struct PosixThread *)tib->tib_pthread)) { atomic_store_explicit(&pt->ptid, tid, memory_order_relaxed); } } if (__threaded && _weaken(_pthread_onfork_child)) { _weaken(_pthread_onfork_child)(); } STRACE("fork() → 0 (child of %d)", parent); } else { if (__threaded && _weaken(_pthread_onfork_parent)) { _weaken(_pthread_onfork_parent)(); } STRACE("fork() → %d% m", ax); } ALLOW_SIGNALS; return ax; } /** * Creates new process. * * @return 0 to child, child pid to parent, or -1 w/ errno * @raise EAGAIN if `RLIMIT_NPROC` was exceeded or system lacked resources * @raise ENOMEM if we require more vespene gas * @asyncsignalsafe * @threadsafe */ int fork(void) { return _fork(0); }
3,847
91
jart/cosmopolitan
false
cosmopolitan/libc/runtime/jmpstack.S
/*-*- 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 // @noreturn _jmpstack: mov %rdi,%rsp mov %rsi,%rax mov %rdx,%rdi mov %rcx,%rsi mov %r8,%rdx mov %r9,%rcx xor %ebp,%ebp call *%rax .unreachable .endfn _jmpstack,globl,hidden
2,236
38
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getdosargv.c
/*-*- 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/intrin/bits.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/tpenc.h" #include "libc/nt/thunk/msabi.h" #include "libc/runtime/internal.h" #include "libc/str/str.h" #include "libc/str/utf16.h" struct DosArgv { const char16_t *s; char *p; char *pe; wint_t wc; }; textwindows noasan void DecodeDosArgv(int ignore, struct DosArgv *st) { wint_t x, y; for (;;) { if (!(x = *st->s++)) break; if (!IsUcs2(x)) { if ((y = *st->s++)) { x = MergeUtf16(x, y); } else { x = 0; } } break; } st->wc = x; } static textwindows noasan void AppendDosArgv(wint_t wc, struct DosArgv *st) { uint64_t w; w = _tpenc(wc); do { if (st->p >= st->pe) break; *st->p++ = w & 0xff; } while (w >>= 8); } static textwindows noasan int Count(int c, struct DosArgv *st) { int ignore, n = 0; asm("" : "=g"(ignore)); while (st->wc == c) { DecodeDosArgv(ignore, st); n++; } return n; } // Tokenizes and transcodes Windows NT CLI args, thus avoiding // CommandLineToArgv() schlepping in forty megs of dependencies. // // @param s is the command line string provided by the executive // @param buf is where we'll store double-NUL-terminated decoded args // @param size is how many bytes are available in buf // @param argv is where we'll store the decoded arg pointer array, which // is guaranteed to be NULL-terminated if max>0 // @param max specifies the item capacity of argv, or 0 to do scanning // @return number of args written, excluding the NULL-terminator; or, // if the output buffer wasn't passed, or was too short, then the // number of args that *would* have been written is returned; and // there are currently no failure conditions that would have this // return -1 since it doesn't do system calls // @see test/libc/dosarg_test.c // @see libc/runtime/ntspawn.c // @note kudos to Simon Tatham for figuring out quoting behavior textwindows noasan int GetDosArgv(const char16_t *cmdline, char *buf, size_t size, char **argv, size_t max) { bool inquote; int i, argc, slashes, quotes, ignore; static struct DosArgv st_; struct DosArgv *st = &st_; asm("" : "=g"(ignore)); asm("" : "+r"(st)); st->s = cmdline; st->p = buf; st->pe = buf + size; argc = 0; DecodeDosArgv(ignore, st); while (st->wc) { while (st->wc && (st->wc == ' ' || st->wc == '\t')) { DecodeDosArgv(ignore, st); } if (!st->wc) break; if (++argc < max) { argv[argc - 1] = st->p < st->pe ? st->p : NULL; } inquote = false; while (st->wc) { if (!inquote && (st->wc == ' ' || st->wc == '\t')) break; if (st->wc == '"' || st->wc == '\\') { slashes = Count('\\', st); quotes = Count('"', st); if (!quotes) { while (slashes--) { AppendDosArgv('\\', st); } } else { while (slashes >= 2) { AppendDosArgv('\\', st); slashes -= 2; } if (slashes) { AppendDosArgv('"', st); quotes--; } if (quotes > 0) { if (!inquote) quotes--; for (i = 3; i <= quotes + 1; i += 3) { AppendDosArgv('"', st); } inquote = (quotes % 3 == 0); } } } else { AppendDosArgv(st->wc, st); DecodeDosArgv(ignore, st); } } AppendDosArgv('\0', st); } AppendDosArgv('\0', st); if (size) buf[min(st->p - buf, size - 1)] = '\0'; if (max) argv[min(argc, max - 1)] = NULL; return argc; }
5,469
146
jart/cosmopolitan
false
cosmopolitan/libc/runtime/finddebugbinary.c
/*-*- 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 "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/elf/def.h" #include "libc/elf/elf.h" #include "libc/elf/struct/ehdr.h" #include "libc/elf/struct/sym.h" #include "libc/errno.h" #include "libc/intrin/bits.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/str/str.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" static bool GetElfSymbolValue(const Elf64_Ehdr *ehdr, size_t esize, const char *name, uint64_t *res) { Elf64_Xword i, n; const char *stab; const Elf64_Sym *st; if ((stab = GetElfStringTable(ehdr, esize)) && (st = GetElfSymbolTable(ehdr, esize, &n))) { for (i = 0; i < n; ++i) { if (!strcmp(stab + st[i].st_name, name)) { *res = st[i].st_value; return true; } } } return false; } static bool IsMyDebugBinaryImpl(const char *path) { int fd; void *map; int64_t size; uintptr_t value; bool res = false; if ((fd = open(path, O_RDONLY | O_CLOEXEC, 0)) != -1) { // sanity test that this .com.dbg file (1) is an elf image, and (2) // contains the same number of bytes of code as our .com executable // which is currently running in memory. if ((size = lseek(fd, 0, SEEK_END)) != -1 && (map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) { if (GetElfSymbolValue(map, size, "_etext", &value)) { res = !_etext || value == (uintptr_t)_etext; } munmap(map, size); } close(fd); } return res; } static bool IsMyDebugBinary(const char *path) { int e; bool res; e = errno; res = IsMyDebugBinaryImpl(path); errno = e; return res; } /** * Returns path of binary with the debug information, or null. * * @return path to debug binary, or NULL */ const char *FindDebugBinary(void) { static bool once; static char *res; static char buf[PATH_MAX]; char *p; size_t n; if (!once) { p = GetProgramExecutableName(); n = strlen(p); if (n > 4 && READ32LE(p + n - 4) == READ32LE(".dbg")) { res = p; } else if (n > 4 && READ32LE(p + n - 4) == READ32LE(".com") && n + 4 < ARRAYLEN(buf)) { mempcpy(mempcpy(buf, p, n), ".dbg", 5); if (IsMyDebugBinary(buf)) { res = buf; } } else if (n + 8 < ARRAYLEN(buf)) { mempcpy(mempcpy(buf, p, n), ".com.dbg", 9); if (IsMyDebugBinary(buf)) { res = buf; } } if (!res) { res = getenv("COMDBG"); } once = true; } return res; }
4,463
118
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mman.internal.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_MMAN_H_ #define COSMOPOLITAN_LIBC_RUNTIME_MMAN_H_ #include "libc/runtime/e820.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct mman { int64_t pdp; /* 0x0500 */ int32_t pdpi; /* 0x0508 */ int32_t e820n; /* 0x050c */ uint64_t frp; /* 0x0510 — free list of reclaimed, previously used pages */ struct SmapEntry e820[256]; /* 0x0518 */ struct SmapEntry e820_end[0]; /* 0x1d18 */ char pc_drive_base_table[11]; /* 0x1d18 */ unsigned char pc_drive_type; /* 0x1d23 */ unsigned char pc_drive_last_sector; /* 0x1d24 */ unsigned short pc_drive_last_cylinder; /* 0x1d26 */ unsigned char pc_drives_attached; /* 0x1d28 */ unsigned char pc_drive_last_head; /* 0x1d29 */ unsigned char pc_drive; /* 0x1d2a */ char bad_idt[2]; /* 0x1d2b — IDTR value for an invalid IDT; really 6 bytes which overlap w/ fields below */ unsigned char pc_drive_next_sector; /* 0x1d2d */ unsigned short pc_drive_next_cylinder; /* 0x1d2e */ unsigned char pc_drive_next_head; /* 0x1d30 */ unsigned char pc_video_type; /* 0x1d31 */ unsigned short pc_video_stride; /* 0x1d32 — line width, including any invisible "pixels" — in bytes (NOTE) */ unsigned short pc_video_width; /* 0x1d34 — width in chars. (text) or pixels (graphics) */ unsigned short pc_video_height; /* 0x1d36 — height in chars. (text) or pixels (graphics) */ uint64_t pc_video_framebuffer; /* 0x1d38 — physical address of video frame buffer */ uint64_t pc_video_framebuffer_size; /* 0x1d40 */ struct { /* 0x1d48 — starting cursor pos. */ unsigned short y, x; } pc_video_curs_info; unsigned short pc_video_char_height; /* 0x1d4c — character height (useful for setting cursor shape in text mode) */ }; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ /* Values for mman::pc_video_type. */ #define PC_VIDEO_TEXT 0 #define PC_VIDEO_BGR565 1 #define PC_VIDEO_BGR555 2 #define PC_VIDEO_BGRX8888 3 #define PC_VIDEO_RGBX8888 4 #endif /* COSMOPOLITAN_LIBC_RUNTIME_MMAN_H_ */
2,852
58
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getmemtracksize.c
/*-*- 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 2022 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/runtime/memtrack.internal.h" noasan size_t GetMemtrackSize(struct MemoryIntervals *mm) { size_t i, n; for (n = i = 0; i < mm->i; ++i) { n += ((size_t)(mm->p[i].y - mm->p[i].x) + 1) << 16; } return n; }
2,066
28
jart/cosmopolitan
false
cosmopolitan/libc/runtime/hook.greg.c
/*-*- 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 "ape/sections.internal.h" #include "libc/calls/struct/sigset.h" #include "libc/runtime/morph.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" /** * Rewrites code in memory to hook function calls. * * We do this by searching each function for the nop instruction * inserted by GCC when we use the -pg -mnop-mcount flags. There's no * risk of corrupting data since the linker scripts won't mix code and * data. * * Modules built with -O3 and without the profiling flags might have * these same nop instructions, but that shouldn't be problematic since * they're only there for the puposes of aligning jumps, and therefore * aren't actually executed. However codebases that use huge function * alignments with wide-nop slides could pose minor issues. Further note * that Cosmopolitan sources are almost never intentionally written to * use code alignment, since we've only seen a few cases where it helps. * * @see ape/ape.lds */ privileged noinstrument noasan int __hook(void *ifunc, struct SymbolTable *symbols) { int rc; size_t i; char *p, *pe; intptr_t addr; sigset_t mask; uint64_t code, mcode; intptr_t kMcount = (intptr_t)&mcount; intptr_t kProgramCodeStart = (intptr_t)_ereal; intptr_t kPrivilegedStart = (intptr_t)__privileged_addr; if (!symbols) return -1; __morph_begin(&mask); for (i = 0; i < symbols->count; ++i) { if (symbols->addr_base + symbols->symbols[i].x < kProgramCodeStart) { continue; } if (symbols->addr_base + symbols->symbols[i].y >= kPrivilegedStart) { break; } for (p = (char *)symbols->addr_base + symbols->symbols[i].x, pe = (char *)symbols->addr_base + symbols->symbols[i].y; p + 8 - 1 <= pe; ++p) { code = ((uint64_t)(255 & p[7]) << 070 | (uint64_t)(255 & p[6]) << 060 | (uint64_t)(255 & p[5]) << 050 | (uint64_t)(255 & p[4]) << 040 | (uint64_t)(255 & p[3]) << 030 | (uint64_t)(255 & p[2]) << 020 | (uint64_t)(255 & p[1]) << 010 | (uint64_t)(255 & p[0]) << 000); /* * Test for -mrecord-mcount (w/ -fpie or -fpic) * * nopw 0x00(%rax,%rax,1) ← morphed by package.com * call *mcount(%rip) ← linked w/o -static * addr32 call mcount ← relaxed w/ -static * addr32 call mcount ← relaxed w/ -static * * Note that gcc refuses to insert the six byte nop. */ if ((code & 0x0000FFFFFFFFFFFF) == 0x0000441F0F66 || (code & 0x0000FFFFFFFFFFFF) == ((((kMcount - ((intptr_t)&p[2] + 4)) << 16) | 0xE867) & 0x0000FFFFFFFFFFFF) || (code & 0x0000FFFFFFFFFFFF) == ((((kMcount - ((intptr_t)&p[2] + 4)) << 16) | 0xFF15) & 0x0000FFFFFFFFFFFF)) { p[0] = 0x67; p[1] = 0xE8; addr = (intptr_t)ifunc - ((intptr_t)&p[2] + 4); p[2] = (addr & 0x000000ff) >> 000; p[3] = (addr & 0x0000ff00) >> 010; p[4] = (addr & 0x00ff0000) >> 020; p[5] = (addr & 0xff000000) >> 030; break; } /* * Test for -mnop-mcount (w/ -fno-pie) */ mcode = code & 0x000000FFFFFFFFFF; if ((mcode == 0x00441F0F /* nopl 0x00(%eax,%eax,1) [canonical] */) || (mcode == 0x00041F0F67 /* nopl (%eax,%eax,1) [older gcc] */)) { if (p[-1] != 0x66 /* nopw 0x0(%rax,%rax,1) [donotwant] */) { p[0] = 0xE8 /* call Jvds */; addr = (intptr_t)ifunc - ((intptr_t)&p[1] + 4); p[1] = (addr & 0x000000ff) >> 000; p[2] = (addr & 0x0000ff00) >> 010; p[3] = (addr & 0x00ff0000) >> 020; p[4] = (addr & 0xff000000) >> 030; } break; } } } __morph_end(&mask); return 0; }
5,681
119
jart/cosmopolitan
false
cosmopolitan/libc/runtime/opensymboltable.greg.c
/*-*- 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/assert.h" #include "libc/calls/blockcancel.internal.h" #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/elf/def.h" #include "libc/elf/scalar.h" #include "libc/elf/struct/phdr.h" #include "libc/elf/struct/shdr.h" #include "libc/elf/struct/sym.h" #include "libc/errno.h" #include "libc/intrin/bits.h" #include "libc/intrin/strace.internal.h" #include "libc/limits.h" #include "libc/log/libfatal.internal.h" #include "libc/macros.internal.h" #include "libc/mem/alg.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/str/str.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" #define GetStr(tab, rva) ((char *)(tab) + (rva)) #define GetSection(e, s) ((void *)((intptr_t)(e) + (size_t)(s)->sh_offset)) #define GetShstrtab(e) GetSection(e, GetShdr(e, (e)->e_shstrndx)) #define GetSectionName(e, s) GetStr(GetShstrtab(e), (s)->sh_name) #define GetPhdr(e, i) \ ((Elf64_Phdr *)((intptr_t)(e) + (e)->e_phoff + \ (size_t)(e)->e_phentsize * (i))) #define GetShdr(e, i) \ ((Elf64_Shdr *)((intptr_t)(e) + (e)->e_shoff + \ (size_t)(e)->e_shentsize * (i))) static char *GetStrtab(Elf64_Ehdr *e, size_t *n) { char *name; Elf64_Half i; Elf64_Shdr *shdr; for (i = 0; i < e->e_shnum; ++i) { shdr = GetShdr(e, i); if (shdr->sh_type == SHT_STRTAB) { name = GetSectionName(e, GetShdr(e, i)); if (name && !__strcmp(name, ".strtab")) { if (n) *n = shdr->sh_size; return GetSection(e, shdr); } } } return 0; } static Elf64_Sym *GetSymtab(Elf64_Ehdr *e, Elf64_Xword *n) { Elf64_Half i; Elf64_Shdr *shdr; for (i = e->e_shnum; i > 0; --i) { shdr = GetShdr(e, i - 1); if (shdr->sh_type == SHT_SYMTAB) { if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue; if (n) *n = shdr->sh_size / shdr->sh_entsize; return GetSection(e, shdr); } } return 0; } static void GetImageRange(Elf64_Ehdr *elf, intptr_t *x, intptr_t *y) { unsigned i; Elf64_Phdr *phdr; intptr_t start, end, pstart, pend; start = INTPTR_MAX; end = 0; for (i = 0; i < elf->e_phnum; ++i) { phdr = GetPhdr(elf, i); if (phdr->p_type != PT_LOAD) continue; pstart = phdr->p_vaddr; pend = phdr->p_vaddr + phdr->p_memsz; if (pstart < start) start = pstart; if (pend > end) end = pend; } if (x) *x = start; if (y) *y = end; } static struct SymbolTable *OpenSymbolTableImpl(const char *filename) { int fd; void *map; long *stp; ssize_t filesize; unsigned i, j, x; const Elf64_Ehdr *elf; const char *name_base; struct SymbolTable *t; size_t n, m, tsz, size; const Elf64_Sym *symtab, *sym; ptrdiff_t names_offset, name_base_offset, stp_offset; map = MAP_FAILED; if ((fd = open(filename, O_RDONLY)) == -1) return 0; if ((filesize = getfiledescriptorsize(fd)) == -1) goto SystemError; if (filesize > INT_MAX) goto RaiseE2big; if (filesize < 64) goto RaiseEnoexec; elf = map = mmap(0, filesize, PROT_READ, MAP_PRIVATE, fd, 0); if (map == MAP_FAILED) goto SystemError; if (READ32LE(map) != READ32LE("\177ELF")) goto RaiseEnoexec; if (!(name_base = GetStrtab(map, &m))) goto RaiseEnobufs; if (!(symtab = GetSymtab(map, &n))) goto RaiseEnobufs; tsz = 0; tsz += sizeof(struct SymbolTable); tsz += sizeof(struct Symbol) * n; names_offset = tsz; tsz += sizeof(unsigned) * n; name_base_offset = tsz; tsz += m; tsz = ROUNDUP(tsz, FRAMESIZE); stp_offset = tsz; size = tsz; tsz += sizeof(const Elf64_Sym *) * n; tsz = ROUNDUP(tsz, FRAMESIZE); t = mmap(0, tsz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (t == MAP_FAILED) goto SystemError; t->magic = SYMBOLS_MAGIC; t->abi = SYMBOLS_ABI; t->size = size; t->mapsize = size; t->names_offset = names_offset; t->name_base_offset = name_base_offset; t->names = (uint32_t *)((char *)t + t->names_offset); t->name_base = (char *)((char *)t + t->name_base_offset); GetImageRange(elf, &t->addr_base, &t->addr_end); memcpy(t->name_base, name_base, m); --t->addr_end; stp = (long *)((char *)t + stp_offset); for (m = i = 0; i < n; ++i) { sym = symtab + i; if (!(sym->st_size > 0 && (ELF64_ST_TYPE(sym->st_info) == STT_FUNC || ELF64_ST_TYPE(sym->st_info) == STT_OBJECT))) { continue; } if (sym->st_value > t->addr_end) continue; if (sym->st_value < t->addr_base) continue; x = sym->st_value - t->addr_base; stp[m++] = (unsigned long)x << 32 | i; } _longsort(stp, m); for (j = i = 0; i < m; ++i) { sym = symtab + (stp[i] & 0x7fffffff); x = stp[i] >> 32; if (j && x == t->symbols[j - 1].x) --j; if (j && t->symbols[j - 1].y >= x) t->symbols[j - 1].y = x - 1; t->names[j] = sym->st_name; t->symbols[j].x = x; if (sym->st_size) { t->symbols[j].y = x + sym->st_size - 1; } else { t->symbols[j].y = t->addr_end - t->addr_base; } ++j; } t->count = j; munmap(stp, ROUNDUP(sizeof(const Elf64_Sym *) * n, FRAMESIZE)); munmap(map, filesize); close(fd); return t; RaiseE2big: errno = E2BIG; goto SystemError; RaiseEnobufs: errno = ENOBUFS; goto SystemError; RaiseEnoexec: errno = ENOEXEC; SystemError: STRACE("OpenSymbolTable()% m"); if (map != MAP_FAILED) { munmap(map, filesize); } close(fd); return 0; } /** * Maps debuggable binary into memory and indexes symbol addresses. * * @return object freeable with CloseSymbolTable(), or NULL w/ errno */ struct SymbolTable *OpenSymbolTable(const char *filename) { struct SymbolTable *st; BLOCK_CANCELLATIONS; st = OpenSymbolTableImpl(filename); ALLOW_CANCELLATIONS; return st; }
7,732
212
jart/cosmopolitan
false
cosmopolitan/libc/runtime/efimain.greg.c
/*-*- 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 2021 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 "ape/sections.internal.h" #include "libc/dce.h" #include "libc/intrin/bits.h" #include "libc/intrin/newbie.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/nt/efi.h" #include "libc/nt/thunk/msabi.h" #include "libc/runtime/e820.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/pc.internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #ifdef __x86_64__ /* TODO: Why can't we change CR3? Could it really need PML5T? */ /* TODO: Why does QEMU in UEFI mode take ten seconds to boot? */ struct EfiArgs { char *Args[0x400 / sizeof(char *)]; char ArgBlock[0xC00]; }; static const EFI_GUID kEfiLoadedImageProtocol = LOADED_IMAGE_PROTOCOL; static const EFI_GUID kEfiGraphicsOutputProtocol = GRAPHICS_OUTPUT_PROTOCOL; extern const char vga_console[]; extern void _EfiPostboot(struct mman *, uint64_t *, uintptr_t, char **); static void EfiInitVga(struct mman *mm, EFI_SYSTEM_TABLE *SystemTable) { EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphInfo; EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *GraphMode; EFI_PIXEL_BITMASK *PixelInfo; unsigned vid_typ = PC_VIDEO_TEXT; size_t bytes_per_pix = 0; SystemTable->BootServices->LocateProtocol(&kEfiGraphicsOutputProtocol, NULL, &GraphInfo); GraphMode = GraphInfo->Mode; switch (GraphMode->Info->PixelFormat) { case PixelRedGreenBlueReserved8BitPerColor: vid_typ = PC_VIDEO_RGBX8888; bytes_per_pix = 4; break; case PixelBlueGreenRedReserved8BitPerColor: vid_typ = PC_VIDEO_BGRX8888; bytes_per_pix = 4; break; case PixelBitMask: PixelInfo = &GraphMode->Info->PixelInformation; switch (le32toh(PixelInfo->RedMask)) { case 0x00FF0000U: if (le32toh(PixelInfo->ReservedMask) >= 0x01000000U && le32toh(PixelInfo->GreenMask) == 0x0000FF00U && le32toh(PixelInfo->BlueMask) == 0x000000FFU) { vid_typ = PC_VIDEO_BGRX8888; bytes_per_pix = 4; } break; case 0x000000FFU: if (le32toh(PixelInfo->ReservedMask) >= 0x01000000U && le32toh(PixelInfo->GreenMask) == 0x0000FF00U && le32toh(PixelInfo->BlueMask) == 0x00FF0000U) { vid_typ = PC_VIDEO_RGBX8888; bytes_per_pix = 4; } break; case 0x0000F800U: if (le32toh(PixelInfo->ReservedMask) <= 0x0000FFFFU && le32toh(PixelInfo->GreenMask) == 0x000007E0U && le32toh(PixelInfo->BlueMask) == 0x0000001FU) { vid_typ = PC_VIDEO_BGR565; bytes_per_pix = 2; } break; case 0x00007C00U: if (le32toh(PixelInfo->ReservedMask) <= 0x0000FFFFU && le32toh(PixelInfo->GreenMask) == 0x000003E0U && le32toh(PixelInfo->BlueMask) == 0x0000001FU) { vid_typ = PC_VIDEO_BGR555; bytes_per_pix = 2; } break; } default: notpossible; } if (!bytes_per_pix) notpossible; mm->pc_video_type = vid_typ; mm->pc_video_stride = GraphMode->Info->PixelsPerScanLine * bytes_per_pix; mm->pc_video_width = GraphMode->Info->HorizontalResolution; mm->pc_video_height = GraphMode->Info->VerticalResolution; mm->pc_video_framebuffer = GraphMode->FrameBufferBase; mm->pc_video_framebuffer_size = GraphMode->FrameBufferSize; mm->pc_video_curs_info.y = mm->pc_video_curs_info.x = 0; SystemTable->BootServices->SetMem((void *)GraphMode->FrameBufferBase, GraphMode->FrameBufferSize, 0); } /** * EFI Application Entrypoint. * * This entrypoint is mutually exclusive from WinMain since * Windows apps and EFI apps use the same PE binary format. * So if you want to trade away Windows so that you can use * UEFI instead of the normal BIOS boot process, do this: * * STATIC_YOINK("EfiMain"); * int main() { ... } * * You can use QEMU to test this, but please note that UEFI * goes thousands of times slower than the normal BIOS boot * * qemu-system-x86_64 \ * -bios OVMF.fd \ * -nographic \ * -net none \ * -drive format=raw,file=fat:rw:o/tool/viz * FS0: * deathstar.com * * @see libc/dce.h */ __msabi noasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { int type, x87cw = 0x037f; struct mman *mm; uint32_t DescVersion; uintptr_t i, j, MapSize; struct EfiArgs *ArgBlock; EFI_LOADED_IMAGE *ImgInfo; EFI_MEMORY_DESCRIPTOR *Map, *Desc; uint64_t Address; uintptr_t Args, MapKey, DescSize; uint64_t p, pe, cr4, *m, *pd, *sp, *pml4t, *pdt1, *pdt2, *pdpt1, *pdpt2; /* * Allocates and clears PC-compatible memory and copies image. Marks the * pages as EfiRuntimeServicesData, so that we can simply free up all * EfiLoader... and EfiBootServices... pages later. The first page at * address 0 is normally already allocated as EfiBootServicesData, so * handle it separately. */ Address = 0; SystemTable->BootServices->AllocatePages( AllocateAddress, EfiRuntimeServicesData, 4096 / 4096, &Address); Address = 4096; SystemTable->BootServices->AllocatePages( AllocateAddress, EfiRuntimeServicesData, (IMAGE_BASE_REAL - 4096) / 4096, &Address); Address = 0x79000; SystemTable->BootServices->AllocatePages( AllocateAddress, EfiRuntimeServicesData, (0x7e000 - 0x79000 + sizeof(struct EfiArgs) + 4095) / 4096, &Address); Address = IMAGE_BASE_PHYSICAL; SystemTable->BootServices->AllocatePages( AllocateAddress, EfiRuntimeServicesData, ((_end - __executable_start) + 4095) / 4096, &Address); mm = (struct mman *)0x0500; SystemTable->BootServices->SetMem(mm, sizeof(*mm), 0); SystemTable->BootServices->SetMem( (void *)0x79000, 0x7e000 - 0x79000 + sizeof(struct EfiArgs), 0); SystemTable->BootServices->CopyMem((void *)IMAGE_BASE_PHYSICAL, __executable_start, _end - __executable_start); /* * Converts UEFI shell arguments to argv. */ ArgBlock = (struct EfiArgs *)0x7e000; SystemTable->BootServices->HandleProtocol(ImageHandle, &kEfiLoadedImageProtocol, &ImgInfo); Args = GetDosArgv(ImgInfo->LoadOptions, ArgBlock->ArgBlock, sizeof(ArgBlock->ArgBlock), ArgBlock->Args, ARRAYLEN(ArgBlock->Args)); /* * Gets information about our current video mode. Clears the screen. * TODO: if needed, switch to a video mode that has a linear frame buffer * type we support. */ if (_weaken(vga_console)) EfiInitVga(mm, SystemTable); /* * Asks UEFI which parts of our RAM we're allowed to use. */ Map = NULL; MapSize = 0; SystemTable->BootServices->GetMemoryMap(&MapSize, Map, &MapKey, &DescSize, &DescVersion); SystemTable->BootServices->AllocatePool(EfiLoaderData, MapSize, &Map); MapSize *= 2; SystemTable->BootServices->GetMemoryMap(&MapSize, Map, &MapKey, &DescSize, &DescVersion); for (j = i = 0, Desc = Map; i < MapSize / DescSize; ++i) { switch (Desc->Type) { case EfiLoaderCode: case EfiLoaderData: case EfiBootServicesCode: case EfiBootServicesData: if (Desc->PhysicalStart != 0) break; /* fallthrough */ case EfiConventionalMemory: mm->e820[j].addr = Desc->PhysicalStart; mm->e820[j].size = Desc->NumberOfPages * 4096; mm->e820[j].type = kMemoryUsable; ++j; } Desc = (EFI_MEMORY_DESCRIPTOR *)((char *)Desc + DescSize); } SystemTable->BootServices->FreePool(Map); /* * Sets up page tables. */ pd = (uint64_t *)0x79000; pdt1 = (uint64_t *)0x7b000; pdt2 = (uint64_t *)0x7a000; pdpt1 = (uint64_t *)0x7d000; pdpt2 = (uint64_t *)0x7c000; pml4t = (uint64_t *)0x7e000; for (i = 0; i < 512; ++i) { pd[i] = 0x1000 * i + PAGE_V + PAGE_RSRV + PAGE_RW; } pdt1[0] = (intptr_t)pd + PAGE_V + PAGE_RW; pdt2[0] = (intptr_t)pd + PAGE_V + PAGE_RW; pdpt1[0] = (intptr_t)pdt1 + PAGE_V + PAGE_RW; pdpt2[0] = (intptr_t)pdt2 + PAGE_V + PAGE_RW; pml4t[0] = (intptr_t)pdpt1 + PAGE_V + PAGE_RW; pml4t[256] = (intptr_t)pdpt2 + PAGE_V + PAGE_RW; /* * Asks UEFI to handover control? */ SystemTable->BootServices->ExitBootServices(ImageHandle, MapKey); /* * Switches to copied image and launches program. */ _EfiPostboot(mm, pml4t, Args, ArgBlock->Args); unreachable; } #endif /* __x86_64__ */
10,554
265
jart/cosmopolitan
false
cosmopolitan/libc/runtime/warnifpowersave.c
/*-*- 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 2022 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/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" // RDTSC on Linux has so much jitter when the CPU is in powersave mode. // Causing things like microbenchmarks to have a 1000% margin of error. #define FILE "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" #define WARN \ "warning: this operation isn't reliable in powersave mode. please run:\n\t" \ "echo performance | sudo tee " \ "/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor\n" void __warn_if_powersave(void) { int fd; char buf[16] = {0}; if (!fileexists(FILE)) return; if ((fd = open(FILE, O_RDONLY)) == -1) return; read(fd, buf, 15); close(fd); if (!_startswith(buf, "powersave")) return; write(2, WARN, sizeof(WARN) - 1); }
2,739
43
jart/cosmopolitan
false
cosmopolitan/libc/runtime/ezmap.internal.h
#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_ */
453
18
jart/cosmopolitan
false
cosmopolitan/libc/runtime/utmp.h
#ifndef COSMOPOLITAN_LIBC_RUNTIME_UTMP_H_ #define COSMOPOLITAN_LIBC_RUNTIME_UTMP_H_ #include "libc/calls/weirdtypes.h" #include "libc/runtime/utmpx.h" #define ACCOUNTING 9 #define UT_NAMESIZE 32 #define UT_HOSTSIZE 256 #define UT_LINESIZE 32 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct lastlog { time_t ll_time; char ll_line[UT_LINESIZE]; char ll_host[UT_HOSTSIZE]; }; #define ut_time ut_tv.tv_sec #define ut_name ut_user #define ut_addr ut_addr_v6[0] #define utmp utmpx #define e_exit __e_exit #define e_termination __e_termination int login_tty(int); int utmpname(const char *); struct utmp *getutent(void); struct utmp *getutid(const struct utmp *); struct utmp *getutline(const struct utmp *); struct utmp *pututline(const struct utmp *); void endutent(void); void setutent(void); void updwtmp(const char *, const struct utmp *); #define _PATH_UTMP "/dev/null/utmp" #define _PATH_WTMP "/dev/null/wtmp" #define UTMP_FILE _PATH_UTMP #define WTMP_FILE _PATH_WTMP #define UTMP_FILENAME _PATH_UTMP #define WTMP_FILENAME _PATH_WTMP COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_UTMP_H_ */
1,224
48
jart/cosmopolitan
false
cosmopolitan/libc/runtime/getpagesize.c
/*-*- 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/runtime/runtime.h" /** * Returns granularity of memory manager. * @see sysconf(_SC_PAGE_SIZE) which is portable */ int getpagesize(void) { return FRAMESIZE; }
2,016
28
jart/cosmopolitan
false
cosmopolitan/libc/runtime/mprotect.c
/*-*- 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/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/likely.h" #include "libc/intrin/strace.internal.h" #include "libc/runtime/internal.h" #include "libc/sysv/consts/prot.h" /** * Modifies restrictions on virtual memory address range. * * @param addr needs to be 4kb aligned * @param prot can have PROT_{NONE,READ,WRITE,EXEC,GROWSDOWN,GROWSUP} * @return 0 on success, or -1 w/ errno * @see mmap() */ int mprotect(void *addr, size_t size, int prot) { int64_t rc; if (SupportsWindows() && (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_GROWSDOWN | PROT_GROWSUP))) { errno = EINVAL; // unix checks prot before checking size rc = -1; } else if (!size) { return 0; // make new technology consistent with unix } else if (UNLIKELY((intptr_t)addr & 4095)) { errno = EINVAL; // unix checks prot before checking size rc = -1; } else if (!IsWindows()) { rc = sys_mprotect(addr, size, prot); } else { rc = sys_mprotect_nt(addr, size, prot); } STRACE("mprotect(%p, %'zu, %s) → %d% m", addr, size, DescribeProtFlags(prot), rc); return rc; }
3,082
56
jart/cosmopolitan
false
cosmopolitan/libc/runtime/stackchkfaillocal.c
/*-*- 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/runtime/internal.h" void __stack_chk_fail_local(void) { __stack_chk_fail(); }
1,932
24
jart/cosmopolitan
false
cosmopolitan/libc/str/lz4decode.c
/*-*- 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/nexgen32e/kompressor.h" #include "libc/nexgen32e/lz4.h" #include "libc/str/str.h" /** * Decompresses LZ4 file. * * We assume (1) the file is mmap()'d or was copied into into memory * beforehand; and (2) folks handling untrustworthy data shall place * 64kb of guard pages on the ends of each buffer, see mapanon(). We * don't intend to support XXHASH; we recommend folks needing checks * against data corruption consider crc32c(), or gzip since it's the * best at file recovery. Dictionaries are supported; by convention, * they are passed in the ≤64kb bytes preceding src. * * @return pointer to end of decoded data, similar to mempcpy() * @see _mapanon(), lz4check() */ void *lz4decode(void *dest, const void *src) { const unsigned char *frame, *block; frame = (const unsigned char *)src; for (block = frame + LZ4_FRAME_HEADERSIZE(frame); !LZ4_BLOCK_ISEOF(block); block += LZ4_BLOCK_SIZE(frame, block)) { if (LZ4_BLOCK_ISCOMPRESSED(block)) { dest = lz4cpy(dest, LZ4_BLOCK_DATA(block), LZ4_BLOCK_DATASIZE(block)); } else { dest = mempcpy(dest, LZ4_BLOCK_DATA(block), LZ4_BLOCK_DATASIZE(block)); } } return dest; }
3,022
50
jart/cosmopolitan
false
cosmopolitan/libc/str/getzipcfilecompressedsize.c
/*-*- 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 2021 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/zip.h" /** * Returns compressed size in bytes from zip central directory header. */ uint64_t GetZipCfileCompressedSize(const uint8_t *z) { uint64_t x; const uint8_t *p, *pe; if ((x = ZIP_CFILE_COMPRESSEDSIZE(z)) == 0xFFFFFFFF) { for (p = ZIP_CFILE_EXTRA(z), pe = p + ZIP_CFILE_EXTRASIZE(z); p < pe; p += ZIP_EXTRA_SIZE(p)) { if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 && 8 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) { return READ64LE(ZIP_EXTRA_CONTENT(p) + 8); } } } return x; }
2,381
38
jart/cosmopolitan
false
cosmopolitan/libc/str/ispunct.c
/*-*- 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/str/str.h" /** * Returns nonzero if ``c ∈ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~`` */ int ispunct(int c) { return (0x21 <= c && c <= 0x7E) && !('0' <= c && c <= '9') && !('A' <= c && c <= 'Z') && !('a' <= c && c <= 'z'); }
2,084
28
jart/cosmopolitan
false
cosmopolitan/libc/str/strxfrm_l.c
/*-*- 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 2022 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/str/locale.h" #include "libc/str/str.h" size_t strxfrm_l(char *dest, const char *src, size_t count, locale_t l) { return strxfrm(dest, src, count); }
2,004
25
jart/cosmopolitan
false
cosmopolitan/libc/str/strcat16.c
/*-*- 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/str/str.h" /** * Appends 𝑠 to 𝑑. * * @param 𝑑 is a NUL-terminated 16-bit string buffer * @param 𝑠 is a NUL-terminated 16-bit string * @return 𝑑 * @asyncsignalsafe */ char16_t *strcat16(char16_t *d, const char16_t *s) { strcpy16(d + strlen16(d), s); return d; }
2,139
33
jart/cosmopolitan
false
cosmopolitan/libc/str/iswseparator.c
/*-*- 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 2021 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/str/str.h" static const unsigned short kCodes[][2] = { {0x00aa, 0x00aa}, /* 1x English */ {0x00b2, 0x00b3}, /* 2x English Arabic */ {0x00b5, 0x00b5}, /* 1x Greek */ {0x00b9, 0x00ba}, /* 2x English Arabic */ {0x00bc, 0x00be}, /* 3x Vulgar English Arabic */ {0x00c0, 0x00d6}, /* 23x Watin */ {0x00d8, 0x00f6}, /* 31x Watin */ {0x0100, 0x02c1}, /* 450x Watin-AB,IPA,Spacemod */ {0x02c6, 0x02d1}, /* 12x Spacemod */ {0x02e0, 0x02e4}, /* 5x Spacemod */ {0x02ec, 0x02ec}, /* 1x Spacemod */ {0x02ee, 0x02ee}, /* 1x Spacemod */ {0x0370, 0x0374}, /* 5x Greek */ {0x0376, 0x0377}, /* 2x Greek */ {0x037a, 0x037d}, /* 4x Greek */ {0x037f, 0x037f}, /* 1x Greek */ {0x0386, 0x0386}, /* 1x Greek */ {0x0388, 0x038a}, /* 3x Greek */ {0x038c, 0x038c}, /* 1x Greek */ {0x038e, 0x03a1}, /* 20x Greek */ {0x03a3, 0x03f5}, /* 83x Greek */ {0x03f7, 0x0481}, /* 139x Greek */ {0x048a, 0x052f}, /* 166x Cyrillic */ {0x0531, 0x0556}, /* 38x Armenian */ {0x0560, 0x0588}, /* 41x Armenian */ {0x05d0, 0x05ea}, /* 27x Hebrew */ {0x0620, 0x064a}, /* 43x Arabic */ {0x0660, 0x0669}, /* 10x Arabic */ {0x0671, 0x06d3}, /* 99x Arabic */ {0x06ee, 0x06fc}, /* 15x Arabic */ {0x0712, 0x072f}, /* 30x Syriac */ {0x074d, 0x07a5}, /* 89x Syriac,Arabic2,Thaana */ {0x07c0, 0x07ea}, /* 43x NKo */ {0x0800, 0x0815}, /* 22x Samaritan */ {0x0840, 0x0858}, /* 25x Mandaic */ {0x0904, 0x0939}, /* 54x Devanagari */ {0x0993, 0x09a8}, /* 22x Bengali */ {0x09e6, 0x09f1}, /* 12x Bengali */ {0x0a13, 0x0a28}, /* 22x Gurmukhi */ {0x0a66, 0x0a6f}, /* 10x Gurmukhi */ {0x0a93, 0x0aa8}, /* 22x Gujarati */ {0x0b13, 0x0b28}, /* 22x Oriya */ {0x0c92, 0x0ca8}, /* 23x Kannada */ {0x0caa, 0x0cb3}, /* 10x Kannada */ {0x0ce6, 0x0cef}, /* 10x Kannada */ {0x0d12, 0x0d3a}, /* 41x Malayalam */ {0x0d85, 0x0d96}, /* 18x Sinhala */ {0x0d9a, 0x0db1}, /* 24x Sinhala */ {0x0de6, 0x0def}, /* 10x Sinhala */ {0x0e01, 0x0e30}, /* 48x Thai */ {0x0e8c, 0x0ea3}, /* 24x Lao */ {0x0f20, 0x0f33}, /* 20x Tibetan */ {0x0f49, 0x0f6c}, /* 36x Tibetan */ {0x109e, 0x10c5}, /* 40x Myanmar,Georgian */ {0x10d0, 0x10fa}, /* 43x Georgian */ {0x10fc, 0x1248}, /* 333x Georgian,Hangul,Ethiopic */ {0x13a0, 0x13f5}, /* 86x Cherokee */ {0x1401, 0x166d}, /* 621x Aboriginal */ {0x16a0, 0x16ea}, /* 75x Runic */ {0x1700, 0x170c}, /* 13x Tagalog */ {0x1780, 0x17b3}, /* 52x Khmer */ {0x1820, 0x1878}, /* 89x Mongolian */ {0x1a00, 0x1a16}, /* 23x Buginese */ {0x1a20, 0x1a54}, /* 53x Tai Tham */ {0x1a80, 0x1a89}, /* 10x Tai Tham */ {0x1a90, 0x1a99}, /* 10x Tai Tham */ {0x1b05, 0x1b33}, /* 47x Balinese */ {0x1b50, 0x1b59}, /* 10x Balinese */ {0x1b83, 0x1ba0}, /* 30x Sundanese */ {0x1bae, 0x1be5}, /* 56x Sundanese */ {0x1c90, 0x1cba}, /* 43x Georgian2 */ {0x1cbd, 0x1cbf}, /* 3x Georgian2 */ {0x1e00, 0x1f15}, /* 278x Watin-C,Greek2 */ {0x2070, 0x2071}, /* 2x Supersub */ {0x2074, 0x2079}, /* 6x Supersub */ {0x207f, 0x2089}, /* 11x Supersub */ {0x2090, 0x209c}, /* 13x Supersub */ {0x2100, 0x2117}, /* 24x Letterlike */ {0x2119, 0x213f}, /* 39x Letterlike */ {0x2145, 0x214a}, /* 6x Letterlike */ {0x214c, 0x218b}, /* 64x Letterlike,Numbery */ {0x21af, 0x21cd}, /* 31x Arrows */ {0x21d5, 0x21f3}, /* 31x Arrows */ {0x230c, 0x231f}, /* 20x Technical */ {0x232b, 0x237b}, /* 81x Technical */ {0x237d, 0x239a}, /* 30x Technical */ {0x23b4, 0x23db}, /* 40x Technical */ {0x23e2, 0x2426}, /* 69x Technical,ControlPictures */ {0x2460, 0x25b6}, /* 343x Enclosed,Boxes,Blocks,Shapes */ {0x25c2, 0x25f7}, /* 54x Shapes */ {0x2600, 0x266e}, /* 111x Symbols */ {0x2670, 0x2767}, /* 248x Symbols,Dingbats */ {0x2776, 0x27bf}, /* 74x Dingbats */ {0x2800, 0x28ff}, /* 256x Braille */ {0x2c00, 0x2c2e}, /* 47x Glagolitic */ {0x2c30, 0x2c5e}, /* 47x Glagolitic */ {0x2c60, 0x2ce4}, /* 133x Watin-D */ {0x2d00, 0x2d25}, /* 38x Georgian2 */ {0x2d30, 0x2d67}, /* 56x Tifinagh */ {0x2d80, 0x2d96}, /* 23x Ethiopic2 */ {0x2e2f, 0x2e2f}, /* 1x Punctuation2 */ {0x3005, 0x3007}, /* 3x CJK Symbols & Punctuation */ {0x3021, 0x3029}, /* 9x CJK Symbols & Punctuation */ {0x3031, 0x3035}, /* 5x CJK Symbols & Punctuation */ {0x3038, 0x303c}, /* 5x CJK Symbols & Punctuation */ {0x3041, 0x3096}, /* 86x Hiragana */ {0x30a1, 0x30fa}, /* 90x Katakana */ {0x3105, 0x312f}, /* 43x Bopomofo */ {0x3131, 0x318e}, /* 94x Hangul Compatibility Jamo */ {0x31a0, 0x31ba}, /* 27x Bopomofo Extended */ {0x31f0, 0x31ff}, /* 16x Katakana Phonetic Extensions */ {0x3220, 0x3229}, /* 10x Enclosed CJK Letters & Months */ {0x3248, 0x324f}, /* 8x Enclosed CJK Letters & Months */ {0x3251, 0x325f}, /* 15x Enclosed CJK Letters & Months */ {0x3280, 0x3289}, /* 10x Enclosed CJK Letters & Months */ {0x32b1, 0x32bf}, /* 15x Enclosed CJK Letters & Months */ {0x3400, 0x4db5}, /* 6582x CJK Unified Ideographs Extension A */ {0x4dc0, 0x9fef}, /* 21040x Yijing Hexagram, CJK Unified Ideographs */ {0xa000, 0xa48c}, /* 1165x Yi Syllables */ {0xa4d0, 0xa4fd}, /* 46x Lisu */ {0xa500, 0xa60c}, /* 269x Vai */ {0xa610, 0xa62b}, /* 28x Vai */ {0xa6a0, 0xa6ef}, /* 80x Bamum */ {0xa80c, 0xa822}, /* 23x Syloti Nagri */ {0xa840, 0xa873}, /* 52x Phags-pa */ {0xa882, 0xa8b3}, /* 50x Saurashtra */ {0xa8d0, 0xa8d9}, /* 10x Saurashtra */ {0xa900, 0xa925}, /* 38x Kayah Li */ {0xa930, 0xa946}, /* 23x Rejang */ {0xa960, 0xa97c}, /* 29x Hangul Jamo Extended-A */ {0xa984, 0xa9b2}, /* 47x Javanese */ {0xa9cf, 0xa9d9}, /* 11x Javanese */ {0xaa00, 0xaa28}, /* 41x Cham */ {0xaa50, 0xaa59}, /* 10x Cham */ {0xabf0, 0xabf9}, /* 10x Meetei Mayek */ {0xac00, 0xd7a3}, /* 11172x Hangul Syllables */ {0xf900, 0xfa6d}, /* 366x CJK Compatibility Ideographs */ {0xfa70, 0xfad9}, /* 106x CJK Compatibility Ideographs */ {0xfb1f, 0xfb28}, /* 10x Alphabetic Presentation Forms */ {0xfb2a, 0xfb36}, /* 13x Alphabetic Presentation Forms */ {0xfb46, 0xfbb1}, /* 108x Alphabetic Presentation Forms */ {0xfbd3, 0xfd3d}, /* 363x Arabic Presentation Forms-A */ {0xfe76, 0xfefc}, /* 135x Arabic Presentation Forms-B */ {0xff10, 0xff19}, /* 10x Dubs */ {0xff21, 0xff3a}, /* 26x Dubs */ {0xff41, 0xff5a}, /* 26x Dubs */ {0xff66, 0xffbe}, /* 89x Dubs */ {0xffc2, 0xffc7}, /* 6x Dubs */ {0xffca, 0xffcf}, /* 6x Dubs */ {0xffd2, 0xffd7}, /* 6x Dubs */ {0xffda, 0xffdc}, /* 3x Dubs */ }; static const unsigned kAstralCodes[][2] = { {0x10107, 0x10133}, /* 45x Aegean */ {0x10140, 0x10178}, /* 57x Ancient Greek Numbers */ {0x1018a, 0x1018b}, /* 2x Ancient Greek Numbers */ {0x10280, 0x1029c}, /* 29x Lycian */ {0x102a0, 0x102d0}, /* 49x Carian */ {0x102e1, 0x102fb}, /* 27x Coptic Epact Numbers */ {0x10300, 0x10323}, /* 36x Old Italic */ {0x1032d, 0x1034a}, /* 30x Old Italic, Gothic */ {0x10350, 0x10375}, /* 38x Old Permic */ {0x10380, 0x1039d}, /* 30x Ugaritic */ {0x103a0, 0x103c3}, /* 36x Old Persian */ {0x103c8, 0x103cf}, /* 8x Old Persian */ {0x103d1, 0x103d5}, /* 5x Old Persian */ {0x10400, 0x1049d}, /* 158x Deseret, Shavian, Osmanya */ {0x104b0, 0x104d3}, /* 36x Osage */ {0x104d8, 0x104fb}, /* 36x Osage */ {0x10500, 0x10527}, /* 40x Elbasan */ {0x10530, 0x10563}, /* 52x Caucasian Albanian */ {0x10600, 0x10736}, /* 311x Linear A */ {0x10800, 0x10805}, /* 6x Cypriot Syllabary */ {0x1080a, 0x10835}, /* 44x Cypriot Syllabary */ {0x10837, 0x10838}, /* 2x Cypriot Syllabary */ {0x1083f, 0x1089e}, /* 86x Cypriot,ImperialAramaic,Palmyrene,Nabataean */ {0x108e0, 0x108f2}, /* 19x Hatran */ {0x108f4, 0x108f5}, /* 2x Hatran */ {0x108fb, 0x1091b}, /* 33x Hatran */ {0x10920, 0x10939}, /* 26x Lydian */ {0x10980, 0x109b7}, /* 56x Meroitic Hieromarks */ {0x109bc, 0x109cf}, /* 20x Meroitic Cursive */ {0x109d2, 0x10a00}, /* 47x Meroitic Cursive */ {0x10a10, 0x10a13}, /* 4x Kharoshthi */ {0x10a15, 0x10a17}, /* 3x Kharoshthi */ {0x10a19, 0x10a35}, /* 29x Kharoshthi */ {0x10a40, 0x10a48}, /* 9x Kharoshthi */ {0x10a60, 0x10a7e}, /* 31x Old South Arabian */ {0x10a80, 0x10a9f}, /* 32x Old North Arabian */ {0x10ac0, 0x10ac7}, /* 8x Manichaean */ {0x10ac9, 0x10ae4}, /* 28x Manichaean */ {0x10aeb, 0x10aef}, /* 5x Manichaean */ {0x10b00, 0x10b35}, /* 54x Avestan */ {0x10b40, 0x10b55}, /* 22x Inscriptional Parthian */ {0x10b58, 0x10b72}, /* 27x Inscriptional Parthian and Pahlavi */ {0x10b78, 0x10b91}, /* 26x Inscriptional Pahlavi, Psalter Pahlavi */ {0x10c00, 0x10c48}, /* 73x Old Turkic */ {0x10c80, 0x10cb2}, /* 51x Old Hungarian */ {0x10cc0, 0x10cf2}, /* 51x Old Hungarian */ {0x10cfa, 0x10d23}, /* 42x Old Hungarian, Hanifi Rohingya */ {0x10d30, 0x10d39}, /* 10x Hanifi Rohingya */ {0x10e60, 0x10e7e}, /* 31x Rumi Numeral Symbols */ {0x10f00, 0x10f27}, /* 40x Old Sogdian */ {0x10f30, 0x10f45}, /* 22x Sogdian */ {0x10f51, 0x10f54}, /* 4x Sogdian */ {0x10fe0, 0x10ff6}, /* 23x Elymaic */ {0x11003, 0x11037}, /* 53x Brahmi */ {0x11052, 0x1106f}, /* 30x Brahmi */ {0x11083, 0x110af}, /* 45x Kaithi */ {0x110d0, 0x110e8}, /* 25x Sora Sompeng */ {0x110f0, 0x110f9}, /* 10x Sora Sompeng */ {0x11103, 0x11126}, /* 36x Chakma */ {0x11136, 0x1113f}, /* 10x Chakma */ {0x11144, 0x11144}, /* 1x Chakma */ {0x11150, 0x11172}, /* 35x Mahajani */ {0x11176, 0x11176}, /* 1x Mahajani */ {0x11183, 0x111b2}, /* 48x Sharada */ {0x111c1, 0x111c4}, /* 4x Sharada */ {0x111d0, 0x111da}, /* 11x Sharada */ {0x111dc, 0x111dc}, /* 1x Sharada */ {0x111e1, 0x111f4}, /* 20x Sinhala Archaic Numbers */ {0x11200, 0x11211}, /* 18x Khojki */ {0x11213, 0x1122b}, /* 25x Khojki */ {0x11280, 0x11286}, /* 7x Multani */ {0x11288, 0x11288}, /* 1x Multani */ {0x1128a, 0x1128d}, /* 4x Multani */ {0x1128f, 0x1129d}, /* 15x Multani */ {0x1129f, 0x112a8}, /* 10x Multani */ {0x112b0, 0x112de}, /* 47x Khudawadi */ {0x112f0, 0x112f9}, /* 10x Khudawadi */ {0x11305, 0x1130c}, /* 8x Grantha */ {0x1130f, 0x11310}, /* 2x Grantha */ {0x11313, 0x11328}, /* 22x Grantha */ {0x1132a, 0x11330}, /* 7x Grantha */ {0x11332, 0x11333}, /* 2x Grantha */ {0x11335, 0x11339}, /* 5x Grantha */ {0x1133d, 0x1133d}, /* 1x Grantha */ {0x11350, 0x11350}, /* 1x Grantha */ {0x1135d, 0x11361}, /* 5x Grantha */ {0x11400, 0x11434}, /* 53x Newa */ {0x11447, 0x1144a}, /* 4x Newa */ {0x11450, 0x11459}, /* 10x Newa */ {0x1145f, 0x1145f}, /* 1x Newa */ {0x11480, 0x114af}, /* 48x Tirhuta */ {0x114c4, 0x114c5}, /* 2x Tirhuta */ {0x114c7, 0x114c7}, /* 1x Tirhuta */ {0x114d0, 0x114d9}, /* 10x Tirhuta */ {0x11580, 0x115ae}, /* 47x Siddham */ {0x115d8, 0x115db}, /* 4x Siddham */ {0x11600, 0x1162f}, /* 48x Modi */ {0x11644, 0x11644}, /* 1x Modi */ {0x11650, 0x11659}, /* 10x Modi */ {0x11680, 0x116aa}, /* 43x Takri */ {0x116b8, 0x116b8}, /* 1x Takri */ {0x116c0, 0x116c9}, /* 10x Takri */ {0x11700, 0x1171a}, /* 27x Ahom */ {0x11730, 0x1173b}, /* 12x Ahom */ {0x11800, 0x1182b}, /* 44x Dogra */ {0x118a0, 0x118f2}, /* 83x Warang Citi */ {0x118ff, 0x118ff}, /* 1x Warang Citi */ {0x119a0, 0x119a7}, /* 8x Nandinagari */ {0x119aa, 0x119d0}, /* 39x Nandinagari */ {0x119e1, 0x119e1}, /* 1x Nandinagari */ {0x119e3, 0x119e3}, /* 1x Nandinagari */ {0x11a00, 0x11a00}, /* 1x Zanabazar Square */ {0x11a0b, 0x11a32}, /* 40x Zanabazar Square */ {0x11a3a, 0x11a3a}, /* 1x Zanabazar Square */ {0x11a50, 0x11a50}, /* 1x Soyombo */ {0x11a5c, 0x11a89}, /* 46x Soyombo */ {0x11a9d, 0x11a9d}, /* 1x Soyombo */ {0x11ac0, 0x11af8}, /* 57x Pau Cin Hau */ {0x11c00, 0x11c08}, /* 9x Bhaiksuki */ {0x11c0a, 0x11c2e}, /* 37x Bhaiksuki */ {0x11c40, 0x11c40}, /* 1x Bhaiksuki */ {0x11c50, 0x11c6c}, /* 29x Bhaiksuki */ {0x11c72, 0x11c8f}, /* 30x Marchen */ {0x11d00, 0x11d06}, /* 7x Masaram Gondi */ {0x11d08, 0x11d09}, /* 2x Masaram Gondi */ {0x11d0b, 0x11d30}, /* 38x Masaram Gondi */ {0x11d46, 0x11d46}, /* 1x Masaram Gondi */ {0x11d50, 0x11d59}, /* 10x Masaram Gondi */ {0x11d60, 0x11d65}, /* 6x Gunjala Gondi */ {0x11d67, 0x11d68}, /* 2x Gunjala Gondi */ {0x11d6a, 0x11d89}, /* 32x Gunjala Gondi */ {0x11d98, 0x11d98}, /* 1x Gunjala Gondi */ {0x11da0, 0x11da9}, /* 10x Gunjala Gondi */ {0x11ee0, 0x11ef2}, /* 19x Makasar */ {0x11fc0, 0x11fd4}, /* 21x Tamil Supplement */ {0x12000, 0x12399}, /* 922x Cuneiform */ {0x12400, 0x1246e}, /* 111x Cuneiform Numbers & Punctuation */ {0x12480, 0x12543}, /* 196x Early Dynastic Cuneiform */ {0x13000, 0x1342e}, /* 1071x Egyptian Hieromarks */ {0x14400, 0x14646}, /* 583x Anatolian Hieromarks */ {0x16800, 0x16a38}, /* 569x Bamum Supplement */ {0x16a40, 0x16a5e}, /* 31x Mro */ {0x16a60, 0x16a69}, /* 10x Mro */ {0x16ad0, 0x16aed}, /* 30x Bassa Vah */ {0x16b00, 0x16b2f}, /* 48x Pahawh Hmong */ {0x16b40, 0x16b43}, /* 4x Pahawh Hmong */ {0x16b50, 0x16b59}, /* 10x Pahawh Hmong */ {0x16b5b, 0x16b61}, /* 7x Pahawh Hmong */ {0x16b63, 0x16b77}, /* 21x Pahawh Hmong */ {0x16b7d, 0x16b8f}, /* 19x Pahawh Hmong */ {0x16e40, 0x16e96}, /* 87x Medefaidrin */ {0x16f00, 0x16f4a}, /* 75x Miao */ {0x16f50, 0x16f50}, /* 1x Miao */ {0x16f93, 0x16f9f}, /* 13x Miao */ {0x16fe0, 0x16fe1}, /* 2x Ideographic Symbols & Punctuation */ {0x16fe3, 0x16fe3}, /* 1x Ideographic Symbols & Punctuation */ {0x17000, 0x187f7}, /* 6136x Tangut */ {0x18800, 0x18af2}, /* 755x Tangut Components */ {0x1b000, 0x1b11e}, /* 287x Kana Supplement */ {0x1b150, 0x1b152}, /* 3x Small Kana Extension */ {0x1b164, 0x1b167}, /* 4x Small Kana Extension */ {0x1b170, 0x1b2fb}, /* 396x Nushu */ {0x1bc00, 0x1bc6a}, /* 107x Duployan */ {0x1bc70, 0x1bc7c}, /* 13x Duployan */ {0x1bc80, 0x1bc88}, /* 9x Duployan */ {0x1bc90, 0x1bc99}, /* 10x Duployan */ {0x1d2e0, 0x1d2f3}, /* 20x Mayan Numerals */ {0x1d360, 0x1d378}, /* 25x Counting Rod Numerals */ {0x1d400, 0x1d454}, /* 85x 𝐀..𝑔 Math */ {0x1d456, 0x1d49c}, /* 71x 𝑖..𝒜 Math */ {0x1d49e, 0x1d49f}, /* 2x 𝒞..𝒟 Math */ {0x1d4a2, 0x1d4a2}, /* 1x 𝒢..𝒢 Math */ {0x1d4a5, 0x1d4a6}, /* 2x 𝒥..𝒦 Math */ {0x1d4a9, 0x1d4ac}, /* 4x 𝒩..𝒬 Math */ {0x1d4ae, 0x1d4b9}, /* 12x 𝒮..𝒹 Math */ {0x1d4bb, 0x1d4bb}, /* 1x 𝒻..𝒻 Math */ {0x1d4bd, 0x1d4c3}, /* 7x 𝒽..𝓃 Math */ {0x1d4c5, 0x1d505}, /* 65x 𝓅..𝔅 Math */ {0x1d507, 0x1d50a}, /* 4x 𝔇..𝔊 Math */ {0x1d50d, 0x1d514}, /* 8x 𝔍..𝔔 Math */ {0x1d516, 0x1d51c}, /* 7x 𝔖..𝔜 Math */ {0x1d51e, 0x1d539}, /* 28x 𝔞..𝔹 Math */ {0x1d53b, 0x1d53e}, /* 4x 𝔻..𝔾 Math */ {0x1d540, 0x1d544}, /* 5x 𝕀..𝕄 Math */ {0x1d546, 0x1d546}, /* 1x 𝕆..𝕆 Math */ {0x1d54a, 0x1d550}, /* 7x 𝕊..𝕐 Math */ {0x1d552, 0x1d6a5}, /* 340x 𝕒..𝚥 Math */ {0x1d6a8, 0x1d6c0}, /* 25x 𝚨..𝛀 Math */ {0x1d6c2, 0x1d6da}, /* 25x 𝛂..𝛚 Math */ {0x1d6dc, 0x1d6fa}, /* 31x 𝛜..𝛺 Math */ {0x1d6fc, 0x1d714}, /* 25x 𝛼..𝜔 Math */ {0x1d716, 0x1d734}, /* 31x 𝜖..𝜴 Math */ {0x1d736, 0x1d74e}, /* 25x 𝜶..𝝎 Math */ {0x1d750, 0x1d76e}, /* 31x 𝝐..𝝮 Math */ {0x1d770, 0x1d788}, /* 25x 𝝰..𝞈 Math */ {0x1d78a, 0x1d7a8}, /* 31x 𝞊..𝞨 Math */ {0x1d7aa, 0x1d7c2}, /* 25x 𝞪..𝟂 Math */ {0x1d7c4, 0x1d7cb}, /* 8x 𝟄..𝟋 Math */ {0x1d7ce, 0x1d9ff}, /* 562x Math, Sutton SignWriting */ {0x1f100, 0x1f10c}, /* 13x Enclosed Alphanumeric Supplement */ {0x20000, 0x2a6d6}, /* 42711x CJK Unified Ideographs Extension B */ {0x2a700, 0x2b734}, /* 4149x CJK Unified Ideographs Extension C */ {0x2b740, 0x2b81d}, /* 222x CJK Unified Ideographs Extension D */ {0x2b820, 0x2cea1}, /* 5762x CJK Unified Ideographs Extension E */ {0x2ceb0, 0x2ebe0}, /* 7473x CJK Unified Ideographs Extension F */ {0x2f800, 0x2fa1d}, /* 542x CJK Compatibility Ideographs Supplement */ }; /** * Returns nonzero if 𝑐 isn't alphanumeric. * * Line reading interfaces generally define this operation as UNICODE * characters that aren't in the letter category (Lu, Ll, Lt, Lm, Lo) * and aren't in the number categorie (Nd, Nl, No). We also add a few * other things like blocks and emoji (So). */ int iswseparator(wint_t c) { int m, l, r, n; if (c < 0200) { return !(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')); } if (c <= 0xffff) { l = 0; r = n = sizeof(kCodes) / sizeof(kCodes[0]); while (l < r) { m = (l + r) >> 1; if (kCodes[m][1] < c) { l = m + 1; } else { r = m; } } return !(l < n && kCodes[l][0] <= c && c <= kCodes[l][1]); } else { l = 0; r = n = sizeof(kAstralCodes) / sizeof(kAstralCodes[0]); while (l < r) { m = (l + r) >> 1; if (kAstralCodes[m][1] < c) { l = m + 1; } else { r = m; } } return !(l < n && kAstralCodes[l][0] <= c && c <= kAstralCodes[l][1]); } }
20,558
424
jart/cosmopolitan
false
cosmopolitan/libc/str/startswithi.c
/*-*- 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 2021 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/str/str.h" #include "libc/str/tab.internal.h" bool _startswithi(const char *s, const char *prefix) { for (;;) { if (!*prefix) return true; if (!*s) return false; if (kToLower[*s++ & 255] != kToLower[*prefix++ & 255]) return false; } }
2,103
29
jart/cosmopolitan
false
cosmopolitan/libc/str/strnwidth16.c
/*-*- 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/intrin/safemacros.internal.h" #include "libc/str/str.h" #include "libc/str/unicode.h" #include "libc/str/utf16.h" /** * Returns monospace display width of UTF-16 or UCS-2 string. */ int strnwidth16(const char16_t *p, size_t n, size_t o) { size_t l; wint_t x, y; l = 0; if (n) { while ((x = *p++)) { if (!IsUcs2(x)) { if ((y = *p++)) { x = MergeUtf16(x, y); } else { ++l; break; } } l += max(0, wcwidth(x)); } } return l; }
2,369
46
jart/cosmopolitan
false