blob: 2668de1b05088af89172261d65c4b2328f78f92d [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 Hutchins1fa3c062011-12-08 20:23:06 +0000352 MutexID UnderlyingMutex; // for ScopedLockable objects
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000353
354 LockData(SourceLocation AcquireLoc, LockKind LKind)
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000355 : AcquireLoc(AcquireLoc), LKind(LKind), UnderlyingMutex(Decl::EmptyShell())
356 {}
357
358 LockData(SourceLocation AcquireLoc, LockKind LKind, const MutexID &Mu)
359 : AcquireLoc(AcquireLoc), LKind(LKind), UnderlyingMutex(Mu) {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000360
361 bool operator==(const LockData &other) const {
362 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
363 }
364
365 bool operator!=(const LockData &other) const {
366 return !(*this == other);
367 }
368
369 void Profile(llvm::FoldingSetNodeID &ID) const {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000370 ID.AddInteger(AcquireLoc.getRawEncoding());
371 ID.AddInteger(LKind);
372 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000373};
374
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000375
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000376/// A Lockset maps each MutexID (defined above) to information about how it has
377/// been locked.
378typedef llvm::ImmutableMap<MutexID, LockData> Lockset;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000379typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000380
381class LocalVariableMap;
382
Richard Smith2e515622012-02-03 04:45:26 +0000383/// A side (entry or exit) of a CFG node.
384enum CFGBlockSide { CBS_Entry, CBS_Exit };
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000385
386/// CFGBlockInfo is a struct which contains all the information that is
387/// maintained for each block in the CFG. See LocalVariableMap for more
388/// information about the contexts.
389struct CFGBlockInfo {
390 Lockset EntrySet; // Lockset held at entry to block
391 Lockset ExitSet; // Lockset held at exit from block
392 LocalVarContext EntryContext; // Context held at entry to block
393 LocalVarContext ExitContext; // Context held at exit from block
Richard Smith2e515622012-02-03 04:45:26 +0000394 SourceLocation EntryLoc; // Location of first statement in block
395 SourceLocation ExitLoc; // Location of last statement in block.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000396 unsigned EntryIndex; // Used to replay contexts later
397
Richard Smith2e515622012-02-03 04:45:26 +0000398 const Lockset &getSet(CFGBlockSide Side) const {
399 return Side == CBS_Entry ? EntrySet : ExitSet;
400 }
401 SourceLocation getLocation(CFGBlockSide Side) const {
402 return Side == CBS_Entry ? EntryLoc : ExitLoc;
403 }
404
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000405private:
406 CFGBlockInfo(Lockset EmptySet, LocalVarContext EmptyCtx)
407 : EntrySet(EmptySet), ExitSet(EmptySet),
408 EntryContext(EmptyCtx), ExitContext(EmptyCtx)
409 { }
410
411public:
412 static CFGBlockInfo getEmptyBlockInfo(Lockset::Factory &F,
413 LocalVariableMap &M);
414};
415
416
417
418// A LocalVariableMap maintains a map from local variables to their currently
419// valid definitions. It provides SSA-like functionality when traversing the
420// CFG. Like SSA, each definition or assignment to a variable is assigned a
421// unique name (an integer), which acts as the SSA name for that definition.
422// The total set of names is shared among all CFG basic blocks.
423// Unlike SSA, we do not rewrite expressions to replace local variables declrefs
424// with their SSA-names. Instead, we compute a Context for each point in the
425// code, which maps local variables to the appropriate SSA-name. This map
426// changes with each assignment.
427//
428// The map is computed in a single pass over the CFG. Subsequent analyses can
429// then query the map to find the appropriate Context for a statement, and use
430// that Context to look up the definitions of variables.
431class LocalVariableMap {
432public:
433 typedef LocalVarContext Context;
434
435 /// A VarDefinition consists of an expression, representing the value of the
436 /// variable, along with the context in which that expression should be
437 /// interpreted. A reference VarDefinition does not itself contain this
438 /// information, but instead contains a pointer to a previous VarDefinition.
439 struct VarDefinition {
440 public:
441 friend class LocalVariableMap;
442
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000443 const NamedDecl *Dec; // The original declaration for this variable.
444 const Expr *Exp; // The expression for this variable, OR
445 unsigned Ref; // Reference to another VarDefinition
446 Context Ctx; // The map with which Exp should be interpreted.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000447
448 bool isReference() { return !Exp; }
449
450 private:
451 // Create ordinary variable definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000452 VarDefinition(const NamedDecl *D, const Expr *E, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000453 : Dec(D), Exp(E), Ref(0), Ctx(C)
454 { }
455
456 // Create reference to previous definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000457 VarDefinition(const NamedDecl *D, unsigned R, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000458 : Dec(D), Exp(0), Ref(R), Ctx(C)
459 { }
460 };
461
462private:
463 Context::Factory ContextFactory;
464 std::vector<VarDefinition> VarDefinitions;
465 std::vector<unsigned> CtxIndices;
466 std::vector<std::pair<Stmt*, Context> > SavedContexts;
467
468public:
469 LocalVariableMap() {
470 // index 0 is a placeholder for undefined variables (aka phi-nodes).
471 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
472 }
473
474 /// Look up a definition, within the given context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000475 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000476 const unsigned *i = Ctx.lookup(D);
477 if (!i)
478 return 0;
479 assert(*i < VarDefinitions.size());
480 return &VarDefinitions[*i];
481 }
482
483 /// Look up the definition for D within the given context. Returns
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000484 /// NULL if the expression is not statically known. If successful, also
485 /// modifies Ctx to hold the context of the return Expr.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000486 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000487 const unsigned *P = Ctx.lookup(D);
488 if (!P)
489 return 0;
490
491 unsigned i = *P;
492 while (i > 0) {
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000493 if (VarDefinitions[i].Exp) {
494 Ctx = VarDefinitions[i].Ctx;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000495 return VarDefinitions[i].Exp;
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000496 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000497 i = VarDefinitions[i].Ref;
498 }
499 return 0;
500 }
501
502 Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
503
504 /// Return the next context after processing S. This function is used by
505 /// clients of the class to get the appropriate context when traversing the
506 /// CFG. It must be called for every assignment or DeclStmt.
507 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
508 if (SavedContexts[CtxIndex+1].first == S) {
509 CtxIndex++;
510 Context Result = SavedContexts[CtxIndex].second;
511 return Result;
512 }
513 return C;
514 }
515
516 void dumpVarDefinitionName(unsigned i) {
517 if (i == 0) {
518 llvm::errs() << "Undefined";
519 return;
520 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000521 const NamedDecl *Dec = VarDefinitions[i].Dec;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000522 if (!Dec) {
523 llvm::errs() << "<<NULL>>";
524 return;
525 }
526 Dec->printName(llvm::errs());
527 llvm::errs() << "." << i << " " << ((void*) Dec);
528 }
529
530 /// Dumps an ASCII representation of the variable map to llvm::errs()
531 void dump() {
532 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000533 const Expr *Exp = VarDefinitions[i].Exp;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000534 unsigned Ref = VarDefinitions[i].Ref;
535
536 dumpVarDefinitionName(i);
537 llvm::errs() << " = ";
538 if (Exp) Exp->dump();
539 else {
540 dumpVarDefinitionName(Ref);
541 llvm::errs() << "\n";
542 }
543 }
544 }
545
546 /// Dumps an ASCII representation of a Context to llvm::errs()
547 void dumpContext(Context C) {
548 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000549 const NamedDecl *D = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000550 D->printName(llvm::errs());
551 const unsigned *i = C.lookup(D);
552 llvm::errs() << " -> ";
553 dumpVarDefinitionName(*i);
554 llvm::errs() << "\n";
555 }
556 }
557
558 /// Builds the variable map.
559 void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
560 std::vector<CFGBlockInfo> &BlockInfo);
561
562protected:
563 // Get the current context index
564 unsigned getContextIndex() { return SavedContexts.size()-1; }
565
566 // Save the current context for later replay
567 void saveContext(Stmt *S, Context C) {
568 SavedContexts.push_back(std::make_pair(S,C));
569 }
570
571 // Adds a new definition to the given context, and returns a new context.
572 // This method should be called when declaring a new variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000573 Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000574 assert(!Ctx.contains(D));
575 unsigned newID = VarDefinitions.size();
576 Context NewCtx = ContextFactory.add(Ctx, D, newID);
577 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
578 return NewCtx;
579 }
580
581 // Add a new reference to an existing definition.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000582 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000583 unsigned newID = VarDefinitions.size();
584 Context NewCtx = ContextFactory.add(Ctx, D, newID);
585 VarDefinitions.push_back(VarDefinition(D, i, Ctx));
586 return NewCtx;
587 }
588
589 // Updates a definition only if that definition is already in the map.
590 // This method should be called when assigning to an existing variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000591 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000592 if (Ctx.contains(D)) {
593 unsigned newID = VarDefinitions.size();
594 Context NewCtx = ContextFactory.remove(Ctx, D);
595 NewCtx = ContextFactory.add(NewCtx, D, newID);
596 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
597 return NewCtx;
598 }
599 return Ctx;
600 }
601
602 // Removes a definition from the context, but keeps the variable name
603 // as a valid variable. The index 0 is a placeholder for cleared definitions.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000604 Context clearDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000605 Context NewCtx = Ctx;
606 if (NewCtx.contains(D)) {
607 NewCtx = ContextFactory.remove(NewCtx, D);
608 NewCtx = ContextFactory.add(NewCtx, D, 0);
609 }
610 return NewCtx;
611 }
612
613 // Remove a definition entirely frmo the context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000614 Context removeDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000615 Context NewCtx = Ctx;
616 if (NewCtx.contains(D)) {
617 NewCtx = ContextFactory.remove(NewCtx, D);
618 }
619 return NewCtx;
620 }
621
622 Context intersectContexts(Context C1, Context C2);
623 Context createReferenceContext(Context C);
624 void intersectBackEdge(Context C1, Context C2);
625
626 friend class VarMapBuilder;
627};
628
629
630// This has to be defined after LocalVariableMap.
631CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(Lockset::Factory &F,
632 LocalVariableMap &M) {
633 return CFGBlockInfo(F.getEmptyMap(), M.getEmptyContext());
634}
635
636
637/// Visitor which builds a LocalVariableMap
638class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
639public:
640 LocalVariableMap* VMap;
641 LocalVariableMap::Context Ctx;
642
643 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
644 : VMap(VM), Ctx(C) {}
645
646 void VisitDeclStmt(DeclStmt *S);
647 void VisitBinaryOperator(BinaryOperator *BO);
648};
649
650
651// Add new local variables to the variable map
652void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
653 bool modifiedCtx = false;
654 DeclGroupRef DGrp = S->getDeclGroup();
655 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
656 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
657 Expr *E = VD->getInit();
658
659 // Add local variables with trivial type to the variable map
660 QualType T = VD->getType();
661 if (T.isTrivialType(VD->getASTContext())) {
662 Ctx = VMap->addDefinition(VD, E, Ctx);
663 modifiedCtx = true;
664 }
665 }
666 }
667 if (modifiedCtx)
668 VMap->saveContext(S, Ctx);
669}
670
671// Update local variable definitions in variable map
672void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
673 if (!BO->isAssignmentOp())
674 return;
675
676 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
677
678 // Update the variable map and current context.
679 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
680 ValueDecl *VDec = DRE->getDecl();
681 if (Ctx.lookup(VDec)) {
682 if (BO->getOpcode() == BO_Assign)
683 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
684 else
685 // FIXME -- handle compound assignment operators
686 Ctx = VMap->clearDefinition(VDec, Ctx);
687 VMap->saveContext(BO, Ctx);
688 }
689 }
690}
691
692
693// Computes the intersection of two contexts. The intersection is the
694// set of variables which have the same definition in both contexts;
695// variables with different definitions are discarded.
696LocalVariableMap::Context
697LocalVariableMap::intersectContexts(Context C1, Context C2) {
698 Context Result = C1;
699 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000700 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000701 unsigned i1 = I.getData();
702 const unsigned *i2 = C2.lookup(Dec);
703 if (!i2) // variable doesn't exist on second path
704 Result = removeDefinition(Dec, Result);
705 else if (*i2 != i1) // variable exists, but has different definition
706 Result = clearDefinition(Dec, Result);
707 }
708 return Result;
709}
710
711// For every variable in C, create a new variable that refers to the
712// definition in C. Return a new context that contains these new variables.
713// (We use this for a naive implementation of SSA on loop back-edges.)
714LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
715 Context Result = getEmptyContext();
716 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000717 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000718 unsigned i = I.getData();
719 Result = addReference(Dec, i, Result);
720 }
721 return Result;
722}
723
724// This routine also takes the intersection of C1 and C2, but it does so by
725// altering the VarDefinitions. C1 must be the result of an earlier call to
726// createReferenceContext.
727void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
728 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000729 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000730 unsigned i1 = I.getData();
731 VarDefinition *VDef = &VarDefinitions[i1];
732 assert(VDef->isReference());
733
734 const unsigned *i2 = C2.lookup(Dec);
735 if (!i2 || (*i2 != i1))
736 VDef->Ref = 0; // Mark this variable as undefined
737 }
738}
739
740
741// Traverse the CFG in topological order, so all predecessors of a block
742// (excluding back-edges) are visited before the block itself. At
743// each point in the code, we calculate a Context, which holds the set of
744// variable definitions which are visible at that point in execution.
745// Visible variables are mapped to their definitions using an array that
746// contains all definitions.
747//
748// At join points in the CFG, the set is computed as the intersection of
749// the incoming sets along each edge, E.g.
750//
751// { Context | VarDefinitions }
752// int x = 0; { x -> x1 | x1 = 0 }
753// int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
754// if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
755// else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
756// ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
757//
758// This is essentially a simpler and more naive version of the standard SSA
759// algorithm. Those definitions that remain in the intersection are from blocks
760// that strictly dominate the current block. We do not bother to insert proper
761// phi nodes, because they are not used in our analysis; instead, wherever
762// a phi node would be required, we simply remove that definition from the
763// context (E.g. x above).
764//
765// The initial traversal does not capture back-edges, so those need to be
766// handled on a separate pass. Whenever the first pass encounters an
767// incoming back edge, it duplicates the context, creating new definitions
768// that refer back to the originals. (These correspond to places where SSA
769// might have to insert a phi node.) On the second pass, these definitions are
770// set to NULL if the the variable has changed on the back-edge (i.e. a phi
771// node was actually required.) E.g.
772//
773// { Context | VarDefinitions }
774// int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
775// while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
776// x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
777// ... { y -> y1 | x3 = 2, x2 = 1, ... }
778//
779void LocalVariableMap::traverseCFG(CFG *CFGraph,
780 PostOrderCFGView *SortedGraph,
781 std::vector<CFGBlockInfo> &BlockInfo) {
782 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
783
784 CtxIndices.resize(CFGraph->getNumBlockIDs());
785
786 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
787 E = SortedGraph->end(); I!= E; ++I) {
788 const CFGBlock *CurrBlock = *I;
789 int CurrBlockID = CurrBlock->getBlockID();
790 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
791
792 VisitedBlocks.insert(CurrBlock);
793
794 // Calculate the entry context for the current block
795 bool HasBackEdges = false;
796 bool CtxInit = true;
797 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
798 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
799 // if *PI -> CurrBlock is a back edge, so skip it
800 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
801 HasBackEdges = true;
802 continue;
803 }
804
805 int PrevBlockID = (*PI)->getBlockID();
806 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
807
808 if (CtxInit) {
809 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
810 CtxInit = false;
811 }
812 else {
813 CurrBlockInfo->EntryContext =
814 intersectContexts(CurrBlockInfo->EntryContext,
815 PrevBlockInfo->ExitContext);
816 }
817 }
818
819 // Duplicate the context if we have back-edges, so we can call
820 // intersectBackEdges later.
821 if (HasBackEdges)
822 CurrBlockInfo->EntryContext =
823 createReferenceContext(CurrBlockInfo->EntryContext);
824
825 // Create a starting context index for the current block
826 saveContext(0, CurrBlockInfo->EntryContext);
827 CurrBlockInfo->EntryIndex = getContextIndex();
828
829 // Visit all the statements in the basic block.
830 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
831 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
832 BE = CurrBlock->end(); BI != BE; ++BI) {
833 switch (BI->getKind()) {
834 case CFGElement::Statement: {
835 const CFGStmt *CS = cast<CFGStmt>(&*BI);
836 VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
837 break;
838 }
839 default:
840 break;
841 }
842 }
843 CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
844
845 // Mark variables on back edges as "unknown" if they've been changed.
846 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
847 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
848 // if CurrBlock -> *SI is *not* a back edge
849 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
850 continue;
851
852 CFGBlock *FirstLoopBlock = *SI;
853 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
854 Context LoopEnd = CurrBlockInfo->ExitContext;
855 intersectBackEdge(LoopBegin, LoopEnd);
856 }
857 }
858
859 // Put an extra entry at the end of the indexed context array
860 unsigned exitID = CFGraph->getExit().getBlockID();
861 saveContext(0, BlockInfo[exitID].ExitContext);
862}
863
Richard Smith2e515622012-02-03 04:45:26 +0000864/// Find the appropriate source locations to use when producing diagnostics for
865/// each block in the CFG.
866static void findBlockLocations(CFG *CFGraph,
867 PostOrderCFGView *SortedGraph,
868 std::vector<CFGBlockInfo> &BlockInfo) {
869 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
870 E = SortedGraph->end(); I!= E; ++I) {
871 const CFGBlock *CurrBlock = *I;
872 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
873
874 // Find the source location of the last statement in the block, if the
875 // block is not empty.
876 if (const Stmt *S = CurrBlock->getTerminator()) {
877 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
878 } else {
879 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
880 BE = CurrBlock->rend(); BI != BE; ++BI) {
881 // FIXME: Handle other CFGElement kinds.
882 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
883 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
884 break;
885 }
886 }
887 }
888
889 if (!CurrBlockInfo->ExitLoc.isInvalid()) {
890 // This block contains at least one statement. Find the source location
891 // of the first statement in the block.
892 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
893 BE = CurrBlock->end(); BI != BE; ++BI) {
894 // FIXME: Handle other CFGElement kinds.
895 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
896 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
897 break;
898 }
899 }
900 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
901 CurrBlock != &CFGraph->getExit()) {
902 // The block is empty, and has a single predecessor. Use its exit
903 // location.
904 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
905 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
906 }
907 }
908}
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000909
910/// \brief Class which implements the core thread safety analysis routines.
911class ThreadSafetyAnalyzer {
912 friend class BuildLockset;
913
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000914 ThreadSafetyHandler &Handler;
915 Lockset::Factory LocksetFactory;
916 LocalVariableMap LocalVarMap;
917 std::vector<CFGBlockInfo> BlockInfo;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000918
919public:
920 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
921
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000922 Lockset addLock(const Lockset &LSet, const MutexID &Mutex,
923 const LockData &LDat);
924 Lockset addLock(const Lockset &LSet, Expr *MutexExp, const NamedDecl *D,
925 const LockData &LDat);
926 Lockset removeLock(const Lockset &LSet, const MutexID &Mutex,
927 SourceLocation UnlockLoc);
928
929 template <class AttrType>
930 Lockset addLocksToSet(const Lockset &LSet, LockKind LK, AttrType *Attr,
931 Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
932 Lockset removeLocksFromSet(const Lockset &LSet,
933 UnlockFunctionAttr *Attr,
934 Expr *Exp, NamedDecl* FunDecl);
935
936 template <class AttrType>
937 Lockset addTrylock(const Lockset &LSet,
938 LockKind LK, AttrType *Attr, Expr *Exp, NamedDecl *FunDecl,
939 const CFGBlock* PredBlock, const CFGBlock *CurrBlock,
940 Expr *BrE, bool Neg);
941 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
942 bool &Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000943
DeLesley Hutchins0da44142012-06-22 17:07:28 +0000944 Lockset getEdgeLockset(const Lockset &ExitSet,
945 const CFGBlock* PredBlock,
946 const CFGBlock *CurrBlock);
947
948 Lockset intersectAndWarn(const Lockset &LSet1, const Lockset &LSet2,
949 SourceLocation JoinLoc, LockErrorKind LEK);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000950
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000951 void runAnalysis(AnalysisDeclContext &AC);
952};
953
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000954
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000955/// \brief Add a new lock to the lockset, warning if the lock is already there.
956/// \param Mutex -- the Mutex expression for the lock
957/// \param LDat -- the LockData for the lock
958Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet,
959 const MutexID &Mutex,
960 const LockData &LDat) {
961 // FIXME: deal with acquired before/after annotations.
962 // FIXME: Don't always warn when we have support for reentrant locks.
963 if (LSet.lookup(Mutex)) {
964 Handler.handleDoubleLock(Mutex.getName(), LDat.AcquireLoc);
965 return LSet;
966 } else {
967 return LocksetFactory.add(LSet, Mutex, LDat);
968 }
969}
970
971/// \brief Construct a new mutex and add it to the lockset.
972Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet,
973 Expr *MutexExp, const NamedDecl *D,
974 const LockData &LDat) {
975 MutexID Mutex(MutexExp, 0, D);
976 if (!Mutex.isValid()) {
977 MutexID::warnInvalidLock(Handler, MutexExp, 0, D);
978 return LSet;
979 }
980 return addLock(LSet, Mutex, LDat);
981}
982
983
984/// \brief Remove a lock from the lockset, warning if the lock is not there.
985/// \param LockExp The lock expression corresponding to the lock to be removed
986/// \param UnlockLoc The source location of the unlock (only used in error msg)
987Lockset ThreadSafetyAnalyzer::removeLock(const Lockset &LSet,
988 const MutexID &Mutex,
989 SourceLocation UnlockLoc) {
990 const LockData *LDat = LSet.lookup(Mutex);
991 if (!LDat) {
992 Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc);
993 return LSet;
994 }
995 else {
996 Lockset Result = LSet;
997 // For scoped-lockable vars, remove the mutex associated with this var.
998 if (LDat->UnderlyingMutex.isValid())
999 Result = removeLock(Result, LDat->UnderlyingMutex, UnlockLoc);
1000 return LocksetFactory.remove(Result, Mutex);
1001 }
1002}
1003
1004/// \brief This function, parameterized by an attribute type, is used to add a
1005/// set of locks specified as attribute arguments to the lockset.
1006template <typename AttrType>
1007Lockset ThreadSafetyAnalyzer::addLocksToSet(const Lockset &LSet,
1008 LockKind LK, AttrType *Attr,
1009 Expr *Exp, NamedDecl* FunDecl,
1010 VarDecl *VD) {
1011 typedef typename AttrType::args_iterator iterator_type;
1012
1013 SourceLocation ExpLocation = Exp->getExprLoc();
1014
1015 // Figure out if we're calling the constructor of scoped lockable class
1016 bool isScopedVar = false;
1017 if (VD) {
1018 if (CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FunDecl)) {
1019 CXXRecordDecl* PD = CD->getParent();
1020 if (PD && PD->getAttr<ScopedLockableAttr>())
1021 isScopedVar = true;
1022 }
1023 }
1024
1025 if (Attr->args_size() == 0) {
1026 // The mutex held is the "this" object.
1027 MutexID Mutex(0, Exp, FunDecl);
1028 if (!Mutex.isValid()) {
1029 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
1030 return LSet;
1031 }
1032 else {
1033 return addLock(LSet, Mutex, LockData(ExpLocation, LK));
1034 }
1035 }
1036
1037 Lockset Result = LSet;
1038 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
1039 MutexID Mutex(*I, Exp, FunDecl);
1040 if (!Mutex.isValid())
1041 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
1042 else {
1043 Result = addLock(Result, Mutex, LockData(ExpLocation, LK));
1044 if (isScopedVar) {
1045 // For scoped lockable vars, map this var to its underlying mutex.
1046 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
1047 MutexID SMutex(&DRE, 0, 0);
1048 Result = addLock(Result, SMutex,
1049 LockData(VD->getLocation(), LK, Mutex));
1050 }
1051 }
1052 }
1053 return Result;
1054}
1055
1056/// \brief This function removes a set of locks specified as attribute
1057/// arguments from the lockset.
1058Lockset ThreadSafetyAnalyzer::removeLocksFromSet(const Lockset &LSet,
1059 UnlockFunctionAttr *Attr,
1060 Expr *Exp, NamedDecl* FunDecl) {
1061 SourceLocation ExpLocation;
1062 if (Exp) ExpLocation = Exp->getExprLoc();
1063
1064 if (Attr->args_size() == 0) {
1065 // The mutex held is the "this" object.
1066 MutexID Mu(0, Exp, FunDecl);
1067 if (!Mu.isValid()) {
1068 MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl);
1069 return LSet;
1070 } else {
1071 return removeLock(LSet, Mu, ExpLocation);
1072 }
1073 }
1074
1075 Lockset Result = LSet;
1076 for (UnlockFunctionAttr::args_iterator I = Attr->args_begin(),
1077 E = Attr->args_end(); I != E; ++I) {
1078 MutexID Mutex(*I, Exp, FunDecl);
1079 if (!Mutex.isValid())
1080 MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl);
1081 else
1082 Result = removeLock(Result, Mutex, ExpLocation);
1083 }
1084 return Result;
1085}
1086
1087
1088/// \brief Add lock to set, if the current block is in the taken branch of a
1089/// trylock.
1090template <class AttrType>
1091Lockset ThreadSafetyAnalyzer::addTrylock(const Lockset &LSet,
1092 LockKind LK, AttrType *Attr,
1093 Expr *Exp, NamedDecl *FunDecl,
1094 const CFGBlock *PredBlock,
1095 const CFGBlock *CurrBlock,
1096 Expr *BrE, bool Neg) {
1097 // Find out which branch has the lock
1098 bool branch = 0;
1099 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1100 branch = BLE->getValue();
1101 }
1102 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1103 branch = ILE->getValue().getBoolValue();
1104 }
1105 int branchnum = branch ? 0 : 1;
1106 if (Neg) branchnum = !branchnum;
1107
1108 Lockset Result = LSet;
1109 // If we've taken the trylock branch, then add the lock
1110 int i = 0;
1111 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1112 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1113 if (*SI == CurrBlock && i == branchnum) {
1114 Result = addLocksToSet(Result, LK, Attr, Exp, FunDecl, 0);
1115 }
1116 }
1117 return Result;
1118}
1119
1120
1121// If Cond can be traced back to a function call, return the call expression.
1122// The negate variable should be called with false, and will be set to true
1123// if the function call is negated, e.g. if (!mu.tryLock(...))
1124const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1125 LocalVarContext C,
1126 bool &Negate) {
1127 if (!Cond)
1128 return 0;
1129
1130 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1131 return CallExp;
1132 }
1133 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1134 return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1135 }
1136 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1137 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1138 return getTrylockCallExpr(E, C, Negate);
1139 }
1140 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1141 if (UOP->getOpcode() == UO_LNot) {
1142 Negate = !Negate;
1143 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1144 }
1145 }
1146 // FIXME -- handle && and || as well.
1147 return NULL;
1148}
1149
1150
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001151/// \brief Find the lockset that holds on the edge between PredBlock
1152/// and CurrBlock. The edge set is the exit set of PredBlock (passed
1153/// as the ExitSet parameter) plus any trylocks, which are conditionally held.
1154Lockset ThreadSafetyAnalyzer::getEdgeLockset(const Lockset &ExitSet,
1155 const CFGBlock *PredBlock,
1156 const CFGBlock *CurrBlock) {
1157 if (!PredBlock->getTerminatorCondition())
1158 return ExitSet;
1159
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001160 bool Negate = false;
1161 const Stmt *Cond = PredBlock->getTerminatorCondition();
1162 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1163 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1164
1165 CallExpr *Exp = const_cast<CallExpr*>(
1166 getTrylockCallExpr(Cond, LVarCtx, Negate));
1167 if (!Exp)
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001168 return ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001169
1170 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1171 if(!FunDecl || !FunDecl->hasAttrs())
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001172 return ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001173
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001174 Lockset Result = ExitSet;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001175
1176 // If the condition is a call to a Trylock function, then grab the attributes
1177 AttrVec &ArgAttrs = FunDecl->getAttrs();
1178 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1179 Attr *Attr = ArgAttrs[i];
1180 switch (Attr->getKind()) {
1181 case attr::ExclusiveTrylockFunction: {
1182 ExclusiveTrylockFunctionAttr *A =
1183 cast<ExclusiveTrylockFunctionAttr>(Attr);
1184 Result = addTrylock(Result, LK_Exclusive, A, Exp, FunDecl,
1185 PredBlock, CurrBlock,
1186 A->getSuccessValue(), Negate);
1187 break;
1188 }
1189 case attr::SharedTrylockFunction: {
1190 SharedTrylockFunctionAttr *A =
1191 cast<SharedTrylockFunctionAttr>(Attr);
1192 Result = addTrylock(Result, LK_Shared, A, Exp, FunDecl,
1193 PredBlock, CurrBlock,
1194 A->getSuccessValue(), Negate);
1195 break;
1196 }
1197 default:
1198 break;
1199 }
1200 }
1201 return Result;
1202}
1203
1204
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001205/// \brief We use this class to visit different types of expressions in
1206/// CFGBlocks, and build up the lockset.
1207/// An expression may cause us to add or remove locks from the lockset, or else
1208/// output error messages related to missing locks.
1209/// FIXME: In future, we may be able to not inherit from a visitor.
1210class BuildLockset : public StmtVisitor<BuildLockset> {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001211 friend class ThreadSafetyAnalyzer;
1212
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001213 ThreadSafetyAnalyzer *Analyzer;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001214 Lockset LSet;
1215 LocalVariableMap::Context LVarCtx;
1216 unsigned CtxIndex;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001217
1218 // Helper functions
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001219 const ValueDecl *getValueDecl(Expr *Exp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001220
1221 void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK,
1222 Expr *MutexExp, ProtectedOperationKind POK);
1223
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001224 void checkAccess(Expr *Exp, AccessKind AK);
1225 void checkDereference(Expr *Exp, AccessKind AK);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001226 void handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD = 0);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001227
1228 /// \brief Returns true if the lockset contains a lock, regardless of whether
1229 /// the lock is held exclusively or shared.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001230 bool locksetContains(const MutexID &Lock) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001231 return LSet.lookup(Lock);
1232 }
1233
1234 /// \brief Returns true if the lockset contains a lock with the passed in
1235 /// locktype.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001236 bool locksetContains(const MutexID &Lock, LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001237 const LockData *LockHeld = LSet.lookup(Lock);
1238 return (LockHeld && KindRequested == LockHeld->LKind);
1239 }
1240
1241 /// \brief Returns true if the lockset contains a lock with at least the
1242 /// passed in locktype. So for example, if we pass in LK_Shared, this function
1243 /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in
1244 /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive.
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001245 bool locksetContainsAtLeast(const MutexID &Lock,
1246 LockKind KindRequested) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001247 switch (KindRequested) {
1248 case LK_Shared:
1249 return locksetContains(Lock);
1250 case LK_Exclusive:
1251 return locksetContains(Lock, KindRequested);
1252 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00001253 llvm_unreachable("Unknown LockKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001254 }
1255
1256public:
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001257 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001258 : StmtVisitor<BuildLockset>(),
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001259 Analyzer(Anlzr),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001260 LSet(Info.EntrySet),
1261 LVarCtx(Info.EntryContext),
1262 CtxIndex(Info.EntryIndex)
1263 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001264
1265 void VisitUnaryOperator(UnaryOperator *UO);
1266 void VisitBinaryOperator(BinaryOperator *BO);
1267 void VisitCastExpr(CastExpr *CE);
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001268 void VisitCallExpr(CallExpr *Exp);
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001269 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001270 void VisitDeclStmt(DeclStmt *S);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001271};
1272
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001273
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001274/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1275const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
1276 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1277 return DR->getDecl();
1278
1279 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1280 return ME->getMemberDecl();
1281
1282 return 0;
1283}
1284
1285/// \brief Warn if the LSet does not contain a lock sufficient to protect access
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001286/// of at least the passed in AccessKind.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001287void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
1288 AccessKind AK, Expr *MutexExp,
1289 ProtectedOperationKind POK) {
1290 LockKind LK = getLockKindFromAccessKind(AK);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001291
1292 MutexID Mutex(MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001293 if (!Mutex.isValid())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001294 MutexID::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001295 else if (!locksetContainsAtLeast(Mutex, LK))
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001296 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK,
1297 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001298}
1299
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001300/// \brief This method identifies variable dereferences and checks pt_guarded_by
1301/// and pt_guarded_var annotations. Note that we only check these annotations
1302/// at the time a pointer is dereferenced.
1303/// FIXME: We need to check for other types of pointer dereferences
1304/// (e.g. [], ->) and deal with them here.
1305/// \param Exp An expression that has been read or written.
1306void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
1307 UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
1308 if (!UO || UO->getOpcode() != clang::UO_Deref)
1309 return;
1310 Exp = UO->getSubExpr()->IgnoreParenCasts();
1311
1312 const ValueDecl *D = getValueDecl(Exp);
1313 if(!D || !D->hasAttrs())
1314 return;
1315
1316 if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001317 Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1318 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001319
1320 const AttrVec &ArgAttrs = D->getAttrs();
1321 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1322 if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1323 warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
1324}
1325
1326/// \brief Checks guarded_by and guarded_var attributes.
1327/// Whenever we identify an access (read or write) of a DeclRefExpr or
1328/// MemberExpr, we need to check whether there are any guarded_by or
1329/// guarded_var attributes, and make sure we hold the appropriate mutexes.
1330void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
1331 const ValueDecl *D = getValueDecl(Exp);
1332 if(!D || !D->hasAttrs())
1333 return;
1334
1335 if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001336 Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1337 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001338
1339 const AttrVec &ArgAttrs = D->getAttrs();
1340 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1341 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1342 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1343}
1344
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001345/// \brief Process a function call, method call, constructor call,
1346/// or destructor call. This involves looking at the attributes on the
1347/// corresponding function/method/constructor/destructor, issuing warnings,
1348/// and updating the locksets accordingly.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001349///
1350/// FIXME: For classes annotated with one of the guarded annotations, we need
1351/// to treat const method calls as reads and non-const method calls as writes,
1352/// and check that the appropriate locks are held. Non-const method calls with
1353/// the same signature as const method calls can be also treated as reads.
1354///
1355/// FIXME: We need to also visit CallExprs to catch/check global functions.
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001356///
1357/// FIXME: Do not flag an error for member variables accessed in constructors/
1358/// destructors
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001359void BuildLockset::handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001360 AttrVec &ArgAttrs = D->getAttrs();
1361 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
1362 Attr *Attr = ArgAttrs[i];
1363 switch (Attr->getKind()) {
1364 // When we encounter an exclusive lock function, we need to add the lock
1365 // to our lockset with kind exclusive.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001366 case attr::ExclusiveLockFunction: {
1367 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001368 LSet = Analyzer->addLocksToSet(LSet, LK_Exclusive, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001369 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001370 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001371
1372 // When we encounter a shared lock function, we need to add the lock
1373 // to our lockset with kind shared.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001374 case attr::SharedLockFunction: {
1375 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001376 LSet = Analyzer->addLocksToSet(LSet, LK_Shared, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001377 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001378 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001379
1380 // When we encounter an unlock function, we need to remove unlocked
1381 // mutexes from the lockset, and flag a warning if they are not there.
1382 case attr::UnlockFunction: {
1383 UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001384 LSet = Analyzer->removeLocksFromSet(LSet, UFAttr, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001385 break;
1386 }
1387
1388 case attr::ExclusiveLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001389 ExclusiveLocksRequiredAttr *ELRAttr =
1390 cast<ExclusiveLocksRequiredAttr>(Attr);
1391
1392 for (ExclusiveLocksRequiredAttr::args_iterator
1393 I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I)
1394 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
1395 break;
1396 }
1397
1398 case attr::SharedLocksRequired: {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001399 SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr);
1400
1401 for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(),
1402 E = SLRAttr->args_end(); I != E; ++I)
1403 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
1404 break;
1405 }
1406
1407 case attr::LocksExcluded: {
1408 LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr);
1409 for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(),
1410 E = LEAttr->args_end(); I != E; ++I) {
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001411 MutexID Mutex(*I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001412 if (!Mutex.isValid())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001413 MutexID::warnInvalidLock(Analyzer->Handler, *I, Exp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +00001414 else if (locksetContains(Mutex))
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001415 Analyzer->Handler.handleFunExcludesLock(D->getName(),
1416 Mutex.getName(),
1417 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001418 }
1419 break;
1420 }
1421
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001422 // Ignore other (non thread-safety) attributes
1423 default:
1424 break;
1425 }
1426 }
1427}
1428
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00001429
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001430/// \brief For unary operations which read and write a variable, we need to
1431/// check whether we hold any required mutexes. Reads are checked in
1432/// VisitCastExpr.
1433void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
1434 switch (UO->getOpcode()) {
1435 case clang::UO_PostDec:
1436 case clang::UO_PostInc:
1437 case clang::UO_PreDec:
1438 case clang::UO_PreInc: {
1439 Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
1440 checkAccess(SubExp, AK_Written);
1441 checkDereference(SubExp, AK_Written);
1442 break;
1443 }
1444 default:
1445 break;
1446 }
1447}
1448
1449/// For binary operations which assign to a variable (writes), we need to check
1450/// whether we hold any required mutexes.
1451/// FIXME: Deal with non-primitive types.
1452void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
1453 if (!BO->isAssignmentOp())
1454 return;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001455
1456 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001457 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001458
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001459 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1460 checkAccess(LHSExp, AK_Written);
1461 checkDereference(LHSExp, AK_Written);
1462}
1463
1464/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
1465/// need to ensure we hold any required mutexes.
1466/// FIXME: Deal with non-primitive types.
1467void BuildLockset::VisitCastExpr(CastExpr *CE) {
1468 if (CE->getCastKind() != CK_LValueToRValue)
1469 return;
1470 Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
1471 checkAccess(SubExp, AK_Read);
1472 checkDereference(SubExp, AK_Read);
1473}
1474
1475
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001476void BuildLockset::VisitCallExpr(CallExpr *Exp) {
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001477 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1478 if(!D || !D->hasAttrs())
1479 return;
1480 handleCall(Exp, D);
1481}
1482
1483void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001484 // FIXME -- only handles constructors in DeclStmt below.
1485}
1486
1487void BuildLockset::VisitDeclStmt(DeclStmt *S) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001488 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001489 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001490
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001491 DeclGroupRef DGrp = S->getDeclGroup();
1492 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1493 Decl *D = *I;
1494 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
1495 Expr *E = VD->getInit();
1496 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
1497 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
1498 if (!CtorD || !CtorD->hasAttrs())
1499 return;
1500 handleCall(CE, CtorD, VD);
1501 }
1502 }
1503 }
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001504}
1505
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001506
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001507
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001508/// \brief Compute the intersection of two locksets and issue warnings for any
1509/// locks in the symmetric difference.
1510///
1511/// This function is used at a merge point in the CFG when comparing the lockset
1512/// of each branch being merged. For example, given the following sequence:
1513/// A; if () then B; else C; D; we need to check that the lockset after B and C
1514/// are the same. In the event of a difference, we use the intersection of these
1515/// two locksets at the start of D.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001516///
1517/// \param LSet1 The first lockset.
1518/// \param LSet2 The second lockset.
1519/// \param JoinLoc The location of the join point for error reporting
1520/// \param LEK The error message to report.
1521Lockset ThreadSafetyAnalyzer::intersectAndWarn(const Lockset &LSet1,
1522 const Lockset &LSet2,
1523 SourceLocation JoinLoc,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001524 LockErrorKind LEK) {
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001525 Lockset Intersection = LSet1;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001526
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001527 for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) {
1528 const MutexID &LSet2Mutex = I.getKey();
1529 const LockData &LSet2LockData = I.getData();
1530 if (const LockData *LD = LSet1.lookup(LSet2Mutex)) {
1531 if (LD->LKind != LSet2LockData.LKind) {
1532 Handler.handleExclusiveAndShared(LSet2Mutex.getName(),
1533 LSet2LockData.AcquireLoc,
1534 LD->AcquireLoc);
1535 if (LD->LKind != LK_Exclusive)
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001536 Intersection = LocksetFactory.add(Intersection, LSet2Mutex,
1537 LSet2LockData);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001538 }
1539 } else {
1540 Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(),
Richard Smith2e515622012-02-03 04:45:26 +00001541 LSet2LockData.AcquireLoc,
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001542 JoinLoc, LEK);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001543 }
1544 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001545
1546 for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) {
1547 if (!LSet2.contains(I.getKey())) {
1548 const MutexID &Mutex = I.getKey();
1549 const LockData &MissingLock = I.getData();
1550 Handler.handleMutexHeldEndOfScope(Mutex.getName(),
Richard Smith2e515622012-02-03 04:45:26 +00001551 MissingLock.AcquireLoc,
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001552 JoinLoc, LEK);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001553 Intersection = LocksetFactory.remove(Intersection, Mutex);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001554 }
1555 }
1556 return Intersection;
1557}
1558
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001559
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001560/// \brief Check a function's CFG for thread-safety violations.
1561///
1562/// We traverse the blocks in the CFG, compute the set of mutexes that are held
1563/// at the end of each block, and issue warnings for thread safety violations.
1564/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00001565void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001566 CFG *CFGraph = AC.getCFG();
1567 if (!CFGraph) return;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001568 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
1569
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001570 // AC.dumpCFG(true);
1571
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001572 if (!D)
1573 return; // Ignore anonymous functions for now.
1574 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
1575 return;
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001576 // FIXME: Do something a bit more intelligent inside constructor and
1577 // destructor code. Constructors and destructors must assume unique access
1578 // to 'this', so checks on member variable access is disabled, but we should
1579 // still enable checks on other objects.
1580 if (isa<CXXConstructorDecl>(D))
1581 return; // Don't check inside constructors.
1582 if (isa<CXXDestructorDecl>(D))
1583 return; // Don't check inside destructors.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001584
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001585 BlockInfo.resize(CFGraph->getNumBlockIDs(),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001586 CFGBlockInfo::getEmptyBlockInfo(LocksetFactory, LocalVarMap));
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001587
1588 // We need to explore the CFG via a "topological" ordering.
1589 // That way, we will be guaranteed to have information about required
1590 // predecessor locksets when exploring a new block.
Ted Kremenek439ed162011-10-22 02:14:27 +00001591 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
1592 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001593
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001594 // Compute SSA names for local variables
1595 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
1596
Richard Smith2e515622012-02-03 04:45:26 +00001597 // Fill in source locations for all CFGBlocks.
1598 findBlockLocations(CFGraph, SortedGraph, BlockInfo);
1599
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001600 // Add locks from exclusive_locks_required and shared_locks_required
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001601 // to initial lockset. Also turn off checking for lock and unlock functions.
1602 // FIXME: is there a more intelligent way to check lock/unlock functions?
Ted Kremenek439ed162011-10-22 02:14:27 +00001603 if (!SortedGraph->empty() && D->hasAttrs()) {
1604 const CFGBlock *FirstBlock = *SortedGraph->begin();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001605 Lockset &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001606 const AttrVec &ArgAttrs = D->getAttrs();
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001607 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001608 Attr *Attr = ArgAttrs[i];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001609 SourceLocation AttrLoc = Attr->getLocation();
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001610 if (SharedLocksRequiredAttr *SLRAttr
1611 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
1612 for (SharedLocksRequiredAttr::args_iterator
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001613 SLRIter = SLRAttr->args_begin(),
1614 SLREnd = SLRAttr->args_end(); SLRIter != SLREnd; ++SLRIter)
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001615 InitialLockset = addLock(InitialLockset, *SLRIter, D,
1616 LockData(AttrLoc, LK_Shared));
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001617 } else if (ExclusiveLocksRequiredAttr *ELRAttr
1618 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
1619 for (ExclusiveLocksRequiredAttr::args_iterator
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001620 ELRIter = ELRAttr->args_begin(),
1621 ELREnd = ELRAttr->args_end(); ELRIter != ELREnd; ++ELRIter)
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001622 InitialLockset = addLock(InitialLockset, *ELRIter, D,
1623 LockData(AttrLoc, LK_Exclusive));
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00001624 } else if (isa<UnlockFunctionAttr>(Attr)) {
1625 // Don't try to check unlock functions for now
1626 return;
1627 } else if (isa<ExclusiveLockFunctionAttr>(Attr)) {
1628 // Don't try to check lock functions for now
1629 return;
1630 } else if (isa<SharedLockFunctionAttr>(Attr)) {
1631 // Don't try to check lock functions for now
1632 return;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00001633 }
1634 }
1635 }
1636
Ted Kremenek439ed162011-10-22 02:14:27 +00001637 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1638 E = SortedGraph->end(); I!= E; ++I) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001639 const CFGBlock *CurrBlock = *I;
1640 int CurrBlockID = CurrBlock->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001641 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001642
1643 // Use the default initial lockset in case there are no predecessors.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001644 VisitedBlocks.insert(CurrBlock);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001645
1646 // Iterate through the predecessor blocks and warn if the lockset for all
1647 // predecessors is not the same. We take the entry lockset of the current
1648 // block to be the intersection of all previous locksets.
1649 // FIXME: By keeping the intersection, we may output more errors in future
1650 // for a lock which is not in the intersection, but was in the union. We
1651 // may want to also keep the union in future. As an example, let's say
1652 // the intersection contains Mutex L, and the union contains L and M.
1653 // Later we unlock M. At this point, we would output an error because we
1654 // never locked M; although the real error is probably that we forgot to
1655 // lock M on all code paths. Conversely, let's say that later we lock M.
1656 // In this case, we should compare against the intersection instead of the
1657 // union because the real error is probably that we forgot to unlock M on
1658 // all code paths.
1659 bool LocksetInitialized = false;
Richard Smithaacde712012-02-03 03:30:07 +00001660 llvm::SmallVector<CFGBlock*, 8> SpecialBlocks;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001661 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1662 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
1663
1664 // if *PI -> CurrBlock is a back edge
1665 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
1666 continue;
1667
DeLesley Hutchins2a35be82012-03-02 22:02:58 +00001668 // Ignore edges from blocks that can't return.
1669 if ((*PI)->hasNoReturnElement())
1670 continue;
1671
Richard Smithaacde712012-02-03 03:30:07 +00001672 // If the previous block ended in a 'continue' or 'break' statement, then
1673 // a difference in locksets is probably due to a bug in that block, rather
1674 // than in some other predecessor. In that case, keep the other
1675 // predecessor's lockset.
1676 if (const Stmt *Terminator = (*PI)->getTerminator()) {
1677 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
1678 SpecialBlocks.push_back(*PI);
1679 continue;
1680 }
1681 }
1682
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001683 int PrevBlockID = (*PI)->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001684 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001685 Lockset PrevLockset =
1686 getEdgeLockset(PrevBlockInfo->ExitSet, *PI, CurrBlock);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001687
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001688 if (!LocksetInitialized) {
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001689 CurrBlockInfo->EntrySet = PrevLockset;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001690 LocksetInitialized = true;
1691 } else {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001692 CurrBlockInfo->EntrySet =
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001693 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
1694 CurrBlockInfo->EntryLoc,
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001695 LEK_LockedSomePredecessors);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001696 }
1697 }
1698
Richard Smithaacde712012-02-03 03:30:07 +00001699 // Process continue and break blocks. Assume that the lockset for the
1700 // resulting block is unaffected by any discrepancies in them.
1701 for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
1702 SpecialI < SpecialN; ++SpecialI) {
1703 CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
1704 int PrevBlockID = PrevBlock->getBlockID();
1705 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1706
1707 if (!LocksetInitialized) {
1708 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
1709 LocksetInitialized = true;
1710 } else {
1711 // Determine whether this edge is a loop terminator for diagnostic
1712 // purposes. FIXME: A 'break' statement might be a loop terminator, but
1713 // it might also be part of a switch. Also, a subsequent destructor
1714 // might add to the lockset, in which case the real issue might be a
1715 // double lock on the other path.
1716 const Stmt *Terminator = PrevBlock->getTerminator();
1717 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
1718
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001719 Lockset PrevLockset =
1720 getEdgeLockset(PrevBlockInfo->ExitSet, PrevBlock, CurrBlock);
1721
Richard Smithaacde712012-02-03 03:30:07 +00001722 // Do not update EntrySet.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001723 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
1724 PrevBlockInfo->ExitLoc,
Richard Smithaacde712012-02-03 03:30:07 +00001725 IsLoop ? LEK_LockedSomeLoopIterations
1726 : LEK_LockedSomePredecessors);
1727 }
1728 }
1729
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001730 BuildLockset LocksetBuilder(this, *CurrBlockInfo);
1731
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001732 // Visit all the statements in the basic block.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001733 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1734 BE = CurrBlock->end(); BI != BE; ++BI) {
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00001735 switch (BI->getKind()) {
1736 case CFGElement::Statement: {
1737 const CFGStmt *CS = cast<CFGStmt>(&*BI);
1738 LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
1739 break;
1740 }
1741 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
1742 case CFGElement::AutomaticObjectDtor: {
1743 const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
1744 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
1745 AD->getDestructorDecl(AC.getASTContext()));
1746 if (!DD->hasAttrs())
1747 break;
1748
1749 // Create a dummy expression,
1750 VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
John McCallf4b88a42012-03-10 09:33:50 +00001751 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00001752 AD->getTriggerStmt()->getLocEnd());
1753 LocksetBuilder.handleCall(&DRE, DD);
1754 break;
1755 }
1756 default:
1757 break;
1758 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001759 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001760 CurrBlockInfo->ExitSet = LocksetBuilder.LSet;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001761
1762 // For every back edge from CurrBlock (the end of the loop) to another block
1763 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
1764 // the one held at the beginning of FirstLoopBlock. We can look up the
1765 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
1766 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1767 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1768
1769 // if CurrBlock -> *SI is *not* a back edge
1770 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1771 continue;
1772
1773 CFGBlock *FirstLoopBlock = *SI;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001774 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
1775 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
1776 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
1777 PreLoop->EntryLoc,
Richard Smith2e515622012-02-03 04:45:26 +00001778 LEK_LockedSomeLoopIterations);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001779 }
1780 }
1781
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001782 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
1783 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00001784
1785 // FIXME: Should we call this function for all blocks which exit the function?
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001786 intersectAndWarn(Initial->EntrySet, Final->ExitSet,
1787 Final->ExitLoc,
Richard Smith2e515622012-02-03 04:45:26 +00001788 LEK_LockedAtEndOfFunction);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001789}
1790
1791} // end anonymous namespace
1792
1793
1794namespace clang {
1795namespace thread_safety {
1796
1797/// \brief Check a function's CFG for thread-safety violations.
1798///
1799/// We traverse the blocks in the CFG, compute the set of mutexes that are held
1800/// at the end of each block, and issue warnings for thread safety violations.
1801/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00001802void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001803 ThreadSafetyHandler &Handler) {
1804 ThreadSafetyAnalyzer Analyzer(Handler);
1805 Analyzer.runAnalysis(AC);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001806}
1807
1808/// \brief Helper function that returns a LockKind required for the given level
1809/// of access.
1810LockKind getLockKindFromAccessKind(AccessKind AK) {
1811 switch (AK) {
1812 case AK_Read :
1813 return LK_Shared;
1814 case AK_Written :
1815 return LK_Exclusive;
1816 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00001817 llvm_unreachable("Unknown AccessKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001818}
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00001819
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001820}} // end namespace clang::thread_safety