1/*! \file */
2/*
3 * tsan_annotations.h -- ThreadSanitizer annotations to support data
4 * race detection in OpenMP programs.
5 */
6
7//===----------------------------------------------------------------------===//
8//
9// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10// See https://llvm.org/LICENSE.txt for license information.
11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef TSAN_ANNOTATIONS_H
16#define TSAN_ANNOTATIONS_H
17
18#include "kmp_config.h"
19
20/* types as used in tsan/rtl/tsan_interface_ann.cc */
21typedef unsigned long uptr;
22typedef signed long sptr;
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* Declaration of all annotation functions in tsan/rtl/tsan_interface_ann.cc */
29void AnnotateHappensBefore(const char *f, int l, uptr addr);
30void AnnotateHappensAfter(const char *f, int l, uptr addr);
31void AnnotateCondVarSignal(const char *f, int l, uptr cv);
32void AnnotateCondVarSignalAll(const char *f, int l, uptr cv);
33void AnnotateMutexIsNotPHB(const char *f, int l, uptr mu);
34void AnnotateCondVarWait(const char *f, int l, uptr cv, uptr lock);
35void AnnotateRWLockCreate(const char *f, int l, uptr m);
36void AnnotateRWLockCreateStatic(const char *f, int l, uptr m);
37void AnnotateRWLockDestroy(const char *f, int l, uptr m);
38void AnnotateRWLockAcquired(const char *f, int l, uptr m, uptr is_w);
39void AnnotateRWLockReleased(const char *f, int l, uptr m, uptr is_w);
40void AnnotateTraceMemory(const char *f, int l, uptr mem);
41void AnnotateFlushState(const char *f, int l);
42void AnnotateNewMemory(const char *f, int l, uptr mem, uptr size);
43void AnnotateNoOp(const char *f, int l, uptr mem);
44void AnnotateFlushExpectedRaces(const char *f, int l);
45void AnnotateEnableRaceDetection(const char *f, int l, int enable);
46void AnnotateMutexIsUsedAsCondVar(const char *f, int l, uptr mu);
47void AnnotatePCQGet(const char *f, int l, uptr pcq);
48void AnnotatePCQPut(const char *f, int l, uptr pcq);
49void AnnotatePCQDestroy(const char *f, int l, uptr pcq);
50void AnnotatePCQCreate(const char *f, int l, uptr pcq);
51void AnnotateExpectRace(const char *f, int l, uptr mem, char *desc);
52void AnnotateBenignRaceSized(const char *f, int l, uptr mem, uptr size,
53                             char *desc);
54void AnnotateBenignRace(const char *f, int l, uptr mem, char *desc);
55void AnnotateIgnoreReadsBegin(const char *f, int l);
56void AnnotateIgnoreReadsEnd(const char *f, int l);
57void AnnotateIgnoreWritesBegin(const char *f, int l);
58void AnnotateIgnoreWritesEnd(const char *f, int l);
59void AnnotateIgnoreSyncBegin(const char *f, int l);
60void AnnotateIgnoreSyncEnd(const char *f, int l);
61void AnnotatePublishMemoryRange(const char *f, int l, uptr addr, uptr size);
62void AnnotateUnpublishMemoryRange(const char *f, int l, uptr addr, uptr size);
63void AnnotateThreadName(const char *f, int l, char *name);
64void WTFAnnotateHappensBefore(const char *f, int l, uptr addr);
65void WTFAnnotateHappensAfter(const char *f, int l, uptr addr);
66void WTFAnnotateBenignRaceSized(const char *f, int l, uptr mem, uptr sz,
67                                char *desc);
68int RunningOnValgrind();
69double ValgrindSlowdown(void);
70const char *ThreadSanitizerQuery(const char *query);
71void AnnotateMemoryIsInitialized(const char *f, int l, uptr mem, uptr sz);
72
73#ifdef __cplusplus
74}
75#endif
76
77#ifdef TSAN_SUPPORT
78#define ANNOTATE_HAPPENS_AFTER(addr)                                           \
79  AnnotateHappensAfter(__FILE__, __LINE__, (uptr)addr)
80#define ANNOTATE_HAPPENS_BEFORE(addr)                                          \
81  AnnotateHappensBefore(__FILE__, __LINE__, (uptr)addr)
82#define ANNOTATE_IGNORE_WRITES_BEGIN()                                         \
83  AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
84#define ANNOTATE_IGNORE_WRITES_END() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
85#define ANNOTATE_RWLOCK_CREATE(lck)                                            \
86  AnnotateRWLockCreate(__FILE__, __LINE__, (uptr)lck)
87#define ANNOTATE_RWLOCK_RELEASED(lck)                                          \
88  AnnotateRWLockAcquired(__FILE__, __LINE__, (uptr)lck, 1)
89#define ANNOTATE_RWLOCK_ACQUIRED(lck)                                          \
90  AnnotateRWLockReleased(__FILE__, __LINE__, (uptr)lck, 1)
91#define ANNOTATE_BARRIER_BEGIN(addr)                                           \
92  AnnotateHappensBefore(__FILE__, __LINE__, (uptr)addr)
93#define ANNOTATE_BARRIER_END(addr)                                             \
94  AnnotateHappensAfter(__FILE__, __LINE__, (uptr)addr)
95#define ANNOTATE_REDUCE_AFTER(addr)                                            \
96  AnnotateHappensAfter(__FILE__, __LINE__, (uptr)addr)
97#define ANNOTATE_REDUCE_BEFORE(addr)                                           \
98  AnnotateHappensBefore(__FILE__, __LINE__, (uptr)addr)
99#else
100#define ANNOTATE_HAPPENS_AFTER(addr)
101#define ANNOTATE_HAPPENS_BEFORE(addr)
102#define ANNOTATE_IGNORE_WRITES_BEGIN()
103#define ANNOTATE_IGNORE_WRITES_END()
104#define ANNOTATE_RWLOCK_CREATE(lck)
105#define ANNOTATE_RWLOCK_RELEASED(lck)
106#define ANNOTATE_RWLOCK_ACQUIRED(lck)
107#define ANNOTATE_BARRIER_BEGIN(addr)
108#define ANNOTATE_BARRIER_END(addr)
109#define ANNOTATE_REDUCE_AFTER(addr)
110#define ANNOTATE_REDUCE_BEFORE(addr)
111#endif
112
113#define ANNOTATE_QUEUING
114#define ANNOTATE_TICKET
115#define ANNOTATE_FUTEX
116#define ANNOTATE_TAS
117#define ANNOTATE_DRDPA
118
119#ifdef ANNOTATE_QUEUING
120#define ANNOTATE_QUEUING_CREATE(lck)
121#define ANNOTATE_QUEUING_RELEASED(lck) ANNOTATE_HAPPENS_BEFORE(lck)
122#define ANNOTATE_QUEUING_ACQUIRED(lck) ANNOTATE_HAPPENS_AFTER(lck)
123#else
124#define ANNOTATE_QUEUING_CREATE(lck)
125#define ANNOTATE_QUEUING_RELEASED(lck)
126#define ANNOTATE_QUEUING_ACQUIRED(lck)
127#endif
128
129#ifdef ANNOTATE_TICKET
130#define ANNOTATE_TICKET_CREATE(lck)
131#define ANNOTATE_TICKET_RELEASED(lck) ANNOTATE_HAPPENS_BEFORE(lck)
132#define ANNOTATE_TICKET_ACQUIRED(lck) ANNOTATE_HAPPENS_AFTER(lck)
133#else
134#define ANNOTATE_TICKET_CREATE(lck)
135#define ANNOTATE_TICKET_RELEASED(lck)
136#define ANNOTATE_TICKET_ACQUIRED(lck)
137#endif
138
139#ifdef ANNOTATE_FUTEX
140#define ANNOTATE_FUTEX_CREATE(lck)
141#define ANNOTATE_FUTEX_RELEASED(lck) ANNOTATE_HAPPENS_BEFORE(lck)
142#define ANNOTATE_FUTEX_ACQUIRED(lck) ANNOTATE_HAPPENS_AFTER(lck)
143#else
144#define ANNOTATE_FUTEX_CREATE(lck)
145#define ANNOTATE_FUTEX_RELEASED(lck)
146#define ANNOTATE_FUTEX_ACQUIRED(lck)
147#endif
148
149#ifdef ANNOTATE_TAS
150#define ANNOTATE_TAS_CREATE(lck)
151#define ANNOTATE_TAS_RELEASED(lck) ANNOTATE_HAPPENS_BEFORE(lck)
152#define ANNOTATE_TAS_ACQUIRED(lck) ANNOTATE_HAPPENS_AFTER(lck)
153#else
154#define ANNOTATE_TAS_CREATE(lck)
155#define ANNOTATE_TAS_RELEASED(lck)
156#define ANNOTATE_TAS_ACQUIRED(lck)
157#endif
158
159#ifdef ANNOTATE_DRDPA
160#define ANNOTATE_DRDPA_CREATE(lck)
161#define ANNOTATE_DRDPA_RELEASED(lck) ANNOTATE_HAPPENS_BEFORE(lck)
162#define ANNOTATE_DRDPA_ACQUIRED(lck) ANNOTATE_HAPPENS_AFTER(lck)
163#else
164#define ANNOTATE_DRDPA_CREATE(lck)
165#define ANNOTATE_DRDPA_RELEASED(lck)
166#define ANNOTATE_DRDPA_ACQUIRED(lck)
167#endif
168
169#endif
170