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

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

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