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 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, | |
157 HANDLE_FLAG_PROTECT_FROM_CLOSE)) { | |
158 DWORD error = GetLastError(); | |
159 // Even our unittests provide invalid/fabricated handles. Reducing scope | |
160 // of change to only deal with valid handles. | |
161 // TODO(shrikant): If this feature has to be permanent then decide right way | |
162 // for unit test ScopedProcessHandleInformation. | |
163 if (error != ERROR_INVALID_HANDLE) { | |
cpu_(ooo_6.6-7.5)
2015/05/08 02:45:18
what other error would SetHandleInformation could
| |
164 base::debug::Alias(&error); | |
165 CHECK(false); | |
166 } | |
167 } | |
168 | |
155 // Grab the thread id before the lock. | 169 // Grab the thread id before the lock. |
156 DWORD thread_id = GetCurrentThreadId(); | 170 DWORD thread_id = GetCurrentThreadId(); |
157 | 171 |
158 AutoNativeLock lock(*lock_); | 172 AutoNativeLock lock(*lock_); |
159 | 173 |
160 Info handle_info = { owner, pc1, pc2, thread_id }; | 174 Info handle_info = { owner, pc1, pc2, thread_id }; |
161 std::pair<HANDLE, Info> item(handle, handle_info); | 175 std::pair<HANDLE, Info> item(handle, handle_info); |
162 std::pair<HandleMap::iterator, bool> result = map_.insert(item); | 176 std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
163 if (!result.second) { | 177 if (!result.second) { |
164 Info other = result.first->second; | 178 Info other = result.first->second; |
165 base::debug::Alias(&other); | 179 base::debug::Alias(&other); |
166 CHECK(false); | 180 CHECK(false); |
167 } | 181 } |
168 } | 182 } |
169 | 183 |
170 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, | 184 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
171 const void* pc1, const void* pc2) { | 185 const void* pc1, const void* pc2) { |
172 if (!enabled_) | 186 if (!enabled_) |
173 return; | 187 return; |
174 | 188 |
189 // We expect handle to be protected till this point. | |
190 DWORD flags = 0; | |
191 if (::GetHandleInformation(handle, &flags)) { | |
192 CHECK_NE(0U, (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE)); | |
193 | |
194 // Unprotect handle so that it could be closed. | |
195 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0); | |
196 } | |
197 | |
175 AutoNativeLock lock(*lock_); | 198 AutoNativeLock lock(*lock_); |
176 HandleMap::iterator i = map_.find(handle); | 199 HandleMap::iterator i = map_.find(handle); |
177 if (i == map_.end()) | 200 if (i == map_.end()) |
178 CHECK(false); | 201 CHECK(false); |
179 | 202 |
180 Info other = i->second; | 203 Info other = i->second; |
181 if (other.owner != owner) { | 204 if (other.owner != owner) { |
182 base::debug::Alias(&other); | 205 base::debug::Alias(&other); |
183 CHECK(false); | 206 CHECK(false); |
184 } | 207 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 void DisableHandleVerifier() { | 256 void DisableHandleVerifier() { |
234 return ActiveVerifier::Get()->Disable(); | 257 return ActiveVerifier::Get()->Disable(); |
235 } | 258 } |
236 | 259 |
237 void OnHandleBeingClosed(HANDLE handle) { | 260 void OnHandleBeingClosed(HANDLE handle) { |
238 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 261 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
239 } | 262 } |
240 | 263 |
241 } // namespace win | 264 } // namespace win |
242 } // namespace base | 265 } // namespace base |
OLD | NEW |