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

@ -10,6 +10,9 @@
* to sort this array before calling bulk_free.
*/
size_t dlbulk_free(void *array[], size_t nelem) {
void **a, **b, *mem, **fence;
struct MallocChunk *p, *next;
size_t psize, newsize, unfreed;
/*
* Try to free all pointers in the given array. Note: this could be
* made faster, by delaying consolidation, at the price of disabling
@ -17,15 +20,15 @@ size_t dlbulk_free(void *array[], size_t nelem) {
* by combining adjacent chunks before freeing, which will occur often
* if allocated with ialloc or the array is sorted.
*/
size_t unfreed = 0;
unfreed = 0;
if (!PREACTION(g_dlmalloc)) {
void **a;
void **fence = &(array[nelem]);
a;
fence = &(array[nelem]);
for (a = array; a != fence; ++a) {
void *mem = *a;
mem = *a;
if (mem != 0) {
mchunkptr p = mem2chunk(AddressDeathAction(mem));
size_t psize = chunksize(p);
p = mem2chunk(AddressDeathAction(mem));
psize = chunksize(p);
#if FOOTERS
if (get_mstate_for(p) != g_dlmalloc) {
++unfreed;
@ -35,10 +38,10 @@ size_t dlbulk_free(void *array[], size_t nelem) {
check_inuse_chunk(g_dlmalloc, p);
*a = 0;
if (RTCHECK(ok_address(g_dlmalloc, p) && ok_inuse(p))) {
void **b = a + 1; /* try to merge with next chunk */
mchunkptr next = next_chunk(p);
b = a + 1; /* try to merge with next chunk */
next = next_chunk(p);
if (b != fence && *b == chunk2mem(next)) {
size_t newsize = chunksize(next) + psize;
newsize = chunksize(next) + psize;
set_inuse(g_dlmalloc, p, newsize);
*b = chunk2mem(p);
} else