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