rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/win/scoped_handle.h" |
| 6 | |
avi | 3131ce82 | 2015-12-24 04:58:49 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 9 | #include <unordered_map> |
tonyg | cf244d1 | 2014-08-27 00:14:13 | [diff] [blame] | 10 | |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 11 | #include "base/debug/alias.h" |
wfh | 0359d7d | 2016-03-03 21:36:13 | [diff] [blame] | 12 | #include "base/debug/stack_trace.h" |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 13 | #include "base/hash.h" |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 14 | #include "base/logging.h" |
avi | 3131ce82 | 2015-12-24 04:58:49 | [diff] [blame] | 15 | #include "base/macros.h" |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 16 | #include "base/synchronization/lock_impl.h" |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 17 | #include "base/threading/thread_local.h" |
sebmarchand | 82ef0204 | 2016-06-08 15:56:32 | [diff] [blame] | 18 | #include "base/win/base_features.h" |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 19 | #include "base/win/current_module.h" |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 20 | |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 21 | extern "C" { |
| 22 | __declspec(dllexport) void* GetHandleVerifier(); |
| 23 | typedef void* (*GetHandleVerifierFn)(); |
| 24 | } |
wfh | 8f20e83 | 2016-03-12 02:09:59 | [diff] [blame] | 25 | |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 26 | namespace { |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 27 | |
| 28 | struct HandleHash { |
| 29 | size_t operator()(const HANDLE& handle) const { |
| 30 | char buffer[sizeof(handle)]; |
| 31 | memcpy(buffer, &handle, sizeof(handle)); |
| 32 | return base::Hash(buffer, sizeof(buffer)); |
| 33 | } |
| 34 | }; |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 35 | |
| 36 | struct Info { |
| 37 | const void* owner; |
rvargas@chromium.org | 30e170a | 2012-06-07 05:32:34 | [diff] [blame] | 38 | const void* pc1; |
| 39 | const void* pc2; |
wfh | 0359d7d | 2016-03-03 21:36:13 | [diff] [blame] | 40 | base::debug::StackTrace stack; |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 41 | DWORD thread_id; |
| 42 | }; |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 43 | typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap; |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 44 | |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 45 | // GetLock() protects the handle map and setting g_active_verifier within this |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 46 | // module. |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 47 | typedef base::internal::LockImpl NativeLock; |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 48 | NativeLock* GetLock() { |
vmpstr | 843b41a | 2017-03-01 21:15:03 | [diff] [blame] | 49 | static auto* native_lock = new NativeLock(); |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 50 | return native_lock; |
| 51 | } |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 52 | |
| 53 | // Simple automatic locking using a native critical section so it supports |
| 54 | // recursive locking. |
| 55 | class AutoNativeLock { |
| 56 | public: |
| 57 | explicit AutoNativeLock(NativeLock& lock) : lock_(lock) { |
| 58 | lock_.Lock(); |
| 59 | } |
| 60 | |
| 61 | ~AutoNativeLock() { |
| 62 | lock_.Unlock(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | NativeLock& lock_; |
| 67 | DISALLOW_COPY_AND_ASSIGN(AutoNativeLock); |
| 68 | }; |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 69 | |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 70 | // Implements the actual object that is verifying handles for this process. |
| 71 | // The active instance is shared across the module boundary but there is no |
| 72 | // way to delete this object from the wrong side of it (or any side, actually). |
| 73 | class ActiveVerifier { |
| 74 | public: |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 75 | explicit ActiveVerifier(bool enabled) : enabled_(enabled), lock_(GetLock()) {} |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 76 | |
| 77 | // Retrieves the current verifier. |
| 78 | static ActiveVerifier* Get(); |
| 79 | |
| 80 | // The methods required by HandleTraits. They are virtual because we need to |
| 81 | // forward the call execution to another module, instead of letting the |
| 82 | // compiler call the version that is linked in the current module. |
| 83 | virtual bool CloseHandle(HANDLE handle); |
| 84 | virtual void StartTracking(HANDLE handle, const void* owner, |
| 85 | const void* pc1, const void* pc2); |
| 86 | virtual void StopTracking(HANDLE handle, const void* owner, |
| 87 | const void* pc1, const void* pc2); |
| 88 | virtual void Disable(); |
| 89 | virtual void OnHandleBeingClosed(HANDLE handle); |
wfh | 8f20e83 | 2016-03-12 02:09:59 | [diff] [blame] | 90 | virtual HMODULE GetModule() const; |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | ~ActiveVerifier(); // Not implemented. |
| 94 | |
| 95 | static void InstallVerifier(); |
| 96 | |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 97 | base::debug::StackTrace creation_stack_; |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 98 | bool enabled_; |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 99 | base::ThreadLocalBoolean closing_; |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 100 | NativeLock* lock_; |
| 101 | HandleMap map_; |
| 102 | DISALLOW_COPY_AND_ASSIGN(ActiveVerifier); |
| 103 | }; |
| 104 | ActiveVerifier* g_active_verifier = NULL; |
| 105 | |
| 106 | // static |
| 107 | ActiveVerifier* ActiveVerifier::Get() { |
| 108 | if (!g_active_verifier) |
| 109 | ActiveVerifier::InstallVerifier(); |
| 110 | |
| 111 | return g_active_verifier; |
| 112 | } |
| 113 | |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 114 | bool CloseHandleWrapper(HANDLE handle) { |
| 115 | if (!::CloseHandle(handle)) |
| 116 | CHECK(false); // CloseHandle failed. |
| 117 | return true; |
| 118 | } |
| 119 | |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 120 | // Assigns the g_active_verifier global within the GetLock() lock. |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 121 | // If |existing_verifier| is non-null then |enabled| is ignored. |
| 122 | void ThreadSafeAssignOrCreateActiveVerifier(ActiveVerifier* existing_verifier, |
| 123 | bool enabled) { |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 124 | AutoNativeLock lock(*GetLock()); |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 125 | // Another thread in this module might be trying to assign the global |
| 126 | // verifier, so check that within the lock here. |
| 127 | if (g_active_verifier) |
| 128 | return; |
| 129 | g_active_verifier = |
| 130 | existing_verifier ? existing_verifier : new ActiveVerifier(enabled); |
| 131 | } |
| 132 | |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 133 | // static |
| 134 | void ActiveVerifier::InstallVerifier() { |
sebmarchand | 82ef0204 | 2016-06-08 15:56:32 | [diff] [blame] | 135 | #if BUILDFLAG(SINGLE_MODULE_MODE_HANDLE_VERIFIER) |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 136 | // Component build has one Active Verifier per module. |
| 137 | ThreadSafeAssignOrCreateActiveVerifier(nullptr, true); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 138 | #else |
| 139 | // If you are reading this, wondering why your process seems deadlocked, take |
| 140 | // a look at your DllMain code and remove things that should not be done |
| 141 | // there, like doing whatever gave you that nice windows handle you are trying |
| 142 | // to store in a ScopedHandle. |
| 143 | HMODULE main_module = ::GetModuleHandle(NULL); |
| 144 | GetHandleVerifierFn get_handle_verifier = |
| 145 | reinterpret_cast<GetHandleVerifierFn>(::GetProcAddress( |
| 146 | main_module, "GetHandleVerifier")); |
| 147 | |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 148 | // This should only happen if running in a DLL is linked with base but the |
| 149 | // hosting EXE is not. In this case, create an ActiveVerifier for the current |
| 150 | // module but leave it disabled. |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 151 | if (!get_handle_verifier) { |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 152 | ThreadSafeAssignOrCreateActiveVerifier(nullptr, false); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 156 | // Check if in the main module. |
| 157 | if (get_handle_verifier == GetHandleVerifier) { |
| 158 | ThreadSafeAssignOrCreateActiveVerifier(nullptr, true); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | ActiveVerifier* main_module_verifier = |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 163 | reinterpret_cast<ActiveVerifier*>(get_handle_verifier()); |
| 164 | |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 165 | // Main module should always on-demand create a verifier. |
| 166 | DCHECK(main_module_verifier); |
| 167 | |
| 168 | ThreadSafeAssignOrCreateActiveVerifier(main_module_verifier, false); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 169 | #endif |
| 170 | } |
| 171 | |
| 172 | bool ActiveVerifier::CloseHandle(HANDLE handle) { |
| 173 | if (!enabled_) |
| 174 | return CloseHandleWrapper(handle); |
| 175 | |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 176 | closing_.Set(true); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 177 | CloseHandleWrapper(handle); |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 178 | closing_.Set(false); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | void ActiveVerifier::StartTracking(HANDLE handle, const void* owner, |
| 184 | const void* pc1, const void* pc2) { |
| 185 | if (!enabled_) |
| 186 | return; |
| 187 | |
| 188 | // Grab the thread id before the lock. |
| 189 | DWORD thread_id = GetCurrentThreadId(); |
| 190 | |
| 191 | AutoNativeLock lock(*lock_); |
| 192 | |
wfh | 0359d7d | 2016-03-03 21:36:13 | [diff] [blame] | 193 | Info handle_info = { owner, pc1, pc2, base::debug::StackTrace(), thread_id }; |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 194 | std::pair<HANDLE, Info> item(handle, handle_info); |
| 195 | std::pair<HandleMap::iterator, bool> result = map_.insert(item); |
| 196 | if (!result.second) { |
| 197 | Info other = result.first->second; |
| 198 | base::debug::Alias(&other); |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 199 | base::debug::Alias(&creation_stack_); |
wfh | 4801736 | 2016-03-08 22:37:57 | [diff] [blame] | 200 | CHECK(false); // Attempt to start tracking already tracked handle. |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | void ActiveVerifier::StopTracking(HANDLE handle, const void* owner, |
| 205 | const void* pc1, const void* pc2) { |
| 206 | if (!enabled_) |
| 207 | return; |
shrikant | c928d34 | 2015-05-12 22:28:58 | [diff] [blame] | 208 | |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 209 | AutoNativeLock lock(*lock_); |
| 210 | HandleMap::iterator i = map_.find(handle); |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 211 | if (i == map_.end()) { |
| 212 | base::debug::Alias(&creation_stack_); |
wfh | 4801736 | 2016-03-08 22:37:57 | [diff] [blame] | 213 | CHECK(false); // Attempting to close an untracked handle. |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 214 | } |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 215 | |
| 216 | Info other = i->second; |
| 217 | if (other.owner != owner) { |
| 218 | base::debug::Alias(&other); |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 219 | base::debug::Alias(&creation_stack_); |
wfh | 4801736 | 2016-03-08 22:37:57 | [diff] [blame] | 220 | CHECK(false); // Attempting to close a handle not owned by opener. |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | map_.erase(i); |
| 224 | } |
| 225 | |
| 226 | void ActiveVerifier::Disable() { |
| 227 | enabled_ = false; |
| 228 | } |
| 229 | |
| 230 | void ActiveVerifier::OnHandleBeingClosed(HANDLE handle) { |
wfh | 2b30ce39 | 2016-01-06 18:30:07 | [diff] [blame] | 231 | if (!enabled_) |
| 232 | return; |
| 233 | |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 234 | if (closing_.Get()) |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 235 | return; |
| 236 | |
siggi | 2ca94bde | 2016-03-09 13:23:19 | [diff] [blame] | 237 | AutoNativeLock lock(*lock_); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 238 | HandleMap::iterator i = map_.find(handle); |
| 239 | if (i == map_.end()) |
| 240 | return; |
| 241 | |
| 242 | Info other = i->second; |
| 243 | base::debug::Alias(&other); |
wfh | 1f9673d | 2016-03-09 00:33:02 | [diff] [blame] | 244 | base::debug::Alias(&creation_stack_); |
wfh | 4801736 | 2016-03-08 22:37:57 | [diff] [blame] | 245 | CHECK(false); // CloseHandle called on tracked handle. |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 246 | } |
| 247 | |
wfh | 8f20e83 | 2016-03-12 02:09:59 | [diff] [blame] | 248 | HMODULE ActiveVerifier::GetModule() const { |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 249 | return CURRENT_MODULE(); |
wfh | 8f20e83 | 2016-03-12 02:09:59 | [diff] [blame] | 250 | } |
| 251 | |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 252 | } // namespace |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 253 | |
| 254 | void* GetHandleVerifier() { |
wfh | 678736a | 2016-03-11 08:41:04 | [diff] [blame] | 255 | return ActiveVerifier::Get(); |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 256 | } |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 257 | |
| 258 | namespace base { |
| 259 | namespace win { |
| 260 | |
| 261 | // Static. |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 262 | bool HandleTraits::CloseHandle(HANDLE handle) { |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 263 | return ActiveVerifier::Get()->CloseHandle(handle); |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Static. |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 267 | void VerifierTraits::StartTracking(HANDLE handle, const void* owner, |
rvargas@chromium.org | 30e170a | 2012-06-07 05:32:34 | [diff] [blame] | 268 | const void* pc1, const void* pc2) { |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 269 | return ActiveVerifier::Get()->StartTracking(handle, owner, pc1, pc2); |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Static. |
| 273 | void VerifierTraits::StopTracking(HANDLE handle, const void* owner, |
rvargas@chromium.org | 30e170a | 2012-06-07 05:32:34 | [diff] [blame] | 274 | const void* pc1, const void* pc2) { |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 275 | return ActiveVerifier::Get()->StopTracking(handle, owner, pc1, pc2); |
rvargas | 1a4d07b | 2014-08-26 19:45:42 | [diff] [blame] | 276 | } |
| 277 | |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 278 | void DisableHandleVerifier() { |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 279 | return ActiveVerifier::Get()->Disable(); |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void OnHandleBeingClosed(HANDLE handle) { |
rvargas | 8545e0e7 | 2015-04-09 01:40:50 | [diff] [blame] | 283 | return ActiveVerifier::Get()->OnHandleBeingClosed(handle); |
rvargas | 86d7c90 | 2014-09-04 22:58:09 | [diff] [blame] | 284 | } |
| 285 | |
wfh | 8f20e83 | 2016-03-12 02:09:59 | [diff] [blame] | 286 | HMODULE GetHandleVerifierModuleForTesting() { |
| 287 | return g_active_verifier->GetModule(); |
| 288 | } |
| 289 | |
rvargas@chromium.org | 6c6c2cc | 2012-05-31 05:04:18 | [diff] [blame] | 290 | } // namespace win |
| 291 | } // namespace base |