1 // RUN: %clangxx_tsan -O0 %s -o %t
2 // RUN: env %env_tsan_opts=stack_trace_format=DEFAULT %deflake %run %t 2>&1 | FileCheck %s
4 // Until I figure out how to make this test work on Linux
5 // REQUIRES: system-darwin
14 #include <sys/types.h>
17 extern "C" int __tsan_get_alloc_stack(void *addr
, void **trace
, size_t size
,
18 int *thread_id
, uint64_t *os_id
);
21 void alloc_func() { mem
= (char *)malloc(10); }
23 void *AllocThread(void *context
) {
26 pthread_threadid_np(NULL
, &tid
);
30 fprintf(stderr
, "alloc stack thread os id = 0x%llx\n", tid
);
31 // CHECK: alloc stack thread os id = [[THREAD_OS_ID:0x[0-9a-f]+]]
36 void *RaceThread(void *context
) {
38 barrier_wait(&barrier
);
44 barrier_init(&barrier
, 2);
46 pthread_create(&t
, NULL
, AllocThread
, NULL
);
47 pthread_join(t
, NULL
);
50 size_t num_frames
= 100;
52 uint64_t thread_os_id
;
54 __tsan_get_alloc_stack(mem
, trace
, num_frames
, &thread_id
, &thread_os_id
);
56 fprintf(stderr
, "alloc stack retval %s\n",
57 (num_frames
> 0 && num_frames
< 10) ? "ok" : "");
58 // CHECK: alloc stack retval ok
59 fprintf(stderr
, "thread id = %d\n", thread_id
);
60 // CHECK: thread id = 1
61 fprintf(stderr
, "thread os id = 0x%llx\n", thread_os_id
);
62 // CHECK: thread os id = [[THREAD_OS_ID]]
63 fprintf(stderr
, "%p\n", trace
[0]);
64 // CHECK: [[ALLOC_FRAME_0:0x[0-9a-f]+]]
65 fprintf(stderr
, "%p\n", trace
[1]);
66 // CHECK: [[ALLOC_FRAME_1:0x[0-9a-f]+]]
67 fprintf(stderr
, "%p\n", trace
[2]);
68 // CHECK: [[ALLOC_FRAME_2:0x[0-9a-f]+]]
70 pthread_create(&t
, NULL
, RaceThread
, NULL
);
71 barrier_wait(&barrier
);
73 pthread_join(t
, NULL
);
80 // CHECK: WARNING: ThreadSanitizer: data race
81 // CHECK: Location is heap block of size 10 at {{.*}} allocated by thread T1
82 // CHECK: #0 [[ALLOC_FRAME_0]]
83 // CHECK: #1 [[ALLOC_FRAME_1]] in alloc_func
84 // CHECK: #2 [[ALLOC_FRAME_2]] in AllocThread