aboutsummaryrefslogtreecommitdiff
path: root/test/tsan/Darwin/external.cc
blob: 2605480d7b82d1921a495c10d706a3b35e4f2f2e (plain) (blame)
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
// RUN: %clangxx_tsan %s -shared -DSHARED_LIB \
// RUN:                               -o %t-lib-instrumented.dylib \
// RUN:   -install_name @rpath/`basename %t-lib-instrumented.dylib`

// RUN: %clangxx_tsan %s -shared -DSHARED_LIB -fno-sanitize=thread \
// RUN:                               -o %t-lib-noninstrumented.dylib \
// RUN:   -install_name @rpath/`basename %t-lib-noninstrumented.dylib`

// RUN: %clangxx_tsan %s -shared -DSHARED_LIB -fno-sanitize=thread -DUSE_TSAN_CALLBACKS \
// RUN:                               -o %t-lib-noninstrumented-callbacks.dylib \
// RUN:   -install_name @rpath/`basename %t-lib-noninstrumented-callbacks.dylib`

// RUN: %clangxx_tsan %s %t-lib-instrumented.dylib -o %t-lib-instrumented
// RUN: %clangxx_tsan %s %t-lib-noninstrumented.dylib -o %t-lib-noninstrumented
// RUN: %clangxx_tsan %s %t-lib-noninstrumented-callbacks.dylib -o %t-lib-noninstrumented-callbacks

// RUN: %deflake %run %t-lib-instrumented              2>&1 \
// RUN:   | FileCheck %s --check-prefix=CHECK --check-prefix=TEST1
// RUN:          %run %t-lib-noninstrumented           2>&1 \
// RUN:   | FileCheck %s --check-prefix=CHECK --check-prefix=TEST2
// RUN: %deflake %run %t-lib-noninstrumented-callbacks 2>&1 \
// RUN:   | FileCheck %s --check-prefix=CHECK --check-prefix=TEST3

#include <thread>

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

struct MyObject;
typedef MyObject *MyObjectRef;
extern "C" {
  void InitializeLibrary();
  MyObject *ObjectCreate();
  long ObjectRead(MyObject *);
  void ObjectWrite(MyObject *, long);
  void ObjectWriteAnother(MyObject *, long);
}

#if defined(SHARED_LIB)

struct MyObject {
  long _val;
  long _another;
};

#if defined(USE_TSAN_CALLBACKS)
static void *tag;
void *(*callback_register_tag)(const char *object_type);
void *(*callback_assign_tag)(void *addr, void *tag);
void (*callback_read)(void *addr, void *caller_pc, void *tag);
void (*callback_write)(void *addr, void *caller_pc, void *tag);
#endif

void InitializeLibrary() {
#if defined(USE_TSAN_CALLBACKS)
  callback_register_tag = (decltype(callback_register_tag))dlsym(RTLD_DEFAULT, "__tsan_external_register_tag");
  callback_assign_tag = (decltype(callback_assign_tag))dlsym(RTLD_DEFAULT, "__tsan_external_assign_tag");
  callback_read = (decltype(callback_read))dlsym(RTLD_DEFAULT, "__tsan_external_read");
  callback_write = (decltype(callback_write))dlsym(RTLD_DEFAULT, "__tsan_external_write");
  tag = callback_register_tag("MyLibrary::MyObject");
#endif
}

MyObject *ObjectCreate() {
  MyObject *ref = (MyObject *)malloc(sizeof(MyObject));
#if defined(USE_TSAN_CALLBACKS)
  callback_assign_tag(ref, tag);
#endif
  return ref;
}

long ObjectRead(MyObject *ref) {
#if defined(USE_TSAN_CALLBACKS)
  callback_read(ref, __builtin_return_address(0), tag);
#endif
  return ref->_val;
}

void ObjectWrite(MyObject *ref, long val) {
#if defined(USE_TSAN_CALLBACKS)
  callback_write(ref, __builtin_return_address(0), tag);
#endif
  ref->_val = val;
}

void ObjectWriteAnother(MyObject *ref, long val) {
#if defined(USE_TSAN_CALLBACKS)
  callback_write(ref, __builtin_return_address(0), tag);
#endif
  ref->_another = val;
}

#else  // defined(SHARED_LIB)

int main(int argc, char *argv[]) {
  InitializeLibrary();
  
  {
    MyObjectRef ref = ObjectCreate();
    std::thread t1([ref]{ ObjectRead(ref); });
    std::thread t2([ref]{ ObjectRead(ref); });
    t1.join();
    t2.join();
  }
  
  // CHECK-NOT: WARNING: ThreadSanitizer
  
  fprintf(stderr, "RR test done\n");
  // CHECK: RR test done

  {
    MyObjectRef ref = ObjectCreate();
    std::thread t1([ref]{ ObjectRead(ref); });
    std::thread t2([ref]{ ObjectWrite(ref, 66); });
    t1.join();
    t2.join();
  }
  
  // TEST1: WARNING: ThreadSanitizer: data race
  // TEST1: {{Write|Read}} of size 8 at
  // TEST1: Previous {{write|read}} of size 8 at
  // TEST1: Location is heap block of size 16 at
  
  // TEST2-NOT: WARNING: ThreadSanitizer
  
  // TEST3: WARNING: ThreadSanitizer: race on a library object
  // TEST3: {{Mutating|read-only}} access of object MyLibrary::MyObject at
  // TEST3: {{ObjectWrite|ObjectRead}}
  // TEST3: Previous {{mutating|read-only}} access of object MyLibrary::MyObject at
  // TEST3: {{ObjectWrite|ObjectRead}}
  // TEST3: Location is MyLibrary::MyObject object of size 16 at
  // TEST3: {{ObjectCreate}}

  fprintf(stderr, "RW test done\n");
  // CHECK: RW test done

  {
    MyObjectRef ref = ObjectCreate();
    std::thread t1([ref]{ ObjectWrite(ref, 76); });
    std::thread t2([ref]{ ObjectWriteAnother(ref, 77); });
    t1.join();
    t2.join();
  }
  
  // TEST1-NOT: WARNING: ThreadSanitizer: data race
  
  // TEST2-NOT: WARNING: ThreadSanitizer
  
  // TEST3: WARNING: ThreadSanitizer: race on a library object
  // TEST3: Mutating access of object MyLibrary::MyObject at
  // TEST3: {{ObjectWrite|ObjectWriteAnother}}
  // TEST3: Previous mutating access of object MyLibrary::MyObject at
  // TEST3: {{ObjectWrite|ObjectWriteAnother}}
  // TEST3: Location is MyLibrary::MyObject object of size 16 at
  // TEST3: {{ObjectCreate}}

  fprintf(stderr, "WW test done\n");
  // CHECK: WW test done
}

#endif  // defined(SHARED_LIB)