Skip to content

Add pthread mutex deadlock detection for debug builds #24607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions system/lib/libc/musl/src/thread/pthread_mutex_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

int __pthread_mutex_lock(pthread_mutex_t *m)
{
#if !defined(__EMSCRIPTEN__) || defined(NDEBUG)
/* XXX EMSCRIPTEN always take the slow path in debug builds so we can trap rather than deadlock */
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
&& !a_cas(&m->_m_lock, 0, EBUSY))
return 0;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should upstream do the same for all platforms? I'm just trying to understand if this is a general improvement or if it works around a specific problem in wasm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its a general improvement for debug builds yes. I guess most libc's don't ship debug builds like we do (especially automatically selecting debug libs based on command line flags, which emcc does is quite unusual).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream musl doesn't really have any/many assertions AFAICT, so its not something they tend to consider I suppose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it looks like upstream musl only has assertion in 2 very specific places:

src/regex/regcomp.c
src/malloc/mallocng/..

No other assertions in whole of libc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks. Makes sense.


return __pthread_mutex_timedlock(m, 0);
}
Expand Down
10 changes: 10 additions & 0 deletions system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "pthread_impl.h"

#ifdef __EMSCRIPTEN__
#include <assert.h>
#endif

#ifndef __EMSCRIPTEN__
#define IS32BIT(x) !((x)+0x80000000ULL>>32)
#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
Expand Down Expand Up @@ -57,9 +61,12 @@ static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct

int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
{
#if !defined(__EMSCRIPTEN__) || defined(NDEBUG)
/* XXX EMSCRIPTEN always take the slow path in debug builds so we can trap rather than deadlock */
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
&& !a_cas(&m->_m_lock, 0, EBUSY))
return 0;
#endif

int type = m->_m_type;
int r, t, priv = (type & 128) ^ 128;
Expand All @@ -79,6 +86,9 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
int own = r & 0x3fffffff;
if (!own && (!r || (type&4)))
continue;
#if defined(__EMSCRIPTEN__) && !defined(NDEBUG)
assert(own != __pthread_self()->tid && "pthread mutex deadlock detected");
#endif
if ((type&3) == PTHREAD_MUTEX_ERRORCHECK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we perhaps enable this PTHREAD_MUTEX_ERRORCHECK globally somehow, rather than add these three lines? (It wouldn't assert though, just return EDEADLK)

&& own == __pthread_self()->tid)
return EDEADLK;
Expand Down
3 changes: 3 additions & 0 deletions system/lib/libc/musl/src/thread/pthread_mutex_trylock.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m)

int __pthread_mutex_trylock(pthread_mutex_t *m)
{
#if !defined(__EMSCRIPTEN__) || defined(NDEBUG)
/* XXX EMSCRIPTEN always take the slow path in debug builds so we can trap rather than deadlock */
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL)
return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
#endif
return __pthread_mutex_trylock_owner(m);
}

Expand Down
9 changes: 5 additions & 4 deletions system/lib/pthread/library_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ weak_alias(dummy_tsd, __pthread_tsd_main);
// See system/lib/README.md for static constructor ordering.
__attribute__((constructor(48)))
void _emscripten_init_main_thread(void) {
_emscripten_init_main_thread_js(&__main_pthread);

// The pthread struct has a field that points to itself - this is used as
// a magic ID to detect whether the pthread_t structure is 'alive'.
__main_pthread.self = &__main_pthread;
__main_pthread.stack = (void*)emscripten_stack_get_base();
__main_pthread.stack_size = emscripten_stack_get_base() - emscripten_stack_get_end();
__main_pthread.detach_state = DT_JOINABLE;
// pthread struct robust_list head should point to itself.
__main_pthread.robust_list.head = &__main_pthread.robust_list.head;
Expand All @@ -157,6 +153,11 @@ void _emscripten_init_main_thread(void) {
__main_pthread.next = __main_pthread.prev = &__main_pthread;
__main_pthread.tsd = (void **)__pthread_tsd_main;

_emscripten_init_main_thread_js(&__main_pthread);

__main_pthread.stack = (void*)emscripten_stack_get_base();
__main_pthread.stack_size = emscripten_stack_get_base() - emscripten_stack_get_end();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the stack setup be as early as possible? Why move it down?


_emscripten_thread_mailbox_init(&__main_pthread);
_emscripten_thread_mailbox_await(&__main_pthread);
}
20 changes: 20 additions & 0 deletions test/other/test_pthread_mutex_deadlock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

int main() {
printf("in main\n");
int rtn = pthread_mutex_lock(&m);
assert(rtn == 0);

// Attempt to lock a second time. In debug builds this should
// hit an assertion. In release builds this will deadlock and
// never return.
pthread_mutex_lock(&m);
printf("should never get here\n");
assert(false);
return 0;
}
6 changes: 6 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -12946,6 +12946,12 @@ def test_pthread_reuse(self):
def test_pthread_hello(self, args):
self.do_other_test('test_pthread_hello.c', args)

@crossplatform
@node_pthreads
def test_pthread_mutex_deadlock(self):
self.do_runf('other/test_pthread_mutex_deadlock.c', 'pthread mutex deadlock detected',
cflags=['-g'], assert_returncode=NON_ZERO)

@node_pthreads
def test_pthread_relocatable(self):
self.do_run_in_out_file_test('hello_world.c', cflags=['-sRELOCATABLE'])
Expand Down