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

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

Coverage information generated Wed Dec 11 11:34:16 2013.