xref: /aosp_15_r20/external/compiler-rt/test/tsan/race_on_heap.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2*7c3d14c8STreehugger Robot #include <pthread.h>
3*7c3d14c8STreehugger Robot #include <stdlib.h>
4*7c3d14c8STreehugger Robot #include <stdio.h>
5*7c3d14c8STreehugger Robot #include "test.h"
6*7c3d14c8STreehugger Robot 
Thread1(void * p)7*7c3d14c8STreehugger Robot void *Thread1(void *p) {
8*7c3d14c8STreehugger Robot   *(int*)p = 42;
9*7c3d14c8STreehugger Robot   return 0;
10*7c3d14c8STreehugger Robot }
11*7c3d14c8STreehugger Robot 
Thread2(void * p)12*7c3d14c8STreehugger Robot void *Thread2(void *p) {
13*7c3d14c8STreehugger Robot   *(int*)p = 44;
14*7c3d14c8STreehugger Robot   return 0;
15*7c3d14c8STreehugger Robot }
16*7c3d14c8STreehugger Robot 
alloc()17*7c3d14c8STreehugger Robot void *alloc() {
18*7c3d14c8STreehugger Robot   return malloc(99);
19*7c3d14c8STreehugger Robot }
20*7c3d14c8STreehugger Robot 
AllocThread(void * arg)21*7c3d14c8STreehugger Robot void *AllocThread(void* arg) {
22*7c3d14c8STreehugger Robot   return alloc();
23*7c3d14c8STreehugger Robot }
24*7c3d14c8STreehugger Robot 
main()25*7c3d14c8STreehugger Robot int main() {
26*7c3d14c8STreehugger Robot   void *p = 0;
27*7c3d14c8STreehugger Robot   pthread_t t[2];
28*7c3d14c8STreehugger Robot   pthread_create(&t[0], 0, AllocThread, 0);
29*7c3d14c8STreehugger Robot   pthread_join(t[0], &p);
30*7c3d14c8STreehugger Robot   print_address("addr=", 1, p);
31*7c3d14c8STreehugger Robot   pthread_create(&t[0], 0, Thread1, (char*)p + 16);
32*7c3d14c8STreehugger Robot   pthread_create(&t[1], 0, Thread2, (char*)p + 16);
33*7c3d14c8STreehugger Robot   pthread_join(t[0], 0);
34*7c3d14c8STreehugger Robot   pthread_join(t[1], 0);
35*7c3d14c8STreehugger Robot   return 0;
36*7c3d14c8STreehugger Robot }
37*7c3d14c8STreehugger Robot 
38*7c3d14c8STreehugger Robot // CHECK: addr=[[ADDR:0x[0-9,a-f]+]]
39*7c3d14c8STreehugger Robot // CHECK: WARNING: ThreadSanitizer: data race
40*7c3d14c8STreehugger Robot // ...
41*7c3d14c8STreehugger Robot // CHECK: Location is heap block of size 99 at [[ADDR]] allocated by thread T1:
42*7c3d14c8STreehugger Robot // CHCEK:     #0 malloc
43*7c3d14c8STreehugger Robot // CHECK:     #{{1|2}} alloc
44*7c3d14c8STreehugger Robot // CHECK:     #{{2|3}} AllocThread
45*7c3d14c8STreehugger Robot // ...
46*7c3d14c8STreehugger Robot // CHECK:   Thread T1 (tid={{.*}}, finished) created by main thread at:
47*7c3d14c8STreehugger Robot // CHECK:     #0 pthread_create
48*7c3d14c8STreehugger Robot // CHECK:     #1 main
49