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

source code of compiler-rt/test/tsan/race_on_heap.cpp