Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Analysis/Analyses/ThreadSafety.h
Line
Count
Source (jump to first uncovered line)
1
//===- ThreadSafety.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
//
10
// A intra-procedural analysis for thread safety (e.g. deadlocks and race
11
// conditions), based off of an annotation system.
12
//
13
// See http://clang.llvm.org/docs/LanguageExtensions.html#thread-safety-annotation-checking
14
// for more information.
15
//
16
//===----------------------------------------------------------------------===//
17
18
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
19
#define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
20
21
#include "clang/Basic/SourceLocation.h"
22
#include "llvm/ADT/StringRef.h"
23
24
namespace clang {
25
26
class AnalysisDeclContext;
27
class FunctionDecl;
28
class NamedDecl;
29
30
namespace threadSafety {
31
32
class BeforeSet;
33
34
/// This enum distinguishes between different kinds of operations that may
35
/// need to be protected by locks. We use this enum in error handling.
36
enum ProtectedOperationKind {
37
  /// Dereferencing a variable (e.g. p in *p = 5;)
38
  POK_VarDereference,
39
40
  /// Reading or writing a variable (e.g. x in x = 5;)
41
  POK_VarAccess,
42
43
  /// Making a function call (e.g. fool())
44
  POK_FunctionCall,
45
46
  /// Passing a guarded variable by reference.
47
  POK_PassByRef,
48
49
  /// Passing a pt-guarded variable by reference.
50
  POK_PtPassByRef
51
};
52
53
/// This enum distinguishes between different kinds of lock actions. For
54
/// example, it is an error to write a variable protected by shared version of a
55
/// mutex.
56
enum LockKind {
57
  /// Shared/reader lock of a mutex.
58
  LK_Shared,
59
60
  /// Exclusive/writer lock of a mutex.
61
  LK_Exclusive,
62
63
  /// Can be either Shared or Exclusive.
64
  LK_Generic
65
};
66
67
/// This enum distinguishes between different ways to access (read or write) a
68
/// variable.
69
enum AccessKind {
70
  /// Reading a variable.
71
  AK_Read,
72
73
  /// Writing a variable.
74
  AK_Written
75
};
76
77
/// This enum distinguishes between different situations where we warn due to
78
/// inconsistent locking.
79
/// \enum SK_LockedSomeLoopIterations -- a mutex is locked for some but not all
80
/// loop iterations.
81
/// \enum SK_LockedSomePredecessors -- a mutex is locked in some but not all
82
/// predecessors of a CFGBlock.
83
/// \enum SK_LockedAtEndOfFunction -- a mutex is still locked at the end of a
84
/// function.
85
enum LockErrorKind {
86
  LEK_LockedSomeLoopIterations,
87
  LEK_LockedSomePredecessors,
88
  LEK_LockedAtEndOfFunction,
89
  LEK_NotLockedAtEndOfFunction
90
};
91
92
/// Handler class for thread safety warnings.
93
class ThreadSafetyHandler {
94
public:
95
  using Name = StringRef;
96
97
2.13k
  ThreadSafetyHandler() = default;
98
  virtual ~ThreadSafetyHandler();
99
100
  /// Warn about lock expressions which fail to resolve to lockable objects.
101
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
102
  /// \param Loc -- the SourceLocation of the unresolved expression.
103
0
  virtual void handleInvalidLockExp(StringRef Kind, SourceLocation Loc) {}
104
105
  /// Warn about unlock function calls that do not have a prior matching lock
106
  /// expression.
107
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
108
  /// \param LockName -- A StringRef name for the lock expression, to be printed
109
  /// in the error message.
110
  /// \param Loc -- The SourceLocation of the Unlock
111
  virtual void handleUnmatchedUnlock(StringRef Kind, Name LockName,
112
0
                                     SourceLocation Loc) {}
113
114
  /// Warn about an unlock function call that attempts to unlock a lock with
115
  /// the incorrect lock kind. For instance, a shared lock being unlocked
116
  /// exclusively, or vice versa.
117
  /// \param LockName -- A StringRef name for the lock expression, to be printed
118
  /// in the error message.
119
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
120
  /// \param Expected -- the kind of lock expected.
121
  /// \param Received -- the kind of lock received.
122
  /// \param Loc -- The SourceLocation of the Unlock.
123
  virtual void handleIncorrectUnlockKind(StringRef Kind, Name LockName,
124
                                         LockKind Expected, LockKind Received,
125
0
                                         SourceLocation Loc) {}
126
127
  /// Warn about lock function calls for locks which are already held.
128
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
129
  /// \param LockName -- A StringRef name for the lock expression, to be printed
130
  /// in the error message.
131
  /// \param LocLocked -- The location of the first lock expression.
132
  /// \param Loc -- The location of the second lock expression.
133
  virtual void handleDoubleLock(StringRef Kind, Name LockName,
134
0
                                SourceLocation LocLocked, SourceLocation Loc) {}
135
136
  /// Warn about situations where a mutex is sometimes held and sometimes not.
137
  /// The three situations are:
138
  /// 1. a mutex is locked on an "if" branch but not the "else" branch,
139
  /// 2, or a mutex is only held at the start of some loop iterations,
140
  /// 3. or when a mutex is locked but not unlocked inside a function.
141
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
142
  /// \param LockName -- A StringRef name for the lock expression, to be printed
143
  /// in the error message.
144
  /// \param LocLocked -- The location of the lock expression where the mutex is
145
  ///               locked
146
  /// \param LocEndOfScope -- The location of the end of the scope where the
147
  ///               mutex is no longer held
148
  /// \param LEK -- which of the three above cases we should warn for
149
  virtual void handleMutexHeldEndOfScope(StringRef Kind, Name LockName,
150
                                         SourceLocation LocLocked,
151
                                         SourceLocation LocEndOfScope,
152
0
                                         LockErrorKind LEK) {}
153
154
  /// Warn when a mutex is held exclusively and shared at the same point. For
155
  /// example, if a mutex is locked exclusively during an if branch and shared
156
  /// during the else branch.
157
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
158
  /// \param LockName -- A StringRef name for the lock expression, to be printed
159
  /// in the error message.
160
  /// \param Loc1 -- The location of the first lock expression.
161
  /// \param Loc2 -- The location of the second lock expression.
162
  virtual void handleExclusiveAndShared(StringRef Kind, Name LockName,
163
                                        SourceLocation Loc1,
164
0
                                        SourceLocation Loc2) {}
165
166
  /// Warn when a protected operation occurs while no locks are held.
167
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
168
  /// \param D -- The decl for the protected variable or function
169
  /// \param POK -- The kind of protected operation (e.g. variable access)
170
  /// \param AK -- The kind of access (i.e. read or write) that occurred
171
  /// \param Loc -- The location of the protected operation.
172
  virtual void handleNoMutexHeld(StringRef Kind, const NamedDecl *D,
173
                                 ProtectedOperationKind POK, AccessKind AK,
174
0
                                 SourceLocation Loc) {}
175
176
  /// Warn when a protected operation occurs while the specific mutex protecting
177
  /// the operation is not locked.
178
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
179
  /// \param D -- The decl for the protected variable or function
180
  /// \param POK -- The kind of protected operation (e.g. variable access)
181
  /// \param LockName -- A StringRef name for the lock expression, to be printed
182
  /// in the error message.
183
  /// \param LK -- The kind of access (i.e. read or write) that occurred
184
  /// \param Loc -- The location of the protected operation.
185
  virtual void handleMutexNotHeld(StringRef Kind, const NamedDecl *D,
186
                                  ProtectedOperationKind POK, Name LockName,
187
                                  LockKind LK, SourceLocation Loc,
188
0
                                  Name *PossibleMatch = nullptr) {}
189
190
  /// Warn when acquiring a lock that the negative capability is not held.
191
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
192
  /// \param LockName -- The name for the lock expression, to be printed in the
193
  /// diagnostic.
194
  /// \param Neg -- The name of the negative capability to be printed in the
195
  /// diagnostic.
196
  /// \param Loc -- The location of the protected operation.
197
  virtual void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg,
198
0
                                     SourceLocation Loc) {}
199
200
  /// Warn when a function is called while an excluded mutex is locked. For
201
  /// example, the mutex may be locked inside the function.
202
  /// \param Kind -- the capability's name parameter (role, mutex, etc).
203
  /// \param FunName -- The name of the function
204
  /// \param LockName -- A StringRef name for the lock expression, to be printed
205
  /// in the error message.
206
  /// \param Loc -- The location of the function call.
207
  virtual void handleFunExcludesLock(StringRef Kind, Name FunName,
208
0
                                     Name LockName, SourceLocation Loc) {}
209
210
  /// Warn that L1 cannot be acquired before L2.
211
  virtual void handleLockAcquiredBefore(StringRef Kind, Name L1Name,
212
0
                                        Name L2Name, SourceLocation Loc) {}
213
214
  /// Warn that there is a cycle in acquired_before/after dependencies.
215
0
  virtual void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) {}
216
217
  /// Called by the analysis when starting analysis of a function.
218
  /// Used to issue suggestions for changes to annotations.
219
0
  virtual void enterFunction(const FunctionDecl *FD) {}
220
221
  /// Called by the analysis when finishing analysis of a function.
222
0
  virtual void leaveFunction(const FunctionDecl *FD) {}
223
224
2.63k
  bool issueBetaWarnings() { return IssueBetaWarnings; }
225
2.06k
  void setIssueBetaWarnings(bool b) { IssueBetaWarnings = b; }
226
227
private:
228
  bool IssueBetaWarnings = false;
229
};
230
231
/// Check a function's CFG for thread-safety violations.
232
///
233
/// We traverse the blocks in the CFG, compute the set of mutexes that are held
234
/// at the end of each block, and issue warnings for thread safety violations.
235
/// Each block in the CFG is traversed exactly once.
236
void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
237
                             ThreadSafetyHandler &Handler,
238
                             BeforeSet **Bset);
239
240
void threadSafetyCleanup(BeforeSet *Cache);
241
242
/// Helper function that returns a LockKind required for the given level
243
/// of access.
244
LockKind getLockKindFromAccessKind(AccessKind AK);
245
246
} // namespace threadSafety
247
} // namespace clang
248
249
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H