[Sanitizer] Add rudimentary support for wide-character strings to scanf/printf interc...
[blocksruntime.git] / test / tsan / malloc_hook.cc
blob82eb6900efd0081677af0bd96b81f8ab94848f99
1 // RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | FileCheck %s
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stddef.h>
8 static int malloc_count;
9 static int free_count;
11 extern "C" {
12 void __tsan_malloc_hook(void *ptr, size_t size) {
13 (void)ptr;
14 (void)size;
15 __sync_fetch_and_add(&malloc_count, 1);
18 void __tsan_free_hook(void *ptr) {
19 (void)ptr;
20 __sync_fetch_and_add(&free_count, 1);
24 void *Thread1(void *x) {
25 ((int*)x)[0]++;
26 return 0;
29 void *Thread2(void *x) {
30 sleep(1);
31 ((int*)x)[0]++;
32 return 0;
35 int main() {
36 int *x = new int;
37 pthread_t t[2];
38 pthread_create(&t[0], 0, Thread1, x);
39 pthread_create(&t[1], 0, Thread2, x);
40 pthread_join(t[0], 0);
41 pthread_join(t[1], 0);
42 delete x;
43 if (malloc_count == 0 || free_count == 0) {
44 fprintf(stderr, "FAILED %d %d\n", malloc_count, free_count);
45 exit(1);
47 fprintf(stderr, "DONE\n");
50 // CHECK: WARNING: ThreadSanitizer: data race
51 // CHECK-NOT: FAILED
52 // CHECK: DONE