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