blob: 005d45545f1c058c776e4af9b5360f4e08d2cad4 [file] [log] [blame]
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001//===- ThreadSafety.cpp ----------------------------------------*- 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// A intra-procedural analysis for thread safety (e.g. deadlocks and race
11// conditions), based off of an annotation system.
12//
Caitlin Sadowski19903462011-09-14 20:05:09 +000013// See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more
14// information.
Caitlin Sadowski402aa062011-09-09 16:11:56 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Analysis/Analyses/ThreadSafety.h"
Ted Kremenek439ed162011-10-22 02:14:27 +000019#include "clang/Analysis/Analyses/PostOrderCFGView.h"
Caitlin Sadowskid5b16052011-09-09 23:00:59 +000020#include "clang/Analysis/AnalysisContext.h"
21#include "clang/Analysis/CFG.h"
22#include "clang/Analysis/CFGStmtMap.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000023#include "clang/AST/DeclCXX.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/StmtCXX.h"
26#include "clang/AST/StmtVisitor.h"
Caitlin Sadowskid5b16052011-09-09 23:00:59 +000027#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/SourceLocation.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000029#include "llvm/ADT/BitVector.h"
30#include "llvm/ADT/FoldingSet.h"
31#include "llvm/ADT/ImmutableMap.h"
32#include "llvm/ADT/PostOrderIterator.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/StringRef.h"
35#include <algorithm>
36#include <vector>
37
38using namespace clang;
39using namespace thread_safety;
40
Caitlin Sadowski19903462011-09-14 20:05:09 +000041// Key method definition
42ThreadSafetyHandler::~ThreadSafetyHandler() {}
43
Caitlin Sadowski402aa062011-09-09 16:11:56 +000044namespace {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +000045
Caitlin Sadowski402aa062011-09-09 16:11:56 +000046/// \brief A MutexID object uniquely identifies a particular mutex, and
47/// is built from an Expr* (i.e. calling a lock function).
48///
49/// Thread-safety analysis works by comparing lock expressions. Within the
50/// body of a function, an expression such as "x->foo->bar.mu" will resolve to
51/// a particular mutex object at run-time. Subsequent occurrences of the same
52/// expression (where "same" means syntactic equality) will refer to the same
53/// run-time object if three conditions hold:
54/// (1) Local variables in the expression, such as "x" have not changed.
55/// (2) Values on the heap that affect the expression have not changed.
56/// (3) The expression involves only pure function calls.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +000057///
Caitlin Sadowski402aa062011-09-09 16:11:56 +000058/// The current implementation assumes, but does not verify, that multiple uses
59/// of the same lock expression satisfies these criteria.
60///
61/// Clang introduces an additional wrinkle, which is that it is difficult to
62/// derive canonical expressions, or compare expressions directly for equality.
63/// Thus, we identify a mutex not by an Expr, but by the set of named
64/// declarations that are referenced by the Expr. In other words,
65/// x->foo->bar.mu will be a four element vector with the Decls for
66/// mu, bar, and foo, and x. The vector will uniquely identify the expression
67/// for all practical purposes.
68///
69/// Note we will need to perform substitution on "this" and function parameter
70/// names when constructing a lock expression.
71///
72/// For example:
73/// class C { Mutex Mu; void lock() EXCLUSIVE_LOCK_FUNCTION(this->Mu); };
74/// void myFunc(C *X) { ... X->lock() ... }
75/// The original expression for the mutex acquired by myFunc is "this->Mu", but
76/// "X" is substituted for "this" so we get X->Mu();
77///
78/// For another example:
79/// foo(MyList *L) EXCLUSIVE_LOCKS_REQUIRED(L->Mu) { ... }
80/// MyList *MyL;
81/// foo(MyL); // requires lock MyL->Mu to be held
82class MutexID {
83 SmallVector<NamedDecl*, 2> DeclSeq;
84
85 /// Build a Decl sequence representing the lock from the given expression.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +000086 /// Recursive function that terminates on DeclRefExpr.
87 /// Note: this function merely creates a MutexID; it does not check to
88 /// ensure that the original expression is a valid mutex expression.
DeLesley Hutchins81216392011-10-17 21:38:02 +000089 void buildMutexID(Expr *Exp, Expr *Parent, int NumArgs,
90 const NamedDecl **FunArgDecls, Expr **FunArgs) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +000091 if (!Exp) {
92 DeclSeq.clear();
93 return;
94 }
95
Caitlin Sadowski402aa062011-09-09 16:11:56 +000096 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
DeLesley Hutchins81216392011-10-17 21:38:02 +000097 if (FunArgDecls) {
98 // Substitute call arguments for references to function parameters
99 for (int i = 0; i < NumArgs; ++i) {
100 if (DRE->getDecl() == FunArgDecls[i]) {
101 buildMutexID(FunArgs[i], 0, 0, 0, 0);
102 return;
103 }
104 }
105 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000106 NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
107 DeclSeq.push_back(ND);
108 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
109 NamedDecl *ND = ME->getMemberDecl();
110 DeclSeq.push_back(ND);
DeLesley Hutchins81216392011-10-17 21:38:02 +0000111 buildMutexID(ME->getBase(), Parent, NumArgs, FunArgDecls, FunArgs);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000112 } else if (isa<CXXThisExpr>(Exp)) {
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000113 if (Parent)
DeLesley Hutchins81216392011-10-17 21:38:02 +0000114 buildMutexID(Parent, 0, 0, 0, 0);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000115 else
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000116 return; // mutexID is still valid in this case
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000117 } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp))
118 buildMutexID(UOE->getSubExpr(), Parent, NumArgs, FunArgDecls, FunArgs);
119 else if (CastExpr *CE = dyn_cast<CastExpr>(Exp))
DeLesley Hutchins81216392011-10-17 21:38:02 +0000120 buildMutexID(CE->getSubExpr(), Parent, NumArgs, FunArgDecls, FunArgs);
Caitlin Sadowski99107eb2011-09-09 16:21:55 +0000121 else
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000122 DeclSeq.clear(); // Mark as invalid lock expression.
123 }
124
125 /// \brief Construct a MutexID from an expression.
126 /// \param MutexExp The original mutex expression within an attribute
127 /// \param DeclExp An expression involving the Decl on which the attribute
128 /// occurs.
129 /// \param D The declaration to which the lock/unlock attribute is attached.
130 void buildMutexIDFromExp(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D) {
131 Expr *Parent = 0;
DeLesley Hutchins81216392011-10-17 21:38:02 +0000132 unsigned NumArgs = 0;
133 Expr **FunArgs = 0;
134 SmallVector<const NamedDecl*, 8> FunArgDecls;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000135
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000136 // If we are processing a raw attribute expression, with no substitutions.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000137 if (DeclExp == 0) {
DeLesley Hutchins81216392011-10-17 21:38:02 +0000138 buildMutexID(MutexExp, 0, 0, 0, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000139 return;
140 }
141
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000142 // Examine DeclExp to find Parent and FunArgs, which are used to substitute
143 // for formal parameters when we call buildMutexID later.
DeLesley Hutchins81216392011-10-17 21:38:02 +0000144 if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000145 Parent = ME->getBase();
DeLesley Hutchins81216392011-10-17 21:38:02 +0000146 } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000147 Parent = CE->getImplicitObjectArgument();
DeLesley Hutchins81216392011-10-17 21:38:02 +0000148 NumArgs = CE->getNumArgs();
149 FunArgs = CE->getArgs();
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000150 } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
151 Parent = 0; // FIXME -- get the parent from DeclStmt
152 NumArgs = CE->getNumArgs();
153 FunArgs = CE->getArgs();
DeLesley Hutchins6db51f72011-10-21 20:51:27 +0000154 } else if (D && isa<CXXDestructorDecl>(D)) {
155 // There's no such thing as a "destructor call" in the AST.
156 Parent = DeclExp;
DeLesley Hutchins81216392011-10-17 21:38:02 +0000157 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000158
159 // If the attribute has no arguments, then assume the argument is "this".
160 if (MutexExp == 0) {
DeLesley Hutchins81216392011-10-17 21:38:02 +0000161 buildMutexID(Parent, 0, 0, 0, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000162 return;
163 }
DeLesley Hutchins81216392011-10-17 21:38:02 +0000164
165 // FIXME: handle default arguments
166 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
167 for (unsigned i = 0, ni = FD->getNumParams(); i < ni && i < NumArgs; ++i) {
168 FunArgDecls.push_back(FD->getParamDecl(i));
169 }
170 }
171 buildMutexID(MutexExp, Parent, NumArgs, &FunArgDecls.front(), FunArgs);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000172 }
173
174public:
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000175 explicit MutexID(clang::Decl::EmptyShell e) {
176 DeclSeq.clear();
177 }
178
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000179 /// \param MutexExp The original mutex expression within an attribute
180 /// \param DeclExp An expression involving the Decl on which the attribute
181 /// occurs.
182 /// \param D The declaration to which the lock/unlock attribute is attached.
183 /// Caller must check isValid() after construction.
184 MutexID(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D) {
185 buildMutexIDFromExp(MutexExp, DeclExp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000186 }
187
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000188 /// Return true if this is a valid decl sequence.
189 /// Caller must call this by hand after construction to handle errors.
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000190 bool isValid() const {
191 return !DeclSeq.empty();
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000192 }
193
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000194 /// Issue a warning about an invalid lock expression
195 static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp,
196 Expr *DeclExp, const NamedDecl* D) {
197 SourceLocation Loc;
198 if (DeclExp)
199 Loc = DeclExp->getExprLoc();
200
201 // FIXME: add a note about the attribute location in MutexExp or D
202 if (Loc.isValid())
203 Handler.handleInvalidLockExp(Loc);
204 }
205
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000206 bool operator==(const MutexID &other) const {
207 return DeclSeq == other.DeclSeq;
208 }
209
210 bool operator!=(const MutexID &other) const {
211 return !(*this == other);
212 }
213
214 // SmallVector overloads Operator< to do lexicographic ordering. Note that
215 // we use pointer equality (and <) to compare NamedDecls. This means the order
216 // of MutexIDs in a lockset is nondeterministic. In order to output
217 // diagnostics in a deterministic ordering, we must order all diagnostics to
218 // output by SourceLocation when iterating through this lockset.
219 bool operator<(const MutexID &other) const {
220 return DeclSeq < other.DeclSeq;
221 }
222
223 /// \brief Returns the name of the first Decl in the list for a given MutexID;
224 /// e.g. the lock expression foo.bar() has name "bar".
225 /// The caret will point unambiguously to the lock expression, so using this
226 /// name in diagnostics is a way to get simple, and consistent, mutex names.
227 /// We do not want to output the entire expression text for security reasons.
228 StringRef getName() const {
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000229 assert(isValid());
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000230 return DeclSeq.front()->getName();
231 }
232
233 void Profile(llvm::FoldingSetNodeID &ID) const {
234 for (SmallVectorImpl<NamedDecl*>::const_iterator I = DeclSeq.begin(),
235 E = DeclSeq.end(); I != E; ++I) {
236 ID.AddPointer(*I);
237 }
238 }
239};
240
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000241
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000242/// \brief This is a helper class that stores info about the most recent
243/// accquire of a Lock.
244///
245/// The main body of the analysis maps MutexIDs to LockDatas.
246struct LockData {
247 SourceLocation AcquireLoc;
248
249 /// \brief LKind stores whether a lock is held shared or exclusively.
250 /// Note that this analysis does not currently support either re-entrant
251 /// locking or lock "upgrading" and "downgrading" between exclusive and
252 /// shared.
253 ///
254 /// FIXME: add support for re-entrant locking and lock up/downgrading
255 LockKind LKind;
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000256 MutexID UnderlyingMutex; // for ScopedLockable objects
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000257
258 LockData(SourceLocation AcquireLoc, LockKind LKind)
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000259 : AcquireLoc(AcquireLoc), LKind(LKind), UnderlyingMutex(Decl::EmptyShell())
260 {}
261
262 LockData(SourceLocation AcquireLoc, LockKind LKind, const MutexID &Mu)
263 : AcquireLoc(AcquireLoc), LKind(LKind), UnderlyingMutex(Mu) {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000264
265 bool operator==(const LockData &other) const {
266 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
267 }
268
269 bool operator!=(const LockData &other) const {
270 return !(*this == other);
271 }
272
273 void Profile(llvm::FoldingSetNodeID &ID) const {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000274 ID.AddInteger(AcquireLoc.getRawEncoding());
275 ID.AddInteger(LKind);
276 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000277};
278
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000279
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000280/// A Lockset maps each MutexID (defined above) to information about how it has
281/// been locked.
282typedef llvm::ImmutableMap<MutexID, LockData> Lockset;
283
284/// \brief We use this class to visit different types of expressions in
285/// CFGBlocks, and build up the lockset.
286/// An expression may cause us to add or remove locks from the lockset, or else
287/// output error messages related to missing locks.
288/// FIXME: In future, we may be able to not inherit from a visitor.
289class BuildLockset : public StmtVisitor<BuildLockset> {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000290 friend class ThreadSafetyAnalyzer;
291
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000292 ThreadSafetyHandler &Handler;
293 Lockset LSet;
294 Lockset::Factory &LocksetFactory;
295
296 // Helper functions
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000297 void addLock(const MutexID &Mutex, const LockData &LDat);
298 void removeLock(const MutexID &Mutex, SourceLocation UnlockLoc);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000299
300 template <class AttrType>
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000301 void addLocksToSet(LockKind LK, AttrType *Attr,
302 Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000303 void removeLocksFromSet(UnlockFunctionAttr *Attr,
304 Expr *Exp, NamedDecl* FunDecl);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000305
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000306 const ValueDecl *getValueDecl(Expr *Exp);
307 void warnIfMutexNotHeld (const NamedDecl *D, Expr *Exp, AccessKind AK,
308 Expr *MutexExp, ProtectedOperationKind POK);
309 void checkAccess(Expr *Exp, AccessKind AK);
310 void checkDereference(Expr *Exp, AccessKind AK);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000311 void handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000312
313 /// \brief Returns true if the lockset contains a lock, regardless of whether
314 /// the lock is held exclusively or shared.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000315 bool locksetContains(const MutexID &Lock) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000316 return LSet.lookup(Lock);
317 }
318
319 /// \brief Returns true if the lockset contains a lock with the passed in
320 /// locktype.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000321 bool locksetContains(const MutexID &Lock, LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000322 const LockData *LockHeld = LSet.lookup(Lock);
323 return (LockHeld && KindRequested == LockHeld->LKind);
324 }
325
326 /// \brief Returns true if the lockset contains a lock with at least the
327 /// passed in locktype. So for example, if we pass in LK_Shared, this function
328 /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in
329 /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000330 bool locksetContainsAtLeast(const MutexID &Lock,
331 LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000332 switch (KindRequested) {
333 case LK_Shared:
334 return locksetContains(Lock);
335 case LK_Exclusive:
336 return locksetContains(Lock, KindRequested);
337 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +0000338 llvm_unreachable("Unknown LockKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000339 }
340
341public:
342 BuildLockset(ThreadSafetyHandler &Handler, Lockset LS, Lockset::Factory &F)
343 : StmtVisitor<BuildLockset>(), Handler(Handler), LSet(LS),
344 LocksetFactory(F) {}
345
346 Lockset getLockset() {
347 return LSet;
348 }
349
350 void VisitUnaryOperator(UnaryOperator *UO);
351 void VisitBinaryOperator(BinaryOperator *BO);
352 void VisitCastExpr(CastExpr *CE);
353 void VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp);
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000354 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000355 void VisitDeclStmt(DeclStmt *S);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000356};
357
358/// \brief Add a new lock to the lockset, warning if the lock is already there.
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000359/// \param Mutex -- the Mutex expression for the lock
360/// \param LDat -- the LockData for the lock
361void BuildLockset::addLock(const MutexID &Mutex, const LockData& LDat) {
362 // FIXME: deal with acquired before/after annotations.
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000363 // FIXME: Don't always warn when we have support for reentrant locks.
364 if (locksetContains(Mutex))
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000365 Handler.handleDoubleLock(Mutex.getName(), LDat.AcquireLoc);
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000366 else
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000367 LSet = LocksetFactory.add(LSet, Mutex, LDat);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000368}
369
370/// \brief Remove a lock from the lockset, warning if the lock is not there.
371/// \param LockExp The lock expression corresponding to the lock to be removed
372/// \param UnlockLoc The source location of the unlock (only used in error msg)
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000373void BuildLockset::removeLock(const MutexID &Mutex, SourceLocation UnlockLoc) {
374 const LockData *LDat = LSet.lookup(Mutex);
375 if (!LDat)
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000376 Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000377 else {
378 // For scoped-lockable vars, remove the mutex associated with this var.
379 if (LDat->UnderlyingMutex.isValid())
380 removeLock(LDat->UnderlyingMutex, UnlockLoc);
381 LSet = LocksetFactory.remove(LSet, Mutex);
382 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000383}
384
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000385/// \brief This function, parameterized by an attribute type, is used to add a
386/// set of locks specified as attribute arguments to the lockset.
387template <typename AttrType>
388void BuildLockset::addLocksToSet(LockKind LK, AttrType *Attr,
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000389 Expr *Exp, NamedDecl* FunDecl, VarDecl *VD) {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000390 typedef typename AttrType::args_iterator iterator_type;
391
392 SourceLocation ExpLocation = Exp->getExprLoc();
393
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000394 // Figure out if we're calling the constructor of scoped lockable class
395 bool isScopedVar = false;
396 if (VD) {
397 if (CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FunDecl)) {
398 CXXRecordDecl* PD = CD->getParent();
399 if (PD && PD->getAttr<ScopedLockableAttr>())
400 isScopedVar = true;
401 }
402 }
403
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000404 if (Attr->args_size() == 0) {
405 // The mutex held is the "this" object.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000406 MutexID Mutex(0, Exp, FunDecl);
407 if (!Mutex.isValid())
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000408 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000409 else
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000410 addLock(Mutex, LockData(ExpLocation, LK));
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000411 return;
412 }
413
414 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
415 MutexID Mutex(*I, Exp, FunDecl);
416 if (!Mutex.isValid())
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000417 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000418 else {
419 addLock(Mutex, LockData(ExpLocation, LK));
420 if (isScopedVar) {
421 // For scoped lockable vars, map this var to its underlying mutex.
422 DeclRefExpr DRE(VD, VD->getType(), VK_LValue, VD->getLocation());
423 MutexID SMutex(&DRE, 0, 0);
424 addLock(SMutex, LockData(VD->getLocation(), LK, Mutex));
425 }
426 }
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000427 }
428}
429
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000430/// \brief This function removes a set of locks specified as attribute
431/// arguments from the lockset.
432void BuildLockset::removeLocksFromSet(UnlockFunctionAttr *Attr,
433 Expr *Exp, NamedDecl* FunDecl) {
434 SourceLocation ExpLocation;
435 if (Exp) ExpLocation = Exp->getExprLoc();
436
437 if (Attr->args_size() == 0) {
438 // The mutex held is the "this" object.
439 MutexID Mu(0, Exp, FunDecl);
440 if (!Mu.isValid())
441 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
442 else
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000443 removeLock(Mu, ExpLocation);
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000444 return;
445 }
446
447 for (UnlockFunctionAttr::args_iterator I = Attr->args_begin(),
448 E = Attr->args_end(); I != E; ++I) {
449 MutexID Mutex(*I, Exp, FunDecl);
450 if (!Mutex.isValid())
451 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
452 else
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000453 removeLock(Mutex, ExpLocation);
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000454 }
455}
456
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000457/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
458const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
459 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
460 return DR->getDecl();
461
462 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
463 return ME->getMemberDecl();
464
465 return 0;
466}
467
468/// \brief Warn if the LSet does not contain a lock sufficient to protect access
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000469/// of at least the passed in AccessKind.
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000470void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
471 AccessKind AK, Expr *MutexExp,
472 ProtectedOperationKind POK) {
473 LockKind LK = getLockKindFromAccessKind(AK);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000474
475 MutexID Mutex(MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000476 if (!Mutex.isValid())
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000477 MutexID::warnInvalidLock(Handler, MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000478 else if (!locksetContainsAtLeast(Mutex, LK))
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000479 Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK, Exp->getExprLoc());
480}
481
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000482/// \brief This method identifies variable dereferences and checks pt_guarded_by
483/// and pt_guarded_var annotations. Note that we only check these annotations
484/// at the time a pointer is dereferenced.
485/// FIXME: We need to check for other types of pointer dereferences
486/// (e.g. [], ->) and deal with them here.
487/// \param Exp An expression that has been read or written.
488void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
489 UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
490 if (!UO || UO->getOpcode() != clang::UO_Deref)
491 return;
492 Exp = UO->getSubExpr()->IgnoreParenCasts();
493
494 const ValueDecl *D = getValueDecl(Exp);
495 if(!D || !D->hasAttrs())
496 return;
497
498 if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty())
499 Handler.handleNoMutexHeld(D, POK_VarDereference, AK, Exp->getExprLoc());
500
501 const AttrVec &ArgAttrs = D->getAttrs();
502 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
503 if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
504 warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
505}
506
507/// \brief Checks guarded_by and guarded_var attributes.
508/// Whenever we identify an access (read or write) of a DeclRefExpr or
509/// MemberExpr, we need to check whether there are any guarded_by or
510/// guarded_var attributes, and make sure we hold the appropriate mutexes.
511void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
512 const ValueDecl *D = getValueDecl(Exp);
513 if(!D || !D->hasAttrs())
514 return;
515
516 if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty())
517 Handler.handleNoMutexHeld(D, POK_VarAccess, AK, Exp->getExprLoc());
518
519 const AttrVec &ArgAttrs = D->getAttrs();
520 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
521 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
522 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
523}
524
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000525/// \brief Process a function call, method call, constructor call,
526/// or destructor call. This involves looking at the attributes on the
527/// corresponding function/method/constructor/destructor, issuing warnings,
528/// and updating the locksets accordingly.
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000529///
530/// FIXME: For classes annotated with one of the guarded annotations, we need
531/// to treat const method calls as reads and non-const method calls as writes,
532/// and check that the appropriate locks are held. Non-const method calls with
533/// the same signature as const method calls can be also treated as reads.
534///
535/// FIXME: We need to also visit CallExprs to catch/check global functions.
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000536///
537/// FIXME: Do not flag an error for member variables accessed in constructors/
538/// destructors
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000539void BuildLockset::handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000540 AttrVec &ArgAttrs = D->getAttrs();
541 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
542 Attr *Attr = ArgAttrs[i];
543 switch (Attr->getKind()) {
544 // When we encounter an exclusive lock function, we need to add the lock
545 // to our lockset with kind exclusive.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000546 case attr::ExclusiveLockFunction: {
547 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(Attr);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000548 addLocksToSet(LK_Exclusive, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000549 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000550 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000551
552 // When we encounter a shared lock function, we need to add the lock
553 // to our lockset with kind shared.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000554 case attr::SharedLockFunction: {
555 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(Attr);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000556 addLocksToSet(LK_Shared, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000557 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000558 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000559
560 // When we encounter an unlock function, we need to remove unlocked
561 // mutexes from the lockset, and flag a warning if they are not there.
562 case attr::UnlockFunction: {
563 UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr);
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000564 removeLocksFromSet(UFAttr, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000565 break;
566 }
567
568 case attr::ExclusiveLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000569 ExclusiveLocksRequiredAttr *ELRAttr =
570 cast<ExclusiveLocksRequiredAttr>(Attr);
571
572 for (ExclusiveLocksRequiredAttr::args_iterator
573 I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I)
574 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
575 break;
576 }
577
578 case attr::SharedLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000579 SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr);
580
581 for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(),
582 E = SLRAttr->args_end(); I != E; ++I)
583 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
584 break;
585 }
586
587 case attr::LocksExcluded: {
588 LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr);
589 for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(),
590 E = LEAttr->args_end(); I != E; ++I) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000591 MutexID Mutex(*I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000592 if (!Mutex.isValid())
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000593 MutexID::warnInvalidLock(Handler, *I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000594 else if (locksetContains(Mutex))
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000595 Handler.handleFunExcludesLock(D->getName(), Mutex.getName(),
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000596 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000597 }
598 break;
599 }
600
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000601 // Ignore other (non thread-safety) attributes
602 default:
603 break;
604 }
605 }
606}
607
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000608/// \brief For unary operations which read and write a variable, we need to
609/// check whether we hold any required mutexes. Reads are checked in
610/// VisitCastExpr.
611void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
612 switch (UO->getOpcode()) {
613 case clang::UO_PostDec:
614 case clang::UO_PostInc:
615 case clang::UO_PreDec:
616 case clang::UO_PreInc: {
617 Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
618 checkAccess(SubExp, AK_Written);
619 checkDereference(SubExp, AK_Written);
620 break;
621 }
622 default:
623 break;
624 }
625}
626
627/// For binary operations which assign to a variable (writes), we need to check
628/// whether we hold any required mutexes.
629/// FIXME: Deal with non-primitive types.
630void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
631 if (!BO->isAssignmentOp())
632 return;
633 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
634 checkAccess(LHSExp, AK_Written);
635 checkDereference(LHSExp, AK_Written);
636}
637
638/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
639/// need to ensure we hold any required mutexes.
640/// FIXME: Deal with non-primitive types.
641void BuildLockset::VisitCastExpr(CastExpr *CE) {
642 if (CE->getCastKind() != CK_LValueToRValue)
643 return;
644 Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
645 checkAccess(SubExp, AK_Read);
646 checkDereference(SubExp, AK_Read);
647}
648
649
650void BuildLockset::VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp) {
651 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
652 if(!D || !D->hasAttrs())
653 return;
654 handleCall(Exp, D);
655}
656
657void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000658 // FIXME -- only handles constructors in DeclStmt below.
659}
660
661void BuildLockset::VisitDeclStmt(DeclStmt *S) {
662 DeclGroupRef DGrp = S->getDeclGroup();
663 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
664 Decl *D = *I;
665 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
666 Expr *E = VD->getInit();
667 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
668 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
669 if (!CtorD || !CtorD->hasAttrs())
670 return;
671 handleCall(CE, CtorD, VD);
672 }
673 }
674 }
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000675}
676
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000677
678/// \brief Class which implements the core thread safety analysis routines.
679class ThreadSafetyAnalyzer {
680 ThreadSafetyHandler &Handler;
681 Lockset::Factory LocksetFactory;
682
683public:
684 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
685
686 Lockset intersectAndWarn(const Lockset LSet1, const Lockset LSet2,
687 LockErrorKind LEK);
688
689 Lockset addLock(Lockset &LSet, Expr *MutexExp, const NamedDecl *D,
690 LockKind LK, SourceLocation Loc);
691
Ted Kremenek1d26f482011-10-24 01:32:45 +0000692 void runAnalysis(AnalysisDeclContext &AC);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000693};
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000694
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000695/// \brief Compute the intersection of two locksets and issue warnings for any
696/// locks in the symmetric difference.
697///
698/// This function is used at a merge point in the CFG when comparing the lockset
699/// of each branch being merged. For example, given the following sequence:
700/// A; if () then B; else C; D; we need to check that the lockset after B and C
701/// are the same. In the event of a difference, we use the intersection of these
702/// two locksets at the start of D.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000703Lockset ThreadSafetyAnalyzer::intersectAndWarn(const Lockset LSet1,
704 const Lockset LSet2,
705 LockErrorKind LEK) {
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000706 Lockset Intersection = LSet1;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000707 for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) {
708 const MutexID &LSet2Mutex = I.getKey();
709 const LockData &LSet2LockData = I.getData();
710 if (const LockData *LD = LSet1.lookup(LSet2Mutex)) {
711 if (LD->LKind != LSet2LockData.LKind) {
712 Handler.handleExclusiveAndShared(LSet2Mutex.getName(),
713 LSet2LockData.AcquireLoc,
714 LD->AcquireLoc);
715 if (LD->LKind != LK_Exclusive)
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000716 Intersection = LocksetFactory.add(Intersection, LSet2Mutex,
717 LSet2LockData);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000718 }
719 } else {
720 Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(),
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000721 LSet2LockData.AcquireLoc, LEK);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000722 }
723 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000724
725 for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) {
726 if (!LSet2.contains(I.getKey())) {
727 const MutexID &Mutex = I.getKey();
728 const LockData &MissingLock = I.getData();
729 Handler.handleMutexHeldEndOfScope(Mutex.getName(),
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000730 MissingLock.AcquireLoc, LEK);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000731 Intersection = LocksetFactory.remove(Intersection, Mutex);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000732 }
733 }
734 return Intersection;
735}
736
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000737Lockset ThreadSafetyAnalyzer::addLock(Lockset &LSet, Expr *MutexExp,
738 const NamedDecl *D,
739 LockKind LK, SourceLocation Loc) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000740 MutexID Mutex(MutexExp, 0, D);
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000741 if (!Mutex.isValid()) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000742 MutexID::warnInvalidLock(Handler, MutexExp, 0, D);
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000743 return LSet;
744 }
745 LockData NewLock(Loc, LK);
746 return LocksetFactory.add(LSet, Mutex, NewLock);
747}
748
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000749/// \brief Check a function's CFG for thread-safety violations.
750///
751/// We traverse the blocks in the CFG, compute the set of mutexes that are held
752/// at the end of each block, and issue warnings for thread safety violations.
753/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +0000754void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000755 CFG *CFGraph = AC.getCFG();
756 if (!CFGraph) return;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000757 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
758
759 if (!D)
760 return; // Ignore anonymous functions for now.
761 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
762 return;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000763
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000764 // FIXME: Switch to SmallVector? Otherwise improve performance impact?
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000765 std::vector<Lockset> EntryLocksets(CFGraph->getNumBlockIDs(),
766 LocksetFactory.getEmptyMap());
767 std::vector<Lockset> ExitLocksets(CFGraph->getNumBlockIDs(),
768 LocksetFactory.getEmptyMap());
769
770 // We need to explore the CFG via a "topological" ordering.
771 // That way, we will be guaranteed to have information about required
772 // predecessor locksets when exploring a new block.
Ted Kremenek439ed162011-10-22 02:14:27 +0000773 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
774 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000775
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000776 // Add locks from exclusive_locks_required and shared_locks_required
777 // to initial lockset.
Ted Kremenek439ed162011-10-22 02:14:27 +0000778 if (!SortedGraph->empty() && D->hasAttrs()) {
779 const CFGBlock *FirstBlock = *SortedGraph->begin();
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000780 Lockset &InitialLockset = EntryLocksets[FirstBlock->getBlockID()];
781 const AttrVec &ArgAttrs = D->getAttrs();
782 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
783 Attr *Attr = ArgAttrs[i];
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000784 SourceLocation AttrLoc = Attr->getLocation();
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000785 if (SharedLocksRequiredAttr *SLRAttr
786 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
787 for (SharedLocksRequiredAttr::args_iterator
788 SLRIter = SLRAttr->args_begin(),
789 SLREnd = SLRAttr->args_end(); SLRIter != SLREnd; ++SLRIter)
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000790 InitialLockset = addLock(InitialLockset,
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000791 *SLRIter, D, LK_Shared,
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000792 AttrLoc);
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000793 } else if (ExclusiveLocksRequiredAttr *ELRAttr
794 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
795 for (ExclusiveLocksRequiredAttr::args_iterator
796 ELRIter = ELRAttr->args_begin(),
797 ELREnd = ELRAttr->args_end(); ELRIter != ELREnd; ++ELRIter)
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000798 InitialLockset = addLock(InitialLockset,
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000799 *ELRIter, D, LK_Exclusive,
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000800 AttrLoc);
Caitlin Sadowskicb967512011-09-15 17:43:08 +0000801 }
802 }
803 }
804
Ted Kremenek439ed162011-10-22 02:14:27 +0000805 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
806 E = SortedGraph->end(); I!= E; ++I) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000807 const CFGBlock *CurrBlock = *I;
808 int CurrBlockID = CurrBlock->getBlockID();
809
810 VisitedBlocks.insert(CurrBlock);
811
812 // Use the default initial lockset in case there are no predecessors.
813 Lockset &Entryset = EntryLocksets[CurrBlockID];
814 Lockset &Exitset = ExitLocksets[CurrBlockID];
815
816 // Iterate through the predecessor blocks and warn if the lockset for all
817 // predecessors is not the same. We take the entry lockset of the current
818 // block to be the intersection of all previous locksets.
819 // FIXME: By keeping the intersection, we may output more errors in future
820 // for a lock which is not in the intersection, but was in the union. We
821 // may want to also keep the union in future. As an example, let's say
822 // the intersection contains Mutex L, and the union contains L and M.
823 // Later we unlock M. At this point, we would output an error because we
824 // never locked M; although the real error is probably that we forgot to
825 // lock M on all code paths. Conversely, let's say that later we lock M.
826 // In this case, we should compare against the intersection instead of the
827 // union because the real error is probably that we forgot to unlock M on
828 // all code paths.
829 bool LocksetInitialized = false;
830 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
831 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
832
833 // if *PI -> CurrBlock is a back edge
834 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
835 continue;
836
837 int PrevBlockID = (*PI)->getBlockID();
838 if (!LocksetInitialized) {
839 Entryset = ExitLocksets[PrevBlockID];
840 LocksetInitialized = true;
841 } else {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000842 Entryset = intersectAndWarn(Entryset, ExitLocksets[PrevBlockID],
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000843 LEK_LockedSomePredecessors);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000844 }
845 }
846
847 BuildLockset LocksetBuilder(Handler, Entryset, LocksetFactory);
848 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
849 BE = CurrBlock->end(); BI != BE; ++BI) {
DeLesley Hutchins6db51f72011-10-21 20:51:27 +0000850 switch (BI->getKind()) {
851 case CFGElement::Statement: {
852 const CFGStmt *CS = cast<CFGStmt>(&*BI);
853 LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
854 break;
855 }
856 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
857 case CFGElement::AutomaticObjectDtor: {
858 const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
859 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
860 AD->getDestructorDecl(AC.getASTContext()));
861 if (!DD->hasAttrs())
862 break;
863
864 // Create a dummy expression,
865 VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
866 DeclRefExpr DRE(VD, VD->getType(), VK_LValue,
867 AD->getTriggerStmt()->getLocEnd());
868 LocksetBuilder.handleCall(&DRE, DD);
869 break;
870 }
871 default:
872 break;
873 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000874 }
875 Exitset = LocksetBuilder.getLockset();
876
877 // For every back edge from CurrBlock (the end of the loop) to another block
878 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
879 // the one held at the beginning of FirstLoopBlock. We can look up the
880 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
881 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
882 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
883
884 // if CurrBlock -> *SI is *not* a back edge
885 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
886 continue;
887
888 CFGBlock *FirstLoopBlock = *SI;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000889 Lockset PreLoop = EntryLocksets[FirstLoopBlock->getBlockID()];
890 Lockset LoopEnd = ExitLocksets[CurrBlockID];
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000891 intersectAndWarn(LoopEnd, PreLoop, LEK_LockedSomeLoopIterations);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000892 }
893 }
894
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +0000895 Lockset InitialLockset = EntryLocksets[CFGraph->getEntry().getBlockID()];
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000896 Lockset FinalLockset = ExitLocksets[CFGraph->getExit().getBlockID()];
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000897
898 // FIXME: Should we call this function for all blocks which exit the function?
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000899 intersectAndWarn(InitialLockset, FinalLockset, LEK_LockedAtEndOfFunction);
900}
901
902} // end anonymous namespace
903
904
905namespace clang {
906namespace thread_safety {
907
908/// \brief Check a function's CFG for thread-safety violations.
909///
910/// We traverse the blocks in the CFG, compute the set of mutexes that are held
911/// at the end of each block, and issue warnings for thread safety violations.
912/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +0000913void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000914 ThreadSafetyHandler &Handler) {
915 ThreadSafetyAnalyzer Analyzer(Handler);
916 Analyzer.runAnalysis(AC);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000917}
918
919/// \brief Helper function that returns a LockKind required for the given level
920/// of access.
921LockKind getLockKindFromAccessKind(AccessKind AK) {
922 switch (AK) {
923 case AK_Read :
924 return LK_Shared;
925 case AK_Written :
926 return LK_Exclusive;
927 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +0000928 llvm_unreachable("Unknown AccessKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000929}
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000930
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000931}} // end namespace clang::thread_safety