OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome_frame/crash_reporting/nt_loader.h" | 5 #include "chrome_frame/crash_reporting/nt_loader.h" |
6 | 6 |
7 #include <tlhelp32.h> | 7 #include <tlhelp32.h> |
8 #include <winnt.h> | 8 #include <winnt.h> |
9 | 9 |
10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/scoped_handle.h" | |
14 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
15 #include "base/string_util.h" | 14 #include "base/string_util.h" |
16 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
17 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
18 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "base/win/scoped_handle.h" |
19 #include "chrome_frame/crash_reporting/crash_dll.h" | 19 #include "chrome_frame/crash_reporting/crash_dll.h" |
20 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 void AssertIsCriticalSection(CRITICAL_SECTION* critsec) { | 23 void AssertIsCriticalSection(CRITICAL_SECTION* critsec) { |
24 // Assert on some of the internals of the debug info if it has one. | 24 // Assert on some of the internals of the debug info if it has one. |
25 RTL_CRITICAL_SECTION_DEBUG* debug = critsec->DebugInfo; | 25 RTL_CRITICAL_SECTION_DEBUG* debug = critsec->DebugInfo; |
26 if (debug) { | 26 if (debug) { |
27 ASSERT_EQ(RTL_CRITSECT_TYPE, debug->Type); | 27 ASSERT_EQ(RTL_CRITSECT_TYPE, debug->Type); |
28 ASSERT_EQ(critsec, debug->CriticalSection); | 28 ASSERT_EQ(critsec, debug->CriticalSection); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 // Should no longer own it. | 77 // Should no longer own it. |
78 EXPECT_FALSE(OwnsCriticalSection(&cs)); | 78 EXPECT_FALSE(OwnsCriticalSection(&cs)); |
79 | 79 |
80 // Make another thread grab it. | 80 // Make another thread grab it. |
81 base::Thread other("Other threads"); | 81 base::Thread other("Other threads"); |
82 ASSERT_TRUE(other.Start()); | 82 ASSERT_TRUE(other.Start()); |
83 other.message_loop()->PostTask( | 83 other.message_loop()->PostTask( |
84 FROM_HERE, NewRunnableFunction(::EnterCriticalSection, &cs)); | 84 FROM_HERE, NewRunnableFunction(::EnterCriticalSection, &cs)); |
85 | 85 |
86 ScopedHandle event(::CreateEvent(NULL, FALSE, FALSE, NULL)); | 86 base::win::ScopedHandle event(::CreateEvent(NULL, FALSE, FALSE, NULL)); |
87 other.message_loop()->PostTask( | 87 other.message_loop()->PostTask( |
88 FROM_HERE, NewRunnableFunction(::SetEvent, event.Get())); | 88 FROM_HERE, NewRunnableFunction(::SetEvent, event.Get())); |
89 | 89 |
90 ASSERT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(event.Get(), INFINITE)); | 90 ASSERT_EQ(WAIT_OBJECT_0, ::WaitForSingleObject(event.Get(), INFINITE)); |
91 | 91 |
92 // We still shouldn't own it - the other thread does. | 92 // We still shouldn't own it - the other thread does. |
93 EXPECT_FALSE(OwnsCriticalSection(&cs)); | 93 EXPECT_FALSE(OwnsCriticalSection(&cs)); |
94 // And we shouldn't be able to enter it. | 94 // And we shouldn't be able to enter it. |
95 EXPECT_EQ(0, ::TryEnterCriticalSection(&cs)); | 95 EXPECT_EQ(0, ::TryEnterCriticalSection(&cs)); |
96 | 96 |
(...skipping 21 matching lines...) Expand all Loading... |
118 | 118 |
119 EXPECT_FALSE(OwnsLoaderLock()); | 119 EXPECT_FALSE(OwnsLoaderLock()); |
120 EnterCriticalSection(loader_lock); | 120 EnterCriticalSection(loader_lock); |
121 EXPECT_TRUE(OwnsLoaderLock()); | 121 EXPECT_TRUE(OwnsLoaderLock()); |
122 LeaveCriticalSection(loader_lock); | 122 LeaveCriticalSection(loader_lock); |
123 EXPECT_FALSE(OwnsLoaderLock()); | 123 EXPECT_FALSE(OwnsLoaderLock()); |
124 } | 124 } |
125 | 125 |
126 TEST(NtLoader, GetLoaderEntry) { | 126 TEST(NtLoader, GetLoaderEntry) { |
127 // Get all modules in the current process. | 127 // Get all modules in the current process. |
128 ScopedHandle snap(::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, | 128 base::win::ScopedHandle snap( |
129 ::GetCurrentProcessId())); | 129 ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ::GetCurrentProcessId())); |
130 EXPECT_TRUE(snap.Get() != NULL); | 130 EXPECT_TRUE(snap.Get() != NULL); |
131 | 131 |
132 // Walk them, while checking we get an entry for each, and that it | 132 // Walk them, while checking we get an entry for each, and that it |
133 // contains sane information. | 133 // contains sane information. |
134 MODULEENTRY32 module = { sizeof(module) }; | 134 MODULEENTRY32 module = { sizeof(module) }; |
135 ASSERT_TRUE(::Module32First(snap.Get(), &module)); | 135 ASSERT_TRUE(::Module32First(snap.Get(), &module)); |
136 do { | 136 do { |
137 ScopedEnterCriticalSection lock(GetLoaderLock()); | 137 ScopedEnterCriticalSection lock(GetLoaderLock()); |
138 | 138 |
139 nt_loader::LDR_DATA_TABLE_ENTRY* entry = | 139 nt_loader::LDR_DATA_TABLE_ENTRY* entry = |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 311 |
312 exceptions_handled = 0; | 312 exceptions_handled = 0; |
313 set_exception_function(OnCrashDuringUnloadLibrary); | 313 set_exception_function(OnCrashDuringUnloadLibrary); |
314 | 314 |
315 // We should crash during unload. | 315 // We should crash during unload. |
316 if (module != NULL) | 316 if (module != NULL) |
317 ::FreeLibrary(module); | 317 ::FreeLibrary(module); |
318 | 318 |
319 EXPECT_EQ(1, exceptions_handled); | 319 EXPECT_EQ(1, exceptions_handled); |
320 } | 320 } |
OLD | NEW |