scoped_handle_verifier.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <unordered_map>
  8. #include "base/base_export.h"
  9. #include "base/debug/stack_trace.h"
  10. #include "base/hash/hash.h"
  11. #include "base/synchronization/lock_impl.h"
  12. #include "base/threading/thread_local.h"
  13. namespace base {
  14. namespace win {
  15. namespace internal {
  16. struct HandleHash {
  17. size_t operator()(const HANDLE& handle) const {
  18. return base::FastHash(as_bytes(make_span(&handle, 1)));
  19. }
  20. };
  21. struct ScopedHandleVerifierInfo {
  22. const void* owner;
  23. const void* pc1;
  24. const void* pc2;
  25. base::debug::StackTrace stack;
  26. DWORD thread_id;
  27. };
  28. // Implements the actual object that is verifying handles for this process.
  29. // The active instance is shared across the module boundary but there is no
  30. // way to delete this object from the wrong side of it (or any side, actually).
  31. // We need [[clang::lto_visibility_public]] because instances of this class are
  32. // passed across module boundaries. This means different modules must have
  33. // compatible definitions of the class even when whole program optimization is
  34. // enabled - which is what this attribute accomplishes. The pragma stops MSVC
  35. // from emitting an unrecognized attribute warning.
  36. #pragma warning(push)
  37. #pragma warning(disable : 5030)
  38. class [[clang::lto_visibility_public]] ScopedHandleVerifier {
  39. #pragma warning(pop)
  40. public:
  41. explicit ScopedHandleVerifier(bool enabled);
  42. // Retrieves the current verifier.
  43. static ScopedHandleVerifier* Get();
  44. // The methods required by HandleTraits. They are virtual because we need to
  45. // forward the call execution to another module, instead of letting the
  46. // compiler call the version that is linked in the current module.
  47. virtual bool CloseHandle(HANDLE handle);
  48. virtual void StartTracking(HANDLE handle, const void* owner, const void* pc1,
  49. const void* pc2);
  50. virtual void StopTracking(HANDLE handle, const void* owner, const void* pc1,
  51. const void* pc2);
  52. virtual void Disable();
  53. virtual void OnHandleBeingClosed(HANDLE handle);
  54. virtual HMODULE GetModule() const;
  55. private:
  56. ~ScopedHandleVerifier(); // Not implemented.
  57. static base::internal::LockImpl* GetLock();
  58. static void InstallVerifier();
  59. base::debug::StackTrace creation_stack_;
  60. bool enabled_;
  61. base::ThreadLocalBoolean closing_;
  62. base::internal::LockImpl* lock_;
  63. std::unordered_map<HANDLE, ScopedHandleVerifierInfo, HandleHash> map_;
  64. DISALLOW_COPY_AND_ASSIGN(ScopedHandleVerifier);
  65. };
  66. // This testing function returns the module that the ActiveVerifier concrete
  67. // implementation was instantiated in.
  68. BASE_EXPORT HMODULE GetHandleVerifierModuleForTesting();
  69. } // namespace internal
  70. } // namespace win
  71. } // namespace base
  72. #endif // BASE_WIN_SCOPED_HANDLE_VERIFIER_H_