xref: /aosp_15_r20/external/compiler-rt/test/tsan/ignore_malloc.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot #include "test.h"
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot extern "C" {
5*7c3d14c8STreehugger Robot void AnnotateIgnoreReadsBegin(const char *f, int l);
6*7c3d14c8STreehugger Robot void AnnotateIgnoreReadsEnd(const char *f, int l);
7*7c3d14c8STreehugger Robot void AnnotateIgnoreWritesBegin(const char *f, int l);
8*7c3d14c8STreehugger Robot void AnnotateIgnoreWritesEnd(const char *f, int l);
9*7c3d14c8STreehugger Robot }
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot int *g;
12*7c3d14c8STreehugger Robot 
Thread(void * a)13*7c3d14c8STreehugger Robot void *Thread(void *a) {
14*7c3d14c8STreehugger Robot   int *p = 0;
15*7c3d14c8STreehugger Robot   while ((p = __atomic_load_n(&g, __ATOMIC_RELAXED)) == 0)
16*7c3d14c8STreehugger Robot     usleep(100);  // spin-wait
17*7c3d14c8STreehugger Robot   *p = 42;
18*7c3d14c8STreehugger Robot   return 0;
19*7c3d14c8STreehugger Robot }
20*7c3d14c8STreehugger Robot 
main()21*7c3d14c8STreehugger Robot int main() {
22*7c3d14c8STreehugger Robot   pthread_t t;
23*7c3d14c8STreehugger Robot   pthread_create(&t, 0, Thread, 0);
24*7c3d14c8STreehugger Robot   AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
25*7c3d14c8STreehugger Robot   int *p = new int(0);
26*7c3d14c8STreehugger Robot   AnnotateIgnoreWritesEnd(__FILE__, __LINE__);
27*7c3d14c8STreehugger Robot   __atomic_store_n(&g, p, __ATOMIC_RELAXED);
28*7c3d14c8STreehugger Robot   pthread_join(t, 0);
29*7c3d14c8STreehugger Robot   delete p;
30*7c3d14c8STreehugger Robot   fprintf(stderr, "OK\n");
31*7c3d14c8STreehugger Robot   return 0;
32*7c3d14c8STreehugger Robot }
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer: data race
35*7c3d14c8STreehugger Robot // CHECK: OK
36