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
7#include <unordered_map>
8
9#include "base/debug/alias.h"
10#include "base/hash.h"
11#include "base/lazy_instance.h"
12#include "base/logging.h"
13#include "base/synchronization/lock_impl.h"
14
15namespace {
16
17struct 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
25struct Info {
26  const void* owner;
27  const void* pc1;
28  const void* pc2;
29  DWORD thread_id;
30};
31typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap;
32
33// g_lock protects g_handle_map and g_closing.
34typedef base::internal::LockImpl NativeLock;
35base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
36base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
37bool 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).
46bool g_verifier_enabled = true;
47
48bool 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.
56class 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};
70
71}  // namespace
72
73namespace base {
74namespace win {
75
76// Static.
77bool 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.
90void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
91                                   const void* pc1, const void* pc2) {
92  if (!g_verifier_enabled)
93    return;
94
95  // Grab the thread id before the lock.
96  DWORD thread_id = GetCurrentThreadId();
97
98  AutoNativeLock lock(g_lock.Get());
99
100  Info handle_info = { owner, pc1, pc2, thread_id };
101  std::pair<HANDLE, Info> item(handle, handle_info);
102  std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
103  if (!result.second) {
104    Info other = result.first->second;
105    debug::Alias(&other);
106    CHECK(false);
107  }
108}
109
110// Static.
111void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
112                                  const void* pc1, const void* pc2) {
113  if (!g_verifier_enabled)
114    return;
115
116  AutoNativeLock lock(g_lock.Get());
117  HandleMap::iterator i = g_handle_map.Get().find(handle);
118  if (i == g_handle_map.Get().end())
119    CHECK(false);
120
121  Info other = i->second;
122  if (other.owner != owner) {
123    debug::Alias(&other);
124    CHECK(false);
125  }
126
127  g_handle_map.Get().erase(i);
128}
129
130void DisableHandleVerifier() {
131  g_verifier_enabled = false;
132}
133
134void 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
148}  // namespace win
149}  // namespace base
150