Path: blob/main/contrib/llvm-project/compiler-rt/include/sanitizer/tsan_interface.h
35235 views
//===-- tsan_interface.h ----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//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_H13#define SANITIZER_TSAN_INTERFACE_H1415#include <sanitizer/common_interface_defs.h>1617#ifdef __cplusplus18extern "C" {19#endif2021// __tsan_release establishes a happens-before relation with a preceding22// __tsan_acquire on the same address.23void SANITIZER_CDECL __tsan_acquire(void *addr);24void SANITIZER_CDECL __tsan_release(void *addr);2526// 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 and29// destruction and potential deadlocks) and improve precision and performance30// (by ignoring individual atomic operations in mutex code). However, the31// downside is that annotated mutex code itself is not checked for correctness.3233// 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_lock36// annotations.3738// Mutex has static storage duration and no-op constructor and destructor.39// This effectively makes tsan ignore destroy annotation.40static const unsigned __tsan_mutex_linker_init = 1 << 0;41// Mutex is write reentrant.42static const unsigned __tsan_mutex_write_reentrant = 1 << 1;43// Mutex is read reentrant.44static const unsigned __tsan_mutex_read_reentrant = 1 << 2;45// Mutex does not have static storage duration, and must not be used after46// its destructor runs. The opposite of __tsan_mutex_linker_init.47// If this flag is passed to __tsan_mutex_destroy, then the destruction48// is ignored unless this flag was previously set on the mutex.49static const unsigned __tsan_mutex_not_static = 1 << 8;5051// Mutex operation flags:5253// Denotes read lock operation.54static const unsigned __tsan_mutex_read_lock = 1 << 3;55// Denotes try lock operation.56static const unsigned __tsan_mutex_try_lock = 1 << 4;57// Denotes that a try lock operation has failed to acquire the mutex.58static 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.63static 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 to66// the corresponding __tsan_mutex_post_lock annotation.67static const unsigned __tsan_mutex_recursive_unlock = 1 << 7;6869// Convenient composed constants.70static const unsigned __tsan_mutex_try_read_lock =71__tsan_mutex_read_lock | __tsan_mutex_try_lock;72static const unsigned __tsan_mutex_try_read_lock_failed =73__tsan_mutex_try_read_lock | __tsan_mutex_try_lock_failed;7475// Annotate creation of a mutex.76// Supported flags: mutex creation flags.77void SANITIZER_CDECL __tsan_mutex_create(void *addr, unsigned flags);7879// Annotate destruction of a mutex.80// Supported flags:81// - __tsan_mutex_linker_init82// - __tsan_mutex_not_static83void SANITIZER_CDECL __tsan_mutex_destroy(void *addr, unsigned flags);8485// Annotate start of lock operation.86// Supported flags:87// - __tsan_mutex_read_lock88// - __tsan_mutex_try_lock89// - all mutex creation flags90void SANITIZER_CDECL __tsan_mutex_pre_lock(void *addr, unsigned flags);9192// Annotate end of lock operation.93// Supported flags:94// - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock)95// - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock)96// - __tsan_mutex_try_lock_failed97// - __tsan_mutex_recursive_lock98// - all mutex creation flags99void SANITIZER_CDECL __tsan_mutex_post_lock(void *addr, unsigned flags,100int recursion);101102// Annotate start of unlock operation.103// Supported flags:104// - __tsan_mutex_read_lock105// - __tsan_mutex_recursive_unlock106int SANITIZER_CDECL __tsan_mutex_pre_unlock(void *addr, unsigned flags);107108// Annotate end of unlock operation.109// Supported flags:110// - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)111void SANITIZER_CDECL __tsan_mutex_post_unlock(void *addr, unsigned flags);112113// Annotate start/end of notify/signal/broadcast operation.114// Supported flags: none.115void SANITIZER_CDECL __tsan_mutex_pre_signal(void *addr, unsigned flags);116void SANITIZER_CDECL __tsan_mutex_post_signal(void *addr, unsigned flags);117118// Annotate start/end of a region of code where lock/unlock/signal operation119// diverts to do something else unrelated to the mutex. This can be used to120// annotate, for example, calls into cooperative scheduler or contention121// profiling code.122// These annotations must be called only from within123// __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,124// __tsan_mutex_pre/post_signal regions.125// Supported flags: none.126void SANITIZER_CDECL __tsan_mutex_pre_divert(void *addr, unsigned flags);127void SANITIZER_CDECL __tsan_mutex_post_divert(void *addr, unsigned flags);128129// Check that the current thread does not hold any mutexes,130// report a bug report otherwise.131void SANITIZER_CDECL __tsan_check_no_mutexes_held();132133// External race detection API.134// Can be used by non-instrumented libraries to detect when their objects are135// being used in an unsafe manner.136// - __tsan_external_read/__tsan_external_write annotates the logical reads137// and writes of the object at the specified address. 'caller_pc' should138// be the PC of the library user, which the library can obtain with e.g.139// `__builtin_return_address(0)`.140// - __tsan_external_register_tag registers a 'tag' with the specified name,141// which is later used in read/write annotations to denote the object type142// - __tsan_external_assign_tag can optionally mark a heap object with a tag143void *SANITIZER_CDECL __tsan_external_register_tag(const char *object_type);144void SANITIZER_CDECL __tsan_external_register_header(void *tag,145const char *header);146void SANITIZER_CDECL __tsan_external_assign_tag(void *addr, void *tag);147void SANITIZER_CDECL __tsan_external_read(void *addr, void *caller_pc,148void *tag);149void SANITIZER_CDECL __tsan_external_write(void *addr, void *caller_pc,150void *tag);151152// Fiber switching API.153// - TSAN context for fiber can be created by __tsan_create_fiber154// and freed by __tsan_destroy_fiber.155// - TSAN context of current fiber or thread can be obtained156// by calling __tsan_get_current_fiber.157// - __tsan_switch_to_fiber should be called immediately before switch158// to fiber, such as call of swapcontext.159// - Fiber name can be set by __tsan_set_fiber_name.160void *SANITIZER_CDECL __tsan_get_current_fiber(void);161void *SANITIZER_CDECL __tsan_create_fiber(unsigned flags);162void SANITIZER_CDECL __tsan_destroy_fiber(void *fiber);163void SANITIZER_CDECL __tsan_switch_to_fiber(void *fiber, unsigned flags);164void SANITIZER_CDECL __tsan_set_fiber_name(void *fiber, const char *name);165166// Flags for __tsan_switch_to_fiber:167// Do not establish a happens-before relation between fibers168static const unsigned __tsan_switch_to_fiber_no_sync = 1 << 0;169170// User-provided callback invoked on TSan initialization.171void SANITIZER_CDECL __tsan_on_initialize();172173// User-provided callback invoked on TSan shutdown.174// `failed` - Nonzero if TSan did detect issues, zero otherwise.175// Return `0` if TSan should exit as if no issues were detected. Return nonzero176// if TSan should exit as if issues were detected.177int SANITIZER_CDECL __tsan_on_finalize(int failed);178179// Release TSan internal memory in a best-effort manner.180void SANITIZER_CDECL __tsan_flush_memory();181182// User-provided default TSAN options.183const char *SANITIZER_CDECL __tsan_default_options(void);184185// User-provided default TSAN suppressions.186const char *SANITIZER_CDECL __tsan_default_suppressions(void);187188/// Returns a report's description.189///190/// Returns a report's description (issue type), number of duplicate issues191/// found, counts of array data (stack traces, memory operations, locations,192/// mutexes, threads, unique thread IDs) and a stack trace of a <c>sleep()</c>193/// call (if one was involved in the issue).194///195/// \param report Opaque pointer to the current report.196/// \param[out] description Report type description.197/// \param[out] count Count of duplicate issues.198/// \param[out] stack_count Count of stack traces.199/// \param[out] mop_count Count of memory operations.200/// \param[out] loc_count Count of locations.201/// \param[out] mutex_count Count of mutexes.202/// \param[out] thread_count Count of threads.203/// \param[out] unique_tid_count Count of unique thread IDs.204/// \param sleep_trace A buffer to store the stack trace of a <c>sleep()</c>205/// call.206/// \param trace_size Size in bytes of the trace buffer.207/// \returns Returns 1 if successful, 0 if not.208int SANITIZER_CDECL __tsan_get_report_data(209void *report, const char **description, int *count, int *stack_count,210int *mop_count, int *loc_count, int *mutex_count, int *thread_count,211int *unique_tid_count, void **sleep_trace, unsigned long trace_size);212213/// Returns information about stack traces included in the report.214///215/// \param report Opaque pointer to the current report.216/// \param idx Index to the report's stacks.217/// \param trace A buffer to store the stack trace.218/// \param trace_size Size in bytes of the trace buffer.219/// \returns Returns 1 if successful, 0 if not.220int SANITIZER_CDECL __tsan_get_report_stack(void *report, unsigned long idx,221void **trace,222unsigned long trace_size);223224/// Returns information about memory operations included in the report.225///226/// \param report Opaque pointer to the current report.227/// \param idx Index to the report's memory operations.228/// \param[out] tid Thread ID of the memory operation.229/// \param[out] addr Address of the memory operation.230/// \param[out] size Size of the memory operation.231/// \param[out] write Write flag of the memory operation.232/// \param[out] atomic Atomicity flag of the memory operation.233/// \param trace A buffer to store the stack trace.234/// \param trace_size Size in bytes of the trace buffer.235/// \returns Returns 1 if successful, 0 if not.236int SANITIZER_CDECL __tsan_get_report_mop(void *report, unsigned long idx,237int *tid, void **addr, int *size,238int *write, int *atomic, void **trace,239unsigned long trace_size);240241/// Returns information about locations included in the report.242///243/// \param report Opaque pointer to the current report.244/// \param idx Index to the report's locations.245/// \param[out] type Type of the location.246/// \param[out] addr Address of the location.247/// \param[out] start Start of the location.248/// \param[out] size Size of the location.249/// \param[out] tid Thread ID of the location.250/// \param[out] fd File descriptor of the location.251/// \param[out] suppressable Suppressable flag.252/// \param trace A buffer to store the stack trace.253/// \param trace_size Size in bytes of the trace buffer.254/// \returns Returns 1 if successful, 0 if not.255int SANITIZER_CDECL __tsan_get_report_loc(void *report, unsigned long idx,256const char **type, void **addr,257void **start, unsigned long *size,258int *tid, int *fd, int *suppressable,259void **trace,260unsigned long trace_size);261262/// Returns information about mutexes included in the report.263///264/// \param report Opaque pointer to the current report.265/// \param idx Index to the report's mutexes.266/// \param[out] mutex_id Id of the mutex.267/// \param[out] addr Address of the mutex.268/// \param[out] destroyed Destroyed mutex flag.269/// \param trace A buffer to store the stack trace.270/// \param trace_size Size in bytes of the trace buffer.271/// \returns Returns 1 if successful, 0 if not.272int SANITIZER_CDECL __tsan_get_report_mutex(void *report, unsigned long idx,273uint64_t *mutex_id, void **addr,274int *destroyed, void **trace,275unsigned long trace_size);276277/// Returns information about threads included in the report.278///279/// \param report Opaque pointer to the current report.280/// \param idx Index to the report's threads.281/// \param[out] tid Thread ID of the thread.282/// \param[out] os_id Operating system's ID of the thread.283/// \param[out] running Running flag of the thread.284/// \param[out] name Name of the thread.285/// \param[out] parent_tid ID of the parent thread.286/// \param trace A buffer to store the stack trace.287/// \param trace_size Size in bytes of the trace buffer.288/// \returns Returns 1 if successful, 0 if not.289int SANITIZER_CDECL __tsan_get_report_thread(void *report, unsigned long idx,290int *tid, uint64_t *os_id,291int *running, const char **name,292int *parent_tid, void **trace,293unsigned long trace_size);294295/// Returns information about unique thread IDs included in the report.296///297/// \param report Opaque pointer to the current report.298/// \param idx Index to the report's unique thread IDs.299/// \param[out] tid Unique thread ID of the report.300/// \returns Returns 1 if successful, 0 if not.301int SANITIZER_CDECL __tsan_get_report_unique_tid(void *report,302unsigned long idx, int *tid);303304/// Returns the current report.305///306/// If TSan is currently reporting a detected issue on the current thread,307/// returns an opaque pointer to the current report. Otherwise returns NULL.308/// \returns An opaque pointer to the current report. Otherwise returns NULL.309void *SANITIZER_CDECL __tsan_get_current_report();310311#ifdef __cplusplus312} // extern "C"313#endif314315#endif // SANITIZER_TSAN_INTERFACE_H316317318