Coverage for /Syzygy/pe/transforms/add_pdb_info_transform.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
68.6%35510.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/transforms/add_pdb_info_transform.h"
  16    :  
  17    :  #include "base/files/file_util.h"
  18    :  #include "base/strings/string_util.h"
  19    :  #include "base/strings/utf_string_conversions.h"
  20    :  #include "base/time/time.h"
  21    :  #include "syzygy/block_graph/typed_block.h"
  22    :  #include "syzygy/pe/pe_data.h"
  23    :  #include "syzygy/pe/transforms/add_debug_directory_entry_transform.h"
  24    :  
  25    :  namespace pe {
  26    :  namespace transforms {
  27    :  
  28    :  using block_graph::TypedBlock;
  29    :  
  30    :  typedef TypedBlock<IMAGE_DEBUG_DIRECTORY> ImageDebugDirectory;
  31    :  typedef TypedBlock<CvInfoPdb70> CvInfoPdb;
  32    :  
  33    :  const char AddPdbInfoTransform::kTransformName[] =
  34    :      "AddPdbInfoTransform";
  35    :  
  36    :  bool AddPdbInfoTransform::TransformBlockGraph(
  37    :      const TransformPolicyInterface* policy,
  38    :      BlockGraph* block_graph,
  39  E :      BlockGraph::Block* dos_header_block) {
  40  E :    DCHECK_NE(reinterpret_cast<TransformPolicyInterface*>(NULL), policy);
  41  E :    DCHECK_NE(reinterpret_cast<BlockGraph*>(NULL), block_graph);
  42  E :    DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL), dos_header_block);
  43  E :    DCHECK_EQ(BlockGraph::PE_IMAGE, block_graph->image_format());
  44    :  
  45    :    // Make sure the PDB path is absolute.
  46  E :    pdb_path_ = base::MakeAbsoluteFilePath(pdb_path_);
  47  E :    if (pdb_path_.empty()) {
  48  i :      LOG(ERROR) << "Unable to get absolute PDB path.";
  49  i :      return false;
  50    :    }
  51    :  
  52    :    // Find or create the appropriate debug directory entry.
  53    :    AddDebugDirectoryEntryTransform debug_dir_tx(IMAGE_DEBUG_TYPE_CODEVIEW,
  54  E :                                                 false);
  55    :    if (!block_graph::ApplyBlockGraphTransform(
  56  E :            &debug_dir_tx, policy, block_graph, dos_header_block)) {
  57  i :      LOG(ERROR) << debug_dir_tx.name() << " failed.";
  58  i :      return false;
  59    :    }
  60    :  
  61  E :    ImageDebugDirectory debug_dir;
  62  E :    if (!debug_dir.Init(debug_dir_tx.offset(), debug_dir_tx.block())) {
  63  i :      LOG(ERROR) << "Unable to cast IMAGE_DEBUG_DIRECTORY.";
  64  i :      return false;
  65    :    }
  66    :  
  67    :    // Get the path to the PDB in UTF8.
  68  E :    std::string new_pdb_path;
  69    :    if (!base::WideToUTF8(pdb_path_.value().c_str(), pdb_path_.value().size(),
  70  E :                          &new_pdb_path)) {
  71  i :      LOG(ERROR) << "Unable to convert PDB path to UTF8.";
  72  i :      return false;
  73    :    }
  74    :  
  75    :    // Calculate the size of the updated debug info struct. The size of the
  76    :    // struct includes the trailing zero of the path.
  77  E :    size_t new_debug_info_size = sizeof(pe::CvInfoPdb70) + new_pdb_path.size();
  78    :  
  79    :    // If the debug directory entry is empty, then create a new CvInfoPdb
  80    :    // block.
  81  E :    if (!debug_dir.HasReference(debug_dir->AddressOfRawData)) {
  82    :      BlockGraph::Block* cv_info_pdb_block = block_graph->AddBlock(
  83  E :          BlockGraph::DATA_BLOCK, new_debug_info_size, "PDB Info");
  84  E :      DCHECK(cv_info_pdb_block != NULL);
  85  E :      cv_info_pdb_block->set_section(debug_dir.block()->section());
  86  E :      cv_info_pdb_block->set_attribute(BlockGraph::PE_PARSED);
  87  E :      if (cv_info_pdb_block->AllocateData(new_debug_info_size) == NULL) {
  88  i :        LOG(ERROR) << "Failed to allocate block data.";
  89  i :        return false;
  90    :      }
  91    :  
  92    :      debug_dir.SetReference(BlockGraph::RELATIVE_REF,
  93    :                             debug_dir->AddressOfRawData,
  94    :                             cv_info_pdb_block,
  95  E :                             0, 0);
  96    :      debug_dir.SetReference(BlockGraph::FILE_OFFSET_REF,
  97    :                             debug_dir->PointerToRawData,
  98    :                             cv_info_pdb_block,
  99  E :                             0, 0);
 100    :  
 101    :      // The type is set by the AddDebugDirectoryEntry transform, and everything
 102    :      // else is zero initialized. We only need to set the size so that the
 103    :      // following dereference works.
 104  E :      debug_dir->SizeOfData = new_debug_info_size;
 105    :    }
 106    :  
 107  E :    CvInfoPdb cv_info_pdb;
 108    :    if (!debug_dir.DereferenceWithSize(debug_dir->AddressOfRawData,
 109    :                                       debug_dir->SizeOfData,
 110  E :                                       &cv_info_pdb)) {
 111  i :      LOG(ERROR) << "Failed to dereference CvInfoPdb.";
 112  i :      return false;
 113    :    }
 114    :  
 115    :    // Update the debug directory.
 116  E :    debug_dir->TimeDateStamp = static_cast<uint32>(time(NULL));
 117  E :    debug_dir->SizeOfData = new_debug_info_size;
 118    :  
 119    :    // Resize the debug info struct while patching up its metadata.
 120    :    if (!cv_info_pdb.block()->InsertOrRemoveData(cv_info_pdb.offset(),
 121    :                                                 cv_info_pdb.size(),
 122    :                                                 new_debug_info_size,
 123  E :                                                 true)) {
 124  i :      LOG(ERROR) << "InsertOrRemoveData failed.";
 125  i :      return false;
 126    :    }
 127    :  
 128    :  #ifndef NDEBUG
 129    :    // We need to reinit cv_info_pdb as the data may have been reallocated and
 130    :    // in that case the debug object is not up to date. This just makes the
 131    :    // following code more easily debuggable.
 132    :    if (!cv_info_pdb.InitWithSize(cv_info_pdb.offset(),
 133    :                                  new_debug_info_size,
 134  E :                                  cv_info_pdb.block())) {
 135  i :      LOG(ERROR) << "Failed to reinitialize CvInfoPdb.";
 136  i :      return false;
 137    :    }
 138    :  #endif  // NDEBUG
 139    :  
 140    :    // Fill in the debug info structure.
 141  E :    cv_info_pdb->cv_signature = pe::kPdb70Signature;
 142  E :    cv_info_pdb->pdb_age = pdb_age_;
 143  E :    cv_info_pdb->signature = pdb_guid_;
 144    :    base::strlcpy(cv_info_pdb->pdb_file_name,
 145    :                  new_pdb_path.c_str(),
 146  E :                  new_pdb_path.size() + 1);
 147    :  
 148  E :    return true;
 149  E :  }
 150    :  
 151    :  }  // namespace transforms
 152    :  }  // namespace pe

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