Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: base/win/scoped_handle.cc

Issue 1113063004: Add HANDLE_FLAG_PROTECT_FROM_CLOSE flag to Tracked/ScopedHandle. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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();
rvargas (doing something else) 2015/04/30 23:57:17 We should not be disabling the verifier on all pla
Shrikant Kelkar 2015/05/01 00:57:31 Acknowledged.
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
159 // Grab the thread id before the lock. 155 // Grab the thread id before the lock.
160 DWORD thread_id = GetCurrentThreadId(); 156 DWORD thread_id = GetCurrentThreadId();
161 157
162 AutoNativeLock lock(*lock_); 158 AutoNativeLock lock(*lock_);
163 159
164 Info handle_info = { owner, pc1, pc2, thread_id }; 160 Info handle_info = { owner, pc1, pc2, thread_id };
161
162 // Idea here is to make our handles non-closable until we close it ourselves.
163 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE,
164 HANDLE_FLAG_PROTECT_FROM_CLOSE);
cpu_(ooo_6.6-7.5) 2015/05/01 00:42:52 this can be done outside the lock.
Shrikant Kelkar 2015/05/01 00:57:31 Done.
165
Will Harris 2015/05/01 00:03:45 I don't think you can call SetHandleInformation on
cpu_(ooo_6.6-7.5) 2015/05/01 00:42:51 Hopefully they are not using ScopedHandle because
Will Harris 2015/05/03 21:55:19 ScopedCreateDC is specifically using VerifierTrait
rvargas (doing something else) 2015/05/06 01:47:23 That's wrong, isn't it? the verifier doesn't work
165 std::pair<HANDLE, Info> item(handle, handle_info); 166 std::pair<HANDLE, Info> item(handle, handle_info);
166 std::pair<HandleMap::iterator, bool> result = map_.insert(item); 167 std::pair<HandleMap::iterator, bool> result = map_.insert(item);
167 if (!result.second) { 168 if (!result.second) {
168 Info other = result.first->second; 169 Info other = result.first->second;
169 base::debug::Alias(&other); 170 base::debug::Alias(&other);
170 CHECK(false); 171 CHECK(false);
171 } 172 }
172 } 173 }
173 174
174 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, 175 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner,
175 const void* pc1, const void* pc2) { 176 const void* pc1, const void* pc2) {
176 if (!enabled_) 177 if (!enabled_)
177 return; 178 return;
178 179
179 AutoNativeLock lock(*lock_); 180 AutoNativeLock lock(*lock_);
180 HandleMap::iterator i = map_.find(handle); 181 HandleMap::iterator i = map_.find(handle);
181 if (i == map_.end()) 182 if (i == map_.end())
182 CHECK(false); 183 CHECK(false);
183 184
184 Info other = i->second; 185 Info other = i->second;
185 if (other.owner != owner) { 186 if (other.owner != owner) {
186 base::debug::Alias(&other); 187 base::debug::Alias(&other);
187 CHECK(false); 188 CHECK(false);
188 } 189 }
189 190
191 // We expect handle to be protected till this point.
192 DWORD flags = 0;
193 ::GetHandleInformation(handle, &flags);
194 if (!(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE))
195 CHECK(FALSE);
196
197 // Unprotect handle so that it could be closed.
198 ::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0);
199
190 map_.erase(i); 200 map_.erase(i);
191 } 201 }
192 202
193 void ActiveVerifier::Disable() { 203 void ActiveVerifier::Disable() {
194 enabled_ = false; 204 enabled_ = false;
195 } 205 }
196 206
197 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { 207 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) {
198 AutoNativeLock lock(*lock_); 208 AutoNativeLock lock(*lock_);
199 if (closing_) 209 if (closing_)
200 return; 210 return;
201 211
202 HandleMap::iterator i = map_.find(handle); 212 HandleMap::iterator i = map_.find(handle);
203 if (i == map_.end()) 213 if (i == map_.end())
204 return; 214 return;
205 215
216 // Mask out all protected handle close attempts. This will give us
217 // idea if protecting handle has any effect or not.
218 // TODO(shrikant): Remove this code once we see results of the above
219 // mentioned experiment.
220 DWORD flags = 0;
221 ::GetHandleInformation(handle, &flags);
222 if (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE)
223 return;
rvargas (doing something else) 2015/04/30 23:57:16 hold on... we should not be modifying the behavior
Shrikant Kelkar 2015/05/01 00:06:48 ?? This code will trigger only if we are about cra
cpu_(ooo_6.6-7.5) 2015/05/01 00:42:51 Not sure if we want this or not, I need to think m
Shrikant Kelkar 2015/05/01 00:57:31 Acknowledged.
rvargas (doing something else) 2015/05/06 01:47:23 ah, right. I missed that this was after searching
224
206 Info other = i->second; 225 Info other = i->second;
207 base::debug::Alias(&other); 226 base::debug::Alias(&other);
208 CHECK(false); 227 CHECK(false);
209 } 228 }
210 229
211 } // namespace 230 } // namespace
212 231
213 void* GetHandleVerifier() { 232 void* GetHandleVerifier() {
214 return g_active_verifier; 233 return g_active_verifier;
215 } 234 }
(...skipping 21 matching lines...) Expand all
237 void DisableHandleVerifier() { 256 void DisableHandleVerifier() {
238 return ActiveVerifier::Get()->Disable(); 257 return ActiveVerifier::Get()->Disable();
239 } 258 }
240 259
241 void OnHandleBeingClosed(HANDLE handle) { 260 void OnHandleBeingClosed(HANDLE handle) {
242 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); 261 return ActiveVerifier::Get()->OnHandleBeingClosed(handle);
243 } 262 }
244 263
245 } // namespace win 264 } // namespace win
246 } // namespace base 265 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698