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
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
//===-- tsan_mop.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_test_util.h"
#include "gtest/gtest.h"
#include <stddef.h>
#include <stdint.h>

TEST(ThreadSanitizer, SimpleWrite) {
  ScopedThread t;
  MemLoc l;
  t.Write1(l);
}

TEST(ThreadSanitizer, SimpleWriteWrite) {
  ScopedThread t1, t2;
  MemLoc l1, l2;
  t1.Write1(l1);
  t2.Write1(l2);
}

TEST(ThreadSanitizer, WriteWriteRace) {
  ScopedThread t1, t2;
  MemLoc l;
  t1.Write1(l);
  t2.Write1(l, true);
}

TEST(ThreadSanitizer, ReadWriteRace) {
  ScopedThread t1, t2;
  MemLoc l;
  t1.Read1(l);
  t2.Write1(l, true);
}

TEST(ThreadSanitizer, WriteReadRace) {
  ScopedThread t1, t2;
  MemLoc l;
  t1.Write1(l);
  t2.Read1(l, true);
}

TEST(ThreadSanitizer, ReadReadNoRace) {
  ScopedThread t1, t2;
  MemLoc l;
  t1.Read1(l);
  t2.Read1(l);
}

TEST(ThreadSanitizer, WriteThenRead) {
  MemLoc l;
  ScopedThread t1, t2;
  t1.Write1(l);
  t1.Read1(l);
  t2.Read1(l, true);
}

TEST(ThreadSanitizer, WriteThenLockedRead) {
  Mutex m(Mutex::RW);
  MainThread t0;
  t0.Create(m);
  MemLoc l;
  {
    ScopedThread t1, t2;

    t1.Write8(l);

    t1.Lock(m);
    t1.Read8(l);
    t1.Unlock(m);

    t2.Read8(l, true);
  }
  t0.Destroy(m);
}

TEST(ThreadSanitizer, LockedWriteThenRead) {
  Mutex m(Mutex::RW);
  MainThread t0;
  t0.Create(m);
  MemLoc l;
  {
    ScopedThread t1, t2;

    t1.Lock(m);
    t1.Write8(l);
    t1.Unlock(m);

    t1.Read8(l);

    t2.Read8(l, true);
  }
  t0.Destroy(m);
}


TEST(ThreadSanitizer, RaceWithOffset) {
  ScopedThread t1, t2;
  {
    MemLoc l;
    t1.Access(l.loc(), true, 8, false);
    t2.Access((char*)l.loc() + 4, true, 4, true);
  }
  {
    MemLoc l;
    t1.Access(l.loc(), true, 8, false);
    t2.Access((char*)l.loc() + 7, true, 1, true);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 4, true, 4, false);
    t2.Access((char*)l.loc() + 4, true, 2, true);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 4, true, 4, false);
    t2.Access((char*)l.loc() + 6, true, 2, true);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 3, true, 2, false);
    t2.Access((char*)l.loc() + 4, true, 1, true);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 1, true, 8, false);
    t2.Access((char*)l.loc() + 3, true, 1, true);
  }
}

TEST(ThreadSanitizer, RaceWithOffset2) {
  ScopedThread t1, t2;
  {
    MemLoc l;
    t1.Access((char*)l.loc(), true, 4, false);
    t2.Access((char*)l.loc() + 2, true, 1, true);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 2, true, 1, false);
    t2.Access((char*)l.loc(), true, 4, true);
  }
}

TEST(ThreadSanitizer, NoRaceWithOffset) {
  ScopedThread t1, t2;
  {
    MemLoc l;
    t1.Access(l.loc(), true, 4, false);
    t2.Access((char*)l.loc() + 4, true, 4, false);
  }
  {
    MemLoc l;
    t1.Access((char*)l.loc() + 3, true, 2, false);
    t2.Access((char*)l.loc() + 1, true, 2, false);
    t2.Access((char*)l.loc() + 5, true, 2, false);
  }
}

TEST(ThreadSanitizer, RaceWithDeadThread) {
  MemLoc l;
  ScopedThread t;
  ScopedThread().Write1(l);
  t.Write1(l, true);
}

TEST(ThreadSanitizer, BenignRaceOnVptr) {
  void *vptr_storage;
  MemLoc vptr(&vptr_storage), val;
  vptr_storage = val.loc();
  ScopedThread t1, t2;
  t1.VptrUpdate(vptr, val);
  t2.Read8(vptr);
}

TEST(ThreadSanitizer, HarmfulRaceOnVptr) {
  void *vptr_storage;
  MemLoc vptr(&vptr_storage), val1, val2;
  vptr_storage = val1.loc();
  ScopedThread t1, t2;
  t1.VptrUpdate(vptr, val2);
  t2.Read8(vptr, true);
}

static void foo() {
  volatile int x = 42;
  int x2 = x;
  (void)x2;
}

static void bar() {
  volatile int x = 43;
  int x2 = x;
  (void)x2;
}

TEST(ThreadSanitizer, ReportDeadThread) {
  MemLoc l;
  ScopedThread t1;
  {
    ScopedThread t2;
    t2.Call(&foo);
    t2.Call(&bar);
    t2.Write1(l);
  }
  t1.Write1(l, true);
}

struct ClassWithStatic {
  static int Data[4];
};

int ClassWithStatic::Data[4];

static void foobarbaz() {}

TEST(ThreadSanitizer, ReportRace) {
  ScopedThread t1;
  MainThread().Access(&ClassWithStatic::Data, true, 4, false);
  t1.Call(&foobarbaz);
  t1.Access(&ClassWithStatic::Data, true, 2, true);
  t1.Return();
}