ThreadSafety.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ThreadSafety.h -------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. //
  15. // A intra-procedural analysis for thread safety (e.g. deadlocks and race
  16. // conditions), based off of an annotation system.
  17. //
  18. // See http://clang.llvm.org/docs/LanguageExtensions.html#thread-safety-annotation-checking
  19. // for more information.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
  23. #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
  24. #include "clang/Basic/SourceLocation.h"
  25. #include "llvm/ADT/StringRef.h"
  26. namespace clang {
  27. class AnalysisDeclContext;
  28. class FunctionDecl;
  29. class NamedDecl;
  30. namespace threadSafety {
  31. class BeforeSet;
  32. /// This enum distinguishes between different kinds of operations that may
  33. /// need to be protected by locks. We use this enum in error handling.
  34. enum ProtectedOperationKind {
  35. /// Dereferencing a variable (e.g. p in *p = 5;)
  36. POK_VarDereference,
  37. /// Reading or writing a variable (e.g. x in x = 5;)
  38. POK_VarAccess,
  39. /// Making a function call (e.g. fool())
  40. POK_FunctionCall,
  41. /// Passing a guarded variable by reference.
  42. POK_PassByRef,
  43. /// Passing a pt-guarded variable by reference.
  44. POK_PtPassByRef
  45. };
  46. /// This enum distinguishes between different kinds of lock actions. For
  47. /// example, it is an error to write a variable protected by shared version of a
  48. /// mutex.
  49. enum LockKind {
  50. /// Shared/reader lock of a mutex.
  51. LK_Shared,
  52. /// Exclusive/writer lock of a mutex.
  53. LK_Exclusive,
  54. /// Can be either Shared or Exclusive.
  55. LK_Generic
  56. };
  57. /// This enum distinguishes between different ways to access (read or write) a
  58. /// variable.
  59. enum AccessKind {
  60. /// Reading a variable.
  61. AK_Read,
  62. /// Writing a variable.
  63. AK_Written
  64. };
  65. /// This enum distinguishes between different situations where we warn due to
  66. /// inconsistent locking.
  67. /// \enum SK_LockedSomeLoopIterations -- a mutex is locked for some but not all
  68. /// loop iterations.
  69. /// \enum SK_LockedSomePredecessors -- a mutex is locked in some but not all
  70. /// predecessors of a CFGBlock.
  71. /// \enum SK_LockedAtEndOfFunction -- a mutex is still locked at the end of a
  72. /// function.
  73. enum LockErrorKind {
  74. LEK_LockedSomeLoopIterations,
  75. LEK_LockedSomePredecessors,
  76. LEK_LockedAtEndOfFunction,
  77. LEK_NotLockedAtEndOfFunction
  78. };
  79. /// Handler class for thread safety warnings.
  80. class ThreadSafetyHandler {
  81. public:
  82. using Name = StringRef;
  83. ThreadSafetyHandler() = default;
  84. virtual ~ThreadSafetyHandler();
  85. /// Warn about lock expressions which fail to resolve to lockable objects.
  86. /// \param Loc -- the SourceLocation of the unresolved expression.
  87. virtual void handleInvalidLockExp(SourceLocation Loc) {}
  88. /// Warn about unlock function calls that do not have a prior matching lock
  89. /// expression.
  90. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  91. /// \param LockName -- A StringRef name for the lock expression, to be printed
  92. /// in the error message.
  93. /// \param Loc -- The SourceLocation of the Unlock
  94. /// \param LocPreviousUnlock -- If valid, the location of a previous Unlock.
  95. virtual void handleUnmatchedUnlock(StringRef Kind, Name LockName,
  96. SourceLocation Loc,
  97. SourceLocation LocPreviousUnlock) {}
  98. /// Warn about an unlock function call that attempts to unlock a lock with
  99. /// the incorrect lock kind. For instance, a shared lock being unlocked
  100. /// exclusively, or vice versa.
  101. /// \param LockName -- A StringRef name for the lock expression, to be printed
  102. /// in the error message.
  103. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  104. /// \param Expected -- the kind of lock expected.
  105. /// \param Received -- the kind of lock received.
  106. /// \param LocLocked -- The SourceLocation of the Lock.
  107. /// \param LocUnlock -- The SourceLocation of the Unlock.
  108. virtual void handleIncorrectUnlockKind(StringRef Kind, Name LockName,
  109. LockKind Expected, LockKind Received,
  110. SourceLocation LocLocked,
  111. SourceLocation LocUnlock) {}
  112. /// Warn about lock function calls for locks which are already held.
  113. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  114. /// \param LockName -- A StringRef name for the lock expression, to be printed
  115. /// in the error message.
  116. /// \param LocLocked -- The location of the first lock expression.
  117. /// \param LocDoubleLock -- The location of the second lock expression.
  118. virtual void handleDoubleLock(StringRef Kind, Name LockName,
  119. SourceLocation LocLocked,
  120. SourceLocation LocDoubleLock) {}
  121. /// Warn about situations where a mutex is sometimes held and sometimes not.
  122. /// The three situations are:
  123. /// 1. a mutex is locked on an "if" branch but not the "else" branch,
  124. /// 2, or a mutex is only held at the start of some loop iterations,
  125. /// 3. or when a mutex is locked but not unlocked inside a function.
  126. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  127. /// \param LockName -- A StringRef name for the lock expression, to be printed
  128. /// in the error message.
  129. /// \param LocLocked -- The location of the lock expression where the mutex is
  130. /// locked
  131. /// \param LocEndOfScope -- The location of the end of the scope where the
  132. /// mutex is no longer held
  133. /// \param LEK -- which of the three above cases we should warn for
  134. virtual void handleMutexHeldEndOfScope(StringRef Kind, Name LockName,
  135. SourceLocation LocLocked,
  136. SourceLocation LocEndOfScope,
  137. LockErrorKind LEK) {}
  138. /// Warn when a mutex is held exclusively and shared at the same point. For
  139. /// example, if a mutex is locked exclusively during an if branch and shared
  140. /// during the else branch.
  141. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  142. /// \param LockName -- A StringRef name for the lock expression, to be printed
  143. /// in the error message.
  144. /// \param Loc1 -- The location of the first lock expression.
  145. /// \param Loc2 -- The location of the second lock expression.
  146. virtual void handleExclusiveAndShared(StringRef Kind, Name LockName,
  147. SourceLocation Loc1,
  148. SourceLocation Loc2) {}
  149. /// Warn when a protected operation occurs while no locks are held.
  150. /// \param D -- The decl for the protected variable or function
  151. /// \param POK -- The kind of protected operation (e.g. variable access)
  152. /// \param AK -- The kind of access (i.e. read or write) that occurred
  153. /// \param Loc -- The location of the protected operation.
  154. virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
  155. AccessKind AK, SourceLocation Loc) {}
  156. /// Warn when a protected operation occurs while the specific mutex protecting
  157. /// the operation is not locked.
  158. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  159. /// \param D -- The decl for the protected variable or function
  160. /// \param POK -- The kind of protected operation (e.g. variable access)
  161. /// \param LockName -- A StringRef name for the lock expression, to be printed
  162. /// in the error message.
  163. /// \param LK -- The kind of access (i.e. read or write) that occurred
  164. /// \param Loc -- The location of the protected operation.
  165. virtual void handleMutexNotHeld(StringRef Kind, const NamedDecl *D,
  166. ProtectedOperationKind POK, Name LockName,
  167. LockKind LK, SourceLocation Loc,
  168. Name *PossibleMatch = nullptr) {}
  169. /// Warn when acquiring a lock that the negative capability is not held.
  170. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  171. /// \param LockName -- The name for the lock expression, to be printed in the
  172. /// diagnostic.
  173. /// \param Neg -- The name of the negative capability to be printed in the
  174. /// diagnostic.
  175. /// \param Loc -- The location of the protected operation.
  176. virtual void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg,
  177. SourceLocation Loc) {}
  178. /// Warn when calling a function that a negative capability is not held.
  179. /// \param D -- The decl for the function requiring the negative capability.
  180. /// \param LockName -- The name for the lock expression, to be printed in the
  181. /// diagnostic.
  182. /// \param Loc -- The location of the protected operation.
  183. virtual void handleNegativeNotHeld(const NamedDecl *D, Name LockName,
  184. SourceLocation Loc) {}
  185. /// Warn when a function is called while an excluded mutex is locked. For
  186. /// example, the mutex may be locked inside the function.
  187. /// \param Kind -- the capability's name parameter (role, mutex, etc).
  188. /// \param FunName -- The name of the function
  189. /// \param LockName -- A StringRef name for the lock expression, to be printed
  190. /// in the error message.
  191. /// \param Loc -- The location of the function call.
  192. virtual void handleFunExcludesLock(StringRef Kind, Name FunName,
  193. Name LockName, SourceLocation Loc) {}
  194. /// Warn that L1 cannot be acquired before L2.
  195. virtual void handleLockAcquiredBefore(StringRef Kind, Name L1Name,
  196. Name L2Name, SourceLocation Loc) {}
  197. /// Warn that there is a cycle in acquired_before/after dependencies.
  198. virtual void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) {}
  199. /// Called by the analysis when starting analysis of a function.
  200. /// Used to issue suggestions for changes to annotations.
  201. virtual void enterFunction(const FunctionDecl *FD) {}
  202. /// Called by the analysis when finishing analysis of a function.
  203. virtual void leaveFunction(const FunctionDecl *FD) {}
  204. bool issueBetaWarnings() { return IssueBetaWarnings; }
  205. void setIssueBetaWarnings(bool b) { IssueBetaWarnings = b; }
  206. private:
  207. bool IssueBetaWarnings = false;
  208. };
  209. /// Check a function's CFG for thread-safety violations.
  210. ///
  211. /// We traverse the blocks in the CFG, compute the set of mutexes that are held
  212. /// at the end of each block, and issue warnings for thread safety violations.
  213. /// Each block in the CFG is traversed exactly once.
  214. void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
  215. ThreadSafetyHandler &Handler,
  216. BeforeSet **Bset);
  217. void threadSafetyCleanup(BeforeSet *Cache);
  218. /// Helper function that returns a LockKind required for the given level
  219. /// of access.
  220. LockKind getLockKindFromAccessKind(AccessKind AK);
  221. } // namespace threadSafety
  222. } // namespace clang
  223. #endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
  224. #ifdef __GNUC__
  225. #pragma GCC diagnostic pop
  226. #endif