1 //===-- tsan_interface.h ----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 // Public interface header for TSan.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_TSAN_INTERFACE_H
14 #define SANITIZER_TSAN_INTERFACE_H
16 #include <sanitizer/common_interface_defs.h>
22 // __tsan_release establishes a happens-before relation with a preceding
23 // __tsan_acquire on the same address.
24 void SANITIZER_CDECL
__tsan_acquire(void *addr
);
25 void SANITIZER_CDECL
__tsan_release(void *addr
);
27 // Annotations for custom mutexes.
28 // The annotations allow to get better reports (with sets of locked mutexes),
29 // detect more types of bugs (e.g. mutex misuses, races between lock/unlock and
30 // destruction and potential deadlocks) and improve precision and performance
31 // (by ignoring individual atomic operations in mutex code). However, the
32 // downside is that annotated mutex code itself is not checked for correctness.
34 // Mutex creation flags are passed to __tsan_mutex_create annotation.
35 // If mutex has no constructor and __tsan_mutex_create is not called,
36 // the flags may be passed to __tsan_mutex_pre_lock/__tsan_mutex_post_lock
39 // Mutex has static storage duration and no-op constructor and destructor.
40 // This effectively makes tsan ignore destroy annotation.
41 static const unsigned __tsan_mutex_linker_init
= 1 << 0;
42 // Mutex is write reentrant.
43 static const unsigned __tsan_mutex_write_reentrant
= 1 << 1;
44 // Mutex is read reentrant.
45 static const unsigned __tsan_mutex_read_reentrant
= 1 << 2;
46 // Mutex does not have static storage duration, and must not be used after
47 // its destructor runs. The opposite of __tsan_mutex_linker_init.
48 // If this flag is passed to __tsan_mutex_destroy, then the destruction
49 // is ignored unless this flag was previously set on the mutex.
50 static const unsigned __tsan_mutex_not_static
= 1 << 8;
52 // Mutex operation flags:
54 // Denotes read lock operation.
55 static const unsigned __tsan_mutex_read_lock
= 1 << 3;
56 // Denotes try lock operation.
57 static const unsigned __tsan_mutex_try_lock
= 1 << 4;
58 // Denotes that a try lock operation has failed to acquire the mutex.
59 static const unsigned __tsan_mutex_try_lock_failed
= 1 << 5;
60 // Denotes that the lock operation acquires multiple recursion levels.
61 // Number of levels is passed in recursion parameter.
62 // This is useful for annotation of e.g. Java builtin monitors,
63 // for which wait operation releases all recursive acquisitions of the mutex.
64 static const unsigned __tsan_mutex_recursive_lock
= 1 << 6;
65 // Denotes that the unlock operation releases all recursion levels.
66 // Number of released levels is returned and later must be passed to
67 // the corresponding __tsan_mutex_post_lock annotation.
68 static const unsigned __tsan_mutex_recursive_unlock
= 1 << 7;
70 // Convenient composed constants.
71 static const unsigned __tsan_mutex_try_read_lock
=
72 __tsan_mutex_read_lock
| __tsan_mutex_try_lock
;
73 static const unsigned __tsan_mutex_try_read_lock_failed
=
74 __tsan_mutex_try_read_lock
| __tsan_mutex_try_lock_failed
;
76 // Annotate creation of a mutex.
77 // Supported flags: mutex creation flags.
78 void SANITIZER_CDECL
__tsan_mutex_create(void *addr
, unsigned flags
);
80 // Annotate destruction of a mutex.
82 // - __tsan_mutex_linker_init
83 // - __tsan_mutex_not_static
84 void SANITIZER_CDECL
__tsan_mutex_destroy(void *addr
, unsigned flags
);
86 // Annotate start of lock operation.
88 // - __tsan_mutex_read_lock
89 // - __tsan_mutex_try_lock
90 // - all mutex creation flags
91 void SANITIZER_CDECL
__tsan_mutex_pre_lock(void *addr
, unsigned flags
);
93 // Annotate end of lock operation.
95 // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock)
96 // - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock)
97 // - __tsan_mutex_try_lock_failed
98 // - __tsan_mutex_recursive_lock
99 // - all mutex creation flags
100 void SANITIZER_CDECL
__tsan_mutex_post_lock(void *addr
, unsigned flags
,
103 // Annotate start of unlock operation.
105 // - __tsan_mutex_read_lock
106 // - __tsan_mutex_recursive_unlock
107 int SANITIZER_CDECL
__tsan_mutex_pre_unlock(void *addr
, unsigned flags
);
109 // Annotate end of unlock operation.
111 // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)
112 void SANITIZER_CDECL
__tsan_mutex_post_unlock(void *addr
, unsigned flags
);
114 // Annotate start/end of notify/signal/broadcast operation.
115 // Supported flags: none.
116 void SANITIZER_CDECL
__tsan_mutex_pre_signal(void *addr
, unsigned flags
);
117 void SANITIZER_CDECL
__tsan_mutex_post_signal(void *addr
, unsigned flags
);
119 // Annotate start/end of a region of code where lock/unlock/signal operation
120 // diverts to do something else unrelated to the mutex. This can be used to
121 // annotate, for example, calls into cooperative scheduler or contention
123 // These annotations must be called only from within
124 // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,
125 // __tsan_mutex_pre/post_signal regions.
126 // Supported flags: none.
127 void SANITIZER_CDECL
__tsan_mutex_pre_divert(void *addr
, unsigned flags
);
128 void SANITIZER_CDECL
__tsan_mutex_post_divert(void *addr
, unsigned flags
);
130 // External race detection API.
131 // Can be used by non-instrumented libraries to detect when their objects are
132 // being used in an unsafe manner.
133 // - __tsan_external_read/__tsan_external_write annotates the logical reads
134 // and writes of the object at the specified address. 'caller_pc' should
135 // be the PC of the library user, which the library can obtain with e.g.
136 // `__builtin_return_address(0)`.
137 // - __tsan_external_register_tag registers a 'tag' with the specified name,
138 // which is later used in read/write annotations to denote the object type
139 // - __tsan_external_assign_tag can optionally mark a heap object with a tag
140 void *SANITIZER_CDECL
__tsan_external_register_tag(const char *object_type
);
141 void SANITIZER_CDECL
__tsan_external_register_header(void *tag
,
143 void SANITIZER_CDECL
__tsan_external_assign_tag(void *addr
, void *tag
);
144 void SANITIZER_CDECL
__tsan_external_read(void *addr
, void *caller_pc
,
146 void SANITIZER_CDECL
__tsan_external_write(void *addr
, void *caller_pc
,
149 // Fiber switching API.
150 // - TSAN context for fiber can be created by __tsan_create_fiber
151 // and freed by __tsan_destroy_fiber.
152 // - TSAN context of current fiber or thread can be obtained
153 // by calling __tsan_get_current_fiber.
154 // - __tsan_switch_to_fiber should be called immediately before switch
155 // to fiber, such as call of swapcontext.
156 // - Fiber name can be set by __tsan_set_fiber_name.
157 void *SANITIZER_CDECL
__tsan_get_current_fiber(void);
158 void *SANITIZER_CDECL
__tsan_create_fiber(unsigned flags
);
159 void SANITIZER_CDECL
__tsan_destroy_fiber(void *fiber
);
160 void SANITIZER_CDECL
__tsan_switch_to_fiber(void *fiber
, unsigned flags
);
161 void SANITIZER_CDECL
__tsan_set_fiber_name(void *fiber
, const char *name
);
163 // Flags for __tsan_switch_to_fiber:
164 // Do not establish a happens-before relation between fibers
165 static const unsigned __tsan_switch_to_fiber_no_sync
= 1 << 0;
167 // User-provided callback invoked on TSan initialization.
168 void SANITIZER_CDECL
__tsan_on_initialize();
170 // User-provided callback invoked on TSan shutdown.
171 // `failed` - Nonzero if TSan did detect issues, zero otherwise.
172 // Return `0` if TSan should exit as if no issues were detected. Return nonzero
173 // if TSan should exit as if issues were detected.
174 int SANITIZER_CDECL
__tsan_on_finalize(int failed
);
176 // Release TSan internal memory in a best-effort manner.
177 void SANITIZER_CDECL
__tsan_flush_memory();
179 // User-provided default TSAN options.
180 const char *SANITIZER_CDECL
__tsan_default_options(void);
182 // User-provided default TSAN suppressions.
183 const char *SANITIZER_CDECL
__tsan_default_suppressions(void);
185 /// Returns a report's description.
187 /// Returns a report's description (issue type), number of duplicate issues
188 /// found, counts of array data (stack traces, memory operations, locations,
189 /// mutexes, threads, unique thread IDs) and a stack trace of a <c>sleep()</c>
190 /// call (if one was involved in the issue).
192 /// \param report Opaque pointer to the current report.
193 /// \param[out] description Report type description.
194 /// \param[out] count Count of duplicate issues.
195 /// \param[out] stack_count Count of stack traces.
196 /// \param[out] mop_count Count of memory operations.
197 /// \param[out] loc_count Count of locations.
198 /// \param[out] mutex_count Count of mutexes.
199 /// \param[out] thread_count Count of threads.
200 /// \param[out] unique_tid_count Count of unique thread IDs.
201 /// \param sleep_trace A buffer to store the stack trace of a <c>sleep()</c>
203 /// \param trace_size Size in bytes of the trace buffer.
204 /// \returns Returns 1 if successful, 0 if not.
205 int SANITIZER_CDECL
__tsan_get_report_data(
206 void *report
, const char **description
, int *count
, int *stack_count
,
207 int *mop_count
, int *loc_count
, int *mutex_count
, int *thread_count
,
208 int *unique_tid_count
, void **sleep_trace
, unsigned long trace_size
);
210 /// Returns information about stack traces included in the report.
212 /// \param report Opaque pointer to the current report.
213 /// \param idx Index to the report's stacks.
214 /// \param trace A buffer to store the stack trace.
215 /// \param trace_size Size in bytes of the trace buffer.
216 /// \returns Returns 1 if successful, 0 if not.
217 int SANITIZER_CDECL
__tsan_get_report_stack(void *report
, unsigned long idx
,
219 unsigned long trace_size
);
221 /// Returns information about memory operations included in the report.
223 /// \param report Opaque pointer to the current report.
224 /// \param idx Index to the report's memory operations.
225 /// \param[out] tid Thread ID of the memory operation.
226 /// \param[out] addr Address of the memory operation.
227 /// \param[out] size Size of the memory operation.
228 /// \param[out] write Write flag of the memory operation.
229 /// \param[out] atomic Atomicity flag of the memory operation.
230 /// \param trace A buffer to store the stack trace.
231 /// \param trace_size Size in bytes of the trace buffer.
232 /// \returns Returns 1 if successful, 0 if not.
233 int SANITIZER_CDECL
__tsan_get_report_mop(void *report
, unsigned long idx
,
234 int *tid
, void **addr
, int *size
,
235 int *write
, int *atomic
, void **trace
,
236 unsigned long trace_size
);
238 /// Returns information about locations included in the report.
240 /// \param report Opaque pointer to the current report.
241 /// \param idx Index to the report's locations.
242 /// \param[out] type Type of the location.
243 /// \param[out] addr Address of the location.
244 /// \param[out] start Start of the location.
245 /// \param[out] size Size of the location.
246 /// \param[out] tid Thread ID of the location.
247 /// \param[out] fd File descriptor of the location.
248 /// \param[out] suppressable Suppressable flag.
249 /// \param trace A buffer to store the stack trace.
250 /// \param trace_size Size in bytes of the trace buffer.
251 /// \returns Returns 1 if successful, 0 if not.
252 int SANITIZER_CDECL
__tsan_get_report_loc(void *report
, unsigned long idx
,
253 const char **type
, void **addr
,
254 void **start
, unsigned long *size
,
255 int *tid
, int *fd
, int *suppressable
,
257 unsigned long trace_size
);
259 /// Returns information about mutexes included in the report.
261 /// \param report Opaque pointer to the current report.
262 /// \param idx Index to the report's mutexes.
263 /// \param[out] mutex_id Id of the mutex.
264 /// \param[out] addr Address of the mutex.
265 /// \param[out] destroyed Destroyed mutex flag.
266 /// \param trace A buffer to store the stack trace.
267 /// \param trace_size Size in bytes of the trace buffer.
268 /// \returns Returns 1 if successful, 0 if not.
269 int SANITIZER_CDECL
__tsan_get_report_mutex(void *report
, unsigned long idx
,
270 uint64_t *mutex_id
, void **addr
,
271 int *destroyed
, void **trace
,
272 unsigned long trace_size
);
274 /// Returns information about threads included in the report.
276 /// \param report Opaque pointer to the current report.
277 /// \param idx Index to the report's threads.
278 /// \param[out] tid Thread ID of the thread.
279 /// \param[out] os_id Operating system's ID of the thread.
280 /// \param[out] running Running flag of the thread.
281 /// \param[out] name Name of the thread.
282 /// \param[out] parent_tid ID of the parent thread.
283 /// \param trace A buffer to store the stack trace.
284 /// \param trace_size Size in bytes of the trace buffer.
285 /// \returns Returns 1 if successful, 0 if not.
286 int SANITIZER_CDECL
__tsan_get_report_thread(void *report
, unsigned long idx
,
287 int *tid
, uint64_t *os_id
,
288 int *running
, const char **name
,
289 int *parent_tid
, void **trace
,
290 unsigned long trace_size
);
292 /// Returns information about unique thread IDs included in the report.
294 /// \param report Opaque pointer to the current report.
295 /// \param idx Index to the report's unique thread IDs.
296 /// \param[out] tid Unique thread ID of the report.
297 /// \returns Returns 1 if successful, 0 if not.
298 int SANITIZER_CDECL
__tsan_get_report_unique_tid(void *report
,
299 unsigned long idx
, int *tid
);
301 /// Returns the current report.
303 /// If TSan is currently reporting a detected issue on the current thread,
304 /// returns an opaque pointer to the current report. Otherwise returns NULL.
305 /// \returns An opaque pointer to the current report. Otherwise returns NULL.
306 void *SANITIZER_CDECL
__tsan_get_current_report();
312 #endif // SANITIZER_TSAN_INTERFACE_H