blob: c7f1f62cb57db6260aed7968ae8d768769919409 [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"
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +000029#include "clang/Basic/OperatorKinds.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000030#include "llvm/ADT/BitVector.h"
31#include "llvm/ADT/FoldingSet.h"
32#include "llvm/ADT/ImmutableMap.h"
33#include "llvm/ADT/PostOrderIterator.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringRef.h"
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000036#include "llvm/Support/raw_ostream.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000037#include <algorithm>
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000038#include <utility>
Caitlin Sadowski402aa062011-09-09 16:11:56 +000039#include <vector>
40
41using namespace clang;
42using namespace thread_safety;
43
Caitlin Sadowski19903462011-09-14 20:05:09 +000044// Key method definition
45ThreadSafetyHandler::~ThreadSafetyHandler() {}
46
Caitlin Sadowski402aa062011-09-09 16:11:56 +000047namespace {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +000048
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000049/// SExpr implements a simple expression language that is used to store,
50/// compare, and pretty-print C++ expressions. Unlike a clang Expr, a SExpr
51/// does not capture surface syntax, and it does not distinguish between
52/// C++ concepts, like pointers and references, that have no real semantic
53/// differences. This simplicity allows SExprs to be meaningfully compared,
54/// e.g.
55/// (x) = x
56/// (*this).foo = this->foo
57/// *&a = a
Caitlin Sadowski402aa062011-09-09 16:11:56 +000058///
59/// Thread-safety analysis works by comparing lock expressions. Within the
60/// body of a function, an expression such as "x->foo->bar.mu" will resolve to
61/// a particular mutex object at run-time. Subsequent occurrences of the same
62/// expression (where "same" means syntactic equality) will refer to the same
63/// run-time object if three conditions hold:
64/// (1) Local variables in the expression, such as "x" have not changed.
65/// (2) Values on the heap that affect the expression have not changed.
66/// (3) The expression involves only pure function calls.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +000067///
Caitlin Sadowski402aa062011-09-09 16:11:56 +000068/// The current implementation assumes, but does not verify, that multiple uses
69/// of the same lock expression satisfies these criteria.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000070class SExpr {
71private:
72 enum ExprOp {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +000073 EOP_Nop, ///< No-op
74 EOP_Wildcard, ///< Matches anything.
75 EOP_Universal, ///< Universal lock.
76 EOP_This, ///< This keyword.
77 EOP_NVar, ///< Named variable.
78 EOP_LVar, ///< Local variable.
79 EOP_Dot, ///< Field access
80 EOP_Call, ///< Function call
81 EOP_MCall, ///< Method call
82 EOP_Index, ///< Array index
83 EOP_Unary, ///< Unary operation
84 EOP_Binary, ///< Binary operation
85 EOP_Unknown ///< Catchall for everything else
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000086 };
87
88
89 class SExprNode {
90 private:
Ted Kremenekad0fe032012-08-22 23:50:41 +000091 unsigned char Op; ///< Opcode of the root node
92 unsigned char Flags; ///< Additional opcode-specific data
93 unsigned short Sz; ///< Number of child nodes
94 const void* Data; ///< Additional opcode-specific data
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000095
96 public:
97 SExprNode(ExprOp O, unsigned F, const void* D)
98 : Op(static_cast<unsigned char>(O)),
99 Flags(static_cast<unsigned char>(F)), Sz(1), Data(D)
100 { }
101
102 unsigned size() const { return Sz; }
103 void setSize(unsigned S) { Sz = S; }
104
105 ExprOp kind() const { return static_cast<ExprOp>(Op); }
106
107 const NamedDecl* getNamedDecl() const {
108 assert(Op == EOP_NVar || Op == EOP_LVar || Op == EOP_Dot);
109 return reinterpret_cast<const NamedDecl*>(Data);
110 }
111
112 const NamedDecl* getFunctionDecl() const {
113 assert(Op == EOP_Call || Op == EOP_MCall);
114 return reinterpret_cast<const NamedDecl*>(Data);
115 }
116
117 bool isArrow() const { return Op == EOP_Dot && Flags == 1; }
118 void setArrow(bool A) { Flags = A ? 1 : 0; }
119
120 unsigned arity() const {
121 switch (Op) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000122 case EOP_Nop: return 0;
123 case EOP_Wildcard: return 0;
124 case EOP_Universal: return 0;
125 case EOP_NVar: return 0;
126 case EOP_LVar: return 0;
127 case EOP_This: return 0;
128 case EOP_Dot: return 1;
129 case EOP_Call: return Flags+1; // First arg is function.
130 case EOP_MCall: return Flags+1; // First arg is implicit obj.
131 case EOP_Index: return 2;
132 case EOP_Unary: return 1;
133 case EOP_Binary: return 2;
134 case EOP_Unknown: return Flags;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000135 }
136 return 0;
137 }
138
139 bool operator==(const SExprNode& Other) const {
140 // Ignore flags and size -- they don't matter.
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000141 return (Op == Other.Op &&
142 Data == Other.Data);
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000143 }
144
145 bool operator!=(const SExprNode& Other) const {
146 return !(*this == Other);
147 }
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000148
149 bool matches(const SExprNode& Other) const {
150 return (*this == Other) ||
151 (Op == EOP_Wildcard) ||
152 (Other.Op == EOP_Wildcard);
153 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000154 };
155
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000156
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000157 /// \brief Encapsulates the lexical context of a function call. The lexical
158 /// context includes the arguments to the call, including the implicit object
159 /// argument. When an attribute containing a mutex expression is attached to
160 /// a method, the expression may refer to formal parameters of the method.
161 /// Actual arguments must be substituted for formal parameters to derive
162 /// the appropriate mutex expression in the lexical context where the function
163 /// is called. PrevCtx holds the context in which the arguments themselves
164 /// should be evaluated; multiple calling contexts can be chained together
165 /// by the lock_returned attribute.
166 struct CallingContext {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000167 const NamedDecl* AttrDecl; // The decl to which the attribute is attached.
168 Expr* SelfArg; // Implicit object argument -- e.g. 'this'
169 bool SelfArrow; // is Self referred to with -> or .?
170 unsigned NumArgs; // Number of funArgs
171 Expr** FunArgs; // Function arguments
172 CallingContext* PrevCtx; // The previous context; or 0 if none.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000173
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000174 CallingContext(const NamedDecl *D = 0, Expr *S = 0,
175 unsigned N = 0, Expr **A = 0, CallingContext *P = 0)
176 : AttrDecl(D), SelfArg(S), SelfArrow(false),
177 NumArgs(N), FunArgs(A), PrevCtx(P)
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000178 { }
179 };
180
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000181 typedef SmallVector<SExprNode, 4> NodeVector;
182
183private:
184 // A SExpr is a list of SExprNodes in prefix order. The Size field allows
185 // the list to be traversed as a tree.
186 NodeVector NodeVec;
187
188private:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000189 unsigned makeNop() {
190 NodeVec.push_back(SExprNode(EOP_Nop, 0, 0));
191 return NodeVec.size()-1;
192 }
193
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000194 unsigned makeWildcard() {
195 NodeVec.push_back(SExprNode(EOP_Wildcard, 0, 0));
196 return NodeVec.size()-1;
197 }
198
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000199 unsigned makeUniversal() {
200 NodeVec.push_back(SExprNode(EOP_Universal, 0, 0));
201 return NodeVec.size()-1;
202 }
203
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000204 unsigned makeNamedVar(const NamedDecl *D) {
205 NodeVec.push_back(SExprNode(EOP_NVar, 0, D));
206 return NodeVec.size()-1;
207 }
208
209 unsigned makeLocalVar(const NamedDecl *D) {
210 NodeVec.push_back(SExprNode(EOP_LVar, 0, D));
211 return NodeVec.size()-1;
212 }
213
214 unsigned makeThis() {
215 NodeVec.push_back(SExprNode(EOP_This, 0, 0));
216 return NodeVec.size()-1;
217 }
218
219 unsigned makeDot(const NamedDecl *D, bool Arrow) {
220 NodeVec.push_back(SExprNode(EOP_Dot, Arrow ? 1 : 0, D));
221 return NodeVec.size()-1;
222 }
223
224 unsigned makeCall(unsigned NumArgs, const NamedDecl *D) {
225 NodeVec.push_back(SExprNode(EOP_Call, NumArgs, D));
226 return NodeVec.size()-1;
227 }
228
DeLesley Hutchins186af2d2012-09-20 22:18:02 +0000229 // Grab the very first declaration of virtual method D
230 const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) {
231 while (true) {
232 D = D->getCanonicalDecl();
233 CXXMethodDecl::method_iterator I = D->begin_overridden_methods(),
234 E = D->end_overridden_methods();
235 if (I == E)
236 return D; // Method does not override anything
237 D = *I; // FIXME: this does not work with multiple inheritance.
238 }
239 return 0;
240 }
241
242 unsigned makeMCall(unsigned NumArgs, const CXXMethodDecl *D) {
243 NodeVec.push_back(SExprNode(EOP_MCall, NumArgs, getFirstVirtualDecl(D)));
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000244 return NodeVec.size()-1;
245 }
246
247 unsigned makeIndex() {
248 NodeVec.push_back(SExprNode(EOP_Index, 0, 0));
249 return NodeVec.size()-1;
250 }
251
252 unsigned makeUnary() {
253 NodeVec.push_back(SExprNode(EOP_Unary, 0, 0));
254 return NodeVec.size()-1;
255 }
256
257 unsigned makeBinary() {
258 NodeVec.push_back(SExprNode(EOP_Binary, 0, 0));
259 return NodeVec.size()-1;
260 }
261
262 unsigned makeUnknown(unsigned Arity) {
263 NodeVec.push_back(SExprNode(EOP_Unknown, Arity, 0));
264 return NodeVec.size()-1;
265 }
266
267 /// Build an SExpr from the given C++ expression.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000268 /// Recursive function that terminates on DeclRefExpr.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000269 /// Note: this function merely creates a SExpr; it does not check to
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000270 /// ensure that the original expression is a valid mutex expression.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000271 ///
272 /// NDeref returns the number of Derefence and AddressOf operations
273 /// preceeding the Expr; this is used to decide whether to pretty-print
274 /// SExprs with . or ->.
275 unsigned buildSExpr(Expr *Exp, CallingContext* CallCtx, int* NDeref = 0) {
276 if (!Exp)
277 return 0;
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000278
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000279 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
280 NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000281 ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND);
282 if (PV) {
283 FunctionDecl *FD =
284 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
285 unsigned i = PV->getFunctionScopeIndex();
286
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000287 if (CallCtx && CallCtx->FunArgs &&
288 FD == CallCtx->AttrDecl->getCanonicalDecl()) {
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000289 // Substitute call arguments for references to function parameters
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000290 assert(i < CallCtx->NumArgs);
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000291 return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref);
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000292 }
293 // Map the param back to the param of the original function declaration.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000294 makeNamedVar(FD->getParamDecl(i));
295 return 1;
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000296 }
297 // Not a function parameter -- just store the reference.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000298 makeNamedVar(ND);
299 return 1;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000300 } else if (isa<CXXThisExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000301 // Substitute parent for 'this'
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000302 if (CallCtx && CallCtx->SelfArg) {
303 if (!CallCtx->SelfArrow && NDeref)
304 // 'this' is a pointer, but self is not, so need to take address.
305 --(*NDeref);
306 return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref);
307 }
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000308 else {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000309 makeThis();
310 return 1;
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000311 }
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000312 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
313 NamedDecl *ND = ME->getMemberDecl();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000314 int ImplicitDeref = ME->isArrow() ? 1 : 0;
315 unsigned Root = makeDot(ND, false);
316 unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref);
317 NodeVec[Root].setArrow(ImplicitDeref > 0);
318 NodeVec[Root].setSize(Sz + 1);
319 return Sz + 1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000320 } else if (CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000321 // When calling a function with a lock_returned attribute, replace
322 // the function call with the expression in lock_returned.
DeLesley Hutchins54081532012-08-31 22:09:53 +0000323 CXXMethodDecl* MD =
324 cast<CXXMethodDecl>(CMCE->getMethodDecl()->getMostRecentDecl());
325 if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000326 CallingContext LRCallCtx(CMCE->getMethodDecl());
327 LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000328 LRCallCtx.SelfArrow =
329 dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow();
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000330 LRCallCtx.NumArgs = CMCE->getNumArgs();
331 LRCallCtx.FunArgs = CMCE->getArgs();
332 LRCallCtx.PrevCtx = CallCtx;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000333 return buildSExpr(At->getArg(), &LRCallCtx);
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000334 }
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000335 // Hack to treat smart pointers and iterators as pointers;
336 // ignore any method named get().
337 if (CMCE->getMethodDecl()->getNameAsString() == "get" &&
338 CMCE->getNumArgs() == 0) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000339 if (NDeref && dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow())
340 ++(*NDeref);
341 return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000342 }
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000343 unsigned NumCallArgs = CMCE->getNumArgs();
DeLesley Hutchins186af2d2012-09-20 22:18:02 +0000344 unsigned Root = makeMCall(NumCallArgs, CMCE->getMethodDecl());
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000345 unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000346 Expr** CallArgs = CMCE->getArgs();
347 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000348 Sz += buildSExpr(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000349 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000350 NodeVec[Root].setSize(Sz + 1);
351 return Sz + 1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000352 } else if (CallExpr *CE = dyn_cast<CallExpr>(Exp)) {
DeLesley Hutchins54081532012-08-31 22:09:53 +0000353 FunctionDecl* FD =
354 cast<FunctionDecl>(CE->getDirectCallee()->getMostRecentDecl());
355 if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000356 CallingContext LRCallCtx(CE->getDirectCallee());
357 LRCallCtx.NumArgs = CE->getNumArgs();
358 LRCallCtx.FunArgs = CE->getArgs();
359 LRCallCtx.PrevCtx = CallCtx;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000360 return buildSExpr(At->getArg(), &LRCallCtx);
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000361 }
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000362 // Treat smart pointers and iterators as pointers;
363 // ignore the * and -> operators.
364 if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) {
365 OverloadedOperatorKind k = OE->getOperator();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000366 if (k == OO_Star) {
367 if (NDeref) ++(*NDeref);
368 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
369 }
370 else if (k == OO_Arrow) {
371 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000372 }
373 }
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000374 unsigned NumCallArgs = CE->getNumArgs();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000375 unsigned Root = makeCall(NumCallArgs, 0);
376 unsigned Sz = buildSExpr(CE->getCallee(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000377 Expr** CallArgs = CE->getArgs();
378 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000379 Sz += buildSExpr(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000380 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000381 NodeVec[Root].setSize(Sz+1);
382 return Sz+1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000383 } else if (BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000384 unsigned Root = makeBinary();
385 unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx);
386 Sz += buildSExpr(BOE->getRHS(), CallCtx);
387 NodeVec[Root].setSize(Sz);
388 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000389 } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000390 // Ignore & and * operators -- they're no-ops.
391 // However, we try to figure out whether the expression is a pointer,
392 // so we can use . and -> appropriately in error messages.
393 if (UOE->getOpcode() == UO_Deref) {
394 if (NDeref) ++(*NDeref);
395 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
396 }
397 if (UOE->getOpcode() == UO_AddrOf) {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000398 if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) {
399 if (DRE->getDecl()->isCXXInstanceMember()) {
400 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
401 // We interpret this syntax specially, as a wildcard.
402 unsigned Root = makeDot(DRE->getDecl(), false);
403 makeWildcard();
404 NodeVec[Root].setSize(2);
405 return 2;
406 }
407 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000408 if (NDeref) --(*NDeref);
409 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
410 }
411 unsigned Root = makeUnary();
412 unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx);
413 NodeVec[Root].setSize(Sz);
414 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000415 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000416 unsigned Root = makeIndex();
417 unsigned Sz = buildSExpr(ASE->getBase(), CallCtx);
418 Sz += buildSExpr(ASE->getIdx(), CallCtx);
419 NodeVec[Root].setSize(Sz);
420 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000421 } else if (AbstractConditionalOperator *CE =
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000422 dyn_cast<AbstractConditionalOperator>(Exp)) {
423 unsigned Root = makeUnknown(3);
424 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
425 Sz += buildSExpr(CE->getTrueExpr(), CallCtx);
426 Sz += buildSExpr(CE->getFalseExpr(), CallCtx);
427 NodeVec[Root].setSize(Sz);
428 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000429 } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000430 unsigned Root = makeUnknown(3);
431 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
432 Sz += buildSExpr(CE->getLHS(), CallCtx);
433 Sz += buildSExpr(CE->getRHS(), CallCtx);
434 NodeVec[Root].setSize(Sz);
435 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000436 } else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000437 return buildSExpr(CE->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000438 } else if (ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000439 return buildSExpr(PE->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +0000440 } else if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000441 return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000442 } else if (CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000443 return buildSExpr(E->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000444 } else if (isa<CharacterLiteral>(Exp) ||
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +0000445 isa<CXXNullPtrLiteralExpr>(Exp) ||
446 isa<GNUNullExpr>(Exp) ||
447 isa<CXXBoolLiteralExpr>(Exp) ||
448 isa<FloatingLiteral>(Exp) ||
449 isa<ImaginaryLiteral>(Exp) ||
450 isa<IntegerLiteral>(Exp) ||
451 isa<StringLiteral>(Exp) ||
452 isa<ObjCStringLiteral>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000453 makeNop();
454 return 1; // FIXME: Ignore literals for now
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000455 } else {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000456 makeNop();
457 return 1; // Ignore. FIXME: mark as invalid expression?
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000458 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000459 }
460
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000461 /// \brief Construct a SExpr from an expression.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000462 /// \param MutexExp The original mutex expression within an attribute
463 /// \param DeclExp An expression involving the Decl on which the attribute
464 /// occurs.
465 /// \param D The declaration to which the lock/unlock attribute is attached.
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +0000466 void buildSExprFromExpr(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D,
467 VarDecl *SelfDecl = 0) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000468 CallingContext CallCtx(D);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000469
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000470 if (MutexExp) {
471 if (StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) {
472 if (SLit->getString() == StringRef("*"))
473 // The "*" expr is a universal lock, which essentially turns off
474 // checks until it is removed from the lockset.
475 makeUniversal();
476 else
477 // Ignore other string literals for now.
478 makeNop();
479 return;
480 }
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000481 }
482
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000483 // If we are processing a raw attribute expression, with no substitutions.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000484 if (DeclExp == 0) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000485 buildSExpr(MutexExp, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000486 return;
487 }
488
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000489 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000490 // for formal parameters when we call buildMutexID later.
DeLesley Hutchins81216392011-10-17 21:38:02 +0000491 if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000492 CallCtx.SelfArg = ME->getBase();
493 CallCtx.SelfArrow = ME->isArrow();
DeLesley Hutchins81216392011-10-17 21:38:02 +0000494 } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000495 CallCtx.SelfArg = CE->getImplicitObjectArgument();
496 CallCtx.SelfArrow = dyn_cast<MemberExpr>(CE->getCallee())->isArrow();
497 CallCtx.NumArgs = CE->getNumArgs();
498 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinsdf497822011-12-29 00:56:48 +0000499 } else if (CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000500 CallCtx.NumArgs = CE->getNumArgs();
501 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000502 } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +0000503 CallCtx.SelfArg = 0; // Will be set below
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000504 CallCtx.NumArgs = CE->getNumArgs();
505 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchins6db51f72011-10-21 20:51:27 +0000506 } else if (D && isa<CXXDestructorDecl>(D)) {
507 // There's no such thing as a "destructor call" in the AST.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000508 CallCtx.SelfArg = DeclExp;
DeLesley Hutchins81216392011-10-17 21:38:02 +0000509 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000510
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +0000511 // Hack to handle constructors, where self cannot be recovered from
512 // the expression.
513 if (SelfDecl && !CallCtx.SelfArg) {
514 DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue,
515 SelfDecl->getLocation());
516 CallCtx.SelfArg = &SelfDRE;
517
518 // If the attribute has no arguments, then assume the argument is "this".
519 if (MutexExp == 0)
520 buildSExpr(CallCtx.SelfArg, 0);
521 else // For most attributes.
522 buildSExpr(MutexExp, &CallCtx);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000523 return;
524 }
DeLesley Hutchins81216392011-10-17 21:38:02 +0000525
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +0000526 // If the attribute has no arguments, then assume the argument is "this".
527 if (MutexExp == 0)
528 buildSExpr(CallCtx.SelfArg, 0);
529 else // For most attributes.
530 buildSExpr(MutexExp, &CallCtx);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000531 }
532
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000533 /// \brief Get index of next sibling of node i.
534 unsigned getNextSibling(unsigned i) const {
535 return i + NodeVec[i].size();
536 }
537
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000538public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000539 explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); }
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000540
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000541 /// \param MutexExp The original mutex expression within an attribute
542 /// \param DeclExp An expression involving the Decl on which the attribute
543 /// occurs.
544 /// \param D The declaration to which the lock/unlock attribute is attached.
545 /// Caller must check isValid() after construction.
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +0000546 SExpr(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D,
547 VarDecl *SelfDecl=0) {
548 buildSExprFromExpr(MutexExp, DeclExp, D, SelfDecl);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000549 }
550
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000551 /// Return true if this is a valid decl sequence.
552 /// Caller must call this by hand after construction to handle errors.
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000553 bool isValid() const {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000554 return !NodeVec.empty();
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000555 }
556
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000557 bool shouldIgnore() const {
558 // Nop is a mutex that we have decided to deliberately ignore.
559 assert(NodeVec.size() > 0 && "Invalid Mutex");
560 return NodeVec[0].kind() == EOP_Nop;
561 }
562
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000563 bool isUniversal() const {
564 assert(NodeVec.size() > 0 && "Invalid Mutex");
565 return NodeVec[0].kind() == EOP_Universal;
566 }
567
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000568 /// Issue a warning about an invalid lock expression
569 static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp,
570 Expr *DeclExp, const NamedDecl* D) {
571 SourceLocation Loc;
572 if (DeclExp)
573 Loc = DeclExp->getExprLoc();
574
575 // FIXME: add a note about the attribute location in MutexExp or D
576 if (Loc.isValid())
577 Handler.handleInvalidLockExp(Loc);
578 }
579
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000580 bool operator==(const SExpr &other) const {
581 return NodeVec == other.NodeVec;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000582 }
583
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000584 bool operator!=(const SExpr &other) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000585 return !(*this == other);
586 }
587
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000588 bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const {
589 if (NodeVec[i].matches(Other.NodeVec[j])) {
DeLesley Hutchinsf9ee0ba2012-09-11 23:04:49 +0000590 unsigned ni = NodeVec[i].arity();
591 unsigned nj = Other.NodeVec[j].arity();
592 unsigned n = (ni < nj) ? ni : nj;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000593 bool Result = true;
594 unsigned ci = i+1; // first child of i
595 unsigned cj = j+1; // first child of j
596 for (unsigned k = 0; k < n;
597 ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) {
598 Result = Result && matches(Other, ci, cj);
599 }
600 return Result;
601 }
602 return false;
603 }
604
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +0000605 // A partial match between a.mu and b.mu returns true a and b have the same
606 // type (and thus mu refers to the same mutex declaration), regardless of
607 // whether a and b are different objects or not.
608 bool partiallyMatches(const SExpr &Other) const {
609 if (NodeVec[0].kind() == EOP_Dot)
610 return NodeVec[0].matches(Other.NodeVec[0]);
611 return false;
612 }
613
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000614 /// \brief Pretty print a lock expression for use in error messages.
615 std::string toString(unsigned i = 0) const {
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000616 assert(isValid());
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000617 if (i >= NodeVec.size())
618 return "";
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000619
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000620 const SExprNode* N = &NodeVec[i];
621 switch (N->kind()) {
622 case EOP_Nop:
623 return "_";
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000624 case EOP_Wildcard:
625 return "(?)";
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000626 case EOP_Universal:
627 return "*";
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000628 case EOP_This:
629 return "this";
630 case EOP_NVar:
631 case EOP_LVar: {
632 return N->getNamedDecl()->getNameAsString();
633 }
634 case EOP_Dot: {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000635 if (NodeVec[i+1].kind() == EOP_Wildcard) {
636 std::string S = "&";
637 S += N->getNamedDecl()->getQualifiedNameAsString();
638 return S;
639 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000640 std::string FieldName = N->getNamedDecl()->getNameAsString();
641 if (NodeVec[i+1].kind() == EOP_This)
642 return FieldName;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000643
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000644 std::string S = toString(i+1);
645 if (N->isArrow())
646 return S + "->" + FieldName;
647 else
648 return S + "." + FieldName;
649 }
650 case EOP_Call: {
651 std::string S = toString(i+1) + "(";
652 unsigned NumArgs = N->arity()-1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000653 unsigned ci = getNextSibling(i+1);
654 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000655 S += toString(ci);
656 if (k+1 < NumArgs) S += ",";
657 }
658 S += ")";
659 return S;
660 }
661 case EOP_MCall: {
662 std::string S = "";
663 if (NodeVec[i+1].kind() != EOP_This)
664 S = toString(i+1) + ".";
665 if (const NamedDecl *D = N->getFunctionDecl())
666 S += D->getNameAsString() + "(";
667 else
668 S += "#(";
669 unsigned NumArgs = N->arity()-1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000670 unsigned ci = getNextSibling(i+1);
671 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000672 S += toString(ci);
673 if (k+1 < NumArgs) S += ",";
674 }
675 S += ")";
676 return S;
677 }
678 case EOP_Index: {
679 std::string S1 = toString(i+1);
680 std::string S2 = toString(i+1 + NodeVec[i+1].size());
681 return S1 + "[" + S2 + "]";
682 }
683 case EOP_Unary: {
684 std::string S = toString(i+1);
685 return "#" + S;
686 }
687 case EOP_Binary: {
688 std::string S1 = toString(i+1);
689 std::string S2 = toString(i+1 + NodeVec[i+1].size());
690 return "(" + S1 + "#" + S2 + ")";
691 }
692 case EOP_Unknown: {
693 unsigned NumChildren = N->arity();
694 if (NumChildren == 0)
695 return "(...)";
696 std::string S = "(";
697 unsigned ci = i+1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000698 for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000699 S += toString(ci);
700 if (j+1 < NumChildren) S += "#";
701 }
702 S += ")";
703 return S;
704 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000705 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000706 return "";
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000707 }
708};
709
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000710
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000711
712/// \brief A short list of SExprs
713class MutexIDList : public SmallVector<SExpr, 3> {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000714public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000715 /// \brief Return true if the list contains the specified SExpr
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000716 /// Performs a linear search, because these lists are almost always very small.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000717 bool contains(const SExpr& M) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000718 for (iterator I=begin(),E=end(); I != E; ++I)
719 if ((*I) == M) return true;
720 return false;
721 }
722
723 /// \brief Push M onto list, bud discard duplicates
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000724 void push_back_nodup(const SExpr& M) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000725 if (!contains(M)) push_back(M);
726 }
727};
728
729
730
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000731/// \brief This is a helper class that stores info about the most recent
732/// accquire of a Lock.
733///
734/// The main body of the analysis maps MutexIDs to LockDatas.
735struct LockData {
736 SourceLocation AcquireLoc;
737
738 /// \brief LKind stores whether a lock is held shared or exclusively.
739 /// Note that this analysis does not currently support either re-entrant
740 /// locking or lock "upgrading" and "downgrading" between exclusive and
741 /// shared.
742 ///
743 /// FIXME: add support for re-entrant locking and lock up/downgrading
744 LockKind LKind;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000745 bool Managed; // for ScopedLockable objects
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000746 SExpr UnderlyingMutex; // for ScopedLockable objects
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000747
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000748 LockData(SourceLocation AcquireLoc, LockKind LKind, bool M = false)
749 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(M),
750 UnderlyingMutex(Decl::EmptyShell())
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000751 {}
752
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000753 LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu)
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000754 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(false),
755 UnderlyingMutex(Mu)
756 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000757
758 bool operator==(const LockData &other) const {
759 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
760 }
761
762 bool operator!=(const LockData &other) const {
763 return !(*this == other);
764 }
765
766 void Profile(llvm::FoldingSetNodeID &ID) const {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000767 ID.AddInteger(AcquireLoc.getRawEncoding());
768 ID.AddInteger(LKind);
769 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000770
771 bool isAtLeast(LockKind LK) {
772 return (LK == LK_Shared) || (LKind == LK_Exclusive);
773 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000774};
775
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000776
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000777/// \brief A FactEntry stores a single fact that is known at a particular point
778/// in the program execution. Currently, this is information regarding a lock
779/// that is held at that point.
780struct FactEntry {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000781 SExpr MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000782 LockData LDat;
783
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000784 FactEntry(const SExpr& M, const LockData& L)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000785 : MutID(M), LDat(L)
786 { }
787};
788
789
790typedef unsigned short FactID;
791
792/// \brief FactManager manages the memory for all facts that are created during
793/// the analysis of a single routine.
794class FactManager {
795private:
796 std::vector<FactEntry> Facts;
797
798public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000799 FactID newLock(const SExpr& M, const LockData& L) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000800 Facts.push_back(FactEntry(M,L));
801 return static_cast<unsigned short>(Facts.size() - 1);
802 }
803
804 const FactEntry& operator[](FactID F) const { return Facts[F]; }
805 FactEntry& operator[](FactID F) { return Facts[F]; }
806};
807
808
809/// \brief A FactSet is the set of facts that are known to be true at a
810/// particular program point. FactSets must be small, because they are
811/// frequently copied, and are thus implemented as a set of indices into a
812/// table maintained by a FactManager. A typical FactSet only holds 1 or 2
813/// locks, so we can get away with doing a linear search for lookup. Note
814/// that a hashtable or map is inappropriate in this case, because lookups
815/// may involve partial pattern matches, rather than exact matches.
816class FactSet {
817private:
818 typedef SmallVector<FactID, 4> FactVec;
819
820 FactVec FactIDs;
821
822public:
823 typedef FactVec::iterator iterator;
824 typedef FactVec::const_iterator const_iterator;
825
826 iterator begin() { return FactIDs.begin(); }
827 const_iterator begin() const { return FactIDs.begin(); }
828
829 iterator end() { return FactIDs.end(); }
830 const_iterator end() const { return FactIDs.end(); }
831
832 bool isEmpty() const { return FactIDs.size() == 0; }
833
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000834 FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000835 FactID F = FM.newLock(M, L);
836 FactIDs.push_back(F);
837 return F;
838 }
839
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000840 bool removeLock(FactManager& FM, const SExpr& M) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000841 unsigned n = FactIDs.size();
842 if (n == 0)
843 return false;
844
845 for (unsigned i = 0; i < n-1; ++i) {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000846 if (FM[FactIDs[i]].MutID.matches(M)) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000847 FactIDs[i] = FactIDs[n-1];
848 FactIDs.pop_back();
849 return true;
850 }
851 }
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000852 if (FM[FactIDs[n-1]].MutID.matches(M)) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000853 FactIDs.pop_back();
854 return true;
855 }
856 return false;
857 }
858
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +0000859 LockData* findLock(FactManager &FM, const SExpr &M) const {
Chad Rosier2de47702012-09-07 18:44:15 +0000860 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chad Rosier589190b2012-09-07 19:49:55 +0000861 const SExpr &Exp = FM[*I].MutID;
Chad Rosier2de47702012-09-07 18:44:15 +0000862 if (Exp.matches(M))
863 return &FM[*I].LDat;
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000864 }
865 return 0;
866 }
867
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +0000868 LockData* findLockUniv(FactManager &FM, const SExpr &M) const {
Chad Rosier2de47702012-09-07 18:44:15 +0000869 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chad Rosier589190b2012-09-07 19:49:55 +0000870 const SExpr &Exp = FM[*I].MutID;
Chad Rosier2de47702012-09-07 18:44:15 +0000871 if (Exp.matches(M) || Exp.isUniversal())
872 return &FM[*I].LDat;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000873 }
874 return 0;
875 }
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +0000876
877 FactEntry* findPartialMatch(FactManager &FM, const SExpr &M) const {
878 for (const_iterator I=begin(), E=end(); I != E; ++I) {
879 const SExpr& Exp = FM[*I].MutID;
880 if (Exp.partiallyMatches(M)) return &FM[*I];
881 }
882 return 0;
883 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000884};
885
886
887
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000888/// A Lockset maps each SExpr (defined above) to information about how it has
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000889/// been locked.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000890typedef llvm::ImmutableMap<SExpr, LockData> Lockset;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000891typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000892
893class LocalVariableMap;
894
Richard Smith2e515622012-02-03 04:45:26 +0000895/// A side (entry or exit) of a CFG node.
896enum CFGBlockSide { CBS_Entry, CBS_Exit };
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000897
898/// CFGBlockInfo is a struct which contains all the information that is
899/// maintained for each block in the CFG. See LocalVariableMap for more
900/// information about the contexts.
901struct CFGBlockInfo {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000902 FactSet EntrySet; // Lockset held at entry to block
903 FactSet ExitSet; // Lockset held at exit from block
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000904 LocalVarContext EntryContext; // Context held at entry to block
905 LocalVarContext ExitContext; // Context held at exit from block
Richard Smith2e515622012-02-03 04:45:26 +0000906 SourceLocation EntryLoc; // Location of first statement in block
907 SourceLocation ExitLoc; // Location of last statement in block.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000908 unsigned EntryIndex; // Used to replay contexts later
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +0000909 bool Reachable; // Is this block reachable?
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000910
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000911 const FactSet &getSet(CFGBlockSide Side) const {
Richard Smith2e515622012-02-03 04:45:26 +0000912 return Side == CBS_Entry ? EntrySet : ExitSet;
913 }
914 SourceLocation getLocation(CFGBlockSide Side) const {
915 return Side == CBS_Entry ? EntryLoc : ExitLoc;
916 }
917
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000918private:
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000919 CFGBlockInfo(LocalVarContext EmptyCtx)
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +0000920 : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000921 { }
922
923public:
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000924 static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000925};
926
927
928
929// A LocalVariableMap maintains a map from local variables to their currently
930// valid definitions. It provides SSA-like functionality when traversing the
931// CFG. Like SSA, each definition or assignment to a variable is assigned a
932// unique name (an integer), which acts as the SSA name for that definition.
933// The total set of names is shared among all CFG basic blocks.
934// Unlike SSA, we do not rewrite expressions to replace local variables declrefs
935// with their SSA-names. Instead, we compute a Context for each point in the
936// code, which maps local variables to the appropriate SSA-name. This map
937// changes with each assignment.
938//
939// The map is computed in a single pass over the CFG. Subsequent analyses can
940// then query the map to find the appropriate Context for a statement, and use
941// that Context to look up the definitions of variables.
942class LocalVariableMap {
943public:
944 typedef LocalVarContext Context;
945
946 /// A VarDefinition consists of an expression, representing the value of the
947 /// variable, along with the context in which that expression should be
948 /// interpreted. A reference VarDefinition does not itself contain this
949 /// information, but instead contains a pointer to a previous VarDefinition.
950 struct VarDefinition {
951 public:
952 friend class LocalVariableMap;
953
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000954 const NamedDecl *Dec; // The original declaration for this variable.
955 const Expr *Exp; // The expression for this variable, OR
956 unsigned Ref; // Reference to another VarDefinition
957 Context Ctx; // The map with which Exp should be interpreted.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000958
959 bool isReference() { return !Exp; }
960
961 private:
962 // Create ordinary variable definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000963 VarDefinition(const NamedDecl *D, const Expr *E, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000964 : Dec(D), Exp(E), Ref(0), Ctx(C)
965 { }
966
967 // Create reference to previous definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000968 VarDefinition(const NamedDecl *D, unsigned R, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000969 : Dec(D), Exp(0), Ref(R), Ctx(C)
970 { }
971 };
972
973private:
974 Context::Factory ContextFactory;
975 std::vector<VarDefinition> VarDefinitions;
976 std::vector<unsigned> CtxIndices;
977 std::vector<std::pair<Stmt*, Context> > SavedContexts;
978
979public:
980 LocalVariableMap() {
981 // index 0 is a placeholder for undefined variables (aka phi-nodes).
982 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
983 }
984
985 /// Look up a definition, within the given context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000986 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000987 const unsigned *i = Ctx.lookup(D);
988 if (!i)
989 return 0;
990 assert(*i < VarDefinitions.size());
991 return &VarDefinitions[*i];
992 }
993
994 /// Look up the definition for D within the given context. Returns
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000995 /// NULL if the expression is not statically known. If successful, also
996 /// modifies Ctx to hold the context of the return Expr.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000997 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000998 const unsigned *P = Ctx.lookup(D);
999 if (!P)
1000 return 0;
1001
1002 unsigned i = *P;
1003 while (i > 0) {
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00001004 if (VarDefinitions[i].Exp) {
1005 Ctx = VarDefinitions[i].Ctx;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001006 return VarDefinitions[i].Exp;
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00001007 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001008 i = VarDefinitions[i].Ref;
1009 }
1010 return 0;
1011 }
1012
1013 Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
1014
1015 /// Return the next context after processing S. This function is used by
1016 /// clients of the class to get the appropriate context when traversing the
1017 /// CFG. It must be called for every assignment or DeclStmt.
1018 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
1019 if (SavedContexts[CtxIndex+1].first == S) {
1020 CtxIndex++;
1021 Context Result = SavedContexts[CtxIndex].second;
1022 return Result;
1023 }
1024 return C;
1025 }
1026
1027 void dumpVarDefinitionName(unsigned i) {
1028 if (i == 0) {
1029 llvm::errs() << "Undefined";
1030 return;
1031 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001032 const NamedDecl *Dec = VarDefinitions[i].Dec;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001033 if (!Dec) {
1034 llvm::errs() << "<<NULL>>";
1035 return;
1036 }
1037 Dec->printName(llvm::errs());
Roman Divacky31ba6132012-09-06 15:59:27 +00001038 llvm::errs() << "." << i << " " << ((const void*) Dec);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001039 }
1040
1041 /// Dumps an ASCII representation of the variable map to llvm::errs()
1042 void dump() {
1043 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001044 const Expr *Exp = VarDefinitions[i].Exp;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001045 unsigned Ref = VarDefinitions[i].Ref;
1046
1047 dumpVarDefinitionName(i);
1048 llvm::errs() << " = ";
1049 if (Exp) Exp->dump();
1050 else {
1051 dumpVarDefinitionName(Ref);
1052 llvm::errs() << "\n";
1053 }
1054 }
1055 }
1056
1057 /// Dumps an ASCII representation of a Context to llvm::errs()
1058 void dumpContext(Context C) {
1059 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001060 const NamedDecl *D = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001061 D->printName(llvm::errs());
1062 const unsigned *i = C.lookup(D);
1063 llvm::errs() << " -> ";
1064 dumpVarDefinitionName(*i);
1065 llvm::errs() << "\n";
1066 }
1067 }
1068
1069 /// Builds the variable map.
1070 void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
1071 std::vector<CFGBlockInfo> &BlockInfo);
1072
1073protected:
1074 // Get the current context index
1075 unsigned getContextIndex() { return SavedContexts.size()-1; }
1076
1077 // Save the current context for later replay
1078 void saveContext(Stmt *S, Context C) {
1079 SavedContexts.push_back(std::make_pair(S,C));
1080 }
1081
1082 // Adds a new definition to the given context, and returns a new context.
1083 // This method should be called when declaring a new variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001084 Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001085 assert(!Ctx.contains(D));
1086 unsigned newID = VarDefinitions.size();
1087 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1088 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1089 return NewCtx;
1090 }
1091
1092 // Add a new reference to an existing definition.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001093 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001094 unsigned newID = VarDefinitions.size();
1095 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1096 VarDefinitions.push_back(VarDefinition(D, i, Ctx));
1097 return NewCtx;
1098 }
1099
1100 // Updates a definition only if that definition is already in the map.
1101 // This method should be called when assigning to an existing variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001102 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001103 if (Ctx.contains(D)) {
1104 unsigned newID = VarDefinitions.size();
1105 Context NewCtx = ContextFactory.remove(Ctx, D);
1106 NewCtx = ContextFactory.add(NewCtx, D, newID);
1107 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1108 return NewCtx;
1109 }
1110 return Ctx;
1111 }
1112
1113 // Removes a definition from the context, but keeps the variable name
1114 // as a valid variable. The index 0 is a placeholder for cleared definitions.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001115 Context clearDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001116 Context NewCtx = Ctx;
1117 if (NewCtx.contains(D)) {
1118 NewCtx = ContextFactory.remove(NewCtx, D);
1119 NewCtx = ContextFactory.add(NewCtx, D, 0);
1120 }
1121 return NewCtx;
1122 }
1123
1124 // Remove a definition entirely frmo the context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001125 Context removeDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001126 Context NewCtx = Ctx;
1127 if (NewCtx.contains(D)) {
1128 NewCtx = ContextFactory.remove(NewCtx, D);
1129 }
1130 return NewCtx;
1131 }
1132
1133 Context intersectContexts(Context C1, Context C2);
1134 Context createReferenceContext(Context C);
1135 void intersectBackEdge(Context C1, Context C2);
1136
1137 friend class VarMapBuilder;
1138};
1139
1140
1141// This has to be defined after LocalVariableMap.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001142CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) {
1143 return CFGBlockInfo(M.getEmptyContext());
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001144}
1145
1146
1147/// Visitor which builds a LocalVariableMap
1148class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
1149public:
1150 LocalVariableMap* VMap;
1151 LocalVariableMap::Context Ctx;
1152
1153 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
1154 : VMap(VM), Ctx(C) {}
1155
1156 void VisitDeclStmt(DeclStmt *S);
1157 void VisitBinaryOperator(BinaryOperator *BO);
1158};
1159
1160
1161// Add new local variables to the variable map
1162void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
1163 bool modifiedCtx = false;
1164 DeclGroupRef DGrp = S->getDeclGroup();
1165 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1166 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
1167 Expr *E = VD->getInit();
1168
1169 // Add local variables with trivial type to the variable map
1170 QualType T = VD->getType();
1171 if (T.isTrivialType(VD->getASTContext())) {
1172 Ctx = VMap->addDefinition(VD, E, Ctx);
1173 modifiedCtx = true;
1174 }
1175 }
1176 }
1177 if (modifiedCtx)
1178 VMap->saveContext(S, Ctx);
1179}
1180
1181// Update local variable definitions in variable map
1182void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
1183 if (!BO->isAssignmentOp())
1184 return;
1185
1186 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1187
1188 // Update the variable map and current context.
1189 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
1190 ValueDecl *VDec = DRE->getDecl();
1191 if (Ctx.lookup(VDec)) {
1192 if (BO->getOpcode() == BO_Assign)
1193 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
1194 else
1195 // FIXME -- handle compound assignment operators
1196 Ctx = VMap->clearDefinition(VDec, Ctx);
1197 VMap->saveContext(BO, Ctx);
1198 }
1199 }
1200}
1201
1202
1203// Computes the intersection of two contexts. The intersection is the
1204// set of variables which have the same definition in both contexts;
1205// variables with different definitions are discarded.
1206LocalVariableMap::Context
1207LocalVariableMap::intersectContexts(Context C1, Context C2) {
1208 Context Result = C1;
1209 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001210 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001211 unsigned i1 = I.getData();
1212 const unsigned *i2 = C2.lookup(Dec);
1213 if (!i2) // variable doesn't exist on second path
1214 Result = removeDefinition(Dec, Result);
1215 else if (*i2 != i1) // variable exists, but has different definition
1216 Result = clearDefinition(Dec, Result);
1217 }
1218 return Result;
1219}
1220
1221// For every variable in C, create a new variable that refers to the
1222// definition in C. Return a new context that contains these new variables.
1223// (We use this for a naive implementation of SSA on loop back-edges.)
1224LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
1225 Context Result = getEmptyContext();
1226 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001227 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001228 unsigned i = I.getData();
1229 Result = addReference(Dec, i, Result);
1230 }
1231 return Result;
1232}
1233
1234// This routine also takes the intersection of C1 and C2, but it does so by
1235// altering the VarDefinitions. C1 must be the result of an earlier call to
1236// createReferenceContext.
1237void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
1238 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001239 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001240 unsigned i1 = I.getData();
1241 VarDefinition *VDef = &VarDefinitions[i1];
1242 assert(VDef->isReference());
1243
1244 const unsigned *i2 = C2.lookup(Dec);
1245 if (!i2 || (*i2 != i1))
1246 VDef->Ref = 0; // Mark this variable as undefined
1247 }
1248}
1249
1250
1251// Traverse the CFG in topological order, so all predecessors of a block
1252// (excluding back-edges) are visited before the block itself. At
1253// each point in the code, we calculate a Context, which holds the set of
1254// variable definitions which are visible at that point in execution.
1255// Visible variables are mapped to their definitions using an array that
1256// contains all definitions.
1257//
1258// At join points in the CFG, the set is computed as the intersection of
1259// the incoming sets along each edge, E.g.
1260//
1261// { Context | VarDefinitions }
1262// int x = 0; { x -> x1 | x1 = 0 }
1263// int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1264// if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
1265// else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
1266// ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
1267//
1268// This is essentially a simpler and more naive version of the standard SSA
1269// algorithm. Those definitions that remain in the intersection are from blocks
1270// that strictly dominate the current block. We do not bother to insert proper
1271// phi nodes, because they are not used in our analysis; instead, wherever
1272// a phi node would be required, we simply remove that definition from the
1273// context (E.g. x above).
1274//
1275// The initial traversal does not capture back-edges, so those need to be
1276// handled on a separate pass. Whenever the first pass encounters an
1277// incoming back edge, it duplicates the context, creating new definitions
1278// that refer back to the originals. (These correspond to places where SSA
1279// might have to insert a phi node.) On the second pass, these definitions are
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00001280// set to NULL if the variable has changed on the back-edge (i.e. a phi
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001281// node was actually required.) E.g.
1282//
1283// { Context | VarDefinitions }
1284// int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1285// while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
1286// x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
1287// ... { y -> y1 | x3 = 2, x2 = 1, ... }
1288//
1289void LocalVariableMap::traverseCFG(CFG *CFGraph,
1290 PostOrderCFGView *SortedGraph,
1291 std::vector<CFGBlockInfo> &BlockInfo) {
1292 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
1293
1294 CtxIndices.resize(CFGraph->getNumBlockIDs());
1295
1296 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1297 E = SortedGraph->end(); I!= E; ++I) {
1298 const CFGBlock *CurrBlock = *I;
1299 int CurrBlockID = CurrBlock->getBlockID();
1300 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
1301
1302 VisitedBlocks.insert(CurrBlock);
1303
1304 // Calculate the entry context for the current block
1305 bool HasBackEdges = false;
1306 bool CtxInit = true;
1307 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1308 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
1309 // if *PI -> CurrBlock is a back edge, so skip it
1310 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
1311 HasBackEdges = true;
1312 continue;
1313 }
1314
1315 int PrevBlockID = (*PI)->getBlockID();
1316 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1317
1318 if (CtxInit) {
1319 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
1320 CtxInit = false;
1321 }
1322 else {
1323 CurrBlockInfo->EntryContext =
1324 intersectContexts(CurrBlockInfo->EntryContext,
1325 PrevBlockInfo->ExitContext);
1326 }
1327 }
1328
1329 // Duplicate the context if we have back-edges, so we can call
1330 // intersectBackEdges later.
1331 if (HasBackEdges)
1332 CurrBlockInfo->EntryContext =
1333 createReferenceContext(CurrBlockInfo->EntryContext);
1334
1335 // Create a starting context index for the current block
1336 saveContext(0, CurrBlockInfo->EntryContext);
1337 CurrBlockInfo->EntryIndex = getContextIndex();
1338
1339 // Visit all the statements in the basic block.
1340 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
1341 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1342 BE = CurrBlock->end(); BI != BE; ++BI) {
1343 switch (BI->getKind()) {
1344 case CFGElement::Statement: {
1345 const CFGStmt *CS = cast<CFGStmt>(&*BI);
1346 VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
1347 break;
1348 }
1349 default:
1350 break;
1351 }
1352 }
1353 CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
1354
1355 // Mark variables on back edges as "unknown" if they've been changed.
1356 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1357 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1358 // if CurrBlock -> *SI is *not* a back edge
1359 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1360 continue;
1361
1362 CFGBlock *FirstLoopBlock = *SI;
1363 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
1364 Context LoopEnd = CurrBlockInfo->ExitContext;
1365 intersectBackEdge(LoopBegin, LoopEnd);
1366 }
1367 }
1368
1369 // Put an extra entry at the end of the indexed context array
1370 unsigned exitID = CFGraph->getExit().getBlockID();
1371 saveContext(0, BlockInfo[exitID].ExitContext);
1372}
1373
Richard Smith2e515622012-02-03 04:45:26 +00001374/// Find the appropriate source locations to use when producing diagnostics for
1375/// each block in the CFG.
1376static void findBlockLocations(CFG *CFGraph,
1377 PostOrderCFGView *SortedGraph,
1378 std::vector<CFGBlockInfo> &BlockInfo) {
1379 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1380 E = SortedGraph->end(); I!= E; ++I) {
1381 const CFGBlock *CurrBlock = *I;
1382 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
1383
1384 // Find the source location of the last statement in the block, if the
1385 // block is not empty.
1386 if (const Stmt *S = CurrBlock->getTerminator()) {
1387 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
1388 } else {
1389 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
1390 BE = CurrBlock->rend(); BI != BE; ++BI) {
1391 // FIXME: Handle other CFGElement kinds.
1392 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1393 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
1394 break;
1395 }
1396 }
1397 }
1398
1399 if (!CurrBlockInfo->ExitLoc.isInvalid()) {
1400 // This block contains at least one statement. Find the source location
1401 // of the first statement in the block.
1402 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1403 BE = CurrBlock->end(); BI != BE; ++BI) {
1404 // FIXME: Handle other CFGElement kinds.
1405 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1406 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
1407 break;
1408 }
1409 }
1410 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
1411 CurrBlock != &CFGraph->getExit()) {
1412 // The block is empty, and has a single predecessor. Use its exit
1413 // location.
1414 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
1415 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
1416 }
1417 }
1418}
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001419
1420/// \brief Class which implements the core thread safety analysis routines.
1421class ThreadSafetyAnalyzer {
1422 friend class BuildLockset;
1423
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001424 ThreadSafetyHandler &Handler;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001425 LocalVariableMap LocalVarMap;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001426 FactManager FactMan;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001427 std::vector<CFGBlockInfo> BlockInfo;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001428
1429public:
1430 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
1431
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001432 void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat);
1433 void removeLock(FactSet &FSet, const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001434 SourceLocation UnlockLoc, bool FullyRemove=false);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001435
1436 template <typename AttrType>
1437 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001438 const NamedDecl *D, VarDecl *SelfDecl=0);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001439
1440 template <class AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001441 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1442 const NamedDecl *D,
1443 const CFGBlock *PredBlock, const CFGBlock *CurrBlock,
1444 Expr *BrE, bool Neg);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001445
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001446 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
1447 bool &Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001448
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001449 void getEdgeLockset(FactSet &Result, const FactSet &ExitSet,
1450 const CFGBlock* PredBlock,
1451 const CFGBlock *CurrBlock);
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001452
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001453 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1454 SourceLocation JoinLoc,
1455 LockErrorKind LEK1, LockErrorKind LEK2,
1456 bool Modify=true);
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001457
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001458 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1459 SourceLocation JoinLoc, LockErrorKind LEK1,
1460 bool Modify=true) {
1461 intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify);
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001462 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001463
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001464 void runAnalysis(AnalysisDeclContext &AC);
1465};
1466
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001467
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001468/// \brief Add a new lock to the lockset, warning if the lock is already there.
1469/// \param Mutex -- the Mutex expression for the lock
1470/// \param LDat -- the LockData for the lock
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001471void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001472 const LockData &LDat) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001473 // FIXME: deal with acquired before/after annotations.
1474 // FIXME: Don't always warn when we have support for reentrant locks.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +00001475 if (Mutex.shouldIgnore())
1476 return;
1477
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001478 if (FSet.findLock(FactMan, Mutex)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001479 Handler.handleDoubleLock(Mutex.toString(), LDat.AcquireLoc);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001480 } else {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001481 FSet.addLock(FactMan, Mutex, LDat);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001482 }
1483}
1484
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001485
1486/// \brief Remove a lock from the lockset, warning if the lock is not there.
Ted Kremenekad0fe032012-08-22 23:50:41 +00001487/// \param Mutex The lock expression corresponding to the lock to be removed
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001488/// \param UnlockLoc The source location of the unlock (only used in error msg)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001489void ThreadSafetyAnalyzer::removeLock(FactSet &FSet,
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001490 const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001491 SourceLocation UnlockLoc,
1492 bool FullyRemove) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +00001493 if (Mutex.shouldIgnore())
1494 return;
1495
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001496 const LockData *LDat = FSet.findLock(FactMan, Mutex);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001497 if (!LDat) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001498 Handler.handleUnmatchedUnlock(Mutex.toString(), UnlockLoc);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001499 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001500 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001501
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001502 if (LDat->UnderlyingMutex.isValid()) {
1503 // This is scoped lockable object, which manages the real mutex.
1504 if (FullyRemove) {
1505 // We're destroying the managing object.
1506 // Remove the underlying mutex if it exists; but don't warn.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001507 if (FSet.findLock(FactMan, LDat->UnderlyingMutex))
1508 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001509 } else {
1510 // We're releasing the underlying mutex, but not destroying the
1511 // managing object. Warn on dual release.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001512 if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001513 Handler.handleUnmatchedUnlock(LDat->UnderlyingMutex.toString(),
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001514 UnlockLoc);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001515 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001516 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1517 return;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001518 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001519 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001520 FSet.removeLock(FactMan, Mutex);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001521}
1522
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001523
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001524/// \brief Extract the list of mutexIDs from the attribute on an expression,
1525/// and push them onto Mtxs, discarding any duplicates.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001526template <typename AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001527void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001528 Expr *Exp, const NamedDecl *D,
1529 VarDecl *SelfDecl) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001530 typedef typename AttrType::args_iterator iterator_type;
1531
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001532 if (Attr->args_size() == 0) {
1533 // The mutex held is the "this" object.
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001534 SExpr Mu(0, Exp, D, SelfDecl);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001535 if (!Mu.isValid())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001536 SExpr::warnInvalidLock(Handler, 0, Exp, D);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001537 else
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001538 Mtxs.push_back_nodup(Mu);
1539 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001540 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001541
1542 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001543 SExpr Mu(*I, Exp, D, SelfDecl);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001544 if (!Mu.isValid())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001545 SExpr::warnInvalidLock(Handler, *I, Exp, D);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001546 else
1547 Mtxs.push_back_nodup(Mu);
1548 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001549}
1550
1551
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001552/// \brief Extract the list of mutexIDs from a trylock attribute. If the
1553/// trylock applies to the given edge, then push them onto Mtxs, discarding
1554/// any duplicates.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001555template <class AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001556void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1557 Expr *Exp, const NamedDecl *D,
1558 const CFGBlock *PredBlock,
1559 const CFGBlock *CurrBlock,
1560 Expr *BrE, bool Neg) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001561 // Find out which branch has the lock
1562 bool branch = 0;
1563 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1564 branch = BLE->getValue();
1565 }
1566 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1567 branch = ILE->getValue().getBoolValue();
1568 }
1569 int branchnum = branch ? 0 : 1;
1570 if (Neg) branchnum = !branchnum;
1571
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001572 // If we've taken the trylock branch, then add the lock
1573 int i = 0;
1574 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1575 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1576 if (*SI == CurrBlock && i == branchnum) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001577 getMutexIDs(Mtxs, Attr, Exp, D);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001578 }
1579 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001580}
1581
1582
DeLesley Hutchins13106112012-07-10 21:47:55 +00001583bool getStaticBooleanValue(Expr* E, bool& TCond) {
1584 if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
1585 TCond = false;
1586 return true;
1587 } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
1588 TCond = BLE->getValue();
1589 return true;
1590 } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) {
1591 TCond = ILE->getValue().getBoolValue();
1592 return true;
1593 } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
1594 return getStaticBooleanValue(CE->getSubExpr(), TCond);
1595 }
1596 return false;
1597}
1598
1599
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001600// If Cond can be traced back to a function call, return the call expression.
1601// The negate variable should be called with false, and will be set to true
1602// if the function call is negated, e.g. if (!mu.tryLock(...))
1603const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1604 LocalVarContext C,
1605 bool &Negate) {
1606 if (!Cond)
1607 return 0;
1608
1609 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1610 return CallExp;
1611 }
DeLesley Hutchins13106112012-07-10 21:47:55 +00001612 else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) {
1613 return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
1614 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001615 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1616 return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1617 }
DeLesley Hutchinsfd0f11c2012-09-05 20:01:16 +00001618 else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) {
1619 return getTrylockCallExpr(EWC->getSubExpr(), C, Negate);
1620 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001621 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1622 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1623 return getTrylockCallExpr(E, C, Negate);
1624 }
1625 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1626 if (UOP->getOpcode() == UO_LNot) {
1627 Negate = !Negate;
1628 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1629 }
DeLesley Hutchins13106112012-07-10 21:47:55 +00001630 return 0;
1631 }
1632 else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) {
1633 if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) {
1634 if (BOP->getOpcode() == BO_NE)
1635 Negate = !Negate;
1636
1637 bool TCond = false;
1638 if (getStaticBooleanValue(BOP->getRHS(), TCond)) {
1639 if (!TCond) Negate = !Negate;
1640 return getTrylockCallExpr(BOP->getLHS(), C, Negate);
1641 }
1642 else if (getStaticBooleanValue(BOP->getLHS(), TCond)) {
1643 if (!TCond) Negate = !Negate;
1644 return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1645 }
1646 return 0;
1647 }
1648 return 0;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001649 }
1650 // FIXME -- handle && and || as well.
DeLesley Hutchins13106112012-07-10 21:47:55 +00001651 return 0;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001652}
1653
1654
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001655/// \brief Find the lockset that holds on the edge between PredBlock
1656/// and CurrBlock. The edge set is the exit set of PredBlock (passed
1657/// as the ExitSet parameter) plus any trylocks, which are conditionally held.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001658void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
1659 const FactSet &ExitSet,
1660 const CFGBlock *PredBlock,
1661 const CFGBlock *CurrBlock) {
1662 Result = ExitSet;
1663
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001664 if (!PredBlock->getTerminatorCondition())
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001665 return;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001666
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001667 bool Negate = false;
1668 const Stmt *Cond = PredBlock->getTerminatorCondition();
1669 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1670 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1671
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001672 CallExpr *Exp =
1673 const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate));
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001674 if (!Exp)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001675 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001676
1677 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1678 if(!FunDecl || !FunDecl->hasAttrs())
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001679 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001680
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001681
1682 MutexIDList ExclusiveLocksToAdd;
1683 MutexIDList SharedLocksToAdd;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001684
1685 // If the condition is a call to a Trylock function, then grab the attributes
1686 AttrVec &ArgAttrs = FunDecl->getAttrs();
1687 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1688 Attr *Attr = ArgAttrs[i];
1689 switch (Attr->getKind()) {
1690 case attr::ExclusiveTrylockFunction: {
1691 ExclusiveTrylockFunctionAttr *A =
1692 cast<ExclusiveTrylockFunctionAttr>(Attr);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001693 getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
1694 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001695 break;
1696 }
1697 case attr::SharedTrylockFunction: {
1698 SharedTrylockFunctionAttr *A =
1699 cast<SharedTrylockFunctionAttr>(Attr);
DeLesley Hutchins60ff1982012-09-20 23:14:43 +00001700 getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl,
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001701 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001702 break;
1703 }
1704 default:
1705 break;
1706 }
1707 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001708
1709 // Add and remove locks.
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001710 SourceLocation Loc = Exp->getExprLoc();
1711 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001712 addLock(Result, ExclusiveLocksToAdd[i],
1713 LockData(Loc, LK_Exclusive));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001714 }
1715 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001716 addLock(Result, SharedLocksToAdd[i],
1717 LockData(Loc, LK_Shared));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001718 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001719}
1720
1721
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001722/// \brief We use this class to visit different types of expressions in
1723/// CFGBlocks, and build up the lockset.
1724/// An expression may cause us to add or remove locks from the lockset, or else
1725/// output error messages related to missing locks.
1726/// FIXME: In future, we may be able to not inherit from a visitor.
1727class BuildLockset : public StmtVisitor<BuildLockset> {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001728 friend class ThreadSafetyAnalyzer;
1729
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001730 ThreadSafetyAnalyzer *Analyzer;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001731 FactSet FSet;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001732 LocalVariableMap::Context LVarCtx;
1733 unsigned CtxIndex;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001734
1735 // Helper functions
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001736 const ValueDecl *getValueDecl(Expr *Exp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001737
1738 void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK,
1739 Expr *MutexExp, ProtectedOperationKind POK);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001740 void warnIfMutexHeld(const NamedDecl *D, Expr *Exp, Expr *MutexExp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001741
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001742 void checkAccess(Expr *Exp, AccessKind AK);
1743 void checkDereference(Expr *Exp, AccessKind AK);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001744 void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001745
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001746public:
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001747 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001748 : StmtVisitor<BuildLockset>(),
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001749 Analyzer(Anlzr),
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001750 FSet(Info.EntrySet),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001751 LVarCtx(Info.EntryContext),
1752 CtxIndex(Info.EntryIndex)
1753 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001754
1755 void VisitUnaryOperator(UnaryOperator *UO);
1756 void VisitBinaryOperator(BinaryOperator *BO);
1757 void VisitCastExpr(CastExpr *CE);
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001758 void VisitCallExpr(CallExpr *Exp);
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001759 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001760 void VisitDeclStmt(DeclStmt *S);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001761};
1762
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001763
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001764/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1765const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
1766 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1767 return DR->getDecl();
1768
1769 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1770 return ME->getMemberDecl();
1771
1772 return 0;
1773}
1774
1775/// \brief Warn if the LSet does not contain a lock sufficient to protect access
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001776/// of at least the passed in AccessKind.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001777void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
1778 AccessKind AK, Expr *MutexExp,
1779 ProtectedOperationKind POK) {
1780 LockKind LK = getLockKindFromAccessKind(AK);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001781
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001782 SExpr Mutex(MutexExp, Exp, D);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001783 if (!Mutex.isValid()) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001784 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001785 return;
1786 } else if (Mutex.shouldIgnore()) {
1787 return;
1788 }
1789
1790 LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex);
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001791 bool NoError = true;
1792 if (!LDat) {
1793 // No exact match found. Look for a partial match.
1794 FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex);
1795 if (FEntry) {
1796 // Warn that there's no precise match.
1797 LDat = &FEntry->LDat;
1798 std::string PartMatchStr = FEntry->MutID.toString();
1799 StringRef PartMatchName(PartMatchStr);
1800 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1801 Exp->getExprLoc(), &PartMatchName);
1802 } else {
1803 // Warn that there's no match at all.
1804 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1805 Exp->getExprLoc());
1806 }
1807 NoError = false;
1808 }
1809 // Make sure the mutex we found is the right kind.
1810 if (NoError && LDat && !LDat->isAtLeast(LK))
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001811 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001812 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001813}
1814
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001815/// \brief Warn if the LSet contains the given lock.
1816void BuildLockset::warnIfMutexHeld(const NamedDecl *D, Expr* Exp,
1817 Expr *MutexExp) {
1818 SExpr Mutex(MutexExp, Exp, D);
1819 if (!Mutex.isValid()) {
1820 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1821 return;
1822 }
1823
1824 LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex);
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001825 if (LDat) {
1826 std::string DeclName = D->getNameAsString();
1827 StringRef DeclNameSR (DeclName);
1828 Analyzer->Handler.handleFunExcludesLock(DeclNameSR, Mutex.toString(),
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001829 Exp->getExprLoc());
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001830 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001831}
1832
1833
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001834/// \brief This method identifies variable dereferences and checks pt_guarded_by
1835/// and pt_guarded_var annotations. Note that we only check these annotations
1836/// at the time a pointer is dereferenced.
1837/// FIXME: We need to check for other types of pointer dereferences
1838/// (e.g. [], ->) and deal with them here.
1839/// \param Exp An expression that has been read or written.
1840void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
1841 UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
1842 if (!UO || UO->getOpcode() != clang::UO_Deref)
1843 return;
1844 Exp = UO->getSubExpr()->IgnoreParenCasts();
1845
1846 const ValueDecl *D = getValueDecl(Exp);
1847 if(!D || !D->hasAttrs())
1848 return;
1849
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001850 if (D->getAttr<PtGuardedVarAttr>() && FSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001851 Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1852 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001853
1854 const AttrVec &ArgAttrs = D->getAttrs();
1855 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1856 if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1857 warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
1858}
1859
1860/// \brief Checks guarded_by and guarded_var attributes.
1861/// Whenever we identify an access (read or write) of a DeclRefExpr or
1862/// MemberExpr, we need to check whether there are any guarded_by or
1863/// guarded_var attributes, and make sure we hold the appropriate mutexes.
1864void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
1865 const ValueDecl *D = getValueDecl(Exp);
1866 if(!D || !D->hasAttrs())
1867 return;
1868
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001869 if (D->getAttr<GuardedVarAttr>() && FSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001870 Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1871 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001872
1873 const AttrVec &ArgAttrs = D->getAttrs();
1874 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1875 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1876 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1877}
1878
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001879/// \brief Process a function call, method call, constructor call,
1880/// or destructor call. This involves looking at the attributes on the
1881/// corresponding function/method/constructor/destructor, issuing warnings,
1882/// and updating the locksets accordingly.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001883///
1884/// FIXME: For classes annotated with one of the guarded annotations, we need
1885/// to treat const method calls as reads and non-const method calls as writes,
1886/// and check that the appropriate locks are held. Non-const method calls with
1887/// the same signature as const method calls can be also treated as reads.
1888///
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001889void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) {
1890 const AttrVec &ArgAttrs = D->getAttrs();
1891 MutexIDList ExclusiveLocksToAdd;
1892 MutexIDList SharedLocksToAdd;
1893 MutexIDList LocksToRemove;
1894
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001895 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001896 Attr *At = const_cast<Attr*>(ArgAttrs[i]);
1897 switch (At->getKind()) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001898 // When we encounter an exclusive lock function, we need to add the lock
1899 // to our lockset with kind exclusive.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001900 case attr::ExclusiveLockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001901 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(At);
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001902 Analyzer->getMutexIDs(ExclusiveLocksToAdd, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001903 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001904 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001905
1906 // When we encounter a shared lock function, we need to add the lock
1907 // to our lockset with kind shared.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001908 case attr::SharedLockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001909 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(At);
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001910 Analyzer->getMutexIDs(SharedLocksToAdd, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001911 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001912 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001913
1914 // When we encounter an unlock function, we need to remove unlocked
1915 // mutexes from the lockset, and flag a warning if they are not there.
1916 case attr::UnlockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001917 UnlockFunctionAttr *A = cast<UnlockFunctionAttr>(At);
DeLesley Hutchinsef2388b2012-10-05 22:38:19 +00001918 Analyzer->getMutexIDs(LocksToRemove, A, Exp, D, VD);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001919 break;
1920 }
1921
1922 case attr::ExclusiveLocksRequired: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001923 ExclusiveLocksRequiredAttr *A = cast<ExclusiveLocksRequiredAttr>(At);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001924
1925 for (ExclusiveLocksRequiredAttr::args_iterator
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001926 I = A->args_begin(), E = A->args_end(); I != E; ++I)
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001927 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
1928 break;
1929 }
1930
1931 case attr::SharedLocksRequired: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001932 SharedLocksRequiredAttr *A = cast<SharedLocksRequiredAttr>(At);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001933
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001934 for (SharedLocksRequiredAttr::args_iterator I = A->args_begin(),
1935 E = A->args_end(); I != E; ++I)
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001936 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
1937 break;
1938 }
1939
1940 case attr::LocksExcluded: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001941 LocksExcludedAttr *A = cast<LocksExcludedAttr>(At);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001942
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001943 for (LocksExcludedAttr::args_iterator I = A->args_begin(),
1944 E = A->args_end(); I != E; ++I) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001945 warnIfMutexHeld(D, Exp, *I);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001946 }
1947 break;
1948 }
1949
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001950 // Ignore other (non thread-safety) attributes
1951 default:
1952 break;
1953 }
1954 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001955
1956 // Figure out if we're calling the constructor of scoped lockable class
1957 bool isScopedVar = false;
1958 if (VD) {
1959 if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) {
1960 const CXXRecordDecl* PD = CD->getParent();
1961 if (PD && PD->getAttr<ScopedLockableAttr>())
1962 isScopedVar = true;
1963 }
1964 }
1965
1966 // Add locks.
1967 SourceLocation Loc = Exp->getExprLoc();
1968 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001969 Analyzer->addLock(FSet, ExclusiveLocksToAdd[i],
1970 LockData(Loc, LK_Exclusive, isScopedVar));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001971 }
1972 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001973 Analyzer->addLock(FSet, SharedLocksToAdd[i],
1974 LockData(Loc, LK_Shared, isScopedVar));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001975 }
1976
1977 // Add the managing object as a dummy mutex, mapped to the underlying mutex.
1978 // FIXME -- this doesn't work if we acquire multiple locks.
1979 if (isScopedVar) {
1980 SourceLocation MLoc = VD->getLocation();
1981 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001982 SExpr SMutex(&DRE, 0, 0);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001983
1984 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001985 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive,
1986 ExclusiveLocksToAdd[i]));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001987 }
1988 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001989 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared,
1990 SharedLocksToAdd[i]));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001991 }
1992 }
1993
1994 // Remove locks.
1995 // FIXME -- should only fully remove if the attribute refers to 'this'.
1996 bool Dtor = isa<CXXDestructorDecl>(D);
1997 for (unsigned i=0,n=LocksToRemove.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001998 Analyzer->removeLock(FSet, LocksToRemove[i], Loc, Dtor);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001999 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002000}
2001
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00002002
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00002003/// \brief For unary operations which read and write a variable, we need to
2004/// check whether we hold any required mutexes. Reads are checked in
2005/// VisitCastExpr.
2006void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
2007 switch (UO->getOpcode()) {
2008 case clang::UO_PostDec:
2009 case clang::UO_PostInc:
2010 case clang::UO_PreDec:
2011 case clang::UO_PreInc: {
2012 Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
2013 checkAccess(SubExp, AK_Written);
2014 checkDereference(SubExp, AK_Written);
2015 break;
2016 }
2017 default:
2018 break;
2019 }
2020}
2021
2022/// For binary operations which assign to a variable (writes), we need to check
2023/// whether we hold any required mutexes.
2024/// FIXME: Deal with non-primitive types.
2025void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
2026 if (!BO->isAssignmentOp())
2027 return;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002028
2029 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002030 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002031
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00002032 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
2033 checkAccess(LHSExp, AK_Written);
2034 checkDereference(LHSExp, AK_Written);
2035}
2036
2037/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
2038/// need to ensure we hold any required mutexes.
2039/// FIXME: Deal with non-primitive types.
2040void BuildLockset::VisitCastExpr(CastExpr *CE) {
2041 if (CE->getCastKind() != CK_LValueToRValue)
2042 return;
2043 Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
2044 checkAccess(SubExp, AK_Read);
2045 checkDereference(SubExp, AK_Read);
2046}
2047
2048
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00002049void BuildLockset::VisitCallExpr(CallExpr *Exp) {
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00002050 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
2051 if(!D || !D->hasAttrs())
2052 return;
2053 handleCall(Exp, D);
2054}
2055
2056void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00002057 // FIXME -- only handles constructors in DeclStmt below.
2058}
2059
2060void BuildLockset::VisitDeclStmt(DeclStmt *S) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002061 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002062 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002063
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00002064 DeclGroupRef DGrp = S->getDeclGroup();
2065 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
2066 Decl *D = *I;
2067 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
2068 Expr *E = VD->getInit();
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +00002069 // handle constructors that involve temporaries
2070 if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E))
2071 E = EWC->getSubExpr();
2072
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00002073 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
2074 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
2075 if (!CtorD || !CtorD->hasAttrs())
2076 return;
2077 handleCall(CE, CtorD, VD);
2078 }
2079 }
2080 }
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00002081}
2082
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002083
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002084
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00002085/// \brief Compute the intersection of two locksets and issue warnings for any
2086/// locks in the symmetric difference.
2087///
2088/// This function is used at a merge point in the CFG when comparing the lockset
2089/// of each branch being merged. For example, given the following sequence:
2090/// A; if () then B; else C; D; we need to check that the lockset after B and C
2091/// are the same. In the event of a difference, we use the intersection of these
2092/// two locksets at the start of D.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002093///
Ted Kremenekad0fe032012-08-22 23:50:41 +00002094/// \param FSet1 The first lockset.
2095/// \param FSet2 The second lockset.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002096/// \param JoinLoc The location of the join point for error reporting
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002097/// \param LEK1 The error message to report if a mutex is missing from LSet1
2098/// \param LEK2 The error message to report if a mutex is missing from Lset2
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002099void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1,
2100 const FactSet &FSet2,
2101 SourceLocation JoinLoc,
2102 LockErrorKind LEK1,
2103 LockErrorKind LEK2,
2104 bool Modify) {
2105 FactSet FSet1Orig = FSet1;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002106
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002107 for (FactSet::const_iterator I = FSet2.begin(), E = FSet2.end();
2108 I != E; ++I) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002109 const SExpr &FSet2Mutex = FactMan[*I].MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002110 const LockData &LDat2 = FactMan[*I].LDat;
2111
2112 if (const LockData *LDat1 = FSet1.findLock(FactMan, FSet2Mutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002113 if (LDat1->LKind != LDat2.LKind) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002114 Handler.handleExclusiveAndShared(FSet2Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002115 LDat2.AcquireLoc,
2116 LDat1->AcquireLoc);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002117 if (Modify && LDat1->LKind != LK_Exclusive) {
2118 FSet1.removeLock(FactMan, FSet2Mutex);
2119 FSet1.addLock(FactMan, FSet2Mutex, LDat2);
2120 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002121 }
2122 } else {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002123 if (LDat2.UnderlyingMutex.isValid()) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002124 if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002125 // If this is a scoped lock that manages another mutex, and if the
2126 // underlying mutex is still held, then warn about the underlying
2127 // mutex.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002128 Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002129 LDat2.AcquireLoc,
2130 JoinLoc, LEK1);
2131 }
2132 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00002133 else if (!LDat2.Managed && !FSet2Mutex.isUniversal())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002134 Handler.handleMutexHeldEndOfScope(FSet2Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002135 LDat2.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002136 JoinLoc, LEK1);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002137 }
2138 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002139
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002140 for (FactSet::const_iterator I = FSet1.begin(), E = FSet1.end();
2141 I != E; ++I) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002142 const SExpr &FSet1Mutex = FactMan[*I].MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002143 const LockData &LDat1 = FactMan[*I].LDat;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00002144
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002145 if (!FSet2.findLock(FactMan, FSet1Mutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002146 if (LDat1.UnderlyingMutex.isValid()) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002147 if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002148 // If this is a scoped lock that manages another mutex, and if the
2149 // underlying mutex is still held, then warn about the underlying
2150 // mutex.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002151 Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002152 LDat1.AcquireLoc,
2153 JoinLoc, LEK1);
2154 }
2155 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00002156 else if (!LDat1.Managed && !FSet1Mutex.isUniversal())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002157 Handler.handleMutexHeldEndOfScope(FSet1Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002158 LDat1.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002159 JoinLoc, LEK2);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002160 if (Modify)
2161 FSet1.removeLock(FactMan, FSet1Mutex);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002162 }
2163 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002164}
2165
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002166
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002167
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002168/// \brief Check a function's CFG for thread-safety violations.
2169///
2170/// We traverse the blocks in the CFG, compute the set of mutexes that are held
2171/// at the end of each block, and issue warnings for thread safety violations.
2172/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00002173void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002174 CFG *CFGraph = AC.getCFG();
2175 if (!CFGraph) return;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00002176 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
2177
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002178 // AC.dumpCFG(true);
2179
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00002180 if (!D)
2181 return; // Ignore anonymous functions for now.
2182 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
2183 return;
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002184 // FIXME: Do something a bit more intelligent inside constructor and
2185 // destructor code. Constructors and destructors must assume unique access
2186 // to 'this', so checks on member variable access is disabled, but we should
2187 // still enable checks on other objects.
2188 if (isa<CXXConstructorDecl>(D))
2189 return; // Don't check inside constructors.
2190 if (isa<CXXDestructorDecl>(D))
2191 return; // Don't check inside destructors.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002192
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002193 BlockInfo.resize(CFGraph->getNumBlockIDs(),
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002194 CFGBlockInfo::getEmptyBlockInfo(LocalVarMap));
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002195
2196 // We need to explore the CFG via a "topological" ordering.
2197 // That way, we will be guaranteed to have information about required
2198 // predecessor locksets when exploring a new block.
Ted Kremenek439ed162011-10-22 02:14:27 +00002199 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
2200 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002201
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002202 // Mark entry block as reachable
2203 BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true;
2204
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002205 // Compute SSA names for local variables
2206 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
2207
Richard Smith2e515622012-02-03 04:45:26 +00002208 // Fill in source locations for all CFGBlocks.
2209 findBlockLocations(CFGraph, SortedGraph, BlockInfo);
2210
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002211 // Add locks from exclusive_locks_required and shared_locks_required
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002212 // to initial lockset. Also turn off checking for lock and unlock functions.
2213 // FIXME: is there a more intelligent way to check lock/unlock functions?
Ted Kremenek439ed162011-10-22 02:14:27 +00002214 if (!SortedGraph->empty() && D->hasAttrs()) {
2215 const CFGBlock *FirstBlock = *SortedGraph->begin();
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002216 FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002217 const AttrVec &ArgAttrs = D->getAttrs();
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002218
2219 MutexIDList ExclusiveLocksToAdd;
2220 MutexIDList SharedLocksToAdd;
2221
2222 SourceLocation Loc = D->getLocation();
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002223 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002224 Attr *Attr = ArgAttrs[i];
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002225 Loc = Attr->getLocation();
2226 if (ExclusiveLocksRequiredAttr *A
2227 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
2228 getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D);
2229 } else if (SharedLocksRequiredAttr *A
2230 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
2231 getMutexIDs(SharedLocksToAdd, A, (Expr*) 0, D);
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002232 } else if (isa<UnlockFunctionAttr>(Attr)) {
2233 // Don't try to check unlock functions for now
2234 return;
2235 } else if (isa<ExclusiveLockFunctionAttr>(Attr)) {
2236 // Don't try to check lock functions for now
2237 return;
2238 } else if (isa<SharedLockFunctionAttr>(Attr)) {
2239 // Don't try to check lock functions for now
2240 return;
DeLesley Hutchins76f0a6e2012-07-02 21:59:24 +00002241 } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
2242 // Don't try to check trylock functions for now
2243 return;
2244 } else if (isa<SharedTrylockFunctionAttr>(Attr)) {
2245 // Don't try to check trylock functions for now
2246 return;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002247 }
2248 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002249
2250 // FIXME -- Loc can be wrong here.
2251 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002252 addLock(InitialLockset, ExclusiveLocksToAdd[i],
2253 LockData(Loc, LK_Exclusive));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002254 }
2255 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002256 addLock(InitialLockset, SharedLocksToAdd[i],
2257 LockData(Loc, LK_Shared));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002258 }
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002259 }
2260
Ted Kremenek439ed162011-10-22 02:14:27 +00002261 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
2262 E = SortedGraph->end(); I!= E; ++I) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002263 const CFGBlock *CurrBlock = *I;
2264 int CurrBlockID = CurrBlock->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002265 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002266
2267 // Use the default initial lockset in case there are no predecessors.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002268 VisitedBlocks.insert(CurrBlock);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002269
2270 // Iterate through the predecessor blocks and warn if the lockset for all
2271 // predecessors is not the same. We take the entry lockset of the current
2272 // block to be the intersection of all previous locksets.
2273 // FIXME: By keeping the intersection, we may output more errors in future
2274 // for a lock which is not in the intersection, but was in the union. We
2275 // may want to also keep the union in future. As an example, let's say
2276 // the intersection contains Mutex L, and the union contains L and M.
2277 // Later we unlock M. At this point, we would output an error because we
2278 // never locked M; although the real error is probably that we forgot to
2279 // lock M on all code paths. Conversely, let's say that later we lock M.
2280 // In this case, we should compare against the intersection instead of the
2281 // union because the real error is probably that we forgot to unlock M on
2282 // all code paths.
2283 bool LocksetInitialized = false;
Richard Smithaacde712012-02-03 03:30:07 +00002284 llvm::SmallVector<CFGBlock*, 8> SpecialBlocks;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002285 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
2286 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
2287
2288 // if *PI -> CurrBlock is a back edge
2289 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
2290 continue;
2291
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002292 int PrevBlockID = (*PI)->getBlockID();
2293 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2294
DeLesley Hutchins2a35be82012-03-02 22:02:58 +00002295 // Ignore edges from blocks that can't return.
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002296 if ((*PI)->hasNoReturnElement() || !PrevBlockInfo->Reachable)
DeLesley Hutchins2a35be82012-03-02 22:02:58 +00002297 continue;
2298
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002299 // Okay, we can reach this block from the entry.
2300 CurrBlockInfo->Reachable = true;
2301
Richard Smithaacde712012-02-03 03:30:07 +00002302 // If the previous block ended in a 'continue' or 'break' statement, then
2303 // a difference in locksets is probably due to a bug in that block, rather
2304 // than in some other predecessor. In that case, keep the other
2305 // predecessor's lockset.
2306 if (const Stmt *Terminator = (*PI)->getTerminator()) {
2307 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
2308 SpecialBlocks.push_back(*PI);
2309 continue;
2310 }
2311 }
2312
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002313
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002314 FactSet PrevLockset;
2315 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002316
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002317 if (!LocksetInitialized) {
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002318 CurrBlockInfo->EntrySet = PrevLockset;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002319 LocksetInitialized = true;
2320 } else {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002321 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2322 CurrBlockInfo->EntryLoc,
2323 LEK_LockedSomePredecessors);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002324 }
2325 }
2326
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002327 // Skip rest of block if it's not reachable.
2328 if (!CurrBlockInfo->Reachable)
2329 continue;
2330
Richard Smithaacde712012-02-03 03:30:07 +00002331 // Process continue and break blocks. Assume that the lockset for the
2332 // resulting block is unaffected by any discrepancies in them.
2333 for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
2334 SpecialI < SpecialN; ++SpecialI) {
2335 CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
2336 int PrevBlockID = PrevBlock->getBlockID();
2337 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2338
2339 if (!LocksetInitialized) {
2340 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
2341 LocksetInitialized = true;
2342 } else {
2343 // Determine whether this edge is a loop terminator for diagnostic
2344 // purposes. FIXME: A 'break' statement might be a loop terminator, but
2345 // it might also be part of a switch. Also, a subsequent destructor
2346 // might add to the lockset, in which case the real issue might be a
2347 // double lock on the other path.
2348 const Stmt *Terminator = PrevBlock->getTerminator();
2349 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
2350
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002351 FactSet PrevLockset;
2352 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet,
2353 PrevBlock, CurrBlock);
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002354
Richard Smithaacde712012-02-03 03:30:07 +00002355 // Do not update EntrySet.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002356 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2357 PrevBlockInfo->ExitLoc,
Richard Smithaacde712012-02-03 03:30:07 +00002358 IsLoop ? LEK_LockedSomeLoopIterations
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002359 : LEK_LockedSomePredecessors,
2360 false);
Richard Smithaacde712012-02-03 03:30:07 +00002361 }
2362 }
2363
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002364 BuildLockset LocksetBuilder(this, *CurrBlockInfo);
2365
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002366 // Visit all the statements in the basic block.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002367 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
2368 BE = CurrBlock->end(); BI != BE; ++BI) {
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00002369 switch (BI->getKind()) {
2370 case CFGElement::Statement: {
2371 const CFGStmt *CS = cast<CFGStmt>(&*BI);
2372 LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
2373 break;
2374 }
2375 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
2376 case CFGElement::AutomaticObjectDtor: {
2377 const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
2378 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
2379 AD->getDestructorDecl(AC.getASTContext()));
2380 if (!DD->hasAttrs())
2381 break;
2382
2383 // Create a dummy expression,
2384 VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
John McCallf4b88a42012-03-10 09:33:50 +00002385 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00002386 AD->getTriggerStmt()->getLocEnd());
2387 LocksetBuilder.handleCall(&DRE, DD);
2388 break;
2389 }
2390 default:
2391 break;
2392 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002393 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002394 CurrBlockInfo->ExitSet = LocksetBuilder.FSet;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002395
2396 // For every back edge from CurrBlock (the end of the loop) to another block
2397 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
2398 // the one held at the beginning of FirstLoopBlock. We can look up the
2399 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
2400 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
2401 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
2402
2403 // if CurrBlock -> *SI is *not* a back edge
2404 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
2405 continue;
2406
2407 CFGBlock *FirstLoopBlock = *SI;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002408 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
2409 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
2410 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
2411 PreLoop->EntryLoc,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002412 LEK_LockedSomeLoopIterations,
2413 false);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002414 }
2415 }
2416
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002417 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
2418 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00002419
DeLesley Hutchinsd2f38822012-09-21 17:57:00 +00002420 // Skip the final check if the exit block is unreachable.
2421 if (!Final->Reachable)
2422 return;
2423
Caitlin Sadowski1748b122011-09-16 00:35:54 +00002424 // FIXME: Should we call this function for all blocks which exit the function?
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002425 intersectAndWarn(Initial->EntrySet, Final->ExitSet,
2426 Final->ExitLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002427 LEK_LockedAtEndOfFunction,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002428 LEK_NotLockedAtEndOfFunction,
2429 false);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002430}
2431
2432} // end anonymous namespace
2433
2434
2435namespace clang {
2436namespace thread_safety {
2437
2438/// \brief Check a function's CFG for thread-safety violations.
2439///
2440/// We traverse the blocks in the CFG, compute the set of mutexes that are held
2441/// at the end of each block, and issue warnings for thread safety violations.
2442/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00002443void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002444 ThreadSafetyHandler &Handler) {
2445 ThreadSafetyAnalyzer Analyzer(Handler);
2446 Analyzer.runAnalysis(AC);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002447}
2448
2449/// \brief Helper function that returns a LockKind required for the given level
2450/// of access.
2451LockKind getLockKindFromAccessKind(AccessKind AK) {
2452 switch (AK) {
2453 case AK_Read :
2454 return LK_Shared;
2455 case AK_Written :
2456 return LK_Exclusive;
2457 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00002458 llvm_unreachable("Unknown AccessKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002459}
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002460
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002461}} // end namespace clang::thread_safety