1// RUN: %clangxx_tsan -O1 %s -o %t
2// RUN: %deflake %run %t 2>&1 | FileCheck %s
3
4#include <pthread.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include "test.h"
11
12extern "C" {
13void __tsan_on_report(void *report);
14void *__tsan_get_current_report();
15int __tsan_get_report_data(void *report, const char **description, int *count,
16 int *stack_count, int *mop_count, int *loc_count,
17 int *mutex_count, int *thread_count,
18 int *unique_tid_count, void **sleep_trace,
19 unsigned long trace_size);
20int __tsan_get_report_mop(void *report, unsigned long idx, int *tid,
21 void **addr, int *size, int *write, int *atomic,
22 void **trace, unsigned long trace_size);
23int __tsan_get_report_thread(void *report, unsigned long idx, int *tid,
24 uint64_t *os_id, int *running,
25 const char **name, int *parent_tid, void **trace,
26 unsigned long trace_size);
27}
28
29long my_global;
30
31void *Thread(void *a) {
32 barrier_wait(&barrier);
33 my_global = 42;
34 return NULL;
35}
36
37int main() {
38 barrier_init(&barrier, 2);
39 fprintf(stderr, "&my_global = %p\n", &my_global);
40 // CHECK: &my_global = [[GLOBAL:0x[0-9a-f]+]]
41 pthread_t t;
42 pthread_create(&t, 0, Thread, 0);
43 my_global = 41;
44 barrier_wait(&barrier);
45 pthread_join(t, 0);
46 fprintf(stderr, "Done.\n");
47}
48
49void __tsan_on_report(void *report) {
50 fprintf(stderr, "__tsan_on_report(%p)\n", report);
51 fprintf(stderr, "__tsan_get_current_report() = %p\n",
52 __tsan_get_current_report());
53 // CHECK: __tsan_on_report([[REPORT:0x[0-9a-f]+]])
54 // CHECK: __tsan_get_current_report() = [[REPORT]]
55
56 const char *description;
57 int count;
58 int stack_count, mop_count, loc_count, mutex_count, thread_count,
59 unique_tid_count;
60 void *sleep_trace[16] = {0};
61 __tsan_get_report_data(report, &description, &count, &stack_count, &mop_count,
62 &loc_count, &mutex_count, &thread_count,
63 &unique_tid_count, sleep_trace, 16);
64 fprintf(stderr, "report type = '%s', count = %d\n", description, count);
65 // CHECK: report type = 'data-race', count = 0
66
67 fprintf(stderr, "mop_count = %d\n", mop_count);
68 // CHECK: mop_count = 2
69
70 int tid;
71 void *addr;
72 int size, write, atomic;
73 void *trace[16] = {0};
74
75 __tsan_get_report_mop(report, 0, &tid, &addr, &size, &write, &atomic, trace,
76 16);
77 fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
78 tid, addr, size, write, atomic);
79 // CHECK: tid = 1, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
80 fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
81 // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
82
83 __tsan_get_report_mop(report, 1, &tid, &addr, &size, &write, &atomic, trace,
84 16);
85 fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
86 tid, addr, size, write, atomic);
87 // CHECK: tid = 0, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
88 fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
89 // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
90
91 fprintf(stderr, "thread_count = %d\n", thread_count);
92 // CHECK: thread_count = 2
93
94 uint64_t os_id;
95 int running;
96 const char *name;
97 int parent_tid;
98
99 __tsan_get_report_thread(report, 0, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
100 fprintf(stderr, "tid = %d\n", tid);
101 // CHECK: tid = 1
102
103 __tsan_get_report_thread(report, 1, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
104 fprintf(stderr, "tid = %d\n", tid);
105 // CHECK: tid = 0
106}
107
108// CHECK: Done.
109// CHECK: ThreadSanitizer: reported 1 warnings
110