mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
Improve memory safety
This commit makes numerous refinements to cosmopolitan memory handling. The default stack size has been reduced from 2mb to 128kb. A new macro is now provided so you can easily reconfigure the stack size to be any value you want. Work around the breaking change by adding to your main: STATIC_STACK_SIZE(0x00200000); // 2mb stack If you're not sure how much stack you need, then you can use: STATIC_YOINK("stack_usage_logging"); After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test suite, nothing in the Cosmopolitan repository (except for Python) needs a stack size greater than 30kb. There are also new macros for detecting the size and address of the stack at runtime, e.g. GetStackAddr(). We also now support sigaltstack() so if you want to see nice looking crash reports whenever a stack overflow happens, you can put this in main(): ShowCrashReports(); Under `make MODE=dbg` and `make MODE=asan` the unit testing framework will now automatically print backtraces of memory allocations when things like memory leaks happen. Bugs are now fixed in ASAN global variable overrun detection. The memtrack and asan runtimes also handle edge cases now. The new tools helped to identify a few memory leaks, which are fixed by this change. This change should fix an issue reported in #288 with ARG_MAX limits. Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
This commit is contained in:
parent
a0b39f886c
commit
226aaf3547
317 changed files with 6474 additions and 3993 deletions
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sigbits.h"
|
||||
|
@ -44,6 +45,7 @@
|
|||
#include "libc/runtime/memtrack.internal.h"
|
||||
#include "libc/runtime/pc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/fileno.h"
|
||||
|
@ -94,20 +96,23 @@ relegated static const char *TinyStrSignal(int sig) {
|
|||
return "???";
|
||||
}
|
||||
|
||||
relegated static void ShowFunctionCalls(int fd, ucontext_t *ctx) {
|
||||
relegated static void ShowFunctionCalls(ucontext_t *ctx) {
|
||||
struct StackFrame *bp;
|
||||
struct StackFrame goodframe;
|
||||
write(fd, "\n", 1);
|
||||
if (ctx && ctx->uc_mcontext.rip && ctx->uc_mcontext.rbp) {
|
||||
if (ctx->uc_mcontext.rip && ctx->uc_mcontext.rbp) {
|
||||
goodframe.next = (struct StackFrame *)ctx->uc_mcontext.rbp;
|
||||
goodframe.addr = ctx->uc_mcontext.rip;
|
||||
bp = &goodframe;
|
||||
ShowBacktrace(fd, bp);
|
||||
ShowBacktrace(2, bp);
|
||||
}
|
||||
}
|
||||
|
||||
relegated static char *AddFlag(char *p, int b, const char *s) {
|
||||
if (b) p = stpcpy(p, s);
|
||||
if (b) {
|
||||
p = __stpcpy(p, s);
|
||||
} else {
|
||||
*p = 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -137,11 +142,13 @@ relegated static char *DescribeCpuFlags(char *p, int flags, int x87sw,
|
|||
return p;
|
||||
}
|
||||
|
||||
relegated static char *ShowGeneralRegisters(char *p, ucontext_t *ctx) {
|
||||
relegated static void ShowGeneralRegisters(ucontext_t *ctx) {
|
||||
int64_t x;
|
||||
const char *s;
|
||||
size_t i, j, k;
|
||||
long double st;
|
||||
char *p, buf[128];
|
||||
p = buf;
|
||||
*p++ = '\n';
|
||||
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
|
||||
if (j > 0) *p++ = ' ';
|
||||
|
@ -162,20 +169,25 @@ relegated static char *ShowGeneralRegisters(char *p, ucontext_t *ctx) {
|
|||
if (x < 0) x = -x, *p++ = '-';
|
||||
p = __uintcpy(p, x / 1000), *p++ = '.';
|
||||
p = __uintcpy(p, x % 1000), *p++ = '\n';
|
||||
*p = 0;
|
||||
__printf("%s", buf);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
return DescribeCpuFlags(
|
||||
DescribeCpuFlags(
|
||||
p, ctx->uc_mcontext.gregs[REG_EFL],
|
||||
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->swd : 0,
|
||||
ctx->uc_mcontext.fpregs ? ctx->uc_mcontext.fpregs->mxcsr : 0);
|
||||
__printf("%s\n", buf);
|
||||
}
|
||||
|
||||
relegated static char *ShowSseRegisters(char *p, ucontext_t *ctx) {
|
||||
relegated static void ShowSseRegisters(ucontext_t *ctx) {
|
||||
size_t i;
|
||||
char *p, buf[128];
|
||||
if (ctx->uc_mcontext.fpregs) {
|
||||
p = __stpcpy(p, "\n\n");
|
||||
__printf("\n");
|
||||
for (i = 0; i < 8; ++i) {
|
||||
p = __stpcpy(p, "XMM");
|
||||
p = buf;
|
||||
if (i >= 10) {
|
||||
*p++ = i / 10 + '0';
|
||||
*p++ = i % 10 + '0';
|
||||
|
@ -197,93 +209,61 @@ relegated static char *ShowSseRegisters(char *p, ucontext_t *ctx) {
|
|||
*p++ = ' ';
|
||||
p = __fixcpy(p, ctx->uc_mcontext.fpregs->xmm[i + 8].u64[1], 64);
|
||||
p = __fixcpy(p, ctx->uc_mcontext.fpregs->xmm[i + 8].u64[0], 64);
|
||||
*p++ = '\n';
|
||||
*p = 0;
|
||||
__printf("XMM%s\n", buf);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
relegated static void ShowMemoryMappings(int outfd) {
|
||||
ssize_t rc;
|
||||
int c, infd;
|
||||
char buf[64];
|
||||
if (!IsTiny()) {
|
||||
PrintMemoryIntervals(outfd, &_mmi);
|
||||
if ((infd = open("/proc/self/maps", O_RDONLY)) != -1) {
|
||||
while ((rc = read(infd, buf, sizeof(buf))) > 0) {
|
||||
__write(buf, rc);
|
||||
}
|
||||
}
|
||||
close(infd);
|
||||
}
|
||||
}
|
||||
|
||||
void ShowCrashReportHook(int, int, int, struct siginfo *, ucontext_t *);
|
||||
|
||||
relegated void ShowCrashReport(int err, int fd, int sig, struct siginfo *si,
|
||||
relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
||||
ucontext_t *ctx) {
|
||||
int i;
|
||||
char *p;
|
||||
bool colorful;
|
||||
char hostname[64];
|
||||
char host[64];
|
||||
intptr_t stackaddr;
|
||||
struct utsname names;
|
||||
static char buf[4096];
|
||||
if (weaken(ShowCrashReportHook)) {
|
||||
ShowCrashReportHook(err, fd, sig, si, ctx);
|
||||
ShowCrashReportHook(2, err, sig, si, ctx);
|
||||
}
|
||||
colorful = cancolor();
|
||||
__stpcpy(hostname, "unknown");
|
||||
gethostname(hostname, sizeof(hostname));
|
||||
__stpcpy(host, "unknown");
|
||||
gethostname(host, sizeof(host));
|
||||
p = buf;
|
||||
p = __stpcpy(p, "\n");
|
||||
if (colorful) p = __stpcpy(p, "\e[30;101m");
|
||||
p = __stpcpy(p, "error");
|
||||
if (colorful) p = __stpcpy(p, "\e[0m");
|
||||
p = __stpcpy(p, ": Uncaught SIG");
|
||||
p = __stpcpy(p, TinyStrSignal(sig));
|
||||
if (si) {
|
||||
p = __stpcpy(p, " (");
|
||||
p = __stpcpy(p, GetSiCodeName(sig, si->si_code));
|
||||
p = __stpcpy(p, ")");
|
||||
__printf("\n%serror%s: Uncaught SIG%s",
|
||||
!g_isterminalinarticulate ? "\e[30;101m" : "",
|
||||
!g_isterminalinarticulate ? "\e[0m" : "", TinyStrSignal(sig));
|
||||
stackaddr = GetStackAddr(0);
|
||||
if (ctx && (ctx->uc_mcontext.rsp >= GetStaticStackAddr(0) &&
|
||||
ctx->uc_mcontext.rsp <= GetStaticStackAddr(0) + PAGESIZE)) {
|
||||
__printf(" (Stack Overflow)");
|
||||
} else if (si) {
|
||||
__printf(" (%s)", GetSiCodeName(sig, si->si_code));
|
||||
}
|
||||
p = __stpcpy(p, " on ");
|
||||
p = __stpcpy(p, hostname);
|
||||
p = __stpcpy(p, " pid ");
|
||||
p = __intcpy(p, __getpid());
|
||||
p = __stpcpy(p, "\n ");
|
||||
p = __stpcpy(p, program_invocation_name);
|
||||
p = __stpcpy(p, "\n ");
|
||||
p = __stpcpy(p, strerror(err));
|
||||
*p++ = '\n';
|
||||
__printf(" on %s pid %d\n %s\n %s\n", host, __getpid(),
|
||||
program_invocation_name, strerror(err));
|
||||
if (uname(&names) != -1) {
|
||||
p = __stpcpy(p, " ");
|
||||
p = __stpcpy(p, names.sysname), *p++ = ' ';
|
||||
p = __stpcpy(p, names.nodename), *p++ = ' ';
|
||||
p = __stpcpy(p, names.release), *p++ = ' ';
|
||||
p = __stpcpy(p, names.version), *p++ = '\n';
|
||||
__printf(" %s %s %s %s\n", names.sysname, names.nodename, names.release,
|
||||
names.version);
|
||||
}
|
||||
__write(buf, p - buf);
|
||||
ShowFunctionCalls(fd, ctx);
|
||||
if (ctx) {
|
||||
p = buf;
|
||||
p = ShowGeneralRegisters(p, ctx);
|
||||
p = ShowSseRegisters(p, ctx);
|
||||
*p++ = '\n';
|
||||
__write(buf, p - buf);
|
||||
__printf("\n");
|
||||
ShowFunctionCalls(ctx);
|
||||
ShowGeneralRegisters(ctx);
|
||||
ShowSseRegisters(ctx);
|
||||
}
|
||||
p = buf;
|
||||
*p++ = '\n';
|
||||
ShowMemoryMappings(fd);
|
||||
__write(buf, p - buf);
|
||||
__printf("\n");
|
||||
PrintMemoryIntervals(2, &_mmi);
|
||||
/* PrintSystemMappings(2); */
|
||||
if (__argv) {
|
||||
for (i = 0; i < __argc; ++i) {
|
||||
if (!__argv[i]) continue;
|
||||
if (IsAsan() && !__asan_is_valid(__argv[i], 1)) continue;
|
||||
__write(__argv[i], strlen(__argv[i]));
|
||||
__write(" ", 1);
|
||||
__printf("%s ", __argv[i]);
|
||||
}
|
||||
}
|
||||
__write("\n", 1);
|
||||
__printf("\n");
|
||||
}
|
||||
|
||||
relegated static void RestoreDefaultCrashSignalHandlers(void) {
|
||||
|
@ -309,29 +289,48 @@ relegated static void RestoreDefaultCrashSignalHandlers(void) {
|
|||
*
|
||||
* This function never returns, except for traps w/ human supervision.
|
||||
*/
|
||||
relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
||||
noasan relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
|
||||
intptr_t rip;
|
||||
int gdbpid, err;
|
||||
static bool once;
|
||||
err = errno;
|
||||
if (once) _exit(119);
|
||||
once = true;
|
||||
static bool noreentry, notpossible;
|
||||
++g_ftrace;
|
||||
rip = ctx ? ctx->uc_mcontext.rip : 0;
|
||||
if ((gdbpid = IsDebuggerPresent(true))) {
|
||||
DebugBreak();
|
||||
} else if (IsTerminalInarticulate() || IsRunningUnderMake()) {
|
||||
gdbpid = -1;
|
||||
} else if (FindDebugBinary()) {
|
||||
RestoreDefaultCrashSignalHandlers();
|
||||
gdbpid =
|
||||
attachdebugger(((sig == SIGTRAP || sig == SIGQUIT) &&
|
||||
(rip >= (intptr_t)&_base && rip < (intptr_t)&_etext))
|
||||
? rip
|
||||
: 0);
|
||||
if (cmpxchg(&noreentry, false, true)) {
|
||||
err = errno;
|
||||
if ((gdbpid = IsDebuggerPresent(true))) {
|
||||
DebugBreak();
|
||||
} else if (g_isterminalinarticulate || g_isrunningundermake) {
|
||||
gdbpid = -1;
|
||||
} else if (FindDebugBinary()) {
|
||||
RestoreDefaultCrashSignalHandlers();
|
||||
gdbpid =
|
||||
attachdebugger(((sig == SIGTRAP || sig == SIGQUIT) &&
|
||||
(rip >= (intptr_t)&_base && rip < (intptr_t)&_etext))
|
||||
? rip
|
||||
: 0);
|
||||
}
|
||||
if (!(gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT))) {
|
||||
__restore_tty(1);
|
||||
ShowCrashReport(err, sig, si, ctx);
|
||||
_Exit(128 + sig);
|
||||
}
|
||||
} else if (cmpxchg(¬possible, false, true)) {
|
||||
__printf("\n"
|
||||
"\n"
|
||||
"CRASHED WHILE CRASHING WITH SIG%s\n"
|
||||
"%s\n"
|
||||
"RIP %x\n"
|
||||
"RSP %x\n"
|
||||
"RBP %x\n"
|
||||
"\n",
|
||||
TinyStrSignal(sig), __argv[0], rip, ctx ? ctx->uc_mcontext.rsp : 0,
|
||||
ctx ? ctx->uc_mcontext.rbp : 0);
|
||||
_Exit(119);
|
||||
} else {
|
||||
for (;;) {
|
||||
asm("ud2");
|
||||
}
|
||||
}
|
||||
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
|
||||
__restore_tty(1);
|
||||
ShowCrashReport(err, STDERR_FILENO, sig, si, ctx);
|
||||
exit(128 + sig);
|
||||
unreachable;
|
||||
noreentry = false;
|
||||
--g_ftrace;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue