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

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
66.7%32480.C++source

Line-by-line coverage:

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

Coverage information generated Thu Sep 06 11:30:46 2012.