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