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 | // |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 13 | // See http://clang.llvm.org/docs/ThreadSafetyAnalysis.html |
Aaron Ballman | fcd5b7e | 2013-06-26 19:17:19 +0000 | [diff] [blame] | 14 | // for more information. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
| 21 | #include "clang/AST/StmtCXX.h" |
| 22 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
Aaron Ballman | 7c192b4 | 2014-05-09 18:26:23 +0000 | [diff] [blame] | 25 | #include "clang/Analysis/Analyses/ThreadSafetyLogical.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/Analyses/ThreadSafetyTIL.h" |
DeLesley Hutchins | 7e615c2 | 2014-04-09 22:39:43 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/Analyses/ThreadSafetyTraverse.h" |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/Analyses/ThreadSafetyCommon.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Analysis/AnalysisContext.h" |
| 30 | #include "clang/Analysis/CFG.h" |
| 31 | #include "clang/Analysis/CFGStmtMap.h" |
DeLesley Hutchins | 3a8d6cf | 2012-07-03 19:47:18 +0000 | [diff] [blame] | 32 | #include "clang/Basic/OperatorKinds.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 33 | #include "clang/Basic/SourceLocation.h" |
| 34 | #include "clang/Basic/SourceManager.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/BitVector.h" |
| 36 | #include "llvm/ADT/FoldingSet.h" |
| 37 | #include "llvm/ADT/ImmutableMap.h" |
| 38 | #include "llvm/ADT/PostOrderIterator.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
| 40 | #include "llvm/ADT/StringRef.h" |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 42 | #include <algorithm> |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 43 | #include <ostream> |
| 44 | #include <sstream> |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 45 | #include <utility> |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 46 | #include <vector> |
| 47 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 48 | |
| 49 | namespace clang { |
| 50 | namespace threadSafety { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 51 | |
Caitlin Sadowski | 5b34a2f | 2011-09-14 20:05:09 +0000 | [diff] [blame] | 52 | // Key method definition |
| 53 | ThreadSafetyHandler::~ThreadSafetyHandler() {} |
| 54 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 55 | class TILPrinter : |
| 56 | public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {}; |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 57 | |
| 58 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 59 | /// Issue a warning about an invalid lock expression |
| 60 | static void warnInvalidLock(ThreadSafetyHandler &Handler, |
| 61 | const Expr *MutexExp, const NamedDecl *D, |
| 62 | const Expr *DeclExp, StringRef Kind) { |
| 63 | SourceLocation Loc; |
| 64 | if (DeclExp) |
| 65 | Loc = DeclExp->getExprLoc(); |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 66 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 67 | // FIXME: add a note about the attribute location in MutexExp or D |
| 68 | if (Loc.isValid()) |
| 69 | Handler.handleInvalidLockExp(Kind, Loc); |
| 70 | } |
DeLesley Hutchins | 9b1d72f | 2012-08-10 20:19:55 +0000 | [diff] [blame] | 71 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 72 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 73 | /// \brief A set of CapabilityInfo objects, which are compiled from the |
| 74 | /// requires attributes on a function. |
| 75 | class CapExprSet : public SmallVector<CapabilityExpr, 4> { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 76 | public: |
Aaron Ballman | cea2609 | 2014-03-06 19:10:16 +0000 | [diff] [blame] | 77 | /// \brief Push M onto list, but discard duplicates. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 78 | void push_back_nodup(const CapabilityExpr &CapE) { |
| 79 | iterator It = std::find_if(begin(), end(), |
| 80 | [=](const CapabilityExpr &CapE2) { |
| 81 | return CapE.equals(CapE2); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 82 | }); |
| 83 | if (It == end()) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 84 | push_back(CapE); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /// \brief This is a helper class that stores a fact that is known at a |
| 91 | /// particular point in program execution. Currently, a fact is a capability, |
| 92 | /// along with additional information, such as where it was acquired, whether |
| 93 | /// it is exclusive or shared, etc. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 94 | /// |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 95 | /// FIXME: this analysis does not currently support either re-entrant |
| 96 | /// locking or lock "upgrading" and "downgrading" between exclusive and |
| 97 | /// shared. |
| 98 | class FactEntry : public CapabilityExpr { |
| 99 | private: |
NAKAMURA Takumi | e9882cf | 2014-08-04 22:48:36 +0000 | [diff] [blame] | 100 | LockKind LKind; ///< exclusive or shared |
| 101 | SourceLocation AcquireLoc; ///< where it was acquired. |
| 102 | bool Managed; ///< for ScopedLockable objects |
| 103 | bool Asserted; ///< true if the lock was asserted |
| 104 | const til::SExpr* UnderlyingMutex; ///< for ScopedLockable objects |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 105 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 106 | public: |
| 107 | FactEntry(const CapabilityExpr &CE, LockKind LK, SourceLocation Loc, |
| 108 | bool Mng=false, bool Asrt=false) |
| 109 | : CapabilityExpr(CE), LKind(LK), AcquireLoc(Loc), Managed(Mng), |
| 110 | Asserted(Asrt), UnderlyingMutex(nullptr) |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 111 | {} |
| 112 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 113 | FactEntry(const CapabilityExpr &CE, LockKind LK, SourceLocation Loc, |
| 114 | const til::SExpr *Mu) |
| 115 | : CapabilityExpr(CE), LKind(LK), AcquireLoc(Loc), Managed(false), |
| 116 | Asserted(false), UnderlyingMutex(Mu) |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 117 | {} |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 118 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 119 | LockKind kind() const { return LKind; } |
| 120 | SourceLocation loc() const { return AcquireLoc; } |
| 121 | bool asserted() const { return Asserted; } |
| 122 | bool managed() const { return Managed; } |
| 123 | const til::SExpr* underlying() const { return UnderlyingMutex; } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 124 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 125 | // Return true if LKind >= LK, where exclusive > shared |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 126 | bool isAtLeast(LockKind LK) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 127 | return (LKind == LK_Exclusive) || (LK == LK_Shared); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 128 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 131 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 132 | typedef unsigned short FactID; |
| 133 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 134 | /// \brief FactManager manages the memory for all facts that are created during |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 135 | /// the analysis of a single routine. |
| 136 | class FactManager { |
| 137 | private: |
| 138 | std::vector<FactEntry> Facts; |
| 139 | |
| 140 | public: |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 141 | FactID newFact(const FactEntry &Entry) { |
| 142 | Facts.push_back(Entry); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 143 | return static_cast<unsigned short>(Facts.size() - 1); |
| 144 | } |
| 145 | |
| 146 | const FactEntry& operator[](FactID F) const { return Facts[F]; } |
| 147 | FactEntry& operator[](FactID F) { return Facts[F]; } |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | /// \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] | 152 | /// particular program point. FactSets must be small, because they are |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 153 | /// 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] | 154 | /// 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] | 155 | /// locks, so we can get away with doing a linear search for lookup. Note |
| 156 | /// that a hashtable or map is inappropriate in this case, because lookups |
| 157 | /// may involve partial pattern matches, rather than exact matches. |
| 158 | class FactSet { |
| 159 | private: |
| 160 | typedef SmallVector<FactID, 4> FactVec; |
| 161 | |
| 162 | FactVec FactIDs; |
| 163 | |
| 164 | public: |
| 165 | typedef FactVec::iterator iterator; |
| 166 | typedef FactVec::const_iterator const_iterator; |
| 167 | |
| 168 | iterator begin() { return FactIDs.begin(); } |
| 169 | const_iterator begin() const { return FactIDs.begin(); } |
| 170 | |
| 171 | iterator end() { return FactIDs.end(); } |
| 172 | const_iterator end() const { return FactIDs.end(); } |
| 173 | |
| 174 | bool isEmpty() const { return FactIDs.size() == 0; } |
| 175 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 176 | // Return true if the set contains only negative facts |
| 177 | bool isEmpty(FactManager &FactMan) const { |
| 178 | for (FactID FID : *this) { |
| 179 | if (!FactMan[FID].negative()) |
| 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | void addLockByID(FactID ID) { FactIDs.push_back(ID); } |
| 186 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 187 | FactID addLock(FactManager& FM, const FactEntry &Entry) { |
| 188 | FactID F = FM.newFact(Entry); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 189 | FactIDs.push_back(F); |
| 190 | return F; |
| 191 | } |
| 192 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 193 | bool removeLock(FactManager& FM, const CapabilityExpr &CapE) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 194 | unsigned n = FactIDs.size(); |
| 195 | if (n == 0) |
| 196 | return false; |
| 197 | |
| 198 | for (unsigned i = 0; i < n-1; ++i) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 199 | if (FM[FactIDs[i]].matches(CapE)) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 200 | FactIDs[i] = FactIDs[n-1]; |
| 201 | FactIDs.pop_back(); |
| 202 | return true; |
| 203 | } |
| 204 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 205 | if (FM[FactIDs[n-1]].matches(CapE)) { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 206 | FactIDs.pop_back(); |
| 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 212 | iterator findLockIter(FactManager &FM, const CapabilityExpr &CapE) { |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 213 | return std::find_if(begin(), end(), [&](FactID ID) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 214 | return FM[ID].matches(CapE); |
Aaron Ballman | 42f9a8a | 2014-05-14 15:01:43 +0000 | [diff] [blame] | 215 | }); |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 216 | } |
| 217 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 218 | FactEntry *findLock(FactManager &FM, const CapabilityExpr &CapE) const { |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 219 | auto I = std::find_if(begin(), end(), [&](FactID ID) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 220 | return FM[ID].matches(CapE); |
Aaron Ballman | 42f9a8a | 2014-05-14 15:01:43 +0000 | [diff] [blame] | 221 | }); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 222 | return I != end() ? &FM[*I] : nullptr; |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 223 | } |
| 224 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 225 | FactEntry *findLockUniv(FactManager &FM, const CapabilityExpr &CapE) const { |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 226 | auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 227 | return FM[ID].matchesUniv(CapE); |
Aaron Ballman | 42f9a8a | 2014-05-14 15:01:43 +0000 | [diff] [blame] | 228 | }); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 229 | return I != end() ? &FM[*I] : nullptr; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 230 | } |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 231 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 232 | FactEntry *findPartialMatch(FactManager &FM, |
| 233 | const CapabilityExpr &CapE) const { |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 234 | auto I = std::find_if(begin(), end(), [&](FactID ID) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 235 | return FM[ID].partiallyMatches(CapE); |
Aaron Ballman | 42f9a8a | 2014-05-14 15:01:43 +0000 | [diff] [blame] | 236 | }); |
Aaron Ballman | 42f9a8a | 2014-05-14 15:01:43 +0000 | [diff] [blame] | 237 | return I != end() ? &FM[*I] : nullptr; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 238 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 241 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 242 | typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 243 | class LocalVariableMap; |
| 244 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 245 | /// A side (entry or exit) of a CFG node. |
| 246 | enum CFGBlockSide { CBS_Entry, CBS_Exit }; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 247 | |
| 248 | /// CFGBlockInfo is a struct which contains all the information that is |
| 249 | /// maintained for each block in the CFG. See LocalVariableMap for more |
| 250 | /// information about the contexts. |
| 251 | struct CFGBlockInfo { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 252 | FactSet EntrySet; // Lockset held at entry to block |
| 253 | FactSet ExitSet; // Lockset held at exit from block |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 254 | LocalVarContext EntryContext; // Context held at entry to block |
| 255 | LocalVarContext ExitContext; // Context held at exit from block |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 256 | SourceLocation EntryLoc; // Location of first statement in block |
| 257 | SourceLocation ExitLoc; // Location of last statement in block. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 258 | unsigned EntryIndex; // Used to replay contexts later |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 259 | bool Reachable; // Is this block reachable? |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 260 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 261 | const FactSet &getSet(CFGBlockSide Side) const { |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 262 | return Side == CBS_Entry ? EntrySet : ExitSet; |
| 263 | } |
| 264 | SourceLocation getLocation(CFGBlockSide Side) const { |
| 265 | return Side == CBS_Entry ? EntryLoc : ExitLoc; |
| 266 | } |
| 267 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 268 | private: |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 269 | CFGBlockInfo(LocalVarContext EmptyCtx) |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 270 | : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 271 | { } |
| 272 | |
| 273 | public: |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 274 | static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | |
| 278 | |
| 279 | // A LocalVariableMap maintains a map from local variables to their currently |
| 280 | // valid definitions. It provides SSA-like functionality when traversing the |
| 281 | // CFG. Like SSA, each definition or assignment to a variable is assigned a |
| 282 | // unique name (an integer), which acts as the SSA name for that definition. |
| 283 | // The total set of names is shared among all CFG basic blocks. |
| 284 | // Unlike SSA, we do not rewrite expressions to replace local variables declrefs |
| 285 | // with their SSA-names. Instead, we compute a Context for each point in the |
| 286 | // code, which maps local variables to the appropriate SSA-name. This map |
| 287 | // changes with each assignment. |
| 288 | // |
| 289 | // The map is computed in a single pass over the CFG. Subsequent analyses can |
| 290 | // then query the map to find the appropriate Context for a statement, and use |
| 291 | // that Context to look up the definitions of variables. |
| 292 | class LocalVariableMap { |
| 293 | public: |
| 294 | typedef LocalVarContext Context; |
| 295 | |
| 296 | /// A VarDefinition consists of an expression, representing the value of the |
| 297 | /// variable, along with the context in which that expression should be |
| 298 | /// interpreted. A reference VarDefinition does not itself contain this |
| 299 | /// information, but instead contains a pointer to a previous VarDefinition. |
| 300 | struct VarDefinition { |
| 301 | public: |
| 302 | friend class LocalVariableMap; |
| 303 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 304 | const NamedDecl *Dec; // The original declaration for this variable. |
| 305 | const Expr *Exp; // The expression for this variable, OR |
| 306 | unsigned Ref; // Reference to another VarDefinition |
| 307 | Context Ctx; // The map with which Exp should be interpreted. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 308 | |
| 309 | bool isReference() { return !Exp; } |
| 310 | |
| 311 | private: |
| 312 | // Create ordinary variable definition |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 313 | VarDefinition(const NamedDecl *D, const Expr *E, Context C) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 314 | : Dec(D), Exp(E), Ref(0), Ctx(C) |
| 315 | { } |
| 316 | |
| 317 | // Create reference to previous definition |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 318 | VarDefinition(const NamedDecl *D, unsigned R, Context C) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 319 | : Dec(D), Exp(nullptr), Ref(R), Ctx(C) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 320 | { } |
| 321 | }; |
| 322 | |
| 323 | private: |
| 324 | Context::Factory ContextFactory; |
| 325 | std::vector<VarDefinition> VarDefinitions; |
| 326 | std::vector<unsigned> CtxIndices; |
| 327 | std::vector<std::pair<Stmt*, Context> > SavedContexts; |
| 328 | |
| 329 | public: |
| 330 | LocalVariableMap() { |
| 331 | // index 0 is a placeholder for undefined variables (aka phi-nodes). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 332 | VarDefinitions.push_back(VarDefinition(nullptr, 0u, getEmptyContext())); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /// Look up a definition, within the given context. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 336 | const VarDefinition* lookup(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 337 | const unsigned *i = Ctx.lookup(D); |
| 338 | if (!i) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 339 | return nullptr; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 340 | assert(*i < VarDefinitions.size()); |
| 341 | return &VarDefinitions[*i]; |
| 342 | } |
| 343 | |
| 344 | /// Look up the definition for D within the given context. Returns |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 345 | /// NULL if the expression is not statically known. If successful, also |
| 346 | /// modifies Ctx to hold the context of the return Expr. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 347 | const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 348 | const unsigned *P = Ctx.lookup(D); |
| 349 | if (!P) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 350 | return nullptr; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 351 | |
| 352 | unsigned i = *P; |
| 353 | while (i > 0) { |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 354 | if (VarDefinitions[i].Exp) { |
| 355 | Ctx = VarDefinitions[i].Ctx; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 356 | return VarDefinitions[i].Exp; |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 357 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 358 | i = VarDefinitions[i].Ref; |
| 359 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 360 | return nullptr; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | Context getEmptyContext() { return ContextFactory.getEmptyMap(); } |
| 364 | |
| 365 | /// Return the next context after processing S. This function is used by |
| 366 | /// clients of the class to get the appropriate context when traversing the |
| 367 | /// CFG. It must be called for every assignment or DeclStmt. |
| 368 | Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) { |
| 369 | if (SavedContexts[CtxIndex+1].first == S) { |
| 370 | CtxIndex++; |
| 371 | Context Result = SavedContexts[CtxIndex].second; |
| 372 | return Result; |
| 373 | } |
| 374 | return C; |
| 375 | } |
| 376 | |
| 377 | void dumpVarDefinitionName(unsigned i) { |
| 378 | if (i == 0) { |
| 379 | llvm::errs() << "Undefined"; |
| 380 | return; |
| 381 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 382 | const NamedDecl *Dec = VarDefinitions[i].Dec; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 383 | if (!Dec) { |
| 384 | llvm::errs() << "<<NULL>>"; |
| 385 | return; |
| 386 | } |
| 387 | Dec->printName(llvm::errs()); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 388 | llvm::errs() << "." << i << " " << ((const void*) Dec); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /// Dumps an ASCII representation of the variable map to llvm::errs() |
| 392 | void dump() { |
| 393 | for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 394 | const Expr *Exp = VarDefinitions[i].Exp; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 395 | unsigned Ref = VarDefinitions[i].Ref; |
| 396 | |
| 397 | dumpVarDefinitionName(i); |
| 398 | llvm::errs() << " = "; |
| 399 | if (Exp) Exp->dump(); |
| 400 | else { |
| 401 | dumpVarDefinitionName(Ref); |
| 402 | llvm::errs() << "\n"; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /// Dumps an ASCII representation of a Context to llvm::errs() |
| 408 | void dumpContext(Context C) { |
| 409 | for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 410 | const NamedDecl *D = I.getKey(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 411 | D->printName(llvm::errs()); |
| 412 | const unsigned *i = C.lookup(D); |
| 413 | llvm::errs() << " -> "; |
| 414 | dumpVarDefinitionName(*i); |
| 415 | llvm::errs() << "\n"; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /// Builds the variable map. |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 420 | void traverseCFG(CFG *CFGraph, const PostOrderCFGView *SortedGraph, |
| 421 | std::vector<CFGBlockInfo> &BlockInfo); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 422 | |
| 423 | protected: |
| 424 | // Get the current context index |
| 425 | unsigned getContextIndex() { return SavedContexts.size()-1; } |
| 426 | |
| 427 | // Save the current context for later replay |
| 428 | void saveContext(Stmt *S, Context C) { |
| 429 | SavedContexts.push_back(std::make_pair(S,C)); |
| 430 | } |
| 431 | |
| 432 | // Adds a new definition to the given context, and returns a new context. |
| 433 | // This method should be called when declaring a new variable. |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 434 | Context addDefinition(const NamedDecl *D, const Expr *Exp, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 435 | assert(!Ctx.contains(D)); |
| 436 | unsigned newID = VarDefinitions.size(); |
| 437 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 438 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 439 | return NewCtx; |
| 440 | } |
| 441 | |
| 442 | // Add a new reference to an existing definition. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 443 | Context addReference(const NamedDecl *D, unsigned i, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 444 | unsigned newID = VarDefinitions.size(); |
| 445 | Context NewCtx = ContextFactory.add(Ctx, D, newID); |
| 446 | VarDefinitions.push_back(VarDefinition(D, i, Ctx)); |
| 447 | return NewCtx; |
| 448 | } |
| 449 | |
| 450 | // Updates a definition only if that definition is already in the map. |
| 451 | // This method should be called when assigning to an existing variable. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 452 | Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 453 | if (Ctx.contains(D)) { |
| 454 | unsigned newID = VarDefinitions.size(); |
| 455 | Context NewCtx = ContextFactory.remove(Ctx, D); |
| 456 | NewCtx = ContextFactory.add(NewCtx, D, newID); |
| 457 | VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); |
| 458 | return NewCtx; |
| 459 | } |
| 460 | return Ctx; |
| 461 | } |
| 462 | |
| 463 | // Removes a definition from the context, but keeps the variable name |
| 464 | // 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] | 465 | Context clearDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 466 | Context NewCtx = Ctx; |
| 467 | if (NewCtx.contains(D)) { |
| 468 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 469 | NewCtx = ContextFactory.add(NewCtx, D, 0); |
| 470 | } |
| 471 | return NewCtx; |
| 472 | } |
| 473 | |
| 474 | // Remove a definition entirely frmo the context. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 475 | Context removeDefinition(const NamedDecl *D, Context Ctx) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 476 | Context NewCtx = Ctx; |
| 477 | if (NewCtx.contains(D)) { |
| 478 | NewCtx = ContextFactory.remove(NewCtx, D); |
| 479 | } |
| 480 | return NewCtx; |
| 481 | } |
| 482 | |
| 483 | Context intersectContexts(Context C1, Context C2); |
| 484 | Context createReferenceContext(Context C); |
| 485 | void intersectBackEdge(Context C1, Context C2); |
| 486 | |
| 487 | friend class VarMapBuilder; |
| 488 | }; |
| 489 | |
| 490 | |
| 491 | // This has to be defined after LocalVariableMap. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 492 | CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) { |
| 493 | return CFGBlockInfo(M.getEmptyContext()); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
| 497 | /// Visitor which builds a LocalVariableMap |
| 498 | class VarMapBuilder : public StmtVisitor<VarMapBuilder> { |
| 499 | public: |
| 500 | LocalVariableMap* VMap; |
| 501 | LocalVariableMap::Context Ctx; |
| 502 | |
| 503 | VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C) |
| 504 | : VMap(VM), Ctx(C) {} |
| 505 | |
| 506 | void VisitDeclStmt(DeclStmt *S); |
| 507 | void VisitBinaryOperator(BinaryOperator *BO); |
| 508 | }; |
| 509 | |
| 510 | |
| 511 | // Add new local variables to the variable map |
| 512 | void VarMapBuilder::VisitDeclStmt(DeclStmt *S) { |
| 513 | bool modifiedCtx = false; |
| 514 | DeclGroupRef DGrp = S->getDeclGroup(); |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 515 | for (const auto *D : DGrp) { |
| 516 | if (const auto *VD = dyn_cast_or_null<VarDecl>(D)) { |
| 517 | const Expr *E = VD->getInit(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 518 | |
| 519 | // Add local variables with trivial type to the variable map |
| 520 | QualType T = VD->getType(); |
| 521 | if (T.isTrivialType(VD->getASTContext())) { |
| 522 | Ctx = VMap->addDefinition(VD, E, Ctx); |
| 523 | modifiedCtx = true; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | if (modifiedCtx) |
| 528 | VMap->saveContext(S, Ctx); |
| 529 | } |
| 530 | |
| 531 | // Update local variable definitions in variable map |
| 532 | void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) { |
| 533 | if (!BO->isAssignmentOp()) |
| 534 | return; |
| 535 | |
| 536 | Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); |
| 537 | |
| 538 | // Update the variable map and current context. |
| 539 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) { |
| 540 | ValueDecl *VDec = DRE->getDecl(); |
| 541 | if (Ctx.lookup(VDec)) { |
| 542 | if (BO->getOpcode() == BO_Assign) |
| 543 | Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx); |
| 544 | else |
| 545 | // FIXME -- handle compound assignment operators |
| 546 | Ctx = VMap->clearDefinition(VDec, Ctx); |
| 547 | VMap->saveContext(BO, Ctx); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | // Computes the intersection of two contexts. The intersection is the |
| 554 | // set of variables which have the same definition in both contexts; |
| 555 | // variables with different definitions are discarded. |
| 556 | LocalVariableMap::Context |
| 557 | LocalVariableMap::intersectContexts(Context C1, Context C2) { |
| 558 | Context Result = C1; |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 559 | for (const auto &P : C1) { |
| 560 | const NamedDecl *Dec = P.first; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 561 | const unsigned *i2 = C2.lookup(Dec); |
| 562 | if (!i2) // variable doesn't exist on second path |
| 563 | Result = removeDefinition(Dec, Result); |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 564 | else if (*i2 != P.second) // variable exists, but has different definition |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 565 | Result = clearDefinition(Dec, Result); |
| 566 | } |
| 567 | return Result; |
| 568 | } |
| 569 | |
| 570 | // For every variable in C, create a new variable that refers to the |
| 571 | // definition in C. Return a new context that contains these new variables. |
| 572 | // (We use this for a naive implementation of SSA on loop back-edges.) |
| 573 | LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) { |
| 574 | Context Result = getEmptyContext(); |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 575 | for (const auto &P : C) |
| 576 | Result = addReference(P.first, P.second, Result); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 577 | return Result; |
| 578 | } |
| 579 | |
| 580 | // This routine also takes the intersection of C1 and C2, but it does so by |
| 581 | // altering the VarDefinitions. C1 must be the result of an earlier call to |
| 582 | // createReferenceContext. |
| 583 | void LocalVariableMap::intersectBackEdge(Context C1, Context C2) { |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 584 | for (const auto &P : C1) { |
| 585 | unsigned i1 = P.second; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 586 | VarDefinition *VDef = &VarDefinitions[i1]; |
| 587 | assert(VDef->isReference()); |
| 588 | |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 589 | const unsigned *i2 = C2.lookup(P.first); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 590 | if (!i2 || (*i2 != i1)) |
| 591 | VDef->Ref = 0; // Mark this variable as undefined |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | // Traverse the CFG in topological order, so all predecessors of a block |
| 597 | // (excluding back-edges) are visited before the block itself. At |
| 598 | // each point in the code, we calculate a Context, which holds the set of |
| 599 | // variable definitions which are visible at that point in execution. |
| 600 | // Visible variables are mapped to their definitions using an array that |
| 601 | // contains all definitions. |
| 602 | // |
| 603 | // At join points in the CFG, the set is computed as the intersection of |
| 604 | // the incoming sets along each edge, E.g. |
| 605 | // |
| 606 | // { Context | VarDefinitions } |
| 607 | // int x = 0; { x -> x1 | x1 = 0 } |
| 608 | // int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 609 | // if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... } |
| 610 | // else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... } |
| 611 | // ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... } |
| 612 | // |
| 613 | // This is essentially a simpler and more naive version of the standard SSA |
| 614 | // algorithm. Those definitions that remain in the intersection are from blocks |
| 615 | // that strictly dominate the current block. We do not bother to insert proper |
| 616 | // phi nodes, because they are not used in our analysis; instead, wherever |
| 617 | // a phi node would be required, we simply remove that definition from the |
| 618 | // context (E.g. x above). |
| 619 | // |
| 620 | // The initial traversal does not capture back-edges, so those need to be |
| 621 | // handled on a separate pass. Whenever the first pass encounters an |
| 622 | // incoming back edge, it duplicates the context, creating new definitions |
| 623 | // that refer back to the originals. (These correspond to places where SSA |
| 624 | // 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] | 625 | // 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] | 626 | // node was actually required.) E.g. |
| 627 | // |
| 628 | // { Context | VarDefinitions } |
| 629 | // int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } |
| 630 | // while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; } |
| 631 | // x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... } |
| 632 | // ... { y -> y1 | x3 = 2, x2 = 1, ... } |
| 633 | // |
| 634 | void LocalVariableMap::traverseCFG(CFG *CFGraph, |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 635 | const PostOrderCFGView *SortedGraph, |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 636 | std::vector<CFGBlockInfo> &BlockInfo) { |
| 637 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
| 638 | |
| 639 | CtxIndices.resize(CFGraph->getNumBlockIDs()); |
| 640 | |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 641 | for (const auto *CurrBlock : *SortedGraph) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 642 | int CurrBlockID = CurrBlock->getBlockID(); |
| 643 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
| 644 | |
| 645 | VisitedBlocks.insert(CurrBlock); |
| 646 | |
| 647 | // Calculate the entry context for the current block |
| 648 | bool HasBackEdges = false; |
| 649 | bool CtxInit = true; |
| 650 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 651 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 652 | // if *PI -> CurrBlock is a back edge, so skip it |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 653 | if (*PI == nullptr || !VisitedBlocks.alreadySet(*PI)) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 654 | HasBackEdges = true; |
| 655 | continue; |
| 656 | } |
| 657 | |
| 658 | int PrevBlockID = (*PI)->getBlockID(); |
| 659 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 660 | |
| 661 | if (CtxInit) { |
| 662 | CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext; |
| 663 | CtxInit = false; |
| 664 | } |
| 665 | else { |
| 666 | CurrBlockInfo->EntryContext = |
| 667 | intersectContexts(CurrBlockInfo->EntryContext, |
| 668 | PrevBlockInfo->ExitContext); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Duplicate the context if we have back-edges, so we can call |
| 673 | // intersectBackEdges later. |
| 674 | if (HasBackEdges) |
| 675 | CurrBlockInfo->EntryContext = |
| 676 | createReferenceContext(CurrBlockInfo->EntryContext); |
| 677 | |
| 678 | // Create a starting context index for the current block |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 679 | saveContext(nullptr, CurrBlockInfo->EntryContext); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 680 | CurrBlockInfo->EntryIndex = getContextIndex(); |
| 681 | |
| 682 | // Visit all the statements in the basic block. |
| 683 | VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext); |
| 684 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 685 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 686 | switch (BI->getKind()) { |
| 687 | case CFGElement::Statement: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 688 | CFGStmt CS = BI->castAs<CFGStmt>(); |
| 689 | VMapBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | default: |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | CurrBlockInfo->ExitContext = VMapBuilder.Ctx; |
| 697 | |
| 698 | // Mark variables on back edges as "unknown" if they've been changed. |
| 699 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 700 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 701 | // if CurrBlock -> *SI is *not* a back edge |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 702 | if (*SI == nullptr || !VisitedBlocks.alreadySet(*SI)) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 703 | continue; |
| 704 | |
| 705 | CFGBlock *FirstLoopBlock = *SI; |
| 706 | Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext; |
| 707 | Context LoopEnd = CurrBlockInfo->ExitContext; |
| 708 | intersectBackEdge(LoopBegin, LoopEnd); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // Put an extra entry at the end of the indexed context array |
| 713 | unsigned exitID = CFGraph->getExit().getBlockID(); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 714 | saveContext(nullptr, BlockInfo[exitID].ExitContext); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 717 | /// Find the appropriate source locations to use when producing diagnostics for |
| 718 | /// each block in the CFG. |
| 719 | static void findBlockLocations(CFG *CFGraph, |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 720 | const PostOrderCFGView *SortedGraph, |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 721 | std::vector<CFGBlockInfo> &BlockInfo) { |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 722 | for (const auto *CurrBlock : *SortedGraph) { |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 723 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()]; |
| 724 | |
| 725 | // Find the source location of the last statement in the block, if the |
| 726 | // block is not empty. |
| 727 | if (const Stmt *S = CurrBlock->getTerminator()) { |
| 728 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart(); |
| 729 | } else { |
| 730 | for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(), |
| 731 | BE = CurrBlock->rend(); BI != BE; ++BI) { |
| 732 | // FIXME: Handle other CFGElement kinds. |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 733 | if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { |
| 734 | CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart(); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (!CurrBlockInfo->ExitLoc.isInvalid()) { |
| 741 | // This block contains at least one statement. Find the source location |
| 742 | // of the first statement in the block. |
| 743 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 744 | BE = CurrBlock->end(); BI != BE; ++BI) { |
| 745 | // FIXME: Handle other CFGElement kinds. |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 746 | if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { |
| 747 | CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart(); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | } |
| 751 | } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() && |
| 752 | CurrBlock != &CFGraph->getExit()) { |
| 753 | // The block is empty, and has a single predecessor. Use its exit |
| 754 | // location. |
| 755 | CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = |
| 756 | BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc; |
| 757 | } |
| 758 | } |
| 759 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 760 | |
| 761 | /// \brief Class which implements the core thread safety analysis routines. |
| 762 | class ThreadSafetyAnalyzer { |
| 763 | friend class BuildLockset; |
| 764 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 765 | llvm::BumpPtrAllocator Bpa; |
| 766 | threadSafety::til::MemRegionRef Arena; |
| 767 | threadSafety::SExprBuilder SxBuilder; |
| 768 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 769 | ThreadSafetyHandler &Handler; |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 770 | const CXXMethodDecl *CurrentMethod; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 771 | LocalVariableMap LocalVarMap; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 772 | FactManager FactMan; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 773 | std::vector<CFGBlockInfo> BlockInfo; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 774 | |
| 775 | public: |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 776 | ThreadSafetyAnalyzer(ThreadSafetyHandler &H) |
| 777 | : Arena(&Bpa), SxBuilder(Arena), Handler(H) {} |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 778 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 779 | bool inCurrentScope(const CapabilityExpr &CapE); |
| 780 | |
| 781 | void addLock(FactSet &FSet, const FactEntry &Entry, StringRef DiagKind, |
| 782 | bool ReqAttr = false); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 783 | void removeLock(FactSet &FSet, const CapabilityExpr &CapE, |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 784 | SourceLocation UnlockLoc, bool FullyRemove, LockKind Kind, |
| 785 | StringRef DiagKind); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 786 | |
| 787 | template <typename AttrType> |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 788 | void getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, Expr *Exp, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 789 | const NamedDecl *D, VarDecl *SelfDecl = nullptr); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 790 | |
| 791 | template <class AttrType> |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 792 | void getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, Expr *Exp, |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 793 | const NamedDecl *D, |
| 794 | const CFGBlock *PredBlock, const CFGBlock *CurrBlock, |
| 795 | Expr *BrE, bool Neg); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 796 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 797 | const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C, |
| 798 | bool &Negate); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 799 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 800 | void getEdgeLockset(FactSet &Result, const FactSet &ExitSet, |
| 801 | const CFGBlock* PredBlock, |
| 802 | const CFGBlock *CurrBlock); |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 803 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 804 | void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, |
| 805 | SourceLocation JoinLoc, |
| 806 | LockErrorKind LEK1, LockErrorKind LEK2, |
| 807 | bool Modify=true); |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 808 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 809 | void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, |
| 810 | SourceLocation JoinLoc, LockErrorKind LEK1, |
| 811 | bool Modify=true) { |
| 812 | intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify); |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 813 | } |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 814 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 815 | void runAnalysis(AnalysisDeclContext &AC); |
| 816 | }; |
| 817 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 818 | /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs. |
| 819 | static const ValueDecl *getValueDecl(const Expr *Exp) { |
| 820 | if (const auto *CE = dyn_cast<ImplicitCastExpr>(Exp)) |
| 821 | return getValueDecl(CE->getSubExpr()); |
| 822 | |
| 823 | if (const auto *DR = dyn_cast<DeclRefExpr>(Exp)) |
| 824 | return DR->getDecl(); |
| 825 | |
| 826 | if (const auto *ME = dyn_cast<MemberExpr>(Exp)) |
| 827 | return ME->getMemberDecl(); |
| 828 | |
| 829 | return nullptr; |
| 830 | } |
| 831 | |
| 832 | template <typename Ty> |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 833 | class has_arg_iterator_range { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 834 | typedef char yes[1]; |
| 835 | typedef char no[2]; |
| 836 | |
| 837 | template <typename Inner> |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 838 | static yes& test(Inner *I, decltype(I->args()) * = nullptr); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 839 | |
| 840 | template <typename> |
| 841 | static no& test(...); |
| 842 | |
| 843 | public: |
| 844 | static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); |
| 845 | }; |
| 846 | |
| 847 | static StringRef ClassifyDiagnostic(const CapabilityAttr *A) { |
| 848 | return A->getName(); |
| 849 | } |
| 850 | |
| 851 | static StringRef ClassifyDiagnostic(QualType VDT) { |
| 852 | // We need to look at the declaration of the type of the value to determine |
| 853 | // which it is. The type should either be a record or a typedef, or a pointer |
| 854 | // or reference thereof. |
| 855 | if (const auto *RT = VDT->getAs<RecordType>()) { |
| 856 | if (const auto *RD = RT->getDecl()) |
| 857 | if (const auto *CA = RD->getAttr<CapabilityAttr>()) |
| 858 | return ClassifyDiagnostic(CA); |
| 859 | } else if (const auto *TT = VDT->getAs<TypedefType>()) { |
| 860 | if (const auto *TD = TT->getDecl()) |
| 861 | if (const auto *CA = TD->getAttr<CapabilityAttr>()) |
| 862 | return ClassifyDiagnostic(CA); |
| 863 | } else if (VDT->isPointerType() || VDT->isReferenceType()) |
| 864 | return ClassifyDiagnostic(VDT->getPointeeType()); |
| 865 | |
| 866 | return "mutex"; |
| 867 | } |
| 868 | |
| 869 | static StringRef ClassifyDiagnostic(const ValueDecl *VD) { |
| 870 | assert(VD && "No ValueDecl passed"); |
| 871 | |
| 872 | // The ValueDecl is the declaration of a mutex or role (hopefully). |
| 873 | return ClassifyDiagnostic(VD->getType()); |
| 874 | } |
| 875 | |
| 876 | template <typename AttrTy> |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 877 | static typename std::enable_if<!has_arg_iterator_range<AttrTy>::value, |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 878 | StringRef>::type |
| 879 | ClassifyDiagnostic(const AttrTy *A) { |
| 880 | if (const ValueDecl *VD = getValueDecl(A->getArg())) |
| 881 | return ClassifyDiagnostic(VD); |
| 882 | return "mutex"; |
| 883 | } |
| 884 | |
| 885 | template <typename AttrTy> |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 886 | static typename std::enable_if<has_arg_iterator_range<AttrTy>::value, |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 887 | StringRef>::type |
| 888 | ClassifyDiagnostic(const AttrTy *A) { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 889 | for (const auto *Arg : A->args()) { |
| 890 | if (const ValueDecl *VD = getValueDecl(Arg)) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 891 | return ClassifyDiagnostic(VD); |
| 892 | } |
| 893 | return "mutex"; |
| 894 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 895 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 896 | |
| 897 | inline bool ThreadSafetyAnalyzer::inCurrentScope(const CapabilityExpr &CapE) { |
| 898 | if (!CurrentMethod) |
| 899 | return false; |
| 900 | if (auto *P = dyn_cast_or_null<til::Project>(CapE.sexpr())) { |
| 901 | auto *VD = P->clangDecl(); |
| 902 | if (VD) |
| 903 | return VD->getDeclContext() == CurrentMethod->getDeclContext(); |
| 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 909 | /// \brief Add a new lock to the lockset, warning if the lock is already there. |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 910 | /// \param ReqAttr -- true if this is part of an initial Requires attribute. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 911 | void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const FactEntry &Entry, |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 912 | StringRef DiagKind, bool ReqAttr) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 913 | if (Entry.shouldIgnore()) |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 914 | return; |
| 915 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 916 | if (!ReqAttr && !Entry.negative()) { |
| 917 | // look for the negative capability, and remove it from the fact set. |
| 918 | CapabilityExpr NegC = !Entry; |
| 919 | FactEntry *Nen = FSet.findLock(FactMan, NegC); |
| 920 | if (Nen) { |
| 921 | FSet.removeLock(FactMan, NegC); |
| 922 | } |
| 923 | else { |
| 924 | if (inCurrentScope(Entry) && !Entry.asserted()) |
| 925 | Handler.handleNegativeNotHeld(DiagKind, Entry.toString(), |
| 926 | NegC.toString(), Entry.loc()); |
| 927 | } |
| 928 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 929 | |
| 930 | // FIXME: deal with acquired before/after annotations. |
| 931 | // FIXME: Don't always warn when we have support for reentrant locks. |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 932 | if (FSet.findLock(FactMan, Entry)) { |
| 933 | if (!Entry.asserted()) |
| 934 | Handler.handleDoubleLock(DiagKind, Entry.toString(), Entry.loc()); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 935 | } else { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 936 | FSet.addLock(FactMan, Entry); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 940 | |
| 941 | /// \brief Remove a lock from the lockset, warning if the lock is not there. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 942 | /// \param UnlockLoc The source location of the unlock (only used in error msg) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 943 | void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, const CapabilityExpr &Cp, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 944 | SourceLocation UnlockLoc, |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 945 | bool FullyRemove, LockKind ReceivedKind, |
| 946 | StringRef DiagKind) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 947 | if (Cp.shouldIgnore()) |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 948 | return; |
| 949 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 950 | const FactEntry *LDat = FSet.findLock(FactMan, Cp); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 951 | if (!LDat) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 952 | Handler.handleUnmatchedUnlock(DiagKind, Cp.toString(), UnlockLoc); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 953 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 954 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 955 | |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 956 | // Generic lock removal doesn't care about lock kind mismatches, but |
| 957 | // otherwise diagnose when the lock kinds are mismatched. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 958 | if (ReceivedKind != LK_Generic && LDat->kind() != ReceivedKind) { |
| 959 | Handler.handleIncorrectUnlockKind(DiagKind, Cp.toString(), |
| 960 | LDat->kind(), ReceivedKind, UnlockLoc); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 961 | } |
| 962 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 963 | if (LDat->underlying()) { |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 964 | assert(!Cp.negative() && "Managing object cannot be negative."); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 965 | CapabilityExpr UnderCp(LDat->underlying(), false); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 966 | FactEntry UnderEntry(!UnderCp, LK_Exclusive, UnlockLoc); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 967 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 968 | // This is scoped lockable object, which manages the real mutex. |
| 969 | if (FullyRemove) { |
| 970 | // We're destroying the managing object. |
| 971 | // Remove the underlying mutex if it exists; but don't warn. |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 972 | if (FSet.findLock(FactMan, UnderCp)) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 973 | FSet.removeLock(FactMan, UnderCp); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 974 | FSet.addLock(FactMan, UnderEntry); |
| 975 | } |
| 976 | FSet.removeLock(FactMan, Cp); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 977 | } else { |
| 978 | // We're releasing the underlying mutex, but not destroying the |
| 979 | // managing object. Warn on dual release. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 980 | if (!FSet.findLock(FactMan, UnderCp)) { |
| 981 | Handler.handleUnmatchedUnlock(DiagKind, UnderCp.toString(), UnlockLoc); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 982 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 983 | FSet.removeLock(FactMan, UnderCp); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 984 | FSet.addLock(FactMan, UnderEntry); |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 985 | } |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 986 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 987 | } |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 988 | // else !LDat->underlying() |
| 989 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 990 | FSet.removeLock(FactMan, Cp); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 991 | if (!Cp.negative()) { |
| 992 | FSet.addLock(FactMan, FactEntry(!Cp, LK_Exclusive, UnlockLoc)); |
| 993 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 994 | } |
| 995 | |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 996 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 997 | /// \brief Extract the list of mutexIDs from the attribute on an expression, |
| 998 | /// and push them onto Mtxs, discarding any duplicates. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 999 | template <typename AttrType> |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1000 | void ThreadSafetyAnalyzer::getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, |
DeLesley Hutchins | 1fe8856 | 2012-10-05 22:38:19 +0000 | [diff] [blame] | 1001 | Expr *Exp, const NamedDecl *D, |
| 1002 | VarDecl *SelfDecl) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1003 | if (Attr->args_size() == 0) { |
| 1004 | // The mutex held is the "this" object. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1005 | CapabilityExpr Cp = SxBuilder.translateAttrExpr(nullptr, D, Exp, SelfDecl); |
| 1006 | if (Cp.isInvalid()) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1007 | warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr)); |
| 1008 | return; |
| 1009 | } |
| 1010 | //else |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1011 | if (!Cp.shouldIgnore()) |
| 1012 | Mtxs.push_back_nodup(Cp); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1013 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1014 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1015 | |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 1016 | for (const auto *Arg : Attr->args()) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1017 | CapabilityExpr Cp = SxBuilder.translateAttrExpr(Arg, D, Exp, SelfDecl); |
| 1018 | if (Cp.isInvalid()) { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1019 | warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr)); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1020 | continue; |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1021 | } |
| 1022 | //else |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1023 | if (!Cp.shouldIgnore()) |
| 1024 | Mtxs.push_back_nodup(Cp); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1025 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1029 | /// \brief Extract the list of mutexIDs from a trylock attribute. If the |
| 1030 | /// trylock applies to the given edge, then push them onto Mtxs, discarding |
| 1031 | /// any duplicates. |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1032 | template <class AttrType> |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1033 | void ThreadSafetyAnalyzer::getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1034 | Expr *Exp, const NamedDecl *D, |
| 1035 | const CFGBlock *PredBlock, |
| 1036 | const CFGBlock *CurrBlock, |
| 1037 | Expr *BrE, bool Neg) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1038 | // Find out which branch has the lock |
Aaron Ballman | 2f3fc6b | 2014-05-14 13:03:55 +0000 | [diff] [blame] | 1039 | bool branch = false; |
| 1040 | if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1041 | branch = BLE->getValue(); |
Aaron Ballman | 2f3fc6b | 2014-05-14 13:03:55 +0000 | [diff] [blame] | 1042 | else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1043 | branch = ILE->getValue().getBoolValue(); |
Aaron Ballman | 2f3fc6b | 2014-05-14 13:03:55 +0000 | [diff] [blame] | 1044 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1045 | int branchnum = branch ? 0 : 1; |
Aaron Ballman | 2f3fc6b | 2014-05-14 13:03:55 +0000 | [diff] [blame] | 1046 | if (Neg) |
| 1047 | branchnum = !branchnum; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1048 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1049 | // If we've taken the trylock branch, then add the lock |
| 1050 | int i = 0; |
| 1051 | for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(), |
| 1052 | SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) { |
Aaron Ballman | 2f3fc6b | 2014-05-14 13:03:55 +0000 | [diff] [blame] | 1053 | if (*SI == CurrBlock && i == branchnum) |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1054 | getMutexIDs(Mtxs, Attr, Exp, D); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1055 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1059 | bool getStaticBooleanValue(Expr* E, bool& TCond) { |
| 1060 | if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) { |
| 1061 | TCond = false; |
| 1062 | return true; |
| 1063 | } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) { |
| 1064 | TCond = BLE->getValue(); |
| 1065 | return true; |
| 1066 | } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) { |
| 1067 | TCond = ILE->getValue().getBoolValue(); |
| 1068 | return true; |
| 1069 | } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
| 1070 | return getStaticBooleanValue(CE->getSubExpr(), TCond); |
| 1071 | } |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1076 | // If Cond can be traced back to a function call, return the call expression. |
| 1077 | // The negate variable should be called with false, and will be set to true |
| 1078 | // if the function call is negated, e.g. if (!mu.tryLock(...)) |
| 1079 | const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond, |
| 1080 | LocalVarContext C, |
| 1081 | bool &Negate) { |
| 1082 | if (!Cond) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1083 | return nullptr; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) { |
| 1086 | return CallExp; |
| 1087 | } |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1088 | else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) { |
| 1089 | return getTrylockCallExpr(PE->getSubExpr(), C, Negate); |
| 1090 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1091 | else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) { |
| 1092 | return getTrylockCallExpr(CE->getSubExpr(), C, Negate); |
| 1093 | } |
DeLesley Hutchins | 93b1b03 | 2012-09-05 20:01:16 +0000 | [diff] [blame] | 1094 | else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) { |
| 1095 | return getTrylockCallExpr(EWC->getSubExpr(), C, Negate); |
| 1096 | } |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1097 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) { |
| 1098 | const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C); |
| 1099 | return getTrylockCallExpr(E, C, Negate); |
| 1100 | } |
| 1101 | else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) { |
| 1102 | if (UOP->getOpcode() == UO_LNot) { |
| 1103 | Negate = !Negate; |
| 1104 | return getTrylockCallExpr(UOP->getSubExpr(), C, Negate); |
| 1105 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1106 | return nullptr; |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1107 | } |
| 1108 | else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) { |
| 1109 | if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) { |
| 1110 | if (BOP->getOpcode() == BO_NE) |
| 1111 | Negate = !Negate; |
| 1112 | |
| 1113 | bool TCond = false; |
| 1114 | if (getStaticBooleanValue(BOP->getRHS(), TCond)) { |
| 1115 | if (!TCond) Negate = !Negate; |
| 1116 | return getTrylockCallExpr(BOP->getLHS(), C, Negate); |
| 1117 | } |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1118 | TCond = false; |
| 1119 | if (getStaticBooleanValue(BOP->getLHS(), TCond)) { |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1120 | if (!TCond) Negate = !Negate; |
| 1121 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1122 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1123 | return nullptr; |
DeLesley Hutchins | 868830f | 2012-07-10 21:47:55 +0000 | [diff] [blame] | 1124 | } |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1125 | if (BOP->getOpcode() == BO_LAnd) { |
| 1126 | // LHS must have been evaluated in a different block. |
| 1127 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1128 | } |
| 1129 | if (BOP->getOpcode() == BO_LOr) { |
| 1130 | return getTrylockCallExpr(BOP->getRHS(), C, Negate); |
| 1131 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1132 | return nullptr; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1133 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1134 | return nullptr; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1138 | /// \brief Find the lockset that holds on the edge between PredBlock |
| 1139 | /// and CurrBlock. The edge set is the exit set of PredBlock (passed |
| 1140 | /// as the ExitSet parameter) plus any trylocks, which are conditionally held. |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1141 | void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result, |
| 1142 | const FactSet &ExitSet, |
| 1143 | const CFGBlock *PredBlock, |
| 1144 | const CFGBlock *CurrBlock) { |
| 1145 | Result = ExitSet; |
| 1146 | |
DeLesley Hutchins | 9f5193c | 2013-08-15 23:06:33 +0000 | [diff] [blame] | 1147 | const Stmt *Cond = PredBlock->getTerminatorCondition(); |
| 1148 | if (!Cond) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1149 | return; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1150 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1151 | bool Negate = false; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1152 | const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()]; |
| 1153 | const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext; |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1154 | StringRef CapDiagKind = "mutex"; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1155 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1156 | CallExpr *Exp = |
| 1157 | const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate)); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1158 | if (!Exp) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1159 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1160 | |
| 1161 | NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1162 | if(!FunDecl || !FunDecl->hasAttrs()) |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1163 | return; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1164 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1165 | CapExprSet ExclusiveLocksToAdd; |
| 1166 | CapExprSet SharedLocksToAdd; |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1167 | |
| 1168 | // If the condition is a call to a Trylock function, then grab the attributes |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 1169 | for (auto *Attr : FunDecl->getAttrs()) { |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1170 | switch (Attr->getKind()) { |
| 1171 | case attr::ExclusiveTrylockFunction: { |
| 1172 | ExclusiveTrylockFunctionAttr *A = |
| 1173 | cast<ExclusiveTrylockFunctionAttr>(Attr); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1174 | getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl, |
| 1175 | PredBlock, CurrBlock, A->getSuccessValue(), Negate); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1176 | CapDiagKind = ClassifyDiagnostic(A); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1177 | break; |
| 1178 | } |
| 1179 | case attr::SharedTrylockFunction: { |
| 1180 | SharedTrylockFunctionAttr *A = |
| 1181 | cast<SharedTrylockFunctionAttr>(Attr); |
DeLesley Hutchins | fcb0ffa | 2012-09-20 23:14:43 +0000 | [diff] [blame] | 1182 | getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl, |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1183 | PredBlock, CurrBlock, A->getSuccessValue(), Negate); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1184 | CapDiagKind = ClassifyDiagnostic(A); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | } |
| 1187 | default: |
| 1188 | break; |
| 1189 | } |
| 1190 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Add and remove locks. |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1193 | SourceLocation Loc = Exp->getExprLoc(); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1194 | for (const auto &ExclusiveLockToAdd : ExclusiveLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1195 | addLock(Result, FactEntry(ExclusiveLockToAdd, LK_Exclusive, Loc), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1196 | CapDiagKind); |
| 1197 | for (const auto &SharedLockToAdd : SharedLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1198 | addLock(Result, FactEntry(SharedLockToAdd, LK_Shared, Loc), |
| 1199 | CapDiagKind); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1202 | /// \brief We use this class to visit different types of expressions in |
| 1203 | /// CFGBlocks, and build up the lockset. |
| 1204 | /// An expression may cause us to add or remove locks from the lockset, or else |
| 1205 | /// output error messages related to missing locks. |
| 1206 | /// FIXME: In future, we may be able to not inherit from a visitor. |
| 1207 | class BuildLockset : public StmtVisitor<BuildLockset> { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1208 | friend class ThreadSafetyAnalyzer; |
| 1209 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1210 | ThreadSafetyAnalyzer *Analyzer; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1211 | FactSet FSet; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1212 | LocalVariableMap::Context LVarCtx; |
| 1213 | unsigned CtxIndex; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1214 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1215 | // helper functions |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1216 | void warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, AccessKind AK, |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1217 | Expr *MutexExp, ProtectedOperationKind POK, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1218 | StringRef DiagKind, SourceLocation Loc); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1219 | void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp, |
| 1220 | StringRef DiagKind); |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1221 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1222 | void checkAccess(const Expr *Exp, AccessKind AK); |
| 1223 | void checkPtAccess(const Expr *Exp, AccessKind AK); |
| 1224 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1225 | void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = nullptr); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1226 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1227 | public: |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1228 | BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info) |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1229 | : StmtVisitor<BuildLockset>(), |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1230 | Analyzer(Anlzr), |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1231 | FSet(Info.EntrySet), |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1232 | LVarCtx(Info.EntryContext), |
| 1233 | CtxIndex(Info.EntryIndex) |
| 1234 | {} |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1235 | |
| 1236 | void VisitUnaryOperator(UnaryOperator *UO); |
| 1237 | void VisitBinaryOperator(BinaryOperator *BO); |
| 1238 | void VisitCastExpr(CastExpr *CE); |
DeLesley Hutchins | 714296c | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1239 | void VisitCallExpr(CallExpr *Exp); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1240 | void VisitCXXConstructExpr(CXXConstructExpr *Exp); |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1241 | void VisitDeclStmt(DeclStmt *S); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1242 | }; |
| 1243 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1244 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1245 | /// \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] | 1246 | /// of at least the passed in AccessKind. |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1247 | void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1248 | AccessKind AK, Expr *MutexExp, |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1249 | ProtectedOperationKind POK, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1250 | StringRef DiagKind, SourceLocation Loc) { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1251 | LockKind LK = getLockKindFromAccessKind(AK); |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1252 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1253 | CapabilityExpr Cp = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp); |
| 1254 | if (Cp.isInvalid()) { |
| 1255 | warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind); |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1256 | return; |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1257 | } else if (Cp.shouldIgnore()) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } |
| 1260 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1261 | if (Cp.negative()) { |
| 1262 | // Negative capabilities act like locks excluded |
| 1263 | FactEntry *LDat = FSet.findLock(Analyzer->FactMan, !Cp); |
| 1264 | if (LDat) { |
| 1265 | Analyzer->Handler.handleFunExcludesLock( |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1266 | DiagKind, D->getNameAsString(), (!Cp).toString(), Loc); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | |
| 1270 | // If this does not refer to a negative capability in the same class, |
| 1271 | // then stop here. |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1272 | if (!Analyzer->inCurrentScope(Cp)) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1273 | return; |
| 1274 | |
| 1275 | // Otherwise the negative requirement must be propagated to the caller. |
| 1276 | LDat = FSet.findLock(Analyzer->FactMan, Cp); |
| 1277 | if (!LDat) { |
| 1278 | Analyzer->Handler.handleMutexNotHeld("", D, POK, Cp.toString(), |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1279 | LK_Shared, Loc); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1280 | } |
| 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | FactEntry* LDat = FSet.findLockUniv(Analyzer->FactMan, Cp); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1285 | bool NoError = true; |
| 1286 | if (!LDat) { |
| 1287 | // No exact match found. Look for a partial match. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1288 | LDat = FSet.findPartialMatch(Analyzer->FactMan, Cp); |
| 1289 | if (LDat) { |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1290 | // Warn that there's no precise match. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1291 | std::string PartMatchStr = LDat->toString(); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1292 | StringRef PartMatchName(PartMatchStr); |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1293 | Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(), |
| 1294 | LK, Loc, &PartMatchName); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | // Warn that there's no match at all. |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1297 | Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(), |
| 1298 | LK, Loc); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1299 | } |
| 1300 | NoError = false; |
| 1301 | } |
| 1302 | // Make sure the mutex we found is the right kind. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1303 | if (NoError && LDat && !LDat->isAtLeast(LK)) { |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1304 | Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(), |
| 1305 | LK, Loc); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1306 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1309 | /// \brief Warn if the LSet contains the given lock. |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1310 | void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1311 | Expr *MutexExp, StringRef DiagKind) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1312 | CapabilityExpr Cp = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp); |
| 1313 | if (Cp.isInvalid()) { |
| 1314 | warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind); |
| 1315 | return; |
| 1316 | } else if (Cp.shouldIgnore()) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
| 1319 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1320 | FactEntry* LDat = FSet.findLock(Analyzer->FactMan, Cp); |
| 1321 | if (LDat) { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1322 | Analyzer->Handler.handleFunExcludesLock( |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1323 | DiagKind, D->getNameAsString(), Cp.toString(), Exp->getExprLoc()); |
| 1324 | } |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1327 | /// \brief Checks guarded_by and pt_guarded_by attributes. |
| 1328 | /// Whenever we identify an access (read or write) to a DeclRefExpr that is |
| 1329 | /// marked with guarded_by, we must ensure the appropriate mutexes are held. |
| 1330 | /// Similarly, we check if the access is to an expression that dereferences |
| 1331 | /// a pointer marked with pt_guarded_by. |
| 1332 | void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) { |
| 1333 | Exp = Exp->IgnoreParenCasts(); |
| 1334 | |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1335 | SourceLocation Loc = Exp->getExprLoc(); |
| 1336 | |
| 1337 | if (Analyzer->Handler.issueBetaWarnings()) { |
| 1338 | // Local variables of reference type cannot be re-assigned; |
| 1339 | // map them to their initializer. |
| 1340 | while (auto *DRE = dyn_cast<DeclRefExpr>(Exp)) { |
| 1341 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 1342 | if (VD && VD->isLocalVarDecl() && VD->getType()->isReferenceType()) { |
| 1343 | if (auto *E = VD->getInit()) { |
| 1344 | Exp = E; |
| 1345 | continue; |
| 1346 | } |
| 1347 | } |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1352 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp)) { |
| 1353 | // For dereferences |
| 1354 | if (UO->getOpcode() == clang::UO_Deref) |
| 1355 | checkPtAccess(UO->getSubExpr(), AK); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1356 | return; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1357 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1358 | |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1359 | if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(Exp)) { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1360 | checkPtAccess(AE->getLHS(), AK); |
| 1361 | return; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1364 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { |
| 1365 | if (ME->isArrow()) |
| 1366 | checkPtAccess(ME->getBase(), AK); |
| 1367 | else |
| 1368 | checkAccess(ME->getBase(), AK); |
DeLesley Hutchins | 0cfa1a5 | 2012-12-08 03:46:30 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1371 | const ValueDecl *D = getValueDecl(Exp); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1372 | if (!D || !D->hasAttrs()) |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1373 | return; |
| 1374 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1375 | if (D->hasAttr<GuardedVarAttr>() && FSet.isEmpty(Analyzer->FactMan)) { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1376 | Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarAccess, AK, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1377 | Loc); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1378 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1379 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1380 | for (const auto *I : D->specific_attrs<GuardedByAttr>()) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1381 | warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarAccess, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1382 | ClassifyDiagnostic(I), Loc); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1385 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1386 | /// \brief Checks pt_guarded_by and pt_guarded_var attributes. |
| 1387 | void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1388 | while (true) { |
| 1389 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { |
| 1390 | Exp = PE->getSubExpr(); |
| 1391 | continue; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1392 | } |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1393 | if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) { |
| 1394 | if (CE->getCastKind() == CK_ArrayToPointerDecay) { |
| 1395 | // If it's an actual array, and not a pointer, then it's elements |
| 1396 | // are protected by GUARDED_BY, not PT_GUARDED_BY; |
| 1397 | checkAccess(CE->getSubExpr(), AK); |
| 1398 | return; |
| 1399 | } |
| 1400 | Exp = CE->getSubExpr(); |
| 1401 | continue; |
| 1402 | } |
| 1403 | break; |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1404 | } |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1405 | |
| 1406 | const ValueDecl *D = getValueDecl(Exp); |
| 1407 | if (!D || !D->hasAttrs()) |
| 1408 | return; |
| 1409 | |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1410 | if (D->hasAttr<PtGuardedVarAttr>() && FSet.isEmpty(Analyzer->FactMan)) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1411 | Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarDereference, AK, |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1412 | Exp->getExprLoc()); |
| 1413 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1414 | for (auto const *I : D->specific_attrs<PtGuardedByAttr>()) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1415 | warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarDereference, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1416 | ClassifyDiagnostic(I), Exp->getExprLoc()); |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1419 | /// \brief Process a function call, method call, constructor call, |
| 1420 | /// or destructor call. This involves looking at the attributes on the |
| 1421 | /// corresponding function/method/constructor/destructor, issuing warnings, |
| 1422 | /// and updating the locksets accordingly. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1423 | /// |
| 1424 | /// FIXME: For classes annotated with one of the guarded annotations, we need |
| 1425 | /// to treat const method calls as reads and non-const method calls as writes, |
| 1426 | /// and check that the appropriate locks are held. Non-const method calls with |
| 1427 | /// the same signature as const method calls can be also treated as reads. |
| 1428 | /// |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1429 | void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) { |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1430 | SourceLocation Loc = Exp->getExprLoc(); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1431 | const AttrVec &ArgAttrs = D->getAttrs(); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1432 | CapExprSet ExclusiveLocksToAdd, SharedLocksToAdd; |
| 1433 | CapExprSet ExclusiveLocksToRemove, SharedLocksToRemove, GenericLocksToRemove; |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1434 | StringRef CapDiagKind = "mutex"; |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1435 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1436 | for(unsigned i = 0; i < ArgAttrs.size(); ++i) { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1437 | Attr *At = const_cast<Attr*>(ArgAttrs[i]); |
| 1438 | switch (At->getKind()) { |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 1439 | // When we encounter a lock function, we need to add the lock to our |
| 1440 | // lockset. |
| 1441 | case attr::AcquireCapability: { |
| 1442 | auto *A = cast<AcquireCapabilityAttr>(At); |
| 1443 | Analyzer->getMutexIDs(A->isShared() ? SharedLocksToAdd |
| 1444 | : ExclusiveLocksToAdd, |
| 1445 | A, Exp, D, VD); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1446 | |
| 1447 | CapDiagKind = ClassifyDiagnostic(A); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1448 | break; |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1449 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1450 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1451 | // An assert will add a lock to the lockset, but will not generate |
| 1452 | // a warning if it is already there, and will not generate a warning |
| 1453 | // if it is not removed. |
| 1454 | case attr::AssertExclusiveLock: { |
| 1455 | AssertExclusiveLockAttr *A = cast<AssertExclusiveLockAttr>(At); |
| 1456 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1457 | CapExprSet AssertLocks; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1458 | Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1459 | for (const auto &AssertLock : AssertLocks) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1460 | Analyzer->addLock(FSet, FactEntry(AssertLock, LK_Exclusive, Loc, |
| 1461 | false, true), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1462 | ClassifyDiagnostic(A)); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1463 | break; |
| 1464 | } |
| 1465 | case attr::AssertSharedLock: { |
| 1466 | AssertSharedLockAttr *A = cast<AssertSharedLockAttr>(At); |
| 1467 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1468 | CapExprSet AssertLocks; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1469 | Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1470 | for (const auto &AssertLock : AssertLocks) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1471 | Analyzer->addLock(FSet, FactEntry(AssertLock, LK_Shared, Loc, |
| 1472 | false, true), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1473 | ClassifyDiagnostic(A)); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1474 | break; |
| 1475 | } |
| 1476 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1477 | // When we encounter an unlock function, we need to remove unlocked |
| 1478 | // 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] | 1479 | case attr::ReleaseCapability: { |
| 1480 | auto *A = cast<ReleaseCapabilityAttr>(At); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1481 | if (A->isGeneric()) |
| 1482 | Analyzer->getMutexIDs(GenericLocksToRemove, A, Exp, D, VD); |
| 1483 | else if (A->isShared()) |
| 1484 | Analyzer->getMutexIDs(SharedLocksToRemove, A, Exp, D, VD); |
| 1485 | else |
| 1486 | Analyzer->getMutexIDs(ExclusiveLocksToRemove, A, Exp, D, VD); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1487 | |
| 1488 | CapDiagKind = ClassifyDiagnostic(A); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | } |
| 1491 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 1492 | case attr::RequiresCapability: { |
| 1493 | RequiresCapabilityAttr *A = cast<RequiresCapabilityAttr>(At); |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 1494 | for (auto *Arg : A->args()) |
| 1495 | warnIfMutexNotHeld(D, Exp, A->isShared() ? AK_Read : AK_Written, Arg, |
DeLesley Hutchins | 4133b13 | 2014-08-14 19:17:06 +0000 | [diff] [blame] | 1496 | POK_FunctionCall, ClassifyDiagnostic(A), |
| 1497 | Exp->getExprLoc()); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | case attr::LocksExcluded: { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1502 | LocksExcludedAttr *A = cast<LocksExcludedAttr>(At); |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 1503 | for (auto *Arg : A->args()) |
| 1504 | warnIfMutexHeld(D, Exp, Arg, ClassifyDiagnostic(A)); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 1508 | // Ignore attributes unrelated to thread-safety |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1509 | default: |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Figure out if we're calling the constructor of scoped lockable class |
| 1515 | bool isScopedVar = false; |
| 1516 | if (VD) { |
| 1517 | if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) { |
| 1518 | const CXXRecordDecl* PD = CD->getParent(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1519 | if (PD && PD->hasAttr<ScopedLockableAttr>()) |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1520 | isScopedVar = true; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | // Add locks. |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1525 | for (const auto &M : ExclusiveLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1526 | Analyzer->addLock(FSet, FactEntry(M, LK_Exclusive, Loc, isScopedVar), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1527 | CapDiagKind); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1528 | for (const auto &M : SharedLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1529 | Analyzer->addLock(FSet, FactEntry(M, LK_Shared, Loc, isScopedVar), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1530 | CapDiagKind); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Add the managing object as a dummy mutex, mapped to the underlying mutex. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1533 | // FIXME: this doesn't work if we acquire multiple locks. |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1534 | if (isScopedVar) { |
| 1535 | SourceLocation MLoc = VD->getLocation(); |
| 1536 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation()); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1537 | // FIXME: does this store a pointer to DRE? |
| 1538 | CapabilityExpr Scp = Analyzer->SxBuilder.translateAttrExpr(&DRE, nullptr); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1539 | |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1540 | for (const auto &M : ExclusiveLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1541 | Analyzer->addLock(FSet, FactEntry(Scp, LK_Exclusive, MLoc, M.sexpr()), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1542 | CapDiagKind); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1543 | for (const auto &M : SharedLocksToAdd) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1544 | Analyzer->addLock(FSet, FactEntry(Scp, LK_Shared, MLoc, M.sexpr()), |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1545 | CapDiagKind); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1546 | |
| 1547 | // handle corner case where the underlying mutex is invalid |
| 1548 | if (ExclusiveLocksToAdd.size() == 0 && SharedLocksToAdd.size() == 0) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1549 | Analyzer->addLock(FSet, FactEntry(Scp, LK_Exclusive, MLoc), |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1550 | CapDiagKind); |
| 1551 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | // Remove locks. |
| 1555 | // FIXME -- should only fully remove if the attribute refers to 'this'. |
| 1556 | bool Dtor = isa<CXXDestructorDecl>(D); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1557 | for (const auto &M : ExclusiveLocksToRemove) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1558 | Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Exclusive, CapDiagKind); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1559 | for (const auto &M : SharedLocksToRemove) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1560 | Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Shared, CapDiagKind); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1561 | for (const auto &M : GenericLocksToRemove) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1562 | Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Generic, CapDiagKind); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
DeLesley Hutchins | 9d53033 | 2012-01-06 19:16:50 +0000 | [diff] [blame] | 1565 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1566 | /// \brief For unary operations which read and write a variable, we need to |
| 1567 | /// check whether we hold any required mutexes. Reads are checked in |
| 1568 | /// VisitCastExpr. |
| 1569 | void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { |
| 1570 | switch (UO->getOpcode()) { |
| 1571 | case clang::UO_PostDec: |
| 1572 | case clang::UO_PostInc: |
| 1573 | case clang::UO_PreDec: |
| 1574 | case clang::UO_PreInc: { |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1575 | checkAccess(UO->getSubExpr(), AK_Written); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1576 | break; |
| 1577 | } |
| 1578 | default: |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | /// For binary operations which assign to a variable (writes), we need to check |
| 1584 | /// whether we hold any required mutexes. |
| 1585 | /// FIXME: Deal with non-primitive types. |
| 1586 | void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { |
| 1587 | if (!BO->isAssignmentOp()) |
| 1588 | return; |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1589 | |
| 1590 | // adjust the context |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1591 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1592 | |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1593 | checkAccess(BO->getLHS(), AK_Written); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1596 | |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1597 | /// Whenever we do an LValue to Rvalue cast, we are reading a variable and |
| 1598 | /// need to ensure we hold any required mutexes. |
| 1599 | /// FIXME: Deal with non-primitive types. |
| 1600 | void BuildLockset::VisitCastExpr(CastExpr *CE) { |
| 1601 | if (CE->getCastKind() != CK_LValueToRValue) |
| 1602 | return; |
DeLesley Hutchins | 5df82f2 | 2012-12-05 00:52:33 +0000 | [diff] [blame] | 1603 | checkAccess(CE->getSubExpr(), AK_Read); |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | |
DeLesley Hutchins | 714296c | 2011-12-29 00:56:48 +0000 | [diff] [blame] | 1607 | void BuildLockset::VisitCallExpr(CallExpr *Exp) { |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1608 | if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Exp)) { |
| 1609 | MemberExpr *ME = dyn_cast<MemberExpr>(CE->getCallee()); |
| 1610 | // ME can be null when calling a method pointer |
| 1611 | CXXMethodDecl *MD = CE->getMethodDecl(); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 1612 | |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1613 | if (ME && MD) { |
| 1614 | if (ME->isArrow()) { |
| 1615 | if (MD->isConst()) { |
| 1616 | checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); |
| 1617 | } else { // FIXME -- should be AK_Written |
| 1618 | checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 1619 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1620 | } else { |
| 1621 | if (MD->isConst()) |
| 1622 | checkAccess(CE->getImplicitObjectArgument(), AK_Read); |
| 1623 | else // FIXME -- should be AK_Written |
| 1624 | checkAccess(CE->getImplicitObjectArgument(), AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 1625 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } else if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) { |
| 1628 | switch (OE->getOperator()) { |
| 1629 | case OO_Equal: { |
| 1630 | const Expr *Target = OE->getArg(0); |
| 1631 | const Expr *Source = OE->getArg(1); |
| 1632 | checkAccess(Target, AK_Written); |
| 1633 | checkAccess(Source, AK_Read); |
| 1634 | break; |
| 1635 | } |
DeLesley Hutchins | 5ede5cc | 2013-11-05 23:09:56 +0000 | [diff] [blame] | 1636 | case OO_Star: |
DeLesley Hutchins | e73d6b6 | 2013-11-08 19:42:01 +0000 | [diff] [blame] | 1637 | case OO_Arrow: |
| 1638 | case OO_Subscript: { |
DeLesley Hutchins | d1c9b37d | 2014-03-10 23:03:49 +0000 | [diff] [blame] | 1639 | const Expr *Obj = OE->getArg(0); |
| 1640 | checkAccess(Obj, AK_Read); |
| 1641 | checkPtAccess(Obj, AK_Read); |
DeLesley Hutchins | 5ede5cc | 2013-11-05 23:09:56 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1644 | default: { |
DeLesley Hutchins | 05b7b37 | 2013-11-06 18:40:01 +0000 | [diff] [blame] | 1645 | const Expr *Obj = OE->getArg(0); |
| 1646 | checkAccess(Obj, AK_Read); |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1647 | break; |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | } |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1651 | NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); |
| 1652 | if(!D || !D->hasAttrs()) |
| 1653 | return; |
| 1654 | handleCall(Exp, D); |
| 1655 | } |
| 1656 | |
| 1657 | void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) { |
DeLesley Hutchins | c105ba1 | 2013-04-01 17:47:37 +0000 | [diff] [blame] | 1658 | const CXXConstructorDecl *D = Exp->getConstructor(); |
| 1659 | if (D && D->isCopyConstructor()) { |
| 1660 | const Expr* Source = Exp->getArg(0); |
| 1661 | checkAccess(Source, AK_Read); |
DeLesley Hutchins | f489d2b | 2012-12-05 01:20:45 +0000 | [diff] [blame] | 1662 | } |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1663 | // FIXME -- only handles constructors in DeclStmt below. |
| 1664 | } |
| 1665 | |
| 1666 | void BuildLockset::VisitDeclStmt(DeclStmt *S) { |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1667 | // adjust the context |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1668 | LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1669 | |
Aaron Ballman | 9ee54d1 | 2014-05-14 20:42:13 +0000 | [diff] [blame] | 1670 | for (auto *D : S->getDeclGroup()) { |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1671 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) { |
| 1672 | Expr *E = VD->getInit(); |
DeLesley Hutchins | 0c1da20 | 2012-07-03 18:25:56 +0000 | [diff] [blame] | 1673 | // handle constructors that involve temporaries |
| 1674 | if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E)) |
| 1675 | E = EWC->getSubExpr(); |
| 1676 | |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1677 | if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) { |
| 1678 | NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor()); |
| 1679 | if (!CtorD || !CtorD->hasAttrs()) |
| 1680 | return; |
| 1681 | handleCall(CE, CtorD, VD); |
| 1682 | } |
| 1683 | } |
| 1684 | } |
DeLesley Hutchins | db917bd | 2011-10-21 18:06:53 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1687 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1688 | |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1689 | /// \brief Compute the intersection of two locksets and issue warnings for any |
| 1690 | /// locks in the symmetric difference. |
| 1691 | /// |
| 1692 | /// This function is used at a merge point in the CFG when comparing the lockset |
| 1693 | /// of each branch being merged. For example, given the following sequence: |
| 1694 | /// A; if () then B; else C; D; we need to check that the lockset after B and C |
| 1695 | /// are the same. In the event of a difference, we use the intersection of these |
| 1696 | /// two locksets at the start of D. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1697 | /// |
Ted Kremenek | 78094ca | 2012-08-22 23:50:41 +0000 | [diff] [blame] | 1698 | /// \param FSet1 The first lockset. |
| 1699 | /// \param FSet2 The second lockset. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1700 | /// \param JoinLoc The location of the join point for error reporting |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 1701 | /// \param LEK1 The error message to report if a mutex is missing from LSet1 |
| 1702 | /// \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] | 1703 | void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1, |
| 1704 | const FactSet &FSet2, |
| 1705 | SourceLocation JoinLoc, |
| 1706 | LockErrorKind LEK1, |
| 1707 | LockErrorKind LEK2, |
| 1708 | bool Modify) { |
| 1709 | FactSet FSet1Orig = FSet1; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1710 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 1711 | // Find locks in FSet2 that conflict or are not in FSet1, and warn. |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 1712 | for (const auto &Fact : FSet2) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1713 | const FactEntry *LDat1 = nullptr; |
| 1714 | const FactEntry *LDat2 = &FactMan[Fact]; |
| 1715 | FactSet::iterator Iter1 = FSet1.findLockIter(FactMan, *LDat2); |
| 1716 | if (Iter1 != FSet1.end()) LDat1 = &FactMan[*Iter1]; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1717 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1718 | if (LDat1) { |
| 1719 | if (LDat1->kind() != LDat2->kind()) { |
| 1720 | Handler.handleExclusiveAndShared("mutex", LDat2->toString(), |
| 1721 | LDat2->loc(), LDat1->loc()); |
| 1722 | if (Modify && LDat1->kind() != LK_Exclusive) { |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 1723 | // Take the exclusive lock, which is the one in FSet2. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1724 | *Iter1 = Fact; |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1725 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1726 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1727 | else if (Modify && LDat1->asserted() && !LDat2->asserted()) { |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 1728 | // The non-asserted lock in FSet2 is the one we want to track. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1729 | *Iter1 = Fact; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 1730 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1731 | } else { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1732 | if (LDat2->underlying()) { |
| 1733 | if (FSet2.findLock(FactMan, CapabilityExpr(LDat2->underlying(), |
| 1734 | false))) { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 1735 | // If this is a scoped lock that manages another mutex, and if the |
| 1736 | // underlying mutex is still held, then warn about the underlying |
| 1737 | // mutex. |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1738 | Handler.handleMutexHeldEndOfScope("mutex", |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1739 | sx::toString(LDat2->underlying()), |
| 1740 | LDat2->loc(), JoinLoc, LEK1); |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1743 | else if (!LDat2->managed() && !LDat2->asserted() && |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1744 | !LDat2->negative() && !LDat2->isUniversal()) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1745 | Handler.handleMutexHeldEndOfScope("mutex", LDat2->toString(), |
| 1746 | LDat2->loc(), JoinLoc, LEK1); |
| 1747 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1748 | } |
| 1749 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1750 | |
DeLesley Hutchins | 3b2c66b | 2013-05-20 17:57:55 +0000 | [diff] [blame] | 1751 | // Find locks in FSet1 that are not in FSet2, and remove them. |
Aaron Ballman | 59a72b9 | 2014-05-14 18:32:59 +0000 | [diff] [blame] | 1752 | for (const auto &Fact : FSet1Orig) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1753 | const FactEntry *LDat1 = &FactMan[Fact]; |
| 1754 | const FactEntry *LDat2 = FSet2.findLock(FactMan, *LDat1); |
DeLesley Hutchins | d162c91 | 2012-06-28 22:42:48 +0000 | [diff] [blame] | 1755 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1756 | if (!LDat2) { |
| 1757 | if (LDat1->underlying()) { |
| 1758 | if (FSet1Orig.findLock(FactMan, CapabilityExpr(LDat1->underlying(), |
| 1759 | false))) { |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 1760 | // If this is a scoped lock that manages another mutex, and if the |
| 1761 | // underlying mutex is still held, then warn about the underlying |
| 1762 | // mutex. |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1763 | Handler.handleMutexHeldEndOfScope("mutex", |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1764 | sx::toString(LDat1->underlying()), |
| 1765 | LDat1->loc(), JoinLoc, LEK1); |
DeLesley Hutchins | ab0d4e6 | 2012-07-02 22:26:29 +0000 | [diff] [blame] | 1766 | } |
| 1767 | } |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1768 | else if (!LDat1->managed() && !LDat1->asserted() && |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1769 | !LDat1->negative() && !LDat1->isUniversal()) { |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1770 | Handler.handleMutexHeldEndOfScope("mutex", LDat1->toString(), |
| 1771 | LDat1->loc(), JoinLoc, LEK2); |
| 1772 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1773 | if (Modify) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1774 | FSet1.removeLock(FactMan, *LDat1); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1779 | |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 1780 | // Return true if block B never continues to its successors. |
| 1781 | inline bool neverReturns(const CFGBlock* B) { |
| 1782 | if (B->hasNoReturnElement()) |
| 1783 | return true; |
| 1784 | if (B->empty()) |
| 1785 | return false; |
| 1786 | |
| 1787 | CFGElement Last = B->back(); |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1788 | if (Optional<CFGStmt> S = Last.getAs<CFGStmt>()) { |
| 1789 | if (isa<CXXThrowExpr>(S->getStmt())) |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 1790 | return true; |
| 1791 | } |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1795 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1796 | /// \brief Check a function's CFG for thread-safety violations. |
| 1797 | /// |
| 1798 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 1799 | /// at the end of each block, and issue warnings for thread safety violations. |
| 1800 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 1801 | void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) { |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 1802 | // TODO: this whole function needs be rewritten as a visitor for CFGWalker. |
| 1803 | // For now, we just use the walker to set things up. |
| 1804 | threadSafety::CFGWalker walker; |
| 1805 | if (!walker.init(AC)) |
| 1806 | return; |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1807 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1808 | // AC.dumpCFG(true); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 1809 | // threadSafety::printSCFG(walker); |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1810 | |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 1811 | CFG *CFGraph = walker.getGraph(); |
| 1812 | const NamedDecl *D = walker.getDecl(); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame^] | 1813 | const FunctionDecl *CurrentFunction = dyn_cast<FunctionDecl>(D); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1814 | CurrentMethod = dyn_cast<CXXMethodDecl>(D); |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 1815 | |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1816 | if (D->hasAttr<NoThreadSafetyAnalysisAttr>()) |
DeLesley Hutchins | a088f67 | 2011-10-17 21:33:35 +0000 | [diff] [blame] | 1817 | return; |
DeLesley Hutchins | b221391 | 2014-04-07 18:09:54 +0000 | [diff] [blame] | 1818 | |
DeLesley Hutchins | c2286f6 | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1819 | // FIXME: Do something a bit more intelligent inside constructor and |
| 1820 | // destructor code. Constructors and destructors must assume unique access |
| 1821 | // to 'this', so checks on member variable access is disabled, but we should |
| 1822 | // still enable checks on other objects. |
| 1823 | if (isa<CXXConstructorDecl>(D)) |
| 1824 | return; // Don't check inside constructors. |
| 1825 | if (isa<CXXDestructorDecl>(D)) |
| 1826 | return; // Don't check inside destructors. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1827 | |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame^] | 1828 | Handler.enterFunction(CurrentFunction); |
| 1829 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 1830 | BlockInfo.resize(CFGraph->getNumBlockIDs(), |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1831 | CFGBlockInfo::getEmptyBlockInfo(LocalVarMap)); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1832 | |
| 1833 | // We need to explore the CFG via a "topological" ordering. |
| 1834 | // That way, we will be guaranteed to have information about required |
| 1835 | // predecessor locksets when exploring a new block. |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 1836 | const PostOrderCFGView *SortedGraph = walker.getSortedGraph(); |
Ted Kremenek | 4b4c51c | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 1837 | PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1838 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 1839 | // Mark entry block as reachable |
| 1840 | BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true; |
| 1841 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1842 | // Compute SSA names for local variables |
| 1843 | LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo); |
| 1844 | |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1845 | // Fill in source locations for all CFGBlocks. |
| 1846 | findBlockLocations(CFGraph, SortedGraph, BlockInfo); |
| 1847 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1848 | CapExprSet ExclusiveLocksAcquired; |
| 1849 | CapExprSet SharedLocksAcquired; |
| 1850 | CapExprSet LocksReleased; |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 1851 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 1852 | // Add locks from exclusive_locks_required and shared_locks_required |
DeLesley Hutchins | c2286f6 | 2012-02-16 17:13:43 +0000 | [diff] [blame] | 1853 | // to initial lockset. Also turn off checking for lock and unlock functions. |
| 1854 | // FIXME: is there a more intelligent way to check lock/unlock functions? |
Ted Kremenek | 4b4c51c | 2011-10-22 02:14:27 +0000 | [diff] [blame] | 1855 | if (!SortedGraph->empty() && D->hasAttrs()) { |
| 1856 | const CFGBlock *FirstBlock = *SortedGraph->begin(); |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1857 | FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet; |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1858 | const AttrVec &ArgAttrs = D->getAttrs(); |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1859 | |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1860 | CapExprSet ExclusiveLocksToAdd; |
| 1861 | CapExprSet SharedLocksToAdd; |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1862 | StringRef CapDiagKind = "mutex"; |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1863 | |
| 1864 | SourceLocation Loc = D->getLocation(); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1865 | for (const auto *Attr : ArgAttrs) { |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1866 | Loc = Attr->getLocation(); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1867 | if (const auto *A = dyn_cast<RequiresCapabilityAttr>(Attr)) { |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 1868 | getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1869 | nullptr, D); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1870 | CapDiagKind = ClassifyDiagnostic(A); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1871 | } else if (const auto *A = dyn_cast<ReleaseCapabilityAttr>(Attr)) { |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 1872 | // UNLOCK_FUNCTION() is used to hide the underlying lock implementation. |
| 1873 | // We must ignore such methods. |
| 1874 | if (A->args_size() == 0) |
| 1875 | return; |
| 1876 | // FIXME -- deal with exclusive vs. shared unlock functions? |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1877 | getMutexIDs(ExclusiveLocksToAdd, A, nullptr, D); |
| 1878 | getMutexIDs(LocksReleased, A, nullptr, D); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1879 | CapDiagKind = ClassifyDiagnostic(A); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1880 | } else if (const auto *A = dyn_cast<AcquireCapabilityAttr>(Attr)) { |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 1881 | if (A->args_size() == 0) |
| 1882 | return; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 1883 | getMutexIDs(A->isShared() ? SharedLocksAcquired |
| 1884 | : ExclusiveLocksAcquired, |
| 1885 | A, nullptr, D); |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1886 | CapDiagKind = ClassifyDiagnostic(A); |
DeLesley Hutchins | c4a6e51 | 2012-07-02 21:59:24 +0000 | [diff] [blame] | 1887 | } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) { |
| 1888 | // Don't try to check trylock functions for now |
| 1889 | return; |
| 1890 | } else if (isa<SharedTrylockFunctionAttr>(Attr)) { |
| 1891 | // Don't try to check trylock functions for now |
| 1892 | return; |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
DeLesley Hutchins | 09bcefc | 2012-07-05 21:16:29 +0000 | [diff] [blame] | 1895 | |
| 1896 | // FIXME -- Loc can be wrong here. |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1897 | for (const auto &Mu : ExclusiveLocksToAdd) |
| 1898 | addLock(InitialLockset, FactEntry(Mu, LK_Exclusive, Loc), |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1899 | CapDiagKind, true); |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 1900 | for (const auto &Mu : SharedLocksToAdd) |
| 1901 | addLock(InitialLockset, FactEntry(Mu, LK_Shared, Loc), |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1902 | CapDiagKind, true); |
Caitlin Sadowski | 6525fb2 | 2011-09-15 17:43:08 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Aaron Ballman | e80bfcd | 2014-04-17 21:44:08 +0000 | [diff] [blame] | 1905 | for (const auto *CurrBlock : *SortedGraph) { |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1906 | int CurrBlockID = CurrBlock->getBlockID(); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1907 | CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Use the default initial lockset in case there are no predecessors. |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1910 | VisitedBlocks.insert(CurrBlock); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Iterate through the predecessor blocks and warn if the lockset for all |
| 1913 | // predecessors is not the same. We take the entry lockset of the current |
| 1914 | // block to be the intersection of all previous locksets. |
| 1915 | // FIXME: By keeping the intersection, we may output more errors in future |
| 1916 | // for a lock which is not in the intersection, but was in the union. We |
| 1917 | // may want to also keep the union in future. As an example, let's say |
| 1918 | // the intersection contains Mutex L, and the union contains L and M. |
| 1919 | // Later we unlock M. At this point, we would output an error because we |
| 1920 | // never locked M; although the real error is probably that we forgot to |
| 1921 | // lock M on all code paths. Conversely, let's say that later we lock M. |
| 1922 | // In this case, we should compare against the intersection instead of the |
| 1923 | // union because the real error is probably that we forgot to unlock M on |
| 1924 | // all code paths. |
| 1925 | bool LocksetInitialized = false; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1926 | SmallVector<CFGBlock *, 8> SpecialBlocks; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1927 | for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), |
| 1928 | PE = CurrBlock->pred_end(); PI != PE; ++PI) { |
| 1929 | |
| 1930 | // if *PI -> CurrBlock is a back edge |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1931 | if (*PI == nullptr || !VisitedBlocks.alreadySet(*PI)) |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1932 | continue; |
| 1933 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 1934 | int PrevBlockID = (*PI)->getBlockID(); |
| 1935 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 1936 | |
DeLesley Hutchins | a2587ef | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 1937 | // Ignore edges from blocks that can't return. |
DeLesley Hutchins | 9fa426a | 2013-01-18 22:15:45 +0000 | [diff] [blame] | 1938 | if (neverReturns(*PI) || !PrevBlockInfo->Reachable) |
DeLesley Hutchins | a2587ef | 2012-03-02 22:02:58 +0000 | [diff] [blame] | 1939 | continue; |
| 1940 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 1941 | // Okay, we can reach this block from the entry. |
| 1942 | CurrBlockInfo->Reachable = true; |
| 1943 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1944 | // If the previous block ended in a 'continue' or 'break' statement, then |
| 1945 | // a difference in locksets is probably due to a bug in that block, rather |
| 1946 | // than in some other predecessor. In that case, keep the other |
| 1947 | // predecessor's lockset. |
| 1948 | if (const Stmt *Terminator = (*PI)->getTerminator()) { |
| 1949 | if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) { |
| 1950 | SpecialBlocks.push_back(*PI); |
| 1951 | continue; |
| 1952 | } |
| 1953 | } |
| 1954 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1955 | FactSet PrevLockset; |
| 1956 | getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock); |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 1957 | |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1958 | if (!LocksetInitialized) { |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1959 | CurrBlockInfo->EntrySet = PrevLockset; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1960 | LocksetInitialized = true; |
| 1961 | } else { |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1962 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 1963 | CurrBlockInfo->EntryLoc, |
| 1964 | LEK_LockedSomePredecessors); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 1965 | } |
| 1966 | } |
| 1967 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 1968 | // Skip rest of block if it's not reachable. |
| 1969 | if (!CurrBlockInfo->Reachable) |
| 1970 | continue; |
| 1971 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1972 | // Process continue and break blocks. Assume that the lockset for the |
| 1973 | // resulting block is unaffected by any discrepancies in them. |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 1974 | for (const auto *PrevBlock : SpecialBlocks) { |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1975 | int PrevBlockID = PrevBlock->getBlockID(); |
| 1976 | CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; |
| 1977 | |
| 1978 | if (!LocksetInitialized) { |
| 1979 | CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet; |
| 1980 | LocksetInitialized = true; |
| 1981 | } else { |
| 1982 | // Determine whether this edge is a loop terminator for diagnostic |
| 1983 | // purposes. FIXME: A 'break' statement might be a loop terminator, but |
| 1984 | // it might also be part of a switch. Also, a subsequent destructor |
| 1985 | // might add to the lockset, in which case the real issue might be a |
| 1986 | // double lock on the other path. |
| 1987 | const Stmt *Terminator = PrevBlock->getTerminator(); |
| 1988 | bool IsLoop = Terminator && isa<ContinueStmt>(Terminator); |
| 1989 | |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1990 | FactSet PrevLockset; |
| 1991 | getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, |
| 1992 | PrevBlock, CurrBlock); |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1993 | |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1994 | // Do not update EntrySet. |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 1995 | intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, |
| 1996 | PrevBlockInfo->ExitLoc, |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 1997 | IsLoop ? LEK_LockedSomeLoopIterations |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 1998 | : LEK_LockedSomePredecessors, |
| 1999 | false); |
Richard Smith | 815b29d | 2012-02-03 03:30:07 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
DeLesley Hutchins | 8c9d957 | 2012-04-19 16:48:43 +0000 | [diff] [blame] | 2003 | BuildLockset LocksetBuilder(this, *CurrBlockInfo); |
| 2004 | |
DeLesley Hutchins | 9b7022e | 2012-01-06 18:36:09 +0000 | [diff] [blame] | 2005 | // Visit all the statements in the basic block. |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2006 | for (CFGBlock::const_iterator BI = CurrBlock->begin(), |
| 2007 | BE = CurrBlock->end(); BI != BE; ++BI) { |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2008 | switch (BI->getKind()) { |
| 2009 | case CFGElement::Statement: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2010 | CFGStmt CS = BI->castAs<CFGStmt>(); |
| 2011 | LocksetBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2012 | break; |
| 2013 | } |
| 2014 | // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now. |
| 2015 | case CFGElement::AutomaticObjectDtor: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2016 | CFGAutomaticObjDtor AD = BI->castAs<CFGAutomaticObjDtor>(); |
| 2017 | CXXDestructorDecl *DD = const_cast<CXXDestructorDecl *>( |
| 2018 | AD.getDestructorDecl(AC.getASTContext())); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2019 | if (!DD->hasAttrs()) |
| 2020 | break; |
| 2021 | |
| 2022 | // Create a dummy expression, |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2023 | VarDecl *VD = const_cast<VarDecl*>(AD.getVarDecl()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2024 | DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 2025 | AD.getTriggerStmt()->getLocEnd()); |
DeLesley Hutchins | f893e8a | 2011-10-21 20:51:27 +0000 | [diff] [blame] | 2026 | LocksetBuilder.handleCall(&DRE, DD); |
| 2027 | break; |
| 2028 | } |
| 2029 | default: |
| 2030 | break; |
| 2031 | } |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2032 | } |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2033 | CurrBlockInfo->ExitSet = LocksetBuilder.FSet; |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2034 | |
| 2035 | // For every back edge from CurrBlock (the end of the loop) to another block |
| 2036 | // (FirstLoopBlock) we need to check that the Lockset of Block is equal to |
| 2037 | // the one held at the beginning of FirstLoopBlock. We can look up the |
| 2038 | // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. |
| 2039 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 2040 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
| 2041 | |
| 2042 | // if CurrBlock -> *SI is *not* a back edge |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2043 | if (*SI == nullptr || !VisitedBlocks.alreadySet(*SI)) |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2044 | continue; |
| 2045 | |
| 2046 | CFGBlock *FirstLoopBlock = *SI; |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2047 | CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()]; |
| 2048 | CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID]; |
| 2049 | intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet, |
| 2050 | PreLoop->EntryLoc, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2051 | LEK_LockedSomeLoopIterations, |
| 2052 | false); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2056 | CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()]; |
| 2057 | CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()]; |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 2058 | |
DeLesley Hutchins | 10958ca | 2012-09-21 17:57:00 +0000 | [diff] [blame] | 2059 | // Skip the final check if the exit block is unreachable. |
| 2060 | if (!Final->Reachable) |
| 2061 | return; |
| 2062 | |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2063 | // By default, we expect all locks held on entry to be held on exit. |
| 2064 | FactSet ExpectedExitSet = Initial->EntrySet; |
| 2065 | |
| 2066 | // Adjust the expected exit set by adding or removing locks, as declared |
| 2067 | // by *-LOCK_FUNCTION and UNLOCK_FUNCTION. The intersect below will then |
| 2068 | // issue the appropriate warning. |
| 2069 | // FIXME: the location here is not quite right. |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 2070 | for (const auto &Lock : ExclusiveLocksAcquired) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 2071 | ExpectedExitSet.addLock(FactMan, FactEntry(Lock, LK_Exclusive, |
| 2072 | D->getLocation())); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 2073 | for (const auto &Lock : SharedLocksAcquired) |
DeLesley Hutchins | 4266522 | 2014-08-04 16:10:59 +0000 | [diff] [blame] | 2074 | ExpectedExitSet.addLock(FactMan, FactEntry(Lock, LK_Shared, |
| 2075 | D->getLocation())); |
Aaron Ballman | 0491afa | 2014-04-18 13:13:15 +0000 | [diff] [blame] | 2076 | for (const auto &Lock : LocksReleased) |
| 2077 | ExpectedExitSet.removeLock(FactMan, Lock); |
DeLesley Hutchins | fd374bb | 2013-04-08 20:11:11 +0000 | [diff] [blame] | 2078 | |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 2079 | // 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] | 2080 | intersectAndWarn(ExpectedExitSet, Final->ExitSet, |
DeLesley Hutchins | ebbf7701 | 2012-06-22 17:07:28 +0000 | [diff] [blame] | 2081 | Final->ExitLoc, |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 2082 | LEK_LockedAtEndOfFunction, |
DeLesley Hutchins | c9776fa | 2012-08-10 18:39:05 +0000 | [diff] [blame] | 2083 | LEK_NotLockedAtEndOfFunction, |
| 2084 | false); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame^] | 2085 | |
| 2086 | Handler.leaveFunction(CurrentFunction); |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2089 | |
| 2090 | /// \brief Check a function's CFG for thread-safety violations. |
| 2091 | /// |
| 2092 | /// We traverse the blocks in the CFG, compute the set of mutexes that are held |
| 2093 | /// at the end of each block, and issue warnings for thread safety violations. |
| 2094 | /// Each block in the CFG is traversed exactly once. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 2095 | void runThreadSafetyAnalysis(AnalysisDeclContext &AC, |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2096 | ThreadSafetyHandler &Handler) { |
| 2097 | ThreadSafetyAnalyzer Analyzer(Handler); |
| 2098 | Analyzer.runAnalysis(AC); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | /// \brief Helper function that returns a LockKind required for the given level |
| 2102 | /// of access. |
| 2103 | LockKind getLockKindFromAccessKind(AccessKind AK) { |
| 2104 | switch (AK) { |
| 2105 | case AK_Read : |
| 2106 | return LK_Shared; |
| 2107 | case AK_Written : |
| 2108 | return LK_Exclusive; |
| 2109 | } |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 2110 | llvm_unreachable("Unknown AccessKind"); |
Caitlin Sadowski | 3320834 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 2111 | } |
DeLesley Hutchins | 3d312b1 | 2011-10-21 16:14:33 +0000 | [diff] [blame] | 2112 | |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 2113 | }} // end namespace clang::threadSafety |