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