2021-10-14 00:27:13 +00:00
|
|
|
/*-*- 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/bits/bits.h"
|
2022-04-08 03:30:04 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/intrin/lockcmpxchg.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
|
2022-03-16 20:33:13 +00:00
|
|
|
STATIC_YOINK("__get_symbol_by_addr");
|
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
static bool once;
|
|
|
|
static bool hasleaks;
|
|
|
|
|
|
|
|
static noasan void CheckLeak(void *x, void *y, size_t n, void *a) {
|
|
|
|
if (n) {
|
|
|
|
if (IsAsan()) {
|
|
|
|
if (__asan_get_heap_size(x)) {
|
|
|
|
hasleaks = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hasleaks = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static noasan void OnMemory(void *x, void *y, size_t n, void *a) {
|
|
|
|
static int i;
|
|
|
|
if (n) {
|
|
|
|
if (++i < 20) {
|
2022-03-17 07:53:45 +00:00
|
|
|
kprintf("%p %,lu bytes [dlmalloc]", x, n);
|
2021-10-14 00:27:13 +00:00
|
|
|
if (IsAsan()) {
|
|
|
|
__asan_print_trace(x);
|
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("\n");
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
if (i == 20) {
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("etc. etc.\n");
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static noasan bool HasLeaks(void) {
|
|
|
|
malloc_inspect_all(CheckLeak, 0);
|
|
|
|
return hasleaks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for memory leaks.
|
|
|
|
*
|
|
|
|
* This function needs to call __cxa_finalize(). Therefore any runtime
|
|
|
|
* services that depend on malloc() cannot be used, after calling this
|
|
|
|
* function.
|
|
|
|
*/
|
2022-03-18 09:33:37 +00:00
|
|
|
noasan void CheckForMemoryLeaks(void) {
|
2021-10-14 00:27:13 +00:00
|
|
|
struct mallinfo mi;
|
2022-04-12 12:20:17 +00:00
|
|
|
if (!_lockcmpxchg(&once, false, true)) {
|
2022-03-18 09:33:37 +00:00
|
|
|
kprintf("CheckForMemoryLeaks() may only be called once\n");
|
2021-10-15 02:36:49 +00:00
|
|
|
exit(1);
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
__cxa_finalize(0);
|
2022-04-13 05:11:00 +00:00
|
|
|
STRACE("checking for memory leaks% m");
|
2021-10-14 00:27:13 +00:00
|
|
|
if (!IsAsan()) {
|
|
|
|
/* TODO(jart): How can we make this work without ASAN? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
malloc_trim(0);
|
|
|
|
if (HasLeaks()) {
|
|
|
|
mi = mallinfo();
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("\n"
|
|
|
|
"UNFREED MEMORY\n"
|
|
|
|
"%s\n"
|
|
|
|
"max allocated space %,*d\n"
|
|
|
|
"total allocated space %,*d\n"
|
|
|
|
"total free space %,*d\n"
|
|
|
|
"releasable space %,*d\n"
|
|
|
|
"mmaped space %,*d\n"
|
|
|
|
"non-mmapped space %,*d\n"
|
|
|
|
"\n",
|
|
|
|
__argv[0], 16l, mi.usmblks, 16l, mi.uordblks, 16l, mi.fordblks, 16l,
|
|
|
|
mi.hblkhd, 16l, mi.keepcost, 16l, mi.arena);
|
2021-10-14 00:27:13 +00:00
|
|
|
if (!IsAsan()) {
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("# NOTE: Use `make -j8 MODE=dbg` for malloc() backtraces\n");
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
malloc_inspect_all(OnMemory, 0);
|
2022-03-16 20:33:13 +00:00
|
|
|
kprintf("\n");
|
2021-10-14 00:27:13 +00:00
|
|
|
PrintMemoryIntervals(2, &_mmi);
|
|
|
|
/* PrintSystemMappings(2); */
|
|
|
|
/* PrintGarbage(); */
|
2022-03-23 13:31:55 +00:00
|
|
|
__restorewintty();
|
2021-10-14 00:27:13 +00:00
|
|
|
_Exit(78);
|
|
|
|
}
|
|
|
|
}
|