tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

scoped_handle_verifier.h (4173B)


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