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.
This commit is contained in:
Justine Tunney 2024-12-16 20:51:27 -08:00
parent 26c051c297
commit af7bd80430
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
141 changed files with 2094 additions and 1601 deletions

View file

@ -1,3 +1,7 @@
#include "libc/cosmo.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/maps.h"
#include "libc/thread/thread.h"
/* --------------------------- Lock preliminaries ------------------------ */
@ -33,11 +37,20 @@
#define MLOCK_T atomic_uint
static int malloc_inlk(MLOCK_T *lk) {
atomic_store_explicit(lk, 0, memory_order_relaxed);
return 0;
}
static int malloc_wipe(MLOCK_T *lk) {
atomic_store_explicit(lk, 0, memory_order_relaxed);
return 0;
}
static int malloc_kilk(MLOCK_T *lk) {
return 0;
}
static int malloc_lock(MLOCK_T *lk) {
for (;;) {
if (!atomic_exchange_explicit(lk, 1, memory_order_acquire))
@ -49,36 +62,71 @@ static int malloc_lock(MLOCK_T *lk) {
return 0;
}
static int malloc_unlock(MLOCK_T *lk) {
static int malloc_unlk(MLOCK_T *lk) {
atomic_store_explicit(lk, 0, memory_order_release);
return 0;
}
#else
#define MLOCK_T nsync_mu
#define MLOCK_T struct MallocLock
static int malloc_wipe(MLOCK_T *lk) {
struct MallocLock {
#if DEBUG
void *edges;
#endif
nsync_mu mu;
};
static int malloc_inlk(MLOCK_T *lk) {
bzero(lk, sizeof(*lk));
return 0;
}
static int malloc_lock(MLOCK_T *lk) {
nsync_mu_lock(lk);
static int malloc_wipe(MLOCK_T *lk) {
bzero(&lk->mu, sizeof(lk->mu));
return 0;
}
static int malloc_unlock(MLOCK_T *lk) {
nsync_mu_unlock(lk);
static int malloc_kilk(MLOCK_T *lk) {
return 0;
}
static int malloc_lock(MLOCK_T *lk) {
#if DEBUG
__deadlock_check(lk, 0);
#endif
nsync_mu_lock(&lk->mu);
#if DEBUG
__deadlock_record(lk, 0);
__deadlock_track(lk, 0);
#endif
return 0;
}
static int malloc_unlk(MLOCK_T *lk) {
#if DEBUG
if (__deadlock_tracked(lk) == 0) {
kprintf("error: unlock malloc mutex not owned by caller: %t\n", lk);
DebugBreak();
}
#endif
nsync_mu_unlock(&lk->mu);
#if DEBUG
__deadlock_untrack(lk);
#endif
return 0;
}
#endif
#define ACQUIRE_LOCK(lk) malloc_lock(lk)
#define RELEASE_LOCK(lk) malloc_unlock(lk)
#define INITIAL_LOCK(lk) malloc_wipe(lk)
#define DESTROY_LOCK(lk) malloc_wipe(lk)
#define RELEASE_LOCK(lk) malloc_unlk(lk)
#define INITIAL_LOCK(lk) malloc_inlk(lk)
#define REFRESH_LOCK(lk) malloc_wipe(lk)
#define DESTROY_LOCK(lk) malloc_kilk(lk)
#define INITIAL_MALLOC_GLOBAL_LOCK() INITIAL_LOCK(&malloc_global_mutex);
#define REFRESH_MALLOC_GLOBAL_LOCK() REFRESH_LOCK(&malloc_global_mutex);
#define ACQUIRE_MALLOC_GLOBAL_LOCK() ACQUIRE_LOCK(&malloc_global_mutex);
#define RELEASE_MALLOC_GLOBAL_LOCK() RELEASE_LOCK(&malloc_global_mutex);