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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 g_active_verifier = new ActiveVerifier(false); | 125 g_active_verifier = new ActiveVerifier(false); |
126 return; | 126 return; |
127 } | 127 } |
128 | 128 |
129 ActiveVerifier* verifier = | 129 ActiveVerifier* verifier = |
130 reinterpret_cast<ActiveVerifier*>(get_handle_verifier()); | 130 reinterpret_cast<ActiveVerifier*>(get_handle_verifier()); |
131 | 131 |
132 // This lock only protects against races in this module, which is fine. | 132 // This lock only protects against races in this module, which is fine. |
133 AutoNativeLock lock(g_lock.Get()); | 133 AutoNativeLock lock(g_lock.Get()); |
134 g_active_verifier = verifier ? verifier : new ActiveVerifier(true); | 134 g_active_verifier = verifier ? verifier : new ActiveVerifier(true); |
135 | |
136 // TODO(shrikant): Enable handle verifier after figuring out | |
137 // AppContainer/DuplicateHandle error. | |
138 g_active_verifier->Disable(); | |
139 #endif | 135 #endif |
140 } | 136 } |
141 | 137 |
142 bool ActiveVerifier::CloseHandle(HANDLE handle) { | 138 bool ActiveVerifier::CloseHandle(HANDLE handle) { |
143 if (!enabled_) | 139 if (!enabled_) |
144 return CloseHandleWrapper(handle); | 140 return CloseHandleWrapper(handle); |
145 | 141 |
146 AutoNativeLock lock(*lock_); | 142 AutoNativeLock lock(*lock_); |
147 closing_ = true; | 143 closing_ = true; |
148 CloseHandleWrapper(handle); | 144 CloseHandleWrapper(handle); |
149 closing_ = false; | 145 closing_ = false; |
150 | 146 |
151 return true; | 147 return true; |
152 } | 148 } |
153 | 149 |
154 void ActiveVerifier::StartTracking(HANDLE handle, const void* owner, | 150 void ActiveVerifier::StartTracking(HANDLE handle, const void* owner, |
155 const void* pc1, const void* pc2) { | 151 const void* pc1, const void* pc2) { |
156 if (!enabled_) | 152 if (!enabled_) |
157 return; | 153 return; |
158 | 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); |
| 160 |
159 // Grab the thread id before the lock. | 161 // Grab the thread id before the lock. |
160 DWORD thread_id = GetCurrentThreadId(); | 162 DWORD thread_id = GetCurrentThreadId(); |
161 | 163 |
162 AutoNativeLock lock(*lock_); | 164 AutoNativeLock lock(*lock_); |
163 | 165 |
164 Info handle_info = { owner, pc1, pc2, thread_id }; | 166 Info handle_info = { owner, pc1, pc2, thread_id }; |
165 std::pair<HANDLE, Info> item(handle, handle_info); | 167 std::pair<HANDLE, Info> item(handle, handle_info); |
166 std::pair<HandleMap::iterator, bool> result = map_.insert(item); | 168 std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
167 if (!result.second) { | 169 if (!result.second) { |
168 Info other = result.first->second; | 170 Info other = result.first->second; |
169 base::debug::Alias(&other); | 171 base::debug::Alias(&other); |
170 CHECK(false); | 172 CHECK(false); |
171 } | 173 } |
172 } | 174 } |
173 | 175 |
174 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, | 176 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
175 const void* pc1, const void* pc2) { | 177 const void* pc1, const void* pc2) { |
176 if (!enabled_) | 178 if (!enabled_) |
177 return; | 179 return; |
178 | 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 |
179 AutoNativeLock lock(*lock_); | 190 AutoNativeLock lock(*lock_); |
180 HandleMap::iterator i = map_.find(handle); | 191 HandleMap::iterator i = map_.find(handle); |
181 if (i == map_.end()) | 192 if (i == map_.end()) |
182 CHECK(false); | 193 CHECK(false); |
183 | 194 |
184 Info other = i->second; | 195 Info other = i->second; |
185 if (other.owner != owner) { | 196 if (other.owner != owner) { |
186 base::debug::Alias(&other); | 197 base::debug::Alias(&other); |
187 CHECK(false); | 198 CHECK(false); |
188 } | 199 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 void DisableHandleVerifier() { | 248 void DisableHandleVerifier() { |
238 return ActiveVerifier::Get()->Disable(); | 249 return ActiveVerifier::Get()->Disable(); |
239 } | 250 } |
240 | 251 |
241 void OnHandleBeingClosed(HANDLE handle) { | 252 void OnHandleBeingClosed(HANDLE handle) { |
242 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 253 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
243 } | 254 } |
244 | 255 |
245 } // namespace win | 256 } // namespace win |
246 } // namespace base | 257 } // namespace base |
OLD | NEW |