1// RUN: %clang_tsan %s -o %t -framework Foundation
2// RUN: %deflake %run %t 2>&1 | FileCheck %s
3
4#import <pthread.h>
5#import <stdio.h>
6#import <stdlib.h>
7
8extern "C" {
9void __tsan_on_report(void *report);
10int __tsan_get_report_thread(void *report, unsigned long idx, int *tid,
11 uint64_t *os_id, int *running,
12 const char **name, int *parent_tid, void **trace,
13 unsigned long trace_size);
14}
15
16// Required for dyld macOS 12.0+
17#if (__APPLE__)
18__attribute__((weak))
19#endif
20__attribute__((disable_sanitizer_instrumentation))
21extern "C" void
22__tsan_on_report(void *report) {
23 fprintf(stderr, format: "__tsan_on_report(%p)\n", report);
24
25 int tid;
26 uint64_t os_id;
27 int running;
28 const char *name;
29 int parent_tid;
30 void *trace[16] = {0};
31 __tsan_get_report_thread(report, 0, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
32 fprintf(stderr, "tid = %d, os_id = %lu\n", tid, os_id);
33}
34
35int main() {
36 fprintf(stderr, format: "Hello world.\n");
37
38 uint64_t threadid;
39 pthread_threadid_np(NULL, &threadid);
40 fprintf(stderr, "pthread_threadid_np = %llu\n", threadid);
41
42 pthread_mutex_t m;
43 pthread_mutex_init(mutex: &m, NULL);
44 pthread_mutex_unlock(mutex: &m);
45 fprintf(stderr, format: "Done.\n");
46}
47
48// CHECK: Hello world.
49// CHECK: pthread_threadid_np = [[ADDR:[0-9]+]]
50// CHECK: WARNING: ThreadSanitizer
51// CHECK: tid = 0, os_id = [[ADDR]]
52// CHECK: Done.
53

source code of compiler-rt/test/tsan/Darwin/main_tid.mm