llvm-project
115 строк · 3.9 Кб
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,
16int *stack_count, int *mop_count, int *loc_count,
17int *mutex_count, int *thread_count,
18int *unique_tid_count, void **sleep_trace,
19unsigned long trace_size);
20int __tsan_get_report_mop(void *report, unsigned long idx, int *tid,
21void **addr, int *size, int *write, int *atomic,
22void **trace, unsigned long trace_size);
23int __tsan_get_report_thread(void *report, unsigned long idx, int *tid,
24uint64_t *os_id, int *running,
25const char **name, int *parent_tid, void **trace,
26unsigned long trace_size);
27}
28
29long my_global;
30
31void *Thread(void *a) {
32barrier_wait(&barrier);
33my_global = 42;
34return NULL;
35}
36
37int main() {
38barrier_init(&barrier, 2);
39fprintf(stderr, "&my_global = %p\n", &my_global);
40// CHECK: &my_global = [[GLOBAL:0x[0-9a-f]+]]
41pthread_t t;
42pthread_create(&t, 0, Thread, 0);
43my_global = 41;
44barrier_wait(&barrier);
45pthread_join(t, 0);
46fprintf(stderr, "Done.\n");
47}
48
49// Required for dyld macOS 12.0+
50#if (__APPLE__)
51__attribute__((weak))
52#endif
53__attribute__((disable_sanitizer_instrumentation))
54extern "C" void
55__tsan_on_report(void *report) {
56fprintf(stderr, "__tsan_on_report(%p)\n", report);
57fprintf(stderr, "__tsan_get_current_report() = %p\n",
58__tsan_get_current_report());
59// CHECK: __tsan_on_report([[REPORT:0x[0-9a-f]+]])
60// CHECK: __tsan_get_current_report() = [[REPORT]]
61
62const char *description;
63int count;
64int stack_count, mop_count, loc_count, mutex_count, thread_count,
65unique_tid_count;
66void *sleep_trace[16] = {0};
67__tsan_get_report_data(report, &description, &count, &stack_count, &mop_count,
68&loc_count, &mutex_count, &thread_count,
69&unique_tid_count, sleep_trace, 16);
70fprintf(stderr, "report type = '%s', count = %d\n", description, count);
71// CHECK: report type = 'data-race', count = 0
72
73fprintf(stderr, "mop_count = %d\n", mop_count);
74// CHECK: mop_count = 2
75
76int tid;
77void *addr;
78int size, write, atomic;
79void *trace[16] = {0};
80
81__tsan_get_report_mop(report, 0, &tid, &addr, &size, &write, &atomic, trace,
8216);
83fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
84tid, addr, size, write, atomic);
85// CHECK: tid = 1, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
86fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
87// CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
88
89__tsan_get_report_mop(report, 1, &tid, &addr, &size, &write, &atomic, trace,
9016);
91fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
92tid, addr, size, write, atomic);
93// CHECK: tid = 0, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
94fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
95// CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
96
97fprintf(stderr, "thread_count = %d\n", thread_count);
98// CHECK: thread_count = 2
99
100uint64_t os_id;
101int running;
102const char *name;
103int parent_tid;
104
105__tsan_get_report_thread(report, 0, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
106fprintf(stderr, "tid = %d\n", tid);
107// CHECK: tid = 1
108
109__tsan_get_report_thread(report, 1, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
110fprintf(stderr, "tid = %d\n", tid);
111// CHECK: tid = 0
112}
113
114// CHECK: Done.
115// CHECK: ThreadSanitizer: reported 1 warnings
116