Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clang_tsan -O1 %s -o %t |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 2 | // RUN: %deflake %run %t | FileCheck %s --check-prefix=CHECK-NOZUPP |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 3 | // RUN: %env_tsan_opts=suppressions='%s.supp':print_suppressions=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUPP |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 4 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 5 | #include "test.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 6 | |
| 7 | int *mem; |
| 8 | pthread_mutex_t mtx; |
| 9 | |
| 10 | void *Thread1(void *x) { |
| 11 | pthread_mutex_lock(&mtx); |
| 12 | free(mem); |
| 13 | pthread_mutex_unlock(&mtx); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 14 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 15 | return NULL; |
| 16 | } |
| 17 | |
| 18 | void *Thread2(void *x) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 19 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 20 | pthread_mutex_lock(&mtx); |
| 21 | mem[0] = 42; |
| 22 | pthread_mutex_unlock(&mtx); |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | int main() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 27 | barrier_init(&barrier, 2); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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$ |