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

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

Issue 490043002: Improve the ScopedHandle verifier. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
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 };
26 53
27 } // namespace 54 } // namespace
28 55
56 namespace BASE_HASH_NAMESPACE {
57 #if defined(COMPILER_MSVC)
58 inline size_t hash_value(const HANDLE& handle) {
59 ULONG_PTR int_handle = reinterpret_cast<ULONG_PTR>(handle);
60 return base::Hash(reinterpret_cast<char*>(&int_handle), sizeof(int_handle));
61 }
62 #elif defined (COMPILER_GCC)
63 template <>
cpu_(ooo_6.6-7.5) 2014/08/22 01:52:04 this branch is for nacl?
rvargas (doing something else) 2014/08/22 02:10:32 Not really. It is for completeness if people are t
cpu_(ooo_6.6-7.5) 2014/08/22 22:10:20 Acknowledged.
64 struct hash<HANDLE> {
65 size_t operator()(const HANDLE& key) const {
66 ULONG_PTR int_handle = reinterpret_cast<ULONG_PTR>(handle);
67 return base::Hash(reinterpret_cast<char*>(&int_handle), sizeof(int_handle));
cpu_(ooo_6.6-7.5) 2014/08/22 01:52:05 shouldn't we be doing bit_cast<char[N]> or somesuc
rvargas (doing something else) 2014/08/22 02:10:31 Maybe? what is N? sizeof? I wanted to be explicit/
cpu_(ooo_6.6-7.5) 2014/08/22 22:10:20 yes sizeof.
rvargas (doing something else) 2014/08/23 02:22:07 Done.
68 }
69 };
70 #endif
71 } // BASE_HASH_NAMESPACE
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 EnableHandleVerifier() {
131 g_verifier_enabled = true;
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

Powered by Google App Engine
This is Rietveld 408576698