|
|
|||
File indexing completed on 2026-05-10 08:36:23
0001 //===- ThreadSafety.h -------------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // 0010 // A intra-procedural analysis for thread safety (e.g. deadlocks and race 0011 // conditions), based off of an annotation system. 0012 // 0013 // See http://clang.llvm.org/docs/LanguageExtensions.html#thread-safety-annotation-checking 0014 // for more information. 0015 // 0016 //===----------------------------------------------------------------------===// 0017 0018 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H 0019 #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H 0020 0021 #include "clang/Basic/SourceLocation.h" 0022 #include "llvm/ADT/StringRef.h" 0023 0024 namespace clang { 0025 0026 class AnalysisDeclContext; 0027 class FunctionDecl; 0028 class NamedDecl; 0029 0030 namespace threadSafety { 0031 0032 class BeforeSet; 0033 0034 /// This enum distinguishes between different kinds of operations that may 0035 /// need to be protected by locks. We use this enum in error handling. 0036 enum ProtectedOperationKind { 0037 /// Dereferencing a variable (e.g. p in *p = 5;) 0038 POK_VarDereference, 0039 0040 /// Reading or writing a variable (e.g. x in x = 5;) 0041 POK_VarAccess, 0042 0043 /// Making a function call (e.g. fool()) 0044 POK_FunctionCall, 0045 0046 /// Passing a guarded variable by reference. 0047 POK_PassByRef, 0048 0049 /// Passing a pt-guarded variable by reference. 0050 POK_PtPassByRef, 0051 0052 /// Returning a guarded variable by reference. 0053 POK_ReturnByRef, 0054 0055 /// Returning a pt-guarded variable by reference. 0056 POK_PtReturnByRef, 0057 }; 0058 0059 /// This enum distinguishes between different kinds of lock actions. For 0060 /// example, it is an error to write a variable protected by shared version of a 0061 /// mutex. 0062 enum LockKind { 0063 /// Shared/reader lock of a mutex. 0064 LK_Shared, 0065 0066 /// Exclusive/writer lock of a mutex. 0067 LK_Exclusive, 0068 0069 /// Can be either Shared or Exclusive. 0070 LK_Generic 0071 }; 0072 0073 /// This enum distinguishes between different ways to access (read or write) a 0074 /// variable. 0075 enum AccessKind { 0076 /// Reading a variable. 0077 AK_Read, 0078 0079 /// Writing a variable. 0080 AK_Written 0081 }; 0082 0083 /// This enum distinguishes between different situations where we warn due to 0084 /// inconsistent locking. 0085 /// \enum SK_LockedSomeLoopIterations -- a mutex is locked for some but not all 0086 /// loop iterations. 0087 /// \enum SK_LockedSomePredecessors -- a mutex is locked in some but not all 0088 /// predecessors of a CFGBlock. 0089 /// \enum SK_LockedAtEndOfFunction -- a mutex is still locked at the end of a 0090 /// function. 0091 enum LockErrorKind { 0092 LEK_LockedSomeLoopIterations, 0093 LEK_LockedSomePredecessors, 0094 LEK_LockedAtEndOfFunction, 0095 LEK_NotLockedAtEndOfFunction 0096 }; 0097 0098 /// Handler class for thread safety warnings. 0099 class ThreadSafetyHandler { 0100 public: 0101 using Name = StringRef; 0102 0103 ThreadSafetyHandler() = default; 0104 virtual ~ThreadSafetyHandler(); 0105 0106 /// Warn about lock expressions which fail to resolve to lockable objects. 0107 /// \param Loc -- the SourceLocation of the unresolved expression. 0108 virtual void handleInvalidLockExp(SourceLocation Loc) {} 0109 0110 /// Warn about unlock function calls that do not have a prior matching lock 0111 /// expression. 0112 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0113 /// \param LockName -- A StringRef name for the lock expression, to be printed 0114 /// in the error message. 0115 /// \param Loc -- The SourceLocation of the Unlock 0116 /// \param LocPreviousUnlock -- If valid, the location of a previous Unlock. 0117 virtual void handleUnmatchedUnlock(StringRef Kind, Name LockName, 0118 SourceLocation Loc, 0119 SourceLocation LocPreviousUnlock) {} 0120 0121 /// Warn about an unlock function call that attempts to unlock a lock with 0122 /// the incorrect lock kind. For instance, a shared lock being unlocked 0123 /// exclusively, or vice versa. 0124 /// \param LockName -- A StringRef name for the lock expression, to be printed 0125 /// in the error message. 0126 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0127 /// \param Expected -- the kind of lock expected. 0128 /// \param Received -- the kind of lock received. 0129 /// \param LocLocked -- The SourceLocation of the Lock. 0130 /// \param LocUnlock -- The SourceLocation of the Unlock. 0131 virtual void handleIncorrectUnlockKind(StringRef Kind, Name LockName, 0132 LockKind Expected, LockKind Received, 0133 SourceLocation LocLocked, 0134 SourceLocation LocUnlock) {} 0135 0136 /// Warn about lock function calls for locks which are already held. 0137 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0138 /// \param LockName -- A StringRef name for the lock expression, to be printed 0139 /// in the error message. 0140 /// \param LocLocked -- The location of the first lock expression. 0141 /// \param LocDoubleLock -- The location of the second lock expression. 0142 virtual void handleDoubleLock(StringRef Kind, Name LockName, 0143 SourceLocation LocLocked, 0144 SourceLocation LocDoubleLock) {} 0145 0146 /// Warn about situations where a mutex is sometimes held and sometimes not. 0147 /// The three situations are: 0148 /// 1. a mutex is locked on an "if" branch but not the "else" branch, 0149 /// 2, or a mutex is only held at the start of some loop iterations, 0150 /// 3. or when a mutex is locked but not unlocked inside a function. 0151 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0152 /// \param LockName -- A StringRef name for the lock expression, to be printed 0153 /// in the error message. 0154 /// \param LocLocked -- The location of the lock expression where the mutex is 0155 /// locked 0156 /// \param LocEndOfScope -- The location of the end of the scope where the 0157 /// mutex is no longer held 0158 /// \param LEK -- which of the three above cases we should warn for 0159 virtual void handleMutexHeldEndOfScope(StringRef Kind, Name LockName, 0160 SourceLocation LocLocked, 0161 SourceLocation LocEndOfScope, 0162 LockErrorKind LEK) {} 0163 0164 /// Warn when a mutex is held exclusively and shared at the same point. For 0165 /// example, if a mutex is locked exclusively during an if branch and shared 0166 /// during the else branch. 0167 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0168 /// \param LockName -- A StringRef name for the lock expression, to be printed 0169 /// in the error message. 0170 /// \param Loc1 -- The location of the first lock expression. 0171 /// \param Loc2 -- The location of the second lock expression. 0172 virtual void handleExclusiveAndShared(StringRef Kind, Name LockName, 0173 SourceLocation Loc1, 0174 SourceLocation Loc2) {} 0175 0176 /// Warn when a protected operation occurs while no locks are held. 0177 /// \param D -- The decl for the protected variable or function 0178 /// \param POK -- The kind of protected operation (e.g. variable access) 0179 /// \param AK -- The kind of access (i.e. read or write) that occurred 0180 /// \param Loc -- The location of the protected operation. 0181 virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, 0182 AccessKind AK, SourceLocation Loc) {} 0183 0184 /// Warn when a protected operation occurs while the specific mutex protecting 0185 /// the operation is not locked. 0186 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0187 /// \param D -- The decl for the protected variable or function 0188 /// \param POK -- The kind of protected operation (e.g. variable access) 0189 /// \param LockName -- A StringRef name for the lock expression, to be printed 0190 /// in the error message. 0191 /// \param LK -- The kind of access (i.e. read or write) that occurred 0192 /// \param Loc -- The location of the protected operation. 0193 virtual void handleMutexNotHeld(StringRef Kind, const NamedDecl *D, 0194 ProtectedOperationKind POK, Name LockName, 0195 LockKind LK, SourceLocation Loc, 0196 Name *PossibleMatch = nullptr) {} 0197 0198 /// Warn when acquiring a lock that the negative capability is not held. 0199 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0200 /// \param LockName -- The name for the lock expression, to be printed in the 0201 /// diagnostic. 0202 /// \param Neg -- The name of the negative capability to be printed in the 0203 /// diagnostic. 0204 /// \param Loc -- The location of the protected operation. 0205 virtual void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg, 0206 SourceLocation Loc) {} 0207 0208 /// Warn when calling a function that a negative capability is not held. 0209 /// \param D -- The decl for the function requiring the negative capability. 0210 /// \param LockName -- The name for the lock expression, to be printed in the 0211 /// diagnostic. 0212 /// \param Loc -- The location of the protected operation. 0213 virtual void handleNegativeNotHeld(const NamedDecl *D, Name LockName, 0214 SourceLocation Loc) {} 0215 0216 /// Warn when a function is called while an excluded mutex is locked. For 0217 /// example, the mutex may be locked inside the function. 0218 /// \param Kind -- the capability's name parameter (role, mutex, etc). 0219 /// \param FunName -- The name of the function 0220 /// \param LockName -- A StringRef name for the lock expression, to be printed 0221 /// in the error message. 0222 /// \param Loc -- The location of the function call. 0223 virtual void handleFunExcludesLock(StringRef Kind, Name FunName, 0224 Name LockName, SourceLocation Loc) {} 0225 0226 /// Warn when an actual underlying mutex of a scoped lockable does not match 0227 /// the expected. 0228 /// \param Loc -- The location of the call expression. 0229 /// \param DLoc -- The location of the function declaration. 0230 /// \param ScopeName -- The name of the scope passed to the function. 0231 /// \param Kind -- The kind of the expected mutex. 0232 /// \param Expected -- The name of the expected mutex. 0233 /// \param Actual -- The name of the actual mutex. 0234 virtual void handleUnmatchedUnderlyingMutexes(SourceLocation Loc, 0235 SourceLocation DLoc, 0236 Name ScopeName, StringRef Kind, 0237 Name Expected, Name Actual) {} 0238 0239 /// Warn when we get fewer underlying mutexes than expected. 0240 /// \param Loc -- The location of the call expression. 0241 /// \param DLoc -- The location of the function declaration. 0242 /// \param ScopeName -- The name of the scope passed to the function. 0243 /// \param Kind -- The kind of the expected mutex. 0244 /// \param Expected -- The name of the expected mutex. 0245 virtual void handleExpectMoreUnderlyingMutexes(SourceLocation Loc, 0246 SourceLocation DLoc, 0247 Name ScopeName, StringRef Kind, 0248 Name Expected) {} 0249 0250 /// Warn when we get more underlying mutexes than expected. 0251 /// \param Loc -- The location of the call expression. 0252 /// \param DLoc -- The location of the function declaration. 0253 /// \param ScopeName -- The name of the scope passed to the function. 0254 /// \param Kind -- The kind of the actual mutex. 0255 /// \param Actual -- The name of the actual mutex. 0256 virtual void handleExpectFewerUnderlyingMutexes(SourceLocation Loc, 0257 SourceLocation DLoc, 0258 Name ScopeName, 0259 StringRef Kind, Name Actual) { 0260 } 0261 0262 /// Warn that L1 cannot be acquired before L2. 0263 virtual void handleLockAcquiredBefore(StringRef Kind, Name L1Name, 0264 Name L2Name, SourceLocation Loc) {} 0265 0266 /// Warn that there is a cycle in acquired_before/after dependencies. 0267 virtual void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) {} 0268 0269 /// Called by the analysis when starting analysis of a function. 0270 /// Used to issue suggestions for changes to annotations. 0271 virtual void enterFunction(const FunctionDecl *FD) {} 0272 0273 /// Called by the analysis when finishing analysis of a function. 0274 virtual void leaveFunction(const FunctionDecl *FD) {} 0275 0276 bool issueBetaWarnings() { return IssueBetaWarnings; } 0277 void setIssueBetaWarnings(bool b) { IssueBetaWarnings = b; } 0278 0279 private: 0280 bool IssueBetaWarnings = false; 0281 }; 0282 0283 /// Check a function's CFG for thread-safety violations. 0284 /// 0285 /// We traverse the blocks in the CFG, compute the set of mutexes that are held 0286 /// at the end of each block, and issue warnings for thread safety violations. 0287 /// Each block in the CFG is traversed exactly once. 0288 void runThreadSafetyAnalysis(AnalysisDeclContext &AC, 0289 ThreadSafetyHandler &Handler, 0290 BeforeSet **Bset); 0291 0292 void threadSafetyCleanup(BeforeSet *Cache); 0293 0294 /// Helper function that returns a LockKind required for the given level 0295 /// of access. 0296 LockKind getLockKindFromAccessKind(AccessKind AK); 0297 0298 } // namespace threadSafety 0299 } // namespace clang 0300 0301 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETY_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|