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

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

Issue 1446363003: Deleted OS_WIN and all Windows specific files from base. (Closed) Base URL: https://github.com/domokit/mojo.git@base_tests
Patch Set: Created 5 years, 1 month 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 | « base/win/scoped_handle.h ('k') | base/win/scoped_hdc.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/win/scoped_handle.h"
6
7 #include <unordered_map>
8
9 #include "base/debug/alias.h"
10 #include "base/hash.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/synchronization/lock_impl.h"
14
15 extern "C" {
16 __declspec(dllexport) void* GetHandleVerifier();
17 typedef void* (*GetHandleVerifierFn)();
18 }
19
20 namespace {
21
22 struct HandleHash {
23 size_t operator()(const HANDLE& handle) const {
24 char buffer[sizeof(handle)];
25 memcpy(buffer, &handle, sizeof(handle));
26 return base::Hash(buffer, sizeof(buffer));
27 }
28 };
29
30 struct Info {
31 const void* owner;
32 const void* pc1;
33 const void* pc2;
34 DWORD thread_id;
35 };
36 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap;
37
38 // g_lock protects the handle map and setting g_active_verifier.
39 typedef base::internal::LockImpl NativeLock;
40 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
41
42 bool CloseHandleWrapper(HANDLE handle) {
43 if (!::CloseHandle(handle))
44 CHECK(false);
45 return true;
46 }
47
48 // Simple automatic locking using a native critical section so it supports
49 // recursive locking.
50 class AutoNativeLock {
51 public:
52 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) {
53 lock_.Lock();
54 }
55
56 ~AutoNativeLock() {
57 lock_.Unlock();
58 }
59
60 private:
61 NativeLock& lock_;
62 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock);
63 };
64
65 // Implements the actual object that is verifying handles for this process.
66 // The active instance is shared across the module boundary but there is no
67 // way to delete this object from the wrong side of it (or any side, actually).
68 class ActiveVerifier {
69 public:
70 explicit ActiveVerifier(bool enabled)
71 : enabled_(enabled), closing_(false), lock_(g_lock.Pointer()) {
72 }
73
74 // Retrieves the current verifier.
75 static ActiveVerifier* Get();
76
77 // The methods required by HandleTraits. They are virtual because we need to
78 // forward the call execution to another module, instead of letting the
79 // compiler call the version that is linked in the current module.
80 virtual bool CloseHandle(HANDLE handle);
81 virtual void StartTracking(HANDLE handle, const void* owner,
82 const void* pc1, const void* pc2);
83 virtual void StopTracking(HANDLE handle, const void* owner,
84 const void* pc1, const void* pc2);
85 virtual void Disable();
86 virtual void OnHandleBeingClosed(HANDLE handle);
87
88 private:
89 ~ActiveVerifier(); // Not implemented.
90
91 static void InstallVerifier();
92
93 bool enabled_;
94 bool closing_;
95 NativeLock* lock_;
96 HandleMap map_;
97 DISALLOW_COPY_AND_ASSIGN(ActiveVerifier);
98 };
99 ActiveVerifier* g_active_verifier = NULL;
100
101 // static
102 ActiveVerifier* ActiveVerifier::Get() {
103 if (!g_active_verifier)
104 ActiveVerifier::InstallVerifier();
105
106 return g_active_verifier;
107 }
108
109 // static
110 void ActiveVerifier::InstallVerifier() {
111 #if defined(COMPONENT_BUILD)
112 AutoNativeLock lock(g_lock.Get());
113 g_active_verifier = new ActiveVerifier(true);
114 #else
115 // If you are reading this, wondering why your process seems deadlocked, take
116 // a look at your DllMain code and remove things that should not be done
117 // there, like doing whatever gave you that nice windows handle you are trying
118 // to store in a ScopedHandle.
119 HMODULE main_module = ::GetModuleHandle(NULL);
120 GetHandleVerifierFn get_handle_verifier =
121 reinterpret_cast<GetHandleVerifierFn>(::GetProcAddress(
122 main_module, "GetHandleVerifier"));
123
124 if (!get_handle_verifier) {
125 g_active_verifier = new ActiveVerifier(false);
126 return;
127 }
128
129 ActiveVerifier* verifier =
130 reinterpret_cast<ActiveVerifier*>(get_handle_verifier());
131
132 // This lock only protects against races in this module, which is fine.
133 AutoNativeLock lock(g_lock.Get());
134 g_active_verifier = verifier ? verifier : new ActiveVerifier(true);
135 #endif
136 }
137
138 bool ActiveVerifier::CloseHandle(HANDLE handle) {
139 if (!enabled_)
140 return CloseHandleWrapper(handle);
141
142 AutoNativeLock lock(*lock_);
143 closing_ = true;
144 CloseHandleWrapper(handle);
145 closing_ = false;
146
147 return true;
148 }
149
150 void ActiveVerifier::StartTracking(HANDLE handle, const void* owner,
151 const void* pc1, const void* pc2) {
152 if (!enabled_)
153 return;
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
161 // Grab the thread id before the lock.
162 DWORD thread_id = GetCurrentThreadId();
163
164 AutoNativeLock lock(*lock_);
165
166 Info handle_info = { owner, pc1, pc2, thread_id };
167 std::pair<HANDLE, Info> item(handle, handle_info);
168 std::pair<HandleMap::iterator, bool> result = map_.insert(item);
169 if (!result.second) {
170 Info other = result.first->second;
171 base::debug::Alias(&other);
172 CHECK(false);
173 }
174 }
175
176 void ActiveVerifier::StopTracking(HANDLE handle, const void* owner,
177 const void* pc1, const void* pc2) {
178 if (!enabled_)
179 return;
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
190 AutoNativeLock lock(*lock_);
191 HandleMap::iterator i = map_.find(handle);
192 if (i == map_.end())
193 CHECK(false);
194
195 Info other = i->second;
196 if (other.owner != owner) {
197 base::debug::Alias(&other);
198 CHECK(false);
199 }
200
201 map_.erase(i);
202 }
203
204 void ActiveVerifier::Disable() {
205 enabled_ = false;
206 }
207
208 void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) {
209 AutoNativeLock lock(*lock_);
210 if (closing_)
211 return;
212
213 HandleMap::iterator i = map_.find(handle);
214 if (i == map_.end())
215 return;
216
217 Info other = i->second;
218 base::debug::Alias(&other);
219 CHECK(false);
220 }
221
222 } // namespace
223
224 void* GetHandleVerifier() {
225 return g_active_verifier;
226 }
227
228 namespace base {
229 namespace win {
230
231 // Static.
232 bool HandleTraits::CloseHandle(HANDLE handle) {
233 return ActiveVerifier::Get()->CloseHandle(handle);
234 }
235
236 // Static.
237 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
238 const void* pc1, const void* pc2) {
239 return ActiveVerifier::Get()->StartTracking(handle, owner, pc1, pc2);
240 }
241
242 // Static.
243 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
244 const void* pc1, const void* pc2) {
245 return ActiveVerifier::Get()->StopTracking(handle, owner, pc1, pc2);
246 }
247
248 void DisableHandleVerifier() {
249 return ActiveVerifier::Get()->Disable();
250 }
251
252 void OnHandleBeingClosed(HANDLE handle) {
253 return ActiveVerifier::Get()->OnHandleBeingClosed(handle);
254 }
255
256 } // namespace win
257 } // namespace base
OLDNEW
« no previous file with comments | « base/win/scoped_handle.h ('k') | base/win/scoped_hdc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698