mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-27 04:50:28 +00:00
Make exciting improvements
- Add Lua backtraces to redbean! - Wipe serving keys after redbean forks - Audit redbean to remove free via exit - Log SSL client ciphersuite preferences - Increase ASAN malloc() backtrace depth - Make GetSslRoots() behave as a singleton - Move leaks.c from LIBC_TESTLIB to LIBC_LOG - Add undocumented %n to printf() for newlines - Fix redbean memory leak reindexing inode change - Fix redbean memory leak with Fetch() DNS object - Restore original environ after __cxa_finalize() - Make backtrace always work after __cxa_finalize() - Introduce COUNTEXPR() diagnostic / benchmark tool - Fix a few more instances of errno being clobbered - Consolidate the ANSI color disabling internal APIs
This commit is contained in:
parent
f5831a62fa
commit
af645fcbec
61 changed files with 1354 additions and 814 deletions
|
@ -52,6 +52,10 @@
|
|||
|
||||
STATIC_YOINK("_init_asan");
|
||||
|
||||
#define ASAN_MORGUE_ITEMS 512
|
||||
#define ASAN_MORGUE_THRESHOLD 65536 // morgue memory O(ITEMS*THRESHOLD)
|
||||
#define ASAN_TRACE_ITEMS 16 // backtrace limit on malloc origin
|
||||
|
||||
/**
|
||||
* @fileoverview Cosmopolitan Address Sanitizer Runtime.
|
||||
*
|
||||
|
@ -83,8 +87,6 @@ STATIC_YOINK("_init_asan");
|
|||
* movq (%addr),%dst
|
||||
*/
|
||||
|
||||
#define ASAN_MORGUE_SIZE 128
|
||||
|
||||
#define HOOK(HOOK, IMPL) \
|
||||
do { \
|
||||
if (weaken(HOOK)) { \
|
||||
|
@ -104,7 +106,7 @@ STATIC_YOINK("_init_asan");
|
|||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
|
||||
|
||||
struct AsanTrace {
|
||||
intptr_t p[4];
|
||||
uint32_t p[ASAN_TRACE_ITEMS]; // assumes linkage into 32-bit space
|
||||
};
|
||||
|
||||
struct AsanExtra {
|
||||
|
@ -139,7 +141,12 @@ struct AsanGlobal {
|
|||
|
||||
struct AsanMorgue {
|
||||
unsigned i;
|
||||
void *p[ASAN_MORGUE_SIZE];
|
||||
void *p[ASAN_MORGUE_ITEMS];
|
||||
};
|
||||
|
||||
struct ReportOriginHeap {
|
||||
const unsigned char *a;
|
||||
int z;
|
||||
};
|
||||
|
||||
bool __asan_noreentry;
|
||||
|
@ -657,11 +664,6 @@ static void __asan_report_memory_origin_image(intptr_t a, int z) {
|
|||
}
|
||||
}
|
||||
|
||||
struct ReportOriginHeap {
|
||||
const unsigned char *a;
|
||||
int z;
|
||||
};
|
||||
|
||||
static noasan void OnMemory(void *x, void *y, size_t n, void *a) {
|
||||
const unsigned char *p = x;
|
||||
struct ReportOriginHeap *t = a;
|
||||
|
@ -725,6 +727,7 @@ nodiscard static __asan_die_f *__asan_report(const void *addr, int size,
|
|||
uint64_t x, y, z;
|
||||
char *p, *q, *base;
|
||||
struct MemoryIntervals *m;
|
||||
++g_ftrace;
|
||||
p = __fatalbuf;
|
||||
kprintf("%n\e[J\e[1;31masan error\e[0m: %s %d-byte %s at %p shadow %p%n%s%n",
|
||||
__asan_describe_access_poison(kind), size, message, addr,
|
||||
|
@ -805,6 +808,7 @@ nodiscard static __asan_die_f *__asan_report(const void *addr, int size,
|
|||
kprintf("%s", __fatalbuf);
|
||||
__asan_report_memory_origin(addr, size, kind);
|
||||
kprintf("%nthe crash was caused by%n");
|
||||
--g_ftrace;
|
||||
return __asan_die();
|
||||
}
|
||||
|
||||
|
@ -1005,7 +1009,7 @@ int __asan_print_trace(void *p) {
|
|||
kprintf(" bad cookie");
|
||||
return -1;
|
||||
}
|
||||
kprintf("%n%p %,lu bytes [asan]", (char *)p + 16, n);
|
||||
kprintf("%n%p %,lu bytes [asan]", (char *)p, n);
|
||||
if (!__asan_is_mapped((((intptr_t)p >> 3) + 0x7fff8000) >> 16)) {
|
||||
kprintf(" (shadow not mapped?!)");
|
||||
}
|
||||
|
@ -1024,7 +1028,7 @@ static void __asan_deallocate(char *p, long kind) {
|
|||
if ((e = __asan_get_extra(p, &c))) {
|
||||
if (__asan_read48(e->size, &n)) {
|
||||
__asan_poison((uintptr_t)p, c, kind);
|
||||
if (c <= FRAMESIZE) {
|
||||
if (c <= ASAN_MORGUE_THRESHOLD) {
|
||||
p = __asan_morgue_add(p);
|
||||
}
|
||||
weaken(dlfree)(p);
|
||||
|
|
21
libc/intrin/ftrace.c
Normal file
21
libc/intrin/ftrace.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*-*- 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"
|
||||
|
||||
int g_ftrace;
|
68
libc/intrin/getenv.c
Normal file
68
libc/intrin/getenv.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*-*- 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/sysdebug.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
||||
forceinline int Identity(int c) {
|
||||
return c;
|
||||
}
|
||||
|
||||
forceinline int ToUpper(int c) {
|
||||
return 'a' <= c && c <= 'z' ? c - ('a' - 'A') : c;
|
||||
}
|
||||
|
||||
forceinline char *GetEnv(const char *s, int xlat(int)) {
|
||||
char **p;
|
||||
size_t i, j;
|
||||
if ((p = environ)) {
|
||||
for (i = 0; p[i]; ++i) {
|
||||
for (j = 0;; ++j) {
|
||||
if (!s[j]) {
|
||||
if (p[i][j] == '=') {
|
||||
return p[i] + j + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (xlat(s[j]) != xlat(p[i][j])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value of environment variable, or NULL if not found.
|
||||
*
|
||||
* Environment variables can store empty string on Unix but not Windows.
|
||||
*
|
||||
* @note should not be used after __cxa_finalize() is called
|
||||
*/
|
||||
char *getenv(const char *s) {
|
||||
char *r;
|
||||
if (!IsWindows()) {
|
||||
r = GetEnv(s, Identity);
|
||||
} else {
|
||||
r = GetEnv(s, ToUpper);
|
||||
}
|
||||
SYSDEBUG("getenv(%#s) → %#s", s, r);
|
||||
return r;
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ 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 │
|
||||
|
@ -16,31 +16,17 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/assert.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/nt/enum/version.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/nt/version.h"
|
||||
|
||||
bool g_isterminalinarticulate;
|
||||
|
||||
bool IsTerminalInarticulate(void) {
|
||||
return g_isterminalinarticulate;
|
||||
/**
|
||||
* Returns true if we're running at least Windows 10.
|
||||
*
|
||||
* This function may only be called if IsWindows() is true.
|
||||
*/
|
||||
privileged bool(IsAtLeastWindows10)(void) {
|
||||
assert(IsWindows());
|
||||
return IsAtLeastWindows10();
|
||||
}
|
||||
|
||||
textstartup noasan void g_isterminalinarticulate_init(int argc, char **argv,
|
||||
char **envp,
|
||||
intptr_t *auxv) {
|
||||
char *s;
|
||||
if (IsWindows() && NtGetVersion() < kNtVersionWindows10) {
|
||||
g_isterminalinarticulate = true;
|
||||
} else if ((s = __getenv(envp, "TERM"))) {
|
||||
g_isterminalinarticulate = !__strcmp(s, "dumb");
|
||||
}
|
||||
}
|
||||
|
||||
const void *const g_isterminalinarticulate_ctor[] initarray = {
|
||||
g_isterminalinarticulate_init,
|
||||
};
|
|
@ -37,7 +37,7 @@ noasan noubsan int IsDebuggerPresent(bool force) {
|
|||
char *p, buf[1024];
|
||||
if (!force) {
|
||||
if (IsGenuineCosmo()) return 0;
|
||||
if (__getenv(__envp, "HEISENDEBUG")) return 0;
|
||||
if (getenv("HEISENDEBUG")) return 0;
|
||||
}
|
||||
if (IsWindows()) {
|
||||
return NtGetPeb()->BeingDebugged; /* needs noasan */
|
|
@ -30,9 +30,8 @@ bool IsRunningUnderMake(void) {
|
|||
return g_isrunningundermake;
|
||||
}
|
||||
|
||||
textstartup void g_isrunningundermake_init(int argc, char **argv, char **envp,
|
||||
intptr_t *auxv) {
|
||||
g_isrunningundermake = !!__getenv(envp, "MAKEFLAGS");
|
||||
textstartup void g_isrunningundermake_init(void) {
|
||||
g_isrunningundermake = !!getenv("MAKEFLAGS");
|
||||
}
|
||||
|
||||
const void *const g_isrunningundermake_ctor[] initarray = {
|
||||
|
|
61
libc/intrin/nocolor.c
Normal file
61
libc/intrin/nocolor.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*-*- 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/dce.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/nt/version.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
||||
#define IsDumb(s) \
|
||||
(s[0] == 'd' && s[1] == 'u' && s[2] == 'm' && s[3] == 'b' && !s[4])
|
||||
|
||||
/**
|
||||
* Indicates if ANSI terminal colors are inappropriate.
|
||||
*
|
||||
* Normally this variable should be false. We only set it to true if
|
||||
* we're running on an old version of Windows or the environment
|
||||
* variable `TERM` is set to `dumb`.
|
||||
*
|
||||
* We think colors should be the norm, since most software is usually
|
||||
* too conservative about removing them. Rather than using `isatty`
|
||||
* consider using sed for instances where color must be removed:
|
||||
*
|
||||
* sed 's/\x1b\[[;[:digit:]]*m//g' <color.txt >uncolor.txt
|
||||
*
|
||||
* For some reason, important software is configured by default in many
|
||||
* operating systems, to not only disable colors, but utf-8 too! Here's
|
||||
* an example of how a wrapper script can fix that for `less`.
|
||||
*
|
||||
* #!/bin/sh
|
||||
* LESSCHARSET=UTF-8 exec /usr/bin/less -RS "$@"
|
||||
*
|
||||
* Thank you for using colors!
|
||||
*/
|
||||
bool __nocolor;
|
||||
|
||||
optimizesize textstartup noasan void __nocolor_init(int argc, char **argv,
|
||||
char **envp,
|
||||
intptr_t *auxv) {
|
||||
char *s;
|
||||
__nocolor = (IsWindows() && !IsAtLeastWindows10()) ||
|
||||
((s = getenv("TERM")) && IsDumb(s));
|
||||
}
|
||||
|
||||
const void *const __nocolor_ctor[] initarray = {
|
||||
__nocolor_init,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue