xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/include/sanitizer/tsan_interface.h (revision a7c257b03e4462df2b1020128fb82716512d7856)
1*a7c257b0Skamil //===-- tsan_interface.h ----------------------------------------*- C++ -*-===//
2*a7c257b0Skamil //
3*a7c257b0Skamil //                     The LLVM Compiler Infrastructure
4*a7c257b0Skamil //
5*a7c257b0Skamil // This file is distributed under the University of Illinois Open Source
6*a7c257b0Skamil // License. See LICENSE.TXT for details.
7*a7c257b0Skamil //
8*a7c257b0Skamil //===----------------------------------------------------------------------===//
9*a7c257b0Skamil //
10*a7c257b0Skamil // This file is a part of ThreadSanitizer (TSan), a race detector.
11*a7c257b0Skamil //
12*a7c257b0Skamil // Public interface header for TSan.
13*a7c257b0Skamil //===----------------------------------------------------------------------===//
14*a7c257b0Skamil #ifndef SANITIZER_TSAN_INTERFACE_H
15*a7c257b0Skamil #define SANITIZER_TSAN_INTERFACE_H
16*a7c257b0Skamil 
17*a7c257b0Skamil #include <sanitizer/common_interface_defs.h>
18*a7c257b0Skamil 
19*a7c257b0Skamil #ifdef __cplusplus
20*a7c257b0Skamil extern "C" {
21*a7c257b0Skamil #endif
22*a7c257b0Skamil 
23*a7c257b0Skamil // __tsan_release establishes a happens-before relation with a preceding
24*a7c257b0Skamil // __tsan_acquire on the same address.
25*a7c257b0Skamil void __tsan_acquire(void *addr);
26*a7c257b0Skamil void __tsan_release(void *addr);
27*a7c257b0Skamil 
28*a7c257b0Skamil // Annotations for custom mutexes.
29*a7c257b0Skamil // The annotations allow to get better reports (with sets of locked mutexes),
30*a7c257b0Skamil // detect more types of bugs (e.g. mutex misuses, races between lock/unlock and
31*a7c257b0Skamil // destruction and potential deadlocks) and improve precision and performance
32*a7c257b0Skamil // (by ignoring individual atomic operations in mutex code). However, the
33*a7c257b0Skamil // downside is that annotated mutex code itself is not checked for correctness.
34*a7c257b0Skamil 
35*a7c257b0Skamil // Mutex creation flags are passed to __tsan_mutex_create annotation.
36*a7c257b0Skamil // If mutex has no constructor and __tsan_mutex_create is not called,
37*a7c257b0Skamil // the flags may be passed to __tsan_mutex_pre_lock/__tsan_mutex_post_lock
38*a7c257b0Skamil // annotations.
39*a7c257b0Skamil 
40*a7c257b0Skamil // Mutex has static storage duration and no-op constructor and destructor.
41*a7c257b0Skamil // This effectively makes tsan ignore destroy annotation.
42*a7c257b0Skamil const unsigned __tsan_mutex_linker_init      = 1 << 0;
43*a7c257b0Skamil // Mutex is write reentrant.
44*a7c257b0Skamil const unsigned __tsan_mutex_write_reentrant  = 1 << 1;
45*a7c257b0Skamil // Mutex is read reentrant.
46*a7c257b0Skamil const unsigned __tsan_mutex_read_reentrant   = 1 << 2;
47*a7c257b0Skamil // Mutex does not have static storage duration, and must not be used after
48*a7c257b0Skamil // its destructor runs.  The opposite of __tsan_mutex_linker_init.
49*a7c257b0Skamil // If this flag is passed to __tsan_mutex_destroy, then the destruction
50*a7c257b0Skamil // is ignored unless this flag was previously set on the mutex.
51*a7c257b0Skamil const unsigned __tsan_mutex_not_static       = 1 << 8;
52*a7c257b0Skamil 
53*a7c257b0Skamil // Mutex operation flags:
54*a7c257b0Skamil 
55*a7c257b0Skamil // Denotes read lock operation.
56*a7c257b0Skamil const unsigned __tsan_mutex_read_lock        = 1 << 3;
57*a7c257b0Skamil // Denotes try lock operation.
58*a7c257b0Skamil const unsigned __tsan_mutex_try_lock         = 1 << 4;
59*a7c257b0Skamil // Denotes that a try lock operation has failed to acquire the mutex.
60*a7c257b0Skamil const unsigned __tsan_mutex_try_lock_failed  = 1 << 5;
61*a7c257b0Skamil // Denotes that the lock operation acquires multiple recursion levels.
62*a7c257b0Skamil // Number of levels is passed in recursion parameter.
63*a7c257b0Skamil // This is useful for annotation of e.g. Java builtin monitors,
64*a7c257b0Skamil // for which wait operation releases all recursive acquisitions of the mutex.
65*a7c257b0Skamil const unsigned __tsan_mutex_recursive_lock   = 1 << 6;
66*a7c257b0Skamil // Denotes that the unlock operation releases all recursion levels.
67*a7c257b0Skamil // Number of released levels is returned and later must be passed to
68*a7c257b0Skamil // the corresponding __tsan_mutex_post_lock annotation.
69*a7c257b0Skamil const unsigned __tsan_mutex_recursive_unlock = 1 << 7;
70*a7c257b0Skamil 
71*a7c257b0Skamil // Annotate creation of a mutex.
72*a7c257b0Skamil // Supported flags: mutex creation flags.
73*a7c257b0Skamil void __tsan_mutex_create(void *addr, unsigned flags);
74*a7c257b0Skamil 
75*a7c257b0Skamil // Annotate destruction of a mutex.
76*a7c257b0Skamil // Supported flags:
77*a7c257b0Skamil //   - __tsan_mutex_linker_init
78*a7c257b0Skamil //   - __tsan_mutex_not_static
79*a7c257b0Skamil void __tsan_mutex_destroy(void *addr, unsigned flags);
80*a7c257b0Skamil 
81*a7c257b0Skamil // Annotate start of lock operation.
82*a7c257b0Skamil // Supported flags:
83*a7c257b0Skamil //   - __tsan_mutex_read_lock
84*a7c257b0Skamil //   - __tsan_mutex_try_lock
85*a7c257b0Skamil //   - all mutex creation flags
86*a7c257b0Skamil void __tsan_mutex_pre_lock(void *addr, unsigned flags);
87*a7c257b0Skamil 
88*a7c257b0Skamil // Annotate end of lock operation.
89*a7c257b0Skamil // Supported flags:
90*a7c257b0Skamil //   - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock)
91*a7c257b0Skamil //   - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock)
92*a7c257b0Skamil //   - __tsan_mutex_try_lock_failed
93*a7c257b0Skamil //   - __tsan_mutex_recursive_lock
94*a7c257b0Skamil //   - all mutex creation flags
95*a7c257b0Skamil void __tsan_mutex_post_lock(void *addr, unsigned flags, int recursion);
96*a7c257b0Skamil 
97*a7c257b0Skamil // Annotate start of unlock operation.
98*a7c257b0Skamil // Supported flags:
99*a7c257b0Skamil //   - __tsan_mutex_read_lock
100*a7c257b0Skamil //   - __tsan_mutex_recursive_unlock
101*a7c257b0Skamil int __tsan_mutex_pre_unlock(void *addr, unsigned flags);
102*a7c257b0Skamil 
103*a7c257b0Skamil // Annotate end of unlock operation.
104*a7c257b0Skamil // Supported flags:
105*a7c257b0Skamil //   - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)
106*a7c257b0Skamil void __tsan_mutex_post_unlock(void *addr, unsigned flags);
107*a7c257b0Skamil 
108*a7c257b0Skamil // Annotate start/end of notify/signal/broadcast operation.
109*a7c257b0Skamil // Supported flags: none.
110*a7c257b0Skamil void __tsan_mutex_pre_signal(void *addr, unsigned flags);
111*a7c257b0Skamil void __tsan_mutex_post_signal(void *addr, unsigned flags);
112*a7c257b0Skamil 
113*a7c257b0Skamil // Annotate start/end of a region of code where lock/unlock/signal operation
114*a7c257b0Skamil // diverts to do something else unrelated to the mutex. This can be used to
115*a7c257b0Skamil // annotate, for example, calls into cooperative scheduler or contention
116*a7c257b0Skamil // profiling code.
117*a7c257b0Skamil // These annotations must be called only from within
118*a7c257b0Skamil // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,
119*a7c257b0Skamil // __tsan_mutex_pre/post_signal regions.
120*a7c257b0Skamil // Supported flags: none.
121*a7c257b0Skamil void __tsan_mutex_pre_divert(void *addr, unsigned flags);
122*a7c257b0Skamil void __tsan_mutex_post_divert(void *addr, unsigned flags);
123*a7c257b0Skamil 
124*a7c257b0Skamil // External race detection API.
125*a7c257b0Skamil // Can be used by non-instrumented libraries to detect when their objects are
126*a7c257b0Skamil // being used in an unsafe manner.
127*a7c257b0Skamil //   - __tsan_external_read/__tsan_external_write annotates the logical reads
128*a7c257b0Skamil //       and writes of the object at the specified address. 'caller_pc' should
129*a7c257b0Skamil //       be the PC of the library user, which the library can obtain with e.g.
130*a7c257b0Skamil //       `__builtin_return_address(0)`.
131*a7c257b0Skamil //   - __tsan_external_register_tag registers a 'tag' with the specified name,
132*a7c257b0Skamil //       which is later used in read/write annotations to denote the object type
133*a7c257b0Skamil //   - __tsan_external_assign_tag can optionally mark a heap object with a tag
134*a7c257b0Skamil void *__tsan_external_register_tag(const char *object_type);
135*a7c257b0Skamil void __tsan_external_register_header(void *tag, const char *header);
136*a7c257b0Skamil void __tsan_external_assign_tag(void *addr, void *tag);
137*a7c257b0Skamil void __tsan_external_read(void *addr, void *caller_pc, void *tag);
138*a7c257b0Skamil void __tsan_external_write(void *addr, void *caller_pc, void *tag);
139*a7c257b0Skamil 
140*a7c257b0Skamil #ifdef __cplusplus
141*a7c257b0Skamil }  // extern "C"
142*a7c257b0Skamil #endif
143*a7c257b0Skamil 
144*a7c257b0Skamil #endif  // SANITIZER_TSAN_INTERFACE_H
145