xref: /llvm-project/compiler-rt/test/dfsan/pthread.c (revision 975327a609e55ad9c53bfeee63443128ce20006c)
1 // RUN: %clang_dfsan %s -o %t && %run %t
2 //
3 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \
4 // RUN:     %run %t >%t.out 2>&1
5 // RUN: FileCheck %s < %t.out
6 //
7 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 -mllvm -dfsan-instrument-with-call-threshold=0 %s -o %t && \
8 // RUN:     %run %t >%t.out 2>&1
9 // RUN: FileCheck %s < %t.out
10 
11 #include <sanitizer/dfsan_interface.h>
12 
13 #include <assert.h>
14 #include <pthread.h>
15 #include <string.h>
16 
17 const int kNumThreads = 24;
18 int x = 0;
19 int __thread y, z;
20 
ThreadFn(void * a)21 static void *ThreadFn(void *a) {
22   y = x;
23   assert(dfsan_get_label(y) == 8);
24   memcpy(&z, &y, sizeof(y));
25   if ((int)a == 7)
26     dfsan_print_origin_trace(&z, NULL);
27   return 0;
28 }
29 
main(void)30 int main(void) {
31   dfsan_set_label(8, &x, sizeof(x));
32 
33   pthread_t t[kNumThreads];
34   for (size_t i = 0; i < kNumThreads; ++i)
35     pthread_create(&t[i], 0, ThreadFn, (void *)i);
36 
37   for (size_t i = 0; i < kNumThreads; ++i)
38     pthread_join(t[i], 0);
39 
40   return 0;
41 }
42 
43 // CHECK: Taint value 0x8 {{.*}} origin tracking ()
44 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
45 // CHECK: #0 {{.*}} in ThreadFn.dfsan {{.*}}pthread.c:[[@LINE-21]]
46 
47 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
48 // CHECK: #0 {{.*}} in ThreadFn.dfsan {{.*}}pthread.c:[[@LINE-26]]
49 
50 // CHECK: Origin value: {{.*}}, Taint value was created at
51 // CHECK: #0 {{.*}} in main {{.*}}pthread.c:[[@LINE-20]]
52