1 //===-- tsan_interface.h ----------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of ThreadSanitizer (TSan), a race detector. 10 // 11 // The functions declared in this header will be inserted by the instrumentation 12 // module. 13 // This header can be included by the instrumented program or by TSan tests. 14 //===----------------------------------------------------------------------===// 15 #ifndef TSAN_INTERFACE_H 16 #define TSAN_INTERFACE_H 17 18 #include <sanitizer_common/sanitizer_internal_defs.h> 19 using __sanitizer::uptr; 20 using __sanitizer::tid_t; 21 22 // This header should NOT include any other headers. 23 // All functions in this header are extern "C" and start with __tsan_. 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 #if !SANITIZER_GO 30 31 // This function should be called at the very beginning of the process, 32 // before any instrumented code is executed and before any call to malloc. 33 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_init(); 34 35 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_flush_memory(); 36 37 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read1(void *addr); 38 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read2(void *addr); 39 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read4(void *addr); 40 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read8(void *addr); 41 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read16(void *addr); 42 43 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write1(void *addr); 44 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write2(void *addr); 45 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write4(void *addr); 46 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write8(void *addr); 47 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write16(void *addr); 48 49 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read2(const void *addr); 50 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read4(const void *addr); 51 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read8(const void *addr); 52 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read16(const void *addr); 53 54 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write2(void *addr); 55 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write4(void *addr); 56 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write8(void *addr); 57 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write16(void *addr); 58 59 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read1_pc(void *addr, void *pc); 60 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read2_pc(void *addr, void *pc); 61 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read4_pc(void *addr, void *pc); 62 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read8_pc(void *addr, void *pc); 63 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_read16_pc(void *addr, void *pc); 64 65 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write1_pc(void *addr, void *pc); 66 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write2_pc(void *addr, void *pc); 67 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write4_pc(void *addr, void *pc); 68 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write8_pc(void *addr, void *pc); 69 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write16_pc(void *addr, void *pc); 70 71 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_vptr_read(void **vptr_p); 72 SANITIZER_INTERFACE_ATTRIBUTE 73 void __tsan_vptr_update(void **vptr_p, void *new_val); 74 75 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_func_entry(void *call_pc); 76 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_func_exit(); 77 78 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_ignore_thread_begin(); 79 SANITIZER_INTERFACE_ATTRIBUTE void __tsan_ignore_thread_end(); 80 81 SANITIZER_INTERFACE_ATTRIBUTE 82 void *__tsan_external_register_tag(const char *object_type); 83 SANITIZER_INTERFACE_ATTRIBUTE 84 void __tsan_external_register_header(void *tag, const char *header); 85 SANITIZER_INTERFACE_ATTRIBUTE 86 void __tsan_external_assign_tag(void *addr, void *tag); 87 SANITIZER_INTERFACE_ATTRIBUTE 88 void __tsan_external_read(void *addr, void *caller_pc, void *tag); 89 SANITIZER_INTERFACE_ATTRIBUTE 90 void __tsan_external_write(void *addr, void *caller_pc, void *tag); 91 92 SANITIZER_INTERFACE_ATTRIBUTE 93 void __tsan_read_range(void *addr, unsigned long size); 94 SANITIZER_INTERFACE_ATTRIBUTE 95 void __tsan_write_range(void *addr, unsigned long size); 96 97 SANITIZER_INTERFACE_ATTRIBUTE 98 void __tsan_read_range_pc(void *addr, unsigned long size, void *pc); 99 SANITIZER_INTERFACE_ATTRIBUTE 100 void __tsan_write_range_pc(void *addr, unsigned long size, void *pc); 101 102 // User may provide function that would be called right when TSan detects 103 // an error. The argument 'report' is an opaque pointer that can be used to 104 // gather additional information using other TSan report API functions. 105 SANITIZER_INTERFACE_ATTRIBUTE 106 void __tsan_on_report(void *report); 107 108 // If TSan is currently reporting a detected issue on the current thread, 109 // returns an opaque pointer to the current report. Otherwise returns NULL. 110 SANITIZER_INTERFACE_ATTRIBUTE 111 void *__tsan_get_current_report(); 112 113 // Returns a report's description (issue type), number of duplicate issues 114 // found, counts of array data (stack traces, memory operations, locations, 115 // mutexes, threads, unique thread IDs) and a stack trace of a sleep() call (if 116 // one was involved in the issue). 117 SANITIZER_INTERFACE_ATTRIBUTE 118 int __tsan_get_report_data(void *report, const char **description, int *count, 119 int *stack_count, int *mop_count, int *loc_count, 120 int *mutex_count, int *thread_count, 121 int *unique_tid_count, void **sleep_trace, 122 uptr trace_size); 123 124 /// Retrieves the "tag" from a report (for external-race report types). External 125 /// races can be associated with a tag which give them more meaning. For example 126 /// tag value '1' means "Swift access race". Tag value '0' indicated a plain 127 /// external race. 128 /// 129 /// \param report opaque pointer to the current report (obtained as argument in 130 /// __tsan_on_report, or from __tsan_get_current_report) 131 /// \param [out] tag points to storage that will be filled with the tag value 132 /// 133 /// \returns non-zero value on success, zero on failure 134 SANITIZER_INTERFACE_ATTRIBUTE 135 int __tsan_get_report_tag(void *report, uptr *tag); 136 137 // Returns information about stack traces included in the report. 138 SANITIZER_INTERFACE_ATTRIBUTE 139 int __tsan_get_report_stack(void *report, uptr idx, void **trace, 140 uptr trace_size); 141 142 // Returns information about memory operations included in the report. 143 SANITIZER_INTERFACE_ATTRIBUTE 144 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr, 145 int *size, int *write, int *atomic, void **trace, 146 uptr trace_size); 147 148 // Returns information about locations included in the report. 149 SANITIZER_INTERFACE_ATTRIBUTE 150 int __tsan_get_report_loc(void *report, uptr idx, const char **type, 151 void **addr, uptr *start, uptr *size, int *tid, 152 int *fd, int *suppressable, void **trace, 153 uptr trace_size); 154 155 SANITIZER_INTERFACE_ATTRIBUTE 156 int __tsan_get_report_loc_object_type(void *report, uptr idx, 157 const char **object_type); 158 159 // Returns information about mutexes included in the report. 160 SANITIZER_INTERFACE_ATTRIBUTE 161 int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr, 162 int *destroyed, void **trace, uptr trace_size); 163 164 // Returns information about threads included in the report. 165 SANITIZER_INTERFACE_ATTRIBUTE 166 int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id, 167 int *running, const char **name, int *parent_tid, 168 void **trace, uptr trace_size); 169 170 // Returns information about unique thread IDs included in the report. 171 SANITIZER_INTERFACE_ATTRIBUTE 172 int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid); 173 174 // Returns the type of the pointer (heap, stack, global, ...) and if possible 175 // also the starting address (e.g. of a heap allocation) and size. 176 SANITIZER_INTERFACE_ATTRIBUTE 177 const char *__tsan_locate_address(uptr addr, char *name, uptr name_size, 178 uptr *region_address, uptr *region_size); 179 180 // Returns the allocation stack for a heap pointer. 181 SANITIZER_INTERFACE_ATTRIBUTE 182 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id, 183 tid_t *os_id); 184 185 #endif // SANITIZER_GO 186 187 #ifdef __cplusplus 188 } // extern "C" 189 #endif 190 191 namespace __tsan { 192 193 // These should match declarations from public tsan_interface_atomic.h header. 194 typedef unsigned char a8; 195 typedef unsigned short a16; 196 typedef unsigned int a32; 197 typedef unsigned long long a64; 198 #if !SANITIZER_GO && (defined(__SIZEOF_INT128__) \ 199 || (__clang_major__ * 100 + __clang_minor__ >= 302)) && \ 200 !defined(__mips64) && !defined(__s390x__) 201 __extension__ typedef __int128 a128; 202 # define __TSAN_HAS_INT128 1 203 #else 204 # define __TSAN_HAS_INT128 0 205 #endif 206 207 // Part of ABI, do not change. 208 // https://github.com/llvm/llvm-project/blob/main/libcxx/include/atomic 209 typedef enum { 210 mo_relaxed, 211 mo_consume, 212 mo_acquire, 213 mo_release, 214 mo_acq_rel, 215 mo_seq_cst 216 } morder; 217 218 struct ThreadState; 219 220 extern "C" { 221 SANITIZER_INTERFACE_ATTRIBUTE 222 a8 __tsan_atomic8_load(const volatile a8 *a, morder mo); 223 SANITIZER_INTERFACE_ATTRIBUTE 224 a16 __tsan_atomic16_load(const volatile a16 *a, morder mo); 225 SANITIZER_INTERFACE_ATTRIBUTE 226 a32 __tsan_atomic32_load(const volatile a32 *a, morder mo); 227 SANITIZER_INTERFACE_ATTRIBUTE 228 a64 __tsan_atomic64_load(const volatile a64 *a, morder mo); 229 #if __TSAN_HAS_INT128 230 SANITIZER_INTERFACE_ATTRIBUTE 231 a128 __tsan_atomic128_load(const volatile a128 *a, morder mo); 232 #endif 233 234 SANITIZER_INTERFACE_ATTRIBUTE 235 void __tsan_atomic8_store(volatile a8 *a, a8 v, morder mo); 236 SANITIZER_INTERFACE_ATTRIBUTE 237 void __tsan_atomic16_store(volatile a16 *a, a16 v, morder mo); 238 SANITIZER_INTERFACE_ATTRIBUTE 239 void __tsan_atomic32_store(volatile a32 *a, a32 v, morder mo); 240 SANITIZER_INTERFACE_ATTRIBUTE 241 void __tsan_atomic64_store(volatile a64 *a, a64 v, morder mo); 242 #if __TSAN_HAS_INT128 243 SANITIZER_INTERFACE_ATTRIBUTE 244 void __tsan_atomic128_store(volatile a128 *a, a128 v, morder mo); 245 #endif 246 247 SANITIZER_INTERFACE_ATTRIBUTE 248 a8 __tsan_atomic8_exchange(volatile a8 *a, a8 v, morder mo); 249 SANITIZER_INTERFACE_ATTRIBUTE 250 a16 __tsan_atomic16_exchange(volatile a16 *a, a16 v, morder mo); 251 SANITIZER_INTERFACE_ATTRIBUTE 252 a32 __tsan_atomic32_exchange(volatile a32 *a, a32 v, morder mo); 253 SANITIZER_INTERFACE_ATTRIBUTE 254 a64 __tsan_atomic64_exchange(volatile a64 *a, a64 v, morder mo); 255 #if __TSAN_HAS_INT128 256 SANITIZER_INTERFACE_ATTRIBUTE 257 a128 __tsan_atomic128_exchange(volatile a128 *a, a128 v, morder mo); 258 #endif 259 260 SANITIZER_INTERFACE_ATTRIBUTE 261 a8 __tsan_atomic8_fetch_add(volatile a8 *a, a8 v, morder mo); 262 SANITIZER_INTERFACE_ATTRIBUTE 263 a16 __tsan_atomic16_fetch_add(volatile a16 *a, a16 v, morder mo); 264 SANITIZER_INTERFACE_ATTRIBUTE 265 a32 __tsan_atomic32_fetch_add(volatile a32 *a, a32 v, morder mo); 266 SANITIZER_INTERFACE_ATTRIBUTE 267 a64 __tsan_atomic64_fetch_add(volatile a64 *a, a64 v, morder mo); 268 #if __TSAN_HAS_INT128 269 SANITIZER_INTERFACE_ATTRIBUTE 270 a128 __tsan_atomic128_fetch_add(volatile a128 *a, a128 v, morder mo); 271 #endif 272 273 SANITIZER_INTERFACE_ATTRIBUTE 274 a8 __tsan_atomic8_fetch_sub(volatile a8 *a, a8 v, morder mo); 275 SANITIZER_INTERFACE_ATTRIBUTE 276 a16 __tsan_atomic16_fetch_sub(volatile a16 *a, a16 v, morder mo); 277 SANITIZER_INTERFACE_ATTRIBUTE 278 a32 __tsan_atomic32_fetch_sub(volatile a32 *a, a32 v, morder mo); 279 SANITIZER_INTERFACE_ATTRIBUTE 280 a64 __tsan_atomic64_fetch_sub(volatile a64 *a, a64 v, morder mo); 281 #if __TSAN_HAS_INT128 282 SANITIZER_INTERFACE_ATTRIBUTE 283 a128 __tsan_atomic128_fetch_sub(volatile a128 *a, a128 v, morder mo); 284 #endif 285 286 SANITIZER_INTERFACE_ATTRIBUTE 287 a8 __tsan_atomic8_fetch_and(volatile a8 *a, a8 v, morder mo); 288 SANITIZER_INTERFACE_ATTRIBUTE 289 a16 __tsan_atomic16_fetch_and(volatile a16 *a, a16 v, morder mo); 290 SANITIZER_INTERFACE_ATTRIBUTE 291 a32 __tsan_atomic32_fetch_and(volatile a32 *a, a32 v, morder mo); 292 SANITIZER_INTERFACE_ATTRIBUTE 293 a64 __tsan_atomic64_fetch_and(volatile a64 *a, a64 v, morder mo); 294 #if __TSAN_HAS_INT128 295 SANITIZER_INTERFACE_ATTRIBUTE 296 a128 __tsan_atomic128_fetch_and(volatile a128 *a, a128 v, morder mo); 297 #endif 298 299 SANITIZER_INTERFACE_ATTRIBUTE 300 a8 __tsan_atomic8_fetch_or(volatile a8 *a, a8 v, morder mo); 301 SANITIZER_INTERFACE_ATTRIBUTE 302 a16 __tsan_atomic16_fetch_or(volatile a16 *a, a16 v, morder mo); 303 SANITIZER_INTERFACE_ATTRIBUTE 304 a32 __tsan_atomic32_fetch_or(volatile a32 *a, a32 v, morder mo); 305 SANITIZER_INTERFACE_ATTRIBUTE 306 a64 __tsan_atomic64_fetch_or(volatile a64 *a, a64 v, morder mo); 307 #if __TSAN_HAS_INT128 308 SANITIZER_INTERFACE_ATTRIBUTE 309 a128 __tsan_atomic128_fetch_or(volatile a128 *a, a128 v, morder mo); 310 #endif 311 312 SANITIZER_INTERFACE_ATTRIBUTE 313 a8 __tsan_atomic8_fetch_xor(volatile a8 *a, a8 v, morder mo); 314 SANITIZER_INTERFACE_ATTRIBUTE 315 a16 __tsan_atomic16_fetch_xor(volatile a16 *a, a16 v, morder mo); 316 SANITIZER_INTERFACE_ATTRIBUTE 317 a32 __tsan_atomic32_fetch_xor(volatile a32 *a, a32 v, morder mo); 318 SANITIZER_INTERFACE_ATTRIBUTE 319 a64 __tsan_atomic64_fetch_xor(volatile a64 *a, a64 v, morder mo); 320 #if __TSAN_HAS_INT128 321 SANITIZER_INTERFACE_ATTRIBUTE 322 a128 __tsan_atomic128_fetch_xor(volatile a128 *a, a128 v, morder mo); 323 #endif 324 325 SANITIZER_INTERFACE_ATTRIBUTE 326 a8 __tsan_atomic8_fetch_nand(volatile a8 *a, a8 v, morder mo); 327 SANITIZER_INTERFACE_ATTRIBUTE 328 a16 __tsan_atomic16_fetch_nand(volatile a16 *a, a16 v, morder mo); 329 SANITIZER_INTERFACE_ATTRIBUTE 330 a32 __tsan_atomic32_fetch_nand(volatile a32 *a, a32 v, morder mo); 331 SANITIZER_INTERFACE_ATTRIBUTE 332 a64 __tsan_atomic64_fetch_nand(volatile a64 *a, a64 v, morder mo); 333 #if __TSAN_HAS_INT128 334 SANITIZER_INTERFACE_ATTRIBUTE 335 a128 __tsan_atomic128_fetch_nand(volatile a128 *a, a128 v, morder mo); 336 #endif 337 338 SANITIZER_INTERFACE_ATTRIBUTE 339 int __tsan_atomic8_compare_exchange_strong(volatile a8 *a, a8 *c, a8 v, 340 morder mo, morder fmo); 341 SANITIZER_INTERFACE_ATTRIBUTE 342 int __tsan_atomic16_compare_exchange_strong(volatile a16 *a, a16 *c, a16 v, 343 morder mo, morder fmo); 344 SANITIZER_INTERFACE_ATTRIBUTE 345 int __tsan_atomic32_compare_exchange_strong(volatile a32 *a, a32 *c, a32 v, 346 morder mo, morder fmo); 347 SANITIZER_INTERFACE_ATTRIBUTE 348 int __tsan_atomic64_compare_exchange_strong(volatile a64 *a, a64 *c, a64 v, 349 morder mo, morder fmo); 350 #if __TSAN_HAS_INT128 351 SANITIZER_INTERFACE_ATTRIBUTE 352 int __tsan_atomic128_compare_exchange_strong(volatile a128 *a, a128 *c, a128 v, 353 morder mo, morder fmo); 354 #endif 355 356 SANITIZER_INTERFACE_ATTRIBUTE 357 int __tsan_atomic8_compare_exchange_weak(volatile a8 *a, a8 *c, a8 v, morder mo, 358 morder fmo); 359 SANITIZER_INTERFACE_ATTRIBUTE 360 int __tsan_atomic16_compare_exchange_weak(volatile a16 *a, a16 *c, a16 v, 361 morder mo, morder fmo); 362 SANITIZER_INTERFACE_ATTRIBUTE 363 int __tsan_atomic32_compare_exchange_weak(volatile a32 *a, a32 *c, a32 v, 364 morder mo, morder fmo); 365 SANITIZER_INTERFACE_ATTRIBUTE 366 int __tsan_atomic64_compare_exchange_weak(volatile a64 *a, a64 *c, a64 v, 367 morder mo, morder fmo); 368 #if __TSAN_HAS_INT128 369 SANITIZER_INTERFACE_ATTRIBUTE 370 int __tsan_atomic128_compare_exchange_weak(volatile a128 *a, a128 *c, a128 v, 371 morder mo, morder fmo); 372 #endif 373 374 SANITIZER_INTERFACE_ATTRIBUTE 375 a8 __tsan_atomic8_compare_exchange_val(volatile a8 *a, a8 c, a8 v, morder mo, 376 morder fmo); 377 SANITIZER_INTERFACE_ATTRIBUTE 378 a16 __tsan_atomic16_compare_exchange_val(volatile a16 *a, a16 c, a16 v, 379 morder mo, morder fmo); 380 SANITIZER_INTERFACE_ATTRIBUTE 381 a32 __tsan_atomic32_compare_exchange_val(volatile a32 *a, a32 c, a32 v, 382 morder mo, morder fmo); 383 SANITIZER_INTERFACE_ATTRIBUTE 384 a64 __tsan_atomic64_compare_exchange_val(volatile a64 *a, a64 c, a64 v, 385 morder mo, morder fmo); 386 #if __TSAN_HAS_INT128 387 SANITIZER_INTERFACE_ATTRIBUTE 388 a128 __tsan_atomic128_compare_exchange_val(volatile a128 *a, a128 c, a128 v, 389 morder mo, morder fmo); 390 #endif 391 392 SANITIZER_INTERFACE_ATTRIBUTE 393 void __tsan_atomic_thread_fence(morder mo); 394 SANITIZER_INTERFACE_ATTRIBUTE 395 void __tsan_atomic_signal_fence(morder mo); 396 397 SANITIZER_INTERFACE_ATTRIBUTE 398 void __tsan_go_atomic32_load(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 399 SANITIZER_INTERFACE_ATTRIBUTE 400 void __tsan_go_atomic64_load(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 401 SANITIZER_INTERFACE_ATTRIBUTE 402 void __tsan_go_atomic32_store(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 403 SANITIZER_INTERFACE_ATTRIBUTE 404 void __tsan_go_atomic64_store(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 405 SANITIZER_INTERFACE_ATTRIBUTE 406 void __tsan_go_atomic32_fetch_add(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 407 SANITIZER_INTERFACE_ATTRIBUTE 408 void __tsan_go_atomic64_fetch_add(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 409 SANITIZER_INTERFACE_ATTRIBUTE 410 void __tsan_go_atomic32_exchange(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 411 SANITIZER_INTERFACE_ATTRIBUTE 412 void __tsan_go_atomic64_exchange(ThreadState *thr, uptr cpc, uptr pc, u8 *a); 413 SANITIZER_INTERFACE_ATTRIBUTE 414 void __tsan_go_atomic32_compare_exchange(ThreadState *thr, uptr cpc, uptr pc, 415 u8 *a); 416 SANITIZER_INTERFACE_ATTRIBUTE 417 void __tsan_go_atomic64_compare_exchange(ThreadState *thr, uptr cpc, uptr pc, 418 u8 *a); 419 420 } // extern "C" 421 422 } // namespace __tsan 423 424 #endif // TSAN_INTERFACE_H 425