| 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 28 matching lines...) Expand all Loading... |
| 39 DWORD thread_id; | 39 DWORD thread_id; |
| 40 }; | 40 }; |
| 41 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap; | 41 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap; |
| 42 | 42 |
| 43 // g_lock protects the handle map and setting g_active_verifier. | 43 // g_lock protects the handle map and setting g_active_verifier. |
| 44 typedef base::internal::LockImpl NativeLock; | 44 typedef base::internal::LockImpl NativeLock; |
| 45 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; | 45 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; |
| 46 | 46 |
| 47 bool CloseHandleWrapper(HANDLE handle) { | 47 bool CloseHandleWrapper(HANDLE handle) { |
| 48 if (!::CloseHandle(handle)) | 48 if (!::CloseHandle(handle)) |
| 49 LOG(FATAL) << "CloseHandle failed."; | 49 CHECK(false); // CloseHandle failed. |
| 50 return true; | 50 return true; |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Simple automatic locking using a native critical section so it supports | 53 // Simple automatic locking using a native critical section so it supports |
| 54 // recursive locking. | 54 // recursive locking. |
| 55 class AutoNativeLock { | 55 class AutoNativeLock { |
| 56 public: | 56 public: |
| 57 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) { | 57 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) { |
| 58 lock_.Lock(); | 58 lock_.Lock(); |
| 59 } | 59 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 DWORD thread_id = GetCurrentThreadId(); | 161 DWORD thread_id = GetCurrentThreadId(); |
| 162 | 162 |
| 163 AutoNativeLock lock(*lock_); | 163 AutoNativeLock lock(*lock_); |
| 164 | 164 |
| 165 Info handle_info = { owner, pc1, pc2, base::debug::StackTrace(), thread_id }; | 165 Info handle_info = { owner, pc1, pc2, base::debug::StackTrace(), thread_id }; |
| 166 std::pair<HANDLE, Info> item(handle, handle_info); | 166 std::pair<HANDLE, Info> item(handle, handle_info); |
| 167 std::pair<HandleMap::iterator, bool> result = map_.insert(item); | 167 std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
| 168 if (!result.second) { | 168 if (!result.second) { |
| 169 Info other = result.first->second; | 169 Info other = result.first->second; |
| 170 base::debug::Alias(&other); | 170 base::debug::Alias(&other); |
| 171 LOG(FATAL) << "Attempt to start tracking already tracked handle."; | 171 CHECK(false); // Attempt to start tracking already tracked handle. |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, | 175 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
| 176 const void* pc1, const void* pc2) { | 176 const void* pc1, const void* pc2) { |
| 177 if (!enabled_) | 177 if (!enabled_) |
| 178 return; | 178 return; |
| 179 | 179 |
| 180 AutoNativeLock lock(*lock_); | 180 AutoNativeLock lock(*lock_); |
| 181 HandleMap::iterator i = map_.find(handle); | 181 HandleMap::iterator i = map_.find(handle); |
| 182 if (i == map_.end()) | 182 if (i == map_.end()) |
| 183 LOG(FATAL) << "Attempting to close an untracked handle."; | 183 CHECK(false); // Attempting to close an untracked handle. |
| 184 | 184 |
| 185 Info other = i->second; | 185 Info other = i->second; |
| 186 if (other.owner != owner) { | 186 if (other.owner != owner) { |
| 187 base::debug::Alias(&other); | 187 base::debug::Alias(&other); |
| 188 LOG(FATAL) << "Attempting to close a handle not owned by opener."; | 188 CHECK(false); // Attempting to close a handle not owned by opener. |
| 189 } | 189 } |
| 190 | 190 |
| 191 map_.erase(i); | 191 map_.erase(i); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void ActiveVerifier::Disable() { | 194 void ActiveVerifier::Disable() { |
| 195 enabled_ = false; | 195 enabled_ = false; |
| 196 } | 196 } |
| 197 | 197 |
| 198 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { | 198 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { |
| 199 if (!enabled_) | 199 if (!enabled_) |
| 200 return; | 200 return; |
| 201 | 201 |
| 202 AutoNativeLock lock(*lock_); | 202 AutoNativeLock lock(*lock_); |
| 203 if (closing_) | 203 if (closing_) |
| 204 return; | 204 return; |
| 205 | 205 |
| 206 HandleMap::iterator i = map_.find(handle); | 206 HandleMap::iterator i = map_.find(handle); |
| 207 if (i == map_.end()) | 207 if (i == map_.end()) |
| 208 return; | 208 return; |
| 209 | 209 |
| 210 Info other = i->second; | 210 Info other = i->second; |
| 211 base::debug::Alias(&other); | 211 base::debug::Alias(&other); |
| 212 LOG(FATAL) << "CloseHandle called on tracked handle."; | 212 CHECK(false); // CloseHandle called on tracked handle. |
| 213 } | 213 } |
| 214 | 214 |
| 215 } // namespace | 215 } // namespace |
| 216 | 216 |
| 217 void* GetHandleVerifier() { | 217 void* GetHandleVerifier() { |
| 218 return g_active_verifier; | 218 return g_active_verifier; |
| 219 } | 219 } |
| 220 | 220 |
| 221 namespace base { | 221 namespace base { |
| 222 namespace win { | 222 namespace win { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 241 void DisableHandleVerifier() { | 241 void DisableHandleVerifier() { |
| 242 return ActiveVerifier::Get()->Disable(); | 242 return ActiveVerifier::Get()->Disable(); |
| 243 } | 243 } |
| 244 | 244 |
| 245 void OnHandleBeingClosed(HANDLE handle) { | 245 void OnHandleBeingClosed(HANDLE handle) { |
| 246 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 246 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
| 247 } | 247 } |
| 248 | 248 |
| 249 } // namespace win | 249 } // namespace win |
| 250 } // namespace base | 250 } // namespace base |
| OLD | NEW |