xref: /llvm-project/compiler-rt/test/dfsan/pthread.c (revision 975327a609e55ad9c53bfeee63443128ce20006c)
15b4dda55SGeorge Balatsouras // RUN: %clang_dfsan %s -o %t && %run %t
24e67ae7bSJianzhou Zhao //
35b4dda55SGeorge Balatsouras // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \
44e67ae7bSJianzhou Zhao // RUN:     %run %t >%t.out 2>&1
54e67ae7bSJianzhou Zhao // RUN: FileCheck %s < %t.out
64e67ae7bSJianzhou Zhao //
75b4dda55SGeorge Balatsouras // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 -mllvm -dfsan-instrument-with-call-threshold=0 %s -o %t && \
84e67ae7bSJianzhou Zhao // RUN:     %run %t >%t.out 2>&1
94e67ae7bSJianzhou Zhao // RUN: FileCheck %s < %t.out
100f3fd3b2SJianzhou Zhao 
110f3fd3b2SJianzhou Zhao #include <sanitizer/dfsan_interface.h>
120f3fd3b2SJianzhou Zhao 
130f3fd3b2SJianzhou Zhao #include <assert.h>
140f3fd3b2SJianzhou Zhao #include <pthread.h>
154e67ae7bSJianzhou Zhao #include <string.h>
160f3fd3b2SJianzhou Zhao 
174e67ae7bSJianzhou Zhao const int kNumThreads = 24;
184e67ae7bSJianzhou Zhao int x = 0;
194e67ae7bSJianzhou Zhao int __thread y, z;
200f3fd3b2SJianzhou Zhao 
ThreadFn(void * a)210f3fd3b2SJianzhou Zhao static void *ThreadFn(void *a) {
220f3fd3b2SJianzhou Zhao   y = x;
230f3fd3b2SJianzhou Zhao   assert(dfsan_get_label(y) == 8);
244e67ae7bSJianzhou Zhao   memcpy(&z, &y, sizeof(y));
254e67ae7bSJianzhou Zhao   if ((int)a == 7)
264e67ae7bSJianzhou Zhao     dfsan_print_origin_trace(&z, NULL);
270f3fd3b2SJianzhou Zhao   return 0;
280f3fd3b2SJianzhou Zhao }
290f3fd3b2SJianzhou Zhao 
main(void)300f3fd3b2SJianzhou Zhao int main(void) {
310f3fd3b2SJianzhou Zhao   dfsan_set_label(8, &x, sizeof(x));
320f3fd3b2SJianzhou Zhao 
330f3fd3b2SJianzhou Zhao   pthread_t t[kNumThreads];
344e67ae7bSJianzhou Zhao   for (size_t i = 0; i < kNumThreads; ++i)
350f3fd3b2SJianzhou Zhao     pthread_create(&t[i], 0, ThreadFn, (void *)i);
364e67ae7bSJianzhou Zhao 
374e67ae7bSJianzhou Zhao   for (size_t i = 0; i < kNumThreads; ++i)
380f3fd3b2SJianzhou Zhao     pthread_join(t[i], 0);
394e67ae7bSJianzhou Zhao 
400f3fd3b2SJianzhou Zhao   return 0;
410f3fd3b2SJianzhou Zhao }
424e67ae7bSJianzhou Zhao 
434e67ae7bSJianzhou Zhao // CHECK: Taint value 0x8 {{.*}} origin tracking ()
444e67ae7bSJianzhou Zhao // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
45*c6b5a25eSGeorge Balatsouras // CHECK: #0 {{.*}} in ThreadFn.dfsan {{.*}}pthread.c:[[@LINE-21]]
464e67ae7bSJianzhou Zhao 
474e67ae7bSJianzhou Zhao // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
48*c6b5a25eSGeorge Balatsouras // CHECK: #0 {{.*}} in ThreadFn.dfsan {{.*}}pthread.c:[[@LINE-26]]
494e67ae7bSJianzhou Zhao 
504e67ae7bSJianzhou Zhao // CHECK: Origin value: {{.*}}, Taint value was created at
514e67ae7bSJianzhou Zhao // CHECK: #0 {{.*}} in main {{.*}}pthread.c:[[@LINE-20]]
52