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

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

Coverage information generated Thu Jul 04 09:34:53 2013.