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

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

Issue 506013004: Improve the ScopedHandle verifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 | « base/win/scoped_handle.h ('k') | chrome/BUILD.gn » ('j') | 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 <map> 7 #include "base/containers/hash_tables.h"
8
9 #include "base/debug/alias.h" 8 #include "base/debug/alias.h"
9 #include "base/hash.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/synchronization/lock.h" 11 #include "base/logging.h"
12 #include "base/synchronization/lock_impl.h"
12 #include "base/win/windows_version.h" 13 #include "base/win/windows_version.h"
13 14
14 namespace { 15 namespace {
15 16
16 struct Info { 17 struct Info {
17 const void* owner; 18 const void* owner;
18 const void* pc1; 19 const void* pc1;
19 const void* pc2; 20 const void* pc2;
20 DWORD thread_id; 21 DWORD thread_id;
21 }; 22 };
22 typedef std::map<HANDLE, Info> HandleMap; 23 typedef base::hash_map<HANDLE, Info> HandleMap;
23 24
25 typedef base::internal::LockImpl NativeLock;
24 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER; 26 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
25 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; 27 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
28 bool g_closing = false;
29 bool g_verifier_enabled = false;
30
31 bool CloseHandleWrapper(HANDLE handle) {
32 if (!::CloseHandle(handle))
33 CHECK(false);
34 return true;
35 }
36
37 // Simple automatic locking using a native critical section so it supports
38 // recursive locking.
39 class AutoNativeLock {
40 public:
41 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) {
42 lock_.Lock();
43 }
44
45 ~AutoNativeLock() {
46 lock_.Unlock();
47 }
48
49 private:
50 NativeLock& lock_;
51 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock);
52 };
53
54 inline size_t HashHandle(const HANDLE& handle) {
55 char buffer[sizeof(handle)];
56 memcpy(buffer, &handle, sizeof(handle));
57 return base::Hash(buffer, sizeof(buffer));
58 }
26 59
27 } // namespace 60 } // namespace
28 61
62 namespace BASE_HASH_NAMESPACE {
63 #if defined(COMPILER_MSVC)
64 inline size_t hash_value(const HANDLE& handle) {
65 return HashHandle(handle);
66 }
67 #elif defined (COMPILER_GCC)
68 template <>
69 struct hash<HANDLE> {
70 size_t operator()(const HANDLE& handle) const {
71 return HashHandle(handle);
72 }
73 };
74 #endif
75 } // BASE_HASH_NAMESPACE
76
29 namespace base { 77 namespace base {
30 namespace win { 78 namespace win {
31 79
32 // Static. 80 // Static.
81 bool HandleTraits::CloseHandle(HANDLE handle) {
82 if (!g_verifier_enabled)
83 return CloseHandleWrapper(handle);
84
85 AutoNativeLock lock(g_lock.Get());
86 g_closing = true;
87 CloseHandleWrapper(handle);
88 g_closing = false;
89
90 return true;
91 }
92
93 // Static.
33 void VerifierTraits::StartTracking(HANDLE handle, const void* owner, 94 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
34 const void* pc1, const void* pc2) { 95 const void* pc1, const void* pc2) {
96 if (!g_verifier_enabled)
97 return;
98
35 // Grab the thread id before the lock. 99 // Grab the thread id before the lock.
36 DWORD thread_id = GetCurrentThreadId(); 100 DWORD thread_id = GetCurrentThreadId();
37 101
38 AutoLock lock(g_lock.Get()); 102 AutoNativeLock lock(g_lock.Get());
39 103
40 Info handle_info = { owner, pc1, pc2, thread_id }; 104 Info handle_info = { owner, pc1, pc2, thread_id };
41 std::pair<HANDLE, Info> item(handle, handle_info); 105 std::pair<HANDLE, Info> item(handle, handle_info);
42 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item); 106 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
43 if (!result.second) { 107 if (!result.second) {
44 Info other = result.first->second; 108 Info other = result.first->second;
45 debug::Alias(&other); 109 debug::Alias(&other);
46 CHECK(false); 110 CHECK(false);
47 } 111 }
48 } 112 }
49 113
50 // Static. 114 // Static.
51 void VerifierTraits::StopTracking(HANDLE handle, const void* owner, 115 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
52 const void* pc1, const void* pc2) { 116 const void* pc1, const void* pc2) {
53 AutoLock lock(g_lock.Get()); 117 if (!g_verifier_enabled)
118 return;
119
120 AutoNativeLock lock(g_lock.Get());
54 HandleMap::iterator i = g_handle_map.Get().find(handle); 121 HandleMap::iterator i = g_handle_map.Get().find(handle);
55 if (i == g_handle_map.Get().end()) 122 if (i == g_handle_map.Get().end())
56 CHECK(false); 123 CHECK(false);
57 124
58 Info other = i->second; 125 Info other = i->second;
59 if (other.owner != owner) { 126 if (other.owner != owner) {
60 debug::Alias(&other); 127 debug::Alias(&other);
61 CHECK(false); 128 CHECK(false);
62 } 129 }
63 130
64 g_handle_map.Get().erase(i); 131 g_handle_map.Get().erase(i);
65 } 132 }
66 133
134 void EnableHandleVerifier() {
135 g_verifier_enabled = true;
136 }
137
138 void OnHandleBeingClosed(HANDLE handle) {
139 AutoNativeLock lock(g_lock.Get());
140 if (g_closing)
141 return;
142
143 HandleMap::iterator i = g_handle_map.Get().find(handle);
144 if (i == g_handle_map.Get().end())
145 return;
146
147 Info other = i->second;
148 debug::Alias(&other);
149 CHECK(false);
150 }
151
67 } // namespace win 152 } // namespace win
68 } // namespace base 153 } // namespace base
OLDNEW
« no previous file with comments | « base/win/scoped_handle.h ('k') | chrome/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698