ThreadSafety.h revision 879a4334e4c4cab0c22ba91492ffc2838bbc21fc
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//===- ThreadSafety.h ------------------------------------------*- C++ --*-===//
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//                     The LLVM Compiler Infrastructure
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// This file is distributed under the University of Illinois Open Source
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// License. See LICENSE.TXT for details.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//===----------------------------------------------------------------------===//
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// A intra-procedural analysis for thread safety (e.g. deadlocks and race
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// conditions), based off of an annotation system.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more
1557212f9469c8056bab3c85243dbb904e386eab95reed@google.com// information.
1657212f9469c8056bab3c85243dbb904e386eab95reed@google.com//
1757212f9469c8056bab3c85243dbb904e386eab95reed@google.com//===----------------------------------------------------------------------===//
1857212f9469c8056bab3c85243dbb904e386eab95reed@google.com
1957212f9469c8056bab3c85243dbb904e386eab95reed@google.com#ifndef LLVM_CLANG_THREADSAFETY_H
2057212f9469c8056bab3c85243dbb904e386eab95reed@google.com#define LLVM_CLANG_THREADSAFETY_H
2157212f9469c8056bab3c85243dbb904e386eab95reed@google.com
2257212f9469c8056bab3c85243dbb904e386eab95reed@google.com#include "clang/Analysis/AnalysisContext.h"
2357212f9469c8056bab3c85243dbb904e386eab95reed@google.com#include "clang/Basic/SourceLocation.h"
2457212f9469c8056bab3c85243dbb904e386eab95reed@google.com#include "llvm/ADT/StringRef.h"
2557212f9469c8056bab3c85243dbb904e386eab95reed@google.com
2657212f9469c8056bab3c85243dbb904e386eab95reed@google.comnamespace clang {
2757212f9469c8056bab3c85243dbb904e386eab95reed@google.comnamespace thread_safety {
2857212f9469c8056bab3c85243dbb904e386eab95reed@google.com
2957212f9469c8056bab3c85243dbb904e386eab95reed@google.com/// This enum distinguishes between different kinds of operations that may
3057212f9469c8056bab3c85243dbb904e386eab95reed@google.com/// need to be protected by locks. We use this enum in error handling.
3157212f9469c8056bab3c85243dbb904e386eab95reed@google.comenum ProtectedOperationKind {
3257212f9469c8056bab3c85243dbb904e386eab95reed@google.com  POK_VarDereference, /// Dereferencing a variable (e.g. p in *p = 5;)
3357212f9469c8056bab3c85243dbb904e386eab95reed@google.com  POK_VarAccess, /// Reading or writing a variable (e.g. x in x = 5;)
3457212f9469c8056bab3c85243dbb904e386eab95reed@google.com  POK_FunctionCall /// Making a function call (e.g. fool())
3557212f9469c8056bab3c85243dbb904e386eab95reed@google.com};
3657212f9469c8056bab3c85243dbb904e386eab95reed@google.com
3757212f9469c8056bab3c85243dbb904e386eab95reed@google.com/// This enum distinguishes between different kinds of lock actions. For
3857212f9469c8056bab3c85243dbb904e386eab95reed@google.com/// example, it is an error to write a variable protected by shared version of a
3957212f9469c8056bab3c85243dbb904e386eab95reed@google.com/// mutex.
4057212f9469c8056bab3c85243dbb904e386eab95reed@google.comenum LockKind {
414b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  LK_Shared, /// Shared/reader lock of a mutex
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  LK_Exclusive /// Exclusive/writer lock of a mutex
430f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org};
440f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org
450f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// This enum distinguishes between different ways to access (read or write) a
460f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// variable.
470f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.orgenum AccessKind {
480f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  AK_Read, /// Reading a variable
490f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  AK_Written /// Writing a variable
50f7927dd60761ca45f26059a8ab434018676cb2e7skia.committer@gmail.com};
510f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org
520f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// This enum distinguishes between different situations where we warn due to
530f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// inconsistent locking.
540f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// \enum SK_LockedSomeLoopIterations -- a mutex is locked for some but not all
550f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// loop iterations.
560f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// \enum SK_LockedSomePredecessors -- a mutex is locked in some but not all
570f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// predecessors of a CFGBlock.
580f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// \enum SK_LockedAtEndOfFunction -- a mutex is still locked at the end of a
590f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// function.
600f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.orgenum LockErrorKind {
610f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  LEK_LockedSomeLoopIterations,
620f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  LEK_LockedSomePredecessors,
630f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  LEK_LockedAtEndOfFunction,
640f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  LEK_NotLockedAtEndOfFunction
650f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org};
660f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org
670f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org/// Handler class for thread safety warnings.
680f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.orgclass ThreadSafetyHandler {
690f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.orgpublic:
700f1fef834c73cb4f38023de0f98d05562d24f2bccommit-bot@chromium.org  typedef llvm::StringRef Name;
714b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  virtual ~ThreadSafetyHandler();
724b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// Warn about lock expressions which fail to resolve to lockable objects.
7444d37d9d7228e7409f4feea722c26f570434b8fdreed@google.com  /// \param Loc -- the SourceLocation of the unresolved expression.
757729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  virtual void handleInvalidLockExp(SourceLocation Loc) {}
767729534da419436ea6127545e9f79b0b47ccffb4reed@google.com
777729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// Warn about unlock function calls that do not have a prior matching lock
787729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// expression.
797729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
807729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// in the error message.
817729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// \param Loc -- The SourceLocation of the Unlock
827729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  virtual void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {}
837729534da419436ea6127545e9f79b0b47ccffb4reed@google.com
847729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// Warn about lock function calls for locks which are already held.
857729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
867729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// in the error message.
873a2682a77f996f649de7699c9f7bee046c6d4f17mtklein  /// \param Loc -- The location of the second lock expression.
885c341d1ff1b58cc2b88c52f4ee41faf53a2a7578reed@google.com  virtual void handleDoubleLock(Name LockName, SourceLocation Loc) {}
895c341d1ff1b58cc2b88c52f4ee41faf53a2a7578reed@google.com
905c341d1ff1b58cc2b88c52f4ee41faf53a2a7578reed@google.com  /// Warn about situations where a mutex is sometimes held and sometimes not.
915c341d1ff1b58cc2b88c52f4ee41faf53a2a7578reed@google.com  /// The three situations are:
927729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// 1. a mutex is locked on an "if" branch but not the "else" branch,
937729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// 2, or a mutex is only held at the start of some loop iterations,
947729534da419436ea6127545e9f79b0b47ccffb4reed@google.com  /// 3. or when a mutex is locked but not unlocked inside a function.
954b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
96eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com  /// in the error message.
974b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param LocLocked -- The location of the lock expression where the mutex is
984b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  ///               locked
994b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param LocEndOfScope -- The location of the end of the scope where the
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  ///               mutex is no longer held
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param LEK -- which of the three above cases we should warn for
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  virtual void handleMutexHeldEndOfScope(Name LockName,
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                         SourceLocation LocLocked,
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                         SourceLocation LocEndOfScope,
1054b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com                                         LockErrorKind LEK){}
1064b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
1074b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// Warn when a mutex is held exclusively and shared at the same point. For
1084b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// example, if a mutex is locked exclusively during an if branch and shared
1094b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// during the else branch.
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// in the error message.
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param Loc1 -- The location of the first lock expression.
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param Loc2 -- The location of the second lock expression.
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  virtual void handleExclusiveAndShared(Name LockName, SourceLocation Loc1,
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                        SourceLocation Loc2) {}
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// Warn when a protected operation occurs while no locks are held.
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param D -- The decl for the protected variable or function
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param POK -- The kind of protected operation (e.g. variable access)
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param AK -- The kind of access (i.e. read or write) that occurred
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param Loc -- The location of the protected operation.
1224b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
1234b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com                                 AccessKind AK, SourceLocation Loc) {}
1244b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
1254b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// Warn when a protected operation occurs while the specific mutex protecting
1264b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// the operation is not locked.
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param D -- The decl for the protected variable or function
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param POK -- The kind of protected operation (e.g. variable access)
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// in the error message.
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param LK -- The kind of access (i.e. read or write) that occurred
1324b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param Loc -- The location of the protected operation.
1334b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  virtual void handleMutexNotHeld(const NamedDecl *D,
1344b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com                                  ProtectedOperationKind POK, Name LockName,
1354b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com                                  LockKind LK, SourceLocation Loc) {}
1364b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
1374b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// Warn when a function is called while an excluded mutex is locked. For
1384b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// example, the mutex may be locked inside the function.
1394b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param FunName -- The name of the function
1404b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com  /// \param LockName -- A StringRef name for the lock expression, to be printed
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// in the error message.
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /// \param Loc -- The location of the function call.
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  virtual void handleFunExcludesLock(Name FunName, Name LockName,
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                     SourceLocation Loc) {}
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1464b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
1474b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com/// \brief Check a function's CFG for thread-safety violations.
1484b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com///
149f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com/// We traverse the blocks in the CFG, compute the set of mutexes that are held
150f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com/// at the end of each block, and issue warnings for thread safety violations.
151f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com/// Each block in the CFG is traversed exactly once.
152f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comvoid runThreadSafetyAnalysis(AnalysisDeclContext &AC,
153f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                             ThreadSafetyHandler &Handler);
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/// \brief Helper function that returns a LockKind required for the given level
1564b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com/// of access.
1574b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.comLockKind getLockKindFromAccessKind(AccessKind AK);
1584b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com
1594b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com}} // end namespace clang::thread_safety
1604b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com#endif
1612092c40a0d0a89a91cc4b6bb2b1b992fd4f02cd7george@mozilla.com