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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 closing_ = false; | 145 closing_ = false; |
146 | 146 |
147 return true; | 147 return true; |
148 } | 148 } |
149 | 149 |
150 void ActiveVerifier::StartTracking(HANDLE handle, const void* owner, | 150 void ActiveVerifier::StartTracking(HANDLE handle, const void* owner, |
151 const void* pc1, const void* pc2) { | 151 const void* pc1, const void* pc2) { |
152 if (!enabled_) | 152 if (!enabled_) |
153 return; | 153 return; |
154 | 154 |
155 // Idea here is to make our handles non-closable until we close it ourselves. | |
156 // Handles provided could be totally fabricated especially through our | |
157 // unittest, we are ignoring that for now by not checking return value. | |
158 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, | |
159 HANDLE_FLAG_PROTECT_FROM_CLOSE); | |
Sébastien Marchand
2015/07/22 15:01:08
This doesn't work if |handle| is as been created l
Sébastien Marchand
2015/07/22 17:37:04
Apparently this is an accepted side effect of your
| |
160 | |
155 // Grab the thread id before the lock. | 161 // Grab the thread id before the lock. |
156 DWORD thread_id = GetCurrentThreadId(); | 162 DWORD thread_id = GetCurrentThreadId(); |
157 | 163 |
158 AutoNativeLock lock(*lock_); | 164 AutoNativeLock lock(*lock_); |
159 | 165 |
160 Info handle_info = { owner, pc1, pc2, thread_id }; | 166 Info handle_info = { owner, pc1, pc2, thread_id }; |
161 std::pair<HANDLE, Info> item(handle, handle_info); | 167 std::pair<HANDLE, Info> item(handle, handle_info); |
162 std::pair<HandleMap::iterator, bool> result = map_.insert(item); | 168 std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
163 if (!result.second) { | 169 if (!result.second) { |
164 Info other = result.first->second; | 170 Info other = result.first->second; |
165 base::debug::Alias(&other); | 171 base::debug::Alias(&other); |
166 CHECK(false); | 172 CHECK(false); |
167 } | 173 } |
168 } | 174 } |
169 | 175 |
170 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, | 176 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
171 const void* pc1, const void* pc2) { | 177 const void* pc1, const void* pc2) { |
172 if (!enabled_) | 178 if (!enabled_) |
173 return; | 179 return; |
174 | 180 |
181 // We expect handle to be protected till this point. | |
182 DWORD flags = 0; | |
183 if (::GetHandleInformation(handle, &flags)) { | |
184 CHECK_NE(0U, (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE)); | |
185 | |
186 // Unprotect handle so that it could be closed. | |
187 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0); | |
188 } | |
189 | |
175 AutoNativeLock lock(*lock_); | 190 AutoNativeLock lock(*lock_); |
176 HandleMap::iterator i = map_.find(handle); | 191 HandleMap::iterator i = map_.find(handle); |
177 if (i == map_.end()) | 192 if (i == map_.end()) |
178 CHECK(false); | 193 CHECK(false); |
179 | 194 |
180 Info other = i->second; | 195 Info other = i->second; |
181 if (other.owner != owner) { | 196 if (other.owner != owner) { |
182 base::debug::Alias(&other); | 197 base::debug::Alias(&other); |
183 CHECK(false); | 198 CHECK(false); |
184 } | 199 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 void DisableHandleVerifier() { | 248 void DisableHandleVerifier() { |
234 return ActiveVerifier::Get()->Disable(); | 249 return ActiveVerifier::Get()->Disable(); |
235 } | 250 } |
236 | 251 |
237 void OnHandleBeingClosed(HANDLE handle) { | 252 void OnHandleBeingClosed(HANDLE handle) { |
238 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 253 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
239 } | 254 } |
240 | 255 |
241 } // namespace win | 256 } // namespace win |
242 } // namespace base | 257 } // namespace base |
OLD | NEW |