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