public class

DUtil

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.core.units.code.android.ir.DUtil

Class Overview

dexdec IR access and manipulation utility methods.

Summary

Public Constructors
DUtil()
Public Methods
static boolean canHandlerCatchException(IDMethodContext ctx, IDExceptionHandler h, String exceptionSig)
static boolean checkBlock(BasicBlock<IDInstruction> b, DOpcodeType... opcodes)
Check for instruction matching in the provided block.
static int checkSequence(CFG<IDInstruction> cfg, int blkindex, DOpcodeType... opcodes)
Check for instruction matching in a sequence of blocks.
static int countVariable(IDExpression e, IDVar var)
Count the number of occurrences of a given variable in the provided IR expression.
static long createVarId(int registerBase, boolean dualSlot)
Create an IR variable id.
static long createVarIdDualSlot(int registerBase)
Create an IR variable id.
static void dump(CFG<IDInstruction> cfg, String filename)
Dump an IR-CFG to a Graphviz dot file in the user's temporary folder.
static void dump(CFG<IDInstruction> cfg, String filename, String title)
Dump an IR-CFG to a Graphviz dot file in the user's temporary folder.
static String generateNativeAddress(IDMethodContext ctx, IDExpression elt)
static ICodeCoordinates generateNativeCoordinates(IDMethodContext ctx, IDExpression elt)
static ContextAccessType getCAT(IDInstruction insn, boolean inclAssigDst)
Determine the aggregated context type of an instruction.
static String getExceptionSignature(IDMethodContext ctx, IDExceptionHandler h)
static Collection<BasicBlock<IDInstruction>> getReachableBlocks(CFG<IDInstruction> cfg)
Get the list of reachable blocks of the provided IR-CFG.
static boolean hasInvokeInfo(IDExpression e)
Determine whether the IR expression makes invocations (general invocations, new, new-array).
static boolean hasVariables(IDExpression e)
Determine whether the provided expression contains variables.
static boolean isUsingCaughtException(IDMethodContext ctx, BasicBlock<IDInstruction> b)
static boolean mayBeFinal(IDField f, IDexUnit dex)
Determine whether the provided field is or appears to be final.
static boolean mayHaveRightSideSideEffects(IDInstruction insn, boolean includeCanThrow)
This heuristic determines whether the right-hand side of the instruction (the 'source', in the case of an assignment) may have side-effects, i.e.
static boolean mayHaveSideEffects(IDExpression e, IDGlobalContext gctx, boolean includeCanThrow)
This heuristic determines whether the provided expression may have side-effects, i.e.
static boolean mayThrow(IDExpression e, IDGlobalContext gctx)
This heuristic determines whether the provided expression may possibly throw.
static int removeGaps(CFG<IDInstruction> cfg)
Remove all gaps from the provided IR-CFG.
static int removeUnreachableBlocks(CFG<IDInstruction> cfg, IDTryData ex)
Remove unreachable blocks from an IR-CFG.
static int removeUnreachableBlocks(IDMethodContext ctx)
Remove unreachable blocks from an IR-CFG.
static int removeUnreachableBlocks(List<BasicBlock<IDInstruction>> tbr, CFG<IDInstruction> cfg, IDTryData ex)
Remove blocks not reachable from the entry block (first block) of a CFG.
static int simplifyJCondsAndSwitches(IDMethodContext ctx)
Simply DOpcodeType#JCOND and IR_SWITCH IR instructions:
- JCOND: the target branch must not point to the fallthrough instruction; if it is, the JCOND must be replaced by a JUMP (while taking care of potential side-effects introduced by the evaluation of the JCOND predicate)
- SWITCH: the case targets must not point to the fallthrough instruction; if any does, the case entry must be removed (and will be taken care by the switch's default entry)

Illegal JCOND/SWITCH are tolerated during an optimization phase; however, an optimizer, or any code modifying the IR, must provide a legal IR to subsequent components.

static boolean unprotectBlock(CFG<IDInstruction> cfg, IDTryData exdata, int blkAddress)
Unprotect a basic block.
static boolean usesReferences(IDExpression e)
Determine whether the IR expression uses references, defined as any of: accessing fields, accessing array elements, creating arrays.
static boolean verifyDefinitionLocation(Collection<IDInstruction> usingInstructions, Map<IDInstruction, Map<Integer, List<IDInstruction>>> dict_full_ud_chains, int reg, IDInstruction wanted_defloc)
DFA utility method: check whether a register, used in multiple places, was defined by a single instruction.
static boolean verifyDefinitionLocation(Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, int reg, IDInstruction wanted_defloc)
DFA utility method.
static boolean verifyDefinitionLocation(Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Set<Integer> registers, IDInstruction wanted_defloc)
DFA utility method.
static boolean verifyDefinitionWithSingleImm(Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Collection<Integer> regs, IDImm[] aimm)
DFA utility method.
static void verifyGraph(IDMethodContext ctx, String info)
Verify an IR-CFG.
static void verifyGraph(CFG<IDInstruction> cfg, IDTryData exdata, String dumpFilename, String info)
Verify an IR-CFG.
static void verifyGraph(IDMethodContext ctx)
Verify an IR-CFG.
static void verifyGraph(IDMethodContext ctx, String dumpFilename, String info)
Verify an IR-CFG.
static boolean verifySingleDefinitionLocation(Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, int reg, IDInstruction[] aloc)
DFA utility method: check whether a register, used somewhere, was defined by a single instruction.
static boolean verifySingleDefinitionLocation(Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Collection<Integer> regs, IDInstruction[] aloc)
DFA utility method.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DUtil ()

Public Methods

public static boolean canHandlerCatchException (IDMethodContext ctx, IDExceptionHandler h, String exceptionSig)

public static boolean checkBlock (BasicBlock<IDInstruction> b, DOpcodeType... opcodes)

Check for instruction matching in the provided block. The block must be fully matched.

public static int checkSequence (CFG<IDInstruction> cfg, int blkindex, DOpcodeType... opcodes)

Check for instruction matching in a sequence of blocks. Blocks must be fully contiguous and fully matched.
This is an extended version of #checkBlock(BasicBlock, DOpcodeType...), which performs matching on a single block.

Returns
  • the number of blocks containing the sequence; on failure

public static int countVariable (IDExpression e, IDVar var)

Count the number of occurrences of a given variable in the provided IR expression.

public static long createVarId (int registerBase, boolean dualSlot)

Create an IR variable id.

public static long createVarIdDualSlot (int registerBase)

Create an IR variable id.

public static void dump (CFG<IDInstruction> cfg, String filename)

Dump an IR-CFG to a Graphviz dot file in the user's temporary folder.

public static void dump (CFG<IDInstruction> cfg, String filename, String title)

Dump an IR-CFG to a Graphviz dot file in the user's temporary folder.

public static String generateNativeAddress (IDMethodContext ctx, IDExpression elt)

public static ICodeCoordinates generateNativeCoordinates (IDMethodContext ctx, IDExpression elt)

public static ContextAccessType getCAT (IDInstruction insn, boolean inclAssigDst)

Determine the aggregated context type of an instruction.

Parameters
insn instruction
inclAssigDst if true and the instruction is an assignment, the destination is included when calculated the CAT; else, the right-side only is considered
Returns
  • aggregated CAT

public static String getExceptionSignature (IDMethodContext ctx, IDExceptionHandler h)

public static Collection<BasicBlock<IDInstruction>> getReachableBlocks (CFG<IDInstruction> cfg)

Get the list of reachable blocks of the provided IR-CFG. They are the blocks that can be reached, regularly or irregularly, from the entry-point block (i.e. the first block, at offset 0).

public static boolean hasInvokeInfo (IDExpression e)

Determine whether the IR expression makes invocations (general invocations, new, new-array).

public static boolean hasVariables (IDExpression e)

Determine whether the provided expression contains variables.

public static boolean isUsingCaughtException (IDMethodContext ctx, BasicBlock<IDInstruction> b)

public static boolean mayBeFinal (IDField f, IDexUnit dex)

Determine whether the provided field is or appears to be final.

public static boolean mayHaveRightSideSideEffects (IDInstruction insn, boolean includeCanThrow)

This heuristic determines whether the right-hand side of the instruction (the 'source', in the case of an assignment) may have side-effects, i.e. whether it may modify the program state or alter its execution flow in unexpected ways.

public static boolean mayHaveSideEffects (IDExpression e, IDGlobalContext gctx, boolean includeCanThrow)

This heuristic determines whether the provided expression may have side-effects, i.e. whether it may modify the program state or alter its execution flow in unexpected ways.

Parameters
includeCanThrow if false, this heuristic attempts to consider code that _may_ raise exceptions as side-effect-free

public static boolean mayThrow (IDExpression e, IDGlobalContext gctx)

This heuristic determines whether the provided expression may possibly throw.

public static int removeGaps (CFG<IDInstruction> cfg)

Remove all gaps from the provided IR-CFG.

Gaps are illegal in an IR-CFG. They are tolerated during an optimization phase; however, an optimizer, or any code modifying the IR, must provide a legal IR to subsequent components.

Returns
  • the number of gaps that were removed

public static int removeUnreachableBlocks (CFG<IDInstruction> cfg, IDTryData ex)

Remove unreachable blocks from an IR-CFG. They are blocks that cannot be reached, regularly or irregularly, from the entry-point block (i.e. the first block, at offset 0).

Unreachable blocks are illegal in an IR-CFG. They are tolerated during an optimization phase; however, an optimizer, or any code modifying the IR, must provide a legal IR to subsequent components.

public static int removeUnreachableBlocks (IDMethodContext ctx)

Remove unreachable blocks from an IR-CFG. They are blocks that cannot be reached, regularly or irregularly, from the entry-point block (i.e. the first block, at offset 0).

Unreachable blocks are illegal in an IR-CFG. They are tolerated during an optimization phase; however, an optimizer, or any code modifying the IR, must provide a legal IR to subsequent components.

public static int removeUnreachableBlocks (List<BasicBlock<IDInstruction>> tbr, CFG<IDInstruction> cfg, IDTryData ex)

Remove blocks not reachable from the entry block (first block) of a CFG.

Parameters
tbr list of blocks to be removed: can be a bootstrap list of a full list, it doesn't matter (the impl. will take care of removing all related, unreachable blocks)
ex optional exception data
Returns
  • number of removed blocks

public static int simplifyJCondsAndSwitches (IDMethodContext ctx)

Simply DOpcodeType#JCOND and IR_SWITCH IR instructions:
- JCOND: the target branch must not point to the fallthrough instruction; if it is, the JCOND must be replaced by a JUMP (while taking care of potential side-effects introduced by the evaluation of the JCOND predicate)
- SWITCH: the case targets must not point to the fallthrough instruction; if any does, the case entry must be removed (and will be taken care by the switch's default entry)

Illegal JCOND/SWITCH are tolerated during an optimization phase; however, an optimizer, or any code modifying the IR, must provide a legal IR to subsequent components.

public static boolean unprotectBlock (CFG<IDInstruction> cfg, IDTryData exdata, int blkAddress)

Unprotect a basic block. The CFG is adjusted cleaned afterward.

Parameters
blkAddress block address
Returns
  • true if the block was protected and is now unprotected; false if the block was not protected

public static boolean usesReferences (IDExpression e)

Determine whether the IR expression uses references, defined as any of: accessing fields, accessing array elements, creating arrays.

public static boolean verifyDefinitionLocation (Collection<IDInstruction> usingInstructions, Map<IDInstruction, Map<Integer, List<IDInstruction>>> dict_full_ud_chains, int reg, IDInstruction wanted_defloc)

DFA utility method: check whether a register, used in multiple places, was defined by a single instruction.

Parameters
usingInstructions a list of instructions using `reg`
dict_full_ud_chains dictionary of UD chains (provided by a DFA object)
reg a register id
wanted_defloc the requested definition instruction (null means defined as a method input variable)
Returns
  • true if all uses of `reg` (by the provided instructions) were defined by the provided instruction `wanted_defloc`

public static boolean verifyDefinitionLocation (Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, int reg, IDInstruction wanted_defloc)

DFA utility method.

public static boolean verifyDefinitionLocation (Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Set<Integer> registers, IDInstruction wanted_defloc)

DFA utility method.

public static boolean verifyDefinitionWithSingleImm (Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Collection<Integer> regs, IDImm[] aimm)

DFA utility method.

public static void verifyGraph (IDMethodContext ctx, String info)

Verify an IR-CFG.

Throws
RuntimeException (or derived, such as IllegalArgumentException or IllegalStateException) if the IR and/or the CFG failed verification

public static void verifyGraph (CFG<IDInstruction> cfg, IDTryData exdata, String dumpFilename, String info)

Verify an IR-CFG.

Throws
RuntimeException (or derived, such as IllegalArgumentException or IllegalStateException) if the IR and/or the CFG failed verification

public static void verifyGraph (IDMethodContext ctx)

Verify an IR-CFG.

Parameters
ctx method context
Throws
RuntimeException (or derived, such as IllegalArgumentException or IllegalStateException) if the IR and/or the CFG failed verification

public static void verifyGraph (IDMethodContext ctx, String dumpFilename, String info)

Verify an IR-CFG.

Throws
RuntimeException (or derived, such as IllegalArgumentException or IllegalStateException) if the IR and/or the CFG failed verification

public static boolean verifySingleDefinitionLocation (Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, int reg, IDInstruction[] aloc)

DFA utility method: check whether a register, used somewhere, was defined by a single instruction.

Parameters
full_ud_chains UD chains at a given instruction
reg a register id
aloc output sink (the result stored may be null, representing the a method input variable)
Returns
  • true if the register was defined at one and only one location (in which case, the defining instruction is returned in `aloc`)

public static boolean verifySingleDefinitionLocation (Map<Integer, ? extends Collection<IDInstruction>> full_ud_chains, Collection<Integer> regs, IDInstruction[] aloc)

DFA utility method.