Class SimpleBlockModel

java.lang.Object
ghidra.program.model.block.SimpleBlockModel
All Implemented Interfaces:
CodeBlockModel
Direct Known Subclasses:
BasicBlockModel

public class SimpleBlockModel extends Object implements CodeBlockModel
This BlockModel implements the simple block model. Each Codeblock is made up of contiguous instructions in address order. Blocks satisfy the following:
  1. Any instruction with a label starts a block.
  2. Each instruction that could cause program control flow to change is the last instruction of a Codeblock.
  3. All other instructions are "NOP" fallthroughs, meaning after execution the program counter will be at the instruction immediately following.
  4. Any instruction that is unreachable and has no label is also considered the start of a block.
So a CodeBlock in this model consists of contiguous code that has zero or more nonflow fallthrough instructions followed by a single flow instruction. Each block may or may not have a label at the first instruction, but may not have a label at any other instruction contained in the block. This model does not implement the pure simple block model because unreachable code is still considered a block. This model handles delay slot instructions with the following assumptions:
  1. A delayed instruction is always corresponds to a change in flow and terminates a block. The delay slot instructions following this instruction are always included with the block. Therefor, delay slot instructions will always fall at the bottom of a simple block.
  2. The delay slot depth of the delayed instruction will always correspond to the number of delay slot instructions immediately following the instruction. The model may not behave properly if the disassembled code violates this assumption.
See Also:
  • Field Details

  • Constructor Details

    • SimpleBlockModel

      public SimpleBlockModel(Program program)
      Construct a SimpleBlockModel on a program. Externals will be excluded.
      Parameters:
      program - program to create blocks from.
    • SimpleBlockModel

      public SimpleBlockModel(Program program, boolean includeExternals)
      Construct a SimpleBlockModel on a program.
      Parameters:
      program - program to create blocks from.
      includeExternals - externals will be included if true
  • Method Details

    • getCodeBlockAt

      public CodeBlock getCodeBlockAt(Address addr, TaskMonitor monitor) throws CancelledException
      Get the code/data block starting at this address.
      Specified by:
      getCodeBlockAt in interface CodeBlockModel
      Parameters:
      addr -
      monitor - task monitor which allows user to cancel operation.
      Returns:
      null if there is no codeblock starting at the address
      Throws:
      CancelledException - if the monitor cancels the operation.
    • hasEndOfBlockFlow

      protected boolean hasEndOfBlockFlow(Instruction instr)
      Examine an instruction for out-bound flows which qualify it as an end-of-block.
      Parameters:
      instr -
      Returns:
      true if end-of-block flow exists from specified instruction.
    • createSimpleDataBlock

      protected CodeBlock createSimpleDataBlock(Address start, Address end)
      Create a new block over an address range with a single entry-point.
      Parameters:
      start - the first address which is also the only entry-point.
      end - the last address.
      Returns:
      CodeBlock
    • getCodeBlocksContaining

      public CodeBlock[] getCodeBlocksContaining(Address addr, TaskMonitor monitor) throws CancelledException
      Get all the Code Blocks containing the address.
      Specified by:
      getCodeBlocksContaining in interface CodeBlockModel
      Parameters:
      addr - Address to find a containing block.
      monitor - task monitor which allows user to cancel operation.
      Returns:
      A SimpleBlock if any block contains the address empty array otherwise.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getFirstCodeBlockContaining

      public CodeBlock getFirstCodeBlockContaining(Address addr, TaskMonitor monitor) throws CancelledException
      Get the First Code Block that contains the address.
      Specified by:
      getFirstCodeBlockContaining in interface CodeBlockModel
      Parameters:
      addr - Address to find a containing block.
      monitor - task monitor which allows user to cancel operation.
      Returns:
      A SimpleBlock if any block contains the address. null otherwise.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getCodeBlocks

      public CodeBlockIterator getCodeBlocks(TaskMonitor monitor) throws CancelledException
      Get an iterator over the code blocks in the entire program.
      Specified by:
      getCodeBlocks in interface CodeBlockModel
      Parameters:
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getCodeBlocksContaining

      public CodeBlockIterator getCodeBlocksContaining(AddressSetView addrSet, TaskMonitor monitor) throws CancelledException
      Get an iterator over CodeBlocks which overlap the specified address set.
      Specified by:
      getCodeBlocksContaining in interface CodeBlockModel
      Parameters:
      addrSet - an address set within program
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getProgram

      public Program getProgram()
      Description copied from interface: CodeBlockModel
      Returns the program object associated with this CodeBlockModel instance.
      Specified by:
      getProgram in interface CodeBlockModel
      Returns:
      program associated with this CodeBlockModel.
      See Also:
    • getListing

      protected Listing getListing()
      Returns the program listing associated with this model.
      Returns:
      the program listing associated with this model
    • isBlockStart

      protected boolean isBlockStart(Address addr)
      Check if the instruction at the address is the start of a basic block.
      Parameters:
      addr - Address to check
      Returns:
      true - if the address starts a basic block false - otherwise
    • isBlockStart

      public boolean isBlockStart(Instruction instruction)
      Check if the instruction starts a Simple block.
      Parameters:
      instruction - instruction to test if it starts a block
      Returns:
      true if this instruction is the start of a simple block.
    • getName

      public String getName(CodeBlock block)
      Description copied from interface: CodeBlockModel
      Get a name for this block.
      Specified by:
      getName in interface CodeBlockModel
      Returns:
      usually the label at the start address of the block however the model can choose any name it wants for its blocks.
      See Also:
    • getFlowType

      public FlowType getFlowType(CodeBlock block)
      Return in general how things flow out of this node. If there are any abnormal ways to flow out of this node, (ie: jump, call, etc...) then the flow type of the node takes on that type. If there are multiple unique ways out of the node, then we should return FlowType.UNKNOWN (or FlowType.MULTIFLOW ?). Fallthrough is returned if that is the only way out. If this block really has no valid instructions, it can't flow, so FlowType.INVALID is returned.
      Specified by:
      getFlowType in interface CodeBlockModel
      Returns:
      flow type of this node
    • getSources

      public CodeBlockReferenceIterator getSources(CodeBlock block, TaskMonitor monitor) throws CancelledException
      Get an iterator over source blocks flowing into this block.
      Specified by:
      getSources in interface CodeBlockModel
      Parameters:
      block - code block to get the source iterator for.
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getNumSources

      @Deprecated public int getNumSources(CodeBlock block, TaskMonitor monitor) throws CancelledException
      Deprecated.
      this method should be avoided since it repeats the work of the getSources iterator
      Get number of source blocks flowing into this block
      Specified by:
      getNumSources in interface CodeBlockModel
      Parameters:
      block - code block to get the source iterator for.
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getDestinations

      public CodeBlockReferenceIterator getDestinations(CodeBlock block, TaskMonitor monitor) throws CancelledException
      Get an iterator over destination blocks flowing from this block.
      Specified by:
      getDestinations in interface CodeBlockModel
      Parameters:
      block - code block to get the destination block iterator for.
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getNumDestinations

      @Deprecated public int getNumDestinations(CodeBlock block, TaskMonitor monitor) throws CancelledException
      Deprecated.
      this method should be avoided since it repeats the work of the getDestinations iterator
      Get number of destination blocks flowing out of this block
      Specified by:
      getNumDestinations in interface CodeBlockModel
      Parameters:
      block - code block to get the destination block iterator for.
      monitor - task monitor which allows user to cancel operation.
      Throws:
      CancelledException - if the monitor cancels the operation.
    • getBasicBlockModel

      public CodeBlockModel getBasicBlockModel()
      Description copied from interface: CodeBlockModel
      Get the basic block model used by this model.
      Specified by:
      getBasicBlockModel in interface CodeBlockModel
      See Also:
    • getName

      public String getName()
      Description copied from interface: CodeBlockModel
      Returns the model name.
      Specified by:
      getName in interface CodeBlockModel
      Returns:
      the model name
      See Also:
    • allowsBlockOverlap

      public boolean allowsBlockOverlap()
      Description copied from interface: CodeBlockModel
      Return true if this model allows overlapping of address sets for the blocks it returns.
      Specified by:
      allowsBlockOverlap in interface CodeBlockModel
      Returns:
      true if this model allows overlapping of address sets for the blocks it returns. This implies that getBlocksContaining() can return more than one block. false implies that getBlocksContaining() will return at most one block.
      See Also:
    • externalsIncluded

      public boolean externalsIncluded()
      Description copied from interface: CodeBlockModel
      Returns true if externals are handled by the model, false if externals are ignored. When handled, externals are represented by an ExtCodeBlockImpl.
      Specified by:
      externalsIncluded in interface CodeBlockModel