reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
//===-- tsan_posix.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_interface.h"
#include "tsan_posix_util.h"
#include "tsan_test_util.h"
#include "gtest/gtest.h"
#include <pthread.h>

struct thread_key {
  pthread_key_t key;
  pthread_mutex_t *mtx;
  int val;
  int *cnt;
  thread_key(pthread_key_t key, pthread_mutex_t *mtx, int val, int *cnt)
    : key(key)
    , mtx(mtx)
    , val(val)
    , cnt(cnt) {
  }
};

static void thread_secific_dtor(void *v) {
  thread_key *k = (thread_key *)v;
  EXPECT_EQ(__interceptor_pthread_mutex_lock(k->mtx), 0);
  (*k->cnt)++;
  __tsan_write4(&k->cnt);
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(k->mtx), 0);
  if (k->val == 42) {
    // Okay.
  } else if (k->val == 43 || k->val == 44) {
    k->val--;
    EXPECT_EQ(pthread_setspecific(k->key, k), 0);
  } else {
    ASSERT_TRUE(false);
  }
}

static void *dtors_thread(void *p) {
  thread_key *k = (thread_key *)p;
  EXPECT_EQ(pthread_setspecific(k->key, k), 0);
  return 0;
}

TEST(Posix, ThreadSpecificDtors) {
  int cnt = 0;
  pthread_key_t key;
  EXPECT_EQ(pthread_key_create(&key, thread_secific_dtor), 0);
  pthread_mutex_t mtx;
  EXPECT_EQ(__interceptor_pthread_mutex_init(&mtx, 0), 0);
  pthread_t th[3];
  thread_key k1 = thread_key(key, &mtx, 42, &cnt);
  thread_key k2 = thread_key(key, &mtx, 43, &cnt);
  thread_key k3 = thread_key(key, &mtx, 44, &cnt);
  EXPECT_EQ(__interceptor_pthread_create(&th[0], 0, dtors_thread, &k1), 0);
  EXPECT_EQ(__interceptor_pthread_create(&th[1], 0, dtors_thread, &k2), 0);
  EXPECT_EQ(__interceptor_pthread_join(th[0], 0), 0);
  EXPECT_EQ(__interceptor_pthread_create(&th[2], 0, dtors_thread, &k3), 0);
  EXPECT_EQ(__interceptor_pthread_join(th[1], 0), 0);
  EXPECT_EQ(__interceptor_pthread_join(th[2], 0), 0);
  EXPECT_EQ(pthread_key_delete(key), 0);
  EXPECT_EQ(6, cnt);
}

#if !defined(__aarch64__) && !defined(__APPLE__)
static __thread int local_var;

static void *local_thread(void *p) {
  __tsan_write1(&local_var);
  __tsan_write1(&p);
  if (p == 0)
    return 0;
  const int kThreads = 4;
  pthread_t th[kThreads];
  for (int i = 0; i < kThreads; i++)
    EXPECT_EQ(__interceptor_pthread_create(&th[i], 0, local_thread,
                                           (void *)((long)p - 1)),
              0);
  for (int i = 0; i < kThreads; i++)
    EXPECT_EQ(__interceptor_pthread_join(th[i], 0), 0);
  return 0;
}
#endif

TEST(Posix, ThreadLocalAccesses) {
// The test is failing with high thread count for aarch64.
// FIXME: track down the issue and re-enable the test.
// On Darwin, we're running unit tests without interceptors and __thread is
// using malloc and free, which causes false data race reports.  On rare
// occasions on powerpc64le this test also fails.
#if !defined(__aarch64__) && !defined(__APPLE__) && !defined(powerpc64le)
  local_thread((void*)2);
#endif
}

struct CondContext {
  pthread_mutex_t m;
  pthread_cond_t c;
  int data;
};

static void *cond_thread(void *p) {
  CondContext &ctx = *static_cast<CondContext*>(p);

  EXPECT_EQ(__interceptor_pthread_mutex_lock(&ctx.m), 0);
  EXPECT_EQ(ctx.data, 0);
  ctx.data = 1;
  EXPECT_EQ(__interceptor_pthread_cond_signal(&ctx.c), 0);
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(&ctx.m), 0);

  EXPECT_EQ(__interceptor_pthread_mutex_lock(&ctx.m), 0);
  while (ctx.data != 2)
    EXPECT_EQ(__interceptor_pthread_cond_wait(&ctx.c, &ctx.m), 0);
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(&ctx.m), 0);

  EXPECT_EQ(__interceptor_pthread_mutex_lock(&ctx.m), 0);
  ctx.data = 3;
  EXPECT_EQ(pthread_cond_broadcast(&ctx.c), 0);
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(&ctx.m), 0);

  return 0;
}

TEST(Posix, CondBasic) {
  CondContext ctx;
  EXPECT_EQ(__interceptor_pthread_mutex_init(&ctx.m, 0), 0);
  EXPECT_EQ(__interceptor_pthread_cond_init(&ctx.c, 0), 0);
  ctx.data = 0;
  pthread_t th;
  EXPECT_EQ(__interceptor_pthread_create(&th, 0, cond_thread, &ctx), 0);

  EXPECT_EQ(__interceptor_pthread_mutex_lock(&ctx.m), 0);
  while (ctx.data != 1)
    EXPECT_EQ(__interceptor_pthread_cond_wait(&ctx.c, &ctx.m), 0);
  ctx.data = 2;
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(&ctx.m), 0);
  EXPECT_EQ(pthread_cond_broadcast(&ctx.c), 0);

  EXPECT_EQ(__interceptor_pthread_mutex_lock(&ctx.m), 0);
  while (ctx.data != 3)
    EXPECT_EQ(__interceptor_pthread_cond_wait(&ctx.c, &ctx.m), 0);
  EXPECT_EQ(__interceptor_pthread_mutex_unlock(&ctx.m), 0);

  EXPECT_EQ(__interceptor_pthread_join(th, 0), 0);
  EXPECT_EQ(__interceptor_pthread_cond_destroy(&ctx.c), 0);
  EXPECT_EQ(__interceptor_pthread_mutex_destroy(&ctx.m), 0);
}