1 // RUN: %clang_tsan -O1 %s -o %t && %env_tsan_opts=report_atomic_races=0 %run %t 2>&1 | FileCheck %s
2 // This test exposed non-atomicity in mmap interceptor
3 // which made shadow for the region temporarily unmapped.
4 // This resulted in crashes in the Accesser thread.
9 // The size needs to be large enough to trigger
10 // large region optimization in the runtime.
11 const size_t kMmapSize
= 16 << 20;
13 void *Remapper(void *arg
) {
15 void *p
= mmap(arg
, kMmapSize
, PROT_READ
| PROT_WRITE
,
16 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
, -1, 0);
18 exit(printf("mmap failed: %d\n", errno
));
23 void *Accesser(void *arg
) {
24 unsigned rnd
= time(0);
26 int index
= rand_r(&rnd
) % kMmapSize
;
27 char *p
= &((char *)arg
)[index
];
28 __atomic_fetch_add(p
, 1, __ATOMIC_ACQ_REL
);
34 void *p
= mmap(0, kMmapSize
, PROT_READ
| PROT_WRITE
,
35 MAP_PRIVATE
| MAP_ANON
, -1, 0);
37 exit(printf("mmap failed: %d\n", errno
));
39 pthread_attr_init(&attr
);
40 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
42 if (pthread_create(&th
[0], &attr
, Remapper
, p
))
43 exit(printf("pthread_create failed: %d\n", errno
));
44 if (pthread_create(&th
[1], &attr
, Accesser
, p
))
45 exit(printf("pthread_create failed: %d\n", errno
));
46 pthread_attr_destroy(&attr
);
48 fprintf(stderr
, "DONE\n");