Coverage for /Syzygy/pe/decomposer.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
80.1%85310650.C++source

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/pe/decomposer.h"
  16    :  
  17    :  #include "pcrecpp.h"  // NOLINT
  18    :  #include "base/bind.h"
  19    :  #include "base/strings/string_split.h"
  20    :  #include "base/strings/stringprintf.h"
  21    :  #include "base/strings/utf_string_conversions.h"
  22    :  #include "base/win/scoped_bstr.h"
  23    :  #include "base/win/scoped_comptr.h"
  24    :  #include "syzygy/core/zstream.h"
  25    :  #include "syzygy/pdb/omap.h"
  26    :  #include "syzygy/pdb/pdb_byte_stream.h"
  27    :  #include "syzygy/pdb/pdb_constants.h"
  28    :  #include "syzygy/pdb/pdb_dbi_stream.h"
  29    :  #include "syzygy/pdb/pdb_file.h"
  30    :  #include "syzygy/pdb/pdb_reader.h"
  31    :  #include "syzygy/pdb/pdb_symbol_record.h"
  32    :  #include "syzygy/pdb/pdb_util.h"
  33    :  #include "syzygy/pe/dia_util.h"
  34    :  #include "syzygy/pe/find.h"
  35    :  #include "syzygy/pe/pe_file_parser.h"
  36    :  #include "syzygy/pe/pe_utils.h"
  37    :  #include "syzygy/pe/serialization.h"
  38    :  #include "third_party/cci/Files/CvInfo.h"
  39    :  
  40    :  namespace cci = Microsoft_Cci_Pdb;
  41    :  
  42    :  namespace {
  43    :  
  44    :  using block_graph::BlockGraph;
  45    :  using block_graph::BlockInfo;
  46    :  using core::AbsoluteAddress;
  47    :  using core::FileOffsetAddress;
  48    :  using core::RelativeAddress;
  49    :  
  50    :  typedef BlockGraph::Block Block;
  51    :  
  52    :  }  // namespace
  53    :  
  54    :  namespace pe {
  55    :  
  56    :  // An intermediate reference representation used while parsing PE blocks.
  57    :  // This is necessary because at that point we haven't yet chunked the whole
  58    :  // image into blocks thus some references cannot be resolved.
  59    :  struct Decomposer::IntermediateReference {
  60    :    RelativeAddress src_addr;
  61    :    BlockGraph::ReferenceType type;
  62    :    BlockGraph::Size size;
  63    :    RelativeAddress dst_addr;
  64    :  };
  65    :  
  66    :  namespace {
  67    :  
  68    :  using base::win::ScopedBstr;
  69    :  using base::win::ScopedComPtr;
  70    :  using builder::Callback;
  71    :  using builder::Opt;
  72    :  using builder::Or;
  73    :  using builder::Seq;
  74    :  using builder::Star;
  75    :  
  76    :  typedef BlockGraph::BlockType BlockType;
  77    :  typedef BlockGraph::Offset Offset;
  78    :  typedef BlockGraph::Reference Reference;
  79    :  typedef BlockGraph::ReferenceType ReferenceType;
  80    :  typedef core::AddressRange<RelativeAddress, size_t> RelativeRange;
  81    :  typedef Decomposer::IntermediateReference IntermediateReference;
  82    :  typedef Decomposer::IntermediateReferences IntermediateReferences;
  83    :  typedef pcrecpp::RE RE;
  84    :  typedef std::vector<OMAP> OMAPs;
  85    :  typedef std::vector<pdb::PdbFixup> PdbFixups;
  86    :  
  87    :  const char kJumpTable[] = "<jump-table>";
  88    :  const char kCaseTable[] = "<case-table>";
  89    :  
  90    :  // The MS linker pads between code blocks with int3s.
  91    :  static const uint8 kInt3 = 0xCC;
  92    :  static const size_t kPointerSize = BlockGraph::Reference::kMaximumSize;
  93    :  
  94    :  // Some helper functions for testing ranges.
  95    :  template<typename T1, typename T2, typename T3>
  96  E :  bool InRange(T1 value, T2 lower_bound_incl, T3 length_excl) {
  97  E :    T1 upper_bound_excl = static_cast<T1>(lower_bound_incl) + length_excl;
  98    :    return static_cast<T1>(lower_bound_incl) <= value &&
  99  E :        value < static_cast<T2>(upper_bound_excl);
 100  E :  }
 101    :  template<typename T1, typename T2, typename T3>
 102  E :  bool InRangeIncl(T1 value, T2 lower_bound_incl, T3 length_incl) {
 103  E :    T1 upper_bound_incl = static_cast<T1>(lower_bound_incl) + length_incl;
 104    :    return static_cast<T1>(lower_bound_incl) <= value &&
 105  E :        value <= upper_bound_incl;
 106  E :  }
 107    :  
 108    :  bool InitializeDia(const PEFile& image_file,
 109    :                     const base::FilePath& pdb_path,
 110    :                     IDiaDataSource** dia_source,
 111    :                     IDiaSession** dia_session,
 112  E :                     IDiaSymbol** global) {
 113  E :    DCHECK_EQ(reinterpret_cast<IDiaDataSource*>(NULL), *dia_source);
 114  E :    DCHECK_EQ(reinterpret_cast<IDiaSession*>(NULL), *dia_session);
 115  E :    DCHECK_EQ(reinterpret_cast<IDiaSymbol*>(NULL), *global);
 116    :  
 117  E :    if (!CreateDiaSource(dia_source))
 118  i :      return false;
 119  E :    DCHECK_NE(reinterpret_cast<IDiaDataSource*>(NULL), *dia_source);
 120    :  
 121    :    // We create the session using the PDB file directly, as we've already
 122    :    // validated that it matches the module.
 123  E :    if (!CreateDiaSession(pdb_path, *dia_source, dia_session))
 124  i :      return false;
 125  E :    DCHECK_NE(reinterpret_cast<IDiaSession*>(NULL), *dia_session);
 126    :  
 127  E :    HRESULT hr = (*dia_session)->get_globalScope(global);
 128  E :    if (hr != S_OK) {
 129  i :      LOG(ERROR) << "Failed to get the DIA global scope: "
 130    :                 << common::LogHr(hr) << ".";
 131  i :      return false;
 132    :    }
 133    :  
 134  E :    return true;
 135  E :  }
 136    :  
 137    :  // Given a compiland, returns its compiland details.
 138    :  bool GetCompilandDetailsForCompiland(IDiaSymbol* compiland,
 139  E :                                       IDiaSymbol** compiland_details) {
 140  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol*>(NULL), compiland);
 141  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol**>(NULL), compiland_details);
 142  E :    DCHECK(IsSymTag(compiland, SymTagCompiland));
 143  E :    DCHECK_EQ(reinterpret_cast<IDiaSymbol*>(NULL), *compiland_details);
 144    :  
 145    :    // Get the enumeration of compiland details.
 146  E :    ScopedComPtr<IDiaEnumSymbols> enum_symbols;
 147    :    HRESULT hr = compiland->findChildren(SymTagCompilandDetails, NULL, 0,
 148  E :                                         enum_symbols.Receive());
 149  E :    DCHECK_EQ(S_OK, hr);
 150    :  
 151    :    // We expect there to be compiland details. For compilands built by
 152    :    // non-standard toolchains, there usually aren't any.
 153  E :    LONG count = 0;
 154  E :    hr = enum_symbols->get_Count(&count);
 155  E :    DCHECK_EQ(S_OK, hr);
 156  E :    if (count == 0) {
 157    :      // We don't log here because we see this quite often.
 158  i :      return false;
 159    :    }
 160    :  
 161    :    // We do sometimes encounter more than one compiland detail. In fact, for
 162    :    // import and export tables we get one compiland detail per table entry.
 163    :    // They are all marked as having been generated by the linker, so using the
 164    :    // first one is sufficient.
 165    :  
 166    :    // Get the compiland details.
 167  E :    ULONG fetched = 0;
 168  E :    hr = enum_symbols->Next(1, compiland_details, &fetched);
 169  E :    DCHECK_EQ(S_OK, hr);
 170  E :    DCHECK_EQ(1u, fetched);
 171    :  
 172  E :    return true;
 173  E :  }
 174    :  
 175    :  // Stores information regarding known compilers.
 176    :  struct KnownCompilerInfo {
 177    :    wchar_t* compiler_name;
 178    :    bool supported;
 179    :  };
 180    :  
 181    :  // A list of known compilers, and their status as being supported or not.
 182    :  KnownCompilerInfo kKnownCompilerInfos[] = {
 183    :    { L"Microsoft (R) Macro Assembler", false },
 184    :    { L"Microsoft (R) Optimizing Compiler", true },
 185    :    { L"Microsoft (R) LINK", false }
 186    :  };
 187    :  
 188    :  // Given a compiland, determines whether the compiler used is one of those that
 189    :  // we whitelist.
 190  E :  bool IsBuiltBySupportedCompiler(IDiaSymbol* compiland) {
 191  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol*>(NULL), compiland);
 192  E :    DCHECK(IsSymTag(compiland, SymTagCompiland));
 193    :  
 194  E :    ScopedComPtr<IDiaSymbol> compiland_details;
 195    :    if (!GetCompilandDetailsForCompiland(compiland,
 196  E :                                         compiland_details.Receive())) {
 197    :      // If the compiland has no compiland details we assume the compiler is not
 198    :      // supported.
 199  i :      ScopedBstr compiland_name;
 200  i :      if (compiland->get_name(compiland_name.Receive()) == S_OK) {
 201  i :        VLOG(1) << "Compiland has no compiland details: "
 202    :                << common::ToString(compiland_name);
 203    :      }
 204  i :      return false;
 205    :    }
 206  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol*>(NULL), compiland_details.get());
 207    :  
 208    :    // Get the compiler name.
 209  E :    ScopedBstr compiler_name;
 210  E :    HRESULT hr = compiland_details->get_compilerName(compiler_name.Receive());
 211  E :    DCHECK_EQ(S_OK, hr);
 212    :  
 213    :    // Check the compiler name against the list of known compilers.
 214  E :    for (size_t i = 0; i < arraysize(kKnownCompilerInfos); ++i) {
 215  E :      if (::wcscmp(kKnownCompilerInfos[i].compiler_name, compiler_name) == 0) {
 216  E :        return kKnownCompilerInfos[i].supported;
 217    :      }
 218  E :    }
 219    :  
 220    :    // Anything we don't explicitly know about is not supported.
 221  E :    VLOG(1) << "Encountered unknown compiler: " << compiler_name;
 222  E :    return false;
 223  E :  }
 224    :  
 225    :  // Adds an intermediate reference to the provided vector. The vector is
 226    :  // specified as the first parameter (in slight violation of our coding
 227    :  // standards) because this function is intended to be used by Bind.
 228    :  bool AddIntermediateReference(IntermediateReferences* references,
 229    :                                RelativeAddress src_addr,
 230    :                                ReferenceType type,
 231    :                                BlockGraph::Size size,
 232  E :                                RelativeAddress dst_addr) {
 233  E :    DCHECK_NE(reinterpret_cast<IntermediateReferences*>(NULL), references);
 234  E :    IntermediateReference ref = { src_addr, type, size, dst_addr };
 235  E :    references->push_back(ref);
 236  E :    return true;
 237  E :  }
 238    :  
 239    :  // Create a reference as specified. Ignores existing references if they are of
 240    :  // the exact same type.
 241    :  bool CreateReference(RelativeAddress src_addr,
 242    :                       BlockGraph::Size ref_size,
 243    :                       ReferenceType ref_type,
 244    :                       RelativeAddress base_addr,
 245    :                       RelativeAddress dst_addr,
 246  E :                       BlockGraph::AddressSpace* image) {
 247  E :    DCHECK_NE(reinterpret_cast<BlockGraph::AddressSpace*>(NULL), image);
 248    :  
 249    :    // Get the source block and offset, and ensure that the reference fits
 250    :    // within it.
 251  E :    Block* src_block = image->GetBlockByAddress(src_addr);
 252  E :    if (src_block == NULL) {
 253  i :      LOG(ERROR) << "Unable to find block for reference originating at "
 254    :                 << src_addr << ".";
 255  i :      return false;
 256    :    }
 257  E :    RelativeAddress src_block_addr;
 258  E :    CHECK(image->GetAddressOf(src_block, &src_block_addr));
 259  E :    Offset src_block_offset = src_addr - src_block_addr;
 260  E :    if (src_block_offset + ref_size > src_block->size()) {
 261  i :      LOG(ERROR) << "Reference originating at " << src_addr
 262    :                 << " extends beyond block \"" << src_block->name() << "\".";
 263  i :      return false;
 264    :    }
 265    :  
 266    :    // Get the destination block and offset.
 267  E :    Block* dst_block = image->GetBlockByAddress(base_addr);
 268  E :    if (dst_block == NULL) {
 269  i :      LOG(ERROR) << "Unable to find block for reference pointing at "
 270    :                 << base_addr << ".";
 271  i :      return false;
 272    :    }
 273  E :    RelativeAddress dst_block_addr;
 274  E :    CHECK(image->GetAddressOf(dst_block, &dst_block_addr));
 275  E :    Offset base = base_addr - dst_block_addr;
 276  E :    Offset offset = dst_addr - dst_block_addr;
 277    :  
 278  E :    Reference ref(ref_type, ref_size, dst_block, offset, base);
 279    :  
 280    :    // Check if a reference already exists at this offset.
 281    :    Block::ReferenceMap::const_iterator ref_it =
 282  E :        src_block->references().find(src_block_offset);
 283  E :    if (ref_it != src_block->references().end()) {
 284    :      // If an identical reference already exists then we're done.
 285  E :      if (ref == ref_it->second)
 286  E :        return true;
 287  i :      LOG(ERROR) << "Block \"" << src_block->name() << "\" has a conflicting "
 288    :                 << "reference at offset " << src_block_offset << ".";
 289  i :      return false;
 290    :    }
 291    :  
 292  E :    CHECK(src_block->SetReference(src_block_offset, ref));
 293    :  
 294  E :    return true;
 295  E :  }
 296    :  
 297    :  // Loads FIXUP and OMAP_FROM debug streams.
 298    :  bool LoadDebugStreams(IDiaSession* dia_session,
 299    :                        PdbFixups* pdb_fixups,
 300  E :                        OMAPs* omap_from) {
 301  E :    DCHECK_NE(reinterpret_cast<IDiaSession*>(NULL), dia_session);
 302  E :    DCHECK_NE(reinterpret_cast<PdbFixups*>(NULL), pdb_fixups);
 303  E :    DCHECK_NE(reinterpret_cast<OMAPs*>(NULL), omap_from);
 304    :  
 305    :    // Load the fixups. These must exist.
 306    :    SearchResult search_result = FindAndLoadDiaDebugStreamByName(
 307  E :        kFixupDiaDebugStreamName, dia_session, pdb_fixups);
 308  E :    if (search_result != kSearchSucceeded) {
 309  i :      if (search_result == kSearchFailed) {
 310  i :        LOG(ERROR) << "PDB file does not contain a FIXUP stream. Module must be "
 311    :                      "linked with '/PROFILE' or '/DEBUGINFO:FIXUP' flag.";
 312    :      }
 313  i :      return false;
 314    :    }
 315    :  
 316    :    // Load the omap_from table. It is not necessary that one exist.
 317    :    search_result = FindAndLoadDiaDebugStreamByName(
 318  E :        kOmapFromDiaDebugStreamName, dia_session, omap_from);
 319  E :    if (search_result == kSearchErrored) {
 320  i :      LOG(ERROR) << "Error trying to read " << kOmapFromDiaDebugStreamName
 321    :                 << " stream.";
 322  i :      return false;
 323    :    }
 324    :  
 325  E :    return true;
 326  E :  }
 327    :  
 328    :  bool GetFixupDestinationAndType(const PEFile& image_file,
 329    :                                  const pdb::PdbFixup& fixup,
 330    :                                  RelativeAddress* dst_addr,
 331  E :                                  ReferenceType* ref_type) {
 332  E :    DCHECK_NE(reinterpret_cast<RelativeAddress*>(NULL), dst_addr);
 333  E :    DCHECK_NE(reinterpret_cast<ReferenceType*>(NULL), ref_type);
 334    :  
 335  E :    RelativeAddress src_addr(fixup.rva_location);
 336    :  
 337    :    // Get the destination displacement from the actual image itself. We only see
 338    :    // fixups for 32-bit references.
 339  E :    uint32 data = 0;
 340  E :    if (!image_file.ReadImage(src_addr, &data, sizeof(data))) {
 341  i :      LOG(ERROR) << "Unable to read image data for fixup with source address "
 342    :                 << "at" << src_addr << ".";
 343  i :      return false;
 344    :    }
 345    :  
 346    :    // Translate this to a relative displacement value.
 347  E :    switch (fixup.type) {
 348    :      case pdb::PdbFixup::TYPE_ABSOLUTE: {
 349  E :        *ref_type = BlockGraph::ABSOLUTE_REF;
 350  E :        *dst_addr = RelativeAddress(image_file.AbsToRelDisplacement(data));
 351  E :        break;
 352    :      }
 353    :  
 354    :      case pdb::PdbFixup::TYPE_PC_RELATIVE: {
 355  E :        *ref_type = BlockGraph::PC_RELATIVE_REF;
 356  E :        *dst_addr = RelativeAddress(fixup.rva_location) + sizeof(data) + data;
 357  E :        break;
 358    :      }
 359    :  
 360    :      case pdb::PdbFixup::TYPE_RELATIVE: {
 361  E :        *ref_type = BlockGraph::RELATIVE_REF;
 362  E :        *dst_addr = RelativeAddress(data);
 363  E :        break;
 364    :      }
 365    :  
 366    :      default: {
 367  i :        LOG(ERROR) << "Unexpected fixup type (" << fixup.type << ").";
 368  i :        return false;
 369    :      }
 370    :    }
 371    :  
 372  E :    return true;
 373  E :  }
 374    :  
 375    :  // Creates references from the @p pdb_fixups (translating them via the
 376    :  // provided @p omap_from information if it is not empty), all while removing the
 377    :  // corresponding entries from @p reloc_set. If @p reloc_set is not empty after
 378    :  // this then the PDB fixups are out of sync with the image and we are unable to
 379    :  // safely decompose.
 380    :  //
 381    :  // @note This function deliberately ignores fixup information for the resource
 382    :  //     section. This is because chrome.dll gets modified by a manifest tool
 383    :  //     which doesn't update the FIXUPs in the corresponding PDB. They are thus
 384    :  //     out of sync. Even if they were in sync this doesn't harm us as we have no
 385    :  //     need to reach in and modify resource data.
 386    :  bool CreateReferencesFromFixupsImpl(
 387    :      const PEFile& image_file,
 388    :      const PdbFixups& pdb_fixups,
 389    :      const OMAPs& omap_from,
 390    :      PEFile::RelocSet* reloc_set,
 391  E :      BlockGraph::AddressSpace* image) {
 392  E :    DCHECK_NE(reinterpret_cast<PEFile::RelocSet*>(NULL), reloc_set);
 393  E :    DCHECK_NE(reinterpret_cast<BlockGraph::AddressSpace*>(NULL), image);
 394    :  
 395  E :    bool have_omap = !omap_from.empty();
 396  E :    size_t fixups_used = 0;
 397    :  
 398    :    // The resource section in Chrome is modified post-link by a tool that adds a
 399    :    // manifest to it. This causes all of the fixups in the resource section (and
 400    :    // anything beyond it) to be invalid. As long as the resource section is the
 401    :    // last section in the image, this is not a problem (we can safely ignore the
 402    :    // .rsrc fixups, which we know how to parse without them). However, if there
 403    :    // is a section after the resource section, things will have been shifted
 404    :    // and potentially crucial fixups will be invalid.
 405    :    const IMAGE_SECTION_HEADER* rsrc_header = image_file.GetSectionHeader(
 406  E :        kResourceSectionName);
 407  E :    RelativeAddress rsrc_start(0xffffffff);
 408  E :    RelativeAddress rsrc_end(0xffffffff);
 409  E :    if (rsrc_header != NULL) {
 410  E :      rsrc_start = RelativeAddress(rsrc_header->VirtualAddress);
 411  E :      rsrc_end = rsrc_start + rsrc_header->Misc.VirtualSize;
 412    :    }
 413    :  
 414    :    // Ensure the fixups are all valid.
 415  E :    for (size_t i = 0; i < pdb_fixups.size(); ++i) {
 416  E :      if (!pdb_fixups[i].ValidHeader()) {
 417  i :        LOG(ERROR) << "Unknown fixup header: "
 418    :                   << base::StringPrintf("0x%08X.", pdb_fixups[i].header);
 419  i :        return false;
 420    :      }
 421    :  
 422    :      // For now, we skip any offset fixups. We've only seen this in the context
 423    :      // of TLS data access, and we don't mess with TLS structures.
 424  E :      if (pdb_fixups[i].is_offset())
 425  E :        continue;
 426    :  
 427    :      // All fixups we handle should be full size pointers.
 428  E :      DCHECK_EQ(Reference::kMaximumSize, pdb_fixups[i].size());
 429    :  
 430    :      // Get the original addresses, and map them through OMAP information.
 431    :      // Normally DIA takes care of this for us, but there is no API for
 432    :      // getting DIA to give us FIXUP information, so we have to do it manually.
 433  E :      RelativeAddress src_addr(pdb_fixups[i].rva_location);
 434  E :      RelativeAddress base_addr(pdb_fixups[i].rva_base);
 435  E :      if (have_omap) {
 436  i :        src_addr = pdb::TranslateAddressViaOmap(omap_from, src_addr);
 437  i :        base_addr = pdb::TranslateAddressViaOmap(omap_from, base_addr);
 438    :      }
 439    :  
 440    :      // If the reference originates beyond the .rsrc section then we can't
 441    :      // trust it.
 442  E :      if (src_addr >= rsrc_end) {
 443  i :        LOG(ERROR) << "Found fixup originating beyond .rsrc section.";
 444  i :        return false;
 445    :      }
 446    :  
 447    :      // If the reference originates from a part of the .rsrc section, ignore it.
 448  E :      if (src_addr >= rsrc_start)
 449  E :        continue;
 450    :  
 451    :      // Get the relative address/displacement of the fixup. This logs on failure.
 452  E :      RelativeAddress dst_addr;
 453  E :      ReferenceType type = BlockGraph::RELATIVE_REF;
 454    :      if (!GetFixupDestinationAndType(image_file, pdb_fixups[i], &dst_addr,
 455  E :                                      &type)) {
 456  i :        return false;
 457    :      }
 458    :  
 459    :      // Finally, create the reference. This logs verbosely for us on failure.
 460    :      if (!CreateReference(src_addr, Reference::kMaximumSize, type, base_addr,
 461  E :                           dst_addr, image)) {
 462  i :        return false;
 463    :      }
 464    :  
 465    :      // Remove this reference from the relocs.
 466  E :      PEFile::RelocSet::iterator reloc_it = reloc_set->find(src_addr);
 467  E :      if (reloc_it != reloc_set->end()) {
 468    :        // We should only find a reloc if the fixup was of absolute type.
 469  E :        if (type != BlockGraph::ABSOLUTE_REF) {
 470  i :          LOG(ERROR) << "Found a reloc corresponding to a non-absolute fixup.";
 471  i :          return false;
 472    :        }
 473    :  
 474  E :        reloc_set->erase(reloc_it);
 475    :      }
 476    :  
 477  E :      ++fixups_used;
 478  E :    }
 479    :  
 480  E :    return true;
 481  E :  }
 482    :  
 483  E :  bool GetDataSymbolSize(IDiaSymbol* symbol, size_t* length) {
 484  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol*>(NULL), symbol);
 485  E :    DCHECK_NE(reinterpret_cast<size_t*>(NULL), length);
 486    :  
 487  E :    *length = 0;
 488  E :    ScopedComPtr<IDiaSymbol> type;
 489  E :    HRESULT hr = symbol->get_type(type.Receive());
 490    :    // This happens if the symbol has no type information.
 491  E :    if (hr == S_FALSE)
 492  E :      return true;
 493  E :    if (hr != S_OK) {
 494  i :      LOG(ERROR) << "Failed to get type symbol: " << common::LogHr(hr) << ".";
 495  i :      return false;
 496    :    }
 497    :  
 498  E :    ULONGLONG ull_length = 0;
 499  E :    hr = type->get_length(&ull_length);
 500  E :    if (hr != S_OK) {
 501  i :      LOG(ERROR) << "Failed to retrieve type length properties: "
 502    :                 << common::LogHr(hr) << ".";
 503  i :      return false;
 504    :    }
 505  E :    DCHECK_LE(ull_length, 0xFFFFFFFF);
 506  E :    *length = static_cast<size_t>(ull_length);
 507    :  
 508  E :    return true;
 509  E :  }
 510    :  
 511    :  bool ScopeSymTagToLabelProperties(enum SymTagEnum sym_tag,
 512    :                                    size_t scope_count,
 513    :                                    BlockGraph::LabelAttributes* attr,
 514  E :                                    std::string* name) {
 515  E :    DCHECK_NE(reinterpret_cast<BlockGraph::LabelAttributes*>(NULL), attr);
 516  E :    DCHECK_NE(reinterpret_cast<std::string*>(NULL), name);
 517    :  
 518  E :    switch (sym_tag) {
 519    :      case SymTagFuncDebugStart: {
 520  E :        *attr = BlockGraph::DEBUG_START_LABEL;
 521  E :        *name = "<debug-start>";
 522  E :        return true;
 523    :      }
 524    :      case SymTagFuncDebugEnd: {
 525  E :        *attr = BlockGraph::DEBUG_END_LABEL;
 526  E :        *name = "<debug-end>";
 527  E :        return true;
 528    :      }
 529    :      case SymTagBlock: {
 530  E :        *attr = BlockGraph::SCOPE_START_LABEL;
 531  E :        *name = base::StringPrintf("<scope-start-%d>", scope_count);
 532  E :        return true;
 533    :      }
 534    :      default:
 535  i :        return false;
 536    :    }
 537  i :    return false;
 538  E :  }
 539    :  
 540    :  // Reads the linker module symbol stream from the given PDB file. This should
 541    :  // always exist as the last module.
 542    :  scoped_refptr<pdb::PdbStream> GetLinkerSymbolStream(
 543  E :      const pdb::PdbFile& pdb_file) {
 544    :    static const char kLinkerModuleName[] = "* Linker *";
 545    :  
 546    :    // Get the DBI stream.
 547    :    scoped_refptr<pdb::PdbStream> stream =
 548  E :        pdb_file.GetStream(pdb::kDbiStream);
 549  E :    if (stream.get() == NULL) {
 550  i :      LOG(ERROR) << "PDB does not contain a DBI stream.";
 551  i :      return false;
 552    :    }
 553    :  
 554    :    // Read the entire thing into memory before parsing it. This makes parsing
 555    :    // much faster.
 556  E :    scoped_refptr<pdb::PdbByteStream> dbi_stream(new pdb::PdbByteStream());
 557  E :    if (!dbi_stream->Init(stream.get())) {
 558  i :      LOG(ERROR) << "Failed to read DBI stream.";
 559    :    }
 560    :  
 561    :    // Parse the DBI stream.
 562  E :    pdb::DbiStream dbi;
 563  E :    if (!dbi.Read(dbi_stream.get())) {
 564  i :      LOG(ERROR) << "Unable to parse DBI stream.";
 565  i :      return false;
 566    :    }
 567    :  
 568  E :    if (dbi.modules().empty()) {
 569  i :      LOG(ERROR) << "DBI stream contains no modules.";
 570  i :      return false;
 571    :    }
 572    :  
 573    :    // The last module has always been observed to be the linker module.
 574  E :    const pdb::DbiModuleInfo& linker = dbi.modules().back();
 575  E :    if (linker.module_name() != kLinkerModuleName) {
 576  i :      LOG(ERROR) << "Last module is not the linker module.";
 577  i :      return false;
 578    :    }
 579    :  
 580    :    // Get the symbol stream.
 581  E :    stream = pdb_file.GetStream(linker.module_info_base().stream);
 582  E :    if (stream.get() == NULL) {
 583  i :      LOG(ERROR) << "Unable to open linker symbol stream.";
 584  i :      return false;
 585    :    }
 586    :  
 587    :    // Also read it entirely into memory for faster parsing.
 588  E :    scoped_refptr<pdb::PdbByteStream> symbols(new pdb::PdbByteStream());
 589  E :    if (!symbols->Init(stream.get())) {
 590  i :      LOG(ERROR) << "Failed to read linker symbol stream.";
 591    :    }
 592    :  
 593  E :    return symbols;
 594  E :  }
 595    :  
 596    :  // Parses a symbol from a PDB symbol stream. The @p buffer is populated with the
 597    :  // data and upon success this returns the symbol directly cast onto the
 598    :  // @p buffer data. On failure this returns NULL.
 599    :  template<typename SymbolType>
 600    :  const SymbolType* ParseSymbol(uint16 symbol_length,
 601    :                                pdb::PdbStream* stream,
 602  E :                                std::vector<uint8>* buffer) {
 603  E :    DCHECK_NE(reinterpret_cast<pdb::PdbStream*>(NULL), stream);
 604  E :    DCHECK_NE(reinterpret_cast<std::vector<uint8>*>(NULL), buffer);
 605    :  
 606  E :    buffer->clear();
 607    :  
 608  E :    if (symbol_length < sizeof(SymbolType)) {
 609  i :      LOG(ERROR) << "Symbol too small for casting.";
 610  i :      return NULL;
 611    :    }
 612    :  
 613  E :    if (!stream->Read(buffer, symbol_length)) {
 614  i :      LOG(ERROR) << "Failed to read symbol.";
 615  i :      return NULL;
 616    :    }
 617    :  
 618  E :    return reinterpret_cast<const SymbolType*>(buffer->data());
 619  E :  }
 620    :  
 621    :  // If the given run of bytes consists of a single value repeated, returns that
 622    :  // value. Otherwise, returns -1.
 623  E :  int RepeatedValue(const uint8* data, size_t size) {
 624  E :    DCHECK_NE(reinterpret_cast<uint8*>(NULL), data);
 625  E :    const uint8* data_end = data + size;
 626  E :    uint8 value = *(data++);
 627  E :    for (; data < data_end; ++data) {
 628  E :      if (*data != value)
 629  i :        return -1;
 630  E :    }
 631  E :    return value;
 632  E :  }
 633    :  
 634    :  // Searches through the given image layout graph, and labels blocks that are
 635    :  // simply padding blocks.
 636  E :  bool FindPaddingBlocks(ImageLayout* image_layout) {
 637  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 638    :  
 639  E :    BlockGraph* block_graph = image_layout->blocks.graph();
 640  E :    DCHECK_NE(reinterpret_cast<BlockGraph*>(NULL), block_graph);
 641    :  
 642    :    BlockGraph::BlockMap::iterator block_it =
 643  E :        block_graph->blocks_mutable().begin();
 644  E :    for (; block_it != block_graph->blocks_mutable().end(); ++block_it) {
 645  E :      Block& block = block_it->second;
 646    :  
 647    :      // Padding blocks must not have any symbol information: no labels,
 648    :      // no references, no referrers, and they must be a gap block.
 649    :      if (block.labels().size() != 0 ||
 650    :          block.references().size() != 0 ||
 651    :          block.referrers().size() != 0 ||
 652  E :          (block.attributes() & BlockGraph::GAP_BLOCK) == 0) {
 653  E :        continue;
 654    :      }
 655    :  
 656  E :      switch (block.type()) {
 657    :        // Code blocks should be fully defined and consist of only int3s.
 658    :        case BlockGraph::CODE_BLOCK: {
 659    :          if (block.data_size() != block.size() ||
 660  E :              RepeatedValue(block.data(), block.data_size()) != kInt3)
 661  i :            continue;
 662  E :          break;
 663    :        }
 664    :  
 665    :        // Data blocks should be uninitialized or have fully defined data
 666    :        // consisting only of zeros.
 667    :        default: {
 668  E :          DCHECK_EQ(BlockGraph::DATA_BLOCK, block.type());
 669  E :          if (block.data_size() == 0)  // Uninitialized data blocks are padding.
 670  E :            break;
 671    :          if (block.data_size() != block.size() ||
 672  E :              RepeatedValue(block.data(), block.data_size()) != 0)
 673  i :            continue;
 674    :        }
 675    :      }
 676    :  
 677    :      // If we fall through to this point, then the block is a padding block.
 678  E :      block.set_attribute(BlockGraph::PADDING_BLOCK);
 679  E :    }
 680    :  
 681  E :    return true;
 682  E :  }
 683    :  
 684  E :  bool CodeBlockHasAlignedJumpTables(const Block* block) {
 685  E :    DCHECK_NE(reinterpret_cast<Block*>(NULL), block);
 686  E :    DCHECK_EQ(BlockGraph::CODE_BLOCK, block->type());
 687    :  
 688    :    // Iterate over the labels of this block looking for jump tables.
 689  E :    bool has_jump_tables = false;
 690    :    Block::LabelMap::const_iterator label_it =
 691  E :        block->labels().begin();
 692  E :    for (; label_it != block->labels().end(); ++label_it) {
 693  E :      if (!label_it->second.has_attributes(BlockGraph::JUMP_TABLE_LABEL))
 694  E :        continue;
 695    :  
 696  E :      has_jump_tables = true;
 697    :  
 698    :      // If the jump table is misaligned we can return false immediately.
 699  E :      if (label_it->first % kPointerSize != 0)
 700  i :        return false;
 701  E :    }
 702    :  
 703  E :    return has_jump_tables;
 704  E :  }
 705    :  
 706  E :  bool AlignCodeBlocks(ImageLayout* image_layout) {
 707  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 708    :  
 709    :    BlockGraph::AddressSpace::RangeMapConstIter block_it =
 710  E :        image_layout->blocks.begin();
 711  E :    for (; block_it != image_layout->blocks.end(); ++block_it) {
 712  E :      Block* block = block_it->second;
 713  E :      if (block->type() != BlockGraph::CODE_BLOCK)
 714  E :        continue;
 715    :  
 716    :      // Preserve alignment for anything built by an unknown compiler. There may
 717    :      // be inline data that has alignment requirements we don't know about. SSE
 718    :      // and AVX instructions have 8 and 16 byte alignments, so we preserve
 719    :      // these. It is not possible for a function to contain both instructions
 720    :      // and data with an alignment constraint unless the size of the block is at
 721    :      // least twice the alignment; this is used as a simple filter to avoid
 722    :      // adding alignment where unnecessary.
 723  E :      if (block->attributes() & BlockGraph::BUILT_BY_UNSUPPORTED_COMPILER) {
 724  E :        uint32 align = std::min(16u, block_it->first.start().GetAlignment());
 725  E :        if (align >= 8 && block->size() >= 2 * align) {
 726  E :          VLOG(1) << "Preserving alignment of " << BlockInfo(block) << " as "
 727    :                  << align << ".";
 728  E :          block->set_alignment(align);
 729  E :          continue;
 730    :        }
 731    :      }
 732    :  
 733    :      // We only care about code blocks that are already 4-byte aligned but
 734    :      // whose explicit alignment is currently less than that.
 735  E :      if (block->alignment() >= kPointerSize)
 736  i :        continue;
 737  E :      if (block_it->first.start().value() % kPointerSize != 0)
 738  E :        continue;
 739    :  
 740    :      // Inspect them to see if they have aligned jump tables. If they do,
 741    :      // set the alignment of the block itself.
 742  E :      if (CodeBlockHasAlignedJumpTables(block_it->second))
 743  E :        block->set_alignment(kPointerSize);
 744  E :    }
 745    :  
 746  E :    return true;
 747  E :  }
 748    :  
 749    :  void GuessDataBlockAlignment(uint32 max_alignment,
 750    :                               RelativeAddress block_rva,
 751  E :                               Block* block) {
 752  E :    DCHECK_NE(static_cast<Block*>(NULL), block);
 753  E :    DCHECK_EQ(BlockGraph::DATA_BLOCK, block->type());
 754  E :    uint32 alignment = block_rva.GetAlignment();
 755    :    // Cap the alignment.
 756  E :    if (alignment > max_alignment)
 757  E :      alignment = max_alignment;
 758  E :    block->set_alignment(alignment);
 759  E :  }
 760    :  
 761    :  void GuessDataBlockAlignments(const PEFile& pe_file,
 762  E :                                ImageLayout* image_layout) {
 763  E :    DCHECK_NE(static_cast<ImageLayout*>(NULL), image_layout);
 764    :  
 765  E :    uint32 max_alignment = pe_file.nt_headers()->OptionalHeader.SectionAlignment;
 766    :  
 767  E :    BlockGraph::AddressSpace::RangeMapConstIter it = image_layout->blocks.begin();
 768  E :    for (; it != image_layout->blocks.end(); ++it) {
 769  E :      RelativeAddress block_rva = it->first.start();
 770  E :      BlockGraph::Block* block = it->second;
 771  E :      if (block->type() != BlockGraph::DATA_BLOCK)
 772  E :        continue;
 773  E :      GuessDataBlockAlignment(max_alignment, block_rva, block);
 774  E :    }
 775  E :  }
 776    :  
 777    :  }  // namespace
 778    :  
 779    :  // We use ", " as a separator between symbol names. We sometimes see commas
 780    :  // in symbol names but do not see whitespace. Thus, this provides a useful
 781    :  // separator that is also human friendly to read.
 782    :  const char Decomposer::kLabelNameSep[] = ", ";
 783    :  
 784    :  // This is by CreateBlocksFromCoffGroups to communicate shared state to
 785    :  // VisitLinkerSymbol via the VisitSymbols helper function.
 786    :  struct Decomposer::VisitLinkerSymbolContext {
 787    :    int current_group_index;
 788    :    std::string current_group_prefix;
 789    :    RelativeAddress current_group_start;
 790    :  
 791    :    // These are the set of patterns that indicate bracketing groups. They
 792    :    // should match both the opening and the closing symbol, and have at least
 793    :    // one match group returning the common prefix.
 794    :    std::vector<RE> bracketing_groups;
 795    :  
 796  E :    VisitLinkerSymbolContext() : current_group_index(-1) {
 797    :      // Matches groups like: .CRT$XCA -> .CRT$XCZ
 798  E :      bracketing_groups.push_back(RE("(\\.CRT\\$X.)[AZ]"));
 799    :      // Matches groups like: .rtc$IAA -> .rtc$IZZ
 800  E :      bracketing_groups.push_back(RE("(\\.rtc\\$.*)(AA|ZZ)"));
 801    :      // Matches exactly: ATL$__a -> ATL$__z
 802  E :      bracketing_groups.push_back(RE("(ATL\\$__)[az]"));
 803    :      // Matches exactly: .tls -> .tls$ZZZ
 804  E :      bracketing_groups.push_back(RE("(\\.tls)(\\$ZZZ)?"));
 805  E :    }
 806    :  
 807    :   private:
 808    :    DISALLOW_COPY_AND_ASSIGN(VisitLinkerSymbolContext);
 809    :  };
 810    :  
 811    :  Decomposer::Decomposer(const PEFile& image_file)
 812    :      : image_file_(image_file), image_layout_(NULL), image_(NULL),
 813  E :        current_block_(NULL), current_scope_count_(0) {
 814  E :  }
 815    :  
 816  E :  bool Decomposer::Decompose(ImageLayout* image_layout) {
 817  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 818    :  
 819    :    // The temporaries should be NULL.
 820  E :    DCHECK_EQ(reinterpret_cast<ImageLayout*>(NULL), image_layout_);
 821  E :    DCHECK_EQ(reinterpret_cast<BlockGraph::AddressSpace*>(NULL), image_);
 822    :  
 823    :    // Set the image format.
 824  E :    image_layout->blocks.graph()->set_image_format(BlockGraph::PE_IMAGE);
 825    :  
 826    :    // We start by finding the PDB path.
 827  E :    if (!FindAndValidatePdbPath())
 828  E :      return false;
 829  E :    DCHECK(!pdb_path_.empty());
 830    :  
 831    :    // Load the serialized block-graph from the PDB if it exists. This allows
 832    :    // round-trip decomposition.
 833  E :    bool stream_exists = false;
 834    :    if (LoadBlockGraphFromPdb(
 835  E :            pdb_path_, image_file_, image_layout, &stream_exists)) {
 836  E :      return true;
 837  E :    } else if (stream_exists) {
 838    :      // If the stream exists but hasn't been loaded we return an error. At this
 839    :      // point an error message has already been logged if there was one.
 840  i :      return false;
 841    :    }
 842    :  
 843    :    // At this point a full decomposition needs to be performed.
 844  E :    image_layout_ = image_layout;
 845  E :    image_ = &(image_layout->blocks);
 846  E :    bool success = DecomposeImpl();
 847  E :    image_layout_ = NULL;
 848  E :    image_ = NULL;
 849    :  
 850  E :    return success;
 851  E :  }
 852    :  
 853  E :  bool Decomposer::FindAndValidatePdbPath() {
 854    :    // Manually find the PDB path if it is not specified.
 855  E :    if (pdb_path_.empty()) {
 856    :      if (!FindPdbForModule(image_file_.path(), &pdb_path_) ||
 857  E :          pdb_path_.empty()) {
 858  i :        LOG(ERROR) << "Unable to find PDB file for module: "
 859    :                   << image_file_.path().value();
 860  i :        return false;
 861    :      }
 862    :    }
 863  E :    DCHECK(!pdb_path_.empty());
 864    :  
 865  E :    if (!base::PathExists(pdb_path_)) {
 866  E :      LOG(ERROR) << "Path not found: " << pdb_path_.value();
 867  E :      return false;
 868    :    }
 869    :  
 870  E :    if (!pe::PeAndPdbAreMatched(image_file_.path(), pdb_path_)) {
 871  i :      LOG(ERROR) << "PDB file \"" << pdb_path_.value() << "\" does not match "
 872    :                 << "module \"" << image_file_.path().value() << "\".";
 873  i :      return false;
 874    :    }
 875    :  
 876  E :    return true;
 877  E :  }
 878    :  
 879    :  bool Decomposer::LoadBlockGraphFromPdbStream(
 880    :      const PEFile& image_file,
 881    :      pdb::PdbStream* block_graph_stream,
 882  E :      ImageLayout* image_layout) {
 883  E :    DCHECK_NE(reinterpret_cast<pdb::PdbStream*>(NULL), block_graph_stream);
 884  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 885  E :    LOG(INFO) << "Reading block-graph and image layout from the PDB.";
 886    :  
 887    :    // Initialize an input archive pointing to the stream.
 888  E :    scoped_refptr<pdb::PdbByteStream> byte_stream = new pdb::PdbByteStream();
 889  E :    if (!byte_stream->Init(block_graph_stream))
 890  i :      return false;
 891  E :    DCHECK_NE(reinterpret_cast<pdb::PdbByteStream*>(NULL), byte_stream.get());
 892    :  
 893  E :    core::ScopedInStreamPtr pdb_in_stream;
 894    :    pdb_in_stream.reset(core::CreateByteInStream(
 895  E :        byte_stream->data(), byte_stream->data() + byte_stream->length()));
 896    :  
 897    :    // Read the header.
 898  E :    uint32 stream_version = 0;
 899  E :    unsigned char compressed = 0;
 900    :    if (!pdb_in_stream->Read(sizeof(stream_version),
 901    :                             reinterpret_cast<core::Byte*>(&stream_version)) ||
 902    :        !pdb_in_stream->Read(sizeof(compressed),
 903  E :                             reinterpret_cast<core::Byte*>(&compressed))) {
 904  i :      LOG(ERROR) << "Failed to read existing Syzygy block-graph stream header.";
 905  i :      return false;
 906    :    }
 907    :  
 908    :    // Check the stream version.
 909  E :    if (stream_version != pdb::kSyzygyBlockGraphStreamVersion) {
 910  E :      LOG(ERROR) << "PDB contains an unsupported Syzygy block-graph stream"
 911    :                 << " version (got " << stream_version << ", expected "
 912    :                 << pdb::kSyzygyBlockGraphStreamVersion << ").";
 913  E :      return false;
 914    :    }
 915    :  
 916    :    // If the stream is compressed insert the decompression filter.
 917  E :    core::InStream* in_stream = pdb_in_stream.get();
 918  E :    scoped_ptr<core::ZInStream> zip_in_stream;
 919  E :    if (compressed != 0) {
 920  E :      zip_in_stream.reset(new core::ZInStream(in_stream));
 921  E :      if (!zip_in_stream->Init()) {
 922  i :        LOG(ERROR) << "Unable to initialize ZInStream.";
 923  i :        return false;
 924    :      }
 925  E :      in_stream = zip_in_stream.get();
 926    :    }
 927    :  
 928    :    // Deserialize the image-layout.
 929  E :    core::NativeBinaryInArchive in_archive(in_stream);
 930  E :    block_graph::BlockGraphSerializer::Attributes attributes = 0;
 931    :    if (!LoadBlockGraphAndImageLayout(
 932  E :        image_file, &attributes, image_layout, &in_archive)) {
 933  i :      LOG(ERROR) << "Failed to deserialize block-graph and image layout.";
 934  i :      return false;
 935    :    }
 936    :  
 937  E :    return true;
 938  E :  }
 939    :  
 940    :  bool Decomposer::LoadBlockGraphFromPdb(const base::FilePath& pdb_path,
 941    :                                            const PEFile& image_file,
 942    :                                            ImageLayout* image_layout,
 943  E :                                            bool* stream_exists) {
 944  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 945  E :    DCHECK_NE(reinterpret_cast<bool*>(NULL), stream_exists);
 946    :  
 947  E :    pdb::PdbFile pdb_file;
 948  E :    pdb::PdbReader pdb_reader;
 949  E :    if (!pdb_reader.Read(pdb_path, &pdb_file)) {
 950  i :      LOG(ERROR) << "Unable to read the PDB named \"" << pdb_path.value()
 951    :                 << "\".";
 952  i :      return NULL;
 953    :    }
 954    :  
 955    :    // Try to get the block-graph stream from the PDB.
 956  E :    scoped_refptr<pdb::PdbStream> block_graph_stream;
 957    :    if (!pdb::LoadNamedStreamFromPdbFile(pdb::kSyzygyBlockGraphStreamName,
 958    :                                         &pdb_file,
 959    :                                         &block_graph_stream) ||
 960  E :        block_graph_stream.get() == NULL) {
 961  E :      *stream_exists = false;
 962  E :      return false;
 963    :    }
 964  E :    if (block_graph_stream->length() == 0) {
 965  i :      *stream_exists = false;
 966  i :      LOG(WARNING) << "The block-graph stream is empty, ignoring it.";
 967  i :      return false;
 968    :    }
 969    :  
 970    :    // The PDB contains a block-graph stream, the block-graph and the image layout
 971    :    // will be read from this stream.
 972  E :    *stream_exists = true;
 973    :    if (!LoadBlockGraphFromPdbStream(image_file, block_graph_stream.get(),
 974  E :                                     image_layout)) {
 975  i :      return false;
 976    :    }
 977    :  
 978  E :    return true;
 979  E :  }
 980    :  
 981  E :  bool Decomposer::DecomposeImpl() {
 982    :    // Instantiate and initialize our Debug Interface Access session. This logs
 983    :    // verbosely for us.
 984  E :    ScopedComPtr<IDiaDataSource> dia_source;
 985  E :    ScopedComPtr<IDiaSession> dia_session;
 986  E :    ScopedComPtr<IDiaSymbol> global;
 987    :    if (!InitializeDia(image_file_, pdb_path_, dia_source.Receive(),
 988  E :                       dia_session.Receive(), global.Receive())) {
 989  i :      return false;
 990    :    }
 991    :  
 992    :    // Copy the image headers to the layout.
 993    :    CopySectionHeadersToImageLayout(
 994    :        image_file_.nt_headers()->FileHeader.NumberOfSections,
 995    :        image_file_.section_headers(),
 996  E :        &(image_layout_->sections));
 997    :  
 998    :    // Create the sections in the underlying block-graph.
 999  E :    if (!CopySectionInfoToBlockGraph(image_file_, image_->graph()))
1000  i :      return false;
1001    :  
1002    :    // We scope the first few operations so that we don't keep the intermediate
1003    :    // references around any longer than we have to.
1004    :    {
1005  E :      IntermediateReferences references;
1006    :  
1007    :      // First we parse out the PE blocks.
1008  E :      VLOG(1) << "Parsing PE blocks.";
1009  E :      if (!CreatePEImageBlocksAndReferences(&references))
1010  i :        return false;
1011    :  
1012    :      // Now we parse the COFF group symbols from the linker's symbol stream.
1013    :      // These indicate things like static initializers, which must stay together
1014    :      // in a single block.
1015  E :      VLOG(1) << "Parsing COFF groups.";
1016  E :      if (!CreateBlocksFromCoffGroups())
1017  i :        return false;
1018    :  
1019    :      // Next we parse out section contributions. Some of these may coincide with
1020    :      // existing PE parsed blocks, but when they do we expect them to be exact
1021    :      // collisions.
1022  E :      VLOG(1) << "Parsing section contributions.";
1023  E :      if (!CreateBlocksFromSectionContribs(dia_session.get()))
1024  i :        return false;
1025    :  
1026  E :      VLOG(1) << "Finding cold blocks.";
1027  E :      if (!FindColdBlocksFromCompilands(dia_session.get()))
1028  i :        return false;
1029    :  
1030    :      // Flesh out the rest of the image with gap blocks.
1031  E :      VLOG(1) << "Creating gap blocks.";
1032  E :      if (!CreateGapBlocks())
1033  i :        return false;
1034    :  
1035    :      // Finalize the PE-parsed intermediate references.
1036  E :      VLOG(1) << "Finalizing intermediate references.";
1037  E :      if (!FinalizeIntermediateReferences(references))
1038  i :        return false;
1039  E :    }
1040    :  
1041    :    // Parse the fixups and use them to create references.
1042  E :    VLOG(1) << "Parsing fixups.";
1043  E :    if (!CreateReferencesFromFixups(dia_session.get()))
1044  i :      return false;
1045    :  
1046    :    // Annotate the block-graph with symbol information.
1047  E :    VLOG(1) << "Parsing symbols.";
1048  E :    if (!ProcessSymbols(global.get()))
1049  i :      return false;
1050    :  
1051    :    // Now, find and label any padding blocks.
1052  E :    VLOG(1) << "Labeling padding blocks.";
1053  E :    if (!FindPaddingBlocks(image_layout_))
1054  i :      return false;
1055    :  
1056    :    // Set the alignment on code blocks with jump tables. This ensures that the
1057    :    // jump tables remain aligned post-transform.
1058  E :    VLOG(1) << "Calculating code block alignments.";
1059  E :    if (!AlignCodeBlocks(image_layout_))
1060  i :      return false;
1061    :  
1062    :    // Set the alignment of data blocks. This is not precise in that it simply
1063    :    // guesses the alignment based on the address of the block. Some instructions
1064    :    // have alignment requirements on their data but unfortunately the PDB does
1065    :    // not contain explicit alignment information.
1066  E :    VLOG(1) << "Guessing data block alignments.";
1067  E :    GuessDataBlockAlignments(image_file_, image_layout_);
1068    :  
1069  E :    return true;
1070  E :  }
1071    :  
1072    :  bool Decomposer::CreatePEImageBlocksAndReferences(
1073  E :      IntermediateReferences* references) {
1074  E :    DCHECK_NE(reinterpret_cast<IntermediateReferences*>(NULL), references);
1075    :  
1076    :    PEFileParser::AddReferenceCallback add_reference(
1077  E :        base::Bind(&AddIntermediateReference, base::Unretained(references)));
1078  E :    PEFileParser parser(image_file_, image_, add_reference);
1079  E :    PEFileParser::PEHeader header;
1080  E :    if (!parser.ParseImage(&header)) {
1081  i :      LOG(ERROR) << "Unable to parse PE image.";
1082  i :      return false;
1083    :    }
1084    :  
1085  E :    return true;
1086  E :  }
1087    :  
1088  E :  bool Decomposer::CreateBlocksFromCoffGroups() {
1089  E :    pdb::PdbFile pdb_file;
1090  E :    pdb::PdbReader pdb_reader;
1091  E :    if (!pdb_reader.Read(pdb_path_, &pdb_file)) {
1092  i :      LOG(ERROR) << "Failed to load PDB: " << pdb_path_.value();
1093  i :      return false;
1094    :    }
1095    :  
1096  E :    scoped_refptr<pdb::PdbStream> symbols = GetLinkerSymbolStream(pdb_file);
1097    :  
1098    :    // Process the symbols in the linker module symbol stream.
1099  E :    VisitLinkerSymbolContext context;
1100    :    pdb::VisitSymbolsCallback callback = base::Bind(
1101    :        &Decomposer::VisitLinkerSymbol,
1102    :        base::Unretained(this),
1103  E :        base::Unretained(&context));
1104  E :    if (!pdb::VisitSymbols(callback, symbols->length(), true, symbols.get()))
1105  i :      return false;
1106    :  
1107    :    // Bail if we did not encounter a closing bracketing symbol where one was
1108    :    // expected.
1109  E :    if (context.current_group_index != -1) {
1110  i :      LOG(ERROR) << "Unable to close bracketed COFF group \""
1111    :                 << context.current_group_prefix << "\".";
1112  i :      return false;
1113    :    }
1114    :  
1115  E :    return true;
1116  E :  }
1117    :  
1118  E :  bool Decomposer::CreateBlocksFromSectionContribs(IDiaSession* session) {
1119  E :    ScopedComPtr<IDiaEnumSectionContribs> section_contribs;
1120    :    SearchResult search_result = FindDiaTable(session,
1121  E :                                              section_contribs.Receive());
1122  E :    if (search_result != kSearchSucceeded) {
1123  i :      if (search_result == kSearchFailed)
1124  i :        LOG(ERROR) << "No section contribution table found.";
1125  i :      return false;
1126    :    }
1127    :  
1128  E :    size_t rsrc_id = image_file_.GetSectionIndex(kResourceSectionName);
1129    :  
1130  E :    LONG count = 0;
1131  E :    if (section_contribs->get_Count(&count) != S_OK) {
1132  i :      LOG(ERROR) << "Failed to get section contributions enumeration length.";
1133  i :      return false;
1134    :    }
1135    :  
1136  E :    for (LONG visited = 0; visited < count; ++visited) {
1137  E :      ScopedComPtr<IDiaSectionContrib> section_contrib;
1138  E :      ULONG fetched = 0;
1139  E :      HRESULT hr = section_contribs->Next(1, section_contrib.Receive(), &fetched);
1140    :      // The standard way to end an enumeration (according to the docs) is by
1141    :      // returning S_FALSE and setting fetched to 0. We don't actually see this,
1142    :      // but it wouldn't be an error if we did.
1143  E :      if (hr == S_FALSE && fetched == 0)
1144  i :        break;
1145  E :      if (hr != S_OK) {
1146  i :        LOG(ERROR) << "Failed to get DIA section contribution: "
1147    :                   << common::LogHr(hr) << ".";
1148  i :        return false;
1149    :      }
1150    :      // We actually end up seeing S_OK and fetched == 0 when the enumeration
1151    :      // terminates, which goes against the publishes documentations.
1152  E :      if (fetched == 0)
1153  i :        break;
1154    :  
1155  E :      DWORD rva = 0;
1156  E :      DWORD length = 0;
1157  E :      DWORD section_id = 0;
1158  E :      BOOL code = FALSE;
1159  E :      ScopedComPtr<IDiaSymbol> compiland;
1160  E :      ScopedBstr bstr_compiland_name;
1161    :      if ((hr = section_contrib->get_relativeVirtualAddress(&rva)) != S_OK ||
1162    :          (hr = section_contrib->get_length(&length)) != S_OK ||
1163    :          (hr = section_contrib->get_addressSection(&section_id)) != S_OK ||
1164    :          (hr = section_contrib->get_code(&code)) != S_OK ||
1165    :          (hr = section_contrib->get_compiland(compiland.Receive())) != S_OK ||
1166  E :          (hr = compiland->get_name(bstr_compiland_name.Receive())) != S_OK) {
1167  i :        LOG(ERROR) << "Failed to get section contribution properties: "
1168    :                   << common::LogHr(hr) << ".";
1169  i :        return false;
1170    :      }
1171    :  
1172    :      // Determine if this function was built by a supported compiler.
1173    :      bool is_built_by_supported_compiler =
1174  E :          IsBuiltBySupportedCompiler(compiland.get());
1175    :  
1176    :      // DIA numbers sections from 1 to n, while we do 0 to n - 1.
1177  E :      DCHECK_LT(0u, section_id);
1178  E :      --section_id;
1179    :  
1180    :      // We don't parse the resource section, as it is parsed by the PEFileParser.
1181  E :      if (section_id == rsrc_id)
1182  E :        continue;
1183    :  
1184  E :      std::string compiland_name;
1185    :      if (!base::WideToUTF8(bstr_compiland_name, bstr_compiland_name.Length(),
1186  E :                            &compiland_name)) {
1187  i :        LOG(ERROR) << "Failed to convert compiland name to UTF8.";
1188  i :        return false;
1189    :      }
1190    :  
1191    :      // Give a name to the block based on the basename of the object file. This
1192    :      // will eventually be replaced by the full symbol name, if one exists for
1193    :      // the block.
1194  E :      size_t last_component = compiland_name.find_last_of('\\');
1195  E :      size_t extension = compiland_name.find_last_of('.');
1196  E :      if (last_component == std::string::npos) {
1197  E :        last_component = 0;
1198  E :      } else {
1199    :        // We don't want to include the last slash.
1200  E :        ++last_component;
1201    :      }
1202  E :      if (extension < last_component)
1203  i :        extension = compiland_name.size();
1204    :      std::string name = compiland_name.substr(last_component,
1205  E :                                               extension - last_component);
1206    :  
1207    :      // TODO(chrisha): We see special section contributions with the name
1208    :      //     "* CIL *". These are concatenations of data symbols and can very
1209    :      //     likely be chunked using symbols directly. A cursory visual inspection
1210    :      //     of symbol names hints that these might be related to WPO.
1211    :  
1212    :      // Create the block.
1213    :      BlockType block_type =
1214  E :          code ? BlockGraph::CODE_BLOCK : BlockGraph::DATA_BLOCK;
1215    :      Block* block = CreateBlockOrFindCoveringPeBlock(
1216  E :          block_type, RelativeAddress(rva), length, name);
1217  E :      if (block == NULL) {
1218  i :        LOG(ERROR) << "Unable to create block for compiland \""
1219    :                   << compiland_name << "\".";
1220  i :        return false;
1221    :      }
1222    :  
1223    :      // Set the block compiland name.
1224  E :      block->set_compiland_name(compiland_name);
1225    :  
1226    :      // Set the block attributes.
1227  E :      block->set_attribute(BlockGraph::SECTION_CONTRIB);
1228  E :      if (!is_built_by_supported_compiler)
1229  E :        block->set_attribute(BlockGraph::BUILT_BY_UNSUPPORTED_COMPILER);
1230  E :    }
1231    :  
1232  E :    return true;
1233  E :  }
1234    :  
1235  E :  bool Decomposer::FindColdBlocksFromCompilands(IDiaSession* session) {
1236    :    // Detect hot/cold code separation. Some blocks are outside the function
1237    :    // address range and must be handled as separate blocks. When building
1238    :    // with PGO, the compiler can split functions into "hot" and "cold" blocks,
1239    :    // and move the "cold" blocks out to separate pages, so the function can be
1240    :    // noncontiguous.
1241  E :    ScopedComPtr<IDiaSymbol> global;
1242  E :    if (session->get_globalScope(global.Receive()) != S_OK) {
1243  i :      LOG(ERROR) << "Cannot get global symbol.";
1244  i :      return false;
1245    :    }
1246    :  
1247    :    // Find compilands within the global scope.
1248  E :    ScopedComPtr<IDiaEnumSymbols> compilands;
1249    :    HRESULT status =
1250  E :        global->findChildren(SymTagCompiland, NULL, 0, compilands.Receive());
1251  E :    if (status != S_OK) {
1252  i :      LOG(ERROR) << "Finding compilands failed on the global symbol: "
1253    :                 << common::LogHr(status) << ".";
1254  i :      return false;
1255    :    }
1256    :  
1257    :    // For each compiland, process its lexical blocks.
1258  E :    while (true) {
1259  E :      ULONG count = 0;
1260  E :      ScopedComPtr<IDiaSymbol> compiland;
1261    :      if (compilands->Next(1, compiland.Receive(), &count) != S_OK ||
1262  E :          count != 1) {
1263  E :        break;
1264    :      }
1265    :  
1266  E :      ScopedComPtr<IDiaEnumSymbols> compiland_blocks;
1267    :      status = compiland->findChildren(SymTagBlock,
1268    :                                       NULL,
1269    :                                       0,
1270  E :                                       compiland_blocks.Receive());
1271  E :      if (status != S_OK) {
1272  i :        LOG(ERROR) << "Finding blocks failed on compiland: "
1273    :                   << common::LogHr(status) << ".";
1274  i :        return false;
1275    :      }
1276    :  
1277  E :      LONG blocks_count = 0;
1278  E :      if (compiland_blocks->get_Count(&blocks_count) != S_OK) {
1279  i :        LOG(ERROR) << "Failed to get compiland blocks enumeration length.";
1280  i :        return false;
1281    :      }
1282    :  
1283  E :      for (LONG block_index = 0; block_index < blocks_count; ++block_index) {
1284  E :        ScopedComPtr<IDiaSymbol> compiland_block;
1285  E :        ULONG fetched = 0;
1286    :  
1287  E :        status = compiland_blocks->Next(1, compiland_block.Receive(), &fetched);
1288  E :        if (status == S_FALSE && fetched == 0)
1289  i :          break;
1290  E :        if (status != S_OK) {
1291  i :            LOG(ERROR) << "Failed to get function block: "
1292    :                       << common::LogHr(status) << ".";
1293  i :          return false;
1294    :        }
1295  E :        if (fetched == 0)
1296  i :          break;
1297    :  
1298  E :        ScopedComPtr<IDiaSymbol> parent;
1299  E :        DWORD parent_tag = 0;
1300    :        if (compiland_block->get_lexicalParent(parent.Receive()) != S_OK ||
1301  E :            parent->get_symTag(&parent_tag) != S_OK) {
1302  i :          LOG(ERROR) << "Cannot retrieve block parent.";
1303  i :          return false;
1304    :        }
1305    :  
1306    :        // Only consider function block.
1307  E :        if (parent_tag != SymTagFunction)
1308  i :          continue;
1309    :  
1310    :        // Get relative adresses.
1311    :        DWORD func_rva, block_rva;
1312    :        ULONGLONG func_length;
1313    :        if (compiland_block->get_relativeVirtualAddress(&block_rva) != S_OK ||
1314    :            parent->get_relativeVirtualAddress(&func_rva) != S_OK ||
1315  E :            parent->get_length(&func_length) != S_OK) {
1316  i :          LOG(ERROR) << "Cannot retrieve parent address range.";
1317  i :          return false;
1318    :        }
1319    :  
1320    :        // Retrieve the function block.
1321  E :        Block* func_block = image_->GetBlockByAddress(RelativeAddress(func_rva));
1322  E :        if (func_block == NULL) {
1323  i :          LOG(ERROR) << "Cannot retrieve parent block.";
1324  i :          return false;
1325    :        }
1326    :  
1327    :        // Skip blocks within the range of its parent.
1328  E :        if (block_rva >= func_rva && block_rva <= func_rva + func_length)
1329  i :          continue;
1330    :  
1331    :        // A cold block is detected and needs special handling.
1332  E :        Block* cold_block = image_->GetBlockByAddress(RelativeAddress(block_rva));
1333  E :        if (cold_block == NULL) {
1334  i :          LOG(ERROR) << "Cannot retrieve parent block.";
1335  i :          return false;
1336    :        }
1337    :  
1338  E :        RelativeAddress cold_block_addr;
1339  E :        if (!image_->GetAddressOf(cold_block, &cold_block_addr)) {
1340  i :          LOG(ERROR) << "Cannot retrieve cold block address.";
1341  i :          return false;
1342    :        }
1343    :  
1344    :        // Add cold_block as a child of the function block.
1345  E :        cold_blocks_[func_block][cold_block_addr] = cold_block;
1346    :  
1347    :        // Set the parent relation for blocks belonging to the function block.
1348  E :        cold_blocks_parent_[func_block] = func_block;
1349  E :        cold_blocks_parent_[cold_block] = func_block;
1350  E :      }
1351  E :    }
1352    :  
1353  E :    return true;
1354  E :  }
1355    :  
1356  E :  bool Decomposer::CreateGapBlocks() {
1357  E :    size_t num_sections = image_file_.nt_headers()->FileHeader.NumberOfSections;
1358    :  
1359    :    // Iterate through all the image sections.
1360  E :    for (size_t i = 0; i < num_sections; ++i) {
1361  E :      const IMAGE_SECTION_HEADER* header = image_file_.section_header(i);
1362  E :      DCHECK_NE(reinterpret_cast<IMAGE_SECTION_HEADER*>(NULL), header);
1363    :  
1364  E :      BlockType type = BlockGraph::CODE_BLOCK;
1365  E :      const char* section_type = NULL;
1366  E :      switch (GetSectionType(*header)) {
1367    :        case kSectionCode:
1368  E :          type = BlockGraph::CODE_BLOCK;
1369  E :          section_type = "code";
1370  E :          break;
1371    :  
1372    :        case kSectionData:
1373  E :          type = BlockGraph::DATA_BLOCK;
1374  E :          section_type = "data";
1375  E :          break;
1376    :  
1377    :        default:
1378  i :          continue;
1379    :      }
1380    :  
1381  E :      if (!CreateSectionGapBlocks(header, type)) {
1382  i :        LOG(ERROR) << "Unable to create gap blocks for " << section_type
1383    :                   << " section \"" << header->Name << "\".";
1384  i :        return false;
1385    :      }
1386  E :    }
1387    :  
1388  E :    return true;
1389  E :  }
1390    :  
1391    :  bool Decomposer::FinalizeIntermediateReferences(
1392  E :      const IntermediateReferences& references) {
1393  E :    for (size_t i = 0; i < references.size(); ++i) {
1394    :      // This logs verbosely for us.
1395    :      if (!CreateReference(references[i].src_addr,
1396    :                           references[i].size,
1397    :                           references[i].type,
1398    :                           references[i].dst_addr,
1399    :                           references[i].dst_addr,
1400  E :                           image_)) {
1401  i :        return false;
1402    :      }
1403  E :    }
1404  E :    return true;
1405  E :  }
1406    :  
1407  E :  bool Decomposer::CreateReferencesFromFixups(IDiaSession* session) {
1408  E :    DCHECK_NE(reinterpret_cast<IDiaSession*>(NULL), session);
1409    :  
1410  E :    PEFile::RelocSet reloc_set;
1411  E :    if (!image_file_.DecodeRelocs(&reloc_set))
1412  i :      return false;
1413    :  
1414  E :    OMAPs omap_from;
1415  E :    PdbFixups fixups;
1416  E :    if (!LoadDebugStreams(session, &fixups, &omap_from))
1417  i :      return false;
1418    :  
1419    :    // While creating references from the fixups this removes the
1420    :    // corresponding reference data from the relocs. We use this as a kind of
1421    :    // double-entry bookkeeping to ensure all is well and right in the world.
1422    :    if (!CreateReferencesFromFixupsImpl(image_file_, fixups, omap_from,
1423  E :                                        &reloc_set, image_)) {
1424  i :      return false;
1425    :    }
1426    :  
1427  E :    if (!reloc_set.empty()) {
1428  i :      LOG(ERROR) << "Found reloc entries without matching FIXUP entries.";
1429  i :      return false;
1430    :    }
1431    :  
1432  E :    return true;
1433  E :  }
1434    :  
1435  E :  bool Decomposer::ProcessSymbols(IDiaSymbol* root) {
1436  E :    DCHECK_NE(reinterpret_cast<IDiaSymbol*>(NULL), root);
1437    :  
1438    :    DiaBrowser::MatchCallback on_push_function_or_thunk_symbol(
1439    :        base::Bind(&Decomposer::OnPushFunctionOrThunkSymbol,
1440  E :                   base::Unretained(this)));
1441    :    DiaBrowser::MatchCallback on_pop_function_or_thunk_symbol(
1442    :        base::Bind(&Decomposer::OnPopFunctionOrThunkSymbol,
1443  E :                   base::Unretained(this)));
1444    :    DiaBrowser::MatchCallback on_function_child_symbol(
1445    :        base::Bind(&Decomposer::OnFunctionChildSymbol,
1446  E :                   base::Unretained(this)));
1447    :    DiaBrowser::MatchCallback on_data_symbol(
1448  E :        base::Bind(&Decomposer::OnDataSymbol, base::Unretained(this)));
1449    :    DiaBrowser::MatchCallback on_public_symbol(
1450  E :        base::Bind(&Decomposer::OnPublicSymbol, base::Unretained(this)));
1451    :    DiaBrowser::MatchCallback on_label_symbol(
1452  E :        base::Bind(&Decomposer::OnLabelSymbol, base::Unretained(this)));
1453    :  
1454  E :    DiaBrowser dia_browser;
1455    :  
1456    :    // Find thunks.
1457    :    dia_browser.AddPattern(Seq(Opt(SymTagCompiland), SymTagThunk),
1458    :                           on_push_function_or_thunk_symbol,
1459  E :                           on_pop_function_or_thunk_symbol);
1460    :  
1461    :    // Find functions and all data, labels, callsites, debug start/end and block
1462    :    // symbols below them. This is done in one single pattern so that the
1463    :    // function pushes/pops happen in the right order.
1464    :    dia_browser.AddPattern(
1465    :        Seq(Opt(SymTagCompiland),
1466    :            Callback(Or(SymTagFunction, SymTagThunk),
1467    :                     on_push_function_or_thunk_symbol,
1468    :                     on_pop_function_or_thunk_symbol),
1469    :            Star(SymTagBlock),
1470    :            Or(SymTagData,
1471    :               SymTagLabel,
1472    :               SymTagBlock,
1473    :               SymTagFuncDebugStart,
1474    :               SymTagFuncDebugEnd,
1475    :               SymTagCallSite)),
1476  E :        on_function_child_symbol);
1477    :  
1478    :    // Global data and code label symbols.
1479    :    dia_browser.AddPattern(Seq(Opt(SymTagCompiland), SymTagLabel),
1480  E :                           on_label_symbol);
1481    :    dia_browser.AddPattern(Seq(Opt(SymTagCompiland), SymTagData),
1482  E :                           on_data_symbol);
1483    :  
1484    :    // Public symbols. These provide decorated names without any type info, but
1485    :    // are useful for debugging.
1486  E :    dia_browser.AddPattern(SymTagPublicSymbol, on_public_symbol);
1487    :  
1488  E :    return dia_browser.Browse(root);
1489  E :  }
1490    :  
1491    :  bool Decomposer::VisitLinkerSymbol(VisitLinkerSymbolContext* context,
1492    :                                        uint16 symbol_length,
1493    :                                        uint16 symbol_type,
1494  E :                                        pdb::PdbStream* stream) {
1495  E :    DCHECK_NE(reinterpret_cast<VisitLinkerSymbolContext*>(NULL), context);
1496  E :    DCHECK_NE(reinterpret_cast<pdb::PdbStream*>(NULL), stream);
1497    :  
1498  E :    if (symbol_type != cci::S_COFFGROUP)
1499  E :      return true;
1500    :  
1501  E :    std::vector<uint8> buffer;
1502    :    const cci::CoffGroupSym* coffgroup =
1503  E :        ParseSymbol<cci::CoffGroupSym>(symbol_length, stream, &buffer);
1504  E :    if (coffgroup == NULL)
1505  i :      return false;
1506    :  
1507    :    // The PDB numbers sections starting at index 1 but we use index 0.
1508    :    RelativeAddress rva(image_layout_->sections[coffgroup->seg - 1].addr +
1509  E :        coffgroup->off);
1510    :  
1511    :    // We are looking for an opening symbol.
1512  E :    if (context->current_group_index == -1) {
1513  E :      for (size_t i = 0; i < context->bracketing_groups.size(); ++i) {
1514  E :        std::string prefix;
1515  E :        if (context->bracketing_groups[i].FullMatch(coffgroup->name, &prefix)) {
1516  E :          context->current_group_index = i;
1517  E :          context->current_group_prefix = prefix;
1518  E :          context->current_group_start = rva;
1519  E :          return true;
1520    :        }
1521  E :      }
1522    :  
1523    :      // No opening symbol was encountered. We can safely ignore this
1524    :      // COFF group symbol.
1525  E :      return true;
1526    :    }
1527    :  
1528    :    // If we get here we've found an opening symbol and we're looking for the
1529    :    // matching closing symbol.
1530  E :    std::string prefix;
1531    :    if (!context->bracketing_groups[context->current_group_index].FullMatch(
1532  E :            coffgroup->name, &prefix)) {
1533  E :      return true;
1534    :    }
1535    :  
1536  E :    if (prefix != context->current_group_prefix) {
1537    :      // We see another symbol open/close while already in an opened symbol.
1538    :      // This indicates nested bracketing information, which we've never seen
1539    :      // before.
1540  i :      LOG(ERROR) << "Encountered nested bracket symbol \"" << prefix
1541    :                 << "\" while in \"" << context->current_group_prefix << "\".";
1542  i :      return false;
1543    :    }
1544    :  
1545  E :    RelativeAddress end = rva + coffgroup->cb;
1546  E :    DCHECK_LE(context->current_group_start, end);
1547    :  
1548    :    // If the COFF group is not empty, then create a block corresponding to it.
1549  E :    if (context->current_group_start != end) {
1550    :      // Create a block for this bracketed COFF group.
1551    :      Block* block = CreateBlock(
1552    :          BlockGraph::DATA_BLOCK,
1553    :          context->current_group_start,
1554    :          end - context->current_group_start,
1555  E :          base::StringPrintf("Bracketed COFF group: %s", prefix.c_str()));
1556  E :      if (block == NULL) {
1557  i :        LOG(ERROR) << "Failed to create bracketed COFF group \""
1558    :                   << prefix << "\".";
1559  i :        return false;
1560    :      }
1561  E :      block->set_attribute(BlockGraph::COFF_GROUP);
1562    :    }
1563    :  
1564    :    // Indicate that this block is closed and we're looking for another opening
1565    :    // bracket symbol.
1566  E :    context->current_group_index = -1;
1567  E :    context->current_group_prefix.clear();
1568  E :    context->current_group_start = RelativeAddress(0);
1569    :  
1570  E :    return true;
1571  E :  }
1572    :  
1573    :  DiaBrowser::BrowserDirective Decomposer::OnPushFunctionOrThunkSymbol(
1574    :      const DiaBrowser& dia_browser,
1575    :      const DiaBrowser::SymTagVector& sym_tags,
1576  E :      const DiaBrowser::SymbolPtrVector& symbols) {
1577  E :    DCHECK(!symbols.empty());
1578  E :    DCHECK_EQ(sym_tags.size(), symbols.size());
1579  E :    DiaBrowser::SymbolPtr symbol = symbols.back();
1580    :  
1581  E :    DCHECK_EQ(reinterpret_cast<Block*>(NULL), current_block_);
1582  E :    DCHECK_EQ(current_address_, RelativeAddress(0));
1583  E :    DCHECK_EQ(0u, current_scope_count_);
1584    :  
1585  E :    HRESULT hr = E_FAIL;
1586  E :    DWORD location_type = LocIsNull;
1587  E :    DWORD rva = 0;
1588  E :    ULONGLONG length = 0;
1589  E :    ScopedBstr name_bstr;
1590    :    if (FAILED(hr = symbol->get_locationType(&location_type)) ||
1591    :        FAILED(hr = symbol->get_relativeVirtualAddress(&rva)) ||
1592    :        FAILED(hr = symbol->get_length(&length)) ||
1593  E :        FAILED(hr = symbol->get_name(name_bstr.Receive()))) {
1594  i :      LOG(ERROR) << "Failed to get function/thunk properties: "
1595    :                 << common::LogHr(hr) << ".";
1596  i :      return DiaBrowser::kBrowserAbort;
1597    :    }
1598    :  
1599    :    // We only care about functions with static storage. We can stop looking at
1600    :    // things below this node, as we won't be able to resolve them either.
1601  E :    if (location_type != LocIsStatic)
1602  i :      return DiaBrowser::kBrowserTerminatePath;
1603    :  
1604  E :    RelativeAddress addr(rva);
1605  E :    Block* block = image_->GetBlockByAddress(addr);
1606  E :    CHECK(block != NULL);
1607  E :    RelativeAddress block_addr;
1608  E :    CHECK(image_->GetAddressOf(block, &block_addr));
1609  E :    DCHECK(InRange(addr, block_addr, block->size()));
1610    :  
1611  E :    std::string name;
1612  E :    if (!base::WideToUTF8(name_bstr, name_bstr.Length(), &name)) {
1613  i :      LOG(ERROR) << "Failed to convert function/thunk name to UTF8.";
1614  i :      return DiaBrowser::kBrowserAbort;
1615    :    }
1616    :  
1617    :    // We know the function starts in this block but we need to make sure its
1618    :    // end does not extend past the end of the block.
1619  E :    if (addr + length > block_addr + block->size()) {
1620  i :      LOG(ERROR) << "Got function/thunk \"" << name << "\" that is not contained "
1621    :                 << "by section contribution \"" << block->name() << "\".";
1622  i :      return DiaBrowser::kBrowserAbort;
1623    :    }
1624    :  
1625  E :    Offset offset = addr - block_addr;
1626  E :    if (!AddLabelToBlock(offset, name, BlockGraph::CODE_LABEL, block))
1627  i :      return DiaBrowser::kBrowserAbort;
1628    :  
1629    :    // Keep track of the generated block. We will use this when parsing symbols
1630    :    // that belong to this function. This prevents us from having to do repeated
1631    :    // lookups and also allows us to associate labels outside of the block to the
1632    :    // correct block.
1633  E :    current_block_ = block;
1634  E :    current_address_ = block_addr;
1635    :  
1636    :    // Certain properties are not defined on all blocks, so the following calls
1637    :    // may return S_FALSE.
1638  E :    BOOL no_return = FALSE;
1639  E :    if (symbol->get_noReturn(&no_return) != S_OK)
1640  E :      no_return = FALSE;
1641    :  
1642  E :    BOOL has_inl_asm = FALSE;
1643  E :    if (symbol->get_hasInlAsm(&has_inl_asm) != S_OK)
1644  E :      has_inl_asm = FALSE;
1645    :  
1646  E :    BOOL has_eh = FALSE;
1647  E :    if (symbol->get_hasEH(&has_eh) != S_OK)
1648  E :      has_eh = FALSE;
1649    :  
1650  E :    BOOL has_seh = FALSE;
1651  E :    if (symbol->get_hasSEH(&has_seh) != S_OK)
1652  E :      has_seh = FALSE;
1653    :  
1654    :    // Set the block attributes.
1655  E :    if (no_return == TRUE)
1656  E :      block->set_attribute(BlockGraph::NON_RETURN_FUNCTION);
1657  E :    if (has_inl_asm == TRUE)
1658  E :      block->set_attribute(BlockGraph::HAS_INLINE_ASSEMBLY);
1659  E :    if (has_eh || has_seh)
1660  E :      block->set_attribute(BlockGraph::HAS_EXCEPTION_HANDLING);
1661  E :    if (IsSymTag(symbol.get(), SymTagThunk))
1662  E :      block->set_attribute(BlockGraph::THUNK);
1663    :  
1664  E :    return DiaBrowser::kBrowserContinue;
1665  E :  }
1666    :  
1667    :  DiaBrowser::BrowserDirective Decomposer::OnPopFunctionOrThunkSymbol(
1668    :      const DiaBrowser& dia_browser,
1669    :      const DiaBrowser::SymTagVector& sym_tags,
1670  E :      const DiaBrowser::SymbolPtrVector& symbols) {
1671    :    // Simply clean up the current function block and address.
1672  E :    current_block_ = NULL;
1673  E :    current_address_ = RelativeAddress(0);
1674  E :    current_scope_count_ = 0;
1675  E :    return DiaBrowser::kBrowserContinue;
1676  E :  }
1677    :  
1678    :  DiaBrowser::BrowserDirective Decomposer::OnFunctionChildSymbol(
1679    :        const DiaBrowser& dia_browser,
1680    :        const DiaBrowser::SymTagVector& sym_tags,
1681  E :        const DiaBrowser::SymbolPtrVector& symbols) {
1682  E :    DCHECK(!symbols.empty());
1683  E :    DCHECK_EQ(sym_tags.size(), symbols.size());
1684    :  
1685    :    // This can only be called from the context of a function, so we expect the
1686    :    // parent function block to be set and remembered.
1687  E :    DCHECK_NE(reinterpret_cast<Block*>(NULL), current_block_);
1688    :  
1689    :    // The set of sym tags here should match the pattern used in the DiaBrowser
1690    :    // instance set up in ProcessSymbols.
1691  E :    switch (sym_tags.back()) {
1692    :      case SymTagData:
1693  E :        return OnDataSymbol(dia_browser, sym_tags, symbols);
1694    :  
1695    :      case SymTagLabel:
1696  E :        return OnLabelSymbol(dia_browser, sym_tags, symbols);
1697    :  
1698    :      case SymTagBlock:
1699    :      case SymTagFuncDebugStart:
1700    :      case SymTagFuncDebugEnd:
1701  E :        return OnScopeSymbol(sym_tags.back(), symbols.back());
1702    :  
1703    :      case SymTagCallSite:
1704  E :        return OnCallSiteSymbol(symbols.back());
1705    :  
1706    :      default:
1707    :        break;
1708    :    }
1709    :  
1710  i :    LOG(ERROR) << "Unhandled function child symbol: " << sym_tags.back() << ".";
1711  i :    return DiaBrowser::kBrowserAbort;
1712  E :  }
1713    :  
1714    :  DiaBrowser::BrowserDirective Decomposer::OnDataSymbol(
1715    :      const DiaBrowser& dia_browser,
1716    :      const DiaBrowser::SymTagVector& sym_tags,
1717  E :      const DiaBrowser::SymbolPtrVector& symbols) {
1718  E :    DCHECK(!symbols.empty());
1719  E :    DCHECK_EQ(sym_tags.size(), symbols.size());
1720  E :    DiaBrowser::SymbolPtr symbol = symbols.back();
1721    :  
1722  E :    HRESULT hr = E_FAIL;
1723  E :    DWORD location_type = LocIsNull;
1724  E :    DWORD rva = 0;
1725  E :    ScopedBstr name_bstr;
1726    :    if (FAILED(hr = symbol->get_locationType(&location_type)) ||
1727    :        FAILED(hr = symbol->get_relativeVirtualAddress(&rva)) ||
1728  E :        FAILED(hr = symbol->get_name(name_bstr.Receive()))) {
1729  i :      LOG(ERROR) << "Failed to get data properties: " << common::LogHr(hr) << ".";
1730  i :      return DiaBrowser::kBrowserAbort;
1731    :    }
1732    :  
1733    :    // Symbols with an address of zero are essentially invalid. They appear to
1734    :    // have been optimized away by the compiler, but they are still reported.
1735  E :    if (rva == 0)
1736  E :      return DiaBrowser::kBrowserTerminatePath;
1737    :  
1738    :    // We only care about functions with static storage. We can stop looking at
1739    :    // things below this node, as we won't be able to resolve them either.
1740  E :    if (location_type != LocIsStatic)
1741  i :      return DiaBrowser::kBrowserTerminatePath;
1742    :  
1743    :    // Get the size of this datum from its type info.
1744  E :    size_t length = 0;
1745  E :    if (!GetDataSymbolSize(symbol.get(), &length))
1746  i :      return DiaBrowser::kBrowserAbort;
1747    :  
1748    :    // Reuse the parent function block if we can. This acts as small lookup
1749    :    // cache.
1750  E :    RelativeAddress addr(rva);
1751  E :    Block* block = current_block_;
1752  E :    RelativeAddress block_addr(current_address_);
1753  E :    if (block == NULL || !InRange(addr, block_addr, block->size())) {
1754  E :      block = image_->GetBlockByAddress(addr);
1755  E :      CHECK(block != NULL);
1756  E :      CHECK(image_->GetAddressOf(block, &block_addr));
1757  E :      DCHECK(InRange(addr, block_addr, block->size()));
1758    :    }
1759    :  
1760  E :    std::string name;
1761  E :    if (!base::WideToUTF8(name_bstr, name_bstr.Length(), &name)) {
1762  i :      LOG(ERROR) << "Failed to convert label name to UTF8.";
1763  i :      return DiaBrowser::kBrowserAbort;
1764    :    }
1765    :  
1766    :    // Zero-length data symbols mark case/jump tables, or are forward declares.
1767  E :    BlockGraph::LabelAttributes attr = BlockGraph::DATA_LABEL;
1768  E :    Offset offset = addr - block_addr;
1769  E :    if (length == 0) {
1770    :      // Jump and case tables come in as data symbols with no name. Jump tables
1771    :      // are always an array of pointers, thus they coincide exactly with a
1772    :      // reference. Case tables are simple arrays of integer values (themselves
1773    :      // indices into a jump table), thus do not coincide with a reference.
1774  E :      if (name.empty() && block->type() == BlockGraph::CODE_BLOCK) {
1775  E :        if (block->references().find(offset) != block->references().end()) {
1776  E :          name = kJumpTable;
1777  E :          attr |= BlockGraph::JUMP_TABLE_LABEL;
1778  E :        } else {
1779  E :          name = kCaseTable;
1780  E :          attr |= BlockGraph::CASE_TABLE_LABEL;
1781    :        }
1782  E :      } else {
1783    :        // Zero-length data symbols act as 'forward declares' in some sense. They
1784    :        // are always followed by a non-zero length data symbol with the same name
1785    :        // and location.
1786  E :        return DiaBrowser::kBrowserTerminatePath;
1787    :      }
1788    :    }
1789    :  
1790    :    // Verify that the data symbol does not exceed the size of the block.
1791  E :    if (addr + length > block_addr + block->size()) {
1792    :      // The data symbol can exceed the size of the block in the case of data
1793    :      // imports. For some reason the toolchain emits a global data symbol with
1794    :      // type information equal to the type of the data *pointed* to by the import
1795    :      // entry rather than the type of the entry itself. Thus, if the data type
1796    :      // is bigger than the entire IAT this symbol will exceed it. To complicate
1797    :      // matters even more, a poorly written module can import its own export in
1798    :      // which case a linker generated pseudo-import-entry block will be
1799    :      // generated. This won't be part of the IAT, so we can't even filter based
1800    :      // on that. Instead, we simply ignore global data symbols that exceed the
1801    :      // block size.
1802  E :      base::StringPiece spname(name);
1803  E :      if (sym_tags.size() == 1 && spname.starts_with("_imp_")) {
1804  E :        VLOG(1) << "Encountered an imported data symbol \"" << name << "\" that "
1805    :                << "extends past its parent block \"" << block->name() << "\".";
1806  E :      } else {
1807  i :        LOG(ERROR) << "Received data symbol \"" << name << "\" that extends past "
1808    :                   << "its parent block \"" << block->name() << "\".";
1809  i :        return DiaBrowser::kBrowserAbort;
1810    :      }
1811    :    }
1812    :  
1813  E :    if (!AddLabelToBlock(offset, name, attr, block))
1814  i :      return DiaBrowser::kBrowserAbort;
1815    :  
1816  E :    return DiaBrowser::kBrowserContinue;
1817  E :  }
1818    :  
1819    :  DiaBrowser::BrowserDirective Decomposer::OnPublicSymbol(
1820    :      const DiaBrowser& dia_browser,
1821    :      const DiaBrowser::SymTagVector& sym_tags,
1822  E :      const DiaBrowser::SymbolPtrVector& symbols) {
1823  E :    DCHECK(!symbols.empty());
1824  E :    DCHECK_EQ(sym_tags.size(), symbols.size());
1825  E :    DCHECK_EQ(reinterpret_cast<Block*>(NULL), current_block_);
1826  E :    DiaBrowser::SymbolPtr symbol = symbols.back();
1827    :  
1828  E :    HRESULT hr = E_FAIL;
1829  E :    DWORD rva = 0;
1830  E :    ScopedBstr name_bstr;
1831    :    if (FAILED(hr = symbol->get_relativeVirtualAddress(&rva)) ||
1832  E :        FAILED(hr = symbol->get_name(name_bstr.Receive()))) {
1833  i :      LOG(ERROR) << "Failed to get public symbol properties: "
1834    :                 << common::LogHr(hr) << ".";
1835  i :      return DiaBrowser::kBrowserAbort;
1836    :    }
1837    :  
1838  E :    RelativeAddress addr(rva);
1839  E :    Block* block = image_->GetBlockByAddress(addr);
1840  E :    CHECK(block != NULL);
1841  E :    RelativeAddress block_addr;
1842  E :    CHECK(image_->GetAddressOf(block, &block_addr));
1843  E :    DCHECK(InRange(addr, block_addr, block->size()));
1844    :  
1845  E :    std::string name;
1846  E :    base::WideToUTF8(name_bstr, name_bstr.Length(), &name);
1847    :  
1848    :    // Public symbol names are mangled. Remove leading '_' as per
1849    :    // http://msdn.microsoft.com/en-us/library/00kh39zz(v=vs.80).aspx
1850  E :    if (name[0] == '_')
1851  E :      name = name.substr(1);
1852    :  
1853  E :    Offset offset = addr - block_addr;
1854  E :    if (!AddLabelToBlock(offset, name, BlockGraph::PUBLIC_SYMBOL_LABEL, block))
1855  i :      return DiaBrowser::kBrowserAbort;
1856    :  
1857  E :    return DiaBrowser::kBrowserContinue;
1858  E :  }
1859    :  
1860    :  DiaBrowser::BrowserDirective Decomposer::OnLabelSymbol(
1861    :      const DiaBrowser& dia_browser,
1862    :      const DiaBrowser::SymTagVector& sym_tags,
1863  E :      const DiaBrowser::SymbolPtrVector& symbols) {
1864  E :    DCHECK(!symbols.empty());
1865  E :    DCHECK_EQ(sym_tags.size(), symbols.size());
1866  E :    DiaBrowser::SymbolPtr symbol = symbols.back();
1867    :  
1868  E :    HRESULT hr = E_FAIL;
1869  E :    DWORD rva = 0;
1870  E :    ScopedBstr name_bstr;
1871    :    if (FAILED(hr = symbol->get_relativeVirtualAddress(&rva)) ||
1872  E :        FAILED(hr = symbol->get_name(name_bstr.Receive()))) {
1873  i :      LOG(ERROR) << "Failed to get label symbol properties: " << common::LogHr(hr)
1874    :                 << ".";
1875  i :      return DiaBrowser::kBrowserAbort;
1876    :    }
1877    :  
1878    :    // If we have a current_block_ the label should lie within its scope.
1879  E :    RelativeAddress addr(rva);
1880  E :    Block* block = current_block_;
1881  E :    RelativeAddress block_addr(current_address_);
1882  E :    if (block != NULL) {
1883    :      // Try to find the block in the cold blocks. The cold blocks aren't in the
1884    :      // same address space as the original function.
1885  E :      if (!InRangeIncl(addr, block_addr, block->size())) {
1886    :  
1887    :        // Determine the function block containing this block.
1888    :        ColdBlocksParent::iterator function_block =
1889  E :            cold_blocks_parent_.find(block);
1890  E :        if (function_block != cold_blocks_parent_.end())
1891  E :          block = function_block->second;
1892    :  
1893    :        // Retrieve the first cold block related to that function before |addr|.
1894  E :        ColdBlocksMap::iterator cold_blocks_it = cold_blocks_.find(block);
1895  E :        if (cold_blocks_it != cold_blocks_.end()) {
1896  E :          ColdBlocks& cold_blocks = cold_blocks_it->second;
1897  E :          if (!cold_blocks.empty()) {
1898    :            // Find the block containing the address |addr|. When |addr| is not
1899    :            // the same as the block address, the iterator points to the next
1900    :            // block.
1901  E :            ColdBlocks::iterator cold_block_it = cold_blocks.lower_bound(addr);
1902    :            if (cold_block_it == cold_blocks.end() ||
1903  E :                cold_block_it->second->addr() != addr) {
1904  E :              cold_block_it--;
1905    :            }
1906    :  
1907    :            // Check whether the address falls into this cold block.
1908  E :            DCHECK(cold_block_it != cold_blocks.end());
1909  E :            Block* cold_block = cold_block_it->second;
1910  E :            if (InRangeIncl(addr, cold_block->addr(), cold_block->size()))
1911  E :              block = cold_block;
1912    :          }
1913    :        }
1914    :  
1915    :        // Update the block address according to the cold block found.
1916  E :        if (!image_->GetAddressOf(block, &block_addr)) {
1917  i :          LOG(ERROR) << "Cannot retrieve cold block address.";
1918  i :          return DiaBrowser::kBrowserAbort;
1919    :        }
1920    :      }
1921    :  
1922  E :      if (!InRangeIncl(addr, block_addr, block->size())) {
1923  i :        LOG(ERROR) << "Label falls outside of current block \""
1924    :                   << block->name() << "\".";
1925  i :        return DiaBrowser::kBrowserAbort;
1926    :      }
1927  E :    } else {
1928    :      // If there is no current block this is a compiland scope label.
1929  E :      block = image_->GetBlockByAddress(addr);
1930  E :      CHECK(block != NULL);
1931  E :      CHECK(image_->GetAddressOf(block, &block_addr));
1932  E :      DCHECK(InRange(addr, block_addr, block->size()));
1933    :  
1934    :      // TODO(chrisha): This label is in compiland scope, so we should be
1935    :      //     finding the block whose section contribution shares the same
1936    :      //     compiland.
1937    :    }
1938    :  
1939  E :    std::string name;
1940  E :    base::WideToUTF8(name_bstr, name_bstr.Length(), &name);
1941    :  
1942  E :    Offset offset = addr - block_addr;
1943  E :    if (!AddLabelToBlock(offset, name, BlockGraph::CODE_LABEL, block))
1944  i :      return DiaBrowser::kBrowserAbort;
1945    :  
1946  E :    return DiaBrowser::kBrowserContinue;
1947  E :  }
1948    :  
1949    :  DiaBrowser::BrowserDirective Decomposer::OnScopeSymbol(
1950  E :      enum SymTagEnum type, DiaBrowser::SymbolPtr symbol) {
1951    :    // We should only get here via the successful exploration of a SymTagFunction,
1952    :    // so current_block_ should be set.
1953  E :    DCHECK_NE(reinterpret_cast<Block*>(NULL), current_block_);
1954    :  
1955  E :    HRESULT hr = E_FAIL;
1956  E :    DWORD rva = 0;
1957  E :    if (FAILED(hr = symbol->get_relativeVirtualAddress(&rva))) {
1958  i :      LOG(ERROR) << "Failed to get scope symbol properties: " << common::LogHr(hr)
1959    :                 << ".";
1960  i :      return DiaBrowser::kBrowserAbort;
1961    :    }
1962    :  
1963    :    // The label may potentially lay at the first byte past the function.
1964  E :    RelativeAddress addr(rva);
1965  E :    DCHECK_LE(current_address_, addr);
1966  E :    DCHECK_LE(addr, current_address_ + current_block_->size());
1967    :  
1968    :    // Get the attributes for this label.
1969  E :    BlockGraph::LabelAttributes attr = 0;
1970  E :    std::string name;
1971  E :    CHECK(ScopeSymTagToLabelProperties(type, current_scope_count_, &attr, &name));
1972    :  
1973    :    // Add the label.
1974  E :    Offset offset = addr - current_address_;
1975  E :    if (!AddLabelToBlock(offset, name, attr, current_block_))
1976  i :      return DiaBrowser::kBrowserAbort;
1977    :  
1978    :    // If this is a scope we extract the length and explicitly add a corresponding
1979    :    // end label.
1980  E :    if (type == SymTagBlock) {
1981  E :      ULONGLONG length = 0;
1982  E :      if (symbol->get_length(&length) != S_OK) {
1983  i :        LOG(ERROR) << "Failed to extract code scope length for block \""
1984    :                    << current_block_->name() << "\".";
1985  i :        return DiaBrowser::kBrowserAbort;
1986    :      }
1987  E :      DCHECK_LE(static_cast<size_t>(offset + length), current_block_->size());
1988  E :      name = base::StringPrintf("<scope-end-%d>", current_scope_count_);
1989  E :      ++current_scope_count_;
1990    :      if (!AddLabelToBlock(offset + length, name,
1991  E :                           BlockGraph::SCOPE_END_LABEL, current_block_)) {
1992  i :        return DiaBrowser::kBrowserAbort;
1993    :      }
1994    :    }
1995    :  
1996  E :    return DiaBrowser::kBrowserContinue;
1997  E :  }
1998    :  
1999    :  DiaBrowser::BrowserDirective Decomposer::OnCallSiteSymbol(
2000  E :      DiaBrowser::SymbolPtr symbol) {
2001    :    // We should only get here via the successful exploration of a SymTagFunction,
2002    :    // so current_block_ should be set.
2003  E :    DCHECK_NE(reinterpret_cast<Block*>(NULL), current_block_);
2004    :  
2005  E :    HRESULT hr = E_FAIL;
2006  E :    DWORD rva = 0;
2007  E :    if (FAILED(hr = symbol->get_relativeVirtualAddress(&rva))) {
2008  i :      LOG(ERROR) << "Failed to get call site symbol properties: "
2009    :                 << common::LogHr(hr) << ".";
2010  i :      return DiaBrowser::kBrowserAbort;
2011    :    }
2012    :  
2013  E :    RelativeAddress addr(rva);
2014  E :    if (!InRange(addr, current_address_, current_block_->size())) {
2015    :      // We see this happen under some build configurations (notably debug
2016    :      // component builds of Chrome). As long as the label falls entirely
2017    :      // outside of the block it is harmless and can be safely ignored.
2018  E :      VLOG(1) << "Call site falls outside of current block \""
2019    :              << current_block_->name() << "\".";
2020  E :      return DiaBrowser::kBrowserContinue;
2021    :    }
2022    :  
2023  E :    Offset offset = addr - current_address_;
2024    :    if (!AddLabelToBlock(offset, "<call-site>", BlockGraph::CALL_SITE_LABEL,
2025  E :                         current_block_)) {
2026  i :      return DiaBrowser::kBrowserAbort;
2027    :    }
2028    :  
2029  E :    return DiaBrowser::kBrowserContinue;
2030  E :  }
2031    :  
2032    :  Block* Decomposer::CreateBlock(BlockType type,
2033    :                                    RelativeAddress address,
2034    :                                    BlockGraph::Size size,
2035  E :                                    const base::StringPiece& name) {
2036  E :    Block* block = image_->AddBlock(type, address, size, name);
2037  E :    if (block == NULL) {
2038  i :      LOG(ERROR) << "Unable to add block \"" << name.as_string() << "\" at "
2039    :                 << address << " with size " << size << ".";
2040  i :      return NULL;
2041    :    }
2042    :  
2043    :    // Mark the source range from whence this block originates. This is assuming
2044    :    // an untransformed image. To handle transformed images we'd have to use the
2045    :    // OMAP information to do this properly.
2046    :    bool pushed = block->source_ranges().Push(
2047    :        Block::DataRange(0, size),
2048  E :        Block::SourceRange(address, size));
2049  E :    DCHECK(pushed);
2050    :  
2051  E :    BlockGraph::SectionId section = image_file_.GetSectionIndex(address, size);
2052  E :    if (section == BlockGraph::kInvalidSectionId) {
2053  i :      LOG(ERROR) << "Block \"" << name.as_string() << "\" at " << address
2054    :                 << " with size " << size << " lies outside of all sections.";
2055  i :      return NULL;
2056    :    }
2057  E :    block->set_section(section);
2058    :  
2059  E :    const uint8* data = image_file_.GetImageData(address, size);
2060  E :    if (data != NULL)
2061  E :      block->SetData(data, size);
2062    :  
2063  E :    return block;
2064  E :  }
2065    :  
2066    :  Block* Decomposer::CreateBlockOrFindCoveringPeBlock(
2067    :      BlockType type,
2068    :      RelativeAddress addr,
2069    :      BlockGraph::Size size,
2070  E :      const base::StringPiece& name) {
2071  E :    Block* block = image_->GetBlockByAddress(addr);
2072  E :    if (block != NULL) {
2073  E :      RelativeAddress block_addr;
2074  E :      CHECK(image_->GetAddressOf(block, &block_addr));
2075    :  
2076    :      // Allow PE-parsed blocks to be grown to reflect reality. For example,
2077    :      // in VS2013 the linker makes space for 2 debug directories rather than
2078    :      // just one, and the symbols reflect this. We parse the debug directory
2079    :      // with the size indicated in the PE header, which conflicts with that
2080    :      // indicated by the section contributions.
2081  E :      if (name == "* Linker *" && block_addr == addr && size > block->size()) {
2082  E :        if (!image_->ResizeBlock(block, size)) {
2083  i :          LOG(ERROR) << "Failed to extend PE-parsed "
2084    :                     << BlockInfo(block, block_addr) << " with linker "
2085    :                     << "section contribution of size " << size << ".";
2086    :  
2087    :          // Get the conflicting block and output additional information about
2088    :          // it.
2089    :          Block* conflict = image_->GetFirstIntersectingBlock(
2090  i :              block_addr + block->size(), size - block->size());
2091  i :          if (conflict) {
2092  i :            RelativeAddress conflict_addr;
2093  i :            CHECK(image_->GetAddressOf(conflict, &conflict_addr));
2094  i :            LOG(ERROR) << "Conflicts with existing "
2095    :                       << BlockInfo(conflict, conflict_addr) << ".";
2096    :          }
2097    :  
2098  i :          return NULL;
2099    :        }
2100    :  
2101    :        // Update the data in the extended block.
2102  E :        const uint8* data = image_file_.GetImageData(addr, size);
2103  E :        block->SetData(data, size);
2104  E :        return block;
2105    :      }
2106    :  
2107    :      // If this is not a PE parsed or COFF group block that covers us entirely,
2108    :      // then this is an error.
2109    :      static const BlockGraph::BlockAttributes kCoveringAttributes =
2110    :          BlockGraph::PE_PARSED | BlockGraph::COFF_GROUP;
2111  E :      RelativeRange existing_block(block_addr, block->size());
2112    :      if ((block->attributes() & kCoveringAttributes) == 0 ||
2113  E :          !existing_block.Contains(addr, size)) {
2114  i :        LOG(ERROR) << "Trying to create block \"" << name.as_string() << "\" at "
2115    :                   << addr.value() << " with size " << size << " that conflicts "
2116    :                   << "with existing " << BlockInfo(block, block_addr) << ".";
2117  i :        return NULL;
2118    :      }
2119    :  
2120  E :      return block;
2121    :    }
2122  E :    DCHECK_EQ(reinterpret_cast<Block*>(NULL), block);
2123    :  
2124  E :    return CreateBlock(type, addr, size, name);
2125  E :  }
2126    :  
2127    :  bool Decomposer::CreateGapBlock(BlockType block_type,
2128    :                                     RelativeAddress address,
2129  E :                                     BlockGraph::Size size) {
2130    :    Block* block = CreateBlock(block_type, address, size,
2131  E :        base::StringPrintf("Gap Block 0x%08X", address.value()).c_str());
2132  E :    if (block == NULL) {
2133  i :      LOG(ERROR) << "Unable to create gap block.";
2134  i :      return false;
2135    :    }
2136  E :    block->set_attribute(BlockGraph::GAP_BLOCK);
2137    :  
2138  E :    return true;
2139  E :  }
2140    :  
2141    :  bool Decomposer::CreateSectionGapBlocks(const IMAGE_SECTION_HEADER* header,
2142  E :                                             BlockType block_type) {
2143  E :    RelativeAddress section_begin(header->VirtualAddress);
2144  E :    RelativeAddress section_end(section_begin + header->Misc.VirtualSize);
2145    :    RelativeAddress image_end(
2146  E :        image_file_.nt_headers()->OptionalHeader.SizeOfImage);
2147    :  
2148    :    // Search for the first and last blocks interesting from the start and end
2149    :    // of the section to the end of the image.
2150    :    BlockGraph::AddressSpace::RangeMap::const_iterator it(
2151    :        image_->address_space_impl().FindFirstIntersection(
2152    :            BlockGraph::AddressSpace::Range(section_begin,
2153  E :                                            image_end - section_begin)));
2154    :  
2155    :    BlockGraph::AddressSpace::RangeMap::const_iterator end =
2156  E :        image_->address_space_impl().end();
2157  E :    if (section_end < image_end) {
2158    :      end = image_->address_space_impl().FindFirstIntersection(
2159    :          BlockGraph::AddressSpace::Range(section_end,
2160  E :                                          image_end - section_end));
2161    :    }
2162    :  
2163    :    // The whole section is missing. Cover it with one gap block.
2164  E :    if (it == end)
2165    :      return CreateGapBlock(
2166  i :          block_type, section_begin, section_end - section_begin);
2167    :  
2168    :    // Create the head gap block if need be.
2169  E :    if (section_begin < it->first.start()) {
2170    :      if (!CreateGapBlock(
2171  i :          block_type, section_begin, it->first.start() - section_begin)) {
2172  i :        return false;
2173    :      }
2174    :    }
2175    :  
2176    :    // Now iterate the blocks and fill in gaps.
2177  E :    for (; it != end; ++it) {
2178  E :      const Block* block = it->second;
2179  E :      DCHECK_NE(reinterpret_cast<Block*>(NULL), block);
2180  E :      RelativeAddress block_end = it->first.start() + block->size();
2181  E :      if (block_end >= section_end)
2182  E :        break;
2183    :  
2184    :      // Walk to the next address in turn.
2185  E :      BlockGraph::AddressSpace::RangeMap::const_iterator next = it;
2186  E :      ++next;
2187  E :      if (next == end) {
2188    :        // We're at the end of the list. Create the tail gap block.
2189  E :        DCHECK_GT(section_end, block_end);
2190  E :        if (!CreateGapBlock(block_type, block_end, section_end - block_end))
2191  i :          return false;
2192  E :        break;
2193    :      }
2194    :  
2195    :      // Create the interstitial gap block.
2196  E :      if (block_end < next->first.start()) {
2197    :        if (!CreateGapBlock(
2198  E :            block_type, block_end, next->first.start() - block_end)) {
2199  i :          return false;
2200    :        }
2201    :      }
2202  E :    }
2203    :  
2204  E :    return true;
2205  E :  }
2206    :  
2207    :  }  // namespace pe

Coverage information generated Thu Jan 14 17:40:38 2016.