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

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: Add comment to the lock 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
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 // g_lock protects g_handle_map and g_closing.
34 typedef base::internal::LockImpl NativeLock;
35 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
24 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER; 36 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
25 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; 37 bool g_closing = false;
38
39 // g_verifier_enabled is not protected by g_lock because that would require
40 // using the lock (hence, synchornizing multiple threads) even when the
41 // verifier is not in use. Note that this variable is initialized to track all
42 // handles, and it should only move to the disabled state, and never back to
43 // enabled, because that would crash when seeing handles created while the
44 // verifier was disabled. This also implies that it is OK if the value change is
45 // not propagated immediately to all CPUs (as would happen with a lock).
46 bool g_verifier_enabled = true;
47
48 bool CloseHandleWrapper(HANDLE handle) {
49 if (!::CloseHandle(handle))
50 CHECK(false);
51 return true;
52 }
53
54 // Simple automatic locking using a native critical section so it supports
55 // recursive locking.
56 class AutoNativeLock {
57 public:
58 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) {
59 lock_.Lock();
60 }
61
62 ~AutoNativeLock() {
63 lock_.Unlock();
64 }
65
66 private:
67 NativeLock& lock_;
68 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock);
69 };
26 70
27 } // namespace 71 } // namespace
28 72
29 namespace base { 73 namespace base {
30 namespace win { 74 namespace win {
31 75
32 // Static. 76 // Static.
77 bool HandleTraits::CloseHandle(HANDLE handle) {
78 if (!g_verifier_enabled)
79 return CloseHandleWrapper(handle);
80
81 AutoNativeLock lock(g_lock.Get());
82 g_closing = true;
83 CloseHandleWrapper(handle);
84 g_closing = false;
85
86 return true;
87 }
88
89 // Static.
33 void VerifierTraits::StartTracking(HANDLE handle, const void* owner, 90 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
34 const void* pc1, const void* pc2) { 91 const void* pc1, const void* pc2) {
92 if (!g_verifier_enabled)
93 return;
94
35 // Grab the thread id before the lock. 95 // Grab the thread id before the lock.
36 DWORD thread_id = GetCurrentThreadId(); 96 DWORD thread_id = GetCurrentThreadId();
37 97
38 AutoLock lock(g_lock.Get()); 98 AutoNativeLock lock(g_lock.Get());
39 99
40 Info handle_info = { owner, pc1, pc2, thread_id }; 100 Info handle_info = { owner, pc1, pc2, thread_id };
41 std::pair<HANDLE, Info> item(handle, handle_info); 101 std::pair<HANDLE, Info> item(handle, handle_info);
42 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item); 102 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
43 if (!result.second) { 103 if (!result.second) {
44 Info other = result.first->second; 104 Info other = result.first->second;
45 debug::Alias(&other); 105 debug::Alias(&other);
46 CHECK(false); 106 CHECK(false);
47 } 107 }
48 } 108 }
49 109
50 // Static. 110 // Static.
51 void VerifierTraits::StopTracking(HANDLE handle, const void* owner, 111 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
52 const void* pc1, const void* pc2) { 112 const void* pc1, const void* pc2) {
53 AutoLock lock(g_lock.Get()); 113 if (!g_verifier_enabled)
114 return;
115
116 AutoNativeLock lock(g_lock.Get());
54 HandleMap::iterator i = g_handle_map.Get().find(handle); 117 HandleMap::iterator i = g_handle_map.Get().find(handle);
55 if (i == g_handle_map.Get().end()) 118 if (i == g_handle_map.Get().end())
56 CHECK(false); 119 CHECK(false);
57 120
58 Info other = i->second; 121 Info other = i->second;
59 if (other.owner != owner) { 122 if (other.owner != owner) {
60 debug::Alias(&other); 123 debug::Alias(&other);
61 CHECK(false); 124 CHECK(false);
62 } 125 }
63 126
64 g_handle_map.Get().erase(i); 127 g_handle_map.Get().erase(i);
65 } 128 }
66 129
130 void DisableHandleVerifier() {
131 g_verifier_enabled = false;
132 }
133
134 void OnHandleBeingClosed(HANDLE handle) {
135 AutoNativeLock lock(g_lock.Get());
136 if (g_closing)
137 return;
138
139 HandleMap::iterator i = g_handle_map.Get().find(handle);
140 if (i == g_handle_map.Get().end())
141 return;
142
143 Info other = i->second;
144 debug::Alias(&other);
145 CHECK(false);
146 }
147
67 } // namespace win 148 } // namespace win
68 } // namespace base 149 } // namespace base
OLDNEW
« no previous file with comments | « base/win/scoped_handle.h ('k') | chrome/BUILD.gn » ('j') | chrome/chrome_dll.gypi » ('J')

Powered by Google App Engine
This is Rietveld 408576698