Skip to content

No tsan interceptors for pthread_mutex_clocklock and other new POSIX APIs #62623

@jwakely

Description

@jwakely

POSIX-1:202x includes several new functions that TSan needs to support:

Austin Group Defect 1216 is applied, adding pthread_cond_clockwait(), pthread_mutex_clocklock(), pthread_rwlock_clockrdlock(), pthread_rwlock_clockwrlock(), and sem_clockwait() to the list of functions that synchronize memory.

(If it matters, pthread_cond_clockwait is required to have cancellation points, and pthread_rwlock_clockwrlock, pthread_rwlock_clockrdlock, and sem_clockwait may have cancellation points.)

Glibc already supports these, and so the following program gives a false positive warning from TSan:

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts = { 0 };

void* tfunc(void* p)
{
    if (!pthread_mutex_trylock(&m))
    {
        puts("Locked mutex in second thread");
        pthread_mutex_unlock(&m);
    }
    else
        puts("Second thread could not lock mutex");
    return p;
}

int main()
{
    if (!pthread_mutex_clocklock(&m, CLOCK_REALTIME, &ts))
    {
        puts("Locked mutex in main() thread");
        pthread_t thr;
        pthread_create(&thr, 0, tfunc, 0);
        pthread_join(thr, 0);
        pthread_mutex_unlock(&m);
    }
    else
        puts("Failed to lock mutex");
}
==================
WARNING: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) (pid=1)
    #0 pthread_mutex_unlock <null> (libtsan.so.2+0x50241) (BuildId: 4e0d0a71dfb217392f9c0b4e6f757e50fa8e9242)
    #1 main /app/example.c:27 (output.s+0x401287) (BuildId: 06c6ce9eaa9b45a121f91cff2016ac89d2e036dc)

  Location is global 'm' of size 40 at 0x000000404080 (output.s+0x404080)

  Mutex M0 (0x000000404080) created at:
    #0 pthread_mutex_unlock <null> (libtsan.so.2+0x50241) (BuildId: 4e0d0a71dfb217392f9c0b4e6f757e50fa8e9242)
    #1 main /app/example.c:27 (output.s+0x401287) (BuildId: 06c6ce9eaa9b45a121f91cff2016ac89d2e036dc)

SUMMARY: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) (/opt/compiler-explorer/gcc-13.1.0/lib64/libtsan.so.2+0x50241) (BuildId: 4e0d0a71dfb217392f9c0b4e6f757e50fa8e9242) in pthread_mutex_unlock
==================
ThreadSanitizer: reported 1 warnings

A similar false positive is given for any use of functions like std::timed_mutex::try_lock_for with libstdc++, as that uses the new Glibc functions when available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions