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/unittests/dump_analysis.h" // NOLINT |
michael@0 | 35 | #include "testing/gtest/include/gtest/gtest.h" |
michael@0 | 36 | |
michael@0 | 37 | DumpAnalysis::~DumpAnalysis() { |
michael@0 | 38 | if (dump_file_view_ != NULL) { |
michael@0 | 39 | EXPECT_TRUE(::UnmapViewOfFile(dump_file_view_)); |
michael@0 | 40 | ::CloseHandle(dump_file_mapping_); |
michael@0 | 41 | dump_file_mapping_ = NULL; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | if (dump_file_handle_ != NULL) { |
michael@0 | 45 | ::CloseHandle(dump_file_handle_); |
michael@0 | 46 | dump_file_handle_ = NULL; |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void DumpAnalysis::EnsureDumpMapped() { |
michael@0 | 51 | if (dump_file_view_ == NULL) { |
michael@0 | 52 | dump_file_handle_ = ::CreateFile(dump_file_.c_str(), |
michael@0 | 53 | GENERIC_READ, |
michael@0 | 54 | 0, |
michael@0 | 55 | NULL, |
michael@0 | 56 | OPEN_EXISTING, |
michael@0 | 57 | 0, |
michael@0 | 58 | NULL); |
michael@0 | 59 | ASSERT_TRUE(dump_file_handle_ != NULL); |
michael@0 | 60 | ASSERT_TRUE(dump_file_mapping_ == NULL); |
michael@0 | 61 | |
michael@0 | 62 | dump_file_mapping_ = ::CreateFileMapping(dump_file_handle_, |
michael@0 | 63 | NULL, |
michael@0 | 64 | PAGE_READONLY, |
michael@0 | 65 | 0, |
michael@0 | 66 | 0, |
michael@0 | 67 | NULL); |
michael@0 | 68 | ASSERT_TRUE(dump_file_mapping_ != NULL); |
michael@0 | 69 | |
michael@0 | 70 | dump_file_view_ = ::MapViewOfFile(dump_file_mapping_, |
michael@0 | 71 | FILE_MAP_READ, |
michael@0 | 72 | 0, |
michael@0 | 73 | 0, |
michael@0 | 74 | 0); |
michael@0 | 75 | ASSERT_TRUE(dump_file_view_ != NULL); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | bool DumpAnalysis::HasTebs() const { |
michael@0 | 80 | MINIDUMP_THREAD_LIST* thread_list = NULL; |
michael@0 | 81 | size_t thread_list_size = GetStream(ThreadListStream, &thread_list); |
michael@0 | 82 | |
michael@0 | 83 | if (thread_list_size > 0 && thread_list != NULL) { |
michael@0 | 84 | for (ULONG i = 0; i < thread_list->NumberOfThreads; ++i) { |
michael@0 | 85 | if (!HasMemory(thread_list->Threads[i].Teb)) |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | return true; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // No thread list, no TEB info. |
michael@0 | 93 | return false; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | bool DumpAnalysis::HasPeb() const { |
michael@0 | 97 | MINIDUMP_THREAD_LIST* thread_list = NULL; |
michael@0 | 98 | size_t thread_list_size = GetStream(ThreadListStream, &thread_list); |
michael@0 | 99 | |
michael@0 | 100 | if (thread_list_size > 0 && thread_list != NULL && |
michael@0 | 101 | thread_list->NumberOfThreads > 0) { |
michael@0 | 102 | FakeTEB* teb = NULL; |
michael@0 | 103 | if (!HasMemory(thread_list->Threads[0].Teb, &teb)) |
michael@0 | 104 | return false; |
michael@0 | 105 | |
michael@0 | 106 | return HasMemory(teb->peb); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | bool DumpAnalysis::HasStream(ULONG stream_number) const { |
michael@0 | 113 | void* stream = NULL; |
michael@0 | 114 | size_t stream_size = GetStreamImpl(stream_number, &stream); |
michael@0 | 115 | return stream_size > 0 && stream != NULL; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | size_t DumpAnalysis::GetStreamImpl(ULONG stream_number, void** stream) const { |
michael@0 | 119 | MINIDUMP_DIRECTORY* directory = NULL; |
michael@0 | 120 | ULONG memory_list_size = 0; |
michael@0 | 121 | BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_, |
michael@0 | 122 | stream_number, |
michael@0 | 123 | &directory, |
michael@0 | 124 | stream, |
michael@0 | 125 | &memory_list_size); |
michael@0 | 126 | |
michael@0 | 127 | return ret ? memory_list_size : 0; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | bool DumpAnalysis::HasMemoryImpl(const void *addr_in, size_t structuresize, |
michael@0 | 131 | void **structure) const { |
michael@0 | 132 | uintptr_t address = reinterpret_cast<uintptr_t>(addr_in); |
michael@0 | 133 | MINIDUMP_MEMORY_LIST* memory_list = NULL; |
michael@0 | 134 | size_t memory_list_size = GetStream(MemoryListStream, &memory_list); |
michael@0 | 135 | if (memory_list_size > 0 && memory_list != NULL) { |
michael@0 | 136 | for (ULONG i = 0; i < memory_list->NumberOfMemoryRanges; ++i) { |
michael@0 | 137 | MINIDUMP_MEMORY_DESCRIPTOR& descr = memory_list->MemoryRanges[i]; |
michael@0 | 138 | const uintptr_t range_start = |
michael@0 | 139 | static_cast<uintptr_t>(descr.StartOfMemoryRange); |
michael@0 | 140 | uintptr_t range_end = range_start + descr.Memory.DataSize; |
michael@0 | 141 | |
michael@0 | 142 | if (address >= range_start && |
michael@0 | 143 | address + structuresize < range_end) { |
michael@0 | 144 | // The start address falls in the range, and the end address is |
michael@0 | 145 | // in bounds, return a pointer to the structure if requested. |
michael@0 | 146 | if (structure != NULL) |
michael@0 | 147 | *structure = RVA_TO_ADDR(dump_file_view_, descr.Memory.Rva); |
michael@0 | 148 | |
michael@0 | 149 | return true; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // We didn't find the range in a MINIDUMP_MEMORY_LIST, so maybe this |
michael@0 | 155 | // is a full dump using MINIDUMP_MEMORY64_LIST with all the memory at the |
michael@0 | 156 | // end of the dump file. |
michael@0 | 157 | MINIDUMP_MEMORY64_LIST* memory64_list = NULL; |
michael@0 | 158 | memory_list_size = GetStream(Memory64ListStream, &memory64_list); |
michael@0 | 159 | if (memory_list_size > 0 && memory64_list != NULL) { |
michael@0 | 160 | // Keep track of where the current descriptor maps to. |
michael@0 | 161 | RVA64 curr_rva = memory64_list->BaseRva; |
michael@0 | 162 | for (ULONG i = 0; i < memory64_list->NumberOfMemoryRanges; ++i) { |
michael@0 | 163 | MINIDUMP_MEMORY_DESCRIPTOR64& descr = memory64_list->MemoryRanges[i]; |
michael@0 | 164 | uintptr_t range_start = |
michael@0 | 165 | static_cast<uintptr_t>(descr.StartOfMemoryRange); |
michael@0 | 166 | uintptr_t range_end = range_start + static_cast<size_t>(descr.DataSize); |
michael@0 | 167 | |
michael@0 | 168 | if (address >= range_start && |
michael@0 | 169 | address + structuresize < range_end) { |
michael@0 | 170 | // The start address falls in the range, and the end address is |
michael@0 | 171 | // in bounds, return a pointer to the structure if requested. |
michael@0 | 172 | if (structure != NULL) |
michael@0 | 173 | *structure = RVA_TO_ADDR(dump_file_view_, curr_rva); |
michael@0 | 174 | |
michael@0 | 175 | return true; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | // Advance the current RVA. |
michael@0 | 179 | curr_rva += descr.DataSize; |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | return false; |
michael@0 | 184 | } |