1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/win/scoped_handle.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/win/scoped_handle.h" 1.9 + 1.10 +#include <map> 1.11 + 1.12 +#include "base/debug/alias.h" 1.13 +#include "base/lazy_instance.h" 1.14 +#include "base/synchronization/lock.h" 1.15 +#include "base/win/windows_version.h" 1.16 + 1.17 +namespace { 1.18 + 1.19 +struct Info { 1.20 + const void* owner; 1.21 + const void* pc1; 1.22 + const void* pc2; 1.23 + DWORD thread_id; 1.24 +}; 1.25 +typedef std::map<HANDLE, Info> HandleMap; 1.26 + 1.27 +base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER; 1.28 +base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; 1.29 + 1.30 +} // namespace 1.31 + 1.32 +namespace base { 1.33 +namespace win { 1.34 + 1.35 +// Static. 1.36 +void VerifierTraits::StartTracking(HANDLE handle, const void* owner, 1.37 + const void* pc1, const void* pc2) { 1.38 + // Grab the thread id before the lock. 1.39 + DWORD thread_id = GetCurrentThreadId(); 1.40 + 1.41 + AutoLock lock(g_lock.Get()); 1.42 + 1.43 + Info handle_info = { owner, pc1, pc2, thread_id }; 1.44 + std::pair<HANDLE, Info> item(handle, handle_info); 1.45 + std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item); 1.46 + if (!result.second) { 1.47 + Info other = result.first->second; 1.48 + debug::Alias(&other); 1.49 + CHECK(false); 1.50 + } 1.51 +} 1.52 + 1.53 +// Static. 1.54 +void VerifierTraits::StopTracking(HANDLE handle, const void* owner, 1.55 + const void* pc1, const void* pc2) { 1.56 + AutoLock lock(g_lock.Get()); 1.57 + HandleMap::iterator i = g_handle_map.Get().find(handle); 1.58 + if (i == g_handle_map.Get().end()) 1.59 + CHECK(false); 1.60 + 1.61 + Info other = i->second; 1.62 + if (other.owner != owner) { 1.63 + debug::Alias(&other); 1.64 + CHECK(false); 1.65 + } 1.66 + 1.67 + g_handle_map.Get().erase(i); 1.68 +} 1.69 + 1.70 +} // namespace win 1.71 +} // namespace base