Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1 | //===- 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 Sadowski | 1990346 | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 13 | // See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more |
| 14 | // information. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
Ted Kremenek | 439ed16 | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
Caitlin Sadowski | d5b1605 | 2011-09-09 23:00:59 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/AnalysisContext.h" |
| 21 | #include "clang/Analysis/CFG.h" |
| 22 | #include "clang/Analysis/CFGStmtMap.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "clang/AST/StmtCXX.h" |
| 26 | #include "clang/AST/StmtVisitor.h" |
Caitlin Sadowski | d5b1605 | 2011-09-09 23:00:59 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
| 28 | #include "clang/Basic/SourceLocation.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/BitVector.h" |
| 30 | #include "llvm/ADT/FoldingSet.h" |
| 31 | #include "llvm/ADT/ImmutableMap.h" |
| 32 | #include "llvm/ADT/PostOrderIterator.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
| 34 | #include "llvm/ADT/StringRef.h" |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 36 | #include <algorithm> |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 37 | #include <utility> |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | using namespace clang; |
| 41 | using namespace thread_safety; |
| 42 | |
Caitlin Sadowski | 1990346 | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 43 | // Key method definition |
| 44 | ThreadSafetyHandler::~ThreadSafetyHandler() {} |
| 45 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 46 | namespace { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 47 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 48 | /// \brief A MutexID object uniquely identifies a particular mutex, and |
| 49 | /// is built from an Expr* (i.e. calling a lock function). |
| 50 | /// |
| 51 | /// Thread-safety analysis works by comparing lock expressions. Within the |
| 52 | /// body of a function, an expression such as "x->foo->bar.mu" will resolve to |
| 53 | /// a particular mutex object at run-time. Subsequent occurrences of the same |
| 54 | /// expression (where "same" means syntactic equality) will refer to the same |
| 55 | /// run-time object if three conditions hold: |
| 56 | /// (1) Local variables in the expression, such as "x" have not changed. |
| 57 | /// (2) Values on the heap that affect the expression have not changed. |
| 58 | /// (3) The expression involves only pure function calls. |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 59 | /// |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 60 | /// The current implementation assumes, but does not verify, that multiple uses |
| 61 | /// of the same lock expression satisfies these criteria. |
| 62 | /// |
| 63 | /// Clang introduces an additional wrinkle, which is that it is difficult to |
| 64 | /// derive canonical expressions, or compare expressions directly for equality. |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 65 | /// Thus, we identify a mutex not by an Expr, but by the list of named |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 66 | /// declarations that are referenced by the Expr. In other words, |
| 67 | /// x->foo->bar.mu will be a four element vector with the Decls for |
| 68 | /// mu, bar, and foo, and x. The vector will uniquely identify the expression |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 69 | /// for all practical purposes. Null is used to denote 'this'. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 70 | /// |
| 71 | /// Note we will need to perform substitution on "this" and function parameter |
| 72 | /// names when constructing a lock expression. |
| 73 | /// |
| 74 | /// For example: |
| 75 | /// class C { Mutex Mu; void lock() EXCLUSIVE_LOCK_FUNCTION(this->Mu); }; |
| 76 | /// void myFunc(C *X) { ... X->lock() ... } |
| 77 | /// The original expression for the mutex acquired by myFunc is "this->Mu", but |
| 78 | /// "X" is substituted for "this" so we get X->Mu(); |
| 79 | /// |
| 80 | /// For another example: |
| 81 | /// foo(MyList *L) EXCLUSIVE_LOCKS_REQUIRED(L->Mu) { ... } |
| 82 | /// MyList *MyL; |
| 83 | /// foo(MyL); // requires lock MyL->Mu to be held |
| 84 | class MutexID { |
| 85 | SmallVector<NamedDecl*, 2> DeclSeq; |
| 86 | |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 87 | /// \brief Encapsulates the lexical context of a function call. The lexical |
| 88 | /// context includes the arguments to the call, including the implicit object |
| 89 | /// argument. When an attribute containing a mutex expression is attached to |
| 90 | /// a method, the expression may refer to formal parameters of the method. |
| 91 | /// Actual arguments must be substituted for formal parameters to derive |
| 92 | /// the appropriate mutex expression in the lexical context where the function |
| 93 | /// is called. PrevCtx holds the context in which the arguments themselves |
| 94 | /// should be evaluated; multiple calling contexts can be chained together |
| 95 | /// by the lock_returned attribute. |
| 96 | struct CallingContext { |
| 97 | const NamedDecl* AttrDecl; // The decl to which the attribute is attached. |
| 98 | Expr* SelfArg; // Implicit object argument -- e.g. 'this' |
| 99 | unsigned NumArgs; // Number of funArgs |
| 100 | Expr** FunArgs; // Function arguments |
| 101 | CallingContext* PrevCtx; // The previous context; or 0 if none. |
| 102 | |
| 103 | CallingContext(const NamedDecl* D = 0, Expr* S = 0, |
| 104 | unsigned N = 0, Expr** A = 0, CallingContext* P = 0) |
| 105 | : AttrDecl(D), SelfArg(S), NumArgs(N), FunArgs(A), PrevCtx(P) |
| 106 | { } |
| 107 | }; |
| 108 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 109 | /// Build a Decl sequence representing the lock from the given expression. |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 110 | /// Recursive function that terminates on DeclRefExpr. |
| 111 | /// Note: this function merely creates a MutexID; it does not check to |
| 112 | /// ensure that the original expression is a valid mutex expression. |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 113 | void buildMutexID(Expr *Exp, CallingContext* CallCtx) { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 114 | if (!Exp) { |
| 115 | DeclSeq.clear(); |
| 116 | return; |
| 117 | } |
| 118 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 119 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) { |
| 120 | NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); |
DeLesley Hutchins | e03b2b3 | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 121 | ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND); |
| 122 | if (PV) { |
| 123 | FunctionDecl *FD = |
| 124 | cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl(); |
| 125 | unsigned i = PV->getFunctionScopeIndex(); |
| 126 | |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 127 | if (CallCtx && CallCtx->FunArgs && |
| 128 | FD == CallCtx->AttrDecl->getCanonicalDecl()) { |
DeLesley Hutchins | e03b2b3 | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 129 | // Substitute call arguments for references to function parameters |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 130 | assert(i < CallCtx->NumArgs); |
| 131 | buildMutexID(CallCtx->FunArgs[i], CallCtx->PrevCtx); |
DeLesley Hutchins | e03b2b3 | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | // Map the param back to the param of the original function declaration. |
| 135 | DeclSeq.push_back(FD->getParamDecl(i)); |
| 136 | return; |
| 137 | } |
| 138 | // Not a function parameter -- just store the reference. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 139 | DeclSeq.push_back(ND); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 140 | } else if (isa<CXXThisExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 141 | // Substitute parent for 'this' |
| 142 | if (CallCtx && CallCtx->SelfArg) |
| 143 | buildMutexID(CallCtx->SelfArg, CallCtx->PrevCtx); |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 144 | else { |
| 145 | DeclSeq.push_back(0); // Use 0 to represent 'this'. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 146 | return; // mutexID is still valid in this case |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 147 | } |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 148 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 149 | NamedDecl *ND = ME->getMemberDecl(); |
| 150 | DeclSeq.push_back(ND); |
| 151 | buildMutexID(ME->getBase(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 152 | } else if (CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 153 | // When calling a function with a lock_returned attribute, replace |
| 154 | // the function call with the expression in lock_returned. |
| 155 | if (LockReturnedAttr* At = |
| 156 | CMCE->getMethodDecl()->getAttr<LockReturnedAttr>()) { |
| 157 | CallingContext LRCallCtx(CMCE->getMethodDecl()); |
| 158 | LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument(); |
| 159 | LRCallCtx.NumArgs = CMCE->getNumArgs(); |
| 160 | LRCallCtx.FunArgs = CMCE->getArgs(); |
| 161 | LRCallCtx.PrevCtx = CallCtx; |
| 162 | buildMutexID(At->getArg(), &LRCallCtx); |
| 163 | return; |
| 164 | } |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 165 | DeclSeq.push_back(CMCE->getMethodDecl()->getCanonicalDecl()); |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 166 | buildMutexID(CMCE->getImplicitObjectArgument(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 167 | unsigned NumCallArgs = CMCE->getNumArgs(); |
| 168 | Expr** CallArgs = CMCE->getArgs(); |
| 169 | for (unsigned i = 0; i < NumCallArgs; ++i) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 170 | buildMutexID(CallArgs[i], CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 171 | } |
| 172 | } else if (CallExpr *CE = dyn_cast<CallExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 173 | if (LockReturnedAttr* At = |
| 174 | CE->getDirectCallee()->getAttr<LockReturnedAttr>()) { |
| 175 | CallingContext LRCallCtx(CE->getDirectCallee()); |
| 176 | LRCallCtx.NumArgs = CE->getNumArgs(); |
| 177 | LRCallCtx.FunArgs = CE->getArgs(); |
| 178 | LRCallCtx.PrevCtx = CallCtx; |
| 179 | buildMutexID(At->getArg(), &LRCallCtx); |
| 180 | return; |
| 181 | } |
| 182 | buildMutexID(CE->getCallee(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 183 | unsigned NumCallArgs = CE->getNumArgs(); |
| 184 | Expr** CallArgs = CE->getArgs(); |
| 185 | for (unsigned i = 0; i < NumCallArgs; ++i) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 186 | buildMutexID(CallArgs[i], CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 187 | } |
| 188 | } else if (BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 189 | buildMutexID(BOE->getLHS(), CallCtx); |
| 190 | buildMutexID(BOE->getRHS(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 191 | } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 192 | buildMutexID(UOE->getSubExpr(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 193 | } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 194 | buildMutexID(ASE->getBase(), CallCtx); |
| 195 | buildMutexID(ASE->getIdx(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 196 | } else if (AbstractConditionalOperator *CE = |
| 197 | dyn_cast<AbstractConditionalOperator>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 198 | buildMutexID(CE->getCond(), CallCtx); |
| 199 | buildMutexID(CE->getTrueExpr(), CallCtx); |
| 200 | buildMutexID(CE->getFalseExpr(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 201 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 202 | buildMutexID(CE->getCond(), CallCtx); |
| 203 | buildMutexID(CE->getLHS(), CallCtx); |
| 204 | buildMutexID(CE->getRHS(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 205 | } else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 206 | buildMutexID(CE->getSubExpr(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 207 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 208 | buildMutexID(PE->getSubExpr(), CallCtx); |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 209 | } else if (isa<CharacterLiteral>(Exp) || |
| 210 | isa<CXXNullPtrLiteralExpr>(Exp) || |
| 211 | isa<GNUNullExpr>(Exp) || |
| 212 | isa<CXXBoolLiteralExpr>(Exp) || |
| 213 | isa<FloatingLiteral>(Exp) || |
| 214 | isa<ImaginaryLiteral>(Exp) || |
| 215 | isa<IntegerLiteral>(Exp) || |
| 216 | isa<StringLiteral>(Exp) || |
| 217 | isa<ObjCStringLiteral>(Exp)) { |
| 218 | return; // FIXME: Ignore literals for now |
| 219 | } else { |
| 220 | // Ignore. FIXME: mark as invalid expression? |
| 221 | } |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /// \brief Construct a MutexID from an expression. |
| 225 | /// \param MutexExp The original mutex expression within an attribute |
| 226 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 227 | /// occurs. |
| 228 | /// \param D The declaration to which the lock/unlock attribute is attached. |
| 229 | void buildMutexIDFromExp(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 230 | CallingContext CallCtx(D); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 231 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 232 | // If we are processing a raw attribute expression, with no substitutions. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 233 | if (DeclExp == 0) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 234 | buildMutexID(MutexExp, 0); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 238 | // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 239 | // for formal parameters when we call buildMutexID later. |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 240 | if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 241 | CallCtx.SelfArg = ME->getBase(); |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 242 | } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 243 | CallCtx.SelfArg = CE->getImplicitObjectArgument(); |
| 244 | CallCtx.NumArgs = CE->getNumArgs(); |
| 245 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | df49782 | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 246 | } else if (CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 247 | CallCtx.NumArgs = CE->getNumArgs(); |
| 248 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 249 | } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 250 | CallCtx.SelfArg = 0; // FIXME -- get the parent from DeclStmt |
| 251 | CallCtx.NumArgs = CE->getNumArgs(); |
| 252 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 253 | } else if (D && isa<CXXDestructorDecl>(D)) { |
| 254 | // There's no such thing as a "destructor call" in the AST. |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 255 | CallCtx.SelfArg = DeclExp; |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 256 | } |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 257 | |
| 258 | // If the attribute has no arguments, then assume the argument is "this". |
| 259 | if (MutexExp == 0) { |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 260 | buildMutexID(CallCtx.SelfArg, 0); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 261 | return; |
| 262 | } |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 263 | |
DeLesley Hutchins | f63797c | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 264 | // For most attributes. |
| 265 | buildMutexID(MutexExp, &CallCtx); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | public: |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 269 | explicit MutexID(clang::Decl::EmptyShell e) { |
| 270 | DeclSeq.clear(); |
| 271 | } |
| 272 | |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 273 | /// \param MutexExp The original mutex expression within an attribute |
| 274 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 275 | /// occurs. |
| 276 | /// \param D The declaration to which the lock/unlock attribute is attached. |
| 277 | /// Caller must check isValid() after construction. |
| 278 | MutexID(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D) { |
| 279 | buildMutexIDFromExp(MutexExp, DeclExp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 282 | /// Return true if this is a valid decl sequence. |
| 283 | /// Caller must call this by hand after construction to handle errors. |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 284 | bool isValid() const { |
| 285 | return !DeclSeq.empty(); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 286 | } |
| 287 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 288 | /// Issue a warning about an invalid lock expression |
| 289 | static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp, |
| 290 | Expr *DeclExp, const NamedDecl* D) { |
| 291 | SourceLocation Loc; |
| 292 | if (DeclExp) |
| 293 | Loc = DeclExp->getExprLoc(); |
| 294 | |
| 295 | // FIXME: add a note about the attribute location in MutexExp or D |
| 296 | if (Loc.isValid()) |
| 297 | Handler.handleInvalidLockExp(Loc); |
| 298 | } |
| 299 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 300 | bool operator==(const MutexID &other) const { |
| 301 | return DeclSeq == other.DeclSeq; |
| 302 | } |
| 303 | |
| 304 | bool operator!=(const MutexID &other) const { |
| 305 | return !(*this == other); |
| 306 | } |
| 307 | |
| 308 | // SmallVector overloads Operator< to do lexicographic ordering. Note that |
| 309 | // we use pointer equality (and <) to compare NamedDecls. This means the order |
| 310 | // of MutexIDs in a lockset is nondeterministic. In order to output |
| 311 | // diagnostics in a deterministic ordering, we must order all diagnostics to |
| 312 | // output by SourceLocation when iterating through this lockset. |
| 313 | bool operator<(const MutexID &other) const { |
| 314 | return DeclSeq < other.DeclSeq; |
| 315 | } |
| 316 | |
| 317 | /// \brief Returns the name of the first Decl in the list for a given MutexID; |
| 318 | /// e.g. the lock expression foo.bar() has name "bar". |
| 319 | /// The caret will point unambiguously to the lock expression, so using this |
| 320 | /// name in diagnostics is a way to get simple, and consistent, mutex names. |
| 321 | /// We do not want to output the entire expression text for security reasons. |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 322 | std::string getName() const { |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 323 | assert(isValid()); |
DeLesley Hutchins | 4bda3ec | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 324 | if (!DeclSeq.front()) |
| 325 | return "this"; // Use 0 to represent 'this'. |
DeLesley Hutchins | 0d95dfc | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 326 | return DeclSeq.front()->getNameAsString(); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 330 | for (SmallVectorImpl<NamedDecl*>::const_iterator I = DeclSeq.begin(), |
| 331 | E = DeclSeq.end(); I != E; ++I) { |
| 332 | ID.AddPointer(*I); |
| 333 | } |
| 334 | } |
| 335 | }; |
| 336 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 337 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 338 | /// \brief This is a helper class that stores info about the most recent |
| 339 | /// accquire of a Lock. |
| 340 | /// |
| 341 | /// The main body of the analysis maps MutexIDs to LockDatas. |
| 342 | struct LockData { |
| 343 | SourceLocation AcquireLoc; |
| 344 | |
| 345 | /// \brief LKind stores whether a lock is held shared or exclusively. |
| 346 | /// Note that this analysis does not currently support either re-entrant |
| 347 | /// locking or lock "upgrading" and "downgrading" between exclusive and |
| 348 | /// shared. |
| 349 | /// |
| 350 | /// FIXME: add support for re-entrant locking and lock up/downgrading |
| 351 | LockKind LKind; |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 352 | bool Managed; // for ScopedLockable objects |
| 353 | MutexID UnderlyingMutex; // for ScopedLockable objects |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 354 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 355 | LockData(SourceLocation AcquireLoc, LockKind LKind, bool M = false) |
| 356 | : AcquireLoc(AcquireLoc), LKind(LKind), Managed(M), |
| 357 | UnderlyingMutex(Decl::EmptyShell()) |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 358 | {} |
| 359 | |
| 360 | LockData(SourceLocation AcquireLoc, LockKind LKind, const MutexID &Mu) |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 361 | : AcquireLoc(AcquireLoc), LKind(LKind), Managed(false), |
| 362 | UnderlyingMutex(Mu) |
| 363 | {} |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 364 | |
| 365 | bool operator==(const LockData &other) const { |
| 366 | return AcquireLoc == other.AcquireLoc && LKind == other.LKind; |
| 367 | } |
| 368 | |
| 369 | bool operator!=(const LockData &other) const { |
| 370 | return !(*this == other); |
| 371 | } |
| 372 | |
| 373 | void Profile(llvm::FoldingSetNodeID &ID) const { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 374 | ID.AddInteger(AcquireLoc.getRawEncoding()); |
| 375 | ID.AddInteger(LKind); |
| 376 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 379 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 380 | /// A Lockset maps each MutexID (defined above) to information about how it has |
| 381 | /// been locked. |
| 382 | typedef llvm::ImmutableMap<MutexID, LockData> Lockset; |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 383 | typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 384 | |
| 385 | class LocalVariableMap; |
| 386 | |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 387 | /// A side (entry or exit) of a CFG node. |
| 388 | enum CFGBlockSide { CBS_Entry, CBS_Exit }; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 389 | |
| 390 | /// CFGBlockInfo is a struct which contains all the information that is |
| 391 | /// maintained for each block in the CFG. See LocalVariableMap for more |
| 392 | /// information about the contexts. |
| 393 | struct CFGBlockInfo { |
| 394 | Lockset EntrySet; // Lockset held at entry to block |
| 395 | Lockset ExitSet; // Lockset held at exit from block |
| 396 | LocalVarContext EntryContext; // Context held at entry to block |
| 397 | LocalVarContext ExitContext; // Context held at exit from block |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 398 | SourceLocation EntryLoc; // Location of first statement in block |
| 399 | SourceLocation ExitLoc; // Location of last statement in block. |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 400 | unsigned EntryIndex; // Used to replay contexts later |
| 401 | |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 402 | const Lockset &getSet(CFGBlockSide Side) const { |
| 403 | return Side == CBS_Entry ? EntrySet : ExitSet; |
| 404 | } |
| 405 | SourceLocation getLocation(CFGBlockSide Side) const { |
| 406 | return Side == CBS_Entry ? EntryLoc : ExitLoc; |
| 407 | } |
| 408 | |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 409 | private: |
| 410 | CFGBlockInfo(Lockset EmptySet, LocalVarContext EmptyCtx) |
| 411 | : EntrySet(EmptySet), ExitSet(EmptySet), |
| 412 | EntryContext(EmptyCtx), ExitContext(EmptyCtx) |
| 413 | { } |
| 414 | |
| 415 | public: |
| 416 | static CFGBlockInfo getEmptyBlockInfo(Lockset::Factory &F, |
| 417 | LocalVariableMap &M); |
| 418 | }; |
| 419 | |
| 420 | |
| 421 | |
| 422 | // A LocalVariableMap maintains a map from local variables to their currently |
| 423 | // valid definitions. It provides SSA-like functionality when traversing the |
| 424 | // CFG. Like SSA, each definition or assignment to a variable is assigned a |
| 425 | // unique name (an integer), which acts as the SSA name for that definition. |
| 426 | // The total set of names is shared among all CFG basic blocks. |
| 427 | // Unlike SSA, we do not rewrite expressions to replace local variables declrefs |
| 428 | // with their SSA-names. Instead, we compute a Context for each point in the |
| 429 | // code, which maps local variables to the appropriate SSA-name. This map |
| 430 | // changes with each assignment. |
| 431 | // |
| 432 | // The map is computed in a single pass over the CFG. Subsequent analyses can |
| 433 | // then query the map to find the appropriate Context for a statement, and use |
| 434 | // that Context to look up the definitions of variables. |
| 435 | class LocalVariableMap { |
| 436 | public: |
| 437 | typedef LocalVarContext Context; |
| 438 | |
| 439 | /// A VarDefinition consists of an expression, representing the value of the |
| 440 | /// variable, along with the context in which that expression should be |
| 441 | /// interpreted. A reference VarDefinition does not itself contain this |
| 442 | /// information, but instead contains a pointer to a previous VarDefinition. |
| 443 | struct VarDefinition { |
| 444 | public: |
| 445 | friend class LocalVariableMap; |
| 446 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 447 | const NamedDecl *Dec; // The original declaration for this variable. |
| 448 | const Expr *Exp; // The expression for this variable, OR |
| 449 | unsigned Ref; // Reference to another VarDefinition |
| 450 | Context Ctx; // The map with which Exp should be interpreted. |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 451 | |
| 452 | bool isReference() { return !Exp; } |
| 453 | |
| 454 | private: |
| 455 | // Create ordinary variable definition |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 456 | VarDefinition(const NamedDecl *D, const Expr *E, Context C) |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 457 | : Dec(D), Exp(E), Ref(0), Ctx(C) |
| 458 | { } |
| 459 | |
| 460 | // Create reference to previous definition |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 461 | VarDefinition(const NamedDecl *D, unsigned R, Context C) |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 462 | : Dec(D), Exp(0), Ref(R), Ctx(C) |
| 463 | { } |
| 464 | }; |
| 465 | |
| 466 | private: |
| 467 | Context::Factory ContextFactory; |
| 468 | std::vector<VarDefinition> VarDefinitions; |
| 469 | std::vector<unsigned> CtxIndices; |
| 470 | std::vector<std::pair<Stmt*, Context> > SavedContexts; |
| 471 | |
| 472 | public: |
| 473 | LocalVariableMap() { |
| 474 | // index 0 is a placeholder for undefined variables (aka phi-nodes). |
| 475 | VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext())); |
| 476 | } |
| 477 | |
| 478 | /// Look up a definition, within the given context. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 479 | const VarDefinition* lookup(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 480 | const unsigned *i = Ctx.lookup(D); |
| 481 | if (!i) |
| 482 | return 0; |
| 483 | assert(*i < VarDefinitions.size()); |
| 484 | return &VarDefinitions[*i]; |
| 485 | } |
| 486 | |
| 487 | /// Look up the definition for D within the given context. Returns |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 488 | /// NULL if the expression is not statically known. If successful, also |
| 489 | /// modifies Ctx to hold the context of the return Expr. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 490 | const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 491 | const unsigned *P = Ctx.lookup(D); |
| 492 | if (!P) |
| 493 | return 0; |
| 494 | |
| 495 | unsigned i = *P; |
| 496 | while (i > 0) { |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 497 | if (VarDefinitions[i].Exp) { |
| 498 | Ctx = VarDefinitions[i].Ctx; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 499 | return VarDefinitions[i].Exp; |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 500 | } |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 501 | i = VarDefinitions[i].Ref; |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | Context getEmptyContext() { return ContextFactory.getEmptyMap(); } |
| 507 | |
| 508 | /// Return the next context after processing S. This function is used by |
| 509 | /// clients of the class to get the appropriate context when traversing the |
| 510 | /// CFG. It must be called for every assignment or DeclStmt. |
| 511 | Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) { |
| 512 | if (SavedContexts[CtxIndex+1].first == S) { |
| 513 | CtxIndex++; |
| 514 | Context Result = SavedContexts[CtxIndex].second; |
| 515 | return Result; |
| 516 | } |
| 517 | return C; |
| 518 | } |
| 519 | |
| 520 | void dumpVarDefinitionName(unsigned i) { |
| 521 | if (i == 0) { |
| 522 | llvm::errs() << "Undefined"; |
| 523 | return; |
| 524 | } |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 525 | const NamedDecl *Dec = VarDefinitions[i].Dec; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 526 | if (!Dec) { |
| 527 | llvm::errs() << "<<NULL>>"; |
| 528 | return; |
| 529 | } |
| 530 | Dec->printName(llvm::errs()); |
| 531 | llvm::errs() << "." << i << " " << ((void*) Dec); |
| 532 | } |
| 533 | |
| 534 | /// Dumps an ASCII representation of the variable map to llvm::errs() |
| 535 | void dump() { |
| 536 | for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 537 | const Expr *Exp = VarDefinitions[i].Exp; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 538 | unsigned Ref = VarDefinitions[i].Ref; |
| 539 | |
| 540 | dumpVarDefinitionName(i); |
| 541 | llvm::errs() << " = "; |
| 542 | if (Exp) Exp->dump(); |
| 543 | else { |
| 544 | dumpVarDefinitionName(Ref); |
| 545 | llvm::errs() << "\n"; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /// Dumps an ASCII representation of a Context to llvm::errs() |
| 551 | void dumpContext(Context C) { |
| 552 | for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 553 | const NamedDecl *D = I.getKey(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 554 | D->printName(llvm::errs()); |
| 555 | const unsigned *i = C.lookup(D); |
| 556 | llvm::errs() << " -> "; |
| 557 | dumpVarDefinitionName(*i); |
| 558 | llvm::errs() << "\n"; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /// Builds the variable map. |
| 563 | void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph, |
| 564 | std::vector<CFGBlockInfo> &BlockInfo); |
| 565 | |
| 566 | protected: |
| 567 | // Get the current context index |
| 568 | unsigned getContextIndex() { return SavedContexts.size()-1; } |
| 569 | |
| 570 | // Save the current context for later replay |
| 571 | void saveContext(Stmt *S, Context C) { |
| 572 | SavedContexts.push_back(std::make_pair(S,C)); |
| 573 | } |
| 574 | |
| 575 | // Adds a new definition to the given context, and returns a new context. |
| 576 | // This method should be called when declaring a new variable. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 577 | Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 578 | assert(!Ctx.contains(D)); |
| 579 | unsigned newID = VarDefinitions.size(); |
| 580 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 581 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 582 | return NewCtx; |
| 583 | } |
| 584 | |
| 585 | // Add a new reference to an existing definition. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 586 | Context addReference(const NamedDecl *D, unsigned i, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 587 | unsigned newID = VarDefinitions.size(); |
| 588 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 589 | VarDefinitions.push_back(VarDefinition(D, i, Ctx)); |
| 590 | return NewCtx; |
| 591 | } |
| 592 | |
| 593 | // Updates a definition only if that definition is already in the map. |
| 594 | // This method should be called when assigning to an existing variable. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 595 | Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 596 | if (Ctx.contains(D)) { |
| 597 | unsigned newID = VarDefinitions.size(); |
| 598 | Context NewCtx = ContextFactory.remove(Ctx, D); |
| 599 | NewCtx = ContextFactory.add(NewCtx, D, newID); |
| 600 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 601 | return NewCtx; |
| 602 | } |
| 603 | return Ctx; |
| 604 | } |
| 605 | |
| 606 | // Removes a definition from the context, but keeps the variable name |
| 607 | // as a valid variable. The index 0 is a placeholder for cleared definitions. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 608 | Context clearDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 609 | Context NewCtx = Ctx; |
| 610 | if (NewCtx.contains(D)) { |
| 611 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 612 | NewCtx = ContextFactory.add(NewCtx, D, 0); |
| 613 | } |
| 614 | return NewCtx; |
| 615 | } |
| 616 | |
| 617 | // Remove a definition entirely frmo the context. |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 618 | Context removeDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 619 | Context NewCtx = Ctx; |
| 620 | if (NewCtx.contains(D)) { |
| 621 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 622 | } |
| 623 | return NewCtx; |
| 624 | } |
| 625 | |
| 626 | Context intersectContexts(Context C1, Context C2); |
| 627 | Context createReferenceContext(Context C); |
| 628 | void intersectBackEdge(Context C1, Context C2); |
| 629 | |
| 630 | friend class VarMapBuilder; |
| 631 | }; |
| 632 | |
| 633 | |
| 634 | // This has to be defined after LocalVariableMap. |
| 635 | CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(Lockset::Factory &F, |
| 636 | LocalVariableMap &M) { |
| 637 | return CFGBlockInfo(F.getEmptyMap(), M.getEmptyContext()); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | /// Visitor which builds a LocalVariableMap |
| 642 | class VarMapBuilder : public StmtVisitor<VarMapBuilder> { |
| 643 | public: |
| 644 | LocalVariableMap* VMap; |
| 645 | LocalVariableMap::Context Ctx; |
| 646 | |
| 647 | VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C) |
| 648 | : VMap(VM), Ctx(C) {} |
| 649 | |
| 650 | void VisitDeclStmt(DeclStmt *S); |
| 651 | void VisitBinaryOperator(BinaryOperator *BO); |
| 652 | }; |
| 653 | |
| 654 | |
| 655 | // Add new local variables to the variable map |
| 656 | void VarMapBuilder::VisitDeclStmt(DeclStmt *S) { |
| 657 | bool modifiedCtx = false; |
| 658 | DeclGroupRef DGrp = S->getDeclGroup(); |
| 659 | for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) { |
| 660 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) { |
| 661 | Expr *E = VD->getInit(); |
| 662 | |
| 663 | // Add local variables with trivial type to the variable map |
| 664 | QualType T = VD->getType(); |
| 665 | if (T.isTrivialType(VD->getASTContext())) { |
| 666 | Ctx = VMap->addDefinition(VD, E, Ctx); |
| 667 | modifiedCtx = true; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | if (modifiedCtx) |
| 672 | VMap->saveContext(S, Ctx); |
| 673 | } |
| 674 | |
| 675 | // Update local variable definitions in variable map |
| 676 | void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) { |
| 677 | if (!BO->isAssignmentOp()) |
| 678 | return; |
| 679 | |
| 680 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
| 681 | |
| 682 | // Update the variable map and current context. |
| 683 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) { |
| 684 | ValueDecl *VDec = DRE->getDecl(); |
| 685 | if (Ctx.lookup(VDec)) { |
| 686 | if (BO->getOpcode() == BO_Assign) |
| 687 | Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx); |
| 688 | else |
| 689 | // FIXME -- handle compound assignment operators |
| 690 | Ctx = VMap->clearDefinition(VDec, Ctx); |
| 691 | VMap->saveContext(BO, Ctx); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | // Computes the intersection of two contexts. The intersection is the |
| 698 | // set of variables which have the same definition in both contexts; |
| 699 | // variables with different definitions are discarded. |
| 700 | LocalVariableMap::Context |
| 701 | LocalVariableMap::intersectContexts(Context C1, Context C2) { |
| 702 | Context Result = C1; |
| 703 | for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 704 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 705 | unsigned i1 = I.getData(); |
| 706 | const unsigned *i2 = C2.lookup(Dec); |
| 707 | if (!i2) // variable doesn't exist on second path |
| 708 | Result = removeDefinition(Dec, Result); |
| 709 | else if (*i2 != i1) // variable exists, but has different definition |
| 710 | Result = clearDefinition(Dec, Result); |
| 711 | } |
| 712 | return Result; |
| 713 | } |
| 714 | |
| 715 | // For every variable in C, create a new variable that refers to the |
| 716 | // definition in C. Return a new context that contains these new variables. |
| 717 | // (We use this for a naive implementation of SSA on loop back-edges.) |
| 718 | LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) { |
| 719 | Context Result = getEmptyContext(); |
| 720 | for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 721 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 722 | unsigned i = I.getData(); |
| 723 | Result = addReference(Dec, i, Result); |
| 724 | } |
| 725 | return Result; |
| 726 | } |
| 727 | |
| 728 | // This routine also takes the intersection of C1 and C2, but it does so by |
| 729 | // altering the VarDefinitions. C1 must be the result of an earlier call to |
| 730 | // createReferenceContext. |
| 731 | void LocalVariableMap::intersectBackEdge(Context C1, Context C2) { |
| 732 | for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 733 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 734 | unsigned i1 = I.getData(); |
| 735 | VarDefinition *VDef = &VarDefinitions[i1]; |
| 736 | assert(VDef->isReference()); |
| 737 | |
| 738 | const unsigned *i2 = C2.lookup(Dec); |
| 739 | if (!i2 || (*i2 != i1)) |
| 740 | VDef->Ref = 0; // Mark this variable as undefined |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | |
| 745 | // Traverse the CFG in topological order, so all predecessors of a block |
| 746 | // (excluding back-edges) are visited before the block itself. At |
| 747 | // each point in the code, we calculate a Context, which holds the set of |
| 748 | // variable definitions which are visible at that point in execution. |
| 749 | // Visible variables are mapped to their definitions using an array that |
| 750 | // contains all definitions. |
| 751 | // |
| 752 | // At join points in the CFG, the set is computed as the intersection of |
| 753 | // the incoming sets along each edge, E.g. |
| 754 | // |
| 755 | // { Context | VarDefinitions } |
| 756 | // int x = 0; { x -> x1 | x1 = 0 } |
| 757 | // int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 758 | // if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... } |
| 759 | // else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... } |
| 760 | // ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... } |
| 761 | // |
| 762 | // This is essentially a simpler and more naive version of the standard SSA |
| 763 | // algorithm. Those definitions that remain in the intersection are from blocks |
| 764 | // that strictly dominate the current block. We do not bother to insert proper |
| 765 | // phi nodes, because they are not used in our analysis; instead, wherever |
| 766 | // a phi node would be required, we simply remove that definition from the |
| 767 | // context (E.g. x above). |
| 768 | // |
| 769 | // The initial traversal does not capture back-edges, so those need to be |
| 770 | // handled on a separate pass. Whenever the first pass encounters an |
| 771 | // incoming back edge, it duplicates the context, creating new definitions |
| 772 | // that refer back to the originals. (These correspond to places where SSA |
| 773 | // might have to insert a phi node.) On the second pass, these definitions are |
| 774 | // set to NULL if the the variable has changed on the back-edge (i.e. a phi |
| 775 | // node was actually required.) E.g. |
| 776 | // |
| 777 | // { Context | VarDefinitions } |
| 778 | // int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 779 | // while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; } |
| 780 | // x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... } |
| 781 | // ... { y -> y1 | x3 = 2, x2 = 1, ... } |
| 782 | // |
| 783 | void LocalVariableMap::traverseCFG(CFG *CFGraph, |
| 784 | PostOrderCFGView *SortedGraph, |
| 785 | std::vector<CFGBlockInfo> &BlockInfo) { |
| 786 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
| 787 | |
| 788 | CtxIndices.resize(CFGraph->getNumBlockIDs()); |
| 789 | |
| 790 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 791 | E = SortedGraph->end(); I!= E; ++I) { |
| 792 | const CFGBlock *CurrBlock = *I; |
| 793 | int CurrBlockID = CurrBlock->getBlockID(); |
| 794 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
| 795 | |
| 796 | VisitedBlocks.insert(CurrBlock); |
| 797 | |
| 798 | // Calculate the entry context for the current block |
| 799 | bool HasBackEdges = false; |
| 800 | bool CtxInit = true; |
| 801 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 802 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 803 | // if *PI -> CurrBlock is a back edge, so skip it |
| 804 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) { |
| 805 | HasBackEdges = true; |
| 806 | continue; |
| 807 | } |
| 808 | |
| 809 | int PrevBlockID = (*PI)->getBlockID(); |
| 810 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 811 | |
| 812 | if (CtxInit) { |
| 813 | CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext; |
| 814 | CtxInit = false; |
| 815 | } |
| 816 | else { |
| 817 | CurrBlockInfo->EntryContext = |
| 818 | intersectContexts(CurrBlockInfo->EntryContext, |
| 819 | PrevBlockInfo->ExitContext); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | // Duplicate the context if we have back-edges, so we can call |
| 824 | // intersectBackEdges later. |
| 825 | if (HasBackEdges) |
| 826 | CurrBlockInfo->EntryContext = |
| 827 | createReferenceContext(CurrBlockInfo->EntryContext); |
| 828 | |
| 829 | // Create a starting context index for the current block |
| 830 | saveContext(0, CurrBlockInfo->EntryContext); |
| 831 | CurrBlockInfo->EntryIndex = getContextIndex(); |
| 832 | |
| 833 | // Visit all the statements in the basic block. |
| 834 | VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext); |
| 835 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 836 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 837 | switch (BI->getKind()) { |
| 838 | case CFGElement::Statement: { |
| 839 | const CFGStmt *CS = cast<CFGStmt>(&*BI); |
| 840 | VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt())); |
| 841 | break; |
| 842 | } |
| 843 | default: |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | CurrBlockInfo->ExitContext = VMapBuilder.Ctx; |
| 848 | |
| 849 | // Mark variables on back edges as "unknown" if they've been changed. |
| 850 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 851 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 852 | // if CurrBlock -> *SI is *not* a back edge |
| 853 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
| 854 | continue; |
| 855 | |
| 856 | CFGBlock *FirstLoopBlock = *SI; |
| 857 | Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext; |
| 858 | Context LoopEnd = CurrBlockInfo->ExitContext; |
| 859 | intersectBackEdge(LoopBegin, LoopEnd); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // Put an extra entry at the end of the indexed context array |
| 864 | unsigned exitID = CFGraph->getExit().getBlockID(); |
| 865 | saveContext(0, BlockInfo[exitID].ExitContext); |
| 866 | } |
| 867 | |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 868 | /// Find the appropriate source locations to use when producing diagnostics for |
| 869 | /// each block in the CFG. |
| 870 | static void findBlockLocations(CFG *CFGraph, |
| 871 | PostOrderCFGView *SortedGraph, |
| 872 | std::vector<CFGBlockInfo> &BlockInfo) { |
| 873 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 874 | E = SortedGraph->end(); I!= E; ++I) { |
| 875 | const CFGBlock *CurrBlock = *I; |
| 876 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()]; |
| 877 | |
| 878 | // Find the source location of the last statement in the block, if the |
| 879 | // block is not empty. |
| 880 | if (const Stmt *S = CurrBlock->getTerminator()) { |
| 881 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart(); |
| 882 | } else { |
| 883 | for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(), |
| 884 | BE = CurrBlock->rend(); BI != BE; ++BI) { |
| 885 | // FIXME: Handle other CFGElement kinds. |
| 886 | if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) { |
| 887 | CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart(); |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | if (!CurrBlockInfo->ExitLoc.isInvalid()) { |
| 894 | // This block contains at least one statement. Find the source location |
| 895 | // of the first statement in the block. |
| 896 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 897 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 898 | // FIXME: Handle other CFGElement kinds. |
| 899 | if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) { |
| 900 | CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart(); |
| 901 | break; |
| 902 | } |
| 903 | } |
| 904 | } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() && |
| 905 | CurrBlock != &CFGraph->getExit()) { |
| 906 | // The block is empty, and has a single predecessor. Use its exit |
| 907 | // location. |
| 908 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = |
| 909 | BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc; |
| 910 | } |
| 911 | } |
| 912 | } |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 913 | |
| 914 | /// \brief Class which implements the core thread safety analysis routines. |
| 915 | class ThreadSafetyAnalyzer { |
| 916 | friend class BuildLockset; |
| 917 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 918 | ThreadSafetyHandler &Handler; |
| 919 | Lockset::Factory LocksetFactory; |
| 920 | LocalVariableMap LocalVarMap; |
| 921 | std::vector<CFGBlockInfo> BlockInfo; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 922 | |
| 923 | public: |
| 924 | ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {} |
| 925 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 926 | Lockset addLock(const Lockset &LSet, const MutexID &Mutex, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 927 | const LockData &LDat, bool Warn=true); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 928 | Lockset addLock(const Lockset &LSet, Expr *MutexExp, const NamedDecl *D, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 929 | const LockData &LDat, bool Warn=true); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 930 | Lockset removeLock(const Lockset &LSet, const MutexID &Mutex, |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 931 | SourceLocation UnlockLoc, |
| 932 | bool Warn=true, bool FullyRemove=false); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 933 | |
| 934 | template <class AttrType> |
| 935 | Lockset addLocksToSet(const Lockset &LSet, LockKind LK, AttrType *Attr, |
| 936 | Expr *Exp, NamedDecl *D, VarDecl *VD = 0); |
| 937 | Lockset removeLocksFromSet(const Lockset &LSet, |
| 938 | UnlockFunctionAttr *Attr, |
| 939 | Expr *Exp, NamedDecl* FunDecl); |
| 940 | |
| 941 | template <class AttrType> |
| 942 | Lockset addTrylock(const Lockset &LSet, |
| 943 | LockKind LK, AttrType *Attr, Expr *Exp, NamedDecl *FunDecl, |
| 944 | const CFGBlock* PredBlock, const CFGBlock *CurrBlock, |
| 945 | Expr *BrE, bool Neg); |
| 946 | const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C, |
| 947 | bool &Negate); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 948 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 949 | Lockset getEdgeLockset(const Lockset &ExitSet, |
| 950 | const CFGBlock* PredBlock, |
| 951 | const CFGBlock *CurrBlock); |
| 952 | |
| 953 | Lockset intersectAndWarn(const Lockset &LSet1, const Lockset &LSet2, |
| 954 | SourceLocation JoinLoc, LockErrorKind LEK); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 955 | |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 956 | void runAnalysis(AnalysisDeclContext &AC); |
| 957 | }; |
| 958 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 959 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 960 | /// \brief Add a new lock to the lockset, warning if the lock is already there. |
| 961 | /// \param Mutex -- the Mutex expression for the lock |
| 962 | /// \param LDat -- the LockData for the lock |
| 963 | Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet, |
| 964 | const MutexID &Mutex, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 965 | const LockData &LDat, |
| 966 | bool Warn) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 967 | // FIXME: deal with acquired before/after annotations. |
| 968 | // FIXME: Don't always warn when we have support for reentrant locks. |
| 969 | if (LSet.lookup(Mutex)) { |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 970 | if (Warn) |
| 971 | Handler.handleDoubleLock(Mutex.getName(), LDat.AcquireLoc); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 972 | return LSet; |
| 973 | } else { |
| 974 | return LocksetFactory.add(LSet, Mutex, LDat); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | /// \brief Construct a new mutex and add it to the lockset. |
| 979 | Lockset ThreadSafetyAnalyzer::addLock(const Lockset &LSet, |
| 980 | Expr *MutexExp, const NamedDecl *D, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 981 | const LockData &LDat, |
| 982 | bool Warn) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 983 | MutexID Mutex(MutexExp, 0, D); |
| 984 | if (!Mutex.isValid()) { |
| 985 | MutexID::warnInvalidLock(Handler, MutexExp, 0, D); |
| 986 | return LSet; |
| 987 | } |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 988 | return addLock(LSet, Mutex, LDat, Warn); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
| 992 | /// \brief Remove a lock from the lockset, warning if the lock is not there. |
| 993 | /// \param LockExp The lock expression corresponding to the lock to be removed |
| 994 | /// \param UnlockLoc The source location of the unlock (only used in error msg) |
| 995 | Lockset ThreadSafetyAnalyzer::removeLock(const Lockset &LSet, |
| 996 | const MutexID &Mutex, |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 997 | SourceLocation UnlockLoc, |
| 998 | bool Warn, bool FullyRemove) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 999 | const LockData *LDat = LSet.lookup(Mutex); |
| 1000 | if (!LDat) { |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1001 | if (Warn) |
| 1002 | Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1003 | return LSet; |
| 1004 | } |
| 1005 | else { |
| 1006 | Lockset Result = LSet; |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1007 | if (LDat->UnderlyingMutex.isValid()) { |
| 1008 | // For scoped-lockable vars, remove the mutex associated with this var. |
| 1009 | Result = removeLock(Result, LDat->UnderlyingMutex, UnlockLoc, |
| 1010 | false, true); |
| 1011 | // Fully remove the object only when the destructor is called |
| 1012 | if (FullyRemove) |
| 1013 | return LocksetFactory.remove(Result, Mutex); |
| 1014 | else |
| 1015 | return Result; |
| 1016 | } |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1017 | return LocksetFactory.remove(Result, Mutex); |
| 1018 | } |
| 1019 | } |
| 1020 | |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1021 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1022 | /// \brief This function, parameterized by an attribute type, is used to add a |
| 1023 | /// set of locks specified as attribute arguments to the lockset. |
| 1024 | template <typename AttrType> |
| 1025 | Lockset ThreadSafetyAnalyzer::addLocksToSet(const Lockset &LSet, |
| 1026 | LockKind LK, AttrType *Attr, |
| 1027 | Expr *Exp, NamedDecl* FunDecl, |
| 1028 | VarDecl *VD) { |
| 1029 | typedef typename AttrType::args_iterator iterator_type; |
| 1030 | |
| 1031 | SourceLocation ExpLocation = Exp->getExprLoc(); |
| 1032 | |
| 1033 | // Figure out if we're calling the constructor of scoped lockable class |
| 1034 | bool isScopedVar = false; |
| 1035 | if (VD) { |
| 1036 | if (CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FunDecl)) { |
| 1037 | CXXRecordDecl* PD = CD->getParent(); |
| 1038 | if (PD && PD->getAttr<ScopedLockableAttr>()) |
| 1039 | isScopedVar = true; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (Attr->args_size() == 0) { |
| 1044 | // The mutex held is the "this" object. |
| 1045 | MutexID Mutex(0, Exp, FunDecl); |
| 1046 | if (!Mutex.isValid()) { |
| 1047 | MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl); |
| 1048 | return LSet; |
| 1049 | } |
| 1050 | else { |
| 1051 | return addLock(LSet, Mutex, LockData(ExpLocation, LK)); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | Lockset Result = LSet; |
| 1056 | for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) { |
| 1057 | MutexID Mutex(*I, Exp, FunDecl); |
| 1058 | if (!Mutex.isValid()) |
| 1059 | MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl); |
| 1060 | else { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1061 | if (isScopedVar) { |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1062 | // Mutex is managed by scoped var -- suppress certain warnings. |
| 1063 | Result = addLock(Result, Mutex, LockData(ExpLocation, LK, true)); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1064 | // For scoped lockable vars, map this var to its underlying mutex. |
| 1065 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation()); |
| 1066 | MutexID SMutex(&DRE, 0, 0); |
| 1067 | Result = addLock(Result, SMutex, |
| 1068 | LockData(VD->getLocation(), LK, Mutex)); |
| 1069 | } |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1070 | else { |
| 1071 | Result = addLock(Result, Mutex, LockData(ExpLocation, LK)); |
| 1072 | } |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | return Result; |
| 1076 | } |
| 1077 | |
| 1078 | /// \brief This function removes a set of locks specified as attribute |
| 1079 | /// arguments from the lockset. |
| 1080 | Lockset ThreadSafetyAnalyzer::removeLocksFromSet(const Lockset &LSet, |
| 1081 | UnlockFunctionAttr *Attr, |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1082 | Expr *Exp, |
| 1083 | NamedDecl* FunDecl) { |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1084 | SourceLocation ExpLocation; |
| 1085 | if (Exp) ExpLocation = Exp->getExprLoc(); |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1086 | bool Dtor = isa<CXXDestructorDecl>(FunDecl); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (Attr->args_size() == 0) { |
| 1089 | // The mutex held is the "this" object. |
| 1090 | MutexID Mu(0, Exp, FunDecl); |
| 1091 | if (!Mu.isValid()) { |
| 1092 | MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl); |
| 1093 | return LSet; |
| 1094 | } else { |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1095 | return removeLock(LSet, Mu, ExpLocation, true, Dtor); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | Lockset Result = LSet; |
| 1100 | for (UnlockFunctionAttr::args_iterator I = Attr->args_begin(), |
| 1101 | E = Attr->args_end(); I != E; ++I) { |
| 1102 | MutexID Mutex(*I, Exp, FunDecl); |
| 1103 | if (!Mutex.isValid()) |
| 1104 | MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl); |
| 1105 | else |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1106 | Result = removeLock(Result, Mutex, ExpLocation, true, Dtor); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1107 | } |
| 1108 | return Result; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | /// \brief Add lock to set, if the current block is in the taken branch of a |
| 1113 | /// trylock. |
| 1114 | template <class AttrType> |
| 1115 | Lockset ThreadSafetyAnalyzer::addTrylock(const Lockset &LSet, |
| 1116 | LockKind LK, AttrType *Attr, |
| 1117 | Expr *Exp, NamedDecl *FunDecl, |
| 1118 | const CFGBlock *PredBlock, |
| 1119 | const CFGBlock *CurrBlock, |
| 1120 | Expr *BrE, bool Neg) { |
| 1121 | // Find out which branch has the lock |
| 1122 | bool branch = 0; |
| 1123 | if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) { |
| 1124 | branch = BLE->getValue(); |
| 1125 | } |
| 1126 | else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) { |
| 1127 | branch = ILE->getValue().getBoolValue(); |
| 1128 | } |
| 1129 | int branchnum = branch ? 0 : 1; |
| 1130 | if (Neg) branchnum = !branchnum; |
| 1131 | |
| 1132 | Lockset Result = LSet; |
| 1133 | // If we've taken the trylock branch, then add the lock |
| 1134 | int i = 0; |
| 1135 | for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(), |
| 1136 | SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) { |
| 1137 | if (*SI == CurrBlock && i == branchnum) { |
| 1138 | Result = addLocksToSet(Result, LK, Attr, Exp, FunDecl, 0); |
| 1139 | } |
| 1140 | } |
| 1141 | return Result; |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | // If Cond can be traced back to a function call, return the call expression. |
| 1146 | // The negate variable should be called with false, and will be set to true |
| 1147 | // if the function call is negated, e.g. if (!mu.tryLock(...)) |
| 1148 | const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond, |
| 1149 | LocalVarContext C, |
| 1150 | bool &Negate) { |
| 1151 | if (!Cond) |
| 1152 | return 0; |
| 1153 | |
| 1154 | if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) { |
| 1155 | return CallExp; |
| 1156 | } |
| 1157 | else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) { |
| 1158 | return getTrylockCallExpr(CE->getSubExpr(), C, Negate); |
| 1159 | } |
| 1160 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) { |
| 1161 | const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C); |
| 1162 | return getTrylockCallExpr(E, C, Negate); |
| 1163 | } |
| 1164 | else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) { |
| 1165 | if (UOP->getOpcode() == UO_LNot) { |
| 1166 | Negate = !Negate; |
| 1167 | return getTrylockCallExpr(UOP->getSubExpr(), C, Negate); |
| 1168 | } |
| 1169 | } |
| 1170 | // FIXME -- handle && and || as well. |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
| 1174 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1175 | /// \brief Find the lockset that holds on the edge between PredBlock |
| 1176 | /// and CurrBlock. The edge set is the exit set of PredBlock (passed |
| 1177 | /// as the ExitSet parameter) plus any trylocks, which are conditionally held. |
| 1178 | Lockset ThreadSafetyAnalyzer::getEdgeLockset(const Lockset &ExitSet, |
| 1179 | const CFGBlock *PredBlock, |
| 1180 | const CFGBlock *CurrBlock) { |
| 1181 | if (!PredBlock->getTerminatorCondition()) |
| 1182 | return ExitSet; |
| 1183 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1184 | bool Negate = false; |
| 1185 | const Stmt *Cond = PredBlock->getTerminatorCondition(); |
| 1186 | const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()]; |
| 1187 | const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext; |
| 1188 | |
| 1189 | CallExpr *Exp = const_cast<CallExpr*>( |
| 1190 | getTrylockCallExpr(Cond, LVarCtx, Negate)); |
| 1191 | if (!Exp) |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1192 | return ExitSet; |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1193 | |
| 1194 | NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1195 | if(!FunDecl || !FunDecl->hasAttrs()) |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1196 | return ExitSet; |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1197 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1198 | Lockset Result = ExitSet; |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1199 | |
| 1200 | // If the condition is a call to a Trylock function, then grab the attributes |
| 1201 | AttrVec &ArgAttrs = FunDecl->getAttrs(); |
| 1202 | for (unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 1203 | Attr *Attr = ArgAttrs[i]; |
| 1204 | switch (Attr->getKind()) { |
| 1205 | case attr::ExclusiveTrylockFunction: { |
| 1206 | ExclusiveTrylockFunctionAttr *A = |
| 1207 | cast<ExclusiveTrylockFunctionAttr>(Attr); |
| 1208 | Result = addTrylock(Result, LK_Exclusive, A, Exp, FunDecl, |
| 1209 | PredBlock, CurrBlock, |
| 1210 | A->getSuccessValue(), Negate); |
| 1211 | break; |
| 1212 | } |
| 1213 | case attr::SharedTrylockFunction: { |
| 1214 | SharedTrylockFunctionAttr *A = |
| 1215 | cast<SharedTrylockFunctionAttr>(Attr); |
| 1216 | Result = addTrylock(Result, LK_Shared, A, Exp, FunDecl, |
| 1217 | PredBlock, CurrBlock, |
| 1218 | A->getSuccessValue(), Negate); |
| 1219 | break; |
| 1220 | } |
| 1221 | default: |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | return Result; |
| 1226 | } |
| 1227 | |
| 1228 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1229 | /// \brief We use this class to visit different types of expressions in |
| 1230 | /// CFGBlocks, and build up the lockset. |
| 1231 | /// An expression may cause us to add or remove locks from the lockset, or else |
| 1232 | /// output error messages related to missing locks. |
| 1233 | /// FIXME: In future, we may be able to not inherit from a visitor. |
| 1234 | class BuildLockset : public StmtVisitor<BuildLockset> { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1235 | friend class ThreadSafetyAnalyzer; |
| 1236 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1237 | ThreadSafetyAnalyzer *Analyzer; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1238 | Lockset LSet; |
| 1239 | LocalVariableMap::Context LVarCtx; |
| 1240 | unsigned CtxIndex; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Helper functions |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1243 | const ValueDecl *getValueDecl(Expr *Exp); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1244 | |
| 1245 | void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK, |
| 1246 | Expr *MutexExp, ProtectedOperationKind POK); |
| 1247 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1248 | void checkAccess(Expr *Exp, AccessKind AK); |
| 1249 | void checkDereference(Expr *Exp, AccessKind AK); |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1250 | void handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD = 0); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1251 | |
| 1252 | /// \brief Returns true if the lockset contains a lock, regardless of whether |
| 1253 | /// the lock is held exclusively or shared. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1254 | bool locksetContains(const MutexID &Lock) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1255 | return LSet.lookup(Lock); |
| 1256 | } |
| 1257 | |
| 1258 | /// \brief Returns true if the lockset contains a lock with the passed in |
| 1259 | /// locktype. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1260 | bool locksetContains(const MutexID &Lock, LockKind KindRequested) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1261 | const LockData *LockHeld = LSet.lookup(Lock); |
| 1262 | return (LockHeld && KindRequested == LockHeld->LKind); |
| 1263 | } |
| 1264 | |
| 1265 | /// \brief Returns true if the lockset contains a lock with at least the |
| 1266 | /// passed in locktype. So for example, if we pass in LK_Shared, this function |
| 1267 | /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in |
| 1268 | /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1269 | bool locksetContainsAtLeast(const MutexID &Lock, |
| 1270 | LockKind KindRequested) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1271 | switch (KindRequested) { |
| 1272 | case LK_Shared: |
| 1273 | return locksetContains(Lock); |
| 1274 | case LK_Exclusive: |
| 1275 | return locksetContains(Lock, KindRequested); |
| 1276 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 1277 | llvm_unreachable("Unknown LockKind"); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | public: |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1281 | BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info) |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1282 | : StmtVisitor<BuildLockset>(), |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1283 | Analyzer(Anlzr), |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1284 | LSet(Info.EntrySet), |
| 1285 | LVarCtx(Info.EntryContext), |
| 1286 | CtxIndex(Info.EntryIndex) |
| 1287 | {} |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1288 | |
| 1289 | void VisitUnaryOperator(UnaryOperator *UO); |
| 1290 | void VisitBinaryOperator(BinaryOperator *BO); |
| 1291 | void VisitCastExpr(CastExpr *CE); |
DeLesley Hutchins | df49782 | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1292 | void VisitCallExpr(CallExpr *Exp); |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1293 | void VisitCXXConstructExpr(CXXConstructExpr *Exp); |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1294 | void VisitDeclStmt(DeclStmt *S); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1295 | }; |
| 1296 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1297 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1298 | /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs |
| 1299 | const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) { |
| 1300 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp)) |
| 1301 | return DR->getDecl(); |
| 1302 | |
| 1303 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) |
| 1304 | return ME->getMemberDecl(); |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | /// \brief Warn if the LSet does not contain a lock sufficient to protect access |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1310 | /// of at least the passed in AccessKind. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1311 | void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, |
| 1312 | AccessKind AK, Expr *MutexExp, |
| 1313 | ProtectedOperationKind POK) { |
| 1314 | LockKind LK = getLockKindFromAccessKind(AK); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1315 | |
| 1316 | MutexID Mutex(MutexExp, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 1317 | if (!Mutex.isValid()) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1318 | MutexID::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 1319 | else if (!locksetContainsAtLeast(Mutex, LK)) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1320 | Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK, |
| 1321 | Exp->getExprLoc()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1324 | /// \brief This method identifies variable dereferences and checks pt_guarded_by |
| 1325 | /// and pt_guarded_var annotations. Note that we only check these annotations |
| 1326 | /// at the time a pointer is dereferenced. |
| 1327 | /// FIXME: We need to check for other types of pointer dereferences |
| 1328 | /// (e.g. [], ->) and deal with them here. |
| 1329 | /// \param Exp An expression that has been read or written. |
| 1330 | void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) { |
| 1331 | UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp); |
| 1332 | if (!UO || UO->getOpcode() != clang::UO_Deref) |
| 1333 | return; |
| 1334 | Exp = UO->getSubExpr()->IgnoreParenCasts(); |
| 1335 | |
| 1336 | const ValueDecl *D = getValueDecl(Exp); |
| 1337 | if(!D || !D->hasAttrs()) |
| 1338 | return; |
| 1339 | |
| 1340 | if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty()) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1341 | Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK, |
| 1342 | Exp->getExprLoc()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1343 | |
| 1344 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 1345 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 1346 | if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i])) |
| 1347 | warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference); |
| 1348 | } |
| 1349 | |
| 1350 | /// \brief Checks guarded_by and guarded_var attributes. |
| 1351 | /// Whenever we identify an access (read or write) of a DeclRefExpr or |
| 1352 | /// MemberExpr, we need to check whether there are any guarded_by or |
| 1353 | /// guarded_var attributes, and make sure we hold the appropriate mutexes. |
| 1354 | void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) { |
| 1355 | const ValueDecl *D = getValueDecl(Exp); |
| 1356 | if(!D || !D->hasAttrs()) |
| 1357 | return; |
| 1358 | |
| 1359 | if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty()) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1360 | Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK, |
| 1361 | Exp->getExprLoc()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1362 | |
| 1363 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 1364 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 1365 | if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i])) |
| 1366 | warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess); |
| 1367 | } |
| 1368 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1369 | /// \brief Process a function call, method call, constructor call, |
| 1370 | /// or destructor call. This involves looking at the attributes on the |
| 1371 | /// corresponding function/method/constructor/destructor, issuing warnings, |
| 1372 | /// and updating the locksets accordingly. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1373 | /// |
| 1374 | /// FIXME: For classes annotated with one of the guarded annotations, we need |
| 1375 | /// to treat const method calls as reads and non-const method calls as writes, |
| 1376 | /// and check that the appropriate locks are held. Non-const method calls with |
| 1377 | /// the same signature as const method calls can be also treated as reads. |
| 1378 | /// |
| 1379 | /// FIXME: We need to also visit CallExprs to catch/check global functions. |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 1380 | /// |
| 1381 | /// FIXME: Do not flag an error for member variables accessed in constructors/ |
| 1382 | /// destructors |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1383 | void BuildLockset::handleCall(Expr *Exp, NamedDecl *D, VarDecl *VD) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1384 | AttrVec &ArgAttrs = D->getAttrs(); |
| 1385 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 1386 | Attr *Attr = ArgAttrs[i]; |
| 1387 | switch (Attr->getKind()) { |
| 1388 | // When we encounter an exclusive lock function, we need to add the lock |
| 1389 | // to our lockset with kind exclusive. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1390 | case attr::ExclusiveLockFunction: { |
| 1391 | ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(Attr); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1392 | LSet = Analyzer->addLocksToSet(LSet, LK_Exclusive, A, Exp, D, VD); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1393 | break; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1394 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1395 | |
| 1396 | // When we encounter a shared lock function, we need to add the lock |
| 1397 | // to our lockset with kind shared. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1398 | case attr::SharedLockFunction: { |
| 1399 | SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(Attr); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1400 | LSet = Analyzer->addLocksToSet(LSet, LK_Shared, A, Exp, D, VD); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1401 | break; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1402 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1403 | |
| 1404 | // When we encounter an unlock function, we need to remove unlocked |
| 1405 | // mutexes from the lockset, and flag a warning if they are not there. |
| 1406 | case attr::UnlockFunction: { |
| 1407 | UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr); |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1408 | LSet = Analyzer->removeLocksFromSet(LSet, UFAttr, Exp, D); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | } |
| 1411 | |
| 1412 | case attr::ExclusiveLocksRequired: { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1413 | ExclusiveLocksRequiredAttr *ELRAttr = |
| 1414 | cast<ExclusiveLocksRequiredAttr>(Attr); |
| 1415 | |
| 1416 | for (ExclusiveLocksRequiredAttr::args_iterator |
| 1417 | I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I) |
| 1418 | warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall); |
| 1419 | break; |
| 1420 | } |
| 1421 | |
| 1422 | case attr::SharedLocksRequired: { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1423 | SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr); |
| 1424 | |
| 1425 | for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(), |
| 1426 | E = SLRAttr->args_end(); I != E; ++I) |
| 1427 | warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall); |
| 1428 | break; |
| 1429 | } |
| 1430 | |
| 1431 | case attr::LocksExcluded: { |
| 1432 | LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr); |
| 1433 | for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(), |
| 1434 | E = LEAttr->args_end(); I != E; ++I) { |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1435 | MutexID Mutex(*I, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 1436 | if (!Mutex.isValid()) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1437 | MutexID::warnInvalidLock(Analyzer->Handler, *I, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 1438 | else if (locksetContains(Mutex)) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1439 | Analyzer->Handler.handleFunExcludesLock(D->getName(), |
| 1440 | Mutex.getName(), |
| 1441 | Exp->getExprLoc()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1442 | } |
| 1443 | break; |
| 1444 | } |
| 1445 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1446 | // Ignore other (non thread-safety) attributes |
| 1447 | default: |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
DeLesley Hutchins | b4fa418 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1453 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1454 | /// \brief For unary operations which read and write a variable, we need to |
| 1455 | /// check whether we hold any required mutexes. Reads are checked in |
| 1456 | /// VisitCastExpr. |
| 1457 | void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { |
| 1458 | switch (UO->getOpcode()) { |
| 1459 | case clang::UO_PostDec: |
| 1460 | case clang::UO_PostInc: |
| 1461 | case clang::UO_PreDec: |
| 1462 | case clang::UO_PreInc: { |
| 1463 | Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 1464 | checkAccess(SubExp, AK_Written); |
| 1465 | checkDereference(SubExp, AK_Written); |
| 1466 | break; |
| 1467 | } |
| 1468 | default: |
| 1469 | break; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /// For binary operations which assign to a variable (writes), we need to check |
| 1474 | /// whether we hold any required mutexes. |
| 1475 | /// FIXME: Deal with non-primitive types. |
| 1476 | void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { |
| 1477 | if (!BO->isAssignmentOp()) |
| 1478 | return; |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1479 | |
| 1480 | // adjust the context |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1481 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1482 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1483 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
| 1484 | checkAccess(LHSExp, AK_Written); |
| 1485 | checkDereference(LHSExp, AK_Written); |
| 1486 | } |
| 1487 | |
| 1488 | /// Whenever we do an LValue to Rvalue cast, we are reading a variable and |
| 1489 | /// need to ensure we hold any required mutexes. |
| 1490 | /// FIXME: Deal with non-primitive types. |
| 1491 | void BuildLockset::VisitCastExpr(CastExpr *CE) { |
| 1492 | if (CE->getCastKind() != CK_LValueToRValue) |
| 1493 | return; |
| 1494 | Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts(); |
| 1495 | checkAccess(SubExp, AK_Read); |
| 1496 | checkDereference(SubExp, AK_Read); |
| 1497 | } |
| 1498 | |
| 1499 | |
DeLesley Hutchins | df49782 | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1500 | void BuildLockset::VisitCallExpr(CallExpr *Exp) { |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1501 | NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1502 | if(!D || !D->hasAttrs()) |
| 1503 | return; |
| 1504 | handleCall(Exp, D); |
| 1505 | } |
| 1506 | |
| 1507 | void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) { |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1508 | // FIXME -- only handles constructors in DeclStmt below. |
| 1509 | } |
| 1510 | |
| 1511 | void BuildLockset::VisitDeclStmt(DeclStmt *S) { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1512 | // adjust the context |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1513 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1514 | |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1515 | DeclGroupRef DGrp = S->getDeclGroup(); |
| 1516 | for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) { |
| 1517 | Decl *D = *I; |
| 1518 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) { |
| 1519 | Expr *E = VD->getInit(); |
| 1520 | if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) { |
| 1521 | NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor()); |
| 1522 | if (!CtorD || !CtorD->hasAttrs()) |
| 1523 | return; |
| 1524 | handleCall(CE, CtorD, VD); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1530 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1531 | |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1532 | /// \brief Compute the intersection of two locksets and issue warnings for any |
| 1533 | /// locks in the symmetric difference. |
| 1534 | /// |
| 1535 | /// This function is used at a merge point in the CFG when comparing the lockset |
| 1536 | /// of each branch being merged. For example, given the following sequence: |
| 1537 | /// A; if () then B; else C; D; we need to check that the lockset after B and C |
| 1538 | /// are the same. In the event of a difference, we use the intersection of these |
| 1539 | /// two locksets at the start of D. |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1540 | /// |
| 1541 | /// \param LSet1 The first lockset. |
| 1542 | /// \param LSet2 The second lockset. |
| 1543 | /// \param JoinLoc The location of the join point for error reporting |
| 1544 | /// \param LEK The error message to report. |
| 1545 | Lockset ThreadSafetyAnalyzer::intersectAndWarn(const Lockset &LSet1, |
| 1546 | const Lockset &LSet2, |
| 1547 | SourceLocation JoinLoc, |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1548 | LockErrorKind LEK) { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1549 | Lockset Intersection = LSet1; |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1550 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1551 | for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) { |
| 1552 | const MutexID &LSet2Mutex = I.getKey(); |
| 1553 | const LockData &LSet2LockData = I.getData(); |
| 1554 | if (const LockData *LD = LSet1.lookup(LSet2Mutex)) { |
| 1555 | if (LD->LKind != LSet2LockData.LKind) { |
| 1556 | Handler.handleExclusiveAndShared(LSet2Mutex.getName(), |
| 1557 | LSet2LockData.AcquireLoc, |
| 1558 | LD->AcquireLoc); |
| 1559 | if (LD->LKind != LK_Exclusive) |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1560 | Intersection = LocksetFactory.add(Intersection, LSet2Mutex, |
| 1561 | LSet2LockData); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1562 | } |
| 1563 | } else { |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1564 | if (!LSet2LockData.Managed) |
| 1565 | Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(), |
| 1566 | LSet2LockData.AcquireLoc, |
| 1567 | JoinLoc, LEK); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1570 | |
| 1571 | for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) { |
| 1572 | if (!LSet2.contains(I.getKey())) { |
| 1573 | const MutexID &Mutex = I.getKey(); |
| 1574 | const LockData &MissingLock = I.getData(); |
DeLesley Hutchins | c99a5d8 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (!MissingLock.Managed) |
| 1577 | Handler.handleMutexHeldEndOfScope(Mutex.getName(), |
| 1578 | MissingLock.AcquireLoc, |
| 1579 | JoinLoc, LEK); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1580 | Intersection = LocksetFactory.remove(Intersection, Mutex); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | return Intersection; |
| 1584 | } |
| 1585 | |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1586 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1587 | /// \brief Check a function's CFG for thread-safety violations. |
| 1588 | /// |
| 1589 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 1590 | /// at the end of each block, and issue warnings for thread safety violations. |
| 1591 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 1592 | void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1593 | CFG *CFGraph = AC.getCFG(); |
| 1594 | if (!CFGraph) return; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1595 | const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl()); |
| 1596 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1597 | // AC.dumpCFG(true); |
| 1598 | |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1599 | if (!D) |
| 1600 | return; // Ignore anonymous functions for now. |
| 1601 | if (D->getAttr<NoThreadSafetyAnalysisAttr>()) |
| 1602 | return; |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1603 | // FIXME: Do something a bit more intelligent inside constructor and |
| 1604 | // destructor code. Constructors and destructors must assume unique access |
| 1605 | // to 'this', so checks on member variable access is disabled, but we should |
| 1606 | // still enable checks on other objects. |
| 1607 | if (isa<CXXConstructorDecl>(D)) |
| 1608 | return; // Don't check inside constructors. |
| 1609 | if (isa<CXXDestructorDecl>(D)) |
| 1610 | return; // Don't check inside destructors. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1611 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1612 | BlockInfo.resize(CFGraph->getNumBlockIDs(), |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1613 | CFGBlockInfo::getEmptyBlockInfo(LocksetFactory, LocalVarMap)); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1614 | |
| 1615 | // We need to explore the CFG via a "topological" ordering. |
| 1616 | // That way, we will be guaranteed to have information about required |
| 1617 | // predecessor locksets when exploring a new block. |
Ted Kremenek | 439ed16 | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 1618 | PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>(); |
| 1619 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1620 | |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1621 | // Compute SSA names for local variables |
| 1622 | LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo); |
| 1623 | |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1624 | // Fill in source locations for all CFGBlocks. |
| 1625 | findBlockLocations(CFGraph, SortedGraph, BlockInfo); |
| 1626 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1627 | // Add locks from exclusive_locks_required and shared_locks_required |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1628 | // to initial lockset. Also turn off checking for lock and unlock functions. |
| 1629 | // FIXME: is there a more intelligent way to check lock/unlock functions? |
Ted Kremenek | 439ed16 | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 1630 | if (!SortedGraph->empty() && D->hasAttrs()) { |
| 1631 | const CFGBlock *FirstBlock = *SortedGraph->begin(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1632 | Lockset &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet; |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1633 | const AttrVec &ArgAttrs = D->getAttrs(); |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1634 | for (unsigned i = 0; i < ArgAttrs.size(); ++i) { |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1635 | Attr *Attr = ArgAttrs[i]; |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 1636 | SourceLocation AttrLoc = Attr->getLocation(); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1637 | if (SharedLocksRequiredAttr *SLRAttr |
| 1638 | = dyn_cast<SharedLocksRequiredAttr>(Attr)) { |
| 1639 | for (SharedLocksRequiredAttr::args_iterator |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1640 | SLRIter = SLRAttr->args_begin(), |
| 1641 | SLREnd = SLRAttr->args_end(); SLRIter != SLREnd; ++SLRIter) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1642 | InitialLockset = addLock(InitialLockset, *SLRIter, D, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 1643 | LockData(AttrLoc, LK_Shared), false); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1644 | } else if (ExclusiveLocksRequiredAttr *ELRAttr |
| 1645 | = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) { |
| 1646 | for (ExclusiveLocksRequiredAttr::args_iterator |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1647 | ELRIter = ELRAttr->args_begin(), |
| 1648 | ELREnd = ELRAttr->args_end(); ELRIter != ELREnd; ++ELRIter) |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1649 | InitialLockset = addLock(InitialLockset, *ELRIter, D, |
DeLesley Hutchins | c36eda1 | 2012-07-02 22:12:12 +0000 | [diff] [blame^] | 1650 | LockData(AttrLoc, LK_Exclusive), false); |
DeLesley Hutchins | 2f13bec | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1651 | } else if (isa<UnlockFunctionAttr>(Attr)) { |
| 1652 | // Don't try to check unlock functions for now |
| 1653 | return; |
| 1654 | } else if (isa<ExclusiveLockFunctionAttr>(Attr)) { |
| 1655 | // Don't try to check lock functions for now |
| 1656 | return; |
| 1657 | } else if (isa<SharedLockFunctionAttr>(Attr)) { |
| 1658 | // Don't try to check lock functions for now |
| 1659 | return; |
DeLesley Hutchins | 76f0a6e | 2012-07-02 21:59:24 +0000 | [diff] [blame] | 1660 | } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) { |
| 1661 | // Don't try to check trylock functions for now |
| 1662 | return; |
| 1663 | } else if (isa<SharedTrylockFunctionAttr>(Attr)) { |
| 1664 | // Don't try to check trylock functions for now |
| 1665 | return; |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
Ted Kremenek | 439ed16 | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 1670 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 1671 | E = SortedGraph->end(); I!= E; ++I) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1672 | const CFGBlock *CurrBlock = *I; |
| 1673 | int CurrBlockID = CurrBlock->getBlockID(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1674 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Use the default initial lockset in case there are no predecessors. |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1677 | VisitedBlocks.insert(CurrBlock); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1678 | |
| 1679 | // Iterate through the predecessor blocks and warn if the lockset for all |
| 1680 | // predecessors is not the same. We take the entry lockset of the current |
| 1681 | // block to be the intersection of all previous locksets. |
| 1682 | // FIXME: By keeping the intersection, we may output more errors in future |
| 1683 | // for a lock which is not in the intersection, but was in the union. We |
| 1684 | // may want to also keep the union in future. As an example, let's say |
| 1685 | // the intersection contains Mutex L, and the union contains L and M. |
| 1686 | // Later we unlock M. At this point, we would output an error because we |
| 1687 | // never locked M; although the real error is probably that we forgot to |
| 1688 | // lock M on all code paths. Conversely, let's say that later we lock M. |
| 1689 | // In this case, we should compare against the intersection instead of the |
| 1690 | // union because the real error is probably that we forgot to unlock M on |
| 1691 | // all code paths. |
| 1692 | bool LocksetInitialized = false; |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1693 | llvm::SmallVector<CFGBlock*, 8> SpecialBlocks; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1694 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 1695 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 1696 | |
| 1697 | // if *PI -> CurrBlock is a back edge |
| 1698 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) |
| 1699 | continue; |
| 1700 | |
DeLesley Hutchins | 2a35be8 | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 1701 | // Ignore edges from blocks that can't return. |
| 1702 | if ((*PI)->hasNoReturnElement()) |
| 1703 | continue; |
| 1704 | |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1705 | // If the previous block ended in a 'continue' or 'break' statement, then |
| 1706 | // a difference in locksets is probably due to a bug in that block, rather |
| 1707 | // than in some other predecessor. In that case, keep the other |
| 1708 | // predecessor's lockset. |
| 1709 | if (const Stmt *Terminator = (*PI)->getTerminator()) { |
| 1710 | if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) { |
| 1711 | SpecialBlocks.push_back(*PI); |
| 1712 | continue; |
| 1713 | } |
| 1714 | } |
| 1715 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1716 | int PrevBlockID = (*PI)->getBlockID(); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1717 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1718 | Lockset PrevLockset = |
| 1719 | getEdgeLockset(PrevBlockInfo->ExitSet, *PI, CurrBlock); |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1720 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1721 | if (!LocksetInitialized) { |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1722 | CurrBlockInfo->EntrySet = PrevLockset; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1723 | LocksetInitialized = true; |
| 1724 | } else { |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1725 | CurrBlockInfo->EntrySet = |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1726 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 1727 | CurrBlockInfo->EntryLoc, |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1728 | LEK_LockedSomePredecessors); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1732 | // Process continue and break blocks. Assume that the lockset for the |
| 1733 | // resulting block is unaffected by any discrepancies in them. |
| 1734 | for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size(); |
| 1735 | SpecialI < SpecialN; ++SpecialI) { |
| 1736 | CFGBlock *PrevBlock = SpecialBlocks[SpecialI]; |
| 1737 | int PrevBlockID = PrevBlock->getBlockID(); |
| 1738 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 1739 | |
| 1740 | if (!LocksetInitialized) { |
| 1741 | CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet; |
| 1742 | LocksetInitialized = true; |
| 1743 | } else { |
| 1744 | // Determine whether this edge is a loop terminator for diagnostic |
| 1745 | // purposes. FIXME: A 'break' statement might be a loop terminator, but |
| 1746 | // it might also be part of a switch. Also, a subsequent destructor |
| 1747 | // might add to the lockset, in which case the real issue might be a |
| 1748 | // double lock on the other path. |
| 1749 | const Stmt *Terminator = PrevBlock->getTerminator(); |
| 1750 | bool IsLoop = Terminator && isa<ContinueStmt>(Terminator); |
| 1751 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1752 | Lockset PrevLockset = |
| 1753 | getEdgeLockset(PrevBlockInfo->ExitSet, PrevBlock, CurrBlock); |
| 1754 | |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1755 | // Do not update EntrySet. |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1756 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 1757 | PrevBlockInfo->ExitLoc, |
Richard Smith | aacde71 | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1758 | IsLoop ? LEK_LockedSomeLoopIterations |
| 1759 | : LEK_LockedSomePredecessors); |
| 1760 | } |
| 1761 | } |
| 1762 | |
DeLesley Hutchins | 54c350a | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1763 | BuildLockset LocksetBuilder(this, *CurrBlockInfo); |
| 1764 | |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1765 | // Visit all the statements in the basic block. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1766 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 1767 | BE = CurrBlock->end(); BI != BE; ++BI) { |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 1768 | switch (BI->getKind()) { |
| 1769 | case CFGElement::Statement: { |
| 1770 | const CFGStmt *CS = cast<CFGStmt>(&*BI); |
| 1771 | LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt())); |
| 1772 | break; |
| 1773 | } |
| 1774 | // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now. |
| 1775 | case CFGElement::AutomaticObjectDtor: { |
| 1776 | const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI); |
| 1777 | CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>( |
| 1778 | AD->getDestructorDecl(AC.getASTContext())); |
| 1779 | if (!DD->hasAttrs()) |
| 1780 | break; |
| 1781 | |
| 1782 | // Create a dummy expression, |
| 1783 | VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl()); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1784 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 1785 | AD->getTriggerStmt()->getLocEnd()); |
| 1786 | LocksetBuilder.handleCall(&DRE, DD); |
| 1787 | break; |
| 1788 | } |
| 1789 | default: |
| 1790 | break; |
| 1791 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1792 | } |
DeLesley Hutchins | b37d2b5 | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1793 | CurrBlockInfo->ExitSet = LocksetBuilder.LSet; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1794 | |
| 1795 | // For every back edge from CurrBlock (the end of the loop) to another block |
| 1796 | // (FirstLoopBlock) we need to check that the Lockset of Block is equal to |
| 1797 | // the one held at the beginning of FirstLoopBlock. We can look up the |
| 1798 | // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. |
| 1799 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 1800 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 1801 | |
| 1802 | // if CurrBlock -> *SI is *not* a back edge |
| 1803 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
| 1804 | continue; |
| 1805 | |
| 1806 | CFGBlock *FirstLoopBlock = *SI; |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1807 | CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()]; |
| 1808 | CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID]; |
| 1809 | intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet, |
| 1810 | PreLoop->EntryLoc, |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1811 | LEK_LockedSomeLoopIterations); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1815 | CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()]; |
| 1816 | CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()]; |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 1817 | |
| 1818 | // FIXME: Should we call this function for all blocks which exit the function? |
DeLesley Hutchins | 0da4414 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1819 | intersectAndWarn(Initial->EntrySet, Final->ExitSet, |
| 1820 | Final->ExitLoc, |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1821 | LEK_LockedAtEndOfFunction); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | } // end anonymous namespace |
| 1825 | |
| 1826 | |
| 1827 | namespace clang { |
| 1828 | namespace thread_safety { |
| 1829 | |
| 1830 | /// \brief Check a function's CFG for thread-safety violations. |
| 1831 | /// |
| 1832 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 1833 | /// at the end of each block, and issue warnings for thread safety violations. |
| 1834 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 1835 | void runThreadSafetyAnalysis(AnalysisDeclContext &AC, |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1836 | ThreadSafetyHandler &Handler) { |
| 1837 | ThreadSafetyAnalyzer Analyzer(Handler); |
| 1838 | Analyzer.runAnalysis(AC); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /// \brief Helper function that returns a LockKind required for the given level |
| 1842 | /// of access. |
| 1843 | LockKind getLockKindFromAccessKind(AccessKind AK) { |
| 1844 | switch (AK) { |
| 1845 | case AK_Read : |
| 1846 | return LK_Shared; |
| 1847 | case AK_Written : |
| 1848 | return LK_Exclusive; |
| 1849 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 1850 | llvm_unreachable("Unknown AccessKind"); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1851 | } |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1852 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1853 | }} // end namespace clang::thread_safety |