]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/test/tsan/free_race.c
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / test / tsan / free_race.c
CommitLineData
1a4d82fc 1// RUN: %clang_tsan -O1 %s -o %t
92a42be0
SL
2// RUN: %deflake %run %t | FileCheck %s --check-prefix=CHECK-NOZUPP
3// RUN: TSAN_OPTIONS="suppressions='%s.supp' print_suppressions=1" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUPP
1a4d82fc 4
92a42be0 5#include "test.h"
1a4d82fc
JJ
6
7int *mem;
8pthread_mutex_t mtx;
9
10void *Thread1(void *x) {
11 pthread_mutex_lock(&mtx);
12 free(mem);
13 pthread_mutex_unlock(&mtx);
92a42be0 14 barrier_wait(&barrier);
1a4d82fc
JJ
15 return NULL;
16}
17
18void *Thread2(void *x) {
92a42be0 19 barrier_wait(&barrier);
1a4d82fc
JJ
20 pthread_mutex_lock(&mtx);
21 mem[0] = 42;
22 pthread_mutex_unlock(&mtx);
23 return NULL;
24}
25
26int main() {
92a42be0 27 barrier_init(&barrier, 2);
1a4d82fc
JJ
28 mem = (int*)malloc(100);
29 pthread_mutex_init(&mtx, 0);
30 pthread_t t;
31 pthread_create(&t, NULL, Thread1, NULL);
32 Thread2(0);
33 pthread_join(t, NULL);
34 pthread_mutex_destroy(&mtx);
35 return 0;
36}
37
38// CHECK-NOZUPP: WARNING: ThreadSanitizer: heap-use-after-free
39// CHECK-NOZUPP: Write of size 4 at {{.*}} by main thread{{.*}}:
40// CHECK-NOZUPP: #0 Thread2
41// CHECK-NOZUPP: #1 main
42// CHECK-NOZUPP: Previous write of size 8 at {{.*}} by thread T1{{.*}}:
43// CHECK-NOZUPP: #0 free
44// CHECK-NOZUPP: #{{(1|2)}} Thread1
45// CHECK-NOZUPP: SUMMARY: ThreadSanitizer: heap-use-after-free{{.*}}Thread2
46// CHECK-SUPP: ThreadSanitizer: Matched 1 suppressions
47// CHECK-SUPP: 1 race:^Thread2$