1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/windows/unittests/dump_analysis.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +// Copyright (c) 2010, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +#include <windows.h> 1.34 +#include <objbase.h> 1.35 +#include <dbghelp.h> 1.36 + 1.37 +#include "client/windows/unittests/dump_analysis.h" // NOLINT 1.38 +#include "testing/gtest/include/gtest/gtest.h" 1.39 + 1.40 +DumpAnalysis::~DumpAnalysis() { 1.41 + if (dump_file_view_ != NULL) { 1.42 + EXPECT_TRUE(::UnmapViewOfFile(dump_file_view_)); 1.43 + ::CloseHandle(dump_file_mapping_); 1.44 + dump_file_mapping_ = NULL; 1.45 + } 1.46 + 1.47 + if (dump_file_handle_ != NULL) { 1.48 + ::CloseHandle(dump_file_handle_); 1.49 + dump_file_handle_ = NULL; 1.50 + } 1.51 +} 1.52 + 1.53 +void DumpAnalysis::EnsureDumpMapped() { 1.54 + if (dump_file_view_ == NULL) { 1.55 + dump_file_handle_ = ::CreateFile(dump_file_.c_str(), 1.56 + GENERIC_READ, 1.57 + 0, 1.58 + NULL, 1.59 + OPEN_EXISTING, 1.60 + 0, 1.61 + NULL); 1.62 + ASSERT_TRUE(dump_file_handle_ != NULL); 1.63 + ASSERT_TRUE(dump_file_mapping_ == NULL); 1.64 + 1.65 + dump_file_mapping_ = ::CreateFileMapping(dump_file_handle_, 1.66 + NULL, 1.67 + PAGE_READONLY, 1.68 + 0, 1.69 + 0, 1.70 + NULL); 1.71 + ASSERT_TRUE(dump_file_mapping_ != NULL); 1.72 + 1.73 + dump_file_view_ = ::MapViewOfFile(dump_file_mapping_, 1.74 + FILE_MAP_READ, 1.75 + 0, 1.76 + 0, 1.77 + 0); 1.78 + ASSERT_TRUE(dump_file_view_ != NULL); 1.79 + } 1.80 +} 1.81 + 1.82 +bool DumpAnalysis::HasTebs() const { 1.83 + MINIDUMP_THREAD_LIST* thread_list = NULL; 1.84 + size_t thread_list_size = GetStream(ThreadListStream, &thread_list); 1.85 + 1.86 + if (thread_list_size > 0 && thread_list != NULL) { 1.87 + for (ULONG i = 0; i < thread_list->NumberOfThreads; ++i) { 1.88 + if (!HasMemory(thread_list->Threads[i].Teb)) 1.89 + return false; 1.90 + } 1.91 + 1.92 + return true; 1.93 + } 1.94 + 1.95 + // No thread list, no TEB info. 1.96 + return false; 1.97 +} 1.98 + 1.99 +bool DumpAnalysis::HasPeb() const { 1.100 + MINIDUMP_THREAD_LIST* thread_list = NULL; 1.101 + size_t thread_list_size = GetStream(ThreadListStream, &thread_list); 1.102 + 1.103 + if (thread_list_size > 0 && thread_list != NULL && 1.104 + thread_list->NumberOfThreads > 0) { 1.105 + FakeTEB* teb = NULL; 1.106 + if (!HasMemory(thread_list->Threads[0].Teb, &teb)) 1.107 + return false; 1.108 + 1.109 + return HasMemory(teb->peb); 1.110 + } 1.111 + 1.112 + return false; 1.113 +} 1.114 + 1.115 +bool DumpAnalysis::HasStream(ULONG stream_number) const { 1.116 + void* stream = NULL; 1.117 + size_t stream_size = GetStreamImpl(stream_number, &stream); 1.118 + return stream_size > 0 && stream != NULL; 1.119 +} 1.120 + 1.121 +size_t DumpAnalysis::GetStreamImpl(ULONG stream_number, void** stream) const { 1.122 + MINIDUMP_DIRECTORY* directory = NULL; 1.123 + ULONG memory_list_size = 0; 1.124 + BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_, 1.125 + stream_number, 1.126 + &directory, 1.127 + stream, 1.128 + &memory_list_size); 1.129 + 1.130 + return ret ? memory_list_size : 0; 1.131 +} 1.132 + 1.133 +bool DumpAnalysis::HasMemoryImpl(const void *addr_in, size_t structuresize, 1.134 + void **structure) const { 1.135 + uintptr_t address = reinterpret_cast<uintptr_t>(addr_in); 1.136 + MINIDUMP_MEMORY_LIST* memory_list = NULL; 1.137 + size_t memory_list_size = GetStream(MemoryListStream, &memory_list); 1.138 + if (memory_list_size > 0 && memory_list != NULL) { 1.139 + for (ULONG i = 0; i < memory_list->NumberOfMemoryRanges; ++i) { 1.140 + MINIDUMP_MEMORY_DESCRIPTOR& descr = memory_list->MemoryRanges[i]; 1.141 + const uintptr_t range_start = 1.142 + static_cast<uintptr_t>(descr.StartOfMemoryRange); 1.143 + uintptr_t range_end = range_start + descr.Memory.DataSize; 1.144 + 1.145 + if (address >= range_start && 1.146 + address + structuresize < range_end) { 1.147 + // The start address falls in the range, and the end address is 1.148 + // in bounds, return a pointer to the structure if requested. 1.149 + if (structure != NULL) 1.150 + *structure = RVA_TO_ADDR(dump_file_view_, descr.Memory.Rva); 1.151 + 1.152 + return true; 1.153 + } 1.154 + } 1.155 + } 1.156 + 1.157 + // We didn't find the range in a MINIDUMP_MEMORY_LIST, so maybe this 1.158 + // is a full dump using MINIDUMP_MEMORY64_LIST with all the memory at the 1.159 + // end of the dump file. 1.160 + MINIDUMP_MEMORY64_LIST* memory64_list = NULL; 1.161 + memory_list_size = GetStream(Memory64ListStream, &memory64_list); 1.162 + if (memory_list_size > 0 && memory64_list != NULL) { 1.163 + // Keep track of where the current descriptor maps to. 1.164 + RVA64 curr_rva = memory64_list->BaseRva; 1.165 + for (ULONG i = 0; i < memory64_list->NumberOfMemoryRanges; ++i) { 1.166 + MINIDUMP_MEMORY_DESCRIPTOR64& descr = memory64_list->MemoryRanges[i]; 1.167 + uintptr_t range_start = 1.168 + static_cast<uintptr_t>(descr.StartOfMemoryRange); 1.169 + uintptr_t range_end = range_start + static_cast<size_t>(descr.DataSize); 1.170 + 1.171 + if (address >= range_start && 1.172 + address + structuresize < range_end) { 1.173 + // The start address falls in the range, and the end address is 1.174 + // in bounds, return a pointer to the structure if requested. 1.175 + if (structure != NULL) 1.176 + *structure = RVA_TO_ADDR(dump_file_view_, curr_rva); 1.177 + 1.178 + return true; 1.179 + } 1.180 + 1.181 + // Advance the current RVA. 1.182 + curr_rva += descr.DataSize; 1.183 + } 1.184 + } 1.185 + 1.186 + return false; 1.187 +}