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