Coverage for /Syzygy/relink/relink_app.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
90.4%1031140.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/relink/relink_app.h"
  16    :  
  17    :  #include "base/logging_win.h"
  18    :  #include "base/string_number_conversions.h"
  19    :  #include "syzygy/block_graph/orderers/original_orderer.h"
  20    :  #include "syzygy/block_graph/orderers/random_orderer.h"
  21    :  #include "syzygy/pe/pe_relinker.h"
  22    :  #include "syzygy/pe/transforms/explode_basic_blocks_transform.h"
  23    :  #include "syzygy/reorder/orderers/explicit_orderer.h"
  24    :  #include "syzygy/reorder/transforms/basic_block_layout_transform.h"
  25    :  
  26    :  namespace relink {
  27    :  
  28    :  namespace {
  29    :  
  30    :  const char kUsageFormatStr[] =
  31    :      "Usage: %ls [options]\n"
  32    :      "  Required Options:\n"
  33    :      "    --input-image=<path>  The input image file to relink.\n"
  34    :      "    --output-image=<path> Output path for the rewritten image file.\n"
  35    :      "  Optional Options:\n"
  36    :      "    --basic-blocks        Reorder at the basic-block level. At present,\n"
  37    :      "                          this is only supported for random reorderings.\n"
  38    :      "    --compress-pdb        If --no-augment-pdb is specified, causes the\n"
  39    :      "                          augmented PDB stream to be compressed.\n"
  40    :      "    --exclude-bb-padding  When randomly reordering basic blocks, exclude\n"
  41    :      "                          padding and unreachable code from the relinked\n"
  42    :      "                          output binary.\n"
  43    :      "    --input-pdb=<path>    The PDB file associated with the input DLL.\n"
  44    :      "                          Default is inferred from input-image.\n"
  45    :      "    --no-augment-pdb      Indicates that the relinker should not augment\n"
  46    :      "                          the PDB with roundtrip decomposition info.\n"
  47    :      "    --no-metadata         Prevents the relinker from adding metadata\n"
  48    :      "                          to the output DLL.\n"
  49    :      "    --no-strip-strings    Causes strings to be output in the augmented\n"
  50    :      "                          PDB stream. The default is to omit these to\n"
  51    :      "                          make smaller PDBs.\n"
  52    :      "    --order-file=<path>   Reorder based on a JSON ordering file.\n"
  53    :      "    --output-pdb=<path>   Output path for the rewritten PDB file.\n"
  54    :      "                          Default is inferred from output-image.\n"
  55    :      "    --overwrite           Allow output files to be overwritten.\n"
  56    :      "    --padding=<integer>   Add bytes of padding between blocks.\n"
  57    :      "    --seed=<integer>      Randomly reorder based on the given seed.\n"
  58    :      "  Deprecated Options:\n"
  59    :      "    --input-dll=<path>    Aliased to --input-image.\n"
  60    :      "    --output-dll=<path>   Aliased to --output-image.\n"
  61    :      "  Notes:\n"
  62    :      "    * The --seed and --order-file options are mutually exclusive\n"
  63    :      "    * If --order-file is specified, --input-image is optional.\n"
  64    :      "    * The --compress-pdb and --no-strip-strings options are only\n"
  65    :      "      effective if --no-augment-pdb is not specified.\n"
  66    :      "    * The --exclude-bb-padding option is only effective if\n"
  67    :      "      --basic-blocks is specified.\n";
  68    :  
  69  E :  bool ParsePadding(const std::wstring& value_str, size_t* out_value) {
  70  E :    DCHECK(out_value != NULL);
  71    :  
  72    :    int temp;
  73  E :    if (!base::StringToInt(value_str, &temp) || temp < 0) {
  74  i :      return false;
  75    :    }
  76    :  
  77  E :    *out_value = static_cast<size_t>(temp);
  78  E :    return true;
  79  E :  }
  80    :  
  81  E :  bool ParseUInt32(const std::wstring& value_str, uint32* out_value) {
  82  E :    DCHECK(out_value != NULL);
  83  E :    return base::StringToInt(value_str, reinterpret_cast<int*>(out_value));
  84  E :  }
  85    :  
  86    :  void GuessPdbPath(const FilePath& module_path, FilePath* pdb_path) {
  87    :    DCHECK(pdb_path != NULL);
  88    :    *pdb_path = module_path.ReplaceExtension(L"pdb");
  89    :  }
  90    :  
  91    :  }  // namespace
  92    :  
  93  E :  bool RelinkApp::ParseCommandLine(const CommandLine* cmd_line) {
  94  E :    input_image_path_ = AbsolutePath(cmd_line->GetSwitchValuePath("input-image"));
  95  E :    if (cmd_line->HasSwitch("input-dll")) {
  96  E :      if (input_image_path_.empty()) {
  97  E :        LOG(WARNING) << "Using deprecated switch --input-dll.";
  98    :        input_image_path_ =
  99  E :            AbsolutePath(cmd_line->GetSwitchValuePath("input-dll"));
 100  E :      } else {
 101    :        return Usage(cmd_line,
 102  E :                     "Can't specify both --input-dll and --input-image.");
 103    :      }
 104    :    }
 105  E :    input_pdb_path_ = AbsolutePath(cmd_line->GetSwitchValuePath("input-pdb"));
 106    :  
 107  E :    output_image_path_ = cmd_line->GetSwitchValuePath("output-image");
 108  E :    if (cmd_line->HasSwitch("output-dll")) {
 109  E :      if (output_image_path_.empty()) {
 110  E :        LOG(WARNING) << "Using deprecated switch --output-dll.";
 111    :        output_image_path_ =
 112  E :            AbsolutePath(cmd_line->GetSwitchValuePath("output-dll"));
 113  E :      } else {
 114    :        return Usage(cmd_line,
 115  E :                     "Can't specify both --output-dll and --output-image.");
 116    :      }
 117    :    }
 118    :  
 119  E :    output_pdb_path_ = cmd_line->GetSwitchValuePath("output-pdb");
 120  E :    order_file_path_ = AbsolutePath(cmd_line->GetSwitchValuePath("order-file"));
 121  E :    no_augment_pdb_ = cmd_line->HasSwitch("no-augment-pdb");
 122  E :    compress_pdb_ = cmd_line->HasSwitch("compress-pdb");
 123  E :    no_strip_strings_ = cmd_line->HasSwitch("no-strip-strings");
 124  E :    output_metadata_ = !cmd_line->HasSwitch("no-metadata");
 125  E :    overwrite_ = cmd_line->HasSwitch("overwrite");
 126  E :    basic_blocks_ = cmd_line->HasSwitch("basic-blocks");
 127  E :    exclude_bb_padding_ = cmd_line->HasSwitch("exclude-bb-padding");
 128    :  
 129    :    // The --output-image argument is required.
 130  E :    if (output_image_path_.empty()) {
 131  E :      return Usage(cmd_line, "You must specify --output-image.");
 132    :    }
 133    :  
 134    :    // Ensure that we have an input-image, either explicity specified, or to be
 135    :    // taken from an order file.
 136  E :    if (input_image_path_.empty() && order_file_path_.empty()) {
 137    :      return Usage(
 138    :          cmd_line,
 139  E :          "You must specify --input-image if --order-file is not given.");
 140    :    }
 141    :  
 142    :    // Parse the random seed, if given. Note that the --seed and --order-file
 143    :    // arguments are mutually exclusive.
 144  E :    if (cmd_line->HasSwitch("seed")) {
 145  E :      if (cmd_line->HasSwitch("order-file")) {
 146    :        return Usage(cmd_line,
 147  i :                     "The seed and order-file arguments are mutually exclusive.");
 148    :      }
 149  E :      std::wstring seed_str(cmd_line->GetSwitchValueNative("seed"));
 150  E :      if (!ParseUInt32(seed_str, &seed_))
 151  i :        return Usage(cmd_line, "Invalid seed value.");
 152  E :    }
 153    :  
 154    :    // Parse the padding argument.
 155  E :    if (cmd_line->HasSwitch("padding")) {
 156  E :      std::wstring padding_str(cmd_line->GetSwitchValueNative("padding"));
 157  E :      if (!ParsePadding(padding_str, &padding_))
 158  i :        return Usage(cmd_line, "Invalid padding value.");
 159  E :    }
 160    :  
 161  E :    return true;
 162  E :  }
 163    :  
 164  E :  bool RelinkApp::SetUp() {
 165  E :    if (input_image_path_.empty()) {
 166  E :      DCHECK(!order_file_path_.empty());
 167    :      if (!reorder::Reorderer::Order::GetOriginalModulePath(order_file_path_,
 168  E :                                                            &input_image_path_)) {
 169  E :        LOG(ERROR) << "Unable to infer input-image.";
 170  E :        return false;
 171    :      }
 172    :  
 173  i :      LOG(INFO) << "Inferring input DLL path from order file: "
 174    :                << input_image_path_.value();
 175    :    }
 176    :  
 177  E :    DCHECK(!input_image_path_.empty());
 178  E :    DCHECK(!output_image_path_.empty());
 179  E :    DCHECK(order_file_path_.empty() || seed_ == 0);
 180    :  
 181  E :    return true;
 182  E :  }
 183    :  
 184  E :  int RelinkApp::Run() {
 185  E :    pe::PERelinker relinker;
 186  E :    relinker.set_input_path(input_image_path_);
 187  E :    relinker.set_input_pdb_path(input_pdb_path_);
 188  E :    relinker.set_output_path(output_image_path_);
 189  E :    relinker.set_output_pdb_path(output_pdb_path_);
 190  E :    relinker.set_padding(padding_);
 191  E :    relinker.set_add_metadata(output_metadata_);
 192  E :    relinker.set_allow_overwrite(overwrite_);
 193  E :    relinker.set_augment_pdb(!no_augment_pdb_);
 194  E :    relinker.set_compress_pdb(compress_pdb_);
 195  E :    relinker.set_strip_strings(!no_strip_strings_);
 196    :  
 197    :    // Initialize the relinker. This does the decomposition, etc.
 198  E :    if (!relinker.Init()) {
 199  i :      LOG(ERROR) << "Failed to initialize relinker.";
 200  i :      return 1;
 201    :    }
 202    :  
 203    :    // Transforms that may be used.
 204  E :    scoped_ptr<pe::transforms::ExplodeBasicBlocksTransform> bb_explode;
 205  E :    scoped_ptr<reorder::transforms::BasicBlockLayoutTransform> bb_layout;
 206    :  
 207    :    // Orderers that may be used.
 208  E :    reorder::Reorderer::Order order;
 209  E :    scoped_ptr<block_graph::orderers::OriginalOrderer> orig_orderer;
 210  E :    scoped_ptr<block_graph::BlockGraphOrdererInterface> orderer;
 211    :  
 212    :    // If an order file is provided we are performing an explicit ordering.
 213  E :    if (!order_file_path_.empty()) {
 214    :      if (!order.LoadFromJSON(relinker.input_pe_file(),
 215    :                              relinker.input_image_layout(),
 216  E :                              order_file_path_)) {
 217  i :        LOG(ERROR) << "Failed to load order file: " << order_file_path_.value();
 218  i :        return 1;
 219    :      }
 220    :  
 221    :      // Allocate a BB layout transform. This applies the basic block portion of
 222    :      // the order specification. It will modify it in place so that it is ready
 223    :      // to be used by the ExplicitOrderer to finish the job.
 224  E :      bb_layout.reset(new reorder::transforms::BasicBlockLayoutTransform(&order));
 225  E :      relinker.AppendTransform(bb_layout.get());
 226    :  
 227    :      // Append an OriginalOrderer to the relinker. We do this so that the
 228    :      // original order is preserved entirely for sections that are not
 229    :      // fully specified by the order file, and therefore not ordered by the
 230    :      // ExplicitOrderer.
 231  E :      orig_orderer.reset(new block_graph::orderers::OriginalOrderer());
 232  E :      relinker.AppendOrderer(orig_orderer.get());
 233    :  
 234    :      // Allocate an explicit orderer.
 235  E :      orderer.reset(new reorder::orderers::ExplicitOrderer(&order));
 236  E :    } else {
 237    :      // No order file was provided, so we're doing a random ordering.
 238    :  
 239    :      // If we've been asked to go down to the basic block level, then we want to
 240    :      // use an explode basic blocks transform so that we randomize the entire
 241    :      // image at the BB level.
 242  E :      if (basic_blocks_) {
 243  E :        bb_explode.reset(new pe::transforms::ExplodeBasicBlocksTransform());
 244  E :        bb_explode->set_exclude_padding(exclude_bb_padding_);
 245  E :        relinker.AppendTransform(bb_explode.get());
 246    :      }
 247    :  
 248    :      // Allocate the random block orderer.
 249  E :      orderer.reset(new block_graph::orderers::RandomOrderer(true, seed_));
 250    :    }
 251    :  
 252    :    // Append the orderer to the relinker.
 253  E :    relinker.AppendOrderer(orderer.get());
 254    :  
 255    :    // Perform the actual relink.
 256  E :    if (!relinker.Relink()) {
 257  i :      LOG(ERROR) << "Unable to relink input image.";
 258  i :      return 1;
 259    :    }
 260    :  
 261  E :    return 0;
 262  E :  }
 263    :  
 264    :  bool RelinkApp::Usage(const CommandLine* cmd_line,
 265  E :                        const base::StringPiece& message) const {
 266  E :    if (!message.empty()) {
 267  E :      ::fwrite(message.data(), 1, message.length(), err());
 268  E :      ::fprintf(err(), "\n\n");
 269    :    }
 270    :  
 271    :    ::fprintf(err(),
 272    :              kUsageFormatStr,
 273  E :              cmd_line->GetProgram().BaseName().value().c_str());
 274    :  
 275  E :    return false;
 276  E :  }
 277    :  
 278    :  }  // namespace relink

Coverage information generated Thu Mar 14 11:53:36 2013.