mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
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:
parent
26c051c297
commit
af7bd80430
141 changed files with 2094 additions and 1601 deletions
25
third_party/dlmalloc/init.inc
vendored
25
third_party/dlmalloc/init.inc
vendored
|
@ -7,31 +7,34 @@
|
|||
#if LOCK_AT_FORK
|
||||
#if ONLY_MSPACES
|
||||
|
||||
static void dlmalloc_pre_fork(void) {
|
||||
void dlmalloc_pre_fork(void) {
|
||||
mstate h;
|
||||
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
||||
ACQUIRE_MALLOC_GLOBAL_LOCK();
|
||||
for (unsigned i = ARRAYLEN(g_heaps); i--;)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
ACQUIRE_LOCK(&h->mutex);
|
||||
}
|
||||
|
||||
static void dlmalloc_post_fork_parent(void) {
|
||||
void dlmalloc_post_fork_parent(void) {
|
||||
mstate h;
|
||||
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
RELEASE_LOCK(&h->mutex);
|
||||
RELEASE_MALLOC_GLOBAL_LOCK();
|
||||
}
|
||||
|
||||
static void dlmalloc_post_fork_child(void) {
|
||||
void dlmalloc_post_fork_child(void) {
|
||||
mstate h;
|
||||
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
(void)INITIAL_LOCK(&h->mutex);
|
||||
(void)REFRESH_LOCK(&h->mutex);
|
||||
(void)REFRESH_MALLOC_GLOBAL_LOCK();
|
||||
}
|
||||
|
||||
#else
|
||||
static void dlmalloc_pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); }
|
||||
static void dlmalloc_post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); }
|
||||
static void dlmalloc_post_fork_child(void) { (void)INITIAL_LOCK(&(gm)->mutex); }
|
||||
void dlmalloc_pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); }
|
||||
void dlmalloc_post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); }
|
||||
void dlmalloc_post_fork_child(void) { (void)REFRESH_LOCK(&(gm)->mutex); }
|
||||
#endif /* ONLY_MSPACES */
|
||||
#endif /* LOCK_AT_FORK */
|
||||
|
||||
|
@ -95,12 +98,6 @@ __attribute__((__constructor__(49))) int init_mparams(void) {
|
|||
(void)INITIAL_LOCK(&gm->mutex);
|
||||
#endif
|
||||
|
||||
#if LOCK_AT_FORK
|
||||
pthread_atfork(&dlmalloc_pre_fork,
|
||||
&dlmalloc_post_fork_parent,
|
||||
&dlmalloc_post_fork_child);
|
||||
#endif
|
||||
|
||||
{
|
||||
#if USE_DEV_RANDOM
|
||||
int fd;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue