10b57cec5SDimitry Andric //===-- tsan_interface.h ----------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // Public interface header for TSan. 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric #ifndef SANITIZER_TSAN_INTERFACE_H 140b57cec5SDimitry Andric #define SANITIZER_TSAN_INTERFACE_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include <sanitizer/common_interface_defs.h> 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #ifdef __cplusplus 190b57cec5SDimitry Andric extern "C" { 200b57cec5SDimitry Andric #endif 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric // __tsan_release establishes a happens-before relation with a preceding 230b57cec5SDimitry Andric // __tsan_acquire on the same address. 240b57cec5SDimitry Andric void __tsan_acquire(void *addr); 250b57cec5SDimitry Andric void __tsan_release(void *addr); 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // Annotations for custom mutexes. 280b57cec5SDimitry Andric // The annotations allow to get better reports (with sets of locked mutexes), 290b57cec5SDimitry Andric // detect more types of bugs (e.g. mutex misuses, races between lock/unlock and 300b57cec5SDimitry Andric // destruction and potential deadlocks) and improve precision and performance 310b57cec5SDimitry Andric // (by ignoring individual atomic operations in mutex code). However, the 320b57cec5SDimitry Andric // downside is that annotated mutex code itself is not checked for correctness. 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric // Mutex creation flags are passed to __tsan_mutex_create annotation. 350b57cec5SDimitry Andric // If mutex has no constructor and __tsan_mutex_create is not called, 360b57cec5SDimitry Andric // the flags may be passed to __tsan_mutex_pre_lock/__tsan_mutex_post_lock 370b57cec5SDimitry Andric // annotations. 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric // Mutex has static storage duration and no-op constructor and destructor. 400b57cec5SDimitry Andric // This effectively makes tsan ignore destroy annotation. 41*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_linker_init = 1 << 0; 420b57cec5SDimitry Andric // Mutex is write reentrant. 43*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_write_reentrant = 1 << 1; 440b57cec5SDimitry Andric // Mutex is read reentrant. 45*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_read_reentrant = 1 << 2; 460b57cec5SDimitry Andric // Mutex does not have static storage duration, and must not be used after 470b57cec5SDimitry Andric // its destructor runs. The opposite of __tsan_mutex_linker_init. 480b57cec5SDimitry Andric // If this flag is passed to __tsan_mutex_destroy, then the destruction 490b57cec5SDimitry Andric // is ignored unless this flag was previously set on the mutex. 50*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_not_static = 1 << 8; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric // Mutex operation flags: 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // Denotes read lock operation. 55*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_read_lock = 1 << 3; 560b57cec5SDimitry Andric // Denotes try lock operation. 57*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_try_lock = 1 << 4; 580b57cec5SDimitry Andric // Denotes that a try lock operation has failed to acquire the mutex. 59*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_try_lock_failed = 1 << 5; 600b57cec5SDimitry Andric // Denotes that the lock operation acquires multiple recursion levels. 610b57cec5SDimitry Andric // Number of levels is passed in recursion parameter. 620b57cec5SDimitry Andric // This is useful for annotation of e.g. Java builtin monitors, 630b57cec5SDimitry Andric // for which wait operation releases all recursive acquisitions of the mutex. 64*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_recursive_lock = 1 << 6; 650b57cec5SDimitry Andric // Denotes that the unlock operation releases all recursion levels. 660b57cec5SDimitry Andric // Number of released levels is returned and later must be passed to 670b57cec5SDimitry Andric // the corresponding __tsan_mutex_post_lock annotation. 68*5ffd83dbSDimitry Andric static const unsigned __tsan_mutex_recursive_unlock = 1 << 7; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Annotate creation of a mutex. 710b57cec5SDimitry Andric // Supported flags: mutex creation flags. 720b57cec5SDimitry Andric void __tsan_mutex_create(void *addr, unsigned flags); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // Annotate destruction of a mutex. 750b57cec5SDimitry Andric // Supported flags: 760b57cec5SDimitry Andric // - __tsan_mutex_linker_init 770b57cec5SDimitry Andric // - __tsan_mutex_not_static 780b57cec5SDimitry Andric void __tsan_mutex_destroy(void *addr, unsigned flags); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Annotate start of lock operation. 810b57cec5SDimitry Andric // Supported flags: 820b57cec5SDimitry Andric // - __tsan_mutex_read_lock 830b57cec5SDimitry Andric // - __tsan_mutex_try_lock 840b57cec5SDimitry Andric // - all mutex creation flags 850b57cec5SDimitry Andric void __tsan_mutex_pre_lock(void *addr, unsigned flags); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric // Annotate end of lock operation. 880b57cec5SDimitry Andric // Supported flags: 890b57cec5SDimitry Andric // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock) 900b57cec5SDimitry Andric // - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock) 910b57cec5SDimitry Andric // - __tsan_mutex_try_lock_failed 920b57cec5SDimitry Andric // - __tsan_mutex_recursive_lock 930b57cec5SDimitry Andric // - all mutex creation flags 940b57cec5SDimitry Andric void __tsan_mutex_post_lock(void *addr, unsigned flags, int recursion); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // Annotate start of unlock operation. 970b57cec5SDimitry Andric // Supported flags: 980b57cec5SDimitry Andric // - __tsan_mutex_read_lock 990b57cec5SDimitry Andric // - __tsan_mutex_recursive_unlock 1000b57cec5SDimitry Andric int __tsan_mutex_pre_unlock(void *addr, unsigned flags); 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Annotate end of unlock operation. 1030b57cec5SDimitry Andric // Supported flags: 1040b57cec5SDimitry Andric // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock) 1050b57cec5SDimitry Andric void __tsan_mutex_post_unlock(void *addr, unsigned flags); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Annotate start/end of notify/signal/broadcast operation. 1080b57cec5SDimitry Andric // Supported flags: none. 1090b57cec5SDimitry Andric void __tsan_mutex_pre_signal(void *addr, unsigned flags); 1100b57cec5SDimitry Andric void __tsan_mutex_post_signal(void *addr, unsigned flags); 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // Annotate start/end of a region of code where lock/unlock/signal operation 1130b57cec5SDimitry Andric // diverts to do something else unrelated to the mutex. This can be used to 1140b57cec5SDimitry Andric // annotate, for example, calls into cooperative scheduler or contention 1150b57cec5SDimitry Andric // profiling code. 1160b57cec5SDimitry Andric // These annotations must be called only from within 1170b57cec5SDimitry Andric // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock, 1180b57cec5SDimitry Andric // __tsan_mutex_pre/post_signal regions. 1190b57cec5SDimitry Andric // Supported flags: none. 1200b57cec5SDimitry Andric void __tsan_mutex_pre_divert(void *addr, unsigned flags); 1210b57cec5SDimitry Andric void __tsan_mutex_post_divert(void *addr, unsigned flags); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // External race detection API. 1240b57cec5SDimitry Andric // Can be used by non-instrumented libraries to detect when their objects are 1250b57cec5SDimitry Andric // being used in an unsafe manner. 1260b57cec5SDimitry Andric // - __tsan_external_read/__tsan_external_write annotates the logical reads 1270b57cec5SDimitry Andric // and writes of the object at the specified address. 'caller_pc' should 1280b57cec5SDimitry Andric // be the PC of the library user, which the library can obtain with e.g. 1290b57cec5SDimitry Andric // `__builtin_return_address(0)`. 1300b57cec5SDimitry Andric // - __tsan_external_register_tag registers a 'tag' with the specified name, 1310b57cec5SDimitry Andric // which is later used in read/write annotations to denote the object type 1320b57cec5SDimitry Andric // - __tsan_external_assign_tag can optionally mark a heap object with a tag 1330b57cec5SDimitry Andric void *__tsan_external_register_tag(const char *object_type); 1340b57cec5SDimitry Andric void __tsan_external_register_header(void *tag, const char *header); 1350b57cec5SDimitry Andric void __tsan_external_assign_tag(void *addr, void *tag); 1360b57cec5SDimitry Andric void __tsan_external_read(void *addr, void *caller_pc, void *tag); 1370b57cec5SDimitry Andric void __tsan_external_write(void *addr, void *caller_pc, void *tag); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric // Fiber switching API. 1400b57cec5SDimitry Andric // - TSAN context for fiber can be created by __tsan_create_fiber 1410b57cec5SDimitry Andric // and freed by __tsan_destroy_fiber. 1420b57cec5SDimitry Andric // - TSAN context of current fiber or thread can be obtained 1430b57cec5SDimitry Andric // by calling __tsan_get_current_fiber. 1440b57cec5SDimitry Andric // - __tsan_switch_to_fiber should be called immediatly before switch 1450b57cec5SDimitry Andric // to fiber, such as call of swapcontext. 1460b57cec5SDimitry Andric // - Fiber name can be set by __tsan_set_fiber_name. 1470b57cec5SDimitry Andric void *__tsan_get_current_fiber(void); 1480b57cec5SDimitry Andric void *__tsan_create_fiber(unsigned flags); 1490b57cec5SDimitry Andric void __tsan_destroy_fiber(void *fiber); 1500b57cec5SDimitry Andric void __tsan_switch_to_fiber(void *fiber, unsigned flags); 1510b57cec5SDimitry Andric void __tsan_set_fiber_name(void *fiber, const char *name); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric // Flags for __tsan_switch_to_fiber: 1540b57cec5SDimitry Andric // Do not establish a happens-before relation between fibers 155*5ffd83dbSDimitry Andric static const unsigned __tsan_switch_to_fiber_no_sync = 1 << 0; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric #ifdef __cplusplus 1580b57cec5SDimitry Andric } // extern "C" 1590b57cec5SDimitry Andric #endif 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric #endif // SANITIZER_TSAN_INTERFACE_H 162