cosmopolitan/libc/mem/leaks.h
Justine Tunney af7bd80430
Eliminate cyclic locks in runtime
This change introduces a new deadlock detector for Cosmo's POSIX threads
implementation. Error check mutexes will now track a DAG of nested locks
and report EDEADLK when a deadlock is theoretically possible. These will
occur rarely, but it's important for production hardening your code. You
don't even need to change your mutexes to use the POSIX error check mode
because `cosmocc -mdbg` will enable error checking on mutexes by default
globally. When cycles are found, an error message showing your demangled
symbols describing the strongly connected component are printed and then
the SIGTRAP is raised, which means you'll also get a backtrace if you're
using ShowCrashReports() too. This new error checker is so low-level and
so pure that it's able to verify the relationships of every libc runtime
lock, including those locks upon which the mutex implementation depends.
2024-12-16 22:25:12 -08:00

23 lines
585 B
C

#ifndef COSMOPOLITAN_LIBC_MEM_LEAKS_H_
#define COSMOPOLITAN_LIBC_MEM_LEAKS_H_
#include "libc/intrin/weaken.h"
COSMOPOLITAN_C_START_
void CheckForMemoryLeaks(void) libcesque;
void AssertNoLocksAreHeld(void) libcesque;
/**
* Declares that allocation needn't be freed.
*
* This function does nothing if CheckForMemoryLeaks() hasn't been
* linked into the binary.
*/
forceinline void *may_leak(void *__p) {
void __may_leak(void *) libcesque;
if (_weaken(__may_leak))
_weaken(__may_leak)(__p);
return __p;
}
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_MEM_LEAKS_H_ */