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