clang API Documentation
00001 //===- ThreadSafety.h ------------------------------------------*- C++ --*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // 00011 // A intra-procedural analysis for thread safety (e.g. deadlocks and race 00012 // conditions), based off of an annotation system. 00013 // 00014 // See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more 00015 // information. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_CLANG_THREADSAFETY_H 00020 #define LLVM_CLANG_THREADSAFETY_H 00021 00022 #include "clang/Analysis/AnalysisContext.h" 00023 #include "clang/Basic/SourceLocation.h" 00024 #include "llvm/ADT/StringRef.h" 00025 00026 namespace clang { 00027 namespace thread_safety { 00028 00029 /// This enum distinguishes between different kinds of operations that may 00030 /// need to be protected by locks. We use this enum in error handling. 00031 enum ProtectedOperationKind { 00032 POK_VarDereference, /// Dereferencing a variable (e.g. p in *p = 5;) 00033 POK_VarAccess, /// Reading or writing a variable (e.g. x in x = 5;) 00034 POK_FunctionCall /// Making a function call (e.g. fool()) 00035 }; 00036 00037 /// This enum distinguishes between different kinds of lock actions. For 00038 /// example, it is an error to write a variable protected by shared version of a 00039 /// mutex. 00040 enum LockKind { 00041 LK_Shared, /// Shared/reader lock of a mutex 00042 LK_Exclusive /// Exclusive/writer lock of a mutex 00043 }; 00044 00045 /// This enum distinguishes between different ways to access (read or write) a 00046 /// variable. 00047 enum AccessKind { 00048 AK_Read, /// Reading a variable 00049 AK_Written /// Writing a variable 00050 }; 00051 00052 /// This enum distinguishes between different situations where we warn due to 00053 /// inconsistent locking. 00054 /// \enum SK_LockedSomeLoopIterations -- a mutex is locked for some but not all 00055 /// loop iterations. 00056 /// \enum SK_LockedSomePredecessors -- a mutex is locked in some but not all 00057 /// predecessors of a CFGBlock. 00058 /// \enum SK_LockedAtEndOfFunction -- a mutex is still locked at the end of a 00059 /// function. 00060 enum LockErrorKind { 00061 LEK_LockedSomeLoopIterations, 00062 LEK_LockedSomePredecessors, 00063 LEK_LockedAtEndOfFunction 00064 }; 00065 00066 /// Handler class for thread safety warnings. 00067 class ThreadSafetyHandler { 00068 public: 00069 typedef llvm::StringRef Name; 00070 virtual ~ThreadSafetyHandler(); 00071 00072 /// Warn about lock expressions which fail to resolve to lockable objects. 00073 /// \param Loc -- the SourceLocation of the unresolved expression. 00074 virtual void handleInvalidLockExp(SourceLocation Loc) {} 00075 00076 /// Warn about unlock function calls that do not have a prior matching lock 00077 /// expression. 00078 /// \param LockName -- A StringRef name for the lock expression, to be printed 00079 /// in the error message. 00080 /// \param Loc -- The SourceLocation of the Unlock 00081 virtual void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {} 00082 00083 /// Warn about lock function calls for locks which are already held. 00084 /// \param LockName -- A StringRef name for the lock expression, to be printed 00085 /// in the error message. 00086 /// \param Loc -- The location of the second lock expression. 00087 virtual void handleDoubleLock(Name LockName, SourceLocation Loc) {} 00088 00089 /// Warn about situations where a mutex is sometimes held and sometimes not. 00090 /// The three situations are: 00091 /// 1. a mutex is locked on an "if" branch but not the "else" branch, 00092 /// 2, or a mutex is only held at the start of some loop iterations, 00093 /// 3. or when a mutex is locked but not unlocked inside a function. 00094 /// \param LockName -- A StringRef name for the lock expression, to be printed 00095 /// in the error message. 00096 /// \param LocLocked -- The location of the lock expression where the mutex is 00097 /// locked 00098 /// \param LocEndOfScope -- The location of the end of the scope where the 00099 /// mutex is no longer held 00100 /// \param LEK -- which of the three above cases we should warn for 00101 virtual void handleMutexHeldEndOfScope(Name LockName, 00102 SourceLocation LocLocked, 00103 SourceLocation LocEndOfScope, 00104 LockErrorKind LEK){} 00105 00106 /// Warn when a mutex is held exclusively and shared at the same point. For 00107 /// example, if a mutex is locked exclusively during an if branch and shared 00108 /// during the else branch. 00109 /// \param LockName -- A StringRef name for the lock expression, to be printed 00110 /// in the error message. 00111 /// \param Loc1 -- The location of the first lock expression. 00112 /// \param Loc2 -- The location of the second lock expression. 00113 virtual void handleExclusiveAndShared(Name LockName, SourceLocation Loc1, 00114 SourceLocation Loc2) {} 00115 00116 /// Warn when a protected operation occurs while no locks are held. 00117 /// \param D -- The decl for the protected variable or function 00118 /// \param POK -- The kind of protected operation (e.g. variable access) 00119 /// \param AK -- The kind of access (i.e. read or write) that occurred 00120 /// \param Loc -- The location of the protected operation. 00121 virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, 00122 AccessKind AK, SourceLocation Loc) {} 00123 00124 /// Warn when a protected operation occurs while the specific mutex protecting 00125 /// the operation is not locked. 00126 /// \param LockName -- A StringRef name for the lock expression, to be printed 00127 /// in the error message. 00128 /// \param D -- The decl for the protected variable or function 00129 /// \param POK -- The kind of protected operation (e.g. variable access) 00130 /// \param AK -- The kind of access (i.e. read or write) that occurred 00131 /// \param Loc -- The location of the protected operation. 00132 virtual void handleMutexNotHeld(const NamedDecl *D, 00133 ProtectedOperationKind POK, Name LockName, 00134 LockKind LK, SourceLocation Loc) {} 00135 00136 /// Warn when a function is called while an excluded mutex is locked. For 00137 /// example, the mutex may be locked inside the function. 00138 /// \param FunName -- The name of the function 00139 /// \param LockName -- A StringRef name for the lock expression, to be printed 00140 /// in the error message. 00141 /// \param Loc -- The location of the function call. 00142 virtual void handleFunExcludesLock(Name FunName, Name LockName, 00143 SourceLocation Loc) {} 00144 }; 00145 00146 /// \brief Check a function's CFG for thread-safety violations. 00147 /// 00148 /// We traverse the blocks in the CFG, compute the set of mutexes that are held 00149 /// at the end of each block, and issue warnings for thread safety violations. 00150 /// Each block in the CFG is traversed exactly once. 00151 void runThreadSafetyAnalysis(AnalysisDeclContext &AC, 00152 ThreadSafetyHandler &Handler); 00153 00154 /// \brief Helper function that returns a LockKind required for the given level 00155 /// of access. 00156 LockKind getLockKindFromAccessKind(AccessKind AK); 00157 00158 }} // end namespace clang::thread_safety 00159 #endif