[compiler-rt] [sanitizer_common] Add tests for the C11 <threads.h> functions (PR #229066)

Krzysztof Jędruczyk via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 02:37:17 PDT 2026


https://github.com/kjedruczyk updated https://github.com/llvm/llvm-project/pull/229066

>From 287acb16c6f1cbe4446e361ed109e233ad8e8d2a Mon Sep 17 00:00:00 2001
From: Krzysztof Jedruczyk <kjedruczyk at bloomberg.net>
Date: Mon, 5 Oct 2026 12:53:58 +0100
Subject: [PATCH 1/3] [sanitizer_common] Add tests for the C11 <threads.h>
 functions

Add one test per C11 family (thrd_*, mtx_*, cnd_*, call_once, tss_*),
marked UNSUPPORTED on glibc for the sanitizers that fail today. Add a
glibc-2.28 lit feature, since <threads.h> appeared in glibc 2.28.
---
 compiler-rt/test/lit.common.cfg.py            |   1 +
 .../TestCases/Posix/c11_call_once.c           |  58 ++++++
 .../TestCases/Posix/c11_cnd.c                 | 196 ++++++++++++++++++
 .../TestCases/Posix/c11_mtx.c                 | 138 ++++++++++++
 .../TestCases/Posix/c11_thrd.c                | 177 ++++++++++++++++
 .../TestCases/Posix/c11_tss.c                 | 134 ++++++++++++
 6 files changed, 704 insertions(+)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c

diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py
index 64e6ede9b19b4..a7c9efaf2ddbc 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -743,6 +743,7 @@ def get_macos_aligned_version(macos_vers):
         for required in [
             (2, 19),
             (2, 27),
+            (2, 28),
             (2, 30),
             (2, 33),
             (2, 34),
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
new file mode 100644
index 0000000000000..8bf2598fb0757
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
@@ -0,0 +1,58 @@
+// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// The threads come from pthread_create, not thrd_create, so that a failure
+// here is due to call_once alone; thrd_* has its own test.
+
+// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// UNSUPPORTED: glibc && tsan
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+
+enum { kNumThreads = 4 };
+
+static once_flag flag = ONCE_FLAG_INIT;
+static int init_calls;
+static int shared_data;
+
+static void init(void) {
+  ++init_calls;
+  shared_data = 42;
+}
+
+static void *thread_func(void *arg) {
+  (void)arg;
+  call_once(&flag, init);
+  if (shared_data != 42) {
+    fprintf(stderr, "shared_data is %d after call_once\n", shared_data);
+    abort();
+  }
+  return NULL;
+}
+
+int main(void) {
+  pthread_t threads[kNumThreads];
+  for (int i = 0; i < kNumThreads; ++i) {
+    if (pthread_create(&threads[i], NULL, thread_func, NULL) != 0) {
+      fprintf(stderr, "pthread_create failed\n");
+      abort();
+    }
+  }
+  for (int i = 0; i < kNumThreads; ++i) {
+    if (pthread_join(threads[i], NULL) != 0) {
+      fprintf(stderr, "pthread_join failed\n");
+      abort();
+    }
+  }
+  if (init_calls != 1) {
+    fprintf(stderr, "init ran %d times\n", init_calls);
+    abort();
+  }
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
new file mode 100644
index 0000000000000..e91f6a7dcdc2f
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
@@ -0,0 +1,196 @@
+// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// The threads come from pthread_create, not thrd_create, so that a failure
+// here is due to the cnd_* (and mtx_*) functions alone; thrd_* has its own
+// test.
+
+// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// UNSUPPORTED: glibc && tsan
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+#include <time.h>
+
+static void check(int res, const char *what) {
+  if (res != thrd_success) {
+    fprintf(stderr, "%s failed: %d\n", what, res);
+    abort();
+  }
+}
+
+static void fail(const char *what) {
+  fprintf(stderr, "%s\n", what);
+  abort();
+}
+
+static pthread_t start_thread(void *(*func)(void *)) {
+  pthread_t t;
+  if (pthread_create(&t, NULL, func, NULL) != 0)
+    fail("pthread_create failed");
+  return t;
+}
+
+static void join_thread(pthread_t t) {
+  if (pthread_join(t, NULL) != 0)
+    fail("pthread_join failed");
+}
+
+// Protects all the state below.
+static mtx_t mtx;
+
+// cnd_signal and cnd_wait: a producer hands items to the main thread through
+// a one-slot buffer.
+enum { kItems = 100 };
+static cnd_t not_empty, not_full;
+static int slot_full;
+static int slot;
+
+static void *producer(void *arg) {
+  (void)arg;
+  for (int i = 0; i < kItems; ++i) {
+    check(mtx_lock(&mtx), "mtx_lock");
+    while (slot_full)
+      check(cnd_wait(&not_full, &mtx), "cnd_wait");
+    slot = i;
+    slot_full = 1;
+    check(cnd_signal(&not_empty), "cnd_signal");
+    check(mtx_unlock(&mtx), "mtx_unlock");
+  }
+  return NULL;
+}
+
+static void test_signal_wait(void) {
+  pthread_t t = start_thread(producer);
+  for (int i = 0; i < kItems; ++i) {
+    check(mtx_lock(&mtx), "mtx_lock");
+    while (!slot_full)
+      check(cnd_wait(&not_empty, &mtx), "cnd_wait");
+    if (slot != i)
+      fail("wrong item");
+    slot_full = 0;
+    check(cnd_signal(&not_full), "cnd_signal");
+    check(mtx_unlock(&mtx), "mtx_unlock");
+  }
+  join_thread(t);
+}
+
+// cnd_broadcast: one broadcast wakes every thread waiting on go_cnd. A waiter
+// that the broadcast misses never returns, so the test then hangs in
+// join_thread rather than failing.
+enum { kWaiters = 4 };
+static cnd_t ready_cnd, go_cnd;
+static int waiting, go;
+
+static void *waiter(void *arg) {
+  (void)arg;
+  check(mtx_lock(&mtx), "mtx_lock");
+  ++waiting;
+  check(cnd_signal(&ready_cnd), "cnd_signal");
+  while (!go)
+    check(cnd_wait(&go_cnd, &mtx), "cnd_wait");
+  check(mtx_unlock(&mtx), "mtx_unlock");
+  return NULL;
+}
+
+static void test_broadcast(void) {
+  pthread_t t[kWaiters];
+  for (int i = 0; i < kWaiters; ++i)
+    t[i] = start_thread(waiter);
+  check(mtx_lock(&mtx), "mtx_lock");
+  // A waiter holds mtx from its increment until cnd_wait releases it, so once
+  // all have counted themselves, all are blocked in cnd_wait.
+  while (waiting < kWaiters)
+    check(cnd_wait(&ready_cnd, &mtx), "cnd_wait");
+  go = 1;
+  check(cnd_broadcast(&go_cnd), "cnd_broadcast");
+  check(mtx_unlock(&mtx), "mtx_unlock");
+  for (int i = 0; i < kWaiters; ++i)
+    join_thread(t[i]);
+}
+
+// cnd_timedwait: an absolute TIME_UTC deadline, first one that expires, then
+// one met by a signal from another thread.
+static cnd_t timed_cnd;
+static int timed_ready;
+
+// C11 timeouts are TIME_UTC, which is CLOCK_REALTIME. MSan intercepts
+// clock_gettime, so the deadline it returns is not reported as uninitialized.
+static struct timespec deadline_after_ms(long ms) {
+  struct timespec ts;
+  if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
+    fail("clock_gettime failed");
+  ts.tv_sec += ms / 1000;
+  ts.tv_nsec += (ms % 1000) * 1000000L;
+  if (ts.tv_nsec >= 1000000000L) {
+    ++ts.tv_sec;
+    ts.tv_nsec -= 1000000000L;
+  }
+  return ts;
+}
+
+static void *timed_signaler(void *arg) {
+  (void)arg;
+  check(mtx_lock(&mtx), "mtx_lock");
+  timed_ready = 1;
+  check(cnd_signal(&timed_cnd), "cnd_signal");
+  check(mtx_unlock(&mtx), "mtx_unlock");
+  return NULL;
+}
+
+static void test_timedwait(void) {
+  check(mtx_lock(&mtx), "mtx_lock");
+
+  // Nothing signals timed_cnd yet, so the wait must time out; an earlier
+  // wakeup is spurious.
+  struct timespec deadline = deadline_after_ms(10);
+  int res;
+  do
+    res = cnd_timedwait(&timed_cnd, &mtx, &deadline);
+  while (res == thrd_success);
+  if (res != thrd_timedout)
+    fail("cnd_timedwait did not time out");
+
+  // The signaler starts while this thread holds mtx, so it can only set
+  // timed_ready while this thread waits in cnd_timedwait. Loop on the
+  // predicate: a timeout is not an error if the predicate has become true,
+  // and if it has not (only on a very slow machine), wait with a new deadline.
+  pthread_t t = start_thread(timed_signaler);
+  deadline = deadline_after_ms(1000);
+  while (!timed_ready) {
+    res = cnd_timedwait(&timed_cnd, &mtx, &deadline);
+    if (res == thrd_timedout)
+      deadline = deadline_after_ms(1000);
+    else
+      check(res, "cnd_timedwait");
+  }
+  check(mtx_unlock(&mtx), "mtx_unlock");
+  join_thread(t);
+}
+
+int main(void) {
+  check(mtx_init(&mtx, mtx_plain), "mtx_init");
+  check(cnd_init(&not_empty), "cnd_init");
+  check(cnd_init(&not_full), "cnd_init");
+  check(cnd_init(&ready_cnd), "cnd_init");
+  check(cnd_init(&go_cnd), "cnd_init");
+  check(cnd_init(&timed_cnd), "cnd_init");
+
+  test_signal_wait();
+  test_broadcast();
+  test_timedwait();
+
+  cnd_destroy(&timed_cnd);
+  cnd_destroy(&go_cnd);
+  cnd_destroy(&ready_cnd);
+  cnd_destroy(&not_full);
+  cnd_destroy(&not_empty);
+  mtx_destroy(&mtx);
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
new file mode 100644
index 0000000000000..c2e56d51f53a1
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
@@ -0,0 +1,138 @@
+// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// The threads come from pthread_create, not thrd_create, and yield with
+// sched_yield, not thrd_yield, so that a failure here is due to the mtx_*
+// functions alone; thrd_* has its own test.
+
+// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// UNSUPPORTED: glibc && tsan
+
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+#include <time.h>
+
+enum { kThreads = 4, kIterations = 1000 };
+
+static mtx_t plain_mtx;     // Guards plain_count.
+static mtx_t timed_mtx;     // Guards timed_count.
+static mtx_t recursive_mtx; // Guards recursive_count.
+static long plain_count, timed_count, recursive_count;
+
+// An absolute TIME_UTC (that is, CLOCK_REALTIME) deadline that is never
+// reached.
+static struct timespec future;
+// An absolute TIME_UTC deadline that has passed.
+static const struct timespec epoch = {0, 0};
+
+static void expect(int res, int expected, const char *what) {
+  if (res != expected) {
+    fprintf(stderr, "%s returned %d, expected %d\n", what, res, expected);
+    abort();
+  }
+}
+
+static void check(int res, const char *what) {
+  expect(res, thrd_success, what);
+}
+
+static void start(pthread_t *thread, void *(*func)(void *)) {
+  if (pthread_create(thread, NULL, func, NULL)) {
+    fprintf(stderr, "pthread_create failed\n");
+    abort();
+  }
+}
+
+static void join(pthread_t thread) {
+  if (pthread_join(thread, NULL)) {
+    fprintf(stderr, "pthread_join failed\n");
+    abort();
+  }
+}
+
+// Runs while main holds plain_mtx and timed_mtx, so it must not get either.
+static void *contender(void *arg) {
+  (void)arg;
+  expect(mtx_trylock(&plain_mtx), thrd_busy, "mtx_trylock (held)");
+  expect(mtx_timedlock(&timed_mtx, &epoch), thrd_timedout,
+         "mtx_timedlock (held)");
+  return NULL;
+}
+
+static void *worker(void *arg) {
+  (void)arg;
+  for (int i = 0; i < kIterations; ++i) {
+    check(mtx_lock(&plain_mtx), "mtx_lock");
+    ++plain_count;
+    check(mtx_unlock(&plain_mtx), "mtx_unlock");
+
+    int res;
+    while ((res = mtx_trylock(&plain_mtx)) == thrd_busy)
+      sched_yield();
+    check(res, "mtx_trylock");
+    ++plain_count;
+    check(mtx_unlock(&plain_mtx), "mtx_unlock");
+
+    check(mtx_timedlock(&timed_mtx, &future), "mtx_timedlock");
+    ++timed_count;
+    check(mtx_unlock(&timed_mtx), "mtx_unlock");
+
+    // The second lock checks that mtx_recursive is honoured: a non-recursive
+    // mutex would deadlock here.
+    check(mtx_lock(&recursive_mtx), "mtx_lock");
+    check(mtx_lock(&recursive_mtx), "mtx_lock (recursive)");
+    ++recursive_count;
+    check(mtx_unlock(&recursive_mtx), "mtx_unlock (recursive)");
+    check(mtx_unlock(&recursive_mtx), "mtx_unlock");
+  }
+  return NULL;
+}
+
+int main(void) {
+  check(mtx_init(&plain_mtx, mtx_plain), "mtx_init(mtx_plain)");
+  check(mtx_init(&timed_mtx, mtx_timed), "mtx_init(mtx_timed)");
+  check(mtx_init(&recursive_mtx, mtx_plain | mtx_recursive),
+        "mtx_init(mtx_plain | mtx_recursive)");
+
+  if (clock_gettime(CLOCK_REALTIME, &future) != 0) {
+    fprintf(stderr, "clock_gettime failed\n");
+    abort();
+  }
+  future.tv_sec += 3600;
+
+  // Failed attempts to lock, made deterministic by holding the mutexes.
+  check(mtx_lock(&plain_mtx), "mtx_lock");
+  check(mtx_lock(&timed_mtx), "mtx_lock");
+  pthread_t thread;
+  start(&thread, contender);
+  join(thread);
+  check(mtx_unlock(&timed_mtx), "mtx_unlock");
+  check(mtx_unlock(&plain_mtx), "mtx_unlock");
+
+  // Contended locking: the counters are only correct, and race-free, if the
+  // mutexes provide mutual exclusion.
+  pthread_t threads[kThreads];
+  for (int i = 0; i < kThreads; ++i)
+    start(&threads[i], worker);
+  for (int i = 0; i < kThreads; ++i)
+    join(threads[i]);
+
+  long expected = (long)kThreads * kIterations;
+  if (plain_count != 2 * expected || timed_count != expected ||
+      recursive_count != expected) {
+    fprintf(stderr, "wrong counts: %ld %ld %ld\n", plain_count, timed_count,
+            recursive_count);
+    abort();
+  }
+
+  mtx_destroy(&plain_mtx);
+  mtx_destroy(&timed_mtx);
+  mtx_destroy(&recursive_mtx);
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
new file mode 100644
index 0000000000000..9f5423fa78ea0
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
@@ -0,0 +1,177 @@
+// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// UNSUPPORTED: glibc && (asan || hwasan || lsan || msan || tsan)
+
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+#include <time.h>
+#include <unistd.h>
+
+enum { kNumThreads = 4 };
+
+typedef struct {
+  int id;
+  thrd_t parent; // Set by main before thrd_create.
+  thrd_t self;   // Set by the thread.
+  int sum;       // Set by the thread.
+} Work;
+
+static atomic_int detached_done;
+
+// Main hands the block over to the holder thread, which clears this pointer.
+// The block is not the thrd_create argument because a sanitizer may keep that
+// reachable on its own while the thread runs.
+static void *handoff;
+static atomic_int holder_ready;
+
+static void check(int res, const char *call) {
+  if (res != thrd_success) {
+    fprintf(stderr, "%s failed: %d\n", call, res);
+    abort();
+  }
+}
+
+static void fail(const char *msg, int id) {
+  fprintf(stderr, "thread %d: %s\n", id, msg);
+  abort();
+}
+
+// Runs on each new thread: records its identity, sleeps briefly, then
+// allocates and frees.
+static void do_work(Work *w) {
+  w->self = thrd_current();
+  if (!thrd_equal(w->self, thrd_current()))
+    fail("thrd_current is not stable", w->id);
+  if (thrd_equal(w->self, w->parent))
+    fail("thrd_current equals the parent thread", w->id);
+
+  // thrd_sleep returns 0 on success, not thrd_success.
+  struct timespec ts = {.tv_sec = 0, .tv_nsec = 1000};
+  if (thrd_sleep(&ts, NULL) != 0)
+    fail("thrd_sleep failed", w->id);
+
+  int n = 100 + w->id;
+  int *v = malloc(n * sizeof(*v));
+  if (!v)
+    fail("malloc failed", w->id);
+  for (int i = 0; i < n; ++i)
+    v[i] = i;
+  w->sum = 0;
+  for (int i = 0; i < n; ++i)
+    w->sum += v[i];
+  free(v);
+}
+
+static int returning_thread(void *arg) {
+  Work *w = arg;
+  do_work(w);
+  return 1000 + w->id;
+}
+
+static int exiting_thread(void *arg) {
+  Work *w = arg;
+  do_work(w);
+  thrd_exit(2000 + w->id);
+}
+
+static int detached_thread(void *arg) {
+  Work *w = arg;
+  do_work(w);
+  atomic_store_explicit(&detached_done, 1, memory_order_release);
+  return 0;
+}
+
+static void check_work(const Work *w) {
+  int n = 100 + w->id;
+  if (w->sum != n * (n - 1) / 2)
+    fail("wrong sum", w->id);
+}
+
+// Keeps the only pointer to the block on its stack until the process exits.
+static int holder_thread(void *arg) {
+  (void)arg;
+  void *volatile block = handoff;
+  handoff = NULL;
+  atomic_store_explicit(&holder_ready, 1, memory_order_release);
+  // block is never null: this sleeps until the process exits.
+  while (block)
+    pause();
+  return 0;
+}
+
+// Main allocates the block, not the holder thread: the leak check ignores a
+// chunk whose allocation stack has no caller frame, and that is all an
+// allocation gets on a thread that the sanitizer does not know about.
+// noinline keeps the pointer out of main's frame.
+__attribute__((noinline)) static void allocate_block(void) {
+  handoff = malloc(1337);
+  if (!handoff) {
+    fprintf(stderr, "malloc failed\n");
+    abort();
+  }
+}
+
+int main(void) {
+  // Start the holder first, so that the rest of main overwrites any stale copy
+  // of the block's address on main's stack. The holder is neither joined nor
+  // detached: it is still running at the leak check.
+  allocate_block();
+  thrd_t holder;
+  check(thrd_create(&holder, holder_thread, NULL), "thrd_create");
+  while (!atomic_load_explicit(&holder_ready, memory_order_acquire))
+    thrd_yield();
+
+  // Threads [0, kNumThreads) return their result, threads
+  // [kNumThreads, 2 * kNumThreads) deliver it through thrd_exit, and the last
+  // Work is for a detached thread.
+  Work work[2 * kNumThreads + 1];
+  thrd_t threads[2 * kNumThreads];
+  thrd_t main_thread = thrd_current();
+  for (int i = 0; i < 2 * kNumThreads + 1; ++i) {
+    work[i].id = i;
+    work[i].parent = main_thread;
+  }
+
+  thrd_t detached;
+  check(thrd_create(&detached, detached_thread, &work[2 * kNumThreads]),
+        "thrd_create");
+  check(thrd_detach(detached), "thrd_detach");
+
+  for (int i = 0; i < 2 * kNumThreads; ++i)
+    check(thrd_create(&threads[i],
+                      i < kNumThreads ? returning_thread : exiting_thread,
+                      &work[i]),
+          "thrd_create");
+
+  // Wait for the detached thread without mtx_* or cnd_*, so that this test
+  // depends only on the thrd_* interceptors. Waiting before the joins gives it
+  // time to finish exiting; if it is still exiting at the leak check, that is
+  // harmless.
+  while (!atomic_load_explicit(&detached_done, memory_order_acquire))
+    thrd_yield();
+  check_work(&work[2 * kNumThreads]);
+
+  for (int i = 0; i < 2 * kNumThreads; ++i) {
+    // thrd_join accepts a null result pointer.
+    if (i == kNumThreads - 1) {
+      check(thrd_join(threads[i], NULL), "thrd_join");
+    } else {
+      int res;
+      check(thrd_join(threads[i], &res), "thrd_join");
+      if (res != (i < kNumThreads ? 1000 : 2000) + i)
+        fail("wrong thrd_join result", i);
+    }
+    if (!thrd_equal(work[i].self, threads[i]))
+      fail("thrd_current differs from the thrd_t set by thrd_create", i);
+    check_work(&work[i]);
+  }
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
new file mode 100644
index 0000000000000..471c1f05b0c50
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
@@ -0,0 +1,134 @@
+// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// The threads come from pthread_create, not thrd_create, so that a failure
+// here is due to the tss_* functions alone; thrd_* has its own test.
+
+// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// UNSUPPORTED: glibc && msan
+
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <threads.h>
+
+enum { kNumThreads = 4 };
+
+// The keys are in heap memory: a global tss_t is zero-initialized, so MSan
+// would see it as initialized whatever tss_create does.
+typedef struct {
+  tss_t with_dtor;
+  tss_t without_dtor;
+} Keys;
+
+static Keys *keys;
+static int sentinel; // The value stored under keys->without_dtor.
+static atomic_int dtor_calls;
+static atomic_uint dtor_ids; // Bit i is set when thread i's value is destroyed.
+
+static void check(int res, const char *call) {
+  if (res != thrd_success) {
+    fprintf(stderr, "%s failed: %d\n", call, res);
+    abort();
+  }
+}
+
+static void fail(const char *msg, int id) {
+  fprintf(stderr, "thread %d: %s\n", id, msg);
+  abort();
+}
+
+static void dtor(void *value) {
+  atomic_fetch_or_explicit(&dtor_ids, 1u << *(int *)value,
+                           memory_order_relaxed);
+  free(value);
+  atomic_fetch_add_explicit(&dtor_calls, 1, memory_order_relaxed);
+}
+
+// Sets and reads back a value under each key on the calling thread.
+static void use_keys(int id) {
+  if (tss_get(keys->with_dtor) || tss_get(keys->without_dtor))
+    fail("tss_get on a new key is not null", id);
+
+  int *value = malloc(sizeof(*value));
+  if (!value)
+    fail("malloc failed", id);
+  *value = id;
+  check(tss_set(keys->with_dtor, value), "tss_set");
+  check(tss_set(keys->without_dtor, &sentinel), "tss_set");
+
+  int *got = tss_get(keys->with_dtor);
+  if (got != value || *got != id)
+    fail("tss_get returned the wrong value", id);
+  if (tss_get(keys->without_dtor) != &sentinel)
+    fail("tss_get returned the wrong value", id);
+}
+
+static void *thread_func(void *arg) {
+  use_keys((int)(long)arg);
+  // Returning runs the destructor for keys->with_dtor.
+  return NULL;
+}
+
+int main(void) {
+  keys = malloc(sizeof(*keys));
+  if (!keys) {
+    fprintf(stderr, "malloc failed\n");
+    abort();
+  }
+  check(tss_create(&keys->with_dtor, dtor), "tss_create");
+  check(tss_create(&keys->without_dtor, NULL), "tss_create");
+  // Two live keys are distinct. Comparing them in instrumented code also means
+  // MSan sees the keys used whether or not it checks call arguments eagerly.
+  if (keys->with_dtor == keys->without_dtor) {
+    fprintf(stderr, "tss_create returned the same key twice\n");
+    abort();
+  }
+
+  pthread_t threads[kNumThreads];
+  for (int i = 0; i < kNumThreads; ++i) {
+    if (pthread_create(&threads[i], NULL, thread_func, (void *)(long)(i + 1))) {
+      fprintf(stderr, "pthread_create failed\n");
+      abort();
+    }
+  }
+  for (int i = 0; i < kNumThreads; ++i) {
+    if (pthread_join(threads[i], NULL)) {
+      fprintf(stderr, "pthread_join failed\n");
+      abort();
+    }
+  }
+  int calls = atomic_load_explicit(&dtor_calls, memory_order_relaxed);
+  unsigned ids = atomic_load_explicit(&dtor_ids, memory_order_relaxed);
+  unsigned expected_ids = (1u << (kNumThreads + 1)) - 2; // Threads 1 to N.
+  if (calls != kNumThreads || ids != expected_ids) {
+    fprintf(stderr, "destructor: %d calls, ids 0x%x; expected %d, 0x%x\n",
+            calls, ids, kNumThreads, expected_ids);
+    abort();
+  }
+
+  // Destructors run neither on tss_delete nor at program exit, so the main
+  // thread frees its own value. Setting it to null exercises tss_set with null.
+  use_keys(0);
+  free(tss_get(keys->with_dtor));
+  check(tss_set(keys->with_dtor, NULL), "tss_set");
+  tss_delete(keys->with_dtor);
+  tss_delete(keys->without_dtor);
+
+  // A key created after tss_delete, which may reuse a deleted slot, starts out
+  // null.
+  tss_t fresh;
+  check(tss_create(&fresh, NULL), "tss_create");
+  if (tss_get(fresh)) {
+    fprintf(stderr, "tss_get on a key created after tss_delete is not null\n");
+    abort();
+  }
+  tss_delete(fresh);
+  free(keys);
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE

>From 0de47652cc7707da4f21da2494ff90b620f1e49a Mon Sep 17 00:00:00 2001
From: Krzysztof Jedruczyk <kjedruczyk at bloomberg.net>
Date: Tue, 6 Oct 2026 10:18:04 +0100
Subject: [PATCH 2/3] Add -pthread build flag to sanitizer c11 threads tests

---
 .../test/sanitizer_common/TestCases/Posix/c11_call_once.c       | 2 +-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c     | 2 +-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c     | 2 +-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c    | 2 +-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c     | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
index 8bf2598fb0757..ca64b310023eb 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
@@ -1,4 +1,4 @@
-// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
 // The threads come from pthread_create, not thrd_create, so that a failure
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
index e91f6a7dcdc2f..9b38606250cad 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
@@ -1,4 +1,4 @@
-// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
 // The threads come from pthread_create, not thrd_create, so that a failure
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
index c2e56d51f53a1..f13cf0942b92d 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
@@ -1,4 +1,4 @@
-// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
 // The threads come from pthread_create, not thrd_create, and yield with
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
index 9f5423fa78ea0..c9df4c0d2f686 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
@@ -1,4 +1,4 @@
-// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
 // UNSUPPORTED: darwin, android, glibc && !glibc-2.28
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
index 471c1f05b0c50..f065882f00a8f 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
@@ -1,4 +1,4 @@
-// RUN: %clang %s -o %t %if freebsd %{ -lstdthreads %}
+// RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
 // The threads come from pthread_create, not thrd_create, so that a failure

>From df45b5f1885d1c2a0a9e3a0e8217974c94c44e1e Mon Sep 17 00:00:00 2001
From: Krzysztof Jedruczyk <kjedruczyk at bloomberg.net>
Date: Tue, 6 Oct 2026 10:25:40 +0100
Subject: [PATCH 3/3] Added comments explaining sanitizer c11 threading test
 exclusions.

Also removed android from exclusion list as it was simply untested. Looking at
NDK I believe the tests should pass under supported sanitizers.
---
 .../test/sanitizer_common/TestCases/Posix/c11_call_once.c     | 4 +++-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c   | 4 +++-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c   | 4 +++-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c  | 4 +++-
 compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c   | 4 +++-
 5 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
index ca64b310023eb..69dc883531269 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_call_once.c
@@ -4,7 +4,9 @@
 // The threads come from pthread_create, not thrd_create, so that a failure
 // here is due to call_once alone; thrd_* has its own test.
 
-// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// <threads.h> is missing on Darwin and before glibc 2.28.
+// UNSUPPORTED: darwin, glibc && !glibc-2.28
+// https://github.com/llvm/llvm-project/issues/199585
 // UNSUPPORTED: glibc && tsan
 
 #include <pthread.h>
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
index 9b38606250cad..d5f2840159765 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_cnd.c
@@ -5,7 +5,9 @@
 // here is due to the cnd_* (and mtx_*) functions alone; thrd_* has its own
 // test.
 
-// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// <threads.h> is missing on Darwin and before glibc 2.28.
+// UNSUPPORTED: darwin, glibc && !glibc-2.28
+// https://github.com/llvm/llvm-project/issues/199585
 // UNSUPPORTED: glibc && tsan
 
 #include <pthread.h>
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
index f13cf0942b92d..74259c4539128 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_mtx.c
@@ -5,7 +5,9 @@
 // sched_yield, not thrd_yield, so that a failure here is due to the mtx_*
 // functions alone; thrd_* has its own test.
 
-// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// <threads.h> is missing on Darwin and before glibc 2.28.
+// UNSUPPORTED: darwin, glibc && !glibc-2.28
+// https://github.com/llvm/llvm-project/issues/199585
 // UNSUPPORTED: glibc && tsan
 
 #include <pthread.h>
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
index c9df4c0d2f686..67e1c0e711c42 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_thrd.c
@@ -1,7 +1,9 @@
 // RUN: %clang -pthread %s -o %t %if freebsd %{ -lstdthreads %}
 // RUN: %run %t 2>&1 | FileCheck %s
 
-// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// <threads.h> is missing on Darwin and before glibc 2.28.
+// UNSUPPORTED: darwin, glibc && !glibc-2.28
+// https://github.com/llvm/llvm-project/issues/199585
 // UNSUPPORTED: glibc && (asan || hwasan || lsan || msan || tsan)
 
 #include <stdatomic.h>
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
index f065882f00a8f..b84c4c5bd529f 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/c11_tss.c
@@ -4,7 +4,9 @@
 // The threads come from pthread_create, not thrd_create, so that a failure
 // here is due to the tss_* functions alone; thrd_* has its own test.
 
-// UNSUPPORTED: darwin, android, glibc && !glibc-2.28
+// <threads.h> is missing on Darwin and before glibc 2.28.
+// UNSUPPORTED: darwin, glibc && !glibc-2.28
+// https://github.com/llvm/llvm-project/issues/199585
 // UNSUPPORTED: glibc && msan
 
 #include <pthread.h>



More information about the llvm-commits mailing list