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 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, | |
157 HANDLE_FLAG_PROTECT_FROM_CLOSE); | |
158 | |
159 // Grab the thread id before the lock. | 159 // Grab the thread id before the lock. |
160 DWORD thread_id = GetCurrentThreadId(); | 160 DWORD thread_id = GetCurrentThreadId(); |
161 | 161 |
162 AutoNativeLock lock(*lock_); | 162 AutoNativeLock lock(*lock_); |
163 | 163 |
164 Info handle_info = { owner, pc1, pc2, thread_id }; | 164 Info handle_info = { owner, pc1, pc2, thread_id }; |
165 std::pair<HANDLE, Info> item(handle, handle_info); | 165 std::pair<HANDLE, Info> item(handle, handle_info); |
166 std::pair<HandleMap::iterator, bool> result = map_.insert(item); | 166 std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
167 if (!result.second) { | 167 if (!result.second) { |
168 Info other = result.first->second; | 168 Info other = result.first->second; |
169 base::debug::Alias(&other); | 169 base::debug::Alias(&other); |
170 CHECK(false); | 170 CHECK(false); |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, | 174 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
175 const void* pc1, const void* pc2) { | 175 const void* pc1, const void* pc2) { |
176 if (!enabled_) | 176 if (!enabled_) |
177 return; | 177 return; |
178 | 178 |
179 // We expect handle to be protected till this point. | |
180 DWORD flags = 0; | |
181 ::GetHandleInformation(handle, &flags); | |
182 if (!(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE)) | |
grt (UTC plus 2)
2015/05/01 13:26:14
minor style nit: check what you care about rather
Shrikant Kelkar
2015/05/01 17:48:41
Done.
| |
183 CHECK(FALSE); | |
184 | |
185 // Unprotect handle so that it could be closed. | |
186 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0); | |
187 | |
179 AutoNativeLock lock(*lock_); | 188 AutoNativeLock lock(*lock_); |
180 HandleMap::iterator i = map_.find(handle); | 189 HandleMap::iterator i = map_.find(handle); |
181 if (i == map_.end()) | 190 if (i == map_.end()) |
182 CHECK(false); | 191 CHECK(false); |
183 | 192 |
184 Info other = i->second; | 193 Info other = i->second; |
185 if (other.owner != owner) { | 194 if (other.owner != owner) { |
186 base::debug::Alias(&other); | 195 base::debug::Alias(&other); |
187 CHECK(false); | 196 CHECK(false); |
188 } | 197 } |
189 | 198 |
190 map_.erase(i); | 199 map_.erase(i); |
191 } | 200 } |
192 | 201 |
193 void ActiveVerifier::Disable() { | 202 void ActiveVerifier::Disable() { |
194 enabled_ = false; | 203 enabled_ = false; |
195 } | 204 } |
196 | 205 |
197 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { | 206 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { |
198 AutoNativeLock lock(*lock_); | 207 AutoNativeLock lock(*lock_); |
199 if (closing_) | 208 if (closing_) |
200 return; | 209 return; |
201 | 210 |
202 HandleMap::iterator i = map_.find(handle); | 211 HandleMap::iterator i = map_.find(handle); |
203 if (i == map_.end()) | 212 if (i == map_.end()) |
204 return; | 213 return; |
205 | 214 |
215 // Mask out all protected handle close attempts. This will give us | |
216 // idea if protecting handle has any effect or not. | |
217 // TODO(shrikant): Remove this code once we see results of the above | |
218 // mentioned experiment. | |
219 DWORD flags = 0; | |
220 ::GetHandleInformation(handle, &flags); | |
221 if (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE) | |
222 return; | |
223 | |
206 Info other = i->second; | 224 Info other = i->second; |
207 base::debug::Alias(&other); | 225 base::debug::Alias(&other); |
208 CHECK(false); | 226 CHECK(false); |
209 } | 227 } |
210 | 228 |
211 } // namespace | 229 } // namespace |
212 | 230 |
213 void* GetHandleVerifier() { | 231 void* GetHandleVerifier() { |
214 return g_active_verifier; | 232 return g_active_verifier; |
215 } | 233 } |
(...skipping 21 matching lines...) Expand all Loading... | |
237 void DisableHandleVerifier() { | 255 void DisableHandleVerifier() { |
238 return ActiveVerifier::Get()->Disable(); | 256 return ActiveVerifier::Get()->Disable(); |
239 } | 257 } |
240 | 258 |
241 void OnHandleBeingClosed(HANDLE handle) { | 259 void OnHandleBeingClosed(HANDLE handle) { |
242 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 260 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
243 } | 261 } |
244 | 262 |
245 } // namespace win | 263 } // namespace win |
246 } // namespace base | 264 } // namespace base |
OLD | NEW |