| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/win/scoped_handle.h" | 5 #include "base/win/scoped_handle.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 | 10 |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/debug/stack_trace.h" | 12 #include "base/debug/stack_trace.h" |
| 13 #include "base/hash.h" | 13 #include "base/hash.h" |
| 14 #include "base/lazy_instance.h" | |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/synchronization/lock_impl.h" | 16 #include "base/synchronization/lock_impl.h" |
| 18 #include "base/threading/thread_local.h" | 17 #include "base/threading/thread_local.h" |
| 19 #include "base/win/base_features.h" | 18 #include "base/win/base_features.h" |
| 20 #include "base/win/current_module.h" | 19 #include "base/win/current_module.h" |
| 21 | 20 |
| 22 extern "C" { | 21 extern "C" { |
| 23 __declspec(dllexport) void* GetHandleVerifier(); | 22 __declspec(dllexport) void* GetHandleVerifier(); |
| 24 typedef void* (*GetHandleVerifierFn)(); | 23 typedef void* (*GetHandleVerifierFn)(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 | 35 |
| 37 struct Info { | 36 struct Info { |
| 38 const void* owner; | 37 const void* owner; |
| 39 const void* pc1; | 38 const void* pc1; |
| 40 const void* pc2; | 39 const void* pc2; |
| 41 base::debug::StackTrace stack; | 40 base::debug::StackTrace stack; |
| 42 DWORD thread_id; | 41 DWORD thread_id; |
| 43 }; | 42 }; |
| 44 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap; | 43 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap; |
| 45 | 44 |
| 46 // g_lock protects the handle map and setting g_active_verifier within this | 45 // GetLock() protects the handle map and setting g_active_verifier within this |
| 47 // module. | 46 // module. |
| 48 typedef base::internal::LockImpl NativeLock; | 47 typedef base::internal::LockImpl NativeLock; |
| 49 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; | 48 NativeLock* GetLock() { |
| 49 static auto native_lock = new NativeLock(); |
| 50 return native_lock; |
| 51 } |
| 50 | 52 |
| 51 // Simple automatic locking using a native critical section so it supports | 53 // Simple automatic locking using a native critical section so it supports |
| 52 // recursive locking. | 54 // recursive locking. |
| 53 class AutoNativeLock { | 55 class AutoNativeLock { |
| 54 public: | 56 public: |
| 55 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) { | 57 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) { |
| 56 lock_.Lock(); | 58 lock_.Lock(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 ~AutoNativeLock() { | 61 ~AutoNativeLock() { |
| 60 lock_.Unlock(); | 62 lock_.Unlock(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 private: | 65 private: |
| 64 NativeLock& lock_; | 66 NativeLock& lock_; |
| 65 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock); | 67 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock); |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 // Implements the actual object that is verifying handles for this process. | 70 // Implements the actual object that is verifying handles for this process. |
| 69 // The active instance is shared across the module boundary but there is no | 71 // The active instance is shared across the module boundary but there is no |
| 70 // way to delete this object from the wrong side of it (or any side, actually). | 72 // way to delete this object from the wrong side of it (or any side, actually). |
| 71 class ActiveVerifier { | 73 class ActiveVerifier { |
| 72 public: | 74 public: |
| 73 explicit ActiveVerifier(bool enabled) | 75 explicit ActiveVerifier(bool enabled) : enabled_(enabled), lock_(GetLock()) {} |
| 74 : enabled_(enabled), lock_(g_lock.Pointer()) { | |
| 75 } | |
| 76 | 76 |
| 77 // Retrieves the current verifier. | 77 // Retrieves the current verifier. |
| 78 static ActiveVerifier* Get(); | 78 static ActiveVerifier* Get(); |
| 79 | 79 |
| 80 // The methods required by HandleTraits. They are virtual because we need to | 80 // The methods required by HandleTraits. They are virtual because we need to |
| 81 // forward the call execution to another module, instead of letting the | 81 // forward the call execution to another module, instead of letting the |
| 82 // compiler call the version that is linked in the current module. | 82 // compiler call the version that is linked in the current module. |
| 83 virtual bool CloseHandle(HANDLE handle); | 83 virtual bool CloseHandle(HANDLE handle); |
| 84 virtual void StartTracking(HANDLE handle, const void* owner, | 84 virtual void StartTracking(HANDLE handle, const void* owner, |
| 85 const void* pc1, const void* pc2); | 85 const void* pc1, const void* pc2); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 110 | 110 |
| 111 return g_active_verifier; | 111 return g_active_verifier; |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool CloseHandleWrapper(HANDLE handle) { | 114 bool CloseHandleWrapper(HANDLE handle) { |
| 115 if (!::CloseHandle(handle)) | 115 if (!::CloseHandle(handle)) |
| 116 CHECK(false); // CloseHandle failed. | 116 CHECK(false); // CloseHandle failed. |
| 117 return true; | 117 return true; |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Assigns the g_active_verifier global within the g_lock lock. | 120 // Assigns the g_active_verifier global within the GetLock() lock. |
| 121 // If |existing_verifier| is non-null then |enabled| is ignored. | 121 // If |existing_verifier| is non-null then |enabled| is ignored. |
| 122 void ThreadSafeAssignOrCreateActiveVerifier(ActiveVerifier* existing_verifier, | 122 void ThreadSafeAssignOrCreateActiveVerifier(ActiveVerifier* existing_verifier, |
| 123 bool enabled) { | 123 bool enabled) { |
| 124 AutoNativeLock lock(g_lock.Get()); | 124 AutoNativeLock lock(*GetLock()); |
| 125 // Another thread in this module might be trying to assign the global | 125 // Another thread in this module might be trying to assign the global |
| 126 // verifier, so check that within the lock here. | 126 // verifier, so check that within the lock here. |
| 127 if (g_active_verifier) | 127 if (g_active_verifier) |
| 128 return; | 128 return; |
| 129 g_active_verifier = | 129 g_active_verifier = |
| 130 existing_verifier ? existing_verifier : new ActiveVerifier(enabled); | 130 existing_verifier ? existing_verifier : new ActiveVerifier(enabled); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // static | 133 // static |
| 134 void ActiveVerifier::InstallVerifier() { | 134 void ActiveVerifier::InstallVerifier() { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 void OnHandleBeingClosed(HANDLE handle) { | 282 void OnHandleBeingClosed(HANDLE handle) { |
| 283 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 283 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
| 284 } | 284 } |
| 285 | 285 |
| 286 HMODULE GetHandleVerifierModuleForTesting() { | 286 HMODULE GetHandleVerifierModuleForTesting() { |
| 287 return g_active_verifier->GetModule(); | 287 return g_active_verifier->GetModule(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 } // namespace win | 290 } // namespace win |
| 291 } // namespace base | 291 } // namespace base |
| OLD | NEW |