scoped_handle_verifier.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SCOPED_HANDLE_VERIFIER_H_
  5. #define BASE_WIN_SCOPED_HANDLE_VERIFIER_H_
  6. #include "base/win/windows_types.h"
  7. #include <memory>
  8. #include <unordered_map>
  9. #include "base/base_export.h"
  10. #include "base/debug/stack_trace.h"
  11. #include "base/hash/hash.h"
  12. #include "base/synchronization/lock_impl.h"
  13. #include "base/threading/thread_local.h"
  14. namespace base {
  15. namespace win {
  16. namespace internal {
  17. struct HandleHash {
  18. size_t operator()(const HANDLE& handle) const {
  19. return base::FastHash(as_bytes(make_span(&handle, 1)));
  20. }
  21. };
  22. struct ScopedHandleVerifierInfo {
  23. ScopedHandleVerifierInfo(const void* owner,
  24. const void* pc1,
  25. const void* pc2,
  26. std::unique_ptr<debug::StackTrace> stack,
  27. DWORD thread_id);
  28. ~ScopedHandleVerifierInfo();
  29. ScopedHandleVerifierInfo(const ScopedHandleVerifierInfo&) = delete;
  30. ScopedHandleVerifierInfo& operator=(const ScopedHandleVerifierInfo&) = delete;
  31. ScopedHandleVerifierInfo(ScopedHandleVerifierInfo&&) noexcept;
  32. ScopedHandleVerifierInfo& operator=(ScopedHandleVerifierInfo&&) noexcept;
  33. const void* owner;
  34. const void* pc1;
  35. const void* pc2;
  36. std::unique_ptr<debug::StackTrace> stack;
  37. DWORD thread_id;
  38. };
  39. // Implements the actual object that is verifying handles for this process.
  40. // The active instance is shared across the module boundary but there is no
  41. // way to delete this object from the wrong side of it (or any side, actually).
  42. // We need [[clang::lto_visibility_public]] because instances of this class are
  43. // passed across module boundaries. This means different modules must have
  44. // compatible definitions of the class even when whole program optimization is
  45. // enabled - which is what this attribute accomplishes. The pragma stops MSVC
  46. // from emitting an unrecognized attribute warning.
  47. #pragma warning(push)
  48. #pragma warning(disable : 5030)
  49. class [[clang::lto_visibility_public]] ScopedHandleVerifier {
  50. #pragma warning(pop)
  51. public:
  52. explicit ScopedHandleVerifier(bool enabled);
  53. // Retrieves the current verifier.
  54. static ScopedHandleVerifier* Get();
  55. // The methods required by HandleTraits. They are virtual because we need to
  56. // forward the call execution to another module, instead of letting the
  57. // compiler call the version that is linked in the current module.
  58. virtual bool CloseHandle(HANDLE handle);
  59. virtual void StartTracking(HANDLE handle, const void* owner, const void* pc1,
  60. const void* pc2);
  61. virtual void StopTracking(HANDLE handle, const void* owner, const void* pc1,
  62. const void* pc2);
  63. virtual void Disable();
  64. virtual void OnHandleBeingClosed(HANDLE handle);
  65. virtual HMODULE GetModule() const;
  66. private:
  67. ~ScopedHandleVerifier(); // Not implemented.
  68. void StartTrackingImpl(HANDLE handle, const void* owner, const void* pc1,
  69. const void* pc2);
  70. void StopTrackingImpl(HANDLE handle, const void* owner, const void* pc1,
  71. const void* pc2);
  72. void OnHandleBeingClosedImpl(HANDLE handle);
  73. static base::internal::LockImpl* GetLock();
  74. static void InstallVerifier();
  75. static void ThreadSafeAssignOrCreateScopedHandleVerifier(
  76. ScopedHandleVerifier * existing_verifier, bool enabled);
  77. base::debug::StackTrace creation_stack_;
  78. bool enabled_;
  79. base::ThreadLocalBoolean closing_;
  80. base::internal::LockImpl* lock_;
  81. std::unordered_map<HANDLE, ScopedHandleVerifierInfo, HandleHash> map_;
  82. DISALLOW_COPY_AND_ASSIGN(ScopedHandleVerifier);
  83. };
  84. // This testing function returns the module that the HandleVerifier concrete
  85. // implementation was instantiated in.
  86. BASE_EXPORT HMODULE GetHandleVerifierModuleForTesting();
  87. } // namespace internal
  88. } // namespace win
  89. } // namespace base
  90. #endif // BASE_WIN_SCOPED_HANDLE_VERIFIER_H_