<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62623>62623</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            No tsan interceptors for pthread_mutex_clocklock and other new POSIX APIs
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jwakely
      </td>
    </tr>
</table>

<pre>
    POSIX-1:202x includes several new functions that tsan needs to support:

> Austin Group Defect [1216](https://austingroupbugs.net/view.php?id=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:

```c
#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.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV19z2joW_zTi5UwYWTbGfuABQugym6SdJJ3t7gsj5GNQI1teSYbQT39HMoRASKf33j7ejGNG1vn7O390xK2VqxpxRAYTMpj2eOvW2oy-b_kzql1vqYvd6Mvnx_m3q4jEY0bZC8haqLZACxY3aLiCGrdQtrVwUtcW3Jo7cJbXUCMWFpwG2zaNNo7EY0KnhB7e8Q2MW-tkDZ-MbhuYYonCARlMIhalZDAlLFs711jPyGaEzXggX3nqZbuy_RodYbONxG2_WTcknsmCxNPAzXKQFnjTKIkFYdfAi0LWK1g0bm2QFwuh62IhlBbPWy7dgrCMsNwTvlJUrcOXjsT_XyIx27ATaEzxK1Rbc0bF6wIWFqt3tnjk3BpBSetAl-cQ210t1kbX8gdChZU2u_4JuiyblyAdVNw5NNarIim97D1JqUfL4P9babDwmtd8gyB4LVAp7tVCo2Xt7MHkN7Leu0dSeqbvPVB7mr2sEwC8ORXffWhD30P3xtdPSi4FcOU17Q7Z5mFCiwcdtgOz1ErprU-ExuiV4RWs5AYtcCi5sgiNttLJDcKWm9qTlUZXIZ3Pszel3SMOeMf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