1 | // RUN: %clangxx_tsan -c -O1 -fno-sanitize=thread %s -o %t.o |
2 | // RUN: %clangxx_tsan -O1 %s %t.o -o %t && %run %t 2>&1 | FileCheck %s |
3 | |
4 | #include <stdio.h> |
5 | #include <stdlib.h> |
6 | |
7 | #if !__has_feature(thread_sanitizer) |
8 | |
9 | // Defined by tsan. |
10 | extern "C" void *__interceptor_malloc(unsigned long size); |
11 | extern "C" void __interceptor_free(void *p); |
12 | extern "C" void *malloc(unsigned long size) { |
13 | static int first = 0; |
14 | if (__sync_lock_test_and_set(&first, 1) == 0) |
15 | fprintf(stderr, format: "user malloc\n" ); |
16 | return __interceptor_malloc(size); |
17 | } |
18 | |
19 | extern "C" void free(void *p) { |
20 | __interceptor_free(p); |
21 | } |
22 | |
23 | #else |
24 | |
25 | int main() { |
26 | volatile char *p = (char*)malloc(10); |
27 | p[0] = 0; |
28 | free((void*)p); |
29 | } |
30 | |
31 | #endif |
32 | |
33 | // CHECK: user malloc |
34 | // CHECK-NOT: ThreadSanitizer |
35 | |
36 | |