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