Skip to content

[tsan] Mark pthread_*_lock functions as blocking #84162

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

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
10 changes: 5 additions & 5 deletions compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ TSAN_INTERCEPTOR(int, pthread_mutex_destroy, void *m) {
TSAN_INTERCEPTOR(int, pthread_mutex_lock, void *m) {
SCOPED_TSAN_INTERCEPTOR(pthread_mutex_lock, m);
MutexPreLock(thr, pc, (uptr)m);
int res = REAL(pthread_mutex_lock)(m);
int res = BLOCK_REAL(pthread_mutex_lock)(m);
if (res == errno_EOWNERDEAD)
MutexRepair(thr, pc, (uptr)m);
if (res == 0 || res == errno_EOWNERDEAD)
Expand Down Expand Up @@ -1385,7 +1385,7 @@ TSAN_INTERCEPTOR(int, pthread_mutex_clocklock, void *m,
__sanitizer_clockid_t clock, void *abstime) {
SCOPED_TSAN_INTERCEPTOR(pthread_mutex_clocklock, m, clock, abstime);
MutexPreLock(thr, pc, (uptr)m);
int res = REAL(pthread_mutex_clocklock)(m, clock, abstime);
int res = BLOCK_REAL(pthread_mutex_clocklock)(m, clock, abstime);
if (res == errno_EOWNERDEAD)
MutexRepair(thr, pc, (uptr)m);
if (res == 0 || res == errno_EOWNERDEAD)
Expand All @@ -1403,7 +1403,7 @@ TSAN_INTERCEPTOR(int, pthread_mutex_clocklock, void *m,
TSAN_INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
SCOPED_TSAN_INTERCEPTOR(__pthread_mutex_lock, m);
MutexPreLock(thr, pc, (uptr)m);
int res = REAL(__pthread_mutex_lock)(m);
int res = BLOCK_REAL(__pthread_mutex_lock)(m);
if (res == errno_EOWNERDEAD)
MutexRepair(thr, pc, (uptr)m);
if (res == 0 || res == errno_EOWNERDEAD)
Expand Down Expand Up @@ -1446,7 +1446,7 @@ TSAN_INTERCEPTOR(int, pthread_spin_destroy, void *m) {
TSAN_INTERCEPTOR(int, pthread_spin_lock, void *m) {
SCOPED_TSAN_INTERCEPTOR(pthread_spin_lock, m);
MutexPreLock(thr, pc, (uptr)m);
int res = REAL(pthread_spin_lock)(m);
int res = BLOCK_REAL(pthread_spin_lock)(m);
if (res == 0) {
MutexPostLock(thr, pc, (uptr)m);
}
Expand Down Expand Up @@ -1521,7 +1521,7 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) {
TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m);
MutexPreLock(thr, pc, (uptr)m);
int res = REAL(pthread_rwlock_wrlock)(m);
int res = BLOCK_REAL(pthread_rwlock_wrlock)(m);
if (res == 0) {
MutexPostLock(thr, pc, (uptr)m);
}
Expand Down
71 changes: 71 additions & 0 deletions compiler-rt/test/tsan/signal_in_mutex_lock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// RUN: %clang_tsan %s -lstdc++ -o %t && %run %t 2>&1 | FileCheck %s

#include "test.h"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>

#include <cassert>
#include <condition_variable>
#include <mutex>

std::mutex sampler_mutex; //dummy mutex to lock in the thread we spawn.
std::mutex done_mutex; // guards the cv and done variables.
std::condition_variable cv;
bool done = false;

void *ThreadFunc(void *x) {
while (true) {
// Lock the mutex
std::lock_guard<std::mutex> guard(sampler_mutex);
// Mutex is released at the end
}

return nullptr;
}

static void SigprofHandler(int signal, siginfo_t *info, void *context) {
// Assuming we did some work, change the variable to let the main thread
// know that we are done.
{
std::unique_lock<std::mutex> lck(done_mutex);
done = true;
cv.notify_one();
}
}

int main() {
alarm(60); // Kill the test if it hangs.

// Install the signal handler
struct sigaction sa;
sa.sa_sigaction = SigprofHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigaction(SIGPROF, &sa, 0) != 0) {
fprintf(stderr, "failed to install signal handler\n");
abort();
}

// Spawn a thread that will just loop and get the mutex lock:
pthread_t thread;
pthread_create(&thread, NULL, ThreadFunc, NULL);

// Lock the mutex before sending the signal
std::lock_guard<std::mutex> guard(sampler_mutex);
// From now on thread 1 will be waiting for the lock

// Send the SIGPROF signal to thread.
int r = pthread_kill(thread, SIGPROF);
assert(r == 0);

// Wait until signal handler sends the data.
std::unique_lock lk(done_mutex);
cv.wait(lk, [] { return done; });

// We got the done variable from the signal handler. Exiting successfully.
fprintf(stderr, "PASS\n");
}

// CHECK-NOT: WARNING: ThreadSanitizer:
// CHECK: PASS