Home | History | Annotate | Download | only in crash_reporting
      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
      3 // found in the LICENSE file.
      4 
      5 #include <windows.h>
      6 #include <dbghelp.h>
      7 #include <objbase.h>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/command_line.h"
     11 #include "base/file_util.h"
     12 #include "base/file_version_info.h"
     13 #include "base/files/file_path.h"
     14 #include "base/logging.h"
     15 #include "base/win/scoped_handle.h"
     16 #include "gtest/gtest.h"
     17 
     18 namespace {
     19 
     20 // Convenience to get to the PEB pointer in a TEB.
     21 struct FakeTEB {
     22   char dummy[0x30];
     23   void* peb;
     24 };
     25 
     26 // Minidump with stacks, PEB, TEB, and unloaded module list.
     27 const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>(
     28     MiniDumpWithProcessThreadData |  // Get PEB and TEB.
     29     MiniDumpWithUnloadedModules);  // Get unloaded modules when available.
     30 
     31 // Minidump with all of the above, plus memory referenced from stack.
     32 const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>(
     33     MiniDumpWithProcessThreadData |  // Get PEB and TEB.
     34     MiniDumpWithUnloadedModules |  // Get unloaded modules when available.
     35     MiniDumpWithIndirectlyReferencedMemory);  // Get memory referenced by stack.
     36 
     37 // Large dump with all process memory.
     38 const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>(
     39     MiniDumpWithFullMemory |  // Full memory from process.
     40     MiniDumpWithProcessThreadData |  // Get PEB and TEB.
     41     MiniDumpWithHandleData |  // Get all handle information.
     42     MiniDumpWithUnloadedModules);  // Get unloaded modules when available.
     43 
     44 
     45 class MinidumpTest: public testing::Test {
     46  public:
     47   MinidumpTest() : dump_file_view_(NULL) {
     48   }
     49 
     50   virtual void SetUp() {
     51     // Make sure URLMon isn't loaded into our process.
     52     ASSERT_EQ(NULL, ::GetModuleHandle(L"urlmon.dll"));
     53 
     54     // Then load and unload it to ensure we have something to
     55     // stock the unloaded module list with.
     56     HMODULE urlmon = ::LoadLibrary(L"urlmon.dll");
     57     ASSERT_TRUE(urlmon != NULL);
     58     ASSERT_TRUE(::FreeLibrary(urlmon));
     59 
     60     ASSERT_TRUE(base::CreateTemporaryFile(&dump_file_));
     61     dump_file_handle_.Set(::CreateFile(dump_file_.value().c_str(),
     62                                        GENERIC_WRITE | GENERIC_READ,
     63                                        0,
     64                                        NULL,
     65                                        OPEN_EXISTING,
     66                                        0,
     67                                        NULL));
     68     ASSERT_TRUE(dump_file_handle_.IsValid());
     69   }
     70 
     71   virtual void TearDown() {
     72     if (dump_file_view_ != NULL) {
     73       EXPECT_TRUE(::UnmapViewOfFile(dump_file_view_));
     74       dump_file_mapping_.Close();
     75     }
     76 
     77     dump_file_handle_.Close();
     78     EXPECT_TRUE(base::DeleteFile(dump_file_, false));
     79   }
     80 
     81   void EnsureDumpMapped() {
     82     ASSERT_TRUE(dump_file_handle_.IsValid());
     83     if (dump_file_view_ == NULL) {
     84       ASSERT_FALSE(dump_file_mapping_.IsValid());
     85 
     86       dump_file_mapping_.Set(::CreateFileMapping(dump_file_handle_,
     87                                                  NULL,
     88                                                  PAGE_READONLY,
     89                                                  0,
     90                                                  0,
     91                                                  NULL));
     92       ASSERT_TRUE(dump_file_mapping_.IsValid());
     93 
     94       dump_file_view_ = ::MapViewOfFile(dump_file_mapping_.Get(),
     95                                         FILE_MAP_READ,
     96                                         0,
     97                                         0,
     98                                         0);
     99       ASSERT_TRUE(dump_file_view_ != NULL);
    100     }
    101   }
    102 
    103   bool WriteDump(ULONG flags) {
    104     // Fake exception is access violation on write to this.
    105     EXCEPTION_RECORD ex_record = {
    106         STATUS_ACCESS_VIOLATION,  // ExceptionCode
    107         0,  // ExceptionFlags
    108         NULL,  // ExceptionRecord;
    109         reinterpret_cast<void*>(0xCAFEBABE), // ExceptionAddress;
    110         2,  // NumberParameters;
    111         { EXCEPTION_WRITE_FAULT, reinterpret_cast<ULONG_PTR>(this) }
    112     };
    113     CONTEXT ctx_record = {};
    114     EXCEPTION_POINTERS ex_ptrs = {
    115       &ex_record,
    116       &ctx_record,
    117     };
    118     MINIDUMP_EXCEPTION_INFORMATION ex_info = {
    119         ::GetCurrentThreadId(),
    120         &ex_ptrs,
    121         FALSE,
    122     };
    123 
    124     // Capture our register context.
    125     ::RtlCaptureContext(&ctx_record);
    126 
    127     // And write a dump
    128     BOOL result = ::MiniDumpWriteDump(::GetCurrentProcess(),
    129                                       ::GetCurrentProcessId(),
    130                                       dump_file_handle_.Get(),
    131                                       static_cast<MINIDUMP_TYPE>(flags),
    132                                       &ex_info,
    133                                       NULL,
    134                                       NULL);
    135     VLOG(1) << "Flags: " << flags << " mindump size: "
    136             << ::GetFileSize(dump_file_handle_.Get(), NULL);
    137 
    138     return result == TRUE;
    139   }
    140 
    141   bool DumpHasStream(ULONG stream_number) {
    142     EnsureDumpMapped();
    143 
    144     MINIDUMP_DIRECTORY* directory = NULL;
    145     void* stream = NULL;
    146     ULONG stream_size = 0;
    147     BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_,
    148                                         stream_number,
    149                                         &directory,
    150                                         &stream,
    151                                         &stream_size);
    152 
    153     return ret != FALSE && stream != NULL && stream_size > 0;
    154   }
    155 
    156   template <class StreamType>
    157   size_t GetStream(ULONG stream_number, StreamType** stream) {
    158     EnsureDumpMapped();
    159     MINIDUMP_DIRECTORY* directory = NULL;
    160     ULONG memory_list_size = 0;
    161     BOOL ret = ::MiniDumpReadDumpStream(dump_file_view_,
    162                                         stream_number,
    163                                         &directory,
    164                                         reinterpret_cast<void**>(stream),
    165                                         &memory_list_size);
    166 
    167     return ret ? memory_list_size : 0;
    168   }
    169 
    170   bool DumpHasTebs() {
    171     MINIDUMP_THREAD_LIST* thread_list = NULL;
    172     size_t thread_list_size = GetStream(ThreadListStream, &thread_list);
    173 
    174     if (thread_list_size > 0 && thread_list != NULL) {
    175       for (ULONG i = 0; i < thread_list->NumberOfThreads; ++i) {
    176         if (!DumpHasMemory(thread_list->Threads[i].Teb))
    177           return false;
    178       }
    179 
    180       return true;
    181     }
    182 
    183     // No thread list, no TEB info.
    184     return false;
    185   }
    186 
    187   bool DumpHasPeb() {
    188     MINIDUMP_THREAD_LIST* thread_list = NULL;
    189     size_t thread_list_size = GetStream(ThreadListStream, &thread_list);
    190 
    191     if (thread_list_size > 0 && thread_list != NULL &&
    192         thread_list->NumberOfThreads > 0) {
    193       FakeTEB* teb = NULL;
    194       if (!DumpHasMemory(thread_list->Threads[0].Teb, &teb))
    195         return false;
    196 
    197       return DumpHasMemory(teb->peb);
    198     }
    199 
    200     return false;
    201   }
    202 
    203   bool DumpHasMemory(ULONG64 address) {
    204     return DumpHasMemory<uint8>(address, NULL);
    205   }
    206 
    207   bool DumpHasMemory(const void* address) {
    208     return DumpHasMemory<uint8>(address, NULL);
    209   }
    210 
    211   template <class StructureType>
    212   bool DumpHasMemory(ULONG64 address, StructureType** structure = NULL) {
    213     // We can't cope with 64 bit addresses for now.
    214     if (address > 0xFFFFFFFFUL)
    215       return false;
    216 
    217     return DumpHasMemory(reinterpret_cast<void*>(address), structure);
    218   }
    219 
    220   template <class StructureType>
    221   bool DumpHasMemory(const void* addr_in, StructureType** structure = NULL) {
    222     uintptr_t address = reinterpret_cast<uintptr_t>(addr_in);
    223     MINIDUMP_MEMORY_LIST* memory_list = NULL;
    224     size_t memory_list_size = GetStream(MemoryListStream, &memory_list);
    225     if (memory_list_size > 0 && memory_list != NULL) {
    226       for (ULONG i = 0; i < memory_list->NumberOfMemoryRanges; ++i) {
    227         MINIDUMP_MEMORY_DESCRIPTOR& descr = memory_list->MemoryRanges[i];
    228         const uintptr_t range_start =
    229             static_cast<uintptr_t>(descr.StartOfMemoryRange);
    230         uintptr_t range_end = range_start + descr.Memory.DataSize;
    231 
    232         if (address >= range_start &&
    233             address + sizeof(StructureType) < range_end) {
    234           // The start address falls in the range, and the end address is
    235           // in bounds, return a pointer to the structure if requested.
    236           if (structure != NULL)
    237             *structure = reinterpret_cast<StructureType*>(
    238                 RVA_TO_ADDR(dump_file_view_, descr.Memory.Rva));
    239 
    240           return true;
    241         }
    242       }
    243     }
    244 
    245     // We didn't find the range in a MINIDUMP_MEMORY_LIST, so maybe this
    246     // is a full dump using MINIDUMP_MEMORY64_LIST with all the memory at the
    247     // end of the dump file.
    248     MINIDUMP_MEMORY64_LIST* memory64_list = NULL;
    249     memory_list_size = GetStream(Memory64ListStream, &memory64_list);
    250     if (memory_list_size > 0 && memory64_list != NULL) {
    251       // Keep track of where the current descriptor maps to.
    252       RVA64 curr_rva = memory64_list->BaseRva;
    253       for (ULONG i = 0; i < memory64_list->NumberOfMemoryRanges; ++i) {
    254         MINIDUMP_MEMORY_DESCRIPTOR64& descr = memory64_list->MemoryRanges[i];
    255         uintptr_t range_start =
    256             static_cast<uintptr_t>(descr.StartOfMemoryRange);
    257         uintptr_t range_end = range_start + static_cast<size_t>(descr.DataSize);
    258 
    259         if (address >= range_start &&
    260             address + sizeof(StructureType) < range_end) {
    261           // The start address falls in the range, and the end address is
    262           // in bounds, return a pointer to the structure if requested.
    263           if (structure != NULL)
    264             *structure = reinterpret_cast<StructureType*>(
    265                 RVA_TO_ADDR(dump_file_view_, curr_rva));
    266 
    267           return true;
    268         }
    269 
    270         // Advance the current RVA.
    271         curr_rva += descr.DataSize;
    272       }
    273     }
    274 
    275 
    276 
    277     return false;
    278   }
    279 
    280  protected:
    281   base::win::ScopedHandle dump_file_handle_;
    282   base::win::ScopedHandle dump_file_mapping_;
    283   void* dump_file_view_;
    284 
    285   base::FilePath dump_file_;
    286 };
    287 
    288 TEST_F(MinidumpTest, Version) {
    289   API_VERSION* version = ::ImagehlpApiVersion();
    290 
    291   VLOG(1) << "Imagehlp Api Version: " << version->MajorVersion << "."
    292           << version->MinorVersion << "." << version->Revision;
    293 
    294   HMODULE dbg_help = ::GetModuleHandle(L"dbghelp.dll");
    295   ASSERT_TRUE(dbg_help != NULL);
    296 
    297   wchar_t dbg_help_file[1024] = {};
    298   ASSERT_TRUE(::GetModuleFileName(dbg_help,
    299                                   dbg_help_file,
    300                                   arraysize(dbg_help_file)));
    301   scoped_ptr<FileVersionInfo> file_info(
    302       FileVersionInfo::CreateFileVersionInfo(base::FilePath(dbg_help_file)));
    303   ASSERT_TRUE(file_info != NULL);
    304 
    305   VLOG(1) << "DbgHelp.dll version: " << file_info->file_version();
    306 }
    307 
    308 TEST_F(MinidumpTest, Normal) {
    309   EXPECT_TRUE(WriteDump(MiniDumpNormal));
    310 
    311   // We expect threads, modules and some memory.
    312   EXPECT_TRUE(DumpHasStream(ThreadListStream));
    313   EXPECT_TRUE(DumpHasStream(ModuleListStream));
    314   EXPECT_TRUE(DumpHasStream(MemoryListStream));
    315   EXPECT_TRUE(DumpHasStream(ExceptionStream));
    316   EXPECT_TRUE(DumpHasStream(SystemInfoStream));
    317   EXPECT_TRUE(DumpHasStream(MiscInfoStream));
    318 
    319   EXPECT_FALSE(DumpHasStream(ThreadExListStream));
    320   EXPECT_FALSE(DumpHasStream(Memory64ListStream));
    321   EXPECT_FALSE(DumpHasStream(CommentStreamA));
    322   EXPECT_FALSE(DumpHasStream(CommentStreamW));
    323   EXPECT_FALSE(DumpHasStream(HandleDataStream));
    324   EXPECT_FALSE(DumpHasStream(FunctionTableStream));
    325   EXPECT_FALSE(DumpHasStream(UnloadedModuleListStream));
    326   EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
    327   EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
    328   EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
    329   EXPECT_FALSE(DumpHasStream(TokenStream));
    330 
    331   // We expect no PEB nor TEBs in this dump.
    332   EXPECT_FALSE(DumpHasTebs());
    333   EXPECT_FALSE(DumpHasPeb());
    334 
    335   // We expect no off-stack memory in this dump.
    336   EXPECT_FALSE(DumpHasMemory(this));
    337 }
    338 
    339 TEST_F(MinidumpTest, SmallDump) {
    340   ASSERT_TRUE(WriteDump(kSmallDumpType));
    341 
    342   EXPECT_TRUE(DumpHasStream(ThreadListStream));
    343   EXPECT_TRUE(DumpHasStream(ModuleListStream));
    344   EXPECT_TRUE(DumpHasStream(MemoryListStream));
    345   EXPECT_TRUE(DumpHasStream(ExceptionStream));
    346   EXPECT_TRUE(DumpHasStream(SystemInfoStream));
    347   EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
    348   EXPECT_TRUE(DumpHasStream(MiscInfoStream));
    349 
    350   // We expect PEB and TEBs in this dump.
    351   EXPECT_TRUE(DumpHasTebs());
    352   EXPECT_TRUE(DumpHasPeb());
    353 
    354   EXPECT_FALSE(DumpHasStream(ThreadExListStream));
    355   EXPECT_FALSE(DumpHasStream(Memory64ListStream));
    356   EXPECT_FALSE(DumpHasStream(CommentStreamA));
    357   EXPECT_FALSE(DumpHasStream(CommentStreamW));
    358   EXPECT_FALSE(DumpHasStream(HandleDataStream));
    359   EXPECT_FALSE(DumpHasStream(FunctionTableStream));
    360   EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
    361   EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
    362   EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
    363   EXPECT_FALSE(DumpHasStream(TokenStream));
    364 
    365   // We expect no off-stack memory in this dump.
    366   EXPECT_FALSE(DumpHasMemory(this));
    367 }
    368 
    369 TEST_F(MinidumpTest, LargerDump) {
    370   ASSERT_TRUE(WriteDump(kLargerDumpType));
    371 
    372   // The dump should have all of these streams.
    373   EXPECT_TRUE(DumpHasStream(ThreadListStream));
    374   EXPECT_TRUE(DumpHasStream(ModuleListStream));
    375   EXPECT_TRUE(DumpHasStream(MemoryListStream));
    376   EXPECT_TRUE(DumpHasStream(ExceptionStream));
    377   EXPECT_TRUE(DumpHasStream(SystemInfoStream));
    378   EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
    379   EXPECT_TRUE(DumpHasStream(MiscInfoStream));
    380 
    381   // We expect memory referenced by stack in this dump.
    382   EXPECT_TRUE(DumpHasMemory(this));
    383 
    384   // We expect PEB and TEBs in this dump.
    385   EXPECT_TRUE(DumpHasTebs());
    386   EXPECT_TRUE(DumpHasPeb());
    387 
    388   EXPECT_FALSE(DumpHasStream(ThreadExListStream));
    389   EXPECT_FALSE(DumpHasStream(Memory64ListStream));
    390   EXPECT_FALSE(DumpHasStream(CommentStreamA));
    391   EXPECT_FALSE(DumpHasStream(CommentStreamW));
    392   EXPECT_FALSE(DumpHasStream(HandleDataStream));
    393   EXPECT_FALSE(DumpHasStream(FunctionTableStream));
    394   EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
    395   EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
    396   EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
    397   EXPECT_FALSE(DumpHasStream(TokenStream));
    398 }
    399 
    400 TEST_F(MinidumpTest, FullDump) {
    401   ASSERT_TRUE(WriteDump(kFullDumpType));
    402 
    403   // The dump should have all of these streams.
    404   EXPECT_TRUE(DumpHasStream(ThreadListStream));
    405   EXPECT_TRUE(DumpHasStream(ModuleListStream));
    406   EXPECT_TRUE(DumpHasStream(Memory64ListStream));
    407   EXPECT_TRUE(DumpHasStream(ExceptionStream));
    408   EXPECT_TRUE(DumpHasStream(SystemInfoStream));
    409   EXPECT_TRUE(DumpHasStream(UnloadedModuleListStream));
    410   EXPECT_TRUE(DumpHasStream(MiscInfoStream));
    411   EXPECT_TRUE(DumpHasStream(HandleDataStream));
    412 
    413   // We expect memory referenced by stack in this dump.
    414   EXPECT_TRUE(DumpHasMemory(this));
    415 
    416   // We expect PEB and TEBs in this dump.
    417   EXPECT_TRUE(DumpHasTebs());
    418   EXPECT_TRUE(DumpHasPeb());
    419 
    420   EXPECT_FALSE(DumpHasStream(ThreadExListStream));
    421   EXPECT_FALSE(DumpHasStream(MemoryListStream));
    422   EXPECT_FALSE(DumpHasStream(CommentStreamA));
    423   EXPECT_FALSE(DumpHasStream(CommentStreamW));
    424   EXPECT_FALSE(DumpHasStream(FunctionTableStream));
    425   EXPECT_FALSE(DumpHasStream(MemoryInfoListStream));
    426   EXPECT_FALSE(DumpHasStream(ThreadInfoListStream));
    427   EXPECT_FALSE(DumpHasStream(HandleOperationListStream));
    428   EXPECT_FALSE(DumpHasStream(TokenStream));
    429 }
    430 
    431 }  // namespace
    432 
    433 int main(int argc, char** argv) {
    434   testing::InitGoogleTest(&argc, argv);
    435   CommandLine::Init(argc, argv);
    436 
    437   logging::LoggingSettings settings;
    438   settings.logging_dest = logging::LOG_TO_ALL;
    439   settings.log_file = L"CON";
    440   settings.lock_log = logging::DONT_LOCK_LOG_FILE;
    441   logging::InitLogging(settings);
    442   return RUN_ALL_TESTS();
    443 }
    444