security/sandbox/chromium/base/win/scoped_handle.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/win/scoped_handle.h"
michael@0 6
michael@0 7 #include <map>
michael@0 8
michael@0 9 #include "base/debug/alias.h"
michael@0 10 #include "base/lazy_instance.h"
michael@0 11 #include "base/synchronization/lock.h"
michael@0 12 #include "base/win/windows_version.h"
michael@0 13
michael@0 14 namespace {
michael@0 15
michael@0 16 struct Info {
michael@0 17 const void* owner;
michael@0 18 const void* pc1;
michael@0 19 const void* pc2;
michael@0 20 DWORD thread_id;
michael@0 21 };
michael@0 22 typedef std::map<HANDLE, Info> HandleMap;
michael@0 23
michael@0 24 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
michael@0 25 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
michael@0 26
michael@0 27 } // namespace
michael@0 28
michael@0 29 namespace base {
michael@0 30 namespace win {
michael@0 31
michael@0 32 // Static.
michael@0 33 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
michael@0 34 const void* pc1, const void* pc2) {
michael@0 35 // Grab the thread id before the lock.
michael@0 36 DWORD thread_id = GetCurrentThreadId();
michael@0 37
michael@0 38 AutoLock lock(g_lock.Get());
michael@0 39
michael@0 40 Info handle_info = { owner, pc1, pc2, thread_id };
michael@0 41 std::pair<HANDLE, Info> item(handle, handle_info);
michael@0 42 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
michael@0 43 if (!result.second) {
michael@0 44 Info other = result.first->second;
michael@0 45 debug::Alias(&other);
michael@0 46 CHECK(false);
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 // Static.
michael@0 51 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
michael@0 52 const void* pc1, const void* pc2) {
michael@0 53 AutoLock lock(g_lock.Get());
michael@0 54 HandleMap::iterator i = g_handle_map.Get().find(handle);
michael@0 55 if (i == g_handle_map.Get().end())
michael@0 56 CHECK(false);
michael@0 57
michael@0 58 Info other = i->second;
michael@0 59 if (other.owner != owner) {
michael@0 60 debug::Alias(&other);
michael@0 61 CHECK(false);
michael@0 62 }
michael@0 63
michael@0 64 g_handle_map.Get().erase(i);
michael@0 65 }
michael@0 66
michael@0 67 } // namespace win
michael@0 68 } // namespace base

mercurial