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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <unordered_map> | 9 #include <unordered_map> |
10 | 10 |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/debug/stack_trace.h" | 12 #include "base/debug/stack_trace.h" |
13 #include "base/hash.h" | 13 #include "base/hash.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/synchronization/lock_impl.h" | 17 #include "base/synchronization/lock_impl.h" |
18 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
19 | 19 |
20 extern "C" { | 20 extern "C" { |
21 __declspec(dllexport) void* GetHandleVerifier(); | 21 __declspec(dllexport) void* GetHandleVerifier(); |
22 typedef void* (*GetHandleVerifierFn)(); | 22 typedef void* (*GetHandleVerifierFn)(); |
23 } | 23 } |
24 | 24 |
| 25 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
| 26 extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 struct HandleHash { | 30 struct HandleHash { |
28 size_t operator()(const HANDLE& handle) const { | 31 size_t operator()(const HANDLE& handle) const { |
29 char buffer[sizeof(handle)]; | 32 char buffer[sizeof(handle)]; |
30 memcpy(buffer, &handle, sizeof(handle)); | 33 memcpy(buffer, &handle, sizeof(handle)); |
31 return base::Hash(buffer, sizeof(buffer)); | 34 return base::Hash(buffer, sizeof(buffer)); |
32 } | 35 } |
33 }; | 36 }; |
34 | 37 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // The methods required by HandleTraits. They are virtual because we need to | 81 // The methods required by HandleTraits. They are virtual because we need to |
79 // forward the call execution to another module, instead of letting the | 82 // forward the call execution to another module, instead of letting the |
80 // compiler call the version that is linked in the current module. | 83 // compiler call the version that is linked in the current module. |
81 virtual bool CloseHandle(HANDLE handle); | 84 virtual bool CloseHandle(HANDLE handle); |
82 virtual void StartTracking(HANDLE handle, const void* owner, | 85 virtual void StartTracking(HANDLE handle, const void* owner, |
83 const void* pc1, const void* pc2); | 86 const void* pc1, const void* pc2); |
84 virtual void StopTracking(HANDLE handle, const void* owner, | 87 virtual void StopTracking(HANDLE handle, const void* owner, |
85 const void* pc1, const void* pc2); | 88 const void* pc1, const void* pc2); |
86 virtual void Disable(); | 89 virtual void Disable(); |
87 virtual void OnHandleBeingClosed(HANDLE handle); | 90 virtual void OnHandleBeingClosed(HANDLE handle); |
| 91 virtual HMODULE GetModule() const; |
88 | 92 |
89 private: | 93 private: |
90 ~ActiveVerifier(); // Not implemented. | 94 ~ActiveVerifier(); // Not implemented. |
91 | 95 |
92 static void InstallVerifier(); | 96 static void InstallVerifier(); |
93 | 97 |
94 base::debug::StackTrace creation_stack_; | 98 base::debug::StackTrace creation_stack_; |
95 bool enabled_; | 99 bool enabled_; |
96 base::ThreadLocalBoolean closing_; | 100 base::ThreadLocalBoolean closing_; |
97 NativeLock* lock_; | 101 NativeLock* lock_; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 HandleMap::iterator i = map_.find(handle); | 239 HandleMap::iterator i = map_.find(handle); |
236 if (i == map_.end()) | 240 if (i == map_.end()) |
237 return; | 241 return; |
238 | 242 |
239 Info other = i->second; | 243 Info other = i->second; |
240 base::debug::Alias(&other); | 244 base::debug::Alias(&other); |
241 base::debug::Alias(&creation_stack_); | 245 base::debug::Alias(&creation_stack_); |
242 CHECK(false); // CloseHandle called on tracked handle. | 246 CHECK(false); // CloseHandle called on tracked handle. |
243 } | 247 } |
244 | 248 |
| 249 HMODULE ActiveVerifier::GetModule() const { |
| 250 return reinterpret_cast<HMODULE>(&__ImageBase); |
| 251 } |
| 252 |
245 } // namespace | 253 } // namespace |
246 | 254 |
247 void* GetHandleVerifier() { | 255 void* GetHandleVerifier() { |
248 return ActiveVerifier::Get(); | 256 return ActiveVerifier::Get(); |
249 } | 257 } |
250 | 258 |
251 namespace base { | 259 namespace base { |
252 namespace win { | 260 namespace win { |
253 | 261 |
254 // Static. | 262 // Static. |
(...skipping 14 matching lines...) Expand all Loading... |
269 } | 277 } |
270 | 278 |
271 void DisableHandleVerifier() { | 279 void DisableHandleVerifier() { |
272 return ActiveVerifier::Get()->Disable(); | 280 return ActiveVerifier::Get()->Disable(); |
273 } | 281 } |
274 | 282 |
275 void OnHandleBeingClosed(HANDLE handle) { | 283 void OnHandleBeingClosed(HANDLE handle) { |
276 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); | 284 return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
277 } | 285 } |
278 | 286 |
| 287 HMODULE GetHandleVerifierModuleForTesting() { |
| 288 return g_active_verifier->GetModule(); |
| 289 } |
| 290 |
279 } // namespace win | 291 } // namespace win |
280 } // namespace base | 292 } // namespace base |
OLD | NEW |