Coverage for /Syzygy/block_graph/basic_block_decomposer.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
94.0%5325660.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    :  // Implementation of basic block decomposer.
  16    :  
  17    :  #include "syzygy/block_graph/basic_block_decomposer.h"
  18    :  
  19    :  #include <algorithm>
  20    :  #include <vector>
  21    :  
  22    :  #include "base/logging.h"
  23    :  #include "base/strings/stringprintf.h"
  24    :  #include "syzygy/block_graph/basic_block.h"
  25    :  #include "syzygy/block_graph/basic_block_subgraph.h"
  26    :  #include "syzygy/block_graph/block_graph.h"
  27    :  #include "syzygy/block_graph/block_util.h"
  28    :  
  29    :  #include "mnemonics.h"  // NOLINT
  30    :  
  31    :  namespace block_graph {
  32    :  
  33    :  namespace {
  34    :  
  35    :  using block_graph::BasicBlock;
  36    :  using block_graph::BasicBlockReference;
  37    :  using block_graph::BasicBlockReferrer;
  38    :  using block_graph::BasicBlockSubGraph;
  39    :  using block_graph::BlockGraph;
  40    :  using block_graph::Instruction;
  41    :  using block_graph::Successor;
  42    :  using core::Disassembler;
  43    :  
  44    :  typedef BlockGraph::Block Block;
  45    :  typedef BlockGraph::Offset Offset;
  46    :  typedef BlockGraph::Size Size;
  47    :  typedef core::AddressSpace<Offset, size_t, BasicBlock*> BBAddressSpace;
  48    :  typedef BBAddressSpace::Range Range;
  49    :  typedef BBAddressSpace::RangeMap RangeMap;
  50    :  typedef BBAddressSpace::RangeMapConstIter RangeMapConstIter;
  51    :  typedef BBAddressSpace::RangeMapIter RangeMapIter;
  52    :  
  53    :  const size_t kPointerSize = BlockGraph::Reference::kMaximumSize;
  54    :  
  55    :  // We use a (somewhat) arbitrary value as the disassembly address for a block
  56    :  // so we can tell the difference between a reference to the beginning of the
  57    :  // block (offset=0) and a null address.
  58    :  const size_t kDisassemblyAddress = 65536;
  59    :  
  60    :  // Look up the reference made from an instruction's byte range within the
  61    :  // given block. The reference should start AFTER the instruction starts
  62    :  // and there should be exactly 1 reference in the byte range.
  63    :  // Returns true if the reference was found, false otherwise.
  64    :  bool GetReferenceOfInstructionAt(const Block* block,
  65    :                                   Offset instr_offset,
  66    :                                   Size instr_size,
  67  E :                                   BlockGraph::Reference* ref) {
  68  E :    DCHECK(block != NULL);
  69  E :    DCHECK_LE(0, instr_offset);
  70  E :    DCHECK_LT(0U, instr_size);
  71  E :    DCHECK(ref != NULL);
  72    :  
  73    :    // Find the first reference following the instruction offset.
  74    :    Block::ReferenceMap::const_iterator ref_iter =
  75  E :        block->references().upper_bound(instr_offset);
  76    :  
  77    :    // If no reference is found then we're done.
  78  E :    if (ref_iter == block->references().end())
  79  E :      return false;
  80    :  
  81    :    // If the reference occurs outside the instruction then we're done.
  82  E :    Offset next_instr_offset = instr_offset + instr_size;
  83  E :    if (ref_iter->first >= next_instr_offset)
  84  E :      return false;
  85    :  
  86    :    // Otherwise, the reference should fit into the instruction.
  87    :    CHECK_LE(static_cast<size_t>(next_instr_offset),
  88  E :             ref_iter->first + ref_iter->second.size());
  89    :  
  90    :    // And it should be the only reference in the instruction.
  91  E :    if (ref_iter != block->references().begin()) {
  92  E :      Block::ReferenceMap::const_iterator prev_iter = ref_iter;
  93  E :      --prev_iter;
  94    :      CHECK_GE(static_cast<size_t>(instr_offset),
  95  E :               prev_iter->first + prev_iter->second.size());
  96    :    }
  97  E :    Block::ReferenceMap::const_iterator next_iter = ref_iter;
  98  E :    ++next_iter;
  99    :    CHECK(next_iter == block->references().end() ||
 100  E :          next_iter->first >= next_instr_offset);
 101    :  
 102  E :    *ref = ref_iter->second;
 103  E :    return true;
 104  E :  }
 105    :  
 106    :  // Transfer instructions from original to tail, starting with the instruction
 107    :  // starting at offset.
 108    :  bool SplitInstructionListAt(Offset offset,
 109    :                              BasicBlock::Instructions* original,
 110  E :                              BasicBlock::Instructions* tail) {
 111  E :    DCHECK(original != NULL);
 112  E :    DCHECK(tail != NULL && tail->empty());
 113    :  
 114  E :    BasicBlock::Instructions::iterator it(original->begin());
 115  E :    while (offset > 0 && it != original->end()) {
 116  E :      offset -= it->size();
 117  E :      ++it;
 118  E :    }
 119    :  
 120    :    // Did we terminate at an instruction boundary?
 121  E :    if (offset != 0)
 122  i :      return false;
 123    :  
 124  E :    tail->splice(tail->end(), *original, it, original->end());
 125  E :    return true;
 126  E :  }
 127    :  
 128    :  }  // namespace
 129    :  
 130    :  BasicBlockDecomposer::BasicBlockDecomposer(const BlockGraph::Block* block,
 131    :                                             BasicBlockSubGraph* subgraph)
 132    :      : block_(block),
 133    :        subgraph_(subgraph),
 134    :        current_block_start_(0),
 135    :        check_decomposition_results_(true),
 136  E :        contains_unsupported_instructions_(false) {
 137    :    // TODO(rogerm): Once we're certain this is stable for all input binaries
 138    :    //     turn on check_decomposition_results_ by default only ifndef NDEBUG.
 139  E :    DCHECK(block != NULL);
 140  E :    DCHECK(block->type() == BlockGraph::CODE_BLOCK);
 141    :  
 142    :    // If no subgraph was provided then use a scratch one.
 143  E :    if (subgraph == NULL) {
 144  E :      scratch_subgraph_.reset(new BasicBlockSubGraph());
 145  E :      subgraph_ = scratch_subgraph_.get();
 146    :    }
 147  E :  }
 148    :  
 149  E :  bool BasicBlockDecomposer::Decompose() {
 150  E :    DCHECK(subgraph_->basic_blocks().empty());
 151  E :    DCHECK(subgraph_->block_descriptions().empty());
 152  E :    DCHECK(original_address_space_.empty());
 153  E :    subgraph_->set_original_block(block_);
 154    :  
 155  E :    bool disassembled = Disassemble();
 156  E :    if (contains_unsupported_instructions_) {
 157  E :      CHECK(!disassembled);
 158  E :      return false;
 159    :    }
 160  E :    CHECK(disassembled);
 161    :  
 162    :    // Don't bother with the following bookkeeping work if the results aren't
 163    :    // being looked at.
 164  E :    if (scratch_subgraph_.get() != NULL)
 165  E :      return true;
 166    :  
 167    :    // TODO(cseri): Decomposition of blocks with nonzero alignment offset is not
 168    :    // yet supported.
 169  E :    CHECK_EQ(0, block_->alignment_offset());
 170    :  
 171    :    typedef BasicBlockSubGraph::BlockDescription BlockDescription;
 172  E :    subgraph_->block_descriptions().push_back(BlockDescription());
 173  E :    BlockDescription& desc = subgraph_->block_descriptions().back();
 174  E :    desc.name = block_->name();
 175  E :    desc.compiland_name = block_->compiland_name();
 176  E :    desc.type = block_->type();
 177  E :    desc.alignment = block_->alignment();
 178  E :    desc.padding_before = block_->padding_before();
 179  E :    desc.attributes = block_->attributes();
 180  E :    desc.section = block_->section();
 181    :  
 182    :    // Add the basic blocks to the block descriptor.
 183  E :    Offset offset = 0;
 184  E :    RangeMapConstIter it = original_address_space_.begin();
 185  E :    for (; it != original_address_space_.end(); ++it) {
 186  E :      DCHECK_EQ(it->first.start(), offset);
 187  E :      desc.basic_block_order.push_back(it->second);
 188    :  
 189    :      // Any data basic blocks (jump and case tables) with 0 mod 4 alignment
 190    :      // are marked so that the alignment is preserved by the block builder.
 191    :      if (desc.alignment >= kPointerSize &&
 192    :          it->second->type() == BasicBlock::BASIC_DATA_BLOCK &&
 193  E :          (offset % kPointerSize) == 0) {
 194  E :        it->second->set_alignment(kPointerSize);
 195    :      }
 196    :  
 197  E :      offset += it->first.size();
 198  E :    }
 199    :  
 200  E :    return true;
 201  E :  }
 202    :  
 203    :  bool BasicBlockDecomposer::DecodeInstruction(Offset offset,
 204    :                                               Offset code_end_offset,
 205  E :                                               Instruction* instruction) const {
 206    :    // The entire offset range should fall within the extent of block_ and the
 207    :    // output instruction pointer must not be NULL.
 208  E :    DCHECK_LE(0, offset);
 209  E :    DCHECK_LT(offset, code_end_offset);
 210  E :    DCHECK_LE(static_cast<Size>(code_end_offset), block_->size());
 211  E :    DCHECK(instruction != NULL);
 212    :  
 213    :    // Decode the instruction.
 214  E :    const uint8* buffer = block_->data() + offset;
 215  E :    size_t max_length = code_end_offset - offset;
 216  E :    if (!Instruction::FromBuffer(buffer, max_length, instruction)) {
 217  i :      VLOG(1) << "Failed to decode instruction at offset " << offset
 218    :              << " of block '" << block_->name() << "'.";
 219    :  
 220    :      // Dump the bytes to aid in debugging.
 221  i :      std::string dump;
 222  i :      size_t dump_length = std::min(max_length, Instruction::kMaxSize);
 223  i :      for (size_t i = 0; i < dump_length; ++i)
 224  i :        base::StringAppendF(&dump, " %02X", buffer[i]);
 225  i :      VLOG(2) << ".text =" << dump << (dump_length < max_length ? "..." : ".");
 226    :  
 227    :      // Return false to indicate an error.
 228  i :      return false;
 229    :    }
 230    :  
 231  E :    VLOG(3) << "Disassembled " << instruction->GetName()
 232    :            << " instruction (" << instruction->size()
 233    :            << " bytes) at offset " << offset << ".";
 234    :  
 235    :    // Track the source range.
 236    :    instruction->set_source_range(
 237  E :        GetSourceRange(offset, instruction->size()));
 238    :  
 239    :    // If the block is labeled, preserve the label.
 240  E :    BlockGraph::Label label;
 241  E :    if (block_->GetLabel(offset, &label)) {
 242    :      // If this instruction has run into known data, then we have a problem!
 243  E :      CHECK(!label.has_attributes(BlockGraph::DATA_LABEL))
 244    :          << "Disassembling into data at offset " << offset << " of "
 245    :          << block_->name() << ".";
 246  E :      instruction->set_label(label);
 247    :    }
 248    :  
 249  E :    return true;
 250  E :  }
 251    :  
 252    :  BasicBlockDecomposer::SourceRange BasicBlockDecomposer::GetSourceRange(
 253  E :      Offset offset, Size size) const {
 254    :    // Find the source range for the original bytes. We may not have a data
 255    :    // range for bytes that were synthesized in other transformations. As a
 256    :    // rule, however, there should be a covered data range for each instruction,
 257    :    // successor, that relates back to the original image.
 258    :    const Block::SourceRanges::RangePair* range_pair =
 259  E :        block_->source_ranges().FindRangePair(offset, size);
 260    :    // Return an empty range if we found nothing.
 261  E :    if (range_pair == NULL)
 262  E :      return SourceRange();
 263    :  
 264  E :    const Block::DataRange& data_range = range_pair->first;
 265  E :    const Block::SourceRange& source_range = range_pair->second;
 266  E :    if (offset == data_range.start() && size == data_range.size()) {
 267    :      // We match a data range exactly, so let's use the entire
 268    :      // matching source range.
 269  E :      return source_range;
 270    :    }
 271    :  
 272    :    // The data range doesn't match exactly, so let's slice the corresponding
 273    :    // source range. The assumption here is that no transformation will ever
 274    :    // slice the data or source ranges for an instruction, so we should always
 275    :    // have a covering data and source ranges.
 276  E :    DCHECK_GE(offset, data_range.start());
 277  E :    DCHECK_LE(offset + size, data_range.start() + data_range.size());
 278    :  
 279  E :    Offset start_offs = offset - data_range.start();
 280  E :    return SourceRange(source_range.start() + start_offs, size);
 281  E :  }
 282    :  
 283    :  bool BasicBlockDecomposer::FindBasicBlock(Offset offset,
 284    :                                            BasicBlock** basic_block,
 285  E :                                            Range* range) const {
 286  E :    DCHECK_LE(0, offset);
 287  E :    DCHECK(basic_block != NULL);
 288  E :    DCHECK(range != NULL);
 289  E :    DCHECK(subgraph_->original_block() != NULL);
 290  E :    DCHECK_GE(subgraph_->original_block()->size(), static_cast<size_t>(offset));
 291    :  
 292    :    RangeMapConstIter bb_iter =
 293  E :        original_address_space_.FindFirstIntersection(Range(offset, 1));
 294    :  
 295  E :    if (bb_iter == original_address_space_.end())
 296  i :      return false;
 297    :  
 298  E :    *basic_block = bb_iter->second;
 299  E :    *range = bb_iter->first;
 300  E :    return true;
 301  E :  }
 302    :  
 303  E :  BasicBlock* BasicBlockDecomposer::GetBasicBlockAt(Offset offset) const {
 304  E :    DCHECK_LE(0, offset);
 305  E :    DCHECK(subgraph_->original_block() != NULL);
 306  E :    DCHECK_GE(subgraph_->original_block()->size(), static_cast<size_t>(offset));
 307    :  
 308  E :    BasicBlock* bb = NULL;
 309  E :    Range range;
 310  E :    CHECK(FindBasicBlock(offset, &bb, &range));
 311  E :    DCHECK(bb != NULL);
 312  E :    DCHECK_EQ(offset, range.start());
 313  E :    return bb;
 314  E :  }
 315    :  
 316  E :  void BasicBlockDecomposer::InitJumpTargets(Offset code_end_offset) {
 317  E :    DCHECK_LE(static_cast<Size>(code_end_offset), block_->size());
 318    :  
 319    :    // Make sure the jump target set is empty.
 320  E :    jump_targets_.clear();
 321    :  
 322    :    // For each referrer, check if it references code. If so, it's a jump target.
 323    :    BlockGraph::Block::ReferrerSet::const_iterator ref_iter =
 324  E :        block_->referrers().begin();
 325  E :    for (; ref_iter != block_->referrers().end(); ++ref_iter) {
 326  E :      BlockGraph::Reference ref;
 327  E :      bool found = ref_iter->first->GetReference(ref_iter->second, &ref);
 328  E :      DCHECK(found);
 329  E :      DCHECK_EQ(block_, ref.referenced());
 330  E :      DCHECK_LE(0, ref.base());
 331  E :      DCHECK_LE(static_cast<size_t>(ref.base()), block_->size());
 332    :  
 333    :      // Ignore references to the data portion of the block.
 334  E :      if (ref.base() >= code_end_offset)
 335  E :        continue;
 336    :  
 337  E :      jump_targets_.insert(ref.base());
 338  E :    }
 339  E :  }
 340    :  
 341    :  bool BasicBlockDecomposer::HandleInstruction(const Instruction& instruction,
 342  E :                                               Offset offset) {
 343    :    // We do not handle the SYS* instructions. These should ONLY occur inside
 344    :    // the OS system libraries, mediated by an OS system call. We expect that
 345    :    // they NEVER occur in application code.
 346  E :    if (instruction.IsSystemCall()) {
 347  i :      VLOG(1) << "Encountered an unexpected " << instruction.GetName()
 348    :              << " instruction at offset " << offset << " of block '"
 349    :              << block_->name() << "'.";
 350  i :      return false;
 351    :    }
 352    :  
 353    :    // Calculate the offset of the next instruction. We'll need this if this
 354    :    // instruction marks the end of a basic block.
 355  E :    Offset next_instruction_offset = offset + instruction.size();
 356    :  
 357    :    // If the instruction is not a branch then it needs to be appended to the
 358    :    // current basic block... which we close if the instruction is a return or
 359    :    // a call to a non-returning function.
 360  E :    if (!instruction.IsBranch()) {
 361  E :      current_instructions_.push_back(instruction);
 362  E :      if (instruction.IsReturn()) {
 363  E :        EndCurrentBasicBlock(next_instruction_offset);
 364  E :      } else if (instruction.IsCall()) {
 365  E :        BlockGraph::Reference ref;
 366    :        bool found = GetReferenceOfInstructionAt(
 367  E :            block_, offset, instruction.size(), &ref);
 368    :        if (found && Instruction::IsCallToNonReturningFunction(
 369  E :                instruction.representation(), ref.referenced(), ref.offset())) {
 370  E :          EndCurrentBasicBlock(next_instruction_offset);
 371    :        }
 372    :      }
 373  E :      return true;
 374    :    }
 375    :  
 376    :    // If the branch is not PC-Relative then it also needs to be appended to
 377    :    // the current basic block... which we then close.
 378  E :    if (!instruction.HasPcRelativeOperand(0)) {
 379  E :      current_instructions_.push_back(instruction);
 380  E :      EndCurrentBasicBlock(next_instruction_offset);
 381  E :      return true;
 382    :    }
 383    :  
 384    :    // Otherwise, we're dealing with a branch whose destination is explicit.
 385  E :    DCHECK(instruction.IsBranch());
 386  E :    DCHECK(instruction.HasPcRelativeOperand(0));
 387    :  
 388    :    // Make sure we understand the branching condition. If we don't, then
 389    :    // there's an instruction we have failed (or are unable) to consider.
 390    :    // This failure is by design, as we can't represent instructions like
 391    :    // JECZX or LOOPZ, as discussed in basic_block.cc.
 392    :    Successor::Condition condition = Successor::OpCodeToCondition(
 393  E :        instruction.opcode());
 394  E :    if (condition == Successor::kInvalidCondition) {
 395  E :      VLOG(1) << "Received unknown condition for branch instruction: "
 396    :              << instruction.GetName() << ".";
 397  E :      contains_unsupported_instructions_ = true;
 398  E :      return false;
 399    :    }
 400    :  
 401    :    // If this is a conditional branch add the inverse conditional successor
 402    :    // to represent the fall-through. If we don't understand the inverse, then
 403    :    // there's an instruction we have failed to consider.
 404  E :    if (instruction.IsConditionalBranch()) {
 405    :      Successor::Condition inverse_condition =
 406  E :          Successor::InvertCondition(condition);
 407  E :      CHECK_NE(Successor::kInvalidCondition, inverse_condition)
 408    :          << "Non-invertible condition seen for branch instruction: "
 409    :          << instruction.GetName() << ".";
 410    :  
 411    :      // Create an (unresolved) successor pointing to the next instruction.
 412    :      BasicBlockReference ref(BlockGraph::PC_RELATIVE_REF,
 413    :                              1,  // The size is irrelevant in successors.
 414    :                              const_cast<Block*>(block_),
 415    :                              next_instruction_offset,
 416  E :                              next_instruction_offset);
 417  E :      current_successors_.push_front(Successor(inverse_condition, ref, 0));
 418  E :      jump_targets_.insert(next_instruction_offset);
 419  E :    }
 420    :  
 421    :    // Attempt to figure out where the branch is going by finding a
 422    :    // reference inside the instruction's byte range.
 423  E :    BlockGraph::Reference ref;
 424    :    bool found = GetReferenceOfInstructionAt(
 425  E :        block_, offset, instruction.size(), &ref);
 426    :  
 427    :    // If a reference was found, prefer its destination information to the
 428    :    // information conveyed by the bytes in the instruction. This should
 429    :    // handle all inter-block jumps (thunks, tail-call elimination, etc).
 430    :    // Otherwise, create a reference into the current block.
 431  E :    if (found) {
 432    :      // This is an explicit branching instruction so we expect the reference to
 433    :      // be direct.
 434  E :      if (!ref.IsDirect()) {
 435  i :        VLOG(1) << "Encountered an explicit control flow instruction containing "
 436    :                << "an indirect reference.";
 437  i :        return false;
 438    :      }
 439  E :    } else {
 440    :      Offset target_offset =
 441  E :          next_instruction_offset + instruction.representation().imm.addr;
 442    :  
 443    :      // If we don't have a reference (coming from a fixup) for a PC-relative jump
 444    :      // then we expect its destination to be in the block. We only see otherwise
 445    :      // in assembly generated code where section contributions don't correspond
 446    :      // to entire function bodies.
 447    :      if (target_offset < 0 ||
 448  E :          static_cast<Size>(target_offset) >= block_->size()) {
 449  i :        VLOG(1) << "Unexpected PC-relative target offset is external to block.";
 450  i :        return false;
 451    :      }
 452    :  
 453    :      ref = BlockGraph::Reference(BlockGraph::PC_RELATIVE_REF,
 454    :                                  1,  // Size is irrelevant in successors.
 455    :                                  const_cast<Block*>(block_),
 456    :                                  target_offset,
 457  E :                                  target_offset);
 458    :    }
 459    :  
 460    :    // If the reference points to the current block, track the target offset.
 461  E :    if (ref.referenced() == block_)
 462  E :      jump_targets_.insert(ref.offset());
 463    :  
 464    :    // Create the successor, preserving the source range and label.
 465    :    BasicBlockReference bb_ref(
 466  E :        ref.type(), ref.size(), ref.referenced(), ref.offset(), ref.base());
 467  E :    Successor succ(condition, bb_ref, instruction.size());
 468  E :    succ.set_source_range(instruction.source_range());
 469  E :    succ.set_label(instruction.label());
 470  E :    current_successors_.push_front(succ);
 471    :  
 472    :    // Having just branched, we need to end the current basic block.
 473  E :    EndCurrentBasicBlock(next_instruction_offset);
 474  E :    return true;
 475  E :  }
 476    :  
 477  E :  bool BasicBlockDecomposer::EndCurrentBasicBlock(Offset end_offset) {
 478    :    // We have reached the end of the current walk or we handled a conditional
 479    :    // branch. Let's mark this as the end of a basic block.
 480  E :    int basic_block_size = end_offset - current_block_start_;
 481  E :    DCHECK_LT(0, basic_block_size);
 482    :    if (!InsertBasicBlockRange(current_block_start_,
 483    :                               basic_block_size,
 484  E :                               BasicBlock::BASIC_CODE_BLOCK)) {
 485  i :      return false;
 486    :    }
 487    :  
 488    :    // Remember the end offset as the start of the next basic block.
 489  E :    current_block_start_ = end_offset;
 490  E :    return true;
 491  E :  }
 492    :  
 493  E :  bool BasicBlockDecomposer::GetCodeRangeAndCreateDataBasicBlocks(Offset* end) {
 494  E :    DCHECK_NE(reinterpret_cast<Offset*>(NULL), end);
 495    :  
 496  E :    *end = 0;
 497    :  
 498    :    // By default, we assume the entire block is code.
 499  E :    Offset code_end = block_->size();
 500    :  
 501    :    // Iterate over all labels, looking for data labels.
 502    :    BlockGraph::Block::LabelMap::const_reverse_iterator it =
 503  E :        block_->labels().rbegin();
 504  E :    bool saw_non_data_label = false;
 505  E :    for (; it != block_->labels().rend(); ++it) {
 506  E :      const BlockGraph::Label& label = it->second;
 507  E :      if (label.has_attributes(BlockGraph::DATA_LABEL)) {
 508    :        // There should never be data labels beyond the end of the block.
 509  E :        if (it->first >= static_cast<Offset>(block_->size())) {
 510  i :          VLOG(1) << "Encountered a data label at offset " << it->first
 511    :                  << "of block \"" << block_->name() << "\" of size "
 512    :                  << block_->size() << ".";
 513  i :          return false;
 514    :        }
 515    :  
 516    :        // If a non-data label was already encountered, and now there's another
 517    :        // data label then bail: the block does not respect the 'code first,
 518    :        // data second' supported layout requirement.
 519  E :        if (saw_non_data_label) {
 520  i :          VLOG(1) << "Block \"" << block_->name() << "\" has an unsupported "
 521    :                  << "code-data layout.";
 522  i :          VLOG(1) << "Unexpected data label at offset " << it->first << ".";
 523  i :          return false;
 524    :        }
 525    :  
 526    :        // Create a data block and update the end-of-code offset. This should
 527    :        // never fail because this is the first time blocks are being created and
 528    :        // they are strictly non-overlapping by the iteration logic of this
 529    :        // function.
 530  E :        size_t size = code_end - it->first;
 531    :        CHECK(InsertBasicBlockRange(it->first, size,
 532  E :                                    BasicBlock::BASIC_DATA_BLOCK));
 533  E :        code_end = it->first;
 534  E :      } else {
 535    :        // We ignore the debug-end label, as it can come after block data.
 536  E :        if (label.attributes() == BlockGraph::DEBUG_END_LABEL)
 537  E :          continue;
 538    :  
 539    :        // Remember that a non-data label was seen. No further data labels should
 540    :        // be encountered.
 541  E :        saw_non_data_label = true;
 542    :      }
 543  E :    }
 544    :  
 545  E :    *end = code_end;
 546    :  
 547  E :    return true;
 548  E :  }
 549    :  
 550  E :  bool BasicBlockDecomposer::ParseInstructions() {
 551    :    // Find the beginning and ending offsets of code bytes within the block.
 552  E :    Offset code_end_offset = 0;
 553  E :    if (!GetCodeRangeAndCreateDataBasicBlocks(&code_end_offset))
 554  i :      return false;
 555    :  
 556    :    // Initialize jump_targets_ to include un-discoverable targets.
 557  E :    InitJumpTargets(code_end_offset);
 558    :  
 559    :    // Disassemble the instruction stream into rudimentary basic blocks.
 560  E :    Offset offset = 0;
 561  E :    current_block_start_ = offset;
 562  E :    while (offset < code_end_offset) {
 563    :      // Decode the next instruction.
 564  E :      Instruction instruction;
 565  E :      if (!DecodeInstruction(offset, code_end_offset, &instruction))
 566  i :        return false;
 567    :  
 568    :      // Handle the decoded instruction.
 569  E :      if (!HandleInstruction(instruction, offset))
 570  E :        return false;
 571    :  
 572    :      // Advance the instruction offset.
 573  E :      offset += instruction.size();
 574  E :    }
 575    :  
 576    :    // If we get here then we must have successfully consumed the entire code
 577    :    // range; otherwise, we should have failed to decode a partial instruction.
 578  E :    CHECK_EQ(offset, code_end_offset);
 579    :  
 580    :    // If the last bb we were working on didn't end with a RET or branch then
 581    :    // we need to close it now. We can detect this if the current_block_start_
 582    :    // does not match the current (end) offset.
 583  E :    if (current_block_start_ != code_end_offset)
 584  E :      EndCurrentBasicBlock(code_end_offset);
 585    :  
 586  E :    return true;
 587  E :  }
 588    :  
 589  E :  bool BasicBlockDecomposer::Disassemble() {
 590    :    // Parse the code bytes into instructions and rudimentary basic blocks.
 591  E :    if (!ParseInstructions())
 592  E :      return false;
 593    :  
 594    :    // Everything below this point is simply book-keeping that can't fail. These
 595    :    // can safely be skipped in a dry-run.
 596  E :    if (scratch_subgraph_.get() != NULL)
 597  E :      return true;
 598    :  
 599    :    // Split the basic blocks at branch targets.
 600  E :    SplitCodeBlocksAtBranchTargets();
 601    :  
 602    :    // At this point we have basic blocks for all code and data. Now create a
 603    :    // basic-block to represent the end of the block. This will potentially carry
 604    :    // labels and references beyond the end of the block.
 605  E :    CHECK(InsertBasicBlockRange(block_->size(), 0, BasicBlock::BASIC_END_BLOCK));
 606    :  
 607    :    // By this point, we should have basic blocks for all visited code.
 608  E :    CheckAllJumpTargetsStartABasicCodeBlock();
 609    :  
 610    :    // We should now have contiguous block ranges that cover every byte in the
 611    :    // macro block. Verify that this is so.
 612  E :    CheckHasCompleteBasicBlockCoverage();
 613    :  
 614    :    // We should have propagated all of the labels in the original block into
 615    :    // the basic-block subgraph.
 616  E :    CheckAllLabelsArePreserved();
 617    :  
 618    :    // Populate the referrers in the basic block data structures by copying
 619    :    // them from the original source block.
 620  E :    CopyExternalReferrers();
 621    :  
 622    :    // Populate the references in the basic block data structures by copying
 623    :    // them from the original source block. This does not handle the successor
 624    :    // references.
 625  E :    CopyReferences();
 626    :  
 627    :    // Wire up the basic-block successors. These are not handled by
 628    :    // CopyReferences(), above.
 629  E :    ResolveSuccessors();
 630    :  
 631    :    // All the control flow we have derived should be valid.
 632  E :    CheckAllControlFlowIsValid();
 633    :  
 634    :    // Mark all unreachable code blocks as padding.
 635  E :    MarkUnreachableCodeAsPadding();
 636    :  
 637    :    // ... and we're done.
 638  E :    return true;
 639  E :  }
 640    :  
 641  E :  void BasicBlockDecomposer::CheckAllJumpTargetsStartABasicCodeBlock() const {
 642  E :    if (!check_decomposition_results_)
 643  i :      return;
 644    :  
 645  E :    JumpTargets::const_iterator offset_iter(jump_targets_.begin());
 646  E :    for (; offset_iter != jump_targets_.end(); ++offset_iter) {
 647    :      // The target basic-block should be a code basic-block.
 648  E :      BasicBlock* target_bb = GetBasicBlockAt(*offset_iter);
 649  E :      CHECK(target_bb != NULL);
 650  E :      CHECK_EQ(BasicBlock::BASIC_CODE_BLOCK, target_bb->type());
 651  E :    }
 652  E :  }
 653    :  
 654  E :  void BasicBlockDecomposer::CheckHasCompleteBasicBlockCoverage() const {
 655  E :    if (!check_decomposition_results_)
 656  i :      return;
 657    :  
 658    :    // Walk through the basic-block address space.
 659  E :    Offset next_start = 0;
 660  E :    RangeMapConstIter it(original_address_space_.begin());
 661  E :    for (; it != original_address_space_.end(); ++it) {
 662  E :      CHECK_EQ(it->first.start(), next_start);
 663  E :      CHECK_EQ(it->first.start(), it->second->offset());
 664    :  
 665  E :      size_t size = it->first.size();
 666    :  
 667  E :      BasicDataBlock* data_block = BasicDataBlock::Cast(it->second);
 668  E :      if (data_block != NULL) {
 669    :        // Data block's size should match the address segment exactly.
 670  E :        CHECK_EQ(size, data_block->size());
 671    :      }
 672    :  
 673  E :      BasicCodeBlock* code_block = BasicCodeBlock::Cast(it->second);
 674  E :      if (code_block != NULL) {
 675    :        // Code blocks may be short the trailing successor instruction.
 676    :        BasicCodeBlock::Successors::const_iterator succ_it(
 677  E :            code_block->successors().begin());
 678  E :        Size block_size = code_block->GetInstructionSize();
 679  E :        for (; succ_it != code_block->successors().end(); ++succ_it)
 680  E :          block_size += succ_it->instruction_size();
 681    :  
 682  E :        CHECK_GE(size, block_size);
 683    :      }
 684    :  
 685  E :      BasicEndBlock* end_block = BasicEndBlock::Cast(it->second);
 686  E :      if (end_block != NULL) {
 687  E :        CHECK_EQ(0u, end_block->size());
 688  E :        size = 0;
 689    :      }
 690    :  
 691    :      // The basic-block must have parsed as one of the fundamental types.
 692  E :      CHECK(data_block != NULL || code_block != NULL || end_block != NULL);
 693    :  
 694  E :      next_start += size;
 695  E :    }
 696    :  
 697    :    // At this point, if there were no gaps, next start will be the same as the
 698    :    // full size of the block we're decomposing.
 699  E :    CHECK_EQ(block_->size(), static_cast<size_t>(next_start));
 700  E :  }
 701    :  
 702  E :  void BasicBlockDecomposer::CheckAllControlFlowIsValid() const {
 703  E :    if (!check_decomposition_results_)
 704  i :      return;
 705    :  
 706    :    // Check that the subgraph is valid. This will make sure that the
 707    :    // instructions and successors generally make sense.
 708  E :    CHECK(subgraph_->IsValid());
 709    :  
 710    :    // The only thing left to check is that synthesized flow-through
 711    :    // successors refer to the adjacent basic-blocks.
 712  E :    RangeMapConstIter it(original_address_space_.begin());
 713  E :    for (; it != original_address_space_.end(); ++it) {
 714  E :      const BasicCodeBlock* bb = BasicCodeBlock::Cast(it->second);
 715  E :      if (bb == NULL)
 716  E :        continue;
 717    :  
 718  E :      const BasicBlock::Successors& successors = bb->successors();
 719    :  
 720    :      // There may be at most 2 successors.
 721  E :      switch (successors.size()) {
 722    :        case 0:
 723  E :          break;
 724    :  
 725    :        case 1:
 726    :          // If the successor is synthesized, then flow is from this basic-block
 727    :          // to the next adjacent one.
 728  E :          if (successors.back().instruction_size() == 0) {
 729  E :            RangeMapConstIter next(it);
 730  E :            ++next;
 731  E :            CHECK(next != original_address_space_.end());
 732  E :            CHECK_EQ(successors.back().reference().basic_block(), next->second);
 733    :          }
 734  E :          break;
 735    :  
 736    :        case 2: {
 737    :          // Exactly one of the successors should have been synthesized.
 738  E :          bool front_synthesized = successors.front().instruction_size() == 0;
 739  E :          bool back_synthesized = successors.back().instruction_size() == 0;
 740  E :          CHECK_NE(front_synthesized, back_synthesized);
 741    :  
 742    :          // The synthesized successor flows from this basic-block to the next
 743    :          // adjacent one.
 744    :          const Successor& synthesized =
 745  E :              front_synthesized ? successors.front() : successors.back();
 746  E :          RangeMapConstIter next(it);
 747  E :          ++next;
 748  E :          CHECK(next != original_address_space_.end());
 749  E :          CHECK_EQ(synthesized.reference().basic_block(), next->second);
 750  E :          break;
 751    :        }
 752    :  
 753    :        default:
 754  i :          NOTREACHED();
 755    :      }
 756  E :    }
 757  E :  }
 758    :  
 759  E :  void BasicBlockDecomposer::CheckAllLabelsArePreserved() const {
 760  E :    if (!check_decomposition_results_)
 761  i :      return;
 762    :  
 763  E :    const Block* original_block = subgraph_->original_block();
 764  E :    if (original_block == NULL)
 765  i :      return;
 766    :  
 767    :    // Remove any labels that fall *after* the given block. This can happen for
 768    :    // scope and debug-end labels when the function has no epilog. It is rare, but
 769    :    // has been observed in the wild.
 770    :    // TODO(chrisha): Find a way to preserve these. We may need the notion of an
 771    :    //     empty basic-block which gets assigned the label, or we may need to
 772    :    //     augment BBs/instructions with the ability to have two labels: one tied
 773    :    //     to the beginning of the object, and one to the end.
 774    :    Block::LabelMap::const_iterator it_past_block_end =
 775  E :        original_block->labels().lower_bound(original_block->size());
 776    :  
 777    :    // Grab a copy of the original labels (except any that are beyond the end of
 778    :    // the block data). We will be matching against these to ensure that they are
 779    :    // preserved in the BB decomposition.
 780    :    const Block::LabelMap original_labels(original_block->labels().begin(),
 781  E :                                          it_past_block_end);
 782  E :    if (original_labels.empty())
 783  E :      return;
 784    :  
 785    :    // A map to track which labels (by offset) have been found in the subgraph.
 786  E :    std::map<Offset, bool> labels_found;
 787    :  
 788    :    // Initialize the map of labels found in the subgraph.
 789  E :    Block::LabelMap::const_iterator label_iter = original_labels.begin();
 790  E :    for (; label_iter != original_labels.end(); ++label_iter)
 791  E :      labels_found.insert(std::make_pair(label_iter->first, false));
 792    :  
 793    :    // Walk through the subgraph and mark all of the labels found.
 794    :    BasicBlockSubGraph::BBCollection::const_iterator bb_iter =
 795  E :        subgraph_->basic_blocks().begin();
 796  E :    for (; bb_iter != subgraph_->basic_blocks().end(); ++bb_iter) {
 797  E :      const BasicDataBlock* data_block = BasicDataBlock::Cast(*bb_iter);
 798  E :      if (data_block != NULL) {
 799    :        // Account for labels attached to basic-blocks.
 800  E :        if (data_block->has_label()) {
 801  E :          BlockGraph::Label label;
 802  E :          CHECK(original_block->GetLabel(data_block->offset(), &label));
 803  E :          CHECK(data_block->label() == label);
 804  E :          labels_found[data_block->offset()] = true;
 805  E :        }
 806    :      }
 807    :  
 808  E :      const BasicCodeBlock* code_block = BasicCodeBlock::Cast(*bb_iter);
 809  E :      if (code_block != NULL) {
 810    :        // Account for labels attached to instructions.
 811    :        BasicBlock::Instructions::const_iterator inst_iter =
 812  E :            code_block->instructions().begin();
 813  E :        Offset inst_offset = code_block->offset();
 814  E :        for (; inst_iter != code_block->instructions().end(); ++inst_iter) {
 815  E :          const Instruction& inst = *inst_iter;
 816  E :          if (inst.has_label()) {
 817  E :            BlockGraph::Label label;
 818  E :            CHECK(original_block->GetLabel(inst_offset, &label));
 819  E :            CHECK(inst.label() == label);
 820  E :            labels_found[inst_offset] = true;
 821  E :          }
 822  E :          inst_offset += inst.size();
 823  E :        }
 824    :  
 825    :        // Account for labels attached to successors.
 826    :        BasicBlock::Successors::const_iterator succ_iter =
 827  E :            code_block->successors().begin();
 828  E :        for (; succ_iter != code_block->successors().end(); ++succ_iter) {
 829  E :          const Successor& succ = *succ_iter;
 830  E :          if (succ.has_label()) {
 831  E :            BlockGraph::Label label;
 832  E :            CHECK_NE(0U, succ.instruction_size());
 833  E :            CHECK(original_block->GetLabel(inst_offset, &label));
 834  E :            CHECK(succ.label() == label);
 835  E :            labels_found[inst_offset] = true;
 836  E :          }
 837  E :          inst_offset += succ.instruction_size();
 838  E :        }
 839    :      }
 840  E :    }
 841    :  
 842    :    // We should have the right number of labels_found (check if we added
 843    :    // something to the wrong place).
 844  E :    CHECK_EQ(original_labels.size(), labels_found.size());
 845    :  
 846    :    // Make sure all of the items in labels_found have been set to true.
 847  E :    std::map<Offset, bool>::const_iterator found_iter = labels_found.begin();
 848  E :    for (; found_iter != labels_found.end(); ++found_iter) {
 849  E :      CHECK(found_iter->second);
 850  E :    }
 851  E :  }
 852    :  
 853    :  bool BasicBlockDecomposer::InsertBasicBlockRange(Offset offset,
 854    :                                                   size_t size,
 855  E :                                                   BasicBlockType type) {
 856  E :    DCHECK_LE(0, offset);
 857  E :    DCHECK_LE(offset + size, block_->size());
 858  E :    DCHECK(type == BasicBlock::BASIC_CODE_BLOCK || current_instructions_.empty());
 859  E :    DCHECK(type == BasicBlock::BASIC_CODE_BLOCK || current_successors_.empty());
 860    :  
 861    :    // Find or create a name for this basic block. Reserve the label, if any,
 862    :    // to propagate to the basic block if there are no instructions in the
 863    :    // block to carry the label(s).
 864  E :    BlockGraph::Label label;
 865  E :    std::string basic_block_name;
 866  E :    bool have_label = block_->GetLabel(offset, &label);
 867  E :    if (have_label) {
 868  E :      basic_block_name = label.ToString();
 869  E :    } else {
 870    :      basic_block_name =
 871    :          base::StringPrintf("<%s+%04X-%s>",
 872    :                             block_->name().c_str(),
 873    :                             offset,
 874  E :                             BasicBlock::BasicBlockTypeToString(type));
 875    :    }
 876    :  
 877    :    // Pre-flight address space insertion to make sure there's no
 878    :    // pre-existing conflicting range.
 879  E :    Range byte_range(offset, size);
 880    :    if (original_address_space_.FindFirstIntersection(byte_range) !=
 881  E :            original_address_space_.end()) {
 882  i :      LOG(ERROR) << "Attempted to insert overlapping basic block.";
 883  i :      return false;
 884    :    }
 885    :  
 886  E :    if (type == BasicBlock::BASIC_CODE_BLOCK) {
 887  E :      DCHECK_LT(0u, size);
 888    :  
 889    :      // Create the code block.
 890  E :      BasicCodeBlock* code_block = subgraph_->AddBasicCodeBlock(basic_block_name);
 891  E :      if (code_block == NULL)
 892  i :        return false;
 893  E :      CHECK(original_address_space_.Insert(byte_range, code_block));
 894    :  
 895    :      // Populate code basic-block with instructions and successors.
 896  E :      code_block->set_offset(offset);
 897  E :      code_block->instructions().swap(current_instructions_);
 898  E :      code_block->successors().swap(current_successors_);
 899  E :    } else if (type == BasicBlock::BASIC_DATA_BLOCK) {
 900  E :      DCHECK_LT(0u, size);
 901    :  
 902    :      // Create the data block.
 903    :      BasicDataBlock* data_block = subgraph_->AddBasicDataBlock(
 904  E :          basic_block_name, size, block_->data() + offset);
 905  E :      if (data_block == NULL)
 906  i :        return false;
 907  E :      CHECK(original_address_space_.Insert(byte_range, data_block));
 908    :  
 909    :      // Capture the source range (if any) for the data block.
 910  E :      data_block->set_source_range(GetSourceRange(offset, size));
 911    :  
 912    :      // Data basic-blocks carry their labels at the head of the basic blocks.
 913    :      // A padding basic-block might also be labeled if the block contains
 914    :      // unreachable code (for example, INT3 or NOP instructions following a call
 915    :      // to a non-returning function).
 916  E :      data_block->set_offset(offset);
 917  E :      if (have_label)
 918  E :        data_block->set_label(label);
 919  E :    } else {
 920  E :      DCHECK_EQ(0u, size);
 921  E :      DCHECK_EQ(BasicBlock::BASIC_END_BLOCK, type);
 922    :  
 923    :      // Create the end block.
 924  E :      BasicEndBlock* end_block = subgraph_->AddBasicEndBlock();
 925  E :      if (end_block == NULL)
 926  i :        return false;
 927    :  
 928    :      // We insert the basic end block with a size of 1, as the address space
 929    :      // does not support empty blocks. However, the block itself has no length.
 930    :      // This is only for internal book-keeping, and does not affect the
 931    :      // BasicBlockSubGraph representation.
 932  E :      CHECK(original_address_space_.Insert(Range(offset, 1), end_block));
 933    :  
 934    :      // Set the offset and any labels.
 935  E :      end_block->set_offset(offset);
 936  E :      if (have_label)
 937  E :        end_block->set_label(label);
 938    :    }
 939    :  
 940  E :    return true;
 941  E :  }
 942    :  
 943  E :  void BasicBlockDecomposer::SplitCodeBlocksAtBranchTargets() {
 944  E :    JumpTargets::const_iterator jump_target_iter(jump_targets_.begin());
 945  E :    for (; jump_target_iter != jump_targets_.end(); ++jump_target_iter) {
 946    :      // Resolve the target basic-block.
 947  E :      Offset target_offset = *jump_target_iter;
 948  E :      BasicBlock* target_bb = NULL;
 949  E :      Range target_bb_range;
 950  E :      CHECK(FindBasicBlock(target_offset, &target_bb, &target_bb_range));
 951    :  
 952    :      // If we're jumping to the start of a basic block, there isn't any work
 953    :      // to do.
 954  E :      if (target_offset == target_bb_range.start())
 955  E :        continue;
 956    :  
 957    :      // The target must be a code block.
 958  E :      BasicCodeBlock* target_code_block = BasicCodeBlock::Cast(target_bb);
 959  E :      CHECK(target_code_block != NULL);
 960    :  
 961    :      // Otherwise, we have found a basic-block that we need to split.
 962    :      // Let's contract the range the original occupies in the basic-block
 963    :      // address space, then add a second block at the target offset.
 964  E :      size_t left_split_size = target_offset - target_bb_range.start();
 965  E :      bool removed = original_address_space_.Remove(target_bb_range);
 966  E :      DCHECK(removed);
 967    :  
 968  E :      Range left_split_range(target_bb_range.start(), left_split_size);
 969    :      bool inserted =
 970  E :          original_address_space_.Insert(left_split_range, target_code_block);
 971  E :      DCHECK(inserted);
 972    :  
 973    :      // Now we split up containing_range into two new ranges and replace
 974    :      // containing_range with the two new entries.
 975    :  
 976    :      // Slice the trailing half of the instructions and the successors
 977    :      // off the block.
 978  E :      DCHECK(current_instructions_.empty());
 979  E :      DCHECK(current_successors_.empty());
 980    :      bool split = SplitInstructionListAt(left_split_size,
 981    :                                          &target_code_block->instructions(),
 982  E :                                          &current_instructions_);
 983  E :      DCHECK(split);
 984  E :      target_code_block->successors().swap(current_successors_);
 985    :  
 986    :      // Set-up the flow-through successor for the first "half".
 987    :      BasicBlockReference ref(BlockGraph::PC_RELATIVE_REF,
 988    :                              1,  // Size is immaterial in successors.
 989    :                              const_cast<Block*>(block_),
 990    :                              target_offset,
 991  E :                              target_offset);
 992    :      target_code_block->successors().push_back(
 993  E :          Successor(Successor::kConditionTrue, ref, 0));
 994    :  
 995    :      // This shouldn't fail because the range used to exist, and we just resized
 996    :      // it.
 997    :      CHECK(InsertBasicBlockRange(target_offset,
 998    :                                  target_bb_range.size() - left_split_size,
 999  E :                                  target_code_block->type()));
1000  E :    }
1001  E :  }
1002    :  
1003  E :  void BasicBlockDecomposer::CopyExternalReferrers() {
1004  E :    const BlockGraph::Block::ReferrerSet& referrers = block_->referrers();
1005  E :    BlockGraph::Block::ReferrerSet::const_iterator iter = referrers.begin();
1006  E :    for (; iter != referrers.end(); ++iter) {
1007    :      // Find the reference this referrer record describes.
1008  E :      const BlockGraph::Block* referrer = iter->first;
1009  E :      DCHECK(referrer != NULL);
1010    :  
1011    :      // We only care about external referrers.
1012  E :      if (referrer == block_)
1013  E :        continue;
1014    :  
1015    :      // This is an external referrer. Find the reference in the referring block.
1016  E :      Offset source_offset = iter->second;
1017  E :      BlockGraph::Reference reference;
1018  E :      bool found = referrer->GetReference(source_offset, &reference);
1019  E :      DCHECK(found);
1020    :  
1021    :      // Find the basic block the reference refers to.
1022  E :      BasicBlock* target_bb = GetBasicBlockAt(reference.base());
1023  E :      DCHECK(target_bb != NULL);
1024    :  
1025    :      // Insert the referrer into the target bb's referrer set. Note that there
1026    :      // is no corresponding reference update to the referring block. The
1027    :      // target bb will track these so a BlockBuilder can properly update
1028    :      // the referrers when merging a subgraph back into the block-graph.
1029    :      bool inserted = target_bb->referrers().insert(
1030  E :          BasicBlockReferrer(referrer, source_offset)).second;
1031  E :      DCHECK(inserted);
1032  E :    }
1033  E :  }
1034    :  
1035    :  void BasicBlockDecomposer::CopyReferences(
1036  E :      Offset item_offset, Size item_size, BasicBlockReferenceMap* refs) {
1037  E :    DCHECK_LE(0, item_offset);
1038  E :    DCHECK_LT(0U, item_size);
1039  E :    DCHECK(refs != NULL);
1040    :  
1041    :    // Figure out the bounds of item.
1042  E :    BlockGraph::Offset end_offset = item_offset + item_size;
1043    :  
1044    :    // Get iterators encompassing all references within the bounds of item.
1045    :    BlockGraph::Block::ReferenceMap::const_iterator ref_iter =
1046  E :       block_->references().lower_bound(item_offset);
1047    :    BlockGraph::Block::ReferenceMap::const_iterator end_iter =
1048  E :       block_->references().lower_bound(end_offset);
1049    :  
1050  E :    for (; ref_iter != end_iter; ++ref_iter) {
1051    :      // Calculate the local offset of this reference within item.
1052  E :      BlockGraph::Offset local_offset = ref_iter->first - item_offset;
1053  E :      const BlockGraph::Reference& reference = ref_iter->second;
1054    :  
1055    :      // We expect long references for everything except flow control.
1056  E :      CHECK_EQ(4U, reference.size());
1057  E :      DCHECK_LE(local_offset + reference.size(), static_cast<Size>(end_offset));
1058    :  
1059  E :      if (reference.referenced() != block_) {
1060    :        // For external references, we can directly reference the other block.
1061    :        bool inserted = refs->insert(std::make_pair(
1062    :              local_offset,
1063    :              BasicBlockReference(reference.type(), reference.size(),
1064    :                                  reference.referenced(), reference.offset(),
1065  E :                                  reference.base()))).second;
1066  E :        DCHECK(inserted);
1067  E :      } else {
1068    :        // For intra block_ references, find the corresponding basic block in
1069    :        // the basic block address space.
1070  E :        BasicBlock* target_bb = GetBasicBlockAt(reference.base());
1071  E :        DCHECK(target_bb != NULL);
1072    :  
1073    :        // Create target basic-block relative values for the base and offset.
1074    :        // TODO(chrisha): Make BasicBlockReferences handle indirect references.
1075  E :        CHECK_EQ(reference.offset(), reference.base());
1076    :  
1077    :        // Insert a reference to the target basic block.
1078    :        bool inserted = refs->insert(std::make_pair(
1079    :            local_offset,
1080    :            BasicBlockReference(reference.type(),
1081    :                                reference.size(),
1082  E :                                target_bb))).second;
1083  E :        DCHECK(inserted);
1084    :      }
1085  E :    }
1086  E :  }
1087    :  
1088  E :  void BasicBlockDecomposer::CopyReferences() {
1089    :    // Copy the references for the source range of each basic-block (by
1090    :    // instruction for code basic-blocks). External referrers and successors are
1091    :    // handled in separate passes.
1092    :    BasicBlockSubGraph::BBCollection::iterator bb_iter =
1093  E :        subgraph_->basic_blocks().begin();
1094  E :    for (; bb_iter != subgraph_->basic_blocks().end(); ++bb_iter) {
1095  E :      BasicCodeBlock* code_block = BasicCodeBlock::Cast(*bb_iter);
1096  E :      if (code_block != NULL) {
1097  E :        DCHECK_EQ(BasicBlock::BASIC_CODE_BLOCK, code_block->type());
1098    :  
1099  E :        Offset inst_offset = code_block->offset();
1100    :        BasicBlock::Instructions::iterator inst_iter =
1101  E :            code_block->instructions().begin();
1102  E :        for (; inst_iter != code_block->instructions().end(); ++inst_iter) {
1103    :          CopyReferences(inst_offset,
1104    :                         inst_iter->size(),
1105  E :                         &inst_iter->references());
1106  E :          inst_offset += inst_iter->size();
1107  E :        }
1108    :      }
1109    :  
1110  E :      BasicDataBlock* data_block = BasicDataBlock::Cast(*bb_iter);
1111  E :      if (data_block != NULL) {
1112  E :        DCHECK_NE(BasicBlock::BASIC_CODE_BLOCK, data_block->type());
1113    :        CopyReferences(data_block->offset(),
1114    :                       data_block->size(),
1115  E :                       &data_block->references());
1116    :      }
1117  E :    }
1118  E :  }
1119    :  
1120  E :  void BasicBlockDecomposer::ResolveSuccessors() {
1121    :    BasicBlockSubGraph::BBCollection::iterator bb_iter =
1122  E :        subgraph_->basic_blocks().begin();
1123  E :    for (; bb_iter != subgraph_->basic_blocks().end(); ++bb_iter) {
1124    :      // Only code basic-blocks have successors and instructions.
1125  E :      BasicCodeBlock* code_block = BasicCodeBlock::Cast(*bb_iter);
1126  E :      if (code_block == NULL)
1127  E :        continue;
1128    :  
1129    :      BasicBlock::Successors::iterator succ_iter =
1130  E :          code_block->successors().begin();
1131    :      BasicBlock::Successors::iterator succ_iter_end =
1132  E :          code_block->successors().end();
1133  E :      for (; succ_iter != succ_iter_end; ++succ_iter) {
1134  E :        if (succ_iter->reference().block() != block_)
1135  E :          continue;
1136    :  
1137    :        // Find the basic block the successor references.
1138    :        BasicBlock* target_code_block =
1139  E :            GetBasicBlockAt(succ_iter->reference().offset());
1140  E :        DCHECK(target_code_block != NULL);
1141    :  
1142    :        // We transform all successor branches into 4-byte pc-relative targets.
1143    :        succ_iter->set_reference(
1144    :            BasicBlockReference(
1145  E :                BlockGraph::PC_RELATIVE_REF, 4, target_code_block));
1146  E :        DCHECK(succ_iter->reference().IsValid());
1147  E :      }
1148  E :    }
1149  E :  }
1150    :  
1151  E :  void BasicBlockDecomposer::MarkUnreachableCodeAsPadding() {
1152  E :    BasicBlockSubGraph::ReachabilityMap rm;
1153  E :    subgraph_->GetReachabilityMap(&rm);
1154  E :    DCHECK_EQ(rm.size(), subgraph_->basic_blocks().size());
1155    :    BasicBlockSubGraph::BBCollection::iterator bb_iter =
1156  E :        subgraph_->basic_blocks().begin();
1157  E :    for (; bb_iter != subgraph_->basic_blocks().end(); ++bb_iter) {
1158  E :      BasicCodeBlock* code_bb = BasicCodeBlock::Cast(*bb_iter);
1159  E :      if (code_bb != NULL) {
1160  E :        if (!subgraph_->IsReachable(rm, code_bb))
1161  E :          code_bb->MarkAsPadding();
1162    :      }
1163  E :    }
1164  E :  }
1165    :  
1166    :  }  // namespace block_graph

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