Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2010, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | #include <windows.h> |
michael@0 | 31 | #include <objbase.h> |
michael@0 | 32 | #include <dbghelp.h> |
michael@0 | 33 | |
michael@0 | 34 | #include "client/windows/crash_generation/minidump_generator.h" |
michael@0 | 35 | #include "client/windows/unittests/dump_analysis.h" // NOLINT |
michael@0 | 36 | |
michael@0 | 37 | #include "gtest/gtest.h" |
michael@0 | 38 | |
michael@0 | 39 | namespace { |
michael@0 | 40 | |
michael@0 | 41 | // Minidump with stacks, PEB, TEB, and unloaded module list. |
michael@0 | 42 | const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>( |
michael@0 | 43 | MiniDumpWithProcessThreadData | // Get PEB and TEB. |
michael@0 | 44 | MiniDumpWithUnloadedModules); // Get unloaded modules when available. |
michael@0 | 45 | |
michael@0 | 46 | // Minidump with all of the above, plus memory referenced from stack. |
michael@0 | 47 | const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>( |
michael@0 | 48 | MiniDumpWithProcessThreadData | // Get PEB and TEB. |
michael@0 | 49 | MiniDumpWithUnloadedModules | // Get unloaded modules when available. |
michael@0 | 50 | MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by stack. |
michael@0 | 51 | |
michael@0 | 52 | // Large dump with all process memory. |
michael@0 | 53 | const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>( |
michael@0 | 54 | MiniDumpWithFullMemory | // Full memory from process. |
michael@0 | 55 | MiniDumpWithProcessThreadData | // Get PEB and TEB. |
michael@0 | 56 | MiniDumpWithHandleData | // Get all handle information. |
michael@0 | 57 | MiniDumpWithUnloadedModules); // Get unloaded modules when available. |
michael@0 | 58 | |
michael@0 | 59 | class MinidumpTest: public testing::Test { |
michael@0 | 60 | public: |
michael@0 | 61 | MinidumpTest() { |
michael@0 | 62 | wchar_t temp_dir_path[ MAX_PATH ] = {0}; |
michael@0 | 63 | ::GetTempPath(MAX_PATH, temp_dir_path); |
michael@0 | 64 | dump_path_ = temp_dir_path; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | virtual void SetUp() { |
michael@0 | 68 | // Make sure URLMon isn't loaded into our process. |
michael@0 | 69 | ASSERT_EQ(NULL, ::GetModuleHandle(L"urlmon.dll")); |
michael@0 | 70 | |
michael@0 | 71 | // Then load and unload it to ensure we have something to |
michael@0 | 72 | // stock the unloaded module list with. |
michael@0 | 73 | HMODULE urlmon = ::LoadLibrary(L"urlmon.dll"); |
michael@0 | 74 | ASSERT_TRUE(urlmon != NULL); |
michael@0 | 75 | ASSERT_TRUE(::FreeLibrary(urlmon)); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | virtual void TearDown() { |
michael@0 | 79 | if (!dump_file_.empty()) { |
michael@0 | 80 | ::DeleteFile(dump_file_.c_str()); |
michael@0 | 81 | dump_file_ = L""; |
michael@0 | 82 | } |
michael@0 | 83 | if (!full_dump_file_.empty()) { |
michael@0 | 84 | ::DeleteFile(full_dump_file_.c_str()); |
michael@0 | 85 | full_dump_file_ = L""; |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | bool WriteDump(ULONG flags) { |
michael@0 | 90 | using google_breakpad::MinidumpGenerator; |
michael@0 | 91 | |
michael@0 | 92 | // Fake exception is access violation on write to this. |
michael@0 | 93 | EXCEPTION_RECORD ex_record = { |
michael@0 | 94 | STATUS_ACCESS_VIOLATION, // ExceptionCode |
michael@0 | 95 | 0, // ExceptionFlags |
michael@0 | 96 | NULL, // ExceptionRecord; |
michael@0 | 97 | reinterpret_cast<void*>(0xCAFEBABE), // ExceptionAddress; |
michael@0 | 98 | 2, // NumberParameters; |
michael@0 | 99 | { EXCEPTION_WRITE_FAULT, reinterpret_cast<ULONG_PTR>(this) } |
michael@0 | 100 | }; |
michael@0 | 101 | CONTEXT ctx_record = {}; |
michael@0 | 102 | EXCEPTION_POINTERS ex_ptrs = { |
michael@0 | 103 | &ex_record, |
michael@0 | 104 | &ctx_record, |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | MinidumpGenerator generator(dump_path_); |
michael@0 | 108 | |
michael@0 | 109 | // And write a dump |
michael@0 | 110 | bool result = generator.WriteMinidump(::GetCurrentProcess(), |
michael@0 | 111 | ::GetCurrentProcessId(), |
michael@0 | 112 | ::GetCurrentThreadId(), |
michael@0 | 113 | ::GetCurrentThreadId(), |
michael@0 | 114 | &ex_ptrs, |
michael@0 | 115 | NULL, |
michael@0 | 116 | static_cast<MINIDUMP_TYPE>(flags), |
michael@0 | 117 | TRUE, |
michael@0 | 118 | &dump_file_, |
michael@0 | 119 | &full_dump_file_); |
michael@0 | 120 | return result == TRUE; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | protected: |
michael@0 | 124 | std::wstring dump_file_; |
michael@0 | 125 | std::wstring full_dump_file_; |
michael@0 | 126 | |
michael@0 | 127 | std::wstring dump_path_; |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | // We need to be able to get file information from Windows |
michael@0 | 131 | bool HasFileInfo(const std::wstring& file_path) { |
michael@0 | 132 | DWORD dummy; |
michael@0 | 133 | const wchar_t* path = file_path.c_str(); |
michael@0 | 134 | DWORD length = ::GetFileVersionInfoSize(path, &dummy); |
michael@0 | 135 | if (length == 0) |
michael@0 | 136 | return NULL; |
michael@0 | 137 | |
michael@0 | 138 | void* data = calloc(length, 1); |
michael@0 | 139 | if (!data) |
michael@0 | 140 | return false; |
michael@0 | 141 | |
michael@0 | 142 | if (!::GetFileVersionInfo(path, dummy, length, data)) { |
michael@0 | 143 | free(data); |
michael@0 | 144 | return false; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void* translate = NULL; |
michael@0 | 148 | UINT page_count; |
michael@0 | 149 | BOOL query_result = VerQueryValue( |
michael@0 | 150 | data, |
michael@0 | 151 | L"\\VarFileInfo\\Translation", |
michael@0 | 152 | static_cast<void**>(&translate), |
michael@0 | 153 | &page_count); |
michael@0 | 154 | |
michael@0 | 155 | free(data); |
michael@0 | 156 | if (query_result && translate) { |
michael@0 | 157 | return true; |
michael@0 | 158 | } else { |
michael@0 | 159 | return false; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | TEST_F(MinidumpTest, Version) { |
michael@0 | 164 | API_VERSION* version = ::ImagehlpApiVersion(); |
michael@0 | 165 | |
michael@0 | 166 | HMODULE dbg_help = ::GetModuleHandle(L"dbghelp.dll"); |
michael@0 | 167 | ASSERT_TRUE(dbg_help != NULL); |
michael@0 | 168 | |
michael@0 | 169 | wchar_t dbg_help_file[1024] = {}; |
michael@0 | 170 | ASSERT_TRUE(::GetModuleFileName(dbg_help, |
michael@0 | 171 | dbg_help_file, |
michael@0 | 172 | sizeof(dbg_help_file) / |
michael@0 | 173 | sizeof(*dbg_help_file))); |
michael@0 | 174 | ASSERT_TRUE(HasFileInfo(std::wstring(dbg_help_file)) != NULL); |
michael@0 | 175 | |
michael@0 | 176 | // LOG(INFO) << "DbgHelp.dll version: " << file_info->file_version(); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | TEST_F(MinidumpTest, Normal) { |
michael@0 | 180 | EXPECT_TRUE(WriteDump(MiniDumpNormal)); |
michael@0 | 181 | DumpAnalysis mini(dump_file_); |
michael@0 | 182 | |
michael@0 | 183 | // We expect threads, modules and some memory. |
michael@0 | 184 | EXPECT_TRUE(mini.HasStream(ThreadListStream)); |
michael@0 | 185 | EXPECT_TRUE(mini.HasStream(ModuleListStream)); |
michael@0 | 186 | EXPECT_TRUE(mini.HasStream(MemoryListStream)); |
michael@0 | 187 | EXPECT_TRUE(mini.HasStream(ExceptionStream)); |
michael@0 | 188 | EXPECT_TRUE(mini.HasStream(SystemInfoStream)); |
michael@0 | 189 | EXPECT_TRUE(mini.HasStream(MiscInfoStream)); |
michael@0 | 190 | |
michael@0 | 191 | EXPECT_FALSE(mini.HasStream(ThreadExListStream)); |
michael@0 | 192 | EXPECT_FALSE(mini.HasStream(Memory64ListStream)); |
michael@0 | 193 | EXPECT_FALSE(mini.HasStream(CommentStreamA)); |
michael@0 | 194 | EXPECT_FALSE(mini.HasStream(CommentStreamW)); |
michael@0 | 195 | EXPECT_FALSE(mini.HasStream(HandleDataStream)); |
michael@0 | 196 | EXPECT_FALSE(mini.HasStream(FunctionTableStream)); |
michael@0 | 197 | EXPECT_FALSE(mini.HasStream(UnloadedModuleListStream)); |
michael@0 | 198 | EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); |
michael@0 | 199 | EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); |
michael@0 | 200 | EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); |
michael@0 | 201 | EXPECT_FALSE(mini.HasStream(TokenStream)); |
michael@0 | 202 | |
michael@0 | 203 | // We expect no PEB nor TEBs in this dump. |
michael@0 | 204 | EXPECT_FALSE(mini.HasTebs()); |
michael@0 | 205 | EXPECT_FALSE(mini.HasPeb()); |
michael@0 | 206 | |
michael@0 | 207 | // We expect no off-stack memory in this dump. |
michael@0 | 208 | EXPECT_FALSE(mini.HasMemory(this)); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | TEST_F(MinidumpTest, SmallDump) { |
michael@0 | 212 | ASSERT_TRUE(WriteDump(kSmallDumpType)); |
michael@0 | 213 | DumpAnalysis mini(dump_file_); |
michael@0 | 214 | |
michael@0 | 215 | EXPECT_TRUE(mini.HasStream(ThreadListStream)); |
michael@0 | 216 | EXPECT_TRUE(mini.HasStream(ModuleListStream)); |
michael@0 | 217 | EXPECT_TRUE(mini.HasStream(MemoryListStream)); |
michael@0 | 218 | EXPECT_TRUE(mini.HasStream(ExceptionStream)); |
michael@0 | 219 | EXPECT_TRUE(mini.HasStream(SystemInfoStream)); |
michael@0 | 220 | EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); |
michael@0 | 221 | EXPECT_TRUE(mini.HasStream(MiscInfoStream)); |
michael@0 | 222 | |
michael@0 | 223 | // We expect PEB and TEBs in this dump. |
michael@0 | 224 | EXPECT_TRUE(mini.HasTebs()); |
michael@0 | 225 | EXPECT_TRUE(mini.HasPeb()); |
michael@0 | 226 | |
michael@0 | 227 | EXPECT_FALSE(mini.HasStream(ThreadExListStream)); |
michael@0 | 228 | EXPECT_FALSE(mini.HasStream(Memory64ListStream)); |
michael@0 | 229 | EXPECT_FALSE(mini.HasStream(CommentStreamA)); |
michael@0 | 230 | EXPECT_FALSE(mini.HasStream(CommentStreamW)); |
michael@0 | 231 | EXPECT_FALSE(mini.HasStream(HandleDataStream)); |
michael@0 | 232 | EXPECT_FALSE(mini.HasStream(FunctionTableStream)); |
michael@0 | 233 | EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); |
michael@0 | 234 | EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); |
michael@0 | 235 | EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); |
michael@0 | 236 | EXPECT_FALSE(mini.HasStream(TokenStream)); |
michael@0 | 237 | |
michael@0 | 238 | // We expect no off-stack memory in this dump. |
michael@0 | 239 | EXPECT_FALSE(mini.HasMemory(this)); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | TEST_F(MinidumpTest, LargerDump) { |
michael@0 | 243 | ASSERT_TRUE(WriteDump(kLargerDumpType)); |
michael@0 | 244 | DumpAnalysis mini(dump_file_); |
michael@0 | 245 | |
michael@0 | 246 | // The dump should have all of these streams. |
michael@0 | 247 | EXPECT_TRUE(mini.HasStream(ThreadListStream)); |
michael@0 | 248 | EXPECT_TRUE(mini.HasStream(ModuleListStream)); |
michael@0 | 249 | EXPECT_TRUE(mini.HasStream(MemoryListStream)); |
michael@0 | 250 | EXPECT_TRUE(mini.HasStream(ExceptionStream)); |
michael@0 | 251 | EXPECT_TRUE(mini.HasStream(SystemInfoStream)); |
michael@0 | 252 | EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); |
michael@0 | 253 | EXPECT_TRUE(mini.HasStream(MiscInfoStream)); |
michael@0 | 254 | |
michael@0 | 255 | // We expect memory referenced by stack in this dump. |
michael@0 | 256 | EXPECT_TRUE(mini.HasMemory(this)); |
michael@0 | 257 | |
michael@0 | 258 | // We expect PEB and TEBs in this dump. |
michael@0 | 259 | EXPECT_TRUE(mini.HasTebs()); |
michael@0 | 260 | EXPECT_TRUE(mini.HasPeb()); |
michael@0 | 261 | |
michael@0 | 262 | EXPECT_FALSE(mini.HasStream(ThreadExListStream)); |
michael@0 | 263 | EXPECT_FALSE(mini.HasStream(Memory64ListStream)); |
michael@0 | 264 | EXPECT_FALSE(mini.HasStream(CommentStreamA)); |
michael@0 | 265 | EXPECT_FALSE(mini.HasStream(CommentStreamW)); |
michael@0 | 266 | EXPECT_FALSE(mini.HasStream(HandleDataStream)); |
michael@0 | 267 | EXPECT_FALSE(mini.HasStream(FunctionTableStream)); |
michael@0 | 268 | EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); |
michael@0 | 269 | EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); |
michael@0 | 270 | EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); |
michael@0 | 271 | EXPECT_FALSE(mini.HasStream(TokenStream)); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | TEST_F(MinidumpTest, FullDump) { |
michael@0 | 275 | ASSERT_TRUE(WriteDump(kFullDumpType)); |
michael@0 | 276 | ASSERT_TRUE(dump_file_ != L""); |
michael@0 | 277 | ASSERT_TRUE(full_dump_file_ != L""); |
michael@0 | 278 | DumpAnalysis mini(dump_file_); |
michael@0 | 279 | DumpAnalysis full(full_dump_file_); |
michael@0 | 280 | |
michael@0 | 281 | // Either dumps can contain part of the information. |
michael@0 | 282 | |
michael@0 | 283 | // The dump should have all of these streams. |
michael@0 | 284 | EXPECT_TRUE(mini.HasStream(ThreadListStream)); |
michael@0 | 285 | EXPECT_TRUE(full.HasStream(ThreadListStream)); |
michael@0 | 286 | EXPECT_TRUE(mini.HasStream(ModuleListStream)); |
michael@0 | 287 | EXPECT_TRUE(full.HasStream(ModuleListStream)); |
michael@0 | 288 | EXPECT_TRUE(mini.HasStream(ExceptionStream)); |
michael@0 | 289 | EXPECT_TRUE(full.HasStream(ExceptionStream)); |
michael@0 | 290 | EXPECT_TRUE(mini.HasStream(SystemInfoStream)); |
michael@0 | 291 | EXPECT_TRUE(full.HasStream(SystemInfoStream)); |
michael@0 | 292 | EXPECT_TRUE(mini.HasStream(UnloadedModuleListStream)); |
michael@0 | 293 | EXPECT_TRUE(full.HasStream(UnloadedModuleListStream)); |
michael@0 | 294 | EXPECT_TRUE(mini.HasStream(MiscInfoStream)); |
michael@0 | 295 | EXPECT_TRUE(full.HasStream(MiscInfoStream)); |
michael@0 | 296 | EXPECT_TRUE(mini.HasStream(HandleDataStream)); |
michael@0 | 297 | EXPECT_TRUE(full.HasStream(HandleDataStream)); |
michael@0 | 298 | |
michael@0 | 299 | // We expect memory referenced by stack in this dump. |
michael@0 | 300 | EXPECT_FALSE(mini.HasMemory(this)); |
michael@0 | 301 | EXPECT_TRUE(full.HasMemory(this)); |
michael@0 | 302 | |
michael@0 | 303 | // We expect PEB and TEBs in this dump. |
michael@0 | 304 | EXPECT_TRUE(mini.HasTebs() || full.HasTebs()); |
michael@0 | 305 | EXPECT_TRUE(mini.HasPeb() || full.HasPeb()); |
michael@0 | 306 | |
michael@0 | 307 | EXPECT_TRUE(mini.HasStream(MemoryListStream)); |
michael@0 | 308 | EXPECT_TRUE(full.HasStream(Memory64ListStream)); |
michael@0 | 309 | EXPECT_FALSE(mini.HasStream(Memory64ListStream)); |
michael@0 | 310 | EXPECT_FALSE(full.HasStream(MemoryListStream)); |
michael@0 | 311 | |
michael@0 | 312 | // This is the only place we don't use OR because we want both not |
michael@0 | 313 | // to have the streams. |
michael@0 | 314 | EXPECT_FALSE(mini.HasStream(ThreadExListStream)); |
michael@0 | 315 | EXPECT_FALSE(full.HasStream(ThreadExListStream)); |
michael@0 | 316 | EXPECT_FALSE(mini.HasStream(CommentStreamA)); |
michael@0 | 317 | EXPECT_FALSE(full.HasStream(CommentStreamA)); |
michael@0 | 318 | EXPECT_FALSE(mini.HasStream(CommentStreamW)); |
michael@0 | 319 | EXPECT_FALSE(full.HasStream(CommentStreamW)); |
michael@0 | 320 | EXPECT_FALSE(mini.HasStream(FunctionTableStream)); |
michael@0 | 321 | EXPECT_FALSE(full.HasStream(FunctionTableStream)); |
michael@0 | 322 | EXPECT_FALSE(mini.HasStream(MemoryInfoListStream)); |
michael@0 | 323 | EXPECT_FALSE(full.HasStream(MemoryInfoListStream)); |
michael@0 | 324 | EXPECT_FALSE(mini.HasStream(ThreadInfoListStream)); |
michael@0 | 325 | EXPECT_FALSE(full.HasStream(ThreadInfoListStream)); |
michael@0 | 326 | EXPECT_FALSE(mini.HasStream(HandleOperationListStream)); |
michael@0 | 327 | EXPECT_FALSE(full.HasStream(HandleOperationListStream)); |
michael@0 | 328 | EXPECT_FALSE(mini.HasStream(TokenStream)); |
michael@0 | 329 | EXPECT_FALSE(full.HasStream(TokenStream)); |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | } // namespace |