llvm-project
47 строк · 1.3 Кб
1// RUN: %clang_tsan -O1 %s -o %t
2// RUN: %deflake %run %t | FileCheck %s --check-prefix=CHECK-NOZUPP
3// RUN: %env_tsan_opts=suppressions='%s.supp':print_suppressions=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUPP
4
5#include "test.h"
6
7int *mem;
8pthread_mutex_t mtx;
9
10void *Thread1(void *x) {
11pthread_mutex_lock(&mtx);
12free(mem);
13pthread_mutex_unlock(&mtx);
14barrier_wait(&barrier);
15return NULL;
16}
17
18__attribute__((noinline)) void *Thread2(void *x) {
19barrier_wait(&barrier);
20pthread_mutex_lock(&mtx);
21mem[0] = 42;
22pthread_mutex_unlock(&mtx);
23return NULL;
24}
25
26int main() {
27barrier_init(&barrier, 2);
28mem = (int*)malloc(100);
29pthread_mutex_init(&mtx, 0);
30pthread_t t;
31pthread_create(&t, NULL, Thread1, NULL);
32Thread2(0);
33pthread_join(t, NULL);
34pthread_mutex_destroy(&mtx);
35return 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$
48