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:
Justine Tunney 2021-10-13 17:27:13 -07:00
parent a0b39f886c
commit 226aaf3547
317 changed files with 6474 additions and 3993 deletions

View file

@ -24,9 +24,11 @@
#include "libc/nexgen32e/rdtscp.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#pragma weak stderr
@ -49,7 +51,7 @@ static int g_lastsymbol;
static uint64_t laststamp;
static struct SymbolTable *g_symbols;
static privileged noinstrument noasan int GetNestingLevelImpl(
static privileged noinstrument noasan noubsan int GetNestingLevelImpl(
struct StackFrame *frame) {
int nesting = -2;
while (frame) {
@ -59,7 +61,7 @@ static privileged noinstrument noasan int GetNestingLevelImpl(
return MAX(0, nesting);
}
static privileged noinstrument noasan int GetNestingLevel(
static privileged noinstrument noasan noubsan int GetNestingLevel(
struct StackFrame *frame) {
int nesting;
nesting = GetNestingLevelImpl(frame);
@ -75,7 +77,8 @@ static privileged noinstrument noasan int GetNestingLevel(
* prologues of other functions. We assume those functions behave
* according to the System Five NexGen32e ABI.
*/
privileged noinstrument noasan void ftracer(void) {
privileged noinstrument noasan noubsan void ftracer(void) {
/* asan runtime depends on this function */
int symbol;
uint64_t stamp;
static bool noreentry;
@ -85,11 +88,11 @@ privileged noinstrument noasan void ftracer(void) {
stamp = rdtsc();
frame = __builtin_frame_address(0);
frame = frame->next;
if ((symbol = GetSymbol(g_symbols, frame->addr)) != -1 &&
if ((symbol = __get_symbol(g_symbols, frame->addr)) != -1 &&
symbol != g_lastsymbol) {
g_lastsymbol = symbol;
__printf("+ %*s%s %d\r\n", GetNestingLevel(frame) * 2, "",
GetSymbolName(g_symbols, symbol),
__get_symbol_name(g_symbols, symbol),
(long)(unsignedsubtract(stamp, laststamp) / 3.3));
laststamp = X86_HAVE(RDTSCP) ? rdtscp(0) : rdtsc();
}