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