Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1 | //===- ThreadSafety.cpp ----------------------------------------*- C++ --*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // A intra-procedural analysis for thread safety (e.g. deadlocks and race |
| 11 | // conditions), based off of an annotation system. |
| 12 | // |
Caitlin Sadowski | 1990346 | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 13 | // See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more |
| 14 | // information. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
Caitlin Sadowski | d5b1605 | 2011-09-09 23:00:59 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/AnalysisContext.h" |
| 20 | #include "clang/Analysis/CFG.h" |
| 21 | #include "clang/Analysis/CFGStmtMap.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
| 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
Caitlin Sadowski | d5b1605 | 2011-09-09 23:00:59 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
| 27 | #include "clang/Basic/SourceLocation.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/BitVector.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
| 31 | #include "llvm/ADT/PostOrderIterator.h" |
| 32 | #include "llvm/ADT/SmallVector.h" |
| 33 | #include "llvm/ADT/StringRef.h" |
| 34 | #include <algorithm> |
| 35 | #include <vector> |
| 36 | |
| 37 | using namespace clang; |
| 38 | using namespace thread_safety; |
| 39 | |
Caitlin Sadowski | 1990346 | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 40 | // Key method definition |
| 41 | ThreadSafetyHandler::~ThreadSafetyHandler() {} |
| 42 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 43 | namespace { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 44 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 45 | /// \brief Implements a set of CFGBlocks using a BitVector. |
| 46 | /// |
| 47 | /// This class contains a minimal interface, primarily dictated by the SetType |
| 48 | /// template parameter of the llvm::po_iterator template, as used with external |
| 49 | /// storage. We also use this set to keep track of which CFGBlocks we visit |
| 50 | /// during the analysis. |
| 51 | class CFGBlockSet { |
| 52 | llvm::BitVector VisitedBlockIDs; |
| 53 | |
| 54 | public: |
| 55 | // po_iterator requires this iterator, but the only interface needed is the |
| 56 | // value_type typedef. |
| 57 | struct iterator { |
| 58 | typedef const CFGBlock *value_type; |
| 59 | }; |
| 60 | |
| 61 | CFGBlockSet() {} |
| 62 | CFGBlockSet(const CFG *G) : VisitedBlockIDs(G->getNumBlockIDs(), false) {} |
| 63 | |
| 64 | /// \brief Set the bit associated with a particular CFGBlock. |
| 65 | /// This is the important method for the SetType template parameter. |
| 66 | bool insert(const CFGBlock *Block) { |
| 67 | // Note that insert() is called by po_iterator, which doesn't check to make |
| 68 | // sure that Block is non-null. Moreover, the CFGBlock iterator will |
| 69 | // occasionally hand out null pointers for pruned edges, so we catch those |
| 70 | // here. |
| 71 | if (Block == 0) |
| 72 | return false; // if an edge is trivially false. |
| 73 | if (VisitedBlockIDs.test(Block->getBlockID())) |
| 74 | return false; |
| 75 | VisitedBlockIDs.set(Block->getBlockID()); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /// \brief Check if the bit for a CFGBlock has been already set. |
| 80 | /// This method is for tracking visited blocks in the main threadsafety loop. |
| 81 | /// Block must not be null. |
| 82 | bool alreadySet(const CFGBlock *Block) { |
| 83 | return VisitedBlockIDs.test(Block->getBlockID()); |
| 84 | } |
| 85 | }; |
| 86 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 87 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 88 | /// \brief We create a helper class which we use to iterate through CFGBlocks in |
| 89 | /// the topological order. |
| 90 | class TopologicallySortedCFG { |
| 91 | typedef llvm::po_iterator<const CFG*, CFGBlockSet, true> po_iterator; |
| 92 | |
| 93 | std::vector<const CFGBlock*> Blocks; |
| 94 | |
| 95 | public: |
| 96 | typedef std::vector<const CFGBlock*>::reverse_iterator iterator; |
| 97 | |
| 98 | TopologicallySortedCFG(const CFG *CFGraph) { |
| 99 | Blocks.reserve(CFGraph->getNumBlockIDs()); |
| 100 | CFGBlockSet BSet(CFGraph); |
| 101 | |
| 102 | for (po_iterator I = po_iterator::begin(CFGraph, BSet), |
| 103 | E = po_iterator::end(CFGraph, BSet); I != E; ++I) { |
| 104 | Blocks.push_back(*I); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | iterator begin() { |
| 109 | return Blocks.rbegin(); |
| 110 | } |
| 111 | |
| 112 | iterator end() { |
| 113 | return Blocks.rend(); |
| 114 | } |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 115 | |
| 116 | bool empty() { |
| 117 | return begin() == end(); |
| 118 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 121 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 122 | /// \brief A MutexID object uniquely identifies a particular mutex, and |
| 123 | /// is built from an Expr* (i.e. calling a lock function). |
| 124 | /// |
| 125 | /// Thread-safety analysis works by comparing lock expressions. Within the |
| 126 | /// body of a function, an expression such as "x->foo->bar.mu" will resolve to |
| 127 | /// a particular mutex object at run-time. Subsequent occurrences of the same |
| 128 | /// expression (where "same" means syntactic equality) will refer to the same |
| 129 | /// run-time object if three conditions hold: |
| 130 | /// (1) Local variables in the expression, such as "x" have not changed. |
| 131 | /// (2) Values on the heap that affect the expression have not changed. |
| 132 | /// (3) The expression involves only pure function calls. |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 133 | /// |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 134 | /// The current implementation assumes, but does not verify, that multiple uses |
| 135 | /// of the same lock expression satisfies these criteria. |
| 136 | /// |
| 137 | /// Clang introduces an additional wrinkle, which is that it is difficult to |
| 138 | /// derive canonical expressions, or compare expressions directly for equality. |
| 139 | /// Thus, we identify a mutex not by an Expr, but by the set of named |
| 140 | /// declarations that are referenced by the Expr. In other words, |
| 141 | /// x->foo->bar.mu will be a four element vector with the Decls for |
| 142 | /// mu, bar, and foo, and x. The vector will uniquely identify the expression |
| 143 | /// for all practical purposes. |
| 144 | /// |
| 145 | /// Note we will need to perform substitution on "this" and function parameter |
| 146 | /// names when constructing a lock expression. |
| 147 | /// |
| 148 | /// For example: |
| 149 | /// class C { Mutex Mu; void lock() EXCLUSIVE_LOCK_FUNCTION(this->Mu); }; |
| 150 | /// void myFunc(C *X) { ... X->lock() ... } |
| 151 | /// The original expression for the mutex acquired by myFunc is "this->Mu", but |
| 152 | /// "X" is substituted for "this" so we get X->Mu(); |
| 153 | /// |
| 154 | /// For another example: |
| 155 | /// foo(MyList *L) EXCLUSIVE_LOCKS_REQUIRED(L->Mu) { ... } |
| 156 | /// MyList *MyL; |
| 157 | /// foo(MyL); // requires lock MyL->Mu to be held |
| 158 | class MutexID { |
| 159 | SmallVector<NamedDecl*, 2> DeclSeq; |
| 160 | |
| 161 | /// Build a Decl sequence representing the lock from the given expression. |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 162 | /// Recursive function that terminates on DeclRefExpr. |
| 163 | /// Note: this function merely creates a MutexID; it does not check to |
| 164 | /// ensure that the original expression is a valid mutex expression. |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 165 | void buildMutexID(Expr *Exp, Expr *Parent, int NumArgs, |
| 166 | const NamedDecl **FunArgDecls, Expr **FunArgs) { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 167 | if (!Exp) { |
| 168 | DeclSeq.clear(); |
| 169 | return; |
| 170 | } |
| 171 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 172 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) { |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 173 | if (FunArgDecls) { |
| 174 | // Substitute call arguments for references to function parameters |
| 175 | for (int i = 0; i < NumArgs; ++i) { |
| 176 | if (DRE->getDecl() == FunArgDecls[i]) { |
| 177 | buildMutexID(FunArgs[i], 0, 0, 0, 0); |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 182 | NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 183 | DeclSeq.push_back(ND); |
| 184 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 185 | NamedDecl *ND = ME->getMemberDecl(); |
| 186 | DeclSeq.push_back(ND); |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 187 | buildMutexID(ME->getBase(), Parent, NumArgs, FunArgDecls, FunArgs); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 188 | } else if (isa<CXXThisExpr>(Exp)) { |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 189 | if (Parent) |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 190 | buildMutexID(Parent, 0, 0, 0, 0); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 191 | else |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 192 | return; // mutexID is still valid in this case |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 193 | } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) |
| 194 | buildMutexID(UOE->getSubExpr(), Parent, NumArgs, FunArgDecls, FunArgs); |
| 195 | else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 196 | buildMutexID(CE->getSubExpr(), Parent, NumArgs, FunArgDecls, FunArgs); |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 197 | else |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 198 | DeclSeq.clear(); // Mark as invalid lock expression. |
| 199 | } |
| 200 | |
| 201 | /// \brief Construct a MutexID from an expression. |
| 202 | /// \param MutexExp The original mutex expression within an attribute |
| 203 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 204 | /// occurs. |
| 205 | /// \param D The declaration to which the lock/unlock attribute is attached. |
| 206 | void buildMutexIDFromExp(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D) { |
| 207 | Expr *Parent = 0; |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 208 | unsigned NumArgs = 0; |
| 209 | Expr **FunArgs = 0; |
| 210 | SmallVector<const NamedDecl*, 8> FunArgDecls; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 211 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 212 | // If we are processing a raw attribute expression, with no substitutions. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 213 | if (DeclExp == 0) { |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 214 | buildMutexID(MutexExp, 0, 0, 0, 0); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 218 | // Examine DeclExp to find Parent and FunArgs, which are used to substitute |
| 219 | // for formal parameters when we call buildMutexID later. |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 220 | if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) { |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 221 | Parent = ME->getBase(); |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 222 | } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) { |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 223 | Parent = CE->getImplicitObjectArgument(); |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 224 | NumArgs = CE->getNumArgs(); |
| 225 | FunArgs = CE->getArgs(); |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 226 | } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) { |
| 227 | Parent = 0; // FIXME -- get the parent from DeclStmt |
| 228 | NumArgs = CE->getNumArgs(); |
| 229 | FunArgs = CE->getArgs(); |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame^] | 230 | } else if (D && isa<CXXDestructorDecl>(D)) { |
| 231 | // There's no such thing as a "destructor call" in the AST. |
| 232 | Parent = DeclExp; |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 233 | } |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 234 | |
| 235 | // If the attribute has no arguments, then assume the argument is "this". |
| 236 | if (MutexExp == 0) { |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 237 | buildMutexID(Parent, 0, 0, 0, 0); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
DeLesley Hutchins | 8121639 | 2011-10-17 21:38:02 +0000 | [diff] [blame] | 240 | |
| 241 | // FIXME: handle default arguments |
| 242 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 243 | for (unsigned i = 0, ni = FD->getNumParams(); i < ni && i < NumArgs; ++i) { |
| 244 | FunArgDecls.push_back(FD->getParamDecl(i)); |
| 245 | } |
| 246 | } |
| 247 | buildMutexID(MutexExp, Parent, NumArgs, &FunArgDecls.front(), FunArgs); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public: |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 251 | /// \param MutexExp The original mutex expression within an attribute |
| 252 | /// \param DeclExp An expression involving the Decl on which the attribute |
| 253 | /// occurs. |
| 254 | /// \param D The declaration to which the lock/unlock attribute is attached. |
| 255 | /// Caller must check isValid() after construction. |
| 256 | MutexID(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D) { |
| 257 | buildMutexIDFromExp(MutexExp, DeclExp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 260 | /// Return true if this is a valid decl sequence. |
| 261 | /// Caller must call this by hand after construction to handle errors. |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 262 | bool isValid() const { |
| 263 | return !DeclSeq.empty(); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 264 | } |
| 265 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 266 | /// Issue a warning about an invalid lock expression |
| 267 | static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp, |
| 268 | Expr *DeclExp, const NamedDecl* D) { |
| 269 | SourceLocation Loc; |
| 270 | if (DeclExp) |
| 271 | Loc = DeclExp->getExprLoc(); |
| 272 | |
| 273 | // FIXME: add a note about the attribute location in MutexExp or D |
| 274 | if (Loc.isValid()) |
| 275 | Handler.handleInvalidLockExp(Loc); |
| 276 | } |
| 277 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 278 | bool operator==(const MutexID &other) const { |
| 279 | return DeclSeq == other.DeclSeq; |
| 280 | } |
| 281 | |
| 282 | bool operator!=(const MutexID &other) const { |
| 283 | return !(*this == other); |
| 284 | } |
| 285 | |
| 286 | // SmallVector overloads Operator< to do lexicographic ordering. Note that |
| 287 | // we use pointer equality (and <) to compare NamedDecls. This means the order |
| 288 | // of MutexIDs in a lockset is nondeterministic. In order to output |
| 289 | // diagnostics in a deterministic ordering, we must order all diagnostics to |
| 290 | // output by SourceLocation when iterating through this lockset. |
| 291 | bool operator<(const MutexID &other) const { |
| 292 | return DeclSeq < other.DeclSeq; |
| 293 | } |
| 294 | |
| 295 | /// \brief Returns the name of the first Decl in the list for a given MutexID; |
| 296 | /// e.g. the lock expression foo.bar() has name "bar". |
| 297 | /// The caret will point unambiguously to the lock expression, so using this |
| 298 | /// name in diagnostics is a way to get simple, and consistent, mutex names. |
| 299 | /// We do not want to output the entire expression text for security reasons. |
| 300 | StringRef getName() const { |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 301 | assert(isValid()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 302 | return DeclSeq.front()->getName(); |
| 303 | } |
| 304 | |
| 305 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 306 | for (SmallVectorImpl<NamedDecl*>::const_iterator I = DeclSeq.begin(), |
| 307 | E = DeclSeq.end(); I != E; ++I) { |
| 308 | ID.AddPointer(*I); |
| 309 | } |
| 310 | } |
| 311 | }; |
| 312 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 313 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 314 | /// \brief This is a helper class that stores info about the most recent |
| 315 | /// accquire of a Lock. |
| 316 | /// |
| 317 | /// The main body of the analysis maps MutexIDs to LockDatas. |
| 318 | struct LockData { |
| 319 | SourceLocation AcquireLoc; |
| 320 | |
| 321 | /// \brief LKind stores whether a lock is held shared or exclusively. |
| 322 | /// Note that this analysis does not currently support either re-entrant |
| 323 | /// locking or lock "upgrading" and "downgrading" between exclusive and |
| 324 | /// shared. |
| 325 | /// |
| 326 | /// FIXME: add support for re-entrant locking and lock up/downgrading |
| 327 | LockKind LKind; |
| 328 | |
| 329 | LockData(SourceLocation AcquireLoc, LockKind LKind) |
| 330 | : AcquireLoc(AcquireLoc), LKind(LKind) {} |
| 331 | |
| 332 | bool operator==(const LockData &other) const { |
| 333 | return AcquireLoc == other.AcquireLoc && LKind == other.LKind; |
| 334 | } |
| 335 | |
| 336 | bool operator!=(const LockData &other) const { |
| 337 | return !(*this == other); |
| 338 | } |
| 339 | |
| 340 | void Profile(llvm::FoldingSetNodeID &ID) const { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 341 | ID.AddInteger(AcquireLoc.getRawEncoding()); |
| 342 | ID.AddInteger(LKind); |
| 343 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 346 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 347 | /// A Lockset maps each MutexID (defined above) to information about how it has |
| 348 | /// been locked. |
| 349 | typedef llvm::ImmutableMap<MutexID, LockData> Lockset; |
| 350 | |
| 351 | /// \brief We use this class to visit different types of expressions in |
| 352 | /// CFGBlocks, and build up the lockset. |
| 353 | /// An expression may cause us to add or remove locks from the lockset, or else |
| 354 | /// output error messages related to missing locks. |
| 355 | /// FIXME: In future, we may be able to not inherit from a visitor. |
| 356 | class BuildLockset : public StmtVisitor<BuildLockset> { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 357 | friend class ThreadSafetyAnalyzer; |
| 358 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 359 | ThreadSafetyHandler &Handler; |
| 360 | Lockset LSet; |
| 361 | Lockset::Factory &LocksetFactory; |
| 362 | |
| 363 | // Helper functions |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 364 | void removeLock(SourceLocation UnlockLoc, MutexID &Mutex); |
| 365 | void addLock(SourceLocation LockLoc, MutexID &Mutex, LockKind LK); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 366 | |
| 367 | template <class AttrType> |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 368 | void addLocksToSet(LockKind LK, AttrType *Attr, Expr *Exp, NamedDecl *D); |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 369 | void removeLocksFromSet(UnlockFunctionAttr *Attr, |
| 370 | Expr *Exp, NamedDecl* FunDecl); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 371 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 372 | const ValueDecl *getValueDecl(Expr *Exp); |
| 373 | void warnIfMutexNotHeld (const NamedDecl *D, Expr *Exp, AccessKind AK, |
| 374 | Expr *MutexExp, ProtectedOperationKind POK); |
| 375 | void checkAccess(Expr *Exp, AccessKind AK); |
| 376 | void checkDereference(Expr *Exp, AccessKind AK); |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 377 | void handleCall(Expr *Exp, NamedDecl *D); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 378 | |
| 379 | /// \brief Returns true if the lockset contains a lock, regardless of whether |
| 380 | /// the lock is held exclusively or shared. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 381 | bool locksetContains(const MutexID &Lock) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 382 | return LSet.lookup(Lock); |
| 383 | } |
| 384 | |
| 385 | /// \brief Returns true if the lockset contains a lock with the passed in |
| 386 | /// locktype. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 387 | bool locksetContains(const MutexID &Lock, LockKind KindRequested) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 388 | const LockData *LockHeld = LSet.lookup(Lock); |
| 389 | return (LockHeld && KindRequested == LockHeld->LKind); |
| 390 | } |
| 391 | |
| 392 | /// \brief Returns true if the lockset contains a lock with at least the |
| 393 | /// passed in locktype. So for example, if we pass in LK_Shared, this function |
| 394 | /// returns true if the lock is held LK_Shared or LK_Exclusive. If we pass in |
| 395 | /// LK_Exclusive, this function returns true if the lock is held LK_Exclusive. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 396 | bool locksetContainsAtLeast(const MutexID &Lock, |
| 397 | LockKind KindRequested) const { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 398 | switch (KindRequested) { |
| 399 | case LK_Shared: |
| 400 | return locksetContains(Lock); |
| 401 | case LK_Exclusive: |
| 402 | return locksetContains(Lock, KindRequested); |
| 403 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 404 | llvm_unreachable("Unknown LockKind"); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | public: |
| 408 | BuildLockset(ThreadSafetyHandler &Handler, Lockset LS, Lockset::Factory &F) |
| 409 | : StmtVisitor<BuildLockset>(), Handler(Handler), LSet(LS), |
| 410 | LocksetFactory(F) {} |
| 411 | |
| 412 | Lockset getLockset() { |
| 413 | return LSet; |
| 414 | } |
| 415 | |
| 416 | void VisitUnaryOperator(UnaryOperator *UO); |
| 417 | void VisitBinaryOperator(BinaryOperator *BO); |
| 418 | void VisitCastExpr(CastExpr *CE); |
| 419 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp); |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 420 | void VisitCXXConstructExpr(CXXConstructExpr *Exp); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | /// \brief Add a new lock to the lockset, warning if the lock is already there. |
| 424 | /// \param LockLoc The source location of the acquire |
| 425 | /// \param LockExp The lock expression corresponding to the lock to be added |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 426 | void BuildLockset::addLock(SourceLocation LockLoc, MutexID &Mutex, |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 427 | LockKind LK) { |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 428 | // FIXME: deal with acquired before/after annotations. We can write a first |
| 429 | // pass that does the transitive lookup lazily, and refine afterwards. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 430 | LockData NewLock(LockLoc, LK); |
| 431 | |
| 432 | // FIXME: Don't always warn when we have support for reentrant locks. |
| 433 | if (locksetContains(Mutex)) |
| 434 | Handler.handleDoubleLock(Mutex.getName(), LockLoc); |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 435 | else |
| 436 | LSet = LocksetFactory.add(LSet, Mutex, NewLock); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /// \brief Remove a lock from the lockset, warning if the lock is not there. |
| 440 | /// \param LockExp The lock expression corresponding to the lock to be removed |
| 441 | /// \param UnlockLoc The source location of the unlock (only used in error msg) |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 442 | void BuildLockset::removeLock(SourceLocation UnlockLoc, MutexID &Mutex) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 443 | Lockset NewLSet = LocksetFactory.remove(LSet, Mutex); |
| 444 | if(NewLSet == LSet) |
| 445 | Handler.handleUnmatchedUnlock(Mutex.getName(), UnlockLoc); |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 446 | else |
| 447 | LSet = NewLSet; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 448 | } |
| 449 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 450 | /// \brief This function, parameterized by an attribute type, is used to add a |
| 451 | /// set of locks specified as attribute arguments to the lockset. |
| 452 | template <typename AttrType> |
| 453 | void BuildLockset::addLocksToSet(LockKind LK, AttrType *Attr, |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 454 | Expr *Exp, NamedDecl* FunDecl) { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 455 | typedef typename AttrType::args_iterator iterator_type; |
| 456 | |
| 457 | SourceLocation ExpLocation = Exp->getExprLoc(); |
| 458 | |
| 459 | if (Attr->args_size() == 0) { |
| 460 | // The mutex held is the "this" object. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 461 | MutexID Mutex(0, Exp, FunDecl); |
| 462 | if (!Mutex.isValid()) |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 463 | MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 464 | else |
| 465 | addLock(ExpLocation, Mutex, LK); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) { |
| 470 | MutexID Mutex(*I, Exp, FunDecl); |
| 471 | if (!Mutex.isValid()) |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 472 | MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 473 | else |
| 474 | addLock(ExpLocation, Mutex, LK); |
| 475 | } |
| 476 | } |
| 477 | |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 478 | /// \brief This function removes a set of locks specified as attribute |
| 479 | /// arguments from the lockset. |
| 480 | void BuildLockset::removeLocksFromSet(UnlockFunctionAttr *Attr, |
| 481 | Expr *Exp, NamedDecl* FunDecl) { |
| 482 | SourceLocation ExpLocation; |
| 483 | if (Exp) ExpLocation = Exp->getExprLoc(); |
| 484 | |
| 485 | if (Attr->args_size() == 0) { |
| 486 | // The mutex held is the "this" object. |
| 487 | MutexID Mu(0, Exp, FunDecl); |
| 488 | if (!Mu.isValid()) |
| 489 | MutexID::warnInvalidLock(Handler, 0, Exp, FunDecl); |
| 490 | else |
| 491 | removeLock(ExpLocation, Mu); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | for (UnlockFunctionAttr::args_iterator I = Attr->args_begin(), |
| 496 | E = Attr->args_end(); I != E; ++I) { |
| 497 | MutexID Mutex(*I, Exp, FunDecl); |
| 498 | if (!Mutex.isValid()) |
| 499 | MutexID::warnInvalidLock(Handler, *I, Exp, FunDecl); |
| 500 | else |
| 501 | removeLock(ExpLocation, Mutex); |
| 502 | } |
| 503 | } |
| 504 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 505 | /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs |
| 506 | const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) { |
| 507 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp)) |
| 508 | return DR->getDecl(); |
| 509 | |
| 510 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) |
| 511 | return ME->getMemberDecl(); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /// \brief Warn if the LSet does not contain a lock sufficient to protect access |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 517 | /// of at least the passed in AccessKind. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 518 | void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, |
| 519 | AccessKind AK, Expr *MutexExp, |
| 520 | ProtectedOperationKind POK) { |
| 521 | LockKind LK = getLockKindFromAccessKind(AK); |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 522 | |
| 523 | MutexID Mutex(MutexExp, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 524 | if (!Mutex.isValid()) |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 525 | MutexID::warnInvalidLock(Handler, MutexExp, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 526 | else if (!locksetContainsAtLeast(Mutex, LK)) |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 527 | Handler.handleMutexNotHeld(D, POK, Mutex.getName(), LK, Exp->getExprLoc()); |
| 528 | } |
| 529 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 530 | /// \brief This method identifies variable dereferences and checks pt_guarded_by |
| 531 | /// and pt_guarded_var annotations. Note that we only check these annotations |
| 532 | /// at the time a pointer is dereferenced. |
| 533 | /// FIXME: We need to check for other types of pointer dereferences |
| 534 | /// (e.g. [], ->) and deal with them here. |
| 535 | /// \param Exp An expression that has been read or written. |
| 536 | void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) { |
| 537 | UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp); |
| 538 | if (!UO || UO->getOpcode() != clang::UO_Deref) |
| 539 | return; |
| 540 | Exp = UO->getSubExpr()->IgnoreParenCasts(); |
| 541 | |
| 542 | const ValueDecl *D = getValueDecl(Exp); |
| 543 | if(!D || !D->hasAttrs()) |
| 544 | return; |
| 545 | |
| 546 | if (D->getAttr<PtGuardedVarAttr>() && LSet.isEmpty()) |
| 547 | Handler.handleNoMutexHeld(D, POK_VarDereference, AK, Exp->getExprLoc()); |
| 548 | |
| 549 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 550 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 551 | if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i])) |
| 552 | warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference); |
| 553 | } |
| 554 | |
| 555 | /// \brief Checks guarded_by and guarded_var attributes. |
| 556 | /// Whenever we identify an access (read or write) of a DeclRefExpr or |
| 557 | /// MemberExpr, we need to check whether there are any guarded_by or |
| 558 | /// guarded_var attributes, and make sure we hold the appropriate mutexes. |
| 559 | void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) { |
| 560 | const ValueDecl *D = getValueDecl(Exp); |
| 561 | if(!D || !D->hasAttrs()) |
| 562 | return; |
| 563 | |
| 564 | if (D->getAttr<GuardedVarAttr>() && LSet.isEmpty()) |
| 565 | Handler.handleNoMutexHeld(D, POK_VarAccess, AK, Exp->getExprLoc()); |
| 566 | |
| 567 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 568 | for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i) |
| 569 | if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i])) |
| 570 | warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess); |
| 571 | } |
| 572 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 573 | /// \brief Process a function call, method call, constructor call, |
| 574 | /// or destructor call. This involves looking at the attributes on the |
| 575 | /// corresponding function/method/constructor/destructor, issuing warnings, |
| 576 | /// and updating the locksets accordingly. |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 577 | /// |
| 578 | /// FIXME: For classes annotated with one of the guarded annotations, we need |
| 579 | /// to treat const method calls as reads and non-const method calls as writes, |
| 580 | /// and check that the appropriate locks are held. Non-const method calls with |
| 581 | /// the same signature as const method calls can be also treated as reads. |
| 582 | /// |
| 583 | /// FIXME: We need to also visit CallExprs to catch/check global functions. |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 584 | /// |
| 585 | /// FIXME: Do not flag an error for member variables accessed in constructors/ |
| 586 | /// destructors |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 587 | void BuildLockset::handleCall(Expr *Exp, NamedDecl *D) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 588 | AttrVec &ArgAttrs = D->getAttrs(); |
| 589 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 590 | Attr *Attr = ArgAttrs[i]; |
| 591 | switch (Attr->getKind()) { |
| 592 | // When we encounter an exclusive lock function, we need to add the lock |
| 593 | // to our lockset with kind exclusive. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 594 | case attr::ExclusiveLockFunction: { |
| 595 | ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(Attr); |
| 596 | addLocksToSet(LK_Exclusive, A, Exp, D); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 597 | break; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 598 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 599 | |
| 600 | // When we encounter a shared lock function, we need to add the lock |
| 601 | // to our lockset with kind shared. |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 602 | case attr::SharedLockFunction: { |
| 603 | SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(Attr); |
| 604 | addLocksToSet(LK_Shared, A, Exp, D); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 605 | break; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 606 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 607 | |
| 608 | // When we encounter an unlock function, we need to remove unlocked |
| 609 | // mutexes from the lockset, and flag a warning if they are not there. |
| 610 | case attr::UnlockFunction: { |
| 611 | UnlockFunctionAttr *UFAttr = cast<UnlockFunctionAttr>(Attr); |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 612 | removeLocksFromSet(UFAttr, Exp, D); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | |
| 616 | case attr::ExclusiveLocksRequired: { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 617 | ExclusiveLocksRequiredAttr *ELRAttr = |
| 618 | cast<ExclusiveLocksRequiredAttr>(Attr); |
| 619 | |
| 620 | for (ExclusiveLocksRequiredAttr::args_iterator |
| 621 | I = ELRAttr->args_begin(), E = ELRAttr->args_end(); I != E; ++I) |
| 622 | warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall); |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | case attr::SharedLocksRequired: { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 627 | SharedLocksRequiredAttr *SLRAttr = cast<SharedLocksRequiredAttr>(Attr); |
| 628 | |
| 629 | for (SharedLocksRequiredAttr::args_iterator I = SLRAttr->args_begin(), |
| 630 | E = SLRAttr->args_end(); I != E; ++I) |
| 631 | warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall); |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | case attr::LocksExcluded: { |
| 636 | LocksExcludedAttr *LEAttr = cast<LocksExcludedAttr>(Attr); |
| 637 | for (LocksExcludedAttr::args_iterator I = LEAttr->args_begin(), |
| 638 | E = LEAttr->args_end(); I != E; ++I) { |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 639 | MutexID Mutex(*I, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 640 | if (!Mutex.isValid()) |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 641 | MutexID::warnInvalidLock(Handler, *I, Exp, D); |
Caitlin Sadowski | 194418f | 2011-09-14 20:00:24 +0000 | [diff] [blame] | 642 | else if (locksetContains(Mutex)) |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 643 | Handler.handleFunExcludesLock(D->getName(), Mutex.getName(), |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 644 | Exp->getExprLoc()); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 645 | } |
| 646 | break; |
| 647 | } |
| 648 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 649 | // Ignore other (non thread-safety) attributes |
| 650 | default: |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
DeLesley Hutchins | e0eaa85 | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 656 | /// \brief For unary operations which read and write a variable, we need to |
| 657 | /// check whether we hold any required mutexes. Reads are checked in |
| 658 | /// VisitCastExpr. |
| 659 | void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { |
| 660 | switch (UO->getOpcode()) { |
| 661 | case clang::UO_PostDec: |
| 662 | case clang::UO_PostInc: |
| 663 | case clang::UO_PreDec: |
| 664 | case clang::UO_PreInc: { |
| 665 | Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 666 | checkAccess(SubExp, AK_Written); |
| 667 | checkDereference(SubExp, AK_Written); |
| 668 | break; |
| 669 | } |
| 670 | default: |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /// For binary operations which assign to a variable (writes), we need to check |
| 676 | /// whether we hold any required mutexes. |
| 677 | /// FIXME: Deal with non-primitive types. |
| 678 | void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { |
| 679 | if (!BO->isAssignmentOp()) |
| 680 | return; |
| 681 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
| 682 | checkAccess(LHSExp, AK_Written); |
| 683 | checkDereference(LHSExp, AK_Written); |
| 684 | } |
| 685 | |
| 686 | /// Whenever we do an LValue to Rvalue cast, we are reading a variable and |
| 687 | /// need to ensure we hold any required mutexes. |
| 688 | /// FIXME: Deal with non-primitive types. |
| 689 | void BuildLockset::VisitCastExpr(CastExpr *CE) { |
| 690 | if (CE->getCastKind() != CK_LValueToRValue) |
| 691 | return; |
| 692 | Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts(); |
| 693 | checkAccess(SubExp, AK_Read); |
| 694 | checkDereference(SubExp, AK_Read); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | void BuildLockset::VisitCXXMemberCallExpr(CXXMemberCallExpr *Exp) { |
| 699 | NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 700 | if(!D || !D->hasAttrs()) |
| 701 | return; |
| 702 | handleCall(Exp, D); |
| 703 | } |
| 704 | |
| 705 | void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) { |
| 706 | NamedDecl *D = cast<NamedDecl>(Exp->getConstructor()); |
| 707 | if(!D || !D->hasAttrs()) |
| 708 | return; |
| 709 | handleCall(Exp, D); |
| 710 | } |
| 711 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 712 | |
| 713 | /// \brief Class which implements the core thread safety analysis routines. |
| 714 | class ThreadSafetyAnalyzer { |
| 715 | ThreadSafetyHandler &Handler; |
| 716 | Lockset::Factory LocksetFactory; |
| 717 | |
| 718 | public: |
| 719 | ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {} |
| 720 | |
| 721 | Lockset intersectAndWarn(const Lockset LSet1, const Lockset LSet2, |
| 722 | LockErrorKind LEK); |
| 723 | |
| 724 | Lockset addLock(Lockset &LSet, Expr *MutexExp, const NamedDecl *D, |
| 725 | LockKind LK, SourceLocation Loc); |
| 726 | |
| 727 | void runAnalysis(AnalysisContext &AC); |
| 728 | }; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 729 | |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 730 | /// \brief Compute the intersection of two locksets and issue warnings for any |
| 731 | /// locks in the symmetric difference. |
| 732 | /// |
| 733 | /// This function is used at a merge point in the CFG when comparing the lockset |
| 734 | /// of each branch being merged. For example, given the following sequence: |
| 735 | /// A; if () then B; else C; D; we need to check that the lockset after B and C |
| 736 | /// are the same. In the event of a difference, we use the intersection of these |
| 737 | /// two locksets at the start of D. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 738 | Lockset ThreadSafetyAnalyzer::intersectAndWarn(const Lockset LSet1, |
| 739 | const Lockset LSet2, |
| 740 | LockErrorKind LEK) { |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 741 | Lockset Intersection = LSet1; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 742 | for (Lockset::iterator I = LSet2.begin(), E = LSet2.end(); I != E; ++I) { |
| 743 | const MutexID &LSet2Mutex = I.getKey(); |
| 744 | const LockData &LSet2LockData = I.getData(); |
| 745 | if (const LockData *LD = LSet1.lookup(LSet2Mutex)) { |
| 746 | if (LD->LKind != LSet2LockData.LKind) { |
| 747 | Handler.handleExclusiveAndShared(LSet2Mutex.getName(), |
| 748 | LSet2LockData.AcquireLoc, |
| 749 | LD->AcquireLoc); |
| 750 | if (LD->LKind != LK_Exclusive) |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 751 | Intersection = LocksetFactory.add(Intersection, LSet2Mutex, |
| 752 | LSet2LockData); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 753 | } |
| 754 | } else { |
| 755 | Handler.handleMutexHeldEndOfScope(LSet2Mutex.getName(), |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 756 | LSet2LockData.AcquireLoc, LEK); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 759 | |
| 760 | for (Lockset::iterator I = LSet1.begin(), E = LSet1.end(); I != E; ++I) { |
| 761 | if (!LSet2.contains(I.getKey())) { |
| 762 | const MutexID &Mutex = I.getKey(); |
| 763 | const LockData &MissingLock = I.getData(); |
| 764 | Handler.handleMutexHeldEndOfScope(Mutex.getName(), |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 765 | MissingLock.AcquireLoc, LEK); |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 766 | Intersection = LocksetFactory.remove(Intersection, Mutex); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | return Intersection; |
| 770 | } |
| 771 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 772 | Lockset ThreadSafetyAnalyzer::addLock(Lockset &LSet, Expr *MutexExp, |
| 773 | const NamedDecl *D, |
| 774 | LockKind LK, SourceLocation Loc) { |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 775 | MutexID Mutex(MutexExp, 0, D); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 776 | if (!Mutex.isValid()) { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 777 | MutexID::warnInvalidLock(Handler, MutexExp, 0, D); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 778 | return LSet; |
| 779 | } |
| 780 | LockData NewLock(Loc, LK); |
| 781 | return LocksetFactory.add(LSet, Mutex, NewLock); |
| 782 | } |
| 783 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 784 | /// \brief Check a function's CFG for thread-safety violations. |
| 785 | /// |
| 786 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 787 | /// at the end of each block, and issue warnings for thread safety violations. |
| 788 | /// Each block in the CFG is traversed exactly once. |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 789 | void ThreadSafetyAnalyzer::runAnalysis(AnalysisContext &AC) { |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 790 | CFG *CFGraph = AC.getCFG(); |
| 791 | if (!CFGraph) return; |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 792 | const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl()); |
| 793 | |
| 794 | if (!D) |
| 795 | return; // Ignore anonymous functions for now. |
| 796 | if (D->getAttr<NoThreadSafetyAnalysisAttr>()) |
| 797 | return; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 798 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 799 | // FIXME: Switch to SmallVector? Otherwise improve performance impact? |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 800 | std::vector<Lockset> EntryLocksets(CFGraph->getNumBlockIDs(), |
| 801 | LocksetFactory.getEmptyMap()); |
| 802 | std::vector<Lockset> ExitLocksets(CFGraph->getNumBlockIDs(), |
| 803 | LocksetFactory.getEmptyMap()); |
| 804 | |
| 805 | // We need to explore the CFG via a "topological" ordering. |
| 806 | // That way, we will be guaranteed to have information about required |
| 807 | // predecessor locksets when exploring a new block. |
| 808 | TopologicallySortedCFG SortedGraph(CFGraph); |
| 809 | CFGBlockSet VisitedBlocks(CFGraph); |
| 810 | |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 811 | // Add locks from exclusive_locks_required and shared_locks_required |
| 812 | // to initial lockset. |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 813 | if (!SortedGraph.empty() && D->hasAttrs()) { |
| 814 | const CFGBlock *FirstBlock = *SortedGraph.begin(); |
| 815 | Lockset &InitialLockset = EntryLocksets[FirstBlock->getBlockID()]; |
| 816 | const AttrVec &ArgAttrs = D->getAttrs(); |
| 817 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
| 818 | Attr *Attr = ArgAttrs[i]; |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 819 | SourceLocation AttrLoc = Attr->getLocation(); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 820 | if (SharedLocksRequiredAttr *SLRAttr |
| 821 | = dyn_cast<SharedLocksRequiredAttr>(Attr)) { |
| 822 | for (SharedLocksRequiredAttr::args_iterator |
| 823 | SLRIter = SLRAttr->args_begin(), |
| 824 | SLREnd = SLRAttr->args_end(); SLRIter != SLREnd; ++SLRIter) |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 825 | InitialLockset = addLock(InitialLockset, |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 826 | *SLRIter, D, LK_Shared, |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 827 | AttrLoc); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 828 | } else if (ExclusiveLocksRequiredAttr *ELRAttr |
| 829 | = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) { |
| 830 | for (ExclusiveLocksRequiredAttr::args_iterator |
| 831 | ELRIter = ELRAttr->args_begin(), |
| 832 | ELREnd = ELRAttr->args_end(); ELRIter != ELREnd; ++ELRIter) |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 833 | InitialLockset = addLock(InitialLockset, |
DeLesley Hutchins | 9f80a97 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 834 | *ELRIter, D, LK_Exclusive, |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 835 | AttrLoc); |
Caitlin Sadowski | cb96751 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 840 | for (TopologicallySortedCFG::iterator I = SortedGraph.begin(), |
| 841 | E = SortedGraph.end(); I!= E; ++I) { |
| 842 | const CFGBlock *CurrBlock = *I; |
| 843 | int CurrBlockID = CurrBlock->getBlockID(); |
| 844 | |
| 845 | VisitedBlocks.insert(CurrBlock); |
| 846 | |
| 847 | // Use the default initial lockset in case there are no predecessors. |
| 848 | Lockset &Entryset = EntryLocksets[CurrBlockID]; |
| 849 | Lockset &Exitset = ExitLocksets[CurrBlockID]; |
| 850 | |
| 851 | // Iterate through the predecessor blocks and warn if the lockset for all |
| 852 | // predecessors is not the same. We take the entry lockset of the current |
| 853 | // block to be the intersection of all previous locksets. |
| 854 | // FIXME: By keeping the intersection, we may output more errors in future |
| 855 | // for a lock which is not in the intersection, but was in the union. We |
| 856 | // may want to also keep the union in future. As an example, let's say |
| 857 | // the intersection contains Mutex L, and the union contains L and M. |
| 858 | // Later we unlock M. At this point, we would output an error because we |
| 859 | // never locked M; although the real error is probably that we forgot to |
| 860 | // lock M on all code paths. Conversely, let's say that later we lock M. |
| 861 | // In this case, we should compare against the intersection instead of the |
| 862 | // union because the real error is probably that we forgot to unlock M on |
| 863 | // all code paths. |
| 864 | bool LocksetInitialized = false; |
| 865 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 866 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 867 | |
| 868 | // if *PI -> CurrBlock is a back edge |
| 869 | if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) |
| 870 | continue; |
| 871 | |
| 872 | int PrevBlockID = (*PI)->getBlockID(); |
| 873 | if (!LocksetInitialized) { |
| 874 | Entryset = ExitLocksets[PrevBlockID]; |
| 875 | LocksetInitialized = true; |
| 876 | } else { |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 877 | Entryset = intersectAndWarn(Entryset, ExitLocksets[PrevBlockID], |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 878 | LEK_LockedSomePredecessors); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | BuildLockset LocksetBuilder(Handler, Entryset, LocksetFactory); |
| 883 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 884 | BE = CurrBlock->end(); BI != BE; ++BI) { |
DeLesley Hutchins | 6db51f7 | 2011-10-21 20:51:27 +0000 | [diff] [blame^] | 885 | switch (BI->getKind()) { |
| 886 | case CFGElement::Statement: { |
| 887 | const CFGStmt *CS = cast<CFGStmt>(&*BI); |
| 888 | LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt())); |
| 889 | break; |
| 890 | } |
| 891 | // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now. |
| 892 | case CFGElement::AutomaticObjectDtor: { |
| 893 | const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI); |
| 894 | CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>( |
| 895 | AD->getDestructorDecl(AC.getASTContext())); |
| 896 | if (!DD->hasAttrs()) |
| 897 | break; |
| 898 | |
| 899 | // Create a dummy expression, |
| 900 | VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl()); |
| 901 | DeclRefExpr DRE(VD, VD->getType(), VK_LValue, |
| 902 | AD->getTriggerStmt()->getLocEnd()); |
| 903 | LocksetBuilder.handleCall(&DRE, DD); |
| 904 | break; |
| 905 | } |
| 906 | default: |
| 907 | break; |
| 908 | } |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 909 | } |
| 910 | Exitset = LocksetBuilder.getLockset(); |
| 911 | |
| 912 | // For every back edge from CurrBlock (the end of the loop) to another block |
| 913 | // (FirstLoopBlock) we need to check that the Lockset of Block is equal to |
| 914 | // the one held at the beginning of FirstLoopBlock. We can look up the |
| 915 | // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. |
| 916 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 917 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 918 | |
| 919 | // if CurrBlock -> *SI is *not* a back edge |
| 920 | if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) |
| 921 | continue; |
| 922 | |
| 923 | CFGBlock *FirstLoopBlock = *SI; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 924 | Lockset PreLoop = EntryLocksets[FirstLoopBlock->getBlockID()]; |
| 925 | Lockset LoopEnd = ExitLocksets[CurrBlockID]; |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 926 | intersectAndWarn(LoopEnd, PreLoop, LEK_LockedSomeLoopIterations); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 930 | Lockset InitialLockset = EntryLocksets[CFGraph->getEntry().getBlockID()]; |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 931 | Lockset FinalLockset = ExitLocksets[CFGraph->getExit().getBlockID()]; |
Caitlin Sadowski | 1748b12 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 932 | |
| 933 | // FIXME: Should we call this function for all blocks which exit the function? |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 934 | intersectAndWarn(InitialLockset, FinalLockset, LEK_LockedAtEndOfFunction); |
| 935 | } |
| 936 | |
| 937 | } // end anonymous namespace |
| 938 | |
| 939 | |
| 940 | namespace clang { |
| 941 | namespace thread_safety { |
| 942 | |
| 943 | /// \brief Check a function's CFG for thread-safety violations. |
| 944 | /// |
| 945 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 946 | /// at the end of each block, and issue warnings for thread safety violations. |
| 947 | /// Each block in the CFG is traversed exactly once. |
| 948 | void runThreadSafetyAnalysis(AnalysisContext &AC, |
| 949 | ThreadSafetyHandler &Handler) { |
| 950 | ThreadSafetyAnalyzer Analyzer(Handler); |
| 951 | Analyzer.runAnalysis(AC); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /// \brief Helper function that returns a LockKind required for the given level |
| 955 | /// of access. |
| 956 | LockKind getLockKindFromAccessKind(AccessKind AK) { |
| 957 | switch (AK) { |
| 958 | case AK_Read : |
| 959 | return LK_Shared; |
| 960 | case AK_Written : |
| 961 | return LK_Exclusive; |
| 962 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 963 | llvm_unreachable("Unknown AccessKind"); |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 964 | } |
DeLesley Hutchins | a60448d | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 965 | |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 966 | }} // end namespace clang::thread_safety |