Caitlin Sadowski | 3320834 | 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 | // |
Aaron Ballman | fcd5b7e | 2013-06-26 19:17:19 +0000 | [diff] [blame] | 13 | // See http://clang.llvm.org/docs/LanguageExtensions.html#thread-safety-annotation-checking |
| 14 | // for more information. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/ExprCXX.h" |
| 22 | #include "clang/AST/StmtCXX.h" |
| 23 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
| 25 | #include "clang/Analysis/AnalysisContext.h" |
| 26 | #include "clang/Analysis/CFG.h" |
| 27 | #include "clang/Analysis/CFGStmtMap.h" |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 28 | #include "clang/Basic/OperatorKinds.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceLocation.h" |
| 30 | #include "clang/Basic/SourceManager.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/BitVector.h" |
| 32 | #include "llvm/ADT/FoldingSet.h" |
| 33 | #include "llvm/ADT/ImmutableMap.h" |
| 34 | #include "llvm/ADT/PostOrderIterator.h" |
| 35 | #include "llvm/ADT/SmallVector.h" |
| 36 | #include "llvm/ADT/StringRef.h" |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 38 | #include <algorithm> |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 39 | #include <utility> |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
| 42 | using namespace clang; |
| 43 | using namespace thread_safety; |
| 44 | |
Caitlin Sadowski | 5b34a2f | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 45 | // Key method definition |
| 46 | ThreadSafetyHandler::~ThreadSafetyHandler() {} |
| 47 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 48 | namespace { |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 49 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 50 | /// SExpr implements a simple expression language that is used to store, |
| 51 | /// compare, and pretty-print C++ expressions. Unlike a clang Expr, a SExpr |
| 52 | /// does not capture surface syntax, and it does not distinguish between |
| 53 | /// C++ concepts, like pointers and references, that have no real semantic |
| 54 | /// differences. This simplicity allows SExprs to be meaningfully compared, |
| 55 | /// e.g. |
| 56 | /// (x) = x |
| 57 | /// (*this).foo = this->foo |
| 58 | /// *&a = a |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 59 | /// |
| 60 | /// Thread-safety analysis works by comparing lock expressions. Within the |
| 61 | /// body of a function, an expression such as "x->foo->bar.mu" will resolve to |
| 62 | /// a particular mutex object at run-time. Subsequent occurrences of the same |
| 63 | /// expression (where "same" means syntactic equality) will refer to the same |
| 64 | /// run-time object if three conditions hold: |
| 65 | /// (1) Local variables in the expression, such as "x" have not changed. |
| 66 | /// (2) Values on the heap that affect the expression have not changed. |
| 67 | /// (3) The expression involves only pure function calls. |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 68 | /// |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 69 | /// The current implementation assumes, but does not verify, that multiple uses |
| 70 | /// of the same lock expression satisfies these criteria. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 71 | class SExpr { |
| 72 | private: |
| 73 | enum ExprOp { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 74 | EOP_Nop, ///< No-op |
| 75 | EOP_Wildcard, ///< Matches anything. |
| 76 | EOP_Universal, ///< Universal lock. |
| 77 | EOP_This, ///< This keyword. |
| 78 | EOP_NVar, ///< Named variable. |
| 79 | EOP_LVar, ///< Local variable. |
| 80 | EOP_Dot, ///< Field access |
| 81 | EOP_Call, ///< Function call |
| 82 | EOP_MCall, ///< Method call |
| 83 | EOP_Index, ///< Array index |
| 84 | EOP_Unary, ///< Unary operation |
| 85 | EOP_Binary, ///< Binary operation |
| 86 | EOP_Unknown ///< Catchall for everything else |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | |
| 90 | class SExprNode { |
| 91 | private: |
Ted Kremenek | 78094ca | 2012-08-22 23:50:41 +0000 | [diff] [blame] | 92 | unsigned char Op; ///< Opcode of the root node |
| 93 | unsigned char Flags; ///< Additional opcode-specific data |
| 94 | unsigned short Sz; ///< Number of child nodes |
| 95 | const void* Data; ///< Additional opcode-specific data |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 96 | |
| 97 | public: |
| 98 | SExprNode(ExprOp O, unsigned F, const void* D) |
| 99 | : Op(static_cast<unsigned char>(O)), |
| 100 | Flags(static_cast<unsigned char>(F)), Sz(1), Data(D) |
| 101 | { } |
| 102 | |
| 103 | unsigned size() const { return Sz; } |
| 104 | void setSize(unsigned S) { Sz = S; } |
| 105 | |
| 106 | ExprOp kind() const { return static_cast<ExprOp>(Op); } |
| 107 | |
| 108 | const NamedDecl* getNamedDecl() const { |
| 109 | assert(Op == EOP_NVar || Op == EOP_LVar || Op == EOP_Dot); |
| 110 | return reinterpret_cast<const NamedDecl*>(Data); |
| 111 | } |
| 112 | |
| 113 | const NamedDecl* getFunctionDecl() const { |
| 114 | assert(Op == EOP_Call || Op == EOP_MCall); |
| 115 | return reinterpret_cast<const NamedDecl*>(Data); |
| 116 | } |
| 117 | |
| 118 | bool isArrow() const { return Op == EOP_Dot && Flags == 1; } |
| 119 | void setArrow(bool A) { Flags = A ? 1 : 0; } |
| 120 | |
| 121 | unsigned arity() const { |
| 122 | switch (Op) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 123 | case EOP_Nop: return 0; |
| 124 | case EOP_Wildcard: return 0; |
| 125 | case EOP_Universal: return 0; |
| 126 | case EOP_NVar: return 0; |
| 127 | case EOP_LVar: return 0; |
| 128 | case EOP_This: return 0; |
| 129 | case EOP_Dot: return 1; |
| 130 | case EOP_Call: return Flags+1; // First arg is function. |
| 131 | case EOP_MCall: return Flags+1; // First arg is implicit obj. |
| 132 | case EOP_Index: return 2; |
| 133 | case EOP_Unary: return 1; |
| 134 | case EOP_Binary: return 2; |
| 135 | case EOP_Unknown: return Flags; |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | bool operator==(const SExprNode& Other) const { |
| 141 | // Ignore flags and size -- they don't matter. |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 142 | return (Op == Other.Op && |
| 143 | Data == Other.Data); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool operator!=(const SExprNode& Other) const { |
| 147 | return !(*this == Other); |
| 148 | } |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 149 | |
| 150 | bool matches(const SExprNode& Other) const { |
| 151 | return (*this == Other) || |
| 152 | (Op == EOP_Wildcard) || |
| 153 | (Other.Op == EOP_Wildcard); |
| 154 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 157 | |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 158 | /// \brief Encapsulates the lexical context of a function call. The lexical |
| 159 | /// context includes the arguments to the call, including the implicit object |
| 160 | /// argument. When an attribute containing a mutex expression is attached to |
| 161 | /// a method, the expression may refer to formal parameters of the method. |
| 162 | /// Actual arguments must be substituted for formal parameters to derive |
| 163 | /// the appropriate mutex expression in the lexical context where the function |
| 164 | /// is called. PrevCtx holds the context in which the arguments themselves |
| 165 | /// should be evaluated; multiple calling contexts can be chained together |
| 166 | /// by the lock_returned attribute. |
| 167 | struct CallingContext { |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 168 | const NamedDecl* AttrDecl; // The decl to which the attribute is attached. |
| 169 | const Expr* SelfArg; // Implicit object argument -- e.g. 'this' |
| 170 | bool SelfArrow; // is Self referred to with -> or .? |
| 171 | unsigned NumArgs; // Number of funArgs |
| 172 | const Expr* const* FunArgs; // Function arguments |
| 173 | CallingContext* PrevCtx; // The previous context; or 0 if none. |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 174 | |
Aaron Ballman | 69bb592 | 2014-03-06 19:37:24 +0000 | [diff] [blame] | 175 | CallingContext(const NamedDecl *D) |
| 176 | : AttrDecl(D), SelfArg(0), SelfArrow(false), NumArgs(0), FunArgs(0), |
| 177 | PrevCtx(0) {} |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 180 | typedef SmallVector<SExprNode, 4> NodeVector; |
| 181 | |
| 182 | private: |
| 183 | // A SExpr is a list of SExprNodes in prefix order. The Size field allows |
| 184 | // the list to be traversed as a tree. |
| 185 | NodeVector NodeVec; |
| 186 | |
| 187 | private: |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 188 | unsigned make(ExprOp O, unsigned F = 0, const void *D = 0) { |
| 189 | NodeVec.push_back(SExprNode(O, F, D)); |
| 190 | return NodeVec.size() - 1; |
| 191 | } |
| 192 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 193 | unsigned makeNop() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 194 | return make(EOP_Nop); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 195 | } |
| 196 | |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 197 | unsigned makeWildcard() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 198 | return make(EOP_Wildcard); |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 199 | } |
| 200 | |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 201 | unsigned makeUniversal() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 202 | return make(EOP_Universal); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 205 | unsigned makeNamedVar(const NamedDecl *D) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 206 | return make(EOP_NVar, 0, D); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | unsigned makeLocalVar(const NamedDecl *D) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 210 | return make(EOP_LVar, 0, D); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | unsigned makeThis() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 214 | return make(EOP_This); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | unsigned makeDot(const NamedDecl *D, bool Arrow) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 218 | return make(EOP_Dot, Arrow ? 1 : 0, D); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | unsigned makeCall(unsigned NumArgs, const NamedDecl *D) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 222 | return make(EOP_Call, NumArgs, D); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
DeLesley Hutchins | b78aeed | 2012-09-20 22:18:02 +0000 | [diff] [blame] | 225 | // Grab the very first declaration of virtual method D |
| 226 | const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) { |
| 227 | while (true) { |
| 228 | D = D->getCanonicalDecl(); |
| 229 | CXXMethodDecl::method_iterator I = D->begin_overridden_methods(), |
| 230 | E = D->end_overridden_methods(); |
| 231 | if (I == E) |
| 232 | return D; // Method does not override anything |
| 233 | D = *I; // FIXME: this does not work with multiple inheritance. |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | unsigned makeMCall(unsigned NumArgs, const CXXMethodDecl *D) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 239 | return make(EOP_MCall, NumArgs, getFirstVirtualDecl(D)); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | unsigned makeIndex() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 243 | return make(EOP_Index); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | unsigned makeUnary() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 247 | return make(EOP_Unary); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | unsigned makeBinary() { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 251 | return make(EOP_Binary); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | unsigned makeUnknown(unsigned Arity) { |
Aaron Ballman | 19842c4 | 2014-03-06 19:25:11 +0000 | [diff] [blame] | 255 | return make(EOP_Unknown, Arity); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 256 | } |
| 257 | |
DeLesley Hutchins | 39b804f | 2013-11-26 19:45:21 +0000 | [diff] [blame] | 258 | inline bool isCalleeArrow(const Expr *E) { |
| 259 | const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts()); |
| 260 | return ME ? ME->isArrow() : false; |
| 261 | } |
| 262 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 263 | /// Build an SExpr from the given C++ expression. |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 264 | /// Recursive function that terminates on DeclRefExpr. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 265 | /// Note: this function merely creates a SExpr; it does not check to |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 266 | /// ensure that the original expression is a valid mutex expression. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 267 | /// |
| 268 | /// NDeref returns the number of Derefence and AddressOf operations |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 269 | /// preceding the Expr; this is used to decide whether to pretty-print |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 270 | /// SExprs with . or ->. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 271 | unsigned buildSExpr(const Expr *Exp, CallingContext* CallCtx, |
| 272 | int* NDeref = 0) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 273 | if (!Exp) |
| 274 | return 0; |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 275 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 276 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) { |
| 277 | const NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 278 | const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND); |
DeLesley Hutchins | 68f7b1a | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 279 | if (PV) { |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 280 | const FunctionDecl *FD = |
DeLesley Hutchins | 68f7b1a | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 281 | cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl(); |
| 282 | unsigned i = PV->getFunctionScopeIndex(); |
| 283 | |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 284 | if (CallCtx && CallCtx->FunArgs && |
| 285 | FD == CallCtx->AttrDecl->getCanonicalDecl()) { |
DeLesley Hutchins | 68f7b1a | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 286 | // Substitute call arguments for references to function parameters |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 287 | assert(i < CallCtx->NumArgs); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 288 | return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref); |
DeLesley Hutchins | 68f7b1a | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 289 | } |
| 290 | // Map the param back to the param of the original function declaration. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 291 | makeNamedVar(FD->getParamDecl(i)); |
| 292 | return 1; |
DeLesley Hutchins | 68f7b1a | 2012-01-20 23:24:41 +0000 | [diff] [blame] | 293 | } |
| 294 | // Not a function parameter -- just store the reference. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 295 | makeNamedVar(ND); |
| 296 | return 1; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 297 | } else if (isa<CXXThisExpr>(Exp)) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 298 | // Substitute parent for 'this' |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 299 | if (CallCtx && CallCtx->SelfArg) { |
| 300 | if (!CallCtx->SelfArrow && NDeref) |
| 301 | // 'this' is a pointer, but self is not, so need to take address. |
| 302 | --(*NDeref); |
| 303 | return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref); |
| 304 | } |
DeLesley Hutchins | bc8ffdb | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 305 | else { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 306 | makeThis(); |
| 307 | return 1; |
DeLesley Hutchins | bc8ffdb | 2012-02-16 17:03:24 +0000 | [diff] [blame] | 308 | } |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 309 | } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 310 | const NamedDecl *ND = ME->getMemberDecl(); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 311 | int ImplicitDeref = ME->isArrow() ? 1 : 0; |
| 312 | unsigned Root = makeDot(ND, false); |
| 313 | unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref); |
| 314 | NodeVec[Root].setArrow(ImplicitDeref > 0); |
| 315 | NodeVec[Root].setSize(Sz + 1); |
| 316 | return Sz + 1; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 317 | } else if (const CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 318 | // When calling a function with a lock_returned attribute, replace |
| 319 | // the function call with the expression in lock_returned. |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 320 | const CXXMethodDecl *MD = CMCE->getMethodDecl()->getMostRecentDecl(); |
DeLesley Hutchins | f5cf790 | 2012-08-31 22:09:53 +0000 | [diff] [blame] | 321 | if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 322 | CallingContext LRCallCtx(CMCE->getMethodDecl()); |
| 323 | LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument(); |
DeLesley Hutchins | 39b804f | 2013-11-26 19:45:21 +0000 | [diff] [blame] | 324 | LRCallCtx.SelfArrow = isCalleeArrow(CMCE->getCallee()); |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 325 | LRCallCtx.NumArgs = CMCE->getNumArgs(); |
| 326 | LRCallCtx.FunArgs = CMCE->getArgs(); |
| 327 | LRCallCtx.PrevCtx = CallCtx; |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 328 | return buildSExpr(At->getArg(), &LRCallCtx); |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 329 | } |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 330 | // Hack to treat smart pointers and iterators as pointers; |
| 331 | // ignore any method named get(). |
| 332 | if (CMCE->getMethodDecl()->getNameAsString() == "get" && |
| 333 | CMCE->getNumArgs() == 0) { |
DeLesley Hutchins | 39b804f | 2013-11-26 19:45:21 +0000 | [diff] [blame] | 334 | if (NDeref && isCalleeArrow(CMCE->getCallee())) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 335 | ++(*NDeref); |
| 336 | return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref); |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 337 | } |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 338 | unsigned NumCallArgs = CMCE->getNumArgs(); |
DeLesley Hutchins | b78aeed | 2012-09-20 22:18:02 +0000 | [diff] [blame] | 339 | unsigned Root = makeMCall(NumCallArgs, CMCE->getMethodDecl()); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 340 | unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 341 | const Expr* const* CallArgs = CMCE->getArgs(); |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 342 | for (unsigned i = 0; i < NumCallArgs; ++i) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 343 | Sz += buildSExpr(CallArgs[i], CallCtx); |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 344 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 345 | NodeVec[Root].setSize(Sz + 1); |
| 346 | return Sz + 1; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 347 | } else if (const CallExpr *CE = dyn_cast<CallExpr>(Exp)) { |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 348 | const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl(); |
DeLesley Hutchins | f5cf790 | 2012-08-31 22:09:53 +0000 | [diff] [blame] | 349 | if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 350 | CallingContext LRCallCtx(CE->getDirectCallee()); |
| 351 | LRCallCtx.NumArgs = CE->getNumArgs(); |
| 352 | LRCallCtx.FunArgs = CE->getArgs(); |
| 353 | LRCallCtx.PrevCtx = CallCtx; |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 354 | return buildSExpr(At->getArg(), &LRCallCtx); |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 355 | } |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 356 | // Treat smart pointers and iterators as pointers; |
| 357 | // ignore the * and -> operators. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 358 | if (const CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) { |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 359 | OverloadedOperatorKind k = OE->getOperator(); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 360 | if (k == OO_Star) { |
| 361 | if (NDeref) ++(*NDeref); |
| 362 | return buildSExpr(OE->getArg(0), CallCtx, NDeref); |
| 363 | } |
| 364 | else if (k == OO_Arrow) { |
| 365 | return buildSExpr(OE->getArg(0), CallCtx, NDeref); |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 368 | unsigned NumCallArgs = CE->getNumArgs(); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 369 | unsigned Root = makeCall(NumCallArgs, 0); |
| 370 | unsigned Sz = buildSExpr(CE->getCallee(), CallCtx); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 371 | const Expr* const* CallArgs = CE->getArgs(); |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 372 | for (unsigned i = 0; i < NumCallArgs; ++i) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 373 | Sz += buildSExpr(CallArgs[i], CallCtx); |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 374 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 375 | NodeVec[Root].setSize(Sz+1); |
| 376 | return Sz+1; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 377 | } else if (const BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 378 | unsigned Root = makeBinary(); |
| 379 | unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx); |
| 380 | Sz += buildSExpr(BOE->getRHS(), CallCtx); |
| 381 | NodeVec[Root].setSize(Sz); |
| 382 | return Sz; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 383 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 384 | // Ignore & and * operators -- they're no-ops. |
| 385 | // However, we try to figure out whether the expression is a pointer, |
| 386 | // so we can use . and -> appropriately in error messages. |
| 387 | if (UOE->getOpcode() == UO_Deref) { |
| 388 | if (NDeref) ++(*NDeref); |
| 389 | return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref); |
| 390 | } |
| 391 | if (UOE->getOpcode() == UO_AddrOf) { |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 392 | if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) { |
| 393 | if (DRE->getDecl()->isCXXInstanceMember()) { |
| 394 | // This is a pointer-to-member expression, e.g. &MyClass::mu_. |
| 395 | // We interpret this syntax specially, as a wildcard. |
| 396 | unsigned Root = makeDot(DRE->getDecl(), false); |
| 397 | makeWildcard(); |
| 398 | NodeVec[Root].setSize(2); |
| 399 | return 2; |
| 400 | } |
| 401 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 402 | if (NDeref) --(*NDeref); |
| 403 | return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref); |
| 404 | } |
| 405 | unsigned Root = makeUnary(); |
| 406 | unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx); |
| 407 | NodeVec[Root].setSize(Sz); |
| 408 | return Sz; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 409 | } else if (const ArraySubscriptExpr *ASE = |
| 410 | dyn_cast<ArraySubscriptExpr>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 411 | unsigned Root = makeIndex(); |
| 412 | unsigned Sz = buildSExpr(ASE->getBase(), CallCtx); |
| 413 | Sz += buildSExpr(ASE->getIdx(), CallCtx); |
| 414 | NodeVec[Root].setSize(Sz); |
| 415 | return Sz; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 416 | } else if (const AbstractConditionalOperator *CE = |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 417 | dyn_cast<AbstractConditionalOperator>(Exp)) { |
| 418 | unsigned Root = makeUnknown(3); |
| 419 | unsigned Sz = buildSExpr(CE->getCond(), CallCtx); |
| 420 | Sz += buildSExpr(CE->getTrueExpr(), CallCtx); |
| 421 | Sz += buildSExpr(CE->getFalseExpr(), CallCtx); |
| 422 | NodeVec[Root].setSize(Sz); |
| 423 | return Sz; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 424 | } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 425 | unsigned Root = makeUnknown(3); |
| 426 | unsigned Sz = buildSExpr(CE->getCond(), CallCtx); |
| 427 | Sz += buildSExpr(CE->getLHS(), CallCtx); |
| 428 | Sz += buildSExpr(CE->getRHS(), CallCtx); |
| 429 | NodeVec[Root].setSize(Sz); |
| 430 | return Sz; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 431 | } else if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 432 | return buildSExpr(CE->getSubExpr(), CallCtx, NDeref); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 433 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 434 | return buildSExpr(PE->getSubExpr(), CallCtx, NDeref); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 435 | } else if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 436 | return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 437 | } else if (const CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 438 | return buildSExpr(E->getSubExpr(), CallCtx, NDeref); |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 439 | } else if (isa<CharacterLiteral>(Exp) || |
DeLesley Hutchins | 0c1da20 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 440 | isa<CXXNullPtrLiteralExpr>(Exp) || |
| 441 | isa<GNUNullExpr>(Exp) || |
| 442 | isa<CXXBoolLiteralExpr>(Exp) || |
| 443 | isa<FloatingLiteral>(Exp) || |
| 444 | isa<ImaginaryLiteral>(Exp) || |
| 445 | isa<IntegerLiteral>(Exp) || |
| 446 | isa<StringLiteral>(Exp) || |
| 447 | isa<ObjCStringLiteral>(Exp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 448 | makeNop(); |
| 449 | return 1; // FIXME: Ignore literals for now |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 450 | } else { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 451 | makeNop(); |
| 452 | return 1; // Ignore. FIXME: mark as invalid expression? |
DeLesley Hutchins | e2a3f75 | 2012-03-02 23:36:05 +0000 | [diff] [blame] | 453 | } |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 454 | } |
| 455 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 456 | /// \brief Construct a SExpr from an expression. |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 457 | /// \param MutexExp The original mutex expression within an attribute |
| 458 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 459 | /// occurs. |
| 460 | /// \param D The declaration to which the lock/unlock attribute is attached. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 461 | void buildSExprFromExpr(const Expr *MutexExp, const Expr *DeclExp, |
| 462 | const NamedDecl *D, VarDecl *SelfDecl = 0) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 463 | CallingContext CallCtx(D); |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 464 | |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 465 | if (MutexExp) { |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 466 | if (const StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 467 | if (SLit->getString() == StringRef("*")) |
| 468 | // The "*" expr is a universal lock, which essentially turns off |
| 469 | // checks until it is removed from the lockset. |
| 470 | makeUniversal(); |
| 471 | else |
| 472 | // Ignore other string literals for now. |
| 473 | makeNop(); |
| 474 | return; |
| 475 | } |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 476 | } |
| 477 | |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 478 | // If we are processing a raw attribute expression, with no substitutions. |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 479 | if (DeclExp == 0) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 480 | buildSExpr(MutexExp, 0); |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 484 | // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 485 | // for formal parameters when we call buildMutexID later. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 486 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 487 | CallCtx.SelfArg = ME->getBase(); |
| 488 | CallCtx.SelfArrow = ME->isArrow(); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 489 | } else if (const CXXMemberCallExpr *CE = |
| 490 | dyn_cast<CXXMemberCallExpr>(DeclExp)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 491 | CallCtx.SelfArg = CE->getImplicitObjectArgument(); |
DeLesley Hutchins | 39b804f | 2013-11-26 19:45:21 +0000 | [diff] [blame] | 492 | CallCtx.SelfArrow = isCalleeArrow(CE->getCallee()); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 493 | CallCtx.NumArgs = CE->getNumArgs(); |
| 494 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | 39b804f | 2013-11-26 19:45:21 +0000 | [diff] [blame] | 495 | } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) { |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 496 | CallCtx.NumArgs = CE->getNumArgs(); |
| 497 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 498 | } else if (const CXXConstructExpr *CE = |
| 499 | dyn_cast<CXXConstructExpr>(DeclExp)) { |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 500 | CallCtx.SelfArg = 0; // Will be set below |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 501 | CallCtx.NumArgs = CE->getNumArgs(); |
| 502 | CallCtx.FunArgs = CE->getArgs(); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 503 | } else if (D && isa<CXXDestructorDecl>(D)) { |
| 504 | // There's no such thing as a "destructor call" in the AST. |
DeLesley Hutchins | 49979f2 | 2012-06-25 18:33:18 +0000 | [diff] [blame] | 505 | CallCtx.SelfArg = DeclExp; |
DeLesley Hutchins | 30abeb1 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 506 | } |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 507 | |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 508 | // Hack to handle constructors, where self cannot be recovered from |
| 509 | // the expression. |
| 510 | if (SelfDecl && !CallCtx.SelfArg) { |
| 511 | DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue, |
| 512 | SelfDecl->getLocation()); |
| 513 | CallCtx.SelfArg = &SelfDRE; |
| 514 | |
| 515 | // If the attribute has no arguments, then assume the argument is "this". |
| 516 | if (MutexExp == 0) |
| 517 | buildSExpr(CallCtx.SelfArg, 0); |
| 518 | else // For most attributes. |
| 519 | buildSExpr(MutexExp, &CallCtx); |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 520 | return; |
| 521 | } |
DeLesley Hutchins | 30abeb1 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 522 | |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 523 | // If the attribute has no arguments, then assume the argument is "this". |
| 524 | if (MutexExp == 0) |
| 525 | buildSExpr(CallCtx.SelfArg, 0); |
| 526 | else // For most attributes. |
| 527 | buildSExpr(MutexExp, &CallCtx); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 528 | } |
| 529 | |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 530 | /// \brief Get index of next sibling of node i. |
| 531 | unsigned getNextSibling(unsigned i) const { |
| 532 | return i + NodeVec[i].size(); |
| 533 | } |
| 534 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 535 | public: |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 536 | explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); } |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 537 | |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 538 | /// \param MutexExp The original mutex expression within an attribute |
| 539 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 540 | /// occurs. |
| 541 | /// \param D The declaration to which the lock/unlock attribute is attached. |
| 542 | /// Caller must check isValid() after construction. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 543 | SExpr(const Expr* MutexExp, const Expr *DeclExp, const NamedDecl* D, |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 544 | VarDecl *SelfDecl=0) { |
| 545 | buildSExprFromExpr(MutexExp, DeclExp, D, SelfDecl); |
Caitlin Sadowski | 787c2a1 | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 546 | } |
| 547 | |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 548 | /// Return true if this is a valid decl sequence. |
| 549 | /// Caller must call this by hand after construction to handle errors. |
Caitlin Sadowski | 787c2a1 | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 550 | bool isValid() const { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 551 | return !NodeVec.empty(); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 552 | } |
| 553 | |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 554 | bool shouldIgnore() const { |
| 555 | // Nop is a mutex that we have decided to deliberately ignore. |
| 556 | assert(NodeVec.size() > 0 && "Invalid Mutex"); |
| 557 | return NodeVec[0].kind() == EOP_Nop; |
| 558 | } |
| 559 | |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 560 | bool isUniversal() const { |
| 561 | assert(NodeVec.size() > 0 && "Invalid Mutex"); |
| 562 | return NodeVec[0].kind() == EOP_Universal; |
| 563 | } |
| 564 | |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 565 | /// Issue a warning about an invalid lock expression |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 566 | static void warnInvalidLock(ThreadSafetyHandler &Handler, |
| 567 | const Expr *MutexExp, |
| 568 | const Expr *DeclExp, const NamedDecl* D) { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 569 | SourceLocation Loc; |
| 570 | if (DeclExp) |
| 571 | Loc = DeclExp->getExprLoc(); |
| 572 | |
| 573 | // FIXME: add a note about the attribute location in MutexExp or D |
| 574 | if (Loc.isValid()) |
| 575 | Handler.handleInvalidLockExp(Loc); |
| 576 | } |
| 577 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 578 | bool operator==(const SExpr &other) const { |
| 579 | return NodeVec == other.NodeVec; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 580 | } |
| 581 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 582 | bool operator!=(const SExpr &other) const { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 583 | return !(*this == other); |
| 584 | } |
| 585 | |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 586 | bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const { |
| 587 | if (NodeVec[i].matches(Other.NodeVec[j])) { |
DeLesley Hutchins | 138568b | 2012-09-11 23:04:49 +0000 | [diff] [blame] | 588 | unsigned ni = NodeVec[i].arity(); |
| 589 | unsigned nj = Other.NodeVec[j].arity(); |
| 590 | unsigned n = (ni < nj) ? ni : nj; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 591 | bool Result = true; |
| 592 | unsigned ci = i+1; // first child of i |
| 593 | unsigned cj = j+1; // first child of j |
| 594 | for (unsigned k = 0; k < n; |
| 595 | ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) { |
| 596 | Result = Result && matches(Other, ci, cj); |
| 597 | } |
| 598 | return Result; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 603 | // A partial match between a.mu and b.mu returns true a and b have the same |
| 604 | // type (and thus mu refers to the same mutex declaration), regardless of |
| 605 | // whether a and b are different objects or not. |
| 606 | bool partiallyMatches(const SExpr &Other) const { |
| 607 | if (NodeVec[0].kind() == EOP_Dot) |
| 608 | return NodeVec[0].matches(Other.NodeVec[0]); |
| 609 | return false; |
| 610 | } |
| 611 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 612 | /// \brief Pretty print a lock expression for use in error messages. |
| 613 | std::string toString(unsigned i = 0) const { |
Caitlin Sadowski | 787c2a1 | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 614 | assert(isValid()); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 615 | if (i >= NodeVec.size()) |
| 616 | return ""; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 617 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 618 | const SExprNode* N = &NodeVec[i]; |
| 619 | switch (N->kind()) { |
| 620 | case EOP_Nop: |
| 621 | return "_"; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 622 | case EOP_Wildcard: |
| 623 | return "(?)"; |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 624 | case EOP_Universal: |
| 625 | return "*"; |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 626 | case EOP_This: |
| 627 | return "this"; |
| 628 | case EOP_NVar: |
| 629 | case EOP_LVar: { |
| 630 | return N->getNamedDecl()->getNameAsString(); |
| 631 | } |
| 632 | case EOP_Dot: { |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 633 | if (NodeVec[i+1].kind() == EOP_Wildcard) { |
| 634 | std::string S = "&"; |
| 635 | S += N->getNamedDecl()->getQualifiedNameAsString(); |
| 636 | return S; |
| 637 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 638 | std::string FieldName = N->getNamedDecl()->getNameAsString(); |
| 639 | if (NodeVec[i+1].kind() == EOP_This) |
| 640 | return FieldName; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 641 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 642 | std::string S = toString(i+1); |
| 643 | if (N->isArrow()) |
| 644 | return S + "->" + FieldName; |
| 645 | else |
| 646 | return S + "." + FieldName; |
| 647 | } |
| 648 | case EOP_Call: { |
| 649 | std::string S = toString(i+1) + "("; |
| 650 | unsigned NumArgs = N->arity()-1; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 651 | unsigned ci = getNextSibling(i+1); |
| 652 | for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 653 | S += toString(ci); |
| 654 | if (k+1 < NumArgs) S += ","; |
| 655 | } |
| 656 | S += ")"; |
| 657 | return S; |
| 658 | } |
| 659 | case EOP_MCall: { |
| 660 | std::string S = ""; |
| 661 | if (NodeVec[i+1].kind() != EOP_This) |
| 662 | S = toString(i+1) + "."; |
| 663 | if (const NamedDecl *D = N->getFunctionDecl()) |
| 664 | S += D->getNameAsString() + "("; |
| 665 | else |
| 666 | S += "#("; |
| 667 | unsigned NumArgs = N->arity()-1; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 668 | unsigned ci = getNextSibling(i+1); |
| 669 | for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 670 | S += toString(ci); |
| 671 | if (k+1 < NumArgs) S += ","; |
| 672 | } |
| 673 | S += ")"; |
| 674 | return S; |
| 675 | } |
| 676 | case EOP_Index: { |
| 677 | std::string S1 = toString(i+1); |
| 678 | std::string S2 = toString(i+1 + NodeVec[i+1].size()); |
| 679 | return S1 + "[" + S2 + "]"; |
| 680 | } |
| 681 | case EOP_Unary: { |
| 682 | std::string S = toString(i+1); |
| 683 | return "#" + S; |
| 684 | } |
| 685 | case EOP_Binary: { |
| 686 | std::string S1 = toString(i+1); |
| 687 | std::string S2 = toString(i+1 + NodeVec[i+1].size()); |
| 688 | return "(" + S1 + "#" + S2 + ")"; |
| 689 | } |
| 690 | case EOP_Unknown: { |
| 691 | unsigned NumChildren = N->arity(); |
| 692 | if (NumChildren == 0) |
| 693 | return "(...)"; |
| 694 | std::string S = "("; |
| 695 | unsigned ci = i+1; |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 696 | for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 697 | S += toString(ci); |
| 698 | if (j+1 < NumChildren) S += "#"; |
| 699 | } |
| 700 | S += ")"; |
| 701 | return S; |
| 702 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 703 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 704 | return ""; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 705 | } |
| 706 | }; |
| 707 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 708 | /// \brief A short list of SExprs |
| 709 | class MutexIDList : public SmallVector<SExpr, 3> { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 710 | public: |
Aaron Ballman | cea2609 | 2014-03-06 19:10:16 +0000 | [diff] [blame] | 711 | /// \brief Push M onto list, but discard duplicates. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 712 | void push_back_nodup(const SExpr& M) { |
Aaron Ballman | cea2609 | 2014-03-06 19:10:16 +0000 | [diff] [blame] | 713 | if (end() == std::find(begin(), end(), M)) |
| 714 | push_back(M); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 715 | } |
| 716 | }; |
| 717 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 718 | /// \brief This is a helper class that stores info about the most recent |
| 719 | /// accquire of a Lock. |
| 720 | /// |
| 721 | /// The main body of the analysis maps MutexIDs to LockDatas. |
| 722 | struct LockData { |
| 723 | SourceLocation AcquireLoc; |
| 724 | |
| 725 | /// \brief LKind stores whether a lock is held shared or exclusively. |
| 726 | /// Note that this analysis does not currently support either re-entrant |
| 727 | /// locking or lock "upgrading" and "downgrading" between exclusive and |
| 728 | /// shared. |
| 729 | /// |
| 730 | /// FIXME: add support for re-entrant locking and lock up/downgrading |
| 731 | LockKind LKind; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 732 | bool Asserted; // for asserted locks |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 733 | bool Managed; // for ScopedLockable objects |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 734 | SExpr UnderlyingMutex; // for ScopedLockable objects |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 735 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 736 | LockData(SourceLocation AcquireLoc, LockKind LKind, bool M=false, |
| 737 | bool Asrt=false) |
| 738 | : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(Asrt), Managed(M), |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 739 | UnderlyingMutex(Decl::EmptyShell()) |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 740 | {} |
| 741 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 742 | LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 743 | : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(false), Managed(false), |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 744 | UnderlyingMutex(Mu) |
| 745 | {} |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 746 | |
| 747 | bool operator==(const LockData &other) const { |
| 748 | return AcquireLoc == other.AcquireLoc && LKind == other.LKind; |
| 749 | } |
| 750 | |
| 751 | bool operator!=(const LockData &other) const { |
| 752 | return !(*this == other); |
| 753 | } |
| 754 | |
| 755 | void Profile(llvm::FoldingSetNodeID &ID) const { |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 756 | ID.AddInteger(AcquireLoc.getRawEncoding()); |
| 757 | ID.AddInteger(LKind); |
| 758 | } |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 759 | |
| 760 | bool isAtLeast(LockKind LK) { |
| 761 | return (LK == LK_Shared) || (LKind == LK_Exclusive); |
| 762 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 763 | }; |
| 764 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 765 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 766 | /// \brief A FactEntry stores a single fact that is known at a particular point |
| 767 | /// in the program execution. Currently, this is information regarding a lock |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 768 | /// that is held at that point. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 769 | struct FactEntry { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 770 | SExpr MutID; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 771 | LockData LDat; |
| 772 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 773 | FactEntry(const SExpr& M, const LockData& L) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 774 | : MutID(M), LDat(L) |
| 775 | { } |
| 776 | }; |
| 777 | |
| 778 | |
| 779 | typedef unsigned short FactID; |
| 780 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 781 | /// \brief FactManager manages the memory for all facts that are created during |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 782 | /// the analysis of a single routine. |
| 783 | class FactManager { |
| 784 | private: |
| 785 | std::vector<FactEntry> Facts; |
| 786 | |
| 787 | public: |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 788 | FactID newLock(const SExpr& M, const LockData& L) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 789 | Facts.push_back(FactEntry(M,L)); |
| 790 | return static_cast<unsigned short>(Facts.size() - 1); |
| 791 | } |
| 792 | |
| 793 | const FactEntry& operator[](FactID F) const { return Facts[F]; } |
| 794 | FactEntry& operator[](FactID F) { return Facts[F]; } |
| 795 | }; |
| 796 | |
| 797 | |
| 798 | /// \brief A FactSet is the set of facts that are known to be true at a |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 799 | /// particular program point. FactSets must be small, because they are |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 800 | /// frequently copied, and are thus implemented as a set of indices into a |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 801 | /// table maintained by a FactManager. A typical FactSet only holds 1 or 2 |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 802 | /// locks, so we can get away with doing a linear search for lookup. Note |
| 803 | /// that a hashtable or map is inappropriate in this case, because lookups |
| 804 | /// may involve partial pattern matches, rather than exact matches. |
| 805 | class FactSet { |
| 806 | private: |
| 807 | typedef SmallVector<FactID, 4> FactVec; |
| 808 | |
| 809 | FactVec FactIDs; |
| 810 | |
| 811 | public: |
| 812 | typedef FactVec::iterator iterator; |
| 813 | typedef FactVec::const_iterator const_iterator; |
| 814 | |
| 815 | iterator begin() { return FactIDs.begin(); } |
| 816 | const_iterator begin() const { return FactIDs.begin(); } |
| 817 | |
| 818 | iterator end() { return FactIDs.end(); } |
| 819 | const_iterator end() const { return FactIDs.end(); } |
| 820 | |
| 821 | bool isEmpty() const { return FactIDs.size() == 0; } |
| 822 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 823 | FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 824 | FactID F = FM.newLock(M, L); |
| 825 | FactIDs.push_back(F); |
| 826 | return F; |
| 827 | } |
| 828 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 829 | bool removeLock(FactManager& FM, const SExpr& M) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 830 | unsigned n = FactIDs.size(); |
| 831 | if (n == 0) |
| 832 | return false; |
| 833 | |
| 834 | for (unsigned i = 0; i < n-1; ++i) { |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 835 | if (FM[FactIDs[i]].MutID.matches(M)) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 836 | FactIDs[i] = FactIDs[n-1]; |
| 837 | FactIDs.pop_back(); |
| 838 | return true; |
| 839 | } |
| 840 | } |
DeLesley Hutchins | 0c90c2b | 2012-08-10 20:29:46 +0000 | [diff] [blame] | 841 | if (FM[FactIDs[n-1]].MutID.matches(M)) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 842 | FactIDs.pop_back(); |
| 843 | return true; |
| 844 | } |
| 845 | return false; |
| 846 | } |
| 847 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 848 | // Returns an iterator |
| 849 | iterator findLockIter(FactManager &FM, const SExpr &M) { |
| 850 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 851 | const SExpr &Exp = FM[*I].MutID; |
| 852 | if (Exp.matches(M)) |
| 853 | return I; |
| 854 | } |
| 855 | return end(); |
| 856 | } |
| 857 | |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 858 | LockData* findLock(FactManager &FM, const SExpr &M) const { |
Chad Rosier | 78af00f | 2012-09-07 18:44:15 +0000 | [diff] [blame] | 859 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Chad Rosier | 37a8563 | 2012-09-07 19:49:55 +0000 | [diff] [blame] | 860 | const SExpr &Exp = FM[*I].MutID; |
Chad Rosier | 78af00f | 2012-09-07 18:44:15 +0000 | [diff] [blame] | 861 | if (Exp.matches(M)) |
| 862 | return &FM[*I].LDat; |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 867 | LockData* findLockUniv(FactManager &FM, const SExpr &M) const { |
Chad Rosier | 78af00f | 2012-09-07 18:44:15 +0000 | [diff] [blame] | 868 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Chad Rosier | 37a8563 | 2012-09-07 19:49:55 +0000 | [diff] [blame] | 869 | const SExpr &Exp = FM[*I].MutID; |
Chad Rosier | 78af00f | 2012-09-07 18:44:15 +0000 | [diff] [blame] | 870 | if (Exp.matches(M) || Exp.isUniversal()) |
| 871 | return &FM[*I].LDat; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 872 | } |
| 873 | return 0; |
| 874 | } |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 875 | |
| 876 | FactEntry* findPartialMatch(FactManager &FM, const SExpr &M) const { |
| 877 | for (const_iterator I=begin(), E=end(); I != E; ++I) { |
| 878 | const SExpr& Exp = FM[*I].MutID; |
| 879 | if (Exp.partiallyMatches(M)) return &FM[*I]; |
| 880 | } |
| 881 | return 0; |
| 882 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
| 885 | |
| 886 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 887 | /// A Lockset maps each SExpr (defined above) to information about how it has |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 888 | /// been locked. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 889 | typedef llvm::ImmutableMap<SExpr, LockData> Lockset; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 890 | typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 891 | |
| 892 | class LocalVariableMap; |
| 893 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 894 | /// A side (entry or exit) of a CFG node. |
| 895 | enum CFGBlockSide { CBS_Entry, CBS_Exit }; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 896 | |
| 897 | /// CFGBlockInfo is a struct which contains all the information that is |
| 898 | /// maintained for each block in the CFG. See LocalVariableMap for more |
| 899 | /// information about the contexts. |
| 900 | struct CFGBlockInfo { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 901 | FactSet EntrySet; // Lockset held at entry to block |
| 902 | FactSet ExitSet; // Lockset held at exit from block |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 903 | LocalVarContext EntryContext; // Context held at entry to block |
| 904 | LocalVarContext ExitContext; // Context held at exit from block |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 905 | SourceLocation EntryLoc; // Location of first statement in block |
| 906 | SourceLocation ExitLoc; // Location of last statement in block. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 907 | unsigned EntryIndex; // Used to replay contexts later |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 908 | bool Reachable; // Is this block reachable? |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 909 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 910 | const FactSet &getSet(CFGBlockSide Side) const { |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 911 | return Side == CBS_Entry ? EntrySet : ExitSet; |
| 912 | } |
| 913 | SourceLocation getLocation(CFGBlockSide Side) const { |
| 914 | return Side == CBS_Entry ? EntryLoc : ExitLoc; |
| 915 | } |
| 916 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 917 | private: |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 918 | CFGBlockInfo(LocalVarContext EmptyCtx) |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 919 | : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 920 | { } |
| 921 | |
| 922 | public: |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 923 | static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 924 | }; |
| 925 | |
| 926 | |
| 927 | |
| 928 | // A LocalVariableMap maintains a map from local variables to their currently |
| 929 | // valid definitions. It provides SSA-like functionality when traversing the |
| 930 | // CFG. Like SSA, each definition or assignment to a variable is assigned a |
| 931 | // unique name (an integer), which acts as the SSA name for that definition. |
| 932 | // The total set of names is shared among all CFG basic blocks. |
| 933 | // Unlike SSA, we do not rewrite expressions to replace local variables declrefs |
| 934 | // with their SSA-names. Instead, we compute a Context for each point in the |
| 935 | // code, which maps local variables to the appropriate SSA-name. This map |
| 936 | // changes with each assignment. |
| 937 | // |
| 938 | // The map is computed in a single pass over the CFG. Subsequent analyses can |
| 939 | // then query the map to find the appropriate Context for a statement, and use |
| 940 | // that Context to look up the definitions of variables. |
| 941 | class LocalVariableMap { |
| 942 | public: |
| 943 | typedef LocalVarContext Context; |
| 944 | |
| 945 | /// A VarDefinition consists of an expression, representing the value of the |
| 946 | /// variable, along with the context in which that expression should be |
| 947 | /// interpreted. A reference VarDefinition does not itself contain this |
| 948 | /// information, but instead contains a pointer to a previous VarDefinition. |
| 949 | struct VarDefinition { |
| 950 | public: |
| 951 | friend class LocalVariableMap; |
| 952 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 953 | const NamedDecl *Dec; // The original declaration for this variable. |
| 954 | const Expr *Exp; // The expression for this variable, OR |
| 955 | unsigned Ref; // Reference to another VarDefinition |
| 956 | Context Ctx; // The map with which Exp should be interpreted. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 957 | |
| 958 | bool isReference() { return !Exp; } |
| 959 | |
| 960 | private: |
| 961 | // Create ordinary variable definition |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 962 | VarDefinition(const NamedDecl *D, const Expr *E, Context C) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 963 | : Dec(D), Exp(E), Ref(0), Ctx(C) |
| 964 | { } |
| 965 | |
| 966 | // Create reference to previous definition |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 967 | VarDefinition(const NamedDecl *D, unsigned R, Context C) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 968 | : Dec(D), Exp(0), Ref(R), Ctx(C) |
| 969 | { } |
| 970 | }; |
| 971 | |
| 972 | private: |
| 973 | Context::Factory ContextFactory; |
| 974 | std::vector<VarDefinition> VarDefinitions; |
| 975 | std::vector<unsigned> CtxIndices; |
| 976 | std::vector<std::pair<Stmt*, Context> > SavedContexts; |
| 977 | |
| 978 | public: |
| 979 | LocalVariableMap() { |
| 980 | // index 0 is a placeholder for undefined variables (aka phi-nodes). |
| 981 | VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext())); |
| 982 | } |
| 983 | |
| 984 | /// Look up a definition, within the given context. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 985 | const VarDefinition* lookup(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 986 | const unsigned *i = Ctx.lookup(D); |
| 987 | if (!i) |
| 988 | return 0; |
| 989 | assert(*i < VarDefinitions.size()); |
| 990 | return &VarDefinitions[*i]; |
| 991 | } |
| 992 | |
| 993 | /// Look up the definition for D within the given context. Returns |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 994 | /// NULL if the expression is not statically known. If successful, also |
| 995 | /// modifies Ctx to hold the context of the return Expr. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 996 | const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 997 | const unsigned *P = Ctx.lookup(D); |
| 998 | if (!P) |
| 999 | return 0; |
| 1000 | |
| 1001 | unsigned i = *P; |
| 1002 | while (i > 0) { |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1003 | if (VarDefinitions[i].Exp) { |
| 1004 | Ctx = VarDefinitions[i].Ctx; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1005 | return VarDefinitions[i].Exp; |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1006 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1007 | i = VarDefinitions[i].Ref; |
| 1008 | } |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | Context getEmptyContext() { return ContextFactory.getEmptyMap(); } |
| 1013 | |
| 1014 | /// Return the next context after processing S. This function is used by |
| 1015 | /// clients of the class to get the appropriate context when traversing the |
| 1016 | /// CFG. It must be called for every assignment or DeclStmt. |
| 1017 | Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) { |
| 1018 | if (SavedContexts[CtxIndex+1].first == S) { |
| 1019 | CtxIndex++; |
| 1020 | Context Result = SavedContexts[CtxIndex].second; |
| 1021 | return Result; |
| 1022 | } |
| 1023 | return C; |
| 1024 | } |
| 1025 | |
| 1026 | void dumpVarDefinitionName(unsigned i) { |
| 1027 | if (i == 0) { |
| 1028 | llvm::errs() << "Undefined"; |
| 1029 | return; |
| 1030 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1031 | const NamedDecl *Dec = VarDefinitions[i].Dec; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1032 | if (!Dec) { |
| 1033 | llvm::errs() << "<<NULL>>"; |
| 1034 | return; |
| 1035 | } |
| 1036 | Dec->printName(llvm::errs()); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1037 | llvm::errs() << "." << i << " " << ((const void*) Dec); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /// Dumps an ASCII representation of the variable map to llvm::errs() |
| 1041 | void dump() { |
| 1042 | for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1043 | const Expr *Exp = VarDefinitions[i].Exp; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1044 | unsigned Ref = VarDefinitions[i].Ref; |
| 1045 | |
| 1046 | dumpVarDefinitionName(i); |
| 1047 | llvm::errs() << " = "; |
| 1048 | if (Exp) Exp->dump(); |
| 1049 | else { |
| 1050 | dumpVarDefinitionName(Ref); |
| 1051 | llvm::errs() << "\n"; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /// Dumps an ASCII representation of a Context to llvm::errs() |
| 1057 | void dumpContext(Context C) { |
| 1058 | for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1059 | const NamedDecl *D = I.getKey(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1060 | D->printName(llvm::errs()); |
| 1061 | const unsigned *i = C.lookup(D); |
| 1062 | llvm::errs() << " -> "; |
| 1063 | dumpVarDefinitionName(*i); |
| 1064 | llvm::errs() << "\n"; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /// Builds the variable map. |
| 1069 | void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph, |
| 1070 | std::vector<CFGBlockInfo> &BlockInfo); |
| 1071 | |
| 1072 | protected: |
| 1073 | // Get the current context index |
| 1074 | unsigned getContextIndex() { return SavedContexts.size()-1; } |
| 1075 | |
| 1076 | // Save the current context for later replay |
| 1077 | void saveContext(Stmt *S, Context C) { |
| 1078 | SavedContexts.push_back(std::make_pair(S,C)); |
| 1079 | } |
| 1080 | |
| 1081 | // Adds a new definition to the given context, and returns a new context. |
| 1082 | // This method should be called when declaring a new variable. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1083 | Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1084 | assert(!Ctx.contains(D)); |
| 1085 | unsigned newID = VarDefinitions.size(); |
| 1086 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 1087 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 1088 | return NewCtx; |
| 1089 | } |
| 1090 | |
| 1091 | // Add a new reference to an existing definition. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1092 | Context addReference(const NamedDecl *D, unsigned i, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1093 | unsigned newID = VarDefinitions.size(); |
| 1094 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 1095 | VarDefinitions.push_back(VarDefinition(D, i, Ctx)); |
| 1096 | return NewCtx; |
| 1097 | } |
| 1098 | |
| 1099 | // Updates a definition only if that definition is already in the map. |
| 1100 | // This method should be called when assigning to an existing variable. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1101 | Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1102 | if (Ctx.contains(D)) { |
| 1103 | unsigned newID = VarDefinitions.size(); |
| 1104 | Context NewCtx = ContextFactory.remove(Ctx, D); |
| 1105 | NewCtx = ContextFactory.add(NewCtx, D, newID); |
| 1106 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 1107 | return NewCtx; |
| 1108 | } |
| 1109 | return Ctx; |
| 1110 | } |
| 1111 | |
| 1112 | // Removes a definition from the context, but keeps the variable name |
| 1113 | // as a valid variable. The index 0 is a placeholder for cleared definitions. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1114 | Context clearDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1115 | Context NewCtx = Ctx; |
| 1116 | if (NewCtx.contains(D)) { |
| 1117 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 1118 | NewCtx = ContextFactory.add(NewCtx, D, 0); |
| 1119 | } |
| 1120 | return NewCtx; |
| 1121 | } |
| 1122 | |
| 1123 | // Remove a definition entirely frmo the context. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1124 | Context removeDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1125 | Context NewCtx = Ctx; |
| 1126 | if (NewCtx.contains(D)) { |
| 1127 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 1128 | } |
| 1129 | return NewCtx; |
| 1130 | } |
| 1131 | |
| 1132 | Context intersectContexts(Context C1, Context C2); |
| 1133 | Context createReferenceContext(Context C); |
| 1134 | void intersectBackEdge(Context C1, Context C2); |
| 1135 | |
| 1136 | friend class VarMapBuilder; |
| 1137 | }; |
| 1138 | |
| 1139 | |
| 1140 | // This has to be defined after LocalVariableMap. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1141 | CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) { |
| 1142 | return CFGBlockInfo(M.getEmptyContext()); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | /// Visitor which builds a LocalVariableMap |
| 1147 | class VarMapBuilder : public StmtVisitor<VarMapBuilder> { |
| 1148 | public: |
| 1149 | LocalVariableMap* VMap; |
| 1150 | LocalVariableMap::Context Ctx; |
| 1151 | |
| 1152 | VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C) |
| 1153 | : VMap(VM), Ctx(C) {} |
| 1154 | |
| 1155 | void VisitDeclStmt(DeclStmt *S); |
| 1156 | void VisitBinaryOperator(BinaryOperator *BO); |
| 1157 | }; |
| 1158 | |
| 1159 | |
| 1160 | // Add new local variables to the variable map |
| 1161 | void VarMapBuilder::VisitDeclStmt(DeclStmt *S) { |
| 1162 | bool modifiedCtx = false; |
| 1163 | DeclGroupRef DGrp = S->getDeclGroup(); |
| 1164 | for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) { |
| 1165 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) { |
| 1166 | Expr *E = VD->getInit(); |
| 1167 | |
| 1168 | // Add local variables with trivial type to the variable map |
| 1169 | QualType T = VD->getType(); |
| 1170 | if (T.isTrivialType(VD->getASTContext())) { |
| 1171 | Ctx = VMap->addDefinition(VD, E, Ctx); |
| 1172 | modifiedCtx = true; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | if (modifiedCtx) |
| 1177 | VMap->saveContext(S, Ctx); |
| 1178 | } |
| 1179 | |
| 1180 | // Update local variable definitions in variable map |
| 1181 | void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) { |
| 1182 | if (!BO->isAssignmentOp()) |
| 1183 | return; |
| 1184 | |
| 1185 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
| 1186 | |
| 1187 | // Update the variable map and current context. |
| 1188 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) { |
| 1189 | ValueDecl *VDec = DRE->getDecl(); |
| 1190 | if (Ctx.lookup(VDec)) { |
| 1191 | if (BO->getOpcode() == BO_Assign) |
| 1192 | Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx); |
| 1193 | else |
| 1194 | // FIXME -- handle compound assignment operators |
| 1195 | Ctx = VMap->clearDefinition(VDec, Ctx); |
| 1196 | VMap->saveContext(BO, Ctx); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | // Computes the intersection of two contexts. The intersection is the |
| 1203 | // set of variables which have the same definition in both contexts; |
| 1204 | // variables with different definitions are discarded. |
| 1205 | LocalVariableMap::Context |
| 1206 | LocalVariableMap::intersectContexts(Context C1, Context C2) { |
| 1207 | Context Result = C1; |
| 1208 | for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1209 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1210 | unsigned i1 = I.getData(); |
| 1211 | const unsigned *i2 = C2.lookup(Dec); |
| 1212 | if (!i2) // variable doesn't exist on second path |
| 1213 | Result = removeDefinition(Dec, Result); |
| 1214 | else if (*i2 != i1) // variable exists, but has different definition |
| 1215 | Result = clearDefinition(Dec, Result); |
| 1216 | } |
| 1217 | return Result; |
| 1218 | } |
| 1219 | |
| 1220 | // For every variable in C, create a new variable that refers to the |
| 1221 | // definition in C. Return a new context that contains these new variables. |
| 1222 | // (We use this for a naive implementation of SSA on loop back-edges.) |
| 1223 | LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) { |
| 1224 | Context Result = getEmptyContext(); |
| 1225 | for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1226 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1227 | unsigned i = I.getData(); |
| 1228 | Result = addReference(Dec, i, Result); |
| 1229 | } |
| 1230 | return Result; |
| 1231 | } |
| 1232 | |
| 1233 | // This routine also takes the intersection of C1 and C2, but it does so by |
| 1234 | // altering the VarDefinitions. C1 must be the result of an earlier call to |
| 1235 | // createReferenceContext. |
| 1236 | void LocalVariableMap::intersectBackEdge(Context C1, Context C2) { |
| 1237 | for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1238 | const NamedDecl *Dec = I.getKey(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1239 | unsigned i1 = I.getData(); |
| 1240 | VarDefinition *VDef = &VarDefinitions[i1]; |
| 1241 | assert(VDef->isReference()); |
| 1242 | |
| 1243 | const unsigned *i2 = C2.lookup(Dec); |
| 1244 | if (!i2 || (*i2 != i1)) |
| 1245 | VDef->Ref = 0; // Mark this variable as undefined |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | // Traverse the CFG in topological order, so all predecessors of a block |
| 1251 | // (excluding back-edges) are visited before the block itself. At |
| 1252 | // each point in the code, we calculate a Context, which holds the set of |
| 1253 | // variable definitions which are visible at that point in execution. |
| 1254 | // Visible variables are mapped to their definitions using an array that |
| 1255 | // contains all definitions. |
| 1256 | // |
| 1257 | // At join points in the CFG, the set is computed as the intersection of |
| 1258 | // the incoming sets along each edge, E.g. |
| 1259 | // |
| 1260 | // { Context | VarDefinitions } |
| 1261 | // int x = 0; { x -> x1 | x1 = 0 } |
| 1262 | // int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 1263 | // if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... } |
| 1264 | // else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... } |
| 1265 | // ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... } |
| 1266 | // |
| 1267 | // This is essentially a simpler and more naive version of the standard SSA |
| 1268 | // algorithm. Those definitions that remain in the intersection are from blocks |
| 1269 | // that strictly dominate the current block. We do not bother to insert proper |
| 1270 | // phi nodes, because they are not used in our analysis; instead, wherever |
| 1271 | // a phi node would be required, we simply remove that definition from the |
| 1272 | // context (E.g. x above). |
| 1273 | // |
| 1274 | // The initial traversal does not capture back-edges, so those need to be |
| 1275 | // handled on a separate pass. Whenever the first pass encounters an |
| 1276 | // incoming back edge, it duplicates the context, creating new definitions |
| 1277 | // that refer back to the originals. (These correspond to places where SSA |
| 1278 | // might have to insert a phi node.) On the second pass, these definitions are |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 1279 | // set to NULL if the variable has changed on the back-edge (i.e. a phi |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1280 | // node was actually required.) E.g. |
| 1281 | // |
| 1282 | // { Context | VarDefinitions } |
| 1283 | // int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 1284 | // while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; } |
| 1285 | // x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... } |
| 1286 | // ... { y -> y1 | x3 = 2, x2 = 1, ... } |
| 1287 | // |
| 1288 | void LocalVariableMap::traverseCFG(CFG *CFGraph, |
| 1289 | PostOrderCFGView *SortedGraph, |
| 1290 | std::vector<CFGBlockInfo> &BlockInfo) { |
| 1291 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
| 1292 | |
| 1293 | CtxIndices.resize(CFGraph->getNumBlockIDs()); |
| 1294 | |
| 1295 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 1296 | E = SortedGraph->end(); I!= E; ++I) { |
| 1297 | const CFGBlock *CurrBlock = *I; |
| 1298 | int CurrBlockID = CurrBlock->getBlockID(); |
| 1299 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
| 1300 | |
| 1301 | VisitedBlocks.insert(CurrBlock); |
| 1302 | |
| 1303 | // Calculate the entry context for the current block |
| 1304 | bool HasBackEdges = false; |
| 1305 | bool CtxInit = true; |
| 1306 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 1307 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 1308 | // if *PI -> CurrBlock is a back edge, so skip it |
| 1309 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) { |
| 1310 | HasBackEdges = true; |
| 1311 | continue; |
| 1312 | } |
| 1313 | |
| 1314 | int PrevBlockID = (*PI)->getBlockID(); |
| 1315 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 1316 | |
| 1317 | if (CtxInit) { |
| 1318 | CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext; |
| 1319 | CtxInit = false; |
| 1320 | } |
| 1321 | else { |
| 1322 | CurrBlockInfo->EntryContext = |
| 1323 | intersectContexts(CurrBlockInfo->EntryContext, |
| 1324 | PrevBlockInfo->ExitContext); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | // Duplicate the context if we have back-edges, so we can call |
| 1329 | // intersectBackEdges later. |
| 1330 | if (HasBackEdges) |
| 1331 | CurrBlockInfo->EntryContext = |
| 1332 | createReferenceContext(CurrBlockInfo->EntryContext); |
| 1333 | |
| 1334 | // Create a starting context index for the current block |
| 1335 | saveContext(0, CurrBlockInfo->EntryContext); |
| 1336 | CurrBlockInfo->EntryIndex = getContextIndex(); |
| 1337 | |
| 1338 | // Visit all the statements in the basic block. |
| 1339 | VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext); |
| 1340 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 1341 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 1342 | switch (BI->getKind()) { |
| 1343 | case CFGElement::Statement: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 1344 | CFGStmt CS = BI->castAs<CFGStmt>(); |
| 1345 | VMapBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | default: |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | CurrBlockInfo->ExitContext = VMapBuilder.Ctx; |
| 1353 | |
| 1354 | // Mark variables on back edges as "unknown" if they've been changed. |
| 1355 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 1356 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 1357 | // if CurrBlock -> *SI is *not* a back edge |
| 1358 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
| 1359 | continue; |
| 1360 | |
| 1361 | CFGBlock *FirstLoopBlock = *SI; |
| 1362 | Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext; |
| 1363 | Context LoopEnd = CurrBlockInfo->ExitContext; |
| 1364 | intersectBackEdge(LoopBegin, LoopEnd); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | // Put an extra entry at the end of the indexed context array |
| 1369 | unsigned exitID = CFGraph->getExit().getBlockID(); |
| 1370 | saveContext(0, BlockInfo[exitID].ExitContext); |
| 1371 | } |
| 1372 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1373 | /// Find the appropriate source locations to use when producing diagnostics for |
| 1374 | /// each block in the CFG. |
| 1375 | static void findBlockLocations(CFG *CFGraph, |
| 1376 | PostOrderCFGView *SortedGraph, |
| 1377 | std::vector<CFGBlockInfo> &BlockInfo) { |
| 1378 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 1379 | E = SortedGraph->end(); I!= E; ++I) { |
| 1380 | const CFGBlock *CurrBlock = *I; |
| 1381 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()]; |
| 1382 | |
| 1383 | // Find the source location of the last statement in the block, if the |
| 1384 | // block is not empty. |
| 1385 | if (const Stmt *S = CurrBlock->getTerminator()) { |
| 1386 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart(); |
| 1387 | } else { |
| 1388 | for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(), |
| 1389 | BE = CurrBlock->rend(); BI != BE; ++BI) { |
| 1390 | // FIXME: Handle other CFGElement kinds. |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1391 | if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { |
| 1392 | CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart(); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | if (!CurrBlockInfo->ExitLoc.isInvalid()) { |
| 1399 | // This block contains at least one statement. Find the source location |
| 1400 | // of the first statement in the block. |
| 1401 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 1402 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 1403 | // FIXME: Handle other CFGElement kinds. |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1404 | if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { |
| 1405 | CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart(); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | } |
| 1409 | } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() && |
| 1410 | CurrBlock != &CFGraph->getExit()) { |
| 1411 | // The block is empty, and has a single predecessor. Use its exit |
| 1412 | // location. |
| 1413 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = |
| 1414 | BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1418 | |
| 1419 | /// \brief Class which implements the core thread safety analysis routines. |
| 1420 | class ThreadSafetyAnalyzer { |
| 1421 | friend class BuildLockset; |
| 1422 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1423 | ThreadSafetyHandler &Handler; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1424 | LocalVariableMap LocalVarMap; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1425 | FactManager FactMan; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1426 | std::vector<CFGBlockInfo> BlockInfo; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1427 | |
| 1428 | public: |
| 1429 | ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {} |
| 1430 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1431 | void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat); |
| 1432 | void removeLock(FactSet &FSet, const SExpr &Mutex, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1433 | SourceLocation UnlockLoc, bool FullyRemove=false); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1434 | |
| 1435 | template <typename AttrType> |
| 1436 | void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp, |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1437 | const NamedDecl *D, VarDecl *SelfDecl=0); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1438 | |
| 1439 | template <class AttrType> |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1440 | void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp, |
| 1441 | const NamedDecl *D, |
| 1442 | const CFGBlock *PredBlock, const CFGBlock *CurrBlock, |
| 1443 | Expr *BrE, bool Neg); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1444 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1445 | const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C, |
| 1446 | bool &Negate); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1447 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1448 | void getEdgeLockset(FactSet &Result, const FactSet &ExitSet, |
| 1449 | const CFGBlock* PredBlock, |
| 1450 | const CFGBlock *CurrBlock); |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1451 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1452 | void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, |
| 1453 | SourceLocation JoinLoc, |
| 1454 | LockErrorKind LEK1, LockErrorKind LEK2, |
| 1455 | bool Modify=true); |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 1456 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1457 | void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, |
| 1458 | SourceLocation JoinLoc, LockErrorKind LEK1, |
| 1459 | bool Modify=true) { |
| 1460 | intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify); |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 1461 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1462 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1463 | void runAnalysis(AnalysisDeclContext &AC); |
| 1464 | }; |
| 1465 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1466 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1467 | /// \brief Add a new lock to the lockset, warning if the lock is already there. |
| 1468 | /// \param Mutex -- the Mutex expression for the lock |
| 1469 | /// \param LDat -- the LockData for the lock |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1470 | void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1471 | const LockData &LDat) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1472 | // FIXME: deal with acquired before/after annotations. |
| 1473 | // FIXME: Don't always warn when we have support for reentrant locks. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 1474 | if (Mutex.shouldIgnore()) |
| 1475 | return; |
| 1476 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1477 | if (FSet.findLock(FactMan, Mutex)) { |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1478 | if (!LDat.Asserted) |
| 1479 | Handler.handleDoubleLock(Mutex.toString(), LDat.AcquireLoc); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1480 | } else { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1481 | FSet.addLock(FactMan, Mutex, LDat); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1485 | |
| 1486 | /// \brief Remove a lock from the lockset, warning if the lock is not there. |
Ted Kremenek | 78094ca | 2012-08-22 23:50:41 +0000 | [diff] [blame] | 1487 | /// \param Mutex The lock expression corresponding to the lock to be removed |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1488 | /// \param UnlockLoc The source location of the unlock (only used in error msg) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1489 | void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1490 | const SExpr &Mutex, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1491 | SourceLocation UnlockLoc, |
| 1492 | bool FullyRemove) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 1493 | if (Mutex.shouldIgnore()) |
| 1494 | return; |
| 1495 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1496 | const LockData *LDat = FSet.findLock(FactMan, Mutex); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1497 | if (!LDat) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1498 | Handler.handleUnmatchedUnlock(Mutex.toString(), UnlockLoc); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1499 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1500 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1501 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1502 | if (LDat->UnderlyingMutex.isValid()) { |
| 1503 | // This is scoped lockable object, which manages the real mutex. |
| 1504 | if (FullyRemove) { |
| 1505 | // We're destroying the managing object. |
| 1506 | // Remove the underlying mutex if it exists; but don't warn. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1507 | if (FSet.findLock(FactMan, LDat->UnderlyingMutex)) |
| 1508 | FSet.removeLock(FactMan, LDat->UnderlyingMutex); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1509 | } else { |
| 1510 | // We're releasing the underlying mutex, but not destroying the |
| 1511 | // managing object. Warn on dual release. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1512 | if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1513 | Handler.handleUnmatchedUnlock(LDat->UnderlyingMutex.toString(), |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1514 | UnlockLoc); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1515 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1516 | FSet.removeLock(FactMan, LDat->UnderlyingMutex); |
| 1517 | return; |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1518 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1519 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1520 | FSet.removeLock(FactMan, Mutex); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1523 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1524 | /// \brief Extract the list of mutexIDs from the attribute on an expression, |
| 1525 | /// and push them onto Mtxs, discarding any duplicates. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1526 | template <typename AttrType> |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1527 | void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1528 | Expr *Exp, const NamedDecl *D, |
| 1529 | VarDecl *SelfDecl) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1530 | typedef typename AttrType::args_iterator iterator_type; |
| 1531 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1532 | if (Attr->args_size() == 0) { |
| 1533 | // The mutex held is the "this" object. |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1534 | SExpr Mu(0, Exp, D, SelfDecl); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1535 | if (!Mu.isValid()) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1536 | SExpr::warnInvalidLock(Handler, 0, Exp, D); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1537 | else |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1538 | Mtxs.push_back_nodup(Mu); |
| 1539 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1540 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1541 | |
| 1542 | for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) { |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1543 | SExpr Mu(*I, Exp, D, SelfDecl); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1544 | if (!Mu.isValid()) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1545 | SExpr::warnInvalidLock(Handler, *I, Exp, D); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1546 | else |
| 1547 | Mtxs.push_back_nodup(Mu); |
| 1548 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1552 | /// \brief Extract the list of mutexIDs from a trylock attribute. If the |
| 1553 | /// trylock applies to the given edge, then push them onto Mtxs, discarding |
| 1554 | /// any duplicates. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1555 | template <class AttrType> |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1556 | void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, |
| 1557 | Expr *Exp, const NamedDecl *D, |
| 1558 | const CFGBlock *PredBlock, |
| 1559 | const CFGBlock *CurrBlock, |
| 1560 | Expr *BrE, bool Neg) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1561 | // Find out which branch has the lock |
| 1562 | bool branch = 0; |
| 1563 | if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) { |
| 1564 | branch = BLE->getValue(); |
| 1565 | } |
| 1566 | else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) { |
| 1567 | branch = ILE->getValue().getBoolValue(); |
| 1568 | } |
| 1569 | int branchnum = branch ? 0 : 1; |
| 1570 | if (Neg) branchnum = !branchnum; |
| 1571 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1572 | // If we've taken the trylock branch, then add the lock |
| 1573 | int i = 0; |
| 1574 | for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(), |
| 1575 | SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) { |
| 1576 | if (*SI == CurrBlock && i == branchnum) { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1577 | getMutexIDs(Mtxs, Attr, Exp, D); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1583 | bool getStaticBooleanValue(Expr* E, bool& TCond) { |
| 1584 | if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) { |
| 1585 | TCond = false; |
| 1586 | return true; |
| 1587 | } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) { |
| 1588 | TCond = BLE->getValue(); |
| 1589 | return true; |
| 1590 | } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) { |
| 1591 | TCond = ILE->getValue().getBoolValue(); |
| 1592 | return true; |
| 1593 | } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
| 1594 | return getStaticBooleanValue(CE->getSubExpr(), TCond); |
| 1595 | } |
| 1596 | return false; |
| 1597 | } |
| 1598 | |
| 1599 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1600 | // If Cond can be traced back to a function call, return the call expression. |
| 1601 | // The negate variable should be called with false, and will be set to true |
| 1602 | // if the function call is negated, e.g. if (!mu.tryLock(...)) |
| 1603 | const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond, |
| 1604 | LocalVarContext C, |
| 1605 | bool &Negate) { |
| 1606 | if (!Cond) |
| 1607 | return 0; |
| 1608 | |
| 1609 | if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) { |
| 1610 | return CallExp; |
| 1611 | } |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1612 | else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) { |
| 1613 | return getTrylockCallExpr(PE->getSubExpr(), C, Negate); |
| 1614 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1615 | else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) { |
| 1616 | return getTrylockCallExpr(CE->getSubExpr(), C, Negate); |
| 1617 | } |
DeLesley Hutchins | 93b1b03 | 2012-09-05 20:01:16 +0000 | [diff] [blame] | 1618 | else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) { |
| 1619 | return getTrylockCallExpr(EWC->getSubExpr(), C, Negate); |
| 1620 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1621 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) { |
| 1622 | const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C); |
| 1623 | return getTrylockCallExpr(E, C, Negate); |
| 1624 | } |
| 1625 | else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) { |
| 1626 | if (UOP->getOpcode() == UO_LNot) { |
| 1627 | Negate = !Negate; |
| 1628 | return getTrylockCallExpr(UOP->getSubExpr(), C, Negate); |
| 1629 | } |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1630 | return 0; |
| 1631 | } |
| 1632 | else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) { |
| 1633 | if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) { |
| 1634 | if (BOP->getOpcode() == BO_NE) |
| 1635 | Negate = !Negate; |
| 1636 | |
| 1637 | bool TCond = false; |
| 1638 | if (getStaticBooleanValue(BOP->getRHS(), TCond)) { |
| 1639 | if (!TCond) Negate = !Negate; |
| 1640 | return getTrylockCallExpr(BOP->getLHS(), C, Negate); |
| 1641 | } |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1642 | TCond = false; |
| 1643 | if (getStaticBooleanValue(BOP->getLHS(), TCond)) { |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1644 | if (!TCond) Negate = !Negate; |
| 1645 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1646 | } |
| 1647 | return 0; |
| 1648 | } |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1649 | if (BOP->getOpcode() == BO_LAnd) { |
| 1650 | // LHS must have been evaluated in a different block. |
| 1651 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1652 | } |
| 1653 | if (BOP->getOpcode() == BO_LOr) { |
| 1654 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1655 | } |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1656 | return 0; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1657 | } |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1658 | return 0; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1662 | /// \brief Find the lockset that holds on the edge between PredBlock |
| 1663 | /// and CurrBlock. The edge set is the exit set of PredBlock (passed |
| 1664 | /// as the ExitSet parameter) plus any trylocks, which are conditionally held. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1665 | void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result, |
| 1666 | const FactSet &ExitSet, |
| 1667 | const CFGBlock *PredBlock, |
| 1668 | const CFGBlock *CurrBlock) { |
| 1669 | Result = ExitSet; |
| 1670 | |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1671 | const Stmt *Cond = PredBlock->getTerminatorCondition(); |
| 1672 | if (!Cond) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1673 | return; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1674 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1675 | bool Negate = false; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1676 | const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()]; |
| 1677 | const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext; |
| 1678 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1679 | CallExpr *Exp = |
| 1680 | const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate)); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1681 | if (!Exp) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1682 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1683 | |
| 1684 | NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1685 | if(!FunDecl || !FunDecl->hasAttrs()) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1686 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1687 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1688 | MutexIDList ExclusiveLocksToAdd; |
| 1689 | MutexIDList SharedLocksToAdd; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1690 | |
| 1691 | // If the condition is a call to a Trylock function, then grab the attributes |
| 1692 | AttrVec &ArgAttrs = FunDecl->getAttrs(); |
| 1693 | for (unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 1694 | Attr *Attr = ArgAttrs[i]; |
| 1695 | switch (Attr->getKind()) { |
| 1696 | case attr::ExclusiveTrylockFunction: { |
| 1697 | ExclusiveTrylockFunctionAttr *A = |
| 1698 | cast<ExclusiveTrylockFunctionAttr>(Attr); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1699 | getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl, |
| 1700 | PredBlock, CurrBlock, A->getSuccessValue(), Negate); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1701 | break; |
| 1702 | } |
| 1703 | case attr::SharedTrylockFunction: { |
| 1704 | SharedTrylockFunctionAttr *A = |
| 1705 | cast<SharedTrylockFunctionAttr>(Attr); |
DeLesley Hutchins | fcb0ffa | 2012-09-20 23:14:43 +0000 | [diff] [blame] | 1706 | getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl, |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1707 | PredBlock, CurrBlock, A->getSuccessValue(), Negate); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1708 | break; |
| 1709 | } |
| 1710 | default: |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1714 | |
| 1715 | // Add and remove locks. |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1716 | SourceLocation Loc = Exp->getExprLoc(); |
| 1717 | for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1718 | addLock(Result, ExclusiveLocksToAdd[i], |
| 1719 | LockData(Loc, LK_Exclusive)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1720 | } |
| 1721 | for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1722 | addLock(Result, SharedLocksToAdd[i], |
| 1723 | LockData(Loc, LK_Shared)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1724 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1728 | /// \brief We use this class to visit different types of expressions in |
| 1729 | /// CFGBlocks, and build up the lockset. |
| 1730 | /// An expression may cause us to add or remove locks from the lockset, or else |
| 1731 | /// output error messages related to missing locks. |
| 1732 | /// FIXME: In future, we may be able to not inherit from a visitor. |
| 1733 | class BuildLockset : public StmtVisitor<BuildLockset> { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1734 | friend class ThreadSafetyAnalyzer; |
| 1735 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1736 | ThreadSafetyAnalyzer *Analyzer; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1737 | FactSet FSet; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1738 | LocalVariableMap::Context LVarCtx; |
| 1739 | unsigned CtxIndex; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1740 | |
| 1741 | // Helper functions |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1742 | const ValueDecl *getValueDecl(const Expr *Exp); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1743 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1744 | void warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, AccessKind AK, |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1745 | Expr *MutexExp, ProtectedOperationKind POK); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1746 | void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1747 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1748 | void checkAccess(const Expr *Exp, AccessKind AK); |
| 1749 | void checkPtAccess(const Expr *Exp, AccessKind AK); |
| 1750 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1751 | void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1752 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1753 | public: |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1754 | BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1755 | : StmtVisitor<BuildLockset>(), |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1756 | Analyzer(Anlzr), |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1757 | FSet(Info.EntrySet), |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1758 | LVarCtx(Info.EntryContext), |
| 1759 | CtxIndex(Info.EntryIndex) |
| 1760 | {} |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1761 | |
| 1762 | void VisitUnaryOperator(UnaryOperator *UO); |
| 1763 | void VisitBinaryOperator(BinaryOperator *BO); |
| 1764 | void VisitCastExpr(CastExpr *CE); |
DeLesley Hutchins | 714296c | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1765 | void VisitCallExpr(CallExpr *Exp); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1766 | void VisitCXXConstructExpr(CXXConstructExpr *Exp); |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1767 | void VisitDeclStmt(DeclStmt *S); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1768 | }; |
| 1769 | |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1770 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1771 | /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1772 | const ValueDecl *BuildLockset::getValueDecl(const Expr *Exp) { |
| 1773 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Exp)) |
| 1774 | return getValueDecl(CE->getSubExpr()); |
| 1775 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1776 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp)) |
| 1777 | return DR->getDecl(); |
| 1778 | |
| 1779 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) |
| 1780 | return ME->getMemberDecl(); |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | /// \brief Warn if the LSet does not contain a lock sufficient to protect access |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1786 | /// of at least the passed in AccessKind. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1787 | void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1788 | AccessKind AK, Expr *MutexExp, |
| 1789 | ProtectedOperationKind POK) { |
| 1790 | LockKind LK = getLockKindFromAccessKind(AK); |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1791 | |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1792 | SExpr Mutex(MutexExp, Exp, D); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1793 | if (!Mutex.isValid()) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1794 | SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1795 | return; |
| 1796 | } else if (Mutex.shouldIgnore()) { |
| 1797 | return; |
| 1798 | } |
| 1799 | |
| 1800 | LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1801 | bool NoError = true; |
| 1802 | if (!LDat) { |
| 1803 | // No exact match found. Look for a partial match. |
| 1804 | FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex); |
| 1805 | if (FEntry) { |
| 1806 | // Warn that there's no precise match. |
| 1807 | LDat = &FEntry->LDat; |
| 1808 | std::string PartMatchStr = FEntry->MutID.toString(); |
| 1809 | StringRef PartMatchName(PartMatchStr); |
| 1810 | Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK, |
| 1811 | Exp->getExprLoc(), &PartMatchName); |
| 1812 | } else { |
| 1813 | // Warn that there's no match at all. |
| 1814 | Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK, |
| 1815 | Exp->getExprLoc()); |
| 1816 | } |
| 1817 | NoError = false; |
| 1818 | } |
| 1819 | // Make sure the mutex we found is the right kind. |
| 1820 | if (NoError && LDat && !LDat->isAtLeast(LK)) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 1821 | Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK, |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1822 | Exp->getExprLoc()); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1825 | /// \brief Warn if the LSet contains the given lock. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1826 | void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr* Exp, |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1827 | Expr *MutexExp) { |
| 1828 | SExpr Mutex(MutexExp, Exp, D); |
| 1829 | if (!Mutex.isValid()) { |
| 1830 | SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D); |
| 1831 | return; |
| 1832 | } |
| 1833 | |
| 1834 | LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex); |
DeLesley Hutchins | a15e1b4 | 2012-09-19 19:18:29 +0000 | [diff] [blame] | 1835 | if (LDat) { |
| 1836 | std::string DeclName = D->getNameAsString(); |
| 1837 | StringRef DeclNameSR (DeclName); |
| 1838 | Analyzer->Handler.handleFunExcludesLock(DeclNameSR, Mutex.toString(), |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1839 | Exp->getExprLoc()); |
DeLesley Hutchins | a15e1b4 | 2012-09-19 19:18:29 +0000 | [diff] [blame] | 1840 | } |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1844 | /// \brief Checks guarded_by and pt_guarded_by attributes. |
| 1845 | /// Whenever we identify an access (read or write) to a DeclRefExpr that is |
| 1846 | /// marked with guarded_by, we must ensure the appropriate mutexes are held. |
| 1847 | /// Similarly, we check if the access is to an expression that dereferences |
| 1848 | /// a pointer marked with pt_guarded_by. |
| 1849 | void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) { |
| 1850 | Exp = Exp->IgnoreParenCasts(); |
| 1851 | |
| 1852 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp)) { |
| 1853 | // For dereferences |
| 1854 | if (UO->getOpcode() == clang::UO_Deref) |
| 1855 | checkPtAccess(UO->getSubExpr(), AK); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1856 | return; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1857 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1858 | |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1859 | if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(Exp)) { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1860 | checkPtAccess(AE->getLHS(), AK); |
| 1861 | return; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1864 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 1865 | if (ME->isArrow()) |
| 1866 | checkPtAccess(ME->getBase(), AK); |
| 1867 | else |
| 1868 | checkAccess(ME->getBase(), AK); |
DeLesley Hutchins | 0cfa1a5 | 2012-12-08 03:46:30 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1871 | const ValueDecl *D = getValueDecl(Exp); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1872 | if (!D || !D->hasAttrs()) |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1873 | return; |
| 1874 | |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1875 | if (D->hasAttr<GuardedVarAttr>() && FSet.isEmpty()) |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1876 | Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK, |
| 1877 | Exp->getExprLoc()); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1878 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1879 | for (const auto *I : D->specific_attrs<GuardedByAttr>()) |
| 1880 | warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarAccess); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1883 | /// \brief Checks pt_guarded_by and pt_guarded_var attributes. |
| 1884 | void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1885 | while (true) { |
| 1886 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { |
| 1887 | Exp = PE->getSubExpr(); |
| 1888 | continue; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1889 | } |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1890 | if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) { |
| 1891 | if (CE->getCastKind() == CK_ArrayToPointerDecay) { |
| 1892 | // If it's an actual array, and not a pointer, then it's elements |
| 1893 | // are protected by GUARDED_BY, not PT_GUARDED_BY; |
| 1894 | checkAccess(CE->getSubExpr(), AK); |
| 1895 | return; |
| 1896 | } |
| 1897 | Exp = CE->getSubExpr(); |
| 1898 | continue; |
| 1899 | } |
| 1900 | break; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1901 | } |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1902 | |
| 1903 | const ValueDecl *D = getValueDecl(Exp); |
| 1904 | if (!D || !D->hasAttrs()) |
| 1905 | return; |
| 1906 | |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1907 | if (D->hasAttr<PtGuardedVarAttr>() && FSet.isEmpty()) |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1908 | Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK, |
| 1909 | Exp->getExprLoc()); |
| 1910 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1911 | for (auto const *I : D->specific_attrs<PtGuardedByAttr>()) |
| 1912 | warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarDereference); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1916 | /// \brief Process a function call, method call, constructor call, |
| 1917 | /// or destructor call. This involves looking at the attributes on the |
| 1918 | /// corresponding function/method/constructor/destructor, issuing warnings, |
| 1919 | /// and updating the locksets accordingly. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1920 | /// |
| 1921 | /// FIXME: For classes annotated with one of the guarded annotations, we need |
| 1922 | /// to treat const method calls as reads and non-const method calls as writes, |
| 1923 | /// and check that the appropriate locks are held. Non-const method calls with |
| 1924 | /// the same signature as const method calls can be also treated as reads. |
| 1925 | /// |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1926 | void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) { |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1927 | SourceLocation Loc = Exp->getExprLoc(); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1928 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 1929 | MutexIDList ExclusiveLocksToAdd; |
| 1930 | MutexIDList SharedLocksToAdd; |
| 1931 | MutexIDList LocksToRemove; |
| 1932 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1933 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1934 | Attr *At = const_cast<Attr*>(ArgAttrs[i]); |
| 1935 | switch (At->getKind()) { |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame^] | 1936 | // When we encounter a lock function, we need to add the lock to our |
| 1937 | // lockset. |
| 1938 | case attr::AcquireCapability: { |
| 1939 | auto *A = cast<AcquireCapabilityAttr>(At); |
| 1940 | Analyzer->getMutexIDs(A->isShared() ? SharedLocksToAdd |
| 1941 | : ExclusiveLocksToAdd, |
| 1942 | A, Exp, D, VD); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1943 | break; |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1944 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1945 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1946 | // An assert will add a lock to the lockset, but will not generate |
| 1947 | // a warning if it is already there, and will not generate a warning |
| 1948 | // if it is not removed. |
| 1949 | case attr::AssertExclusiveLock: { |
| 1950 | AssertExclusiveLockAttr *A = cast<AssertExclusiveLockAttr>(At); |
| 1951 | |
| 1952 | MutexIDList AssertLocks; |
| 1953 | Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); |
| 1954 | for (unsigned i=0,n=AssertLocks.size(); i<n; ++i) { |
| 1955 | Analyzer->addLock(FSet, AssertLocks[i], |
| 1956 | LockData(Loc, LK_Exclusive, false, true)); |
| 1957 | } |
| 1958 | break; |
| 1959 | } |
| 1960 | case attr::AssertSharedLock: { |
| 1961 | AssertSharedLockAttr *A = cast<AssertSharedLockAttr>(At); |
| 1962 | |
| 1963 | MutexIDList AssertLocks; |
| 1964 | Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); |
| 1965 | for (unsigned i=0,n=AssertLocks.size(); i<n; ++i) { |
| 1966 | Analyzer->addLock(FSet, AssertLocks[i], |
| 1967 | LockData(Loc, LK_Shared, false, true)); |
| 1968 | } |
| 1969 | break; |
| 1970 | } |
| 1971 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1972 | // When we encounter an unlock function, we need to remove unlocked |
| 1973 | // mutexes from the lockset, and flag a warning if they are not there. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame^] | 1974 | case attr::ReleaseCapability: { |
| 1975 | auto *A = cast<ReleaseCapabilityAttr>(At); |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1976 | Analyzer->getMutexIDs(LocksToRemove, A, Exp, D, VD); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1977 | break; |
| 1978 | } |
| 1979 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 1980 | case attr::RequiresCapability: { |
| 1981 | RequiresCapabilityAttr *A = cast<RequiresCapabilityAttr>(At); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1982 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 1983 | for (RequiresCapabilityAttr::args_iterator I = A->args_begin(), |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1984 | E = A->args_end(); I != E; ++I) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 1985 | warnIfMutexNotHeld(D, Exp, A->isShared() ? AK_Read : AK_Written, *I, |
| 1986 | POK_FunctionCall); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1987 | break; |
| 1988 | } |
| 1989 | |
| 1990 | case attr::LocksExcluded: { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1991 | LocksExcludedAttr *A = cast<LocksExcludedAttr>(At); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1992 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1993 | for (LocksExcludedAttr::args_iterator I = A->args_begin(), |
| 1994 | E = A->args_end(); I != E; ++I) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1995 | warnIfMutexHeld(D, Exp, *I); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1996 | } |
| 1997 | break; |
| 1998 | } |
| 1999 | |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2000 | // Ignore attributes unrelated to thread-safety |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2001 | default: |
| 2002 | break; |
| 2003 | } |
| 2004 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Figure out if we're calling the constructor of scoped lockable class |
| 2007 | bool isScopedVar = false; |
| 2008 | if (VD) { |
| 2009 | if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) { |
| 2010 | const CXXRecordDecl* PD = CD->getParent(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2011 | if (PD && PD->hasAttr<ScopedLockableAttr>()) |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2012 | isScopedVar = true; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | // Add locks. |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2017 | for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2018 | Analyzer->addLock(FSet, ExclusiveLocksToAdd[i], |
| 2019 | LockData(Loc, LK_Exclusive, isScopedVar)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2020 | } |
| 2021 | for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2022 | Analyzer->addLock(FSet, SharedLocksToAdd[i], |
| 2023 | LockData(Loc, LK_Shared, isScopedVar)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | // Add the managing object as a dummy mutex, mapped to the underlying mutex. |
| 2027 | // FIXME -- this doesn't work if we acquire multiple locks. |
| 2028 | if (isScopedVar) { |
| 2029 | SourceLocation MLoc = VD->getLocation(); |
| 2030 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation()); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2031 | SExpr SMutex(&DRE, 0, 0); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2032 | |
| 2033 | for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2034 | Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive, |
| 2035 | ExclusiveLocksToAdd[i])); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2036 | } |
| 2037 | for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2038 | Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared, |
| 2039 | SharedLocksToAdd[i])); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | // Remove locks. |
| 2044 | // FIXME -- should only fully remove if the attribute refers to 'this'. |
| 2045 | bool Dtor = isa<CXXDestructorDecl>(D); |
| 2046 | for (unsigned i=0,n=LocksToRemove.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2047 | Analyzer->removeLock(FSet, LocksToRemove[i], Loc, Dtor); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2048 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 2051 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2052 | /// \brief For unary operations which read and write a variable, we need to |
| 2053 | /// check whether we hold any required mutexes. Reads are checked in |
| 2054 | /// VisitCastExpr. |
| 2055 | void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { |
| 2056 | switch (UO->getOpcode()) { |
| 2057 | case clang::UO_PostDec: |
| 2058 | case clang::UO_PostInc: |
| 2059 | case clang::UO_PreDec: |
| 2060 | case clang::UO_PreInc: { |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 2061 | checkAccess(UO->getSubExpr(), AK_Written); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2062 | break; |
| 2063 | } |
| 2064 | default: |
| 2065 | break; |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | /// For binary operations which assign to a variable (writes), we need to check |
| 2070 | /// whether we hold any required mutexes. |
| 2071 | /// FIXME: Deal with non-primitive types. |
| 2072 | void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { |
| 2073 | if (!BO->isAssignmentOp()) |
| 2074 | return; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2075 | |
| 2076 | // adjust the context |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 2077 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2078 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 2079 | checkAccess(BO->getLHS(), AK_Written); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 2082 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2083 | /// Whenever we do an LValue to Rvalue cast, we are reading a variable and |
| 2084 | /// need to ensure we hold any required mutexes. |
| 2085 | /// FIXME: Deal with non-primitive types. |
| 2086 | void BuildLockset::VisitCastExpr(CastExpr *CE) { |
| 2087 | if (CE->getCastKind() != CK_LValueToRValue) |
| 2088 | return; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 2089 | checkAccess(CE->getSubExpr(), AK_Read); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | |
DeLesley Hutchins | 714296c | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 2093 | void BuildLockset::VisitCallExpr(CallExpr *Exp) { |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2094 | if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Exp)) { |
| 2095 | MemberExpr *ME = dyn_cast<MemberExpr>(CE->getCallee()); |
| 2096 | // ME can be null when calling a method pointer |
| 2097 | CXXMethodDecl *MD = CE->getMethodDecl(); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 2098 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2099 | if (ME && MD) { |
| 2100 | if (ME->isArrow()) { |
| 2101 | if (MD->isConst()) { |
| 2102 | checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); |
| 2103 | } else { // FIXME -- should be AK_Written |
| 2104 | checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 2105 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2106 | } else { |
| 2107 | if (MD->isConst()) |
| 2108 | checkAccess(CE->getImplicitObjectArgument(), AK_Read); |
| 2109 | else // FIXME -- should be AK_Written |
| 2110 | checkAccess(CE->getImplicitObjectArgument(), AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 2111 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } else if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) { |
| 2114 | switch (OE->getOperator()) { |
| 2115 | case OO_Equal: { |
| 2116 | const Expr *Target = OE->getArg(0); |
| 2117 | const Expr *Source = OE->getArg(1); |
| 2118 | checkAccess(Target, AK_Written); |
| 2119 | checkAccess(Source, AK_Read); |
| 2120 | break; |
| 2121 | } |
DeLesley Hutchins | 5ede5cc | 2013-11-05 23:09:56 +0000 | [diff] [blame] | 2122 | case OO_Star: |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 2123 | case OO_Arrow: |
| 2124 | case OO_Subscript: { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 2125 | const Expr *Obj = OE->getArg(0); |
| 2126 | checkAccess(Obj, AK_Read); |
| 2127 | checkPtAccess(Obj, AK_Read); |
DeLesley Hutchins | 5ede5cc | 2013-11-05 23:09:56 +0000 | [diff] [blame] | 2128 | break; |
| 2129 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2130 | default: { |
DeLesley Hutchins | 05b7b37 | 2013-11-06 18:40:01 +0000 | [diff] [blame] | 2131 | const Expr *Obj = OE->getArg(0); |
| 2132 | checkAccess(Obj, AK_Read); |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2133 | break; |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | } |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2137 | NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 2138 | if(!D || !D->hasAttrs()) |
| 2139 | return; |
| 2140 | handleCall(Exp, D); |
| 2141 | } |
| 2142 | |
| 2143 | void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) { |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 2144 | const CXXConstructorDecl *D = Exp->getConstructor(); |
| 2145 | if (D && D->isCopyConstructor()) { |
| 2146 | const Expr* Source = Exp->getArg(0); |
| 2147 | checkAccess(Source, AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 2148 | } |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 2149 | // FIXME -- only handles constructors in DeclStmt below. |
| 2150 | } |
| 2151 | |
| 2152 | void BuildLockset::VisitDeclStmt(DeclStmt *S) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2153 | // adjust the context |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 2154 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2155 | |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 2156 | DeclGroupRef DGrp = S->getDeclGroup(); |
| 2157 | for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) { |
| 2158 | Decl *D = *I; |
| 2159 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) { |
| 2160 | Expr *E = VD->getInit(); |
DeLesley Hutchins | 0c1da20 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 2161 | // handle constructors that involve temporaries |
| 2162 | if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E)) |
| 2163 | E = EWC->getSubExpr(); |
| 2164 | |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 2165 | if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) { |
| 2166 | NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor()); |
| 2167 | if (!CtorD || !CtorD->hasAttrs()) |
| 2168 | return; |
| 2169 | handleCall(CE, CtorD, VD); |
| 2170 | } |
| 2171 | } |
| 2172 | } |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2175 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2176 | |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 2177 | /// \brief Compute the intersection of two locksets and issue warnings for any |
| 2178 | /// locks in the symmetric difference. |
| 2179 | /// |
| 2180 | /// This function is used at a merge point in the CFG when comparing the lockset |
| 2181 | /// of each branch being merged. For example, given the following sequence: |
| 2182 | /// A; if () then B; else C; D; we need to check that the lockset after B and C |
| 2183 | /// are the same. In the event of a difference, we use the intersection of these |
| 2184 | /// two locksets at the start of D. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2185 | /// |
Ted Kremenek | 78094ca | 2012-08-22 23:50:41 +0000 | [diff] [blame] | 2186 | /// \param FSet1 The first lockset. |
| 2187 | /// \param FSet2 The second lockset. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2188 | /// \param JoinLoc The location of the join point for error reporting |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2189 | /// \param LEK1 The error message to report if a mutex is missing from LSet1 |
| 2190 | /// \param LEK2 The error message to report if a mutex is missing from Lset2 |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2191 | void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1, |
| 2192 | const FactSet &FSet2, |
| 2193 | SourceLocation JoinLoc, |
| 2194 | LockErrorKind LEK1, |
| 2195 | LockErrorKind LEK2, |
| 2196 | bool Modify) { |
| 2197 | FactSet FSet1Orig = FSet1; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2198 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2199 | // Find locks in FSet2 that conflict or are not in FSet1, and warn. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2200 | for (FactSet::const_iterator I = FSet2.begin(), E = FSet2.end(); |
| 2201 | I != E; ++I) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2202 | const SExpr &FSet2Mutex = FactMan[*I].MutID; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2203 | const LockData &LDat2 = FactMan[*I].LDat; |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2204 | FactSet::iterator I1 = FSet1.findLockIter(FactMan, FSet2Mutex); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2205 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2206 | if (I1 != FSet1.end()) { |
| 2207 | const LockData* LDat1 = &FactMan[*I1].LDat; |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2208 | if (LDat1->LKind != LDat2.LKind) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2209 | Handler.handleExclusiveAndShared(FSet2Mutex.toString(), |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2210 | LDat2.AcquireLoc, |
| 2211 | LDat1->AcquireLoc); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2212 | if (Modify && LDat1->LKind != LK_Exclusive) { |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2213 | // Take the exclusive lock, which is the one in FSet2. |
| 2214 | *I1 = *I; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2215 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2216 | } |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2217 | else if (LDat1->Asserted && !LDat2.Asserted) { |
| 2218 | // The non-asserted lock in FSet2 is the one we want to track. |
| 2219 | *I1 = *I; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 2220 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2221 | } else { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2222 | if (LDat2.UnderlyingMutex.isValid()) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2223 | if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2224 | // If this is a scoped lock that manages another mutex, and if the |
| 2225 | // underlying mutex is still held, then warn about the underlying |
| 2226 | // mutex. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2227 | Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.toString(), |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2228 | LDat2.AcquireLoc, |
| 2229 | JoinLoc, LEK1); |
| 2230 | } |
| 2231 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 2232 | else if (!LDat2.Managed && !FSet2Mutex.isUniversal() && !LDat2.Asserted) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2233 | Handler.handleMutexHeldEndOfScope(FSet2Mutex.toString(), |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2234 | LDat2.AcquireLoc, |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2235 | JoinLoc, LEK1); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2238 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 2239 | // Find locks in FSet1 that are not in FSet2, and remove them. |
| 2240 | for (FactSet::const_iterator I = FSet1Orig.begin(), E = FSet1Orig.end(); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2241 | I != E; ++I) { |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2242 | const SExpr &FSet1Mutex = FactMan[*I].MutID; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2243 | const LockData &LDat1 = FactMan[*I].LDat; |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 2244 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2245 | if (!FSet2.findLock(FactMan, FSet1Mutex)) { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2246 | if (LDat1.UnderlyingMutex.isValid()) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2247 | if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2248 | // If this is a scoped lock that manages another mutex, and if the |
| 2249 | // underlying mutex is still held, then warn about the underlying |
| 2250 | // mutex. |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2251 | Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.toString(), |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2252 | LDat1.AcquireLoc, |
| 2253 | JoinLoc, LEK1); |
| 2254 | } |
| 2255 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 2256 | else if (!LDat1.Managed && !FSet1Mutex.isUniversal() && !LDat1.Asserted) |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 2257 | Handler.handleMutexHeldEndOfScope(FSet1Mutex.toString(), |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 2258 | LDat1.AcquireLoc, |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2259 | JoinLoc, LEK2); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2260 | if (Modify) |
| 2261 | FSet1.removeLock(FactMan, FSet1Mutex); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 2266 | |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 2267 | // Return true if block B never continues to its successors. |
| 2268 | inline bool neverReturns(const CFGBlock* B) { |
| 2269 | if (B->hasNoReturnElement()) |
| 2270 | return true; |
| 2271 | if (B->empty()) |
| 2272 | return false; |
| 2273 | |
| 2274 | CFGElement Last = B->back(); |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 2275 | if (Optional<CFGStmt> S = Last.getAs<CFGStmt>()) { |
| 2276 | if (isa<CXXThrowExpr>(S->getStmt())) |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 2277 | return true; |
| 2278 | } |
| 2279 | return false; |
| 2280 | } |
| 2281 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2282 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2283 | /// \brief Check a function's CFG for thread-safety violations. |
| 2284 | /// |
| 2285 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 2286 | /// at the end of each block, and issue warnings for thread safety violations. |
| 2287 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 2288 | void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2289 | CFG *CFGraph = AC.getCFG(); |
| 2290 | if (!CFGraph) return; |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 2291 | const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl()); |
| 2292 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2293 | // AC.dumpCFG(true); |
| 2294 | |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 2295 | if (!D) |
| 2296 | return; // Ignore anonymous functions for now. |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2297 | if (D->hasAttr<NoThreadSafetyAnalysisAttr>()) |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 2298 | return; |
DeLesley Hutchins | c2286f6 | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 2299 | // FIXME: Do something a bit more intelligent inside constructor and |
| 2300 | // destructor code. Constructors and destructors must assume unique access |
| 2301 | // to 'this', so checks on member variable access is disabled, but we should |
| 2302 | // still enable checks on other objects. |
| 2303 | if (isa<CXXConstructorDecl>(D)) |
| 2304 | return; // Don't check inside constructors. |
| 2305 | if (isa<CXXDestructorDecl>(D)) |
| 2306 | return; // Don't check inside destructors. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2307 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 2308 | BlockInfo.resize(CFGraph->getNumBlockIDs(), |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2309 | CFGBlockInfo::getEmptyBlockInfo(LocalVarMap)); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2310 | |
| 2311 | // We need to explore the CFG via a "topological" ordering. |
| 2312 | // That way, we will be guaranteed to have information about required |
| 2313 | // predecessor locksets when exploring a new block. |
Ted Kremenek | 4b4c51c | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 2314 | PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>(); |
| 2315 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2316 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2317 | // Mark entry block as reachable |
| 2318 | BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true; |
| 2319 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2320 | // Compute SSA names for local variables |
| 2321 | LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo); |
| 2322 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 2323 | // Fill in source locations for all CFGBlocks. |
| 2324 | findBlockLocations(CFGraph, SortedGraph, BlockInfo); |
| 2325 | |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2326 | MutexIDList ExclusiveLocksAcquired; |
| 2327 | MutexIDList SharedLocksAcquired; |
| 2328 | MutexIDList LocksReleased; |
| 2329 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2330 | // Add locks from exclusive_locks_required and shared_locks_required |
DeLesley Hutchins | c2286f6 | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 2331 | // to initial lockset. Also turn off checking for lock and unlock functions. |
| 2332 | // FIXME: is there a more intelligent way to check lock/unlock functions? |
Ted Kremenek | 4b4c51c | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 2333 | if (!SortedGraph->empty() && D->hasAttrs()) { |
| 2334 | const CFGBlock *FirstBlock = *SortedGraph->begin(); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2335 | FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet; |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 2336 | const AttrVec &ArgAttrs = D->getAttrs(); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2337 | |
| 2338 | MutexIDList ExclusiveLocksToAdd; |
| 2339 | MutexIDList SharedLocksToAdd; |
| 2340 | |
| 2341 | SourceLocation Loc = D->getLocation(); |
DeLesley Hutchins | c2286f6 | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 2342 | for (unsigned i = 0; i < ArgAttrs.size(); ++i) { |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 2343 | Attr *Attr = ArgAttrs[i]; |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2344 | Loc = Attr->getLocation(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 2345 | if (RequiresCapabilityAttr *A = dyn_cast<RequiresCapabilityAttr>(Attr)) { |
| 2346 | getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A, |
| 2347 | 0, D); |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame^] | 2348 | } else if (auto *A = dyn_cast<ReleaseCapabilityAttr>(Attr)) { |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2349 | // UNLOCK_FUNCTION() is used to hide the underlying lock implementation. |
| 2350 | // We must ignore such methods. |
| 2351 | if (A->args_size() == 0) |
| 2352 | return; |
| 2353 | // FIXME -- deal with exclusive vs. shared unlock functions? |
| 2354 | getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D); |
| 2355 | getMutexIDs(LocksReleased, A, (Expr*) 0, D); |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame^] | 2356 | } else if (auto *A = dyn_cast<AcquireCapabilityAttr>(Attr)) { |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2357 | if (A->args_size() == 0) |
| 2358 | return; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame^] | 2359 | getMutexIDs(A->isShared() ? SharedLocksAcquired |
| 2360 | : ExclusiveLocksAcquired, |
| 2361 | A, nullptr, D); |
DeLesley Hutchins | c4a6e51 | 2012-07-02 21:59:24 +0000 | [diff] [blame] | 2362 | } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) { |
| 2363 | // Don't try to check trylock functions for now |
| 2364 | return; |
| 2365 | } else if (isa<SharedTrylockFunctionAttr>(Attr)) { |
| 2366 | // Don't try to check trylock functions for now |
| 2367 | return; |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2370 | |
| 2371 | // FIXME -- Loc can be wrong here. |
| 2372 | for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2373 | addLock(InitialLockset, ExclusiveLocksToAdd[i], |
| 2374 | LockData(Loc, LK_Exclusive)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2375 | } |
| 2376 | for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2377 | addLock(InitialLockset, SharedLocksToAdd[i], |
| 2378 | LockData(Loc, LK_Shared)); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 2379 | } |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Ted Kremenek | 4b4c51c | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 2382 | for (PostOrderCFGView::iterator I = SortedGraph->begin(), |
| 2383 | E = SortedGraph->end(); I!= E; ++I) { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2384 | const CFGBlock *CurrBlock = *I; |
| 2385 | int CurrBlockID = CurrBlock->getBlockID(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2386 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Use the default initial lockset in case there are no predecessors. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2389 | VisitedBlocks.insert(CurrBlock); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2390 | |
| 2391 | // Iterate through the predecessor blocks and warn if the lockset for all |
| 2392 | // predecessors is not the same. We take the entry lockset of the current |
| 2393 | // block to be the intersection of all previous locksets. |
| 2394 | // FIXME: By keeping the intersection, we may output more errors in future |
| 2395 | // for a lock which is not in the intersection, but was in the union. We |
| 2396 | // may want to also keep the union in future. As an example, let's say |
| 2397 | // the intersection contains Mutex L, and the union contains L and M. |
| 2398 | // Later we unlock M. At this point, we would output an error because we |
| 2399 | // never locked M; although the real error is probably that we forgot to |
| 2400 | // lock M on all code paths. Conversely, let's say that later we lock M. |
| 2401 | // In this case, we should compare against the intersection instead of the |
| 2402 | // union because the real error is probably that we forgot to unlock M on |
| 2403 | // all code paths. |
| 2404 | bool LocksetInitialized = false; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2405 | SmallVector<CFGBlock *, 8> SpecialBlocks; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2406 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 2407 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 2408 | |
| 2409 | // if *PI -> CurrBlock is a back edge |
| 2410 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) |
| 2411 | continue; |
| 2412 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2413 | int PrevBlockID = (*PI)->getBlockID(); |
| 2414 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 2415 | |
DeLesley Hutchins | a2587ef | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 2416 | // Ignore edges from blocks that can't return. |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 2417 | if (neverReturns(*PI) || !PrevBlockInfo->Reachable) |
DeLesley Hutchins | a2587ef | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 2418 | continue; |
| 2419 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2420 | // Okay, we can reach this block from the entry. |
| 2421 | CurrBlockInfo->Reachable = true; |
| 2422 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2423 | // If the previous block ended in a 'continue' or 'break' statement, then |
| 2424 | // a difference in locksets is probably due to a bug in that block, rather |
| 2425 | // than in some other predecessor. In that case, keep the other |
| 2426 | // predecessor's lockset. |
| 2427 | if (const Stmt *Terminator = (*PI)->getTerminator()) { |
| 2428 | if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) { |
| 2429 | SpecialBlocks.push_back(*PI); |
| 2430 | continue; |
| 2431 | } |
| 2432 | } |
| 2433 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2434 | FactSet PrevLockset; |
| 2435 | getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2436 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2437 | if (!LocksetInitialized) { |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2438 | CurrBlockInfo->EntrySet = PrevLockset; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2439 | LocksetInitialized = true; |
| 2440 | } else { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2441 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 2442 | CurrBlockInfo->EntryLoc, |
| 2443 | LEK_LockedSomePredecessors); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2447 | // Skip rest of block if it's not reachable. |
| 2448 | if (!CurrBlockInfo->Reachable) |
| 2449 | continue; |
| 2450 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2451 | // Process continue and break blocks. Assume that the lockset for the |
| 2452 | // resulting block is unaffected by any discrepancies in them. |
| 2453 | for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size(); |
| 2454 | SpecialI < SpecialN; ++SpecialI) { |
| 2455 | CFGBlock *PrevBlock = SpecialBlocks[SpecialI]; |
| 2456 | int PrevBlockID = PrevBlock->getBlockID(); |
| 2457 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 2458 | |
| 2459 | if (!LocksetInitialized) { |
| 2460 | CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet; |
| 2461 | LocksetInitialized = true; |
| 2462 | } else { |
| 2463 | // Determine whether this edge is a loop terminator for diagnostic |
| 2464 | // purposes. FIXME: A 'break' statement might be a loop terminator, but |
| 2465 | // it might also be part of a switch. Also, a subsequent destructor |
| 2466 | // might add to the lockset, in which case the real issue might be a |
| 2467 | // double lock on the other path. |
| 2468 | const Stmt *Terminator = PrevBlock->getTerminator(); |
| 2469 | bool IsLoop = Terminator && isa<ContinueStmt>(Terminator); |
| 2470 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2471 | FactSet PrevLockset; |
| 2472 | getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, |
| 2473 | PrevBlock, CurrBlock); |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2474 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2475 | // Do not update EntrySet. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2476 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 2477 | PrevBlockInfo->ExitLoc, |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2478 | IsLoop ? LEK_LockedSomeLoopIterations |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2479 | : LEK_LockedSomePredecessors, |
| 2480 | false); |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 2484 | BuildLockset LocksetBuilder(this, *CurrBlockInfo); |
| 2485 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2486 | // Visit all the statements in the basic block. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2487 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 2488 | BE = CurrBlock->end(); BI != BE; ++BI) { |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2489 | switch (BI->getKind()) { |
| 2490 | case CFGElement::Statement: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2491 | CFGStmt CS = BI->castAs<CFGStmt>(); |
| 2492 | LocksetBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2493 | break; |
| 2494 | } |
| 2495 | // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now. |
| 2496 | case CFGElement::AutomaticObjectDtor: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2497 | CFGAutomaticObjDtor AD = BI->castAs<CFGAutomaticObjDtor>(); |
| 2498 | CXXDestructorDecl *DD = const_cast<CXXDestructorDecl *>( |
| 2499 | AD.getDestructorDecl(AC.getASTContext())); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2500 | if (!DD->hasAttrs()) |
| 2501 | break; |
| 2502 | |
| 2503 | // Create a dummy expression, |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2504 | VarDecl *VD = const_cast<VarDecl*>(AD.getVarDecl()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2505 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2506 | AD.getTriggerStmt()->getLocEnd()); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2507 | LocksetBuilder.handleCall(&DRE, DD); |
| 2508 | break; |
| 2509 | } |
| 2510 | default: |
| 2511 | break; |
| 2512 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2513 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2514 | CurrBlockInfo->ExitSet = LocksetBuilder.FSet; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2515 | |
| 2516 | // For every back edge from CurrBlock (the end of the loop) to another block |
| 2517 | // (FirstLoopBlock) we need to check that the Lockset of Block is equal to |
| 2518 | // the one held at the beginning of FirstLoopBlock. We can look up the |
| 2519 | // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. |
| 2520 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 2521 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 2522 | |
| 2523 | // if CurrBlock -> *SI is *not* a back edge |
| 2524 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
| 2525 | continue; |
| 2526 | |
| 2527 | CFGBlock *FirstLoopBlock = *SI; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2528 | CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()]; |
| 2529 | CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID]; |
| 2530 | intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet, |
| 2531 | PreLoop->EntryLoc, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2532 | LEK_LockedSomeLoopIterations, |
| 2533 | false); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2537 | CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()]; |
| 2538 | CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()]; |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 2539 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2540 | // Skip the final check if the exit block is unreachable. |
| 2541 | if (!Final->Reachable) |
| 2542 | return; |
| 2543 | |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2544 | // By default, we expect all locks held on entry to be held on exit. |
| 2545 | FactSet ExpectedExitSet = Initial->EntrySet; |
| 2546 | |
| 2547 | // Adjust the expected exit set by adding or removing locks, as declared |
| 2548 | // by *-LOCK_FUNCTION and UNLOCK_FUNCTION. The intersect below will then |
| 2549 | // issue the appropriate warning. |
| 2550 | // FIXME: the location here is not quite right. |
| 2551 | for (unsigned i=0,n=ExclusiveLocksAcquired.size(); i<n; ++i) { |
| 2552 | ExpectedExitSet.addLock(FactMan, ExclusiveLocksAcquired[i], |
| 2553 | LockData(D->getLocation(), LK_Exclusive)); |
| 2554 | } |
| 2555 | for (unsigned i=0,n=SharedLocksAcquired.size(); i<n; ++i) { |
| 2556 | ExpectedExitSet.addLock(FactMan, SharedLocksAcquired[i], |
| 2557 | LockData(D->getLocation(), LK_Shared)); |
| 2558 | } |
| 2559 | for (unsigned i=0,n=LocksReleased.size(); i<n; ++i) { |
| 2560 | ExpectedExitSet.removeLock(FactMan, LocksReleased[i]); |
| 2561 | } |
| 2562 | |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 2563 | // FIXME: Should we call this function for all blocks which exit the function? |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2564 | intersectAndWarn(ExpectedExitSet, Final->ExitSet, |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2565 | Final->ExitLoc, |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2566 | LEK_LockedAtEndOfFunction, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2567 | LEK_NotLockedAtEndOfFunction, |
| 2568 | false); |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | } // end anonymous namespace |
| 2572 | |
| 2573 | |
| 2574 | namespace clang { |
| 2575 | namespace thread_safety { |
| 2576 | |
| 2577 | /// \brief Check a function's CFG for thread-safety violations. |
| 2578 | /// |
| 2579 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 2580 | /// at the end of each block, and issue warnings for thread safety violations. |
| 2581 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 2582 | void runThreadSafetyAnalysis(AnalysisDeclContext &AC, |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2583 | ThreadSafetyHandler &Handler) { |
| 2584 | ThreadSafetyAnalyzer Analyzer(Handler); |
| 2585 | Analyzer.runAnalysis(AC); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /// \brief Helper function that returns a LockKind required for the given level |
| 2589 | /// of access. |
| 2590 | LockKind getLockKindFromAccessKind(AccessKind AK) { |
| 2591 | switch (AK) { |
| 2592 | case AK_Read : |
| 2593 | return LK_Shared; |
| 2594 | case AK_Written : |
| 2595 | return LK_Exclusive; |
| 2596 | } |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 2597 | llvm_unreachable("Unknown AccessKind"); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2598 | } |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2599 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2600 | }} // end namespace clang::thread_safety |