blob: f9d93ee0dfad03d8d2d4e7e8ea3bca4013b88aa6 [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"
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000035#include "llvm/Support/raw_ostream.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000036#include <algorithm>
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000037#include <utility>
Caitlin Sadowski402aa062011-09-09 16:11:56 +000038#include <vector>
39
40using namespace clang;
41using namespace thread_safety;
42
Caitlin Sadowski19903462011-09-14 20:05:09 +000043// Key method definition
44ThreadSafetyHandler::~ThreadSafetyHandler() {}
45
Caitlin Sadowski402aa062011-09-09 16:11:56 +000046namespace {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +000047
Caitlin Sadowski402aa062011-09-09 16:11:56 +000048/// \brief A MutexID object uniquely identifies a particular mutex, and
49/// is built from an Expr* (i.e. calling a lock function).
50///
51/// Thread-safety analysis works by comparing lock expressions. Within the
52/// body of a function, an expression such as "x->foo->bar.mu" will resolve to
53/// a particular mutex object at run-time. Subsequent occurrences of the same
54/// expression (where "same" means syntactic equality) will refer to the same
55/// run-time object if three conditions hold:
56/// (1) Local variables in the expression, such as "x" have not changed.
57/// (2) Values on the heap that affect the expression have not changed.
58/// (3) The expression involves only pure function calls.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +000059///
Caitlin Sadowski402aa062011-09-09 16:11:56 +000060/// The current implementation assumes, but does not verify, that multiple uses
61/// of the same lock expression satisfies these criteria.
62///
63/// Clang introduces an additional wrinkle, which is that it is difficult to
64/// derive canonical expressions, or compare expressions directly for equality.
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +000065/// Thus, we identify a mutex not by an Expr, but by the list of named
Caitlin Sadowski402aa062011-09-09 16:11:56 +000066/// declarations that are referenced by the Expr. In other words,
67/// x->foo->bar.mu will be a four element vector with the Decls for
68/// mu, bar, and foo, and x. The vector will uniquely identify the expression
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +000069/// for all practical purposes. Null is used to denote 'this'.
Caitlin Sadowski402aa062011-09-09 16:11:56 +000070///
71/// Note we will need to perform substitution on "this" and function parameter
72/// names when constructing a lock expression.
73///
74/// For example:
75/// class C { Mutex Mu; void lock() EXCLUSIVE_LOCK_FUNCTION(this->Mu); };
76/// void myFunc(C *X) { ... X->lock() ... }
77/// The original expression for the mutex acquired by myFunc is "this->Mu", but
78/// "X" is substituted for "this" so we get X->Mu();
79///
80/// For another example:
81/// foo(MyList *L) EXCLUSIVE_LOCKS_REQUIRED(L->Mu) { ... }
82/// MyList *MyL;
83/// foo(MyL); // requires lock MyL->Mu to be held
84class MutexID {
85 SmallVector<NamedDecl*, 2> DeclSeq;
86
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +000087 /// \brief Encapsulates the lexical context of a function call. The lexical
88 /// context includes the arguments to the call, including the implicit object
89 /// argument. When an attribute containing a mutex expression is attached to
90 /// a method, the expression may refer to formal parameters of the method.
91 /// Actual arguments must be substituted for formal parameters to derive
92 /// the appropriate mutex expression in the lexical context where the function
93 /// is called. PrevCtx holds the context in which the arguments themselves
94 /// should be evaluated; multiple calling contexts can be chained together
95 /// by the lock_returned attribute.
96 struct CallingContext {
97 const NamedDecl* AttrDecl; // The decl to which the attribute is attached.
98 Expr* SelfArg; // Implicit object argument -- e.g. 'this'
99 unsigned NumArgs; // Number of funArgs
100 Expr** FunArgs; // Function arguments
101 CallingContext* PrevCtx; // The previous context; or 0 if none.
102
103 CallingContext(const NamedDecl* D = 0, Expr* S = 0,
104 unsigned N = 0, Expr** A = 0, CallingContext* P = 0)
105 : AttrDecl(D), SelfArg(S), NumArgs(N), FunArgs(A), PrevCtx(P)
106 { }
107 };
108
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000109 /// Build a Decl sequence representing the lock from the given expression.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000110 /// Recursive function that terminates on DeclRefExpr.
111 /// Note: this function merely creates a MutexID; it does not check to
112 /// ensure that the original expression is a valid mutex expression.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000113 void buildMutexID(Expr *Exp, CallingContext* CallCtx) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000114 if (!Exp) {
115 DeclSeq.clear();
116 return;
117 }
118
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000119 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
120 NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000121 ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND);
122 if (PV) {
123 FunctionDecl *FD =
124 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
125 unsigned i = PV->getFunctionScopeIndex();
126
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000127 if (CallCtx && CallCtx->FunArgs &&
128 FD == CallCtx->AttrDecl->getCanonicalDecl()) {
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000129 // Substitute call arguments for references to function parameters
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000130 assert(i < CallCtx->NumArgs);
131 buildMutexID(CallCtx->FunArgs[i], CallCtx->PrevCtx);
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000132 return;
133 }
134 // Map the param back to the param of the original function declaration.
135 DeclSeq.push_back(FD->getParamDecl(i));
136 return;
137 }
138 // Not a function parameter -- just store the reference.
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000139 DeclSeq.push_back(ND);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000140 } else if (isa<CXXThisExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000141 // Substitute parent for 'this'
142 if (CallCtx && CallCtx->SelfArg)
143 buildMutexID(CallCtx->SelfArg, CallCtx->PrevCtx);
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000144 else {
145 DeclSeq.push_back(0); // Use 0 to represent 'this'.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000146 return; // mutexID is still valid in this case
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000147 }
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000148 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
149 NamedDecl *ND = ME->getMemberDecl();
150 DeclSeq.push_back(ND);
151 buildMutexID(ME->getBase(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000152 } else if (CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000153 // When calling a function with a lock_returned attribute, replace
154 // the function call with the expression in lock_returned.
155 if (LockReturnedAttr* At =
156 CMCE->getMethodDecl()->getAttr<LockReturnedAttr>()) {
157 CallingContext LRCallCtx(CMCE->getMethodDecl());
158 LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument();
159 LRCallCtx.NumArgs = CMCE->getNumArgs();
160 LRCallCtx.FunArgs = CMCE->getArgs();
161 LRCallCtx.PrevCtx = CallCtx;
162 buildMutexID(At->getArg(), &LRCallCtx);
163 return;
164 }
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000165 DeclSeq.push_back(CMCE->getMethodDecl()->getCanonicalDecl());
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000166 buildMutexID(CMCE->getImplicitObjectArgument(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000167 unsigned NumCallArgs = CMCE->getNumArgs();
168 Expr** CallArgs = CMCE->getArgs();
169 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000170 buildMutexID(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000171 }
172 } else if (CallExpr *CE = dyn_cast<CallExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000173 if (LockReturnedAttr* At =
174 CE->getDirectCallee()->getAttr<LockReturnedAttr>()) {
175 CallingContext LRCallCtx(CE->getDirectCallee());
176 LRCallCtx.NumArgs = CE->getNumArgs();
177 LRCallCtx.FunArgs = CE->getArgs();
178 LRCallCtx.PrevCtx = CallCtx;
179 buildMutexID(At->getArg(), &LRCallCtx);
180 return;
181 }
182 buildMutexID(CE->getCallee(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000183 unsigned NumCallArgs = CE->getNumArgs();
184 Expr** CallArgs = CE->getArgs();
185 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000186 buildMutexID(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000187 }
188 } else if (BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000189 buildMutexID(BOE->getLHS(), CallCtx);
190 buildMutexID(BOE->getRHS(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000191 } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000192 buildMutexID(UOE->getSubExpr(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000193 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000194 buildMutexID(ASE->getBase(), CallCtx);
195 buildMutexID(ASE->getIdx(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000196 } else if (AbstractConditionalOperator *CE =
197 dyn_cast<AbstractConditionalOperator>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000198 buildMutexID(CE->getCond(), CallCtx);
199 buildMutexID(CE->getTrueExpr(), CallCtx);
200 buildMutexID(CE->getFalseExpr(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000201 } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000202 buildMutexID(CE->getCond(), CallCtx);
203 buildMutexID(CE->getLHS(), CallCtx);
204 buildMutexID(CE->getRHS(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000205 } else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000206 buildMutexID(CE->getSubExpr(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000207 } else if (ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000208 buildMutexID(PE->getSubExpr(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000209 } else if (isa<CharacterLiteral>(Exp) ||
210 isa<CXXNullPtrLiteralExpr>(Exp) ||
211 isa<GNUNullExpr>(Exp) ||
212 isa<CXXBoolLiteralExpr>(Exp) ||
213 isa<FloatingLiteral>(Exp) ||
214 isa<ImaginaryLiteral>(Exp) ||
215 isa<IntegerLiteral>(Exp) ||
216 isa<StringLiteral>(Exp) ||
217 isa<ObjCStringLiteral>(Exp)) {
218 return; // FIXME: Ignore literals for now
219 } else {
220 // Ignore. FIXME: mark as invalid expression?
221 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000222 }
223
224 /// \brief Construct a MutexID from an expression.
225 /// \param MutexExp The original mutex expression within an attribute
226 /// \param DeclExp An expression involving the Decl on which the attribute
227 /// occurs.
228 /// \param D The declaration to which the lock/unlock attribute is attached.
229 void buildMutexIDFromExp(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000230 CallingContext CallCtx(D);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000231
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000232 // If we are processing a raw attribute expression, with no substitutions.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000233 if (DeclExp == 0) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000234 buildMutexID(MutexExp, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000235 return;
236 }
237
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000238 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000239 // for formal parameters when we call buildMutexID later.
DeLesley Hutchins81216392011-10-17 21:38:02 +0000240 if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000241 CallCtx.SelfArg = ME->getBase();
DeLesley Hutchins81216392011-10-17 21:38:02 +0000242 } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000243 CallCtx.SelfArg = CE->getImplicitObjectArgument();
244 CallCtx.NumArgs = CE->getNumArgs();
245 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinsdf497822011-12-29 00:56:48 +0000246 } else if (CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000247 CallCtx.NumArgs = CE->getNumArgs();
248 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000249 } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000250 CallCtx.SelfArg = 0; // FIXME -- get the parent from DeclStmt
251 CallCtx.NumArgs = CE->getNumArgs();
252 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchins6db51f72011-10-21 20:51:27 +0000253 } else if (D && isa<CXXDestructorDecl>(D)) {
254 // There's no such thing as a "destructor call" in the AST.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000255 CallCtx.SelfArg = DeclExp;
DeLesley Hutchins81216392011-10-17 21:38:02 +0000256 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000257
258 // If the attribute has no arguments, then assume the argument is "this".
259 if (MutexExp == 0) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000260 buildMutexID(CallCtx.SelfArg, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000261 return;
262 }
DeLesley Hutchins81216392011-10-17 21:38:02 +0000263
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000264 // For most attributes.
265 buildMutexID(MutexExp, &CallCtx);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000266 }
267
268public:
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000269 explicit MutexID(clang::Decl::EmptyShell e) {
270 DeclSeq.clear();
271 }
272
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000273 /// \param MutexExp The original mutex expression within an attribute
274 /// \param DeclExp An expression involving the Decl on which the attribute
275 /// occurs.
276 /// \param D The declaration to which the lock/unlock attribute is attached.
277 /// Caller must check isValid() after construction.
278 MutexID(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D) {
279 buildMutexIDFromExp(MutexExp, DeclExp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000280 }
281
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000282 /// Return true if this is a valid decl sequence.
283 /// Caller must call this by hand after construction to handle errors.
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000284 bool isValid() const {
285 return !DeclSeq.empty();
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000286 }
287
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000288 /// Issue a warning about an invalid lock expression
289 static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp,
290 Expr *DeclExp, const NamedDecl* D) {
291 SourceLocation Loc;
292 if (DeclExp)
293 Loc = DeclExp->getExprLoc();
294
295 // FIXME: add a note about the attribute location in MutexExp or D
296 if (Loc.isValid())
297 Handler.handleInvalidLockExp(Loc);
298 }
299
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000300 bool operator==(const MutexID &other) const {
301 return DeclSeq == other.DeclSeq;
302 }
303
304 bool operator!=(const MutexID &other) const {
305 return !(*this == other);
306 }
307
308 // SmallVector overloads Operator< to do lexicographic ordering. Note that
309 // we use pointer equality (and <) to compare NamedDecls. This means the order
310 // of MutexIDs in a lockset is nondeterministic. In order to output
311 // diagnostics in a deterministic ordering, we must order all diagnostics to
312 // output by SourceLocation when iterating through this lockset.
313 bool operator<(const MutexID &other) const {
314 return DeclSeq < other.DeclSeq;
315 }
316
317 /// \brief Returns the name of the first Decl in the list for a given MutexID;
318 /// e.g. the lock expression foo.bar() has name "bar".
319 /// The caret will point unambiguously to the lock expression, so using this
320 /// name in diagnostics is a way to get simple, and consistent, mutex names.
321 /// We do not want to output the entire expression text for security reasons.
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000322 std::string getName() const {
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000323 assert(isValid());
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000324 if (!DeclSeq.front())
325 return "this"; // Use 0 to represent 'this'.
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000326 return DeclSeq.front()->getNameAsString();
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000327 }
328
329 void Profile(llvm::FoldingSetNodeID &ID) const {
330 for (SmallVectorImpl<NamedDecl*>::const_iterator I = DeclSeq.begin(),
331 E = DeclSeq.end(); I != E; ++I) {
332 ID.AddPointer(*I);
333 }
334 }
335};
336
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000337
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000338/// \brief This is a helper class that stores info about the most recent
339/// accquire of a Lock.
340///
341/// The main body of the analysis maps MutexIDs to LockDatas.
342struct LockData {
343 SourceLocation AcquireLoc;
344
345 /// \brief LKind stores whether a lock is held shared or exclusively.
346 /// Note that this analysis does not currently support either re-entrant
347 /// locking or lock "upgrading" and "downgrading" between exclusive and
348 /// shared.
349 ///
350 /// FIXME: add support for re-entrant locking and lock up/downgrading
351 LockKind LKind;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000352 bool Managed; // for ScopedLockable objects
353 MutexID UnderlyingMutex; // for ScopedLockable objects
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000354
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000355 LockData(SourceLocation AcquireLoc, LockKind LKind, bool M = false)
356 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(M),
357 UnderlyingMutex(Decl::EmptyShell())
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000358 {}
359
360 LockData(SourceLocation AcquireLoc, LockKind LKind, const MutexID &Mu)
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000361 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(false),
362 UnderlyingMutex(Mu)
363 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000364
365 bool operator==(const LockData &other) const {
366 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
367 }
368
369 bool operator!=(const LockData &other) const {
370 return !(*this == other);
371 }
372
373 void Profile(llvm::FoldingSetNodeID &ID) const {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000374 ID.AddInteger(AcquireLoc.getRawEncoding());
375 ID.AddInteger(LKind);
376 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000377};
378
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000379
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000380/// A Lockset maps each MutexID (defined above) to information about how it has
381/// been locked.
382typedef llvm::ImmutableMap<MutexID, LockData> Lockset;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000383typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000384
385class LocalVariableMap;
386
Richard Smith2e515622012-02-03 04:45:26 +0000387/// A side (entry or exit) of a CFG node.
388enum CFGBlockSide { CBS_Entry, CBS_Exit };
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000389
390/// CFGBlockInfo is a struct which contains all the information that is
391/// maintained for each block in the CFG. See LocalVariableMap for more
392/// information about the contexts.
393struct CFGBlockInfo {
394 Lockset EntrySet; // Lockset held at entry to block
395 Lockset ExitSet; // Lockset held at exit from block
396 LocalVarContext EntryContext; // Context held at entry to block
397 LocalVarContext ExitContext; // Context held at exit from block
Richard Smith2e515622012-02-03 04:45:26 +0000398 SourceLocation EntryLoc; // Location of first statement in block
399 SourceLocation ExitLoc; // Location of last statement in block.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000400 unsigned EntryIndex; // Used to replay contexts later
401
Richard Smith2e515622012-02-03 04:45:26 +0000402 const Lockset &getSet(CFGBlockSide Side) const {
403 return Side == CBS_Entry ? EntrySet : ExitSet;
404 }
405 SourceLocation getLocation(CFGBlockSide Side) const {
406 return Side == CBS_Entry ? EntryLoc : ExitLoc;
407 }
408
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000409private:
410 CFGBlockInfo(Lockset EmptySet, LocalVarContext EmptyCtx)
411 : EntrySet(EmptySet), ExitSet(EmptySet),
412 EntryContext(EmptyCtx), ExitContext(EmptyCtx)
413 { }
414
415public:
416 static CFGBlockInfo getEmptyBlockInfo(Lockset::Factory &F,
417 LocalVariableMap &M);
418};
419
420
421
422// A LocalVariableMap maintains a map from local variables to their currently
423// valid definitions. It provides SSA-like functionality when traversing the
424// CFG. Like SSA, each definition or assignment to a variable is assigned a
425// unique name (an integer), which acts as the SSA name for that definition.
426// The total set of names is shared among all CFG basic blocks.
427// Unlike SSA, we do not rewrite expressions to replace local variables declrefs
428// with their SSA-names. Instead, we compute a Context for each point in the
429// code, which maps local variables to the appropriate SSA-name. This map
430// changes with each assignment.
431//
432// The map is computed in a single pass over the CFG. Subsequent analyses can
433// then query the map to find the appropriate Context for a statement, and use
434// that Context to look up the definitions of variables.
435class LocalVariableMap {
436public:
437 typedef LocalVarContext Context;
438
439 /// A VarDefinition consists of an expression, representing the value of the
440 /// variable, along with the context in which that expression should be
441 /// interpreted. A reference VarDefinition does not itself contain this
442 /// information, but instead contains a pointer to a previous VarDefinition.
443 struct VarDefinition {
444 public:
445 friend class LocalVariableMap;
446
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000447 const NamedDecl *Dec; // The original declaration for this variable.
448 const Expr *Exp; // The expression for this variable, OR
449 unsigned Ref; // Reference to another VarDefinition
450 Context Ctx; // The map with which Exp should be interpreted.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000451
452 bool isReference() { return !Exp; }
453
454 private:
455 // Create ordinary variable definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000456 VarDefinition(const NamedDecl *D, const Expr *E, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000457 : Dec(D), Exp(E), Ref(0), Ctx(C)
458 { }
459
460 // Create reference to previous definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000461 VarDefinition(const NamedDecl *D, unsigned R, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000462 : Dec(D), Exp(0), Ref(R), Ctx(C)
463 { }
464 };
465
466private:
467 Context::Factory ContextFactory;
468 std::vector<VarDefinition> VarDefinitions;
469 std::vector<unsigned> CtxIndices;
470 std::vector<std::pair<Stmt*, Context> > SavedContexts;
471
472public:
473 LocalVariableMap() {
474 // index 0 is a placeholder for undefined variables (aka phi-nodes).
475 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
476 }
477
478 /// Look up a definition, within the given context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000479 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000480 const unsigned *i = Ctx.lookup(D);
481 if (!i)
482 return 0;
483 assert(*i < VarDefinitions.size());
484 return &VarDefinitions[*i];
485 }
486
487 /// Look up the definition for D within the given context. Returns
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000488 /// NULL if the expression is not statically known. If successful, also
489 /// modifies Ctx to hold the context of the return Expr.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000490 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000491 const unsigned *P = Ctx.lookup(D);
492 if (!P)
493 return 0;
494
495 unsigned i = *P;
496 while (i > 0) {
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000497 if (VarDefinitions[i].Exp) {
498 Ctx = VarDefinitions[i].Ctx;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000499 return VarDefinitions[i].Exp;
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000500 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000501 i = VarDefinitions[i].Ref;
502 }
503 return 0;
504 }
505
506 Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
507
508 /// Return the next context after processing S. This function is used by
509 /// clients of the class to get the appropriate context when traversing the
510 /// CFG. It must be called for every assignment or DeclStmt.
511 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
512 if (SavedContexts[CtxIndex+1].first == S) {
513 CtxIndex++;
514 Context Result = SavedContexts[CtxIndex].second;
515 return Result;
516 }
517 return C;
518 }
519
520 void dumpVarDefinitionName(unsigned i) {
521 if (i == 0) {
522 llvm::errs() << "Undefined";
523 return;
524 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000525 const NamedDecl *Dec = VarDefinitions[i].Dec;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000526 if (!Dec) {
527 llvm::errs() << "<<NULL>>";
528 return;
529 }
530 Dec->printName(llvm::errs());
531 llvm::errs() << "." << i << " " << ((void*) Dec);
532 }
533
534 /// Dumps an ASCII representation of the variable map to llvm::errs()
535 void dump() {
536 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000537 const Expr *Exp = VarDefinitions[i].Exp;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000538 unsigned Ref = VarDefinitions[i].Ref;
539
540 dumpVarDefinitionName(i);
541 llvm::errs() << " = ";
542 if (Exp) Exp->dump();
543 else {
544 dumpVarDefinitionName(Ref);
545 llvm::errs() << "\n";
546 }
547 }
548 }
549
550 /// Dumps an ASCII representation of a Context to llvm::errs()
551 void dumpContext(Context C) {
552 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000553 const NamedDecl *D = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000554 D->printName(llvm::errs());
555 const unsigned *i = C.lookup(D);
556 llvm::errs() << " -> ";
557 dumpVarDefinitionName(*i);
558 llvm::errs() << "\n";
559 }
560 }
561
562 /// Builds the variable map.
563 void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
564 std::vector<CFGBlockInfo> &BlockInfo);
565
566protected:
567 // Get the current context index
568 unsigned getContextIndex() { return SavedContexts.size()-1; }
569
570 // Save the current context for later replay
571 void saveContext(Stmt *S, Context C) {
572 SavedContexts.push_back(std::make_pair(S,C));
573 }
574
575 // Adds a new definition to the given context, and returns a new context.
576 // This method should be called when declaring a new variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000577 Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000578 assert(!Ctx.contains(D));
579 unsigned newID = VarDefinitions.size();
580 Context NewCtx = ContextFactory.add(Ctx, D, newID);
581 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
582 return NewCtx;
583 }
584
585 // Add a new reference to an existing definition.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000586 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000587 unsigned newID = VarDefinitions.size();
588 Context NewCtx = ContextFactory.add(Ctx, D, newID);
589 VarDefinitions.push_back(VarDefinition(D, i, Ctx));
590 return NewCtx;
591 }
592
593 // Updates a definition only if that definition is already in the map.
594 // This method should be called when assigning to an existing variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000595 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000596 if (Ctx.contains(D)) {
597 unsigned newID = VarDefinitions.size();
598 Context NewCtx = ContextFactory.remove(Ctx, D);
599 NewCtx = ContextFactory.add(NewCtx, D, newID);
600 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
601 return NewCtx;
602 }
603 return Ctx;
604 }
605
606 // Removes a definition from the context, but keeps the variable name
607 // as a valid variable. The index 0 is a placeholder for cleared definitions.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000608 Context clearDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000609 Context NewCtx = Ctx;
610 if (NewCtx.contains(D)) {
611 NewCtx = ContextFactory.remove(NewCtx, D);
612 NewCtx = ContextFactory.add(NewCtx, D, 0);
613 }
614 return NewCtx;
615 }
616
617 // Remove a definition entirely frmo the context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000618 Context removeDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000619 Context NewCtx = Ctx;
620 if (NewCtx.contains(D)) {
621 NewCtx = ContextFactory.remove(NewCtx, D);
622 }
623 return NewCtx;
624 }
625
626 Context intersectContexts(Context C1, Context C2);
627 Context createReferenceContext(Context C);
628 void intersectBackEdge(Context C1, Context C2);
629
630 friend class VarMapBuilder;
631};
632
633
634// This has to be defined after LocalVariableMap.
635CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(Lockset::Factory &F,
636 LocalVariableMap &M) {
637 return CFGBlockInfo(F.getEmptyMap(), M.getEmptyContext());
638}
639
640
641/// Visitor which builds a LocalVariableMap
642class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
643public:
644 LocalVariableMap* VMap;
645 LocalVariableMap::Context Ctx;
646
647 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
648 : VMap(VM), Ctx(C) {}
649
650 void VisitDeclStmt(DeclStmt *S);
651 void VisitBinaryOperator(BinaryOperator *BO);
652};
653
654
655// Add new local variables to the variable map
656void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
657 bool modifiedCtx = false;
658 DeclGroupRef DGrp = S->getDeclGroup();
659 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
660 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
661 Expr *E = VD->getInit();
662
663 // Add local variables with trivial type to the variable map
664 QualType T = VD->getType();
665 if (T.isTrivialType(VD->getASTContext())) {
666 Ctx = VMap->addDefinition(VD, E, Ctx);
667 modifiedCtx = true;
668 }
669 }
670 }
671 if (modifiedCtx)
672 VMap->saveContext(S, Ctx);
673}
674
675// Update local variable definitions in variable map
676void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
677 if (!BO->isAssignmentOp())
678 return;
679
680 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
681
682 // Update the variable map and current context.
683 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
684 ValueDecl *VDec = DRE->getDecl();
685 if (Ctx.lookup(VDec)) {
686 if (BO->getOpcode() == BO_Assign)
687 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
688 else
689 // FIXME -- handle compound assignment operators
690 Ctx = VMap->clearDefinition(VDec, Ctx);
691 VMap->saveContext(BO, Ctx);
692 }
693 }
694}
695
696
697// Computes the intersection of two contexts. The intersection is the
698// set of variables which have the same definition in both contexts;
699// variables with different definitions are discarded.
700LocalVariableMap::Context
701LocalVariableMap::intersectContexts(Context C1, Context C2) {
702 Context Result = C1;
703 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000704 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000705 unsigned i1 = I.getData();
706 const unsigned *i2 = C2.lookup(Dec);
707 if (!i2) // variable doesn't exist on second path
708 Result = removeDefinition(Dec, Result);
709 else if (*i2 != i1) // variable exists, but has different definition
710 Result = clearDefinition(Dec, Result);
711 }
712 return Result;
713}
714
715// For every variable in C, create a new variable that refers to the
716// definition in C. Return a new context that contains these new variables.
717// (We use this for a naive implementation of SSA on loop back-edges.)
718LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
719 Context Result = getEmptyContext();
720 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000721 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000722 unsigned i = I.getData();
723 Result = addReference(Dec, i, Result);
724 }
725 return Result;
726}
727
728// This routine also takes the intersection of C1 and C2, but it does so by
729// altering the VarDefinitions. C1 must be the result of an earlier call to
730// createReferenceContext.
731void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
732 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000733 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000734 unsigned i1 = I.getData();
735 VarDefinition *VDef = &VarDefinitions[i1];
736 assert(VDef->isReference());
737
738 const unsigned *i2 = C2.lookup(Dec);
739 if (!i2 || (*i2 != i1))
740 VDef->Ref = 0; // Mark this variable as undefined
741 }
742}
743
744
745// Traverse the CFG in topological order, so all predecessors of a block
746// (excluding back-edges) are visited before the block itself. At
747// each point in the code, we calculate a Context, which holds the set of
748// variable definitions which are visible at that point in execution.
749// Visible variables are mapped to their definitions using an array that
750// contains all definitions.
751//
752// At join points in the CFG, the set is computed as the intersection of
753// the incoming sets along each edge, E.g.
754//
755// { Context | VarDefinitions }
756// int x = 0; { x -> x1 | x1 = 0 }
757// int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
758// if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
759// else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
760// ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
761//
762// This is essentially a simpler and more naive version of the standard SSA
763// algorithm. Those definitions that remain in the intersection are from blocks
764// that strictly dominate the current block. We do not bother to insert proper
765// phi nodes, because they are not used in our analysis; instead, wherever
766// a phi node would be required, we simply remove that definition from the
767// context (E.g. x above).
768//
769// The initial traversal does not capture back-edges, so those need to be
770// handled on a separate pass. Whenever the first pass encounters an
771// incoming back edge, it duplicates the context, creating new definitions
772// that refer back to the originals. (These correspond to places where SSA
773// might have to insert a phi node.) On the second pass, these definitions are
774// set to NULL if the the variable has changed on the back-edge (i.e. a phi
775// node was actually required.) E.g.
776//
777// { Context | VarDefinitions }
778// int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
779// while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
780// x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
781// ... { y -> y1 | x3 = 2, x2 = 1, ... }
782//
783void LocalVariableMap::traverseCFG(CFG *CFGraph,
784 PostOrderCFGView *SortedGraph,
785 std::vector<CFGBlockInfo> &BlockInfo) {
786 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
787
788 CtxIndices.resize(CFGraph->getNumBlockIDs());
789
790 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
791 E = SortedGraph->end(); I!= E; ++I) {
792 const CFGBlock *CurrBlock = *I;
793 int CurrBlockID = CurrBlock->getBlockID();
794 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
795
796 VisitedBlocks.insert(CurrBlock);
797
798 // Calculate the entry context for the current block
799 bool HasBackEdges = false;
800 bool CtxInit = true;
801 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
802 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
803 // if *PI -> CurrBlock is a back edge, so skip it
804 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
805 HasBackEdges = true;
806 continue;
807 }
808
809 int PrevBlockID = (*PI)->getBlockID();
810 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
811
812 if (CtxInit) {
813 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
814 CtxInit = false;
815 }
816 else {
817 CurrBlockInfo->EntryContext =
818 intersectContexts(CurrBlockInfo->EntryContext,
819 PrevBlockInfo->ExitContext);
820 }
821 }
822
823 // Duplicate the context if we have back-edges, so we can call
824 // intersectBackEdges later.
825 if (HasBackEdges)
826 CurrBlockInfo->EntryContext =
827 createReferenceContext(CurrBlockInfo->EntryContext);
828
829 // Create a starting context index for the current block
830 saveContext(0, CurrBlockInfo->EntryContext);
831 CurrBlockInfo->EntryIndex = getContextIndex();
832
833 // Visit all the statements in the basic block.
834 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
835 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
836 BE = CurrBlock->end(); BI != BE; ++BI) {
837 switch (BI->getKind()) {
838 case CFGElement::Statement: {
839 const CFGStmt *CS = cast<CFGStmt>(&*BI);
840 VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
841 break;
842 }
843 default:
844 break;
845 }
846 }
847 CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
848
849 // Mark variables on back edges as "unknown" if they've been changed.
850 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
851 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
852 // if CurrBlock -> *SI is *not* a back edge
853 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
854 continue;
855
856 CFGBlock *FirstLoopBlock = *SI;
857 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
858 Context LoopEnd = CurrBlockInfo->ExitContext;
859 intersectBackEdge(LoopBegin, LoopEnd);
860 }
861 }
862
863 // Put an extra entry at the end of the indexed context array
864 unsigned exitID = CFGraph->getExit().getBlockID();
865 saveContext(0, BlockInfo[exitID].ExitContext);
866}
867
Richard Smith2e515622012-02-03 04:45:26 +0000868/// Find the appropriate source locations to use when producing diagnostics for
869/// each block in the CFG.
870static void findBlockLocations(CFG *CFGraph,
871 PostOrderCFGView *SortedGraph,
872 std::vector<CFGBlockInfo> &BlockInfo) {
873 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
874 E = SortedGraph->end(); I!= E; ++I) {
875 const CFGBlock *CurrBlock = *I;
876 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
877
878 // Find the source location of the last statement in the block, if the
879 // block is not empty.
880 if (const Stmt *S = CurrBlock->getTerminator()) {
881 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
882 } else {
883 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
884 BE = CurrBlock->rend(); BI != BE; ++BI) {
885 // FIXME: Handle other CFGElement kinds.
886 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
887 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
888 break;
889 }
890 }
891 }
892
893 if (!CurrBlockInfo->ExitLoc.isInvalid()) {
894 // This block contains at least one statement. Find the source location
895 // of the first statement in the block.
896 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
897 BE = CurrBlock->end(); BI != BE; ++BI) {
898 // FIXME: Handle other CFGElement kinds.
899 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
900 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
901 break;
902 }
903 }
904 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
905 CurrBlock != &CFGraph->getExit()) {
906 // The block is empty, and has a single predecessor. Use its exit
907 // location.
908 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
909 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
910 }
911 }
912}
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000913
914/// \brief Class which implements the core thread safety analysis routines.
915class ThreadSafetyAnalyzer {
916 friend class BuildLockset;
917
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000918 ThreadSafetyHandler &Handler;
919 Lockset::Factory LocksetFactory;
920 LocalVariableMap LocalVarMap;
921 std::vector<CFGBlockInfo> BlockInfo;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000922
923public:
924 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
925
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000926 Lockset addLock(const Lockset &LSet, const MutexID &Mutex,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000927 const LockData &LDat, bool Warn=true);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000928 Lockset addLock(const Lockset &LSet, Expr *MutexExp, const NamedDecl *D,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000929 const LockData &LDat, bool Warn=true);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000930 Lockset removeLock(const Lockset &LSet, const MutexID &Mutex,
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000931 SourceLocation UnlockLoc,
932 bool Warn=true, bool FullyRemove=false);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000933
934 template <class AttrType>
935 Lockset addLocksToSet(const Lockset &LSet, LockKind LK, AttrType *Attr,
936 Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
937 Lockset removeLocksFromSet(const Lockset &LSet,
938 UnlockFunctionAttr *Attr,
939 Expr *Exp, NamedDecl* FunDecl);
940
941 template <class AttrType>
942 Lockset addTrylock(const Lockset &LSet,
943 LockKind LK, AttrType *Attr, Expr *Exp, NamedDecl *FunDecl,
944 const CFGBlock* PredBlock, const CFGBlock *CurrBlock,
945 Expr *BrE, bool Neg);
946 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
947 bool &Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000948
DeLesley Hutchins0da44142012-06-22 17:07:28 +0000949 Lockset getEdgeLockset(const Lockset &ExitSet,
950 const CFGBlock* PredBlock,
951 const CFGBlock *CurrBlock);
952
953 Lockset intersectAndWarn(const Lockset &LSet1, const Lockset &LSet2,
DeLesley Hutchins879a4332012-07-02 22:16:54 +0000954 SourceLocation JoinLoc,
955 LockErrorKind LEK1, LockErrorKind LEK2);
956
957 Lockset intersectAndWarn(const Lockset &LSet1, const Lockset &LSet2,
958 SourceLocation JoinLoc, LockErrorKind LEK1) {
959 return intersectAndWarn(LSet1, LSet2, JoinLoc, LEK1, LEK1);
960 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000961
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000962 void runAnalysis(AnalysisDeclContext &AC);
963};
964
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000965
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000966/// \brief Add a new lock to the lockset, warning if the lock is already there.
967/// \param Mutex -- the Mutex expression for the lock
968/// \param LDat -- the LockData for the lock
969Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet,
970 const MutexID &Mutex,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000971 const LockData &LDat,
972 bool Warn) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000973 // FIXME: deal with acquired before/after annotations.
974 // FIXME: Don't always warn when we have support for reentrant locks.
975 if (LSet.lookup(Mutex)) {
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000976 if (Warn)
977 Handler.handleDoubleLock(Mutex.getName(), LDat.AcquireLoc);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000978 return LSet;
979 } else {
980 return LocksetFactory.add(LSet, Mutex, LDat);
981 }
982}
983
984/// \brief Construct a new mutex and add it to the lockset.
985Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet,
986 Expr *MutexExp, const NamedDecl *D,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000987 const LockData &LDat,
988 bool Warn) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000989 MutexID Mutex(MutexExp, 0, D);
990 if (!Mutex.isValid()) {
991 MutexID::warnInvalidLock(Handler, MutexExp, 0, D);
992 return LSet;
993 }
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +0000994 return addLock(LSet, Mutex, LDat, Warn);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000995}
996
997
998/// \brief Remove a lock from the lockset, warning if the lock is not there.
999/// \param LockExp The lock expression corresponding to the lock to be removed
1000/// \param UnlockLoc The source location of the unlock (only used in error msg)
1001Lockset ThreadSafetyAnalyzer::removeLock(const Lockset &LSet,
1002 const MutexID &Mutex,
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001003 SourceLocation UnlockLoc,
1004 bool Warn, bool FullyRemove) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001005 const LockData *LDat = LSet.lookup(Mutex);
1006 if (!LDat) {
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001007 if (Warn)
1008 Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001009 return LSet;
1010 }
1011 else {
1012 Lockset Result = LSet;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001013 if (LDat->UnderlyingMutex.isValid()) {
1014 // For scoped-lockable vars, remove the mutex associated with this var.
1015 Result = removeLock(Result, LDat->UnderlyingMutex, UnlockLoc,
1016 false, true);
1017 // Fully remove the object only when the destructor is called
1018 if (FullyRemove)
1019 return LocksetFactory.remove(Result, Mutex);
1020 else
1021 return Result;
1022 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001023 return LocksetFactory.remove(Result, Mutex);
1024 }
1025}
1026
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001027
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001028/// \brief This function, parameterized by an attribute type, is used to add a
1029/// set of locks specified as attribute arguments to the lockset.
1030template <typename AttrType>
1031Lockset ThreadSafetyAnalyzer::addLocksToSet(const Lockset &LSet,
1032 LockKind LK, AttrType *Attr,
1033 Expr *Exp, NamedDecl* FunDecl,
1034 VarDecl *VD) {
1035 typedef typename AttrType::args_iterator iterator_type;
1036
1037 SourceLocation ExpLocation = Exp->getExprLoc();
1038
1039 // Figure out if we're calling the constructor of scoped lockable class
1040 bool isScopedVar = false;
1041 if (VD) {
1042 if (CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FunDecl)) {
1043 CXXRecordDecl* PD = CD->getParent();
1044 if (PD && PD->getAttr<ScopedLockableAttr>())
1045 isScopedVar = true;
1046 }
1047 }
1048
1049 if (Attr->args_size() == 0) {
1050 // The mutex held is the "this" object.
1051 MutexID Mutex(0, Exp, FunDecl);
1052 if (!Mutex.isValid()) {
1053 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
1054 return LSet;
1055 }
1056 else {
1057 return addLock(LSet, Mutex, LockData(ExpLocation, LK));
1058 }
1059 }
1060
1061 Lockset Result = LSet;
1062 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
1063 MutexID Mutex(*I, Exp, FunDecl);
1064 if (!Mutex.isValid())
1065 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
1066 else {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001067 if (isScopedVar) {
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001068 // Mutex is managed by scoped var -- suppress certain warnings.
1069 Result = addLock(Result, Mutex, LockData(ExpLocation, LK, true));
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001070 // For scoped lockable vars, map this var to its underlying mutex.
1071 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
1072 MutexID SMutex(&DRE, 0, 0);
1073 Result = addLock(Result, SMutex,
1074 LockData(VD->getLocation(), LK, Mutex));
1075 }
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001076 else {
1077 Result = addLock(Result, Mutex, LockData(ExpLocation, LK));
1078 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001079 }
1080 }
1081 return Result;
1082}
1083
1084/// \brief This function removes a set of locks specified as attribute
1085/// arguments from the lockset.
1086Lockset ThreadSafetyAnalyzer::removeLocksFromSet(const Lockset &LSet,
1087 UnlockFunctionAttr *Attr,
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001088 Expr *Exp,
1089 NamedDecl* FunDecl) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001090 SourceLocation ExpLocation;
1091 if (Exp) ExpLocation = Exp->getExprLoc();
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001092 bool Dtor = isa<CXXDestructorDecl>(FunDecl);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001093
1094 if (Attr->args_size() == 0) {
1095 // The mutex held is the "this" object.
1096 MutexID Mu(0, Exp, FunDecl);
1097 if (!Mu.isValid()) {
1098 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
1099 return LSet;
1100 } else {
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001101 return removeLock(LSet, Mu, ExpLocation, true, Dtor);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001102 }
1103 }
1104
1105 Lockset Result = LSet;
1106 for (UnlockFunctionAttr::args_iterator I = Attr->args_begin(),
1107 E = Attr->args_end(); I != E; ++I) {
1108 MutexID Mutex(*I, Exp, FunDecl);
1109 if (!Mutex.isValid())
1110 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
1111 else
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001112 Result = removeLock(Result, Mutex, ExpLocation, true, Dtor);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001113 }
1114 return Result;
1115}
1116
1117
1118/// \brief Add lock to set, if the current block is in the taken branch of a
1119/// trylock.
1120template <class AttrType>
1121Lockset ThreadSafetyAnalyzer::addTrylock(const Lockset &LSet,
1122 LockKind LK, AttrType *Attr,
1123 Expr *Exp, NamedDecl *FunDecl,
1124 const CFGBlock *PredBlock,
1125 const CFGBlock *CurrBlock,
1126 Expr *BrE, bool Neg) {
1127 // Find out which branch has the lock
1128 bool branch = 0;
1129 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1130 branch = BLE->getValue();
1131 }
1132 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1133 branch = ILE->getValue().getBoolValue();
1134 }
1135 int branchnum = branch ? 0 : 1;
1136 if (Neg) branchnum = !branchnum;
1137
1138 Lockset Result = LSet;
1139 // If we've taken the trylock branch, then add the lock
1140 int i = 0;
1141 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1142 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1143 if (*SI == CurrBlock && i == branchnum) {
1144 Result = addLocksToSet(Result, LK, Attr, Exp, FunDecl, 0);
1145 }
1146 }
1147 return Result;
1148}
1149
1150
1151// If Cond can be traced back to a function call, return the call expression.
1152// The negate variable should be called with false, and will be set to true
1153// if the function call is negated, e.g. if (!mu.tryLock(...))
1154const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1155 LocalVarContext C,
1156 bool &Negate) {
1157 if (!Cond)
1158 return 0;
1159
1160 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1161 return CallExp;
1162 }
1163 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1164 return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1165 }
1166 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1167 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1168 return getTrylockCallExpr(E, C, Negate);
1169 }
1170 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1171 if (UOP->getOpcode() == UO_LNot) {
1172 Negate = !Negate;
1173 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1174 }
1175 }
1176 // FIXME -- handle && and || as well.
1177 return NULL;
1178}
1179
1180
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001181/// \brief Find the lockset that holds on the edge between PredBlock
1182/// and CurrBlock. The edge set is the exit set of PredBlock (passed
1183/// as the ExitSet parameter) plus any trylocks, which are conditionally held.
1184Lockset ThreadSafetyAnalyzer::getEdgeLockset(const Lockset &ExitSet,
1185 const CFGBlock *PredBlock,
1186 const CFGBlock *CurrBlock) {
1187 if (!PredBlock->getTerminatorCondition())
1188 return ExitSet;
1189
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001190 bool Negate = false;
1191 const Stmt *Cond = PredBlock->getTerminatorCondition();
1192 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1193 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1194
1195 CallExpr *Exp = const_cast<CallExpr*>(
1196 getTrylockCallExpr(Cond, LVarCtx, Negate));
1197 if (!Exp)
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001198 return ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001199
1200 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1201 if(!FunDecl || !FunDecl->hasAttrs())
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001202 return ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001203
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001204 Lockset Result = ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001205
1206 // If the condition is a call to a Trylock function, then grab the attributes
1207 AttrVec &ArgAttrs = FunDecl->getAttrs();
1208 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1209 Attr *Attr = ArgAttrs[i];
1210 switch (Attr->getKind()) {
1211 case attr::ExclusiveTrylockFunction: {
1212 ExclusiveTrylockFunctionAttr *A =
1213 cast<ExclusiveTrylockFunctionAttr>(Attr);
1214 Result = addTrylock(Result, LK_Exclusive, A, Exp, FunDecl,
1215 PredBlock, CurrBlock,
1216 A->getSuccessValue(), Negate);
1217 break;
1218 }
1219 case attr::SharedTrylockFunction: {
1220 SharedTrylockFunctionAttr *A =
1221 cast<SharedTrylockFunctionAttr>(Attr);
1222 Result = addTrylock(Result, LK_Shared, A, Exp, FunDecl,
1223 PredBlock, CurrBlock,
1224 A->getSuccessValue(), Negate);
1225 break;
1226 }
1227 default:
1228 break;
1229 }
1230 }
1231 return Result;
1232}
1233
1234
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001235/// \brief We use this class to visit different types of expressions in
1236/// CFGBlocks, and build up the lockset.
1237/// An expression may cause us to add or remove locks from the lockset, or else
1238/// output error messages related to missing locks.
1239/// FIXME: In future, we may be able to not inherit from a visitor.
1240class BuildLockset : public StmtVisitor<BuildLockset> {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001241 friend class ThreadSafetyAnalyzer;
1242
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001243 ThreadSafetyAnalyzer *Analyzer;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001244 Lockset LSet;
1245 LocalVariableMap::Context LVarCtx;
1246 unsigned CtxIndex;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001247
1248 // Helper functions
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001249 const ValueDecl *getValueDecl(Expr *Exp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001250
1251 void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK,
1252 Expr *MutexExp, ProtectedOperationKind POK);
1253
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001254 void checkAccess(Expr *Exp, AccessKind AK);
1255 void checkDereference(Expr *Exp, AccessKind AK);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001256 void handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001257
1258 /// \brief Returns true if the lockset contains a lock, regardless of whether
1259 /// the lock is held exclusively or shared.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001260 bool locksetContains(const MutexID &Lock) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001261 return LSet.lookup(Lock);
1262 }
1263
1264 /// \brief Returns true if the lockset contains a lock with the passed in
1265 /// locktype.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001266 bool locksetContains(const MutexID &Lock, LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001267 const LockData *LockHeld = LSet.lookup(Lock);
1268 return (LockHeld && KindRequested == LockHeld->LKind);
1269 }
1270
1271 /// \brief Returns true if the lockset contains a lock with at least the
1272 /// passed in locktype. So for example, if we pass in LK_Shared, this function
1273 /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in
1274 /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001275 bool locksetContainsAtLeast(const MutexID &Lock,
1276 LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001277 switch (KindRequested) {
1278 case LK_Shared:
1279 return locksetContains(Lock);
1280 case LK_Exclusive:
1281 return locksetContains(Lock, KindRequested);
1282 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00001283 llvm_unreachable("Unknown LockKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001284 }
1285
1286public:
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001287 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001288 : StmtVisitor<BuildLockset>(),
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001289 Analyzer(Anlzr),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001290 LSet(Info.EntrySet),
1291 LVarCtx(Info.EntryContext),
1292 CtxIndex(Info.EntryIndex)
1293 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001294
1295 void VisitUnaryOperator(UnaryOperator *UO);
1296 void VisitBinaryOperator(BinaryOperator *BO);
1297 void VisitCastExpr(CastExpr *CE);
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001298 void VisitCallExpr(CallExpr *Exp);
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001299 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001300 void VisitDeclStmt(DeclStmt *S);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001301};
1302
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001303
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001304/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1305const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
1306 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1307 return DR->getDecl();
1308
1309 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1310 return ME->getMemberDecl();
1311
1312 return 0;
1313}
1314
1315/// \brief Warn if the LSet does not contain a lock sufficient to protect access
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001316/// of at least the passed in AccessKind.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001317void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
1318 AccessKind AK, Expr *MutexExp,
1319 ProtectedOperationKind POK) {
1320 LockKind LK = getLockKindFromAccessKind(AK);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001321
1322 MutexID Mutex(MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001323 if (!Mutex.isValid())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001324 MutexID::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001325 else if (!locksetContainsAtLeast(Mutex, LK))
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001326 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK,
1327 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001328}
1329
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001330/// \brief This method identifies variable dereferences and checks pt_guarded_by
1331/// and pt_guarded_var annotations. Note that we only check these annotations
1332/// at the time a pointer is dereferenced.
1333/// FIXME: We need to check for other types of pointer dereferences
1334/// (e.g. [], ->) and deal with them here.
1335/// \param Exp An expression that has been read or written.
1336void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
1337 UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
1338 if (!UO || UO->getOpcode() != clang::UO_Deref)
1339 return;
1340 Exp = UO->getSubExpr()->IgnoreParenCasts();
1341
1342 const ValueDecl *D = getValueDecl(Exp);
1343 if(!D || !D->hasAttrs())
1344 return;
1345
1346 if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001347 Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1348 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001349
1350 const AttrVec &ArgAttrs = D->getAttrs();
1351 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1352 if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1353 warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
1354}
1355
1356/// \brief Checks guarded_by and guarded_var attributes.
1357/// Whenever we identify an access (read or write) of a DeclRefExpr or
1358/// MemberExpr, we need to check whether there are any guarded_by or
1359/// guarded_var attributes, and make sure we hold the appropriate mutexes.
1360void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
1361 const ValueDecl *D = getValueDecl(Exp);
1362 if(!D || !D->hasAttrs())
1363 return;
1364
1365 if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001366 Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1367 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001368
1369 const AttrVec &ArgAttrs = D->getAttrs();
1370 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1371 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1372 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1373}
1374
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001375/// \brief Process a function call, method call, constructor call,
1376/// or destructor call. This involves looking at the attributes on the
1377/// corresponding function/method/constructor/destructor, issuing warnings,
1378/// and updating the locksets accordingly.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001379///
1380/// FIXME: For classes annotated with one of the guarded annotations, we need
1381/// to treat const method calls as reads and non-const method calls as writes,
1382/// and check that the appropriate locks are held. Non-const method calls with
1383/// the same signature as const method calls can be also treated as reads.
1384///
1385/// FIXME: We need to also visit CallExprs to catch/check global functions.
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001386///
1387/// FIXME: Do not flag an error for member variables accessed in constructors/
1388/// destructors
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001389void BuildLockset::handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001390 AttrVec &ArgAttrs = D->getAttrs();
1391 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
1392 Attr *Attr = ArgAttrs[i];
1393 switch (Attr->getKind()) {
1394 // When we encounter an exclusive lock function, we need to add the lock
1395 // to our lockset with kind exclusive.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001396 case attr::ExclusiveLockFunction: {
1397 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001398 LSet = Analyzer->addLocksToSet(LSet, LK_Exclusive, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001399 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001400 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001401
1402 // When we encounter a shared lock function, we need to add the lock
1403 // to our lockset with kind shared.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001404 case attr::SharedLockFunction: {
1405 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001406 LSet = Analyzer->addLocksToSet(LSet, LK_Shared, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001407 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001408 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001409
1410 // When we encounter an unlock function, we need to remove unlocked
1411 // mutexes from the lockset, and flag a warning if they are not there.
1412 case attr::UnlockFunction: {
1413 UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001414 LSet = Analyzer->removeLocksFromSet(LSet, UFAttr, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001415 break;
1416 }
1417
1418 case attr::ExclusiveLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001419 ExclusiveLocksRequiredAttr *ELRAttr =
1420 cast<ExclusiveLocksRequiredAttr>(Attr);
1421
1422 for (ExclusiveLocksRequiredAttr::args_iterator
1423 I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I)
1424 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
1425 break;
1426 }
1427
1428 case attr::SharedLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001429 SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr);
1430
1431 for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(),
1432 E = SLRAttr->args_end(); I != E; ++I)
1433 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
1434 break;
1435 }
1436
1437 case attr::LocksExcluded: {
1438 LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr);
1439 for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(),
1440 E = LEAttr->args_end(); I != E; ++I) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001441 MutexID Mutex(*I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001442 if (!Mutex.isValid())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001443 MutexID::warnInvalidLock(Analyzer->Handler, *I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001444 else if (locksetContains(Mutex))
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001445 Analyzer->Handler.handleFunExcludesLock(D->getName(),
1446 Mutex.getName(),
1447 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001448 }
1449 break;
1450 }
1451
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001452 // Ignore other (non thread-safety) attributes
1453 default:
1454 break;
1455 }
1456 }
1457}
1458
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00001459
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001460/// \brief For unary operations which read and write a variable, we need to
1461/// check whether we hold any required mutexes. Reads are checked in
1462/// VisitCastExpr.
1463void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
1464 switch (UO->getOpcode()) {
1465 case clang::UO_PostDec:
1466 case clang::UO_PostInc:
1467 case clang::UO_PreDec:
1468 case clang::UO_PreInc: {
1469 Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
1470 checkAccess(SubExp, AK_Written);
1471 checkDereference(SubExp, AK_Written);
1472 break;
1473 }
1474 default:
1475 break;
1476 }
1477}
1478
1479/// For binary operations which assign to a variable (writes), we need to check
1480/// whether we hold any required mutexes.
1481/// FIXME: Deal with non-primitive types.
1482void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
1483 if (!BO->isAssignmentOp())
1484 return;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001485
1486 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001487 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001488
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001489 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1490 checkAccess(LHSExp, AK_Written);
1491 checkDereference(LHSExp, AK_Written);
1492}
1493
1494/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
1495/// need to ensure we hold any required mutexes.
1496/// FIXME: Deal with non-primitive types.
1497void BuildLockset::VisitCastExpr(CastExpr *CE) {
1498 if (CE->getCastKind() != CK_LValueToRValue)
1499 return;
1500 Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
1501 checkAccess(SubExp, AK_Read);
1502 checkDereference(SubExp, AK_Read);
1503}
1504
1505
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001506void BuildLockset::VisitCallExpr(CallExpr *Exp) {
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001507 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1508 if(!D || !D->hasAttrs())
1509 return;
1510 handleCall(Exp, D);
1511}
1512
1513void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001514 // FIXME -- only handles constructors in DeclStmt below.
1515}
1516
1517void BuildLockset::VisitDeclStmt(DeclStmt *S) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001518 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001519 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001520
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001521 DeclGroupRef DGrp = S->getDeclGroup();
1522 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1523 Decl *D = *I;
1524 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
1525 Expr *E = VD->getInit();
1526 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
1527 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
1528 if (!CtorD || !CtorD->hasAttrs())
1529 return;
1530 handleCall(CE, CtorD, VD);
1531 }
1532 }
1533 }
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001534}
1535
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001536
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001537
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001538/// \brief Compute the intersection of two locksets and issue warnings for any
1539/// locks in the symmetric difference.
1540///
1541/// This function is used at a merge point in the CFG when comparing the lockset
1542/// of each branch being merged. For example, given the following sequence:
1543/// A; if () then B; else C; D; we need to check that the lockset after B and C
1544/// are the same. In the event of a difference, we use the intersection of these
1545/// two locksets at the start of D.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001546///
1547/// \param LSet1 The first lockset.
1548/// \param LSet2 The second lockset.
1549/// \param JoinLoc The location of the join point for error reporting
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001550/// \param LEK1 The error message to report if a mutex is missing from LSet1
1551/// \param LEK2 The error message to report if a mutex is missing from Lset2
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001552Lockset ThreadSafetyAnalyzer::intersectAndWarn(const Lockset &LSet1,
1553 const Lockset &LSet2,
1554 SourceLocation JoinLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001555 LockErrorKind LEK1,
1556 LockErrorKind LEK2) {
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001557 Lockset Intersection = LSet1;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001558
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001559 for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) {
1560 const MutexID &LSet2Mutex = I.getKey();
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001561 const LockData &LDat2 = I.getData();
1562 if (const LockData *LDat1 = LSet1.lookup(LSet2Mutex)) {
1563 if (LDat1->LKind != LDat2.LKind) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001564 Handler.handleExclusiveAndShared(LSet2Mutex.getName(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001565 LDat2.AcquireLoc,
1566 LDat1->AcquireLoc);
1567 if (LDat1->LKind != LK_Exclusive)
1568 Intersection = LocksetFactory.add(Intersection, LSet2Mutex, LDat2);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001569 }
1570 } else {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001571 if (LDat2.UnderlyingMutex.isValid()) {
1572 if (LSet2.lookup(LDat2.UnderlyingMutex)) {
1573 // If this is a scoped lock that manages another mutex, and if the
1574 // underlying mutex is still held, then warn about the underlying
1575 // mutex.
1576 Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.getName(),
1577 LDat2.AcquireLoc,
1578 JoinLoc, LEK1);
1579 }
1580 }
1581 else if (!LDat2.Managed)
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001582 Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001583 LDat2.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001584 JoinLoc, LEK1);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001585 }
1586 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001587
1588 for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) {
1589 if (!LSet2.contains(I.getKey())) {
1590 const MutexID &Mutex = I.getKey();
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001591 const LockData &LDat1 = I.getData();
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001592
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001593 if (LDat1.UnderlyingMutex.isValid()) {
1594 if (LSet1.lookup(LDat1.UnderlyingMutex)) {
1595 // If this is a scoped lock that manages another mutex, and if the
1596 // underlying mutex is still held, then warn about the underlying
1597 // mutex.
1598 Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.getName(),
1599 LDat1.AcquireLoc,
1600 JoinLoc, LEK1);
1601 }
1602 }
1603 else if (!LDat1.Managed)
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001604 Handler.handleMutexHeldEndOfScope(Mutex.getName(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00001605 LDat1.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001606 JoinLoc, LEK2);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001607 Intersection = LocksetFactory.remove(Intersection, Mutex);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001608 }
1609 }
1610 return Intersection;
1611}
1612
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001613
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001614/// \brief Check a function's CFG for thread-safety violations.
1615///
1616/// We traverse the blocks in the CFG, compute the set of mutexes that are held
1617/// at the end of each block, and issue warnings for thread safety violations.
1618/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00001619void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001620 CFG *CFGraph = AC.getCFG();
1621 if (!CFGraph) return;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001622 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
1623
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001624 // AC.dumpCFG(true);
1625
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001626 if (!D)
1627 return; // Ignore anonymous functions for now.
1628 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
1629 return;
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001630 // FIXME: Do something a bit more intelligent inside constructor and
1631 // destructor code. Constructors and destructors must assume unique access
1632 // to 'this', so checks on member variable access is disabled, but we should
1633 // still enable checks on other objects.
1634 if (isa<CXXConstructorDecl>(D))
1635 return; // Don't check inside constructors.
1636 if (isa<CXXDestructorDecl>(D))
1637 return; // Don't check inside destructors.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001638
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001639 BlockInfo.resize(CFGraph->getNumBlockIDs(),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001640 CFGBlockInfo::getEmptyBlockInfo(LocksetFactory, LocalVarMap));
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001641
1642 // We need to explore the CFG via a "topological" ordering.
1643 // That way, we will be guaranteed to have information about required
1644 // predecessor locksets when exploring a new block.
Ted Kremenek439ed162011-10-22 02:14:27 +00001645 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
1646 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001647
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001648 // Compute SSA names for local variables
1649 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
1650
Richard Smith2e515622012-02-03 04:45:26 +00001651 // Fill in source locations for all CFGBlocks.
1652 findBlockLocations(CFGraph, SortedGraph, BlockInfo);
1653
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001654 // Add locks from exclusive_locks_required and shared_locks_required
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001655 // to initial lockset. Also turn off checking for lock and unlock functions.
1656 // FIXME: is there a more intelligent way to check lock/unlock functions?
Ted Kremenek439ed162011-10-22 02:14:27 +00001657 if (!SortedGraph->empty() && D->hasAttrs()) {
1658 const CFGBlock *FirstBlock = *SortedGraph->begin();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001659 Lockset &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001660 const AttrVec &ArgAttrs = D->getAttrs();
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001661 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001662 Attr *Attr = ArgAttrs[i];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001663 SourceLocation AttrLoc = Attr->getLocation();
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001664 if (SharedLocksRequiredAttr *SLRAttr
1665 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
1666 for (SharedLocksRequiredAttr::args_iterator
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001667 SLRIter = SLRAttr->args_begin(),
1668 SLREnd = SLRAttr->args_end(); SLRIter != SLREnd; ++SLRIter)
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001669 InitialLockset = addLock(InitialLockset, *SLRIter, D,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +00001670 LockData(AttrLoc, LK_Shared), false);
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001671 } else if (ExclusiveLocksRequiredAttr *ELRAttr
1672 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
1673 for (ExclusiveLocksRequiredAttr::args_iterator
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001674 ELRIter = ELRAttr->args_begin(),
1675 ELREnd = ELRAttr->args_end(); ELRIter != ELREnd; ++ELRIter)
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001676 InitialLockset = addLock(InitialLockset, *ELRIter, D,
DeLesley Hutchinsc36eda12012-07-02 22:12:12 +00001677 LockData(AttrLoc, LK_Exclusive), false);
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001678 } else if (isa<UnlockFunctionAttr>(Attr)) {
1679 // Don't try to check unlock functions for now
1680 return;
1681 } else if (isa<ExclusiveLockFunctionAttr>(Attr)) {
1682 // Don't try to check lock functions for now
1683 return;
1684 } else if (isa<SharedLockFunctionAttr>(Attr)) {
1685 // Don't try to check lock functions for now
1686 return;
DeLesley Hutchins76f0a6e2012-07-02 21:59:24 +00001687 } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
1688 // Don't try to check trylock functions for now
1689 return;
1690 } else if (isa<SharedTrylockFunctionAttr>(Attr)) {
1691 // Don't try to check trylock functions for now
1692 return;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001693 }
1694 }
1695 }
1696
Ted Kremenek439ed162011-10-22 02:14:27 +00001697 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1698 E = SortedGraph->end(); I!= E; ++I) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001699 const CFGBlock *CurrBlock = *I;
1700 int CurrBlockID = CurrBlock->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001701 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001702
1703 // Use the default initial lockset in case there are no predecessors.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001704 VisitedBlocks.insert(CurrBlock);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001705
1706 // Iterate through the predecessor blocks and warn if the lockset for all
1707 // predecessors is not the same. We take the entry lockset of the current
1708 // block to be the intersection of all previous locksets.
1709 // FIXME: By keeping the intersection, we may output more errors in future
1710 // for a lock which is not in the intersection, but was in the union. We
1711 // may want to also keep the union in future. As an example, let's say
1712 // the intersection contains Mutex L, and the union contains L and M.
1713 // Later we unlock M. At this point, we would output an error because we
1714 // never locked M; although the real error is probably that we forgot to
1715 // lock M on all code paths. Conversely, let's say that later we lock M.
1716 // In this case, we should compare against the intersection instead of the
1717 // union because the real error is probably that we forgot to unlock M on
1718 // all code paths.
1719 bool LocksetInitialized = false;
Richard Smithaacde712012-02-03 03:30:07 +00001720 llvm::SmallVector<CFGBlock*, 8> SpecialBlocks;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001721 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1722 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
1723
1724 // if *PI -> CurrBlock is a back edge
1725 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
1726 continue;
1727
DeLesley Hutchins2a35be82012-03-02 22:02:58 +00001728 // Ignore edges from blocks that can't return.
1729 if ((*PI)->hasNoReturnElement())
1730 continue;
1731
Richard Smithaacde712012-02-03 03:30:07 +00001732 // If the previous block ended in a 'continue' or 'break' statement, then
1733 // a difference in locksets is probably due to a bug in that block, rather
1734 // than in some other predecessor. In that case, keep the other
1735 // predecessor's lockset.
1736 if (const Stmt *Terminator = (*PI)->getTerminator()) {
1737 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
1738 SpecialBlocks.push_back(*PI);
1739 continue;
1740 }
1741 }
1742
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001743 int PrevBlockID = (*PI)->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001744 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001745 Lockset PrevLockset =
1746 getEdgeLockset(PrevBlockInfo->ExitSet, *PI, CurrBlock);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001747
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001748 if (!LocksetInitialized) {
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001749 CurrBlockInfo->EntrySet = PrevLockset;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001750 LocksetInitialized = true;
1751 } else {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001752 CurrBlockInfo->EntrySet =
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001753 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
1754 CurrBlockInfo->EntryLoc,
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001755 LEK_LockedSomePredecessors);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001756 }
1757 }
1758
Richard Smithaacde712012-02-03 03:30:07 +00001759 // Process continue and break blocks. Assume that the lockset for the
1760 // resulting block is unaffected by any discrepancies in them.
1761 for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
1762 SpecialI < SpecialN; ++SpecialI) {
1763 CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
1764 int PrevBlockID = PrevBlock->getBlockID();
1765 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1766
1767 if (!LocksetInitialized) {
1768 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
1769 LocksetInitialized = true;
1770 } else {
1771 // Determine whether this edge is a loop terminator for diagnostic
1772 // purposes. FIXME: A 'break' statement might be a loop terminator, but
1773 // it might also be part of a switch. Also, a subsequent destructor
1774 // might add to the lockset, in which case the real issue might be a
1775 // double lock on the other path.
1776 const Stmt *Terminator = PrevBlock->getTerminator();
1777 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
1778
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001779 Lockset PrevLockset =
1780 getEdgeLockset(PrevBlockInfo->ExitSet, PrevBlock, CurrBlock);
1781
Richard Smithaacde712012-02-03 03:30:07 +00001782 // Do not update EntrySet.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001783 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
1784 PrevBlockInfo->ExitLoc,
Richard Smithaacde712012-02-03 03:30:07 +00001785 IsLoop ? LEK_LockedSomeLoopIterations
1786 : LEK_LockedSomePredecessors);
1787 }
1788 }
1789
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001790 BuildLockset LocksetBuilder(this, *CurrBlockInfo);
1791
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001792 // Visit all the statements in the basic block.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001793 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1794 BE = CurrBlock->end(); BI != BE; ++BI) {
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00001795 switch (BI->getKind()) {
1796 case CFGElement::Statement: {
1797 const CFGStmt *CS = cast<CFGStmt>(&*BI);
1798 LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
1799 break;
1800 }
1801 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
1802 case CFGElement::AutomaticObjectDtor: {
1803 const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
1804 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
1805 AD->getDestructorDecl(AC.getASTContext()));
1806 if (!DD->hasAttrs())
1807 break;
1808
1809 // Create a dummy expression,
1810 VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
John McCallf4b88a42012-03-10 09:33:50 +00001811 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00001812 AD->getTriggerStmt()->getLocEnd());
1813 LocksetBuilder.handleCall(&DRE, DD);
1814 break;
1815 }
1816 default:
1817 break;
1818 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001819 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001820 CurrBlockInfo->ExitSet = LocksetBuilder.LSet;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001821
1822 // For every back edge from CurrBlock (the end of the loop) to another block
1823 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
1824 // the one held at the beginning of FirstLoopBlock. We can look up the
1825 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
1826 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1827 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1828
1829 // if CurrBlock -> *SI is *not* a back edge
1830 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1831 continue;
1832
1833 CFGBlock *FirstLoopBlock = *SI;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001834 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
1835 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
1836 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
1837 PreLoop->EntryLoc,
Richard Smith2e515622012-02-03 04:45:26 +00001838 LEK_LockedSomeLoopIterations);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001839 }
1840 }
1841
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001842 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
1843 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001844
1845 // FIXME: Should we call this function for all blocks which exit the function?
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001846 intersectAndWarn(Initial->EntrySet, Final->ExitSet,
1847 Final->ExitLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001848 LEK_LockedAtEndOfFunction,
1849 LEK_NotLockedAtEndOfFunction);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001850}
1851
1852} // end anonymous namespace
1853
1854
1855namespace clang {
1856namespace thread_safety {
1857
1858/// \brief Check a function's CFG for thread-safety violations.
1859///
1860/// We traverse the blocks in the CFG, compute the set of mutexes that are held
1861/// at the end of each block, and issue warnings for thread safety violations.
1862/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00001863void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001864 ThreadSafetyHandler &Handler) {
1865 ThreadSafetyAnalyzer Analyzer(Handler);
1866 Analyzer.runAnalysis(AC);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001867}
1868
1869/// \brief Helper function that returns a LockKind required for the given level
1870/// of access.
1871LockKind getLockKindFromAccessKind(AccessKind AK) {
1872 switch (AK) {
1873 case AK_Read :
1874 return LK_Shared;
1875 case AK_Written :
1876 return LK_Exclusive;
1877 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00001878 llvm_unreachable("Unknown AccessKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001879}
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001880
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001881}} // end namespace clang::thread_safety