Chromium Code Reviews| 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 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 if (!g_active_verifier) | 110 if (!g_active_verifier) |
| 111 ActiveVerifier::InstallVerifier(); | 111 ActiveVerifier::InstallVerifier(); |
| 112 | 112 |
| 113 return g_active_verifier; | 113 return g_active_verifier; |
| 114 } | 114 } |
| 115 | 115 |
| 116 // static | 116 // static |
| 117 void ActiveVerifier::InstallVerifier() { | 117 void ActiveVerifier::InstallVerifier() { |
| 118 #if defined(COMPONENT_BUILD) | 118 #if defined(COMPONENT_BUILD) |
| 119 AutoNativeLock lock(g_lock.Get()); | 119 AutoNativeLock lock(g_lock.Get()); |
| 120 if (g_active_verifier) | |
| 121 return; | |
| 120 g_active_verifier = new ActiveVerifier(true); | 122 g_active_verifier = new ActiveVerifier(true); |
| 121 #else | 123 #else |
| 122 // If you are reading this, wondering why your process seems deadlocked, take | 124 // If you are reading this, wondering why your process seems deadlocked, take |
| 123 // a look at your DllMain code and remove things that should not be done | 125 // a look at your DllMain code and remove things that should not be done |
| 124 // there, like doing whatever gave you that nice windows handle you are trying | 126 // there, like doing whatever gave you that nice windows handle you are trying |
| 125 // to store in a ScopedHandle. | 127 // to store in a ScopedHandle. |
| 126 HMODULE main_module = ::GetModuleHandle(NULL); | 128 HMODULE main_module = ::GetModuleHandle(NULL); |
| 127 GetHandleVerifierFn get_handle_verifier = | 129 GetHandleVerifierFn get_handle_verifier = |
| 128 reinterpret_cast<GetHandleVerifierFn>(::GetProcAddress( | 130 reinterpret_cast<GetHandleVerifierFn>(::GetProcAddress( |
| 129 main_module, "GetHandleVerifier")); | 131 main_module, "GetHandleVerifier")); |
| 130 | 132 |
| 133 // This should only happen if running in a DLL is linked with base but the | |
| 134 // hosting EXE is not. In this case, create an ActiveVerifier for the current | |
| 135 // module but leave it disabled. | |
| 131 if (!get_handle_verifier) { | 136 if (!get_handle_verifier) { |
| 132 g_active_verifier = new ActiveVerifier(false); | 137 g_active_verifier = new ActiveVerifier(false); |
| 133 return; | 138 return; |
| 134 } | 139 } |
| 135 | 140 |
| 136 ActiveVerifier* verifier = | 141 // Check if in the main module. |
| 142 if (get_handle_verifier == GetHandleVerifier) { | |
| 143 AutoNativeLock lock(g_lock.Get()); | |
| 144 if (g_active_verifier) | |
| 145 return; | |
| 146 g_active_verifier = new ActiveVerifier(true); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 ActiveVerifier* main_module_verifier = | |
| 137 reinterpret_cast<ActiveVerifier*>(get_handle_verifier()); | 151 reinterpret_cast<ActiveVerifier*>(get_handle_verifier()); |
| 138 | 152 |
| 153 // Main module should always on-demand create a verifier. | |
| 154 DCHECK(main_module_verifier); | |
| 155 | |
| 139 // This lock only protects against races in this module, which is fine. | 156 // This lock only protects against races in this module, which is fine. |
| 140 AutoNativeLock lock(g_lock.Get()); | 157 AutoNativeLock lock(g_lock.Get()); |
| 141 g_active_verifier = verifier ? verifier : new ActiveVerifier(true); | 158 if (g_active_verifier) |
|
scottmg
2016/03/10 20:31:26
Maybe an extra comment here describing what we dis
Will Harris
2016/03/10 21:08:45
Done.
| |
| 159 return; | |
| 160 g_active_verifier = main_module_verifier; | |
| 142 #endif | 161 #endif |
| 143 } | 162 } |
| 144 | 163 |
| 145 bool ActiveVerifier::CloseHandle(HANDLE handle) { | 164 bool ActiveVerifier::CloseHandle(HANDLE handle) { |
| 146 if (!enabled_) | 165 if (!enabled_) |
| 147 return CloseHandleWrapper(handle); | 166 return CloseHandleWrapper(handle); |
| 148 | 167 |
| 149 closing_.Set(true); | 168 closing_.Set(true); |
| 150 CloseHandleWrapper(handle); | 169 CloseHandleWrapper(handle); |
| 151 closing_.Set(false); | 170 closing_.Set(false); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 | 233 |
| 215 Info other = i->second; | 234 Info other = i->second; |
| 216 base::debug::Alias(&other); | 235 base::debug::Alias(&other); |
| 217 base::debug::Alias(&creation_stack_); | 236 base::debug::Alias(&creation_stack_); |
| 218 CHECK(false); // CloseHandle called on tracked handle. | 237 CHECK(false); // CloseHandle called on tracked handle. |
| 219 } | 238 } |
| 220 | 239 |
| 221 } // namespace | 240 } // namespace |
| 222 | 241 |
| 223 void* GetHandleVerifier() { | 242 void* GetHandleVerifier() { |
| 224 return g_active_verifier; | 243 return ActiveVerifier::Get(); |
| 225 } | 244 } |
| 226 | 245 |
| 227 namespace base { | 246 namespace base { |
| 228 namespace win { | 247 namespace win { |
| 229 | 248 |
| 230 // Static. | 249 // Static. |
| 231 bool HandleTraits::CloseHandle(HANDLE handle) { | 250 bool HandleTraits::CloseHandle(HANDLE handle) { |
| 232 return ActiveVerifier::Get()->CloseHandle(handle); | 251 return ActiveVerifier::Get()->CloseHandle(handle); |
| 233 } | 252 } |
| 234 | 253 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 247 void DisableHandleVerifier() { | 266 void DisableHandleVerifier() { |
| 248 return ActiveVerifier::Get()->Disable(); | 267 return ActiveVerifier::Get()->Disable(); |
| 249 } | 268 } |
| 250 | 269 |
| 251 void OnHandleBeingClosed(HANDLE handle) { | 270 void OnHandleBeingClosed(HANDLE handle) { |
| 252 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 271 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
| 253 } | 272 } |
| 254 | 273 |
| 255 } // namespace win | 274 } // namespace win |
| 256 } // namespace base | 275 } // namespace base |
| OLD | NEW |