public class

CUtil

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.core.units.code.asm.decompiler.ast.CUtil

Class Overview

Utility methods to manipulate AST elements.

Summary

Nested Classes
class CUtil.BreakFlowResult  
enum CUtil.BreakFlowStatus Status of a flow breaker AST statement. 
Public Constructors
CUtil()
Public Methods
static ICOperation add(ICMethod m, ICExpression a, ICExpression b)
static ICOperation add(ICMethod m, ICExpression a, ICExpression b, ICExpression c)
static ICOperation andB(ICMethod m, ICExpression a, ICExpression b)
static ICOperation andL(ICMethod m, ICExpression a, ICExpression b)
static boolean canFallthrough(ICStatement currentStmt, ICStatement fallthroughStmt, boolean ignoreBreak)
Check if the current statement can fallthrough to the next statement, i.e.
static boolean containsBreak(ICStatement stm)
static boolean containsLabel(ICStatement stm)
static boolean containsStatement(ICStatement stm, Class<? extends ICStatement> class_)
static int countAllSubElements(ICElement elem)
Recursively count the total number of sub-elements in the given element (see getSubElements()).
static ICOperation div(ICMethod m, ICExpression a, ICExpression b)
static ICOperation eq(ICMethod m, ICExpression a, ICExpression b)
static ICElement findParent(ICElement root, ICElement elt)
Retrieve the parent of an AST element (or first parent, if the element is reusable and has potentially multiple parents - e.g.
static boolean flipIfElse(ICStatement stm, ICOperatorFactory of)
Check if a statement is a if-statement with two blocks, including a default block, and reverse the condition.
static ICOperation ge(ICMethod m, ICExpression a, ICExpression b)
static CUtil.BreakFlowResult getBreakingFlowResult(ICStatement e)
Indicate if next instruction will be executed.
static Long getConstantAsLong(ICConstant<?> expr)
Retrieve the value associated to an ICConstant as a Long.
static ICDecl getDefinition(ICElement element)
Get the ICDecl possibly contained in a statement; either the statement itself, or the left-side of an assignment.
static ICElement getDefinitionInitialValue(ICElement element)
Get the initial value of a defined variable, from its definition statement.
static ICStatement getFirstRealStatement(ICBlock b, int i)
Get the next "real" statement to be executed in the block, starting at the given index inclusively.
static ICStatement getFirstRealStatementEx(ICMethod m, ICStatement first)
This method does two things:
  • locating the statement provided, and skipping over it (note: if it is a compound statement, the entire compound is skipped)
  • the next real statement that immediately follows is returned (see #getFirstRealStatement()).
static ICLabel getGotoLabel(ICStatement stm)
Check if the statement is a ICGoto to return the label; else, return null.
static ICLabel getIfGotoTarget(ICStatement stm)
Check if the statement follows the pattern if(P){goto x;} and if so, returns x.
static COperatorType getOperation(ICExpression e, COperatorType... optypes)
static COperatorType getOperation(ICOperation e, COperatorType... optypes)
static ICStatement getParentBlock(ICMethod m, ICStatement b)
Retrieve parent block or null if no parent was found (or processed statement is the top body of the ICMethod)
static List<ICStatement> getPreviouslyExecutedStatements(ICMethod m, ICStatement first)
Attempt to retrieve the previously executed statements from a given statement.
static ICOperation gt(ICMethod m, ICExpression a, ICExpression b)
static boolean hasEmptyBranch(ICIfStm stm)
Check if a if-statement has an empty branch (including the default one).
static boolean hasNoCall(ICElement elem)
Check if no ICCall is done by this ICElement.
static boolean hasNoSideEffects(ICElement elem)
Check if an ICElement can not possibly modify variables.
static boolean isBreakTo(ICStatement stm, ICLabel target)
Check if the statement is a ICBreak to the given label.
static CUtil.BreakFlowStatus isBreakingFlow(ICStatement e)
static boolean isClassMethodField(ICElement e)
Check if the element is either an ICClass, an ICMethod or an ICField
static boolean isContainingLabel(ICStatement stm, ICLabel targetLabel)
Check if a statement contains a given label; compound statements are recursively processed to search for the label.
static boolean isDeclareAndAssign(ICElement element)
static boolean isDoWhileTrue(ICStatement stm)
Check if a statement is a do-while loop, whose predicate is literal True.
static boolean isGotoTo(ICStatement stm, ICLabel target)
Check if the statement is a ICGoto to the given label.
static boolean isIfBranch(ICStatement stm)
Check if the statement follows the pattern if(P){BRANCH;}, where BRANCH is a sole branch instruction.
static boolean isIfElse(ICStatement stm)
Check if a statement is a if-statement with two blocks, including a default block.
static boolean isIfNoElse(ICStatement stm)
Check if a statement follows the pattern if(P){ }[else if(...){}]*, i.e.
static boolean isIntegerConstant(ICExpression expr)
Check if the element is a ICConstantInteger
static boolean isIntegerValue(ICExpression expr, long value)
Check if the element is a ICConstantInteger equals to the given value.
static boolean isOperation(ICExpression e, COperatorType... optypes)
static boolean isPlainBreak(ICStatement stm)
Check if the statement is a ICBreak without label.
static boolean isWhileTrue(ICStatement stm)
Check if a statement is a while loop, whose predicate is literal True.
static ICOperation le(ICMethod m, ICExpression a, ICExpression b)
static ICOperation lt(ICMethod m, ICExpression a, ICExpression b)
static ICOperation mul(ICMethod m, ICExpression a, ICExpression b)
static ICOperation ne(ICMethod m, ICExpression a, ICExpression b)
static ICExpression notB(ICMethod m, ICExpression a)
static ICExpression notL(ICMethod m, ICExpression a)
static ICExpression notLDeepReplace(ICMethod m, ICExpression a)
static ICOperation orB(ICMethod m, ICExpression a, ICExpression b)
static ICOperation orL(ICMethod m, ICExpression a, ICExpression b)
static ICOperation rem(ICMethod m, ICExpression a, ICExpression b)
static int replaceSubElementRecurse(ICElement elt, ICExpression oldElement, ICExpression newElement)
Recursively replace sub-elements of an element , using equals() equality.
static ICExpression resolveNotOperation(ICMethod m, ICOperation e, ICElement parent)
static ICOperation shl(ICMethod m, ICExpression a, ICExpression b)
static ICOperation shr(ICMethod m, ICExpression a, ICExpression b)
static ICOperation sub(ICMethod m, ICExpression a, ICExpression b)
static ICOperation ushr(ICMethod m, ICExpression a, ICExpression b)
static boolean visitICStatementDepthPost(ICVisitor visitor, ICBlock elt, int from, CVisitResults results)
static boolean visitICStatementDepthPost(ICVisitor visitor, ICStatement elt, CVisitResults results)
static ICOperation xorB(ICMethod m, ICExpression a, ICExpression b)
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public CUtil ()

Public Methods

public static ICOperation add (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation add (ICMethod m, ICExpression a, ICExpression b, ICExpression c)

public static ICOperation andB (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation andL (ICMethod m, ICExpression a, ICExpression b)

public static boolean canFallthrough (ICStatement currentStmt, ICStatement fallthroughStmt, boolean ignoreBreak)

Check if the current statement can fallthrough to the next statement, i.e. if the first one can redirect execution to the second one (directly or indirectly) when these two statements are next to each other. If the answer is false, the second statement can not possibly be executed once its predecessor has been executed.

Note: this is a conservative analysis (= false is a correct result, while true might indicate an unknown case). In particular, if the fallthrough statement is a label or a compound statement, the answer is true.

Parameters
fallthroughStmt the statement immediately following the current statement in the block
ignoreBreak indicate that the break should be ignored (in particular, when not in loop or in a switch context)

public static boolean containsBreak (ICStatement stm)

public static boolean containsLabel (ICStatement stm)

public static boolean containsStatement (ICStatement stm, Class<? extends ICStatement> class_)

public static int countAllSubElements (ICElement elem)

Recursively count the total number of sub-elements in the given element (see getSubElements()).

public static ICOperation div (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation eq (ICMethod m, ICExpression a, ICExpression b)

public static ICElement findParent (ICElement root, ICElement elt)

Retrieve the parent of an AST element (or first parent, if the element is reusable and has potentially multiple parents - e.g. ICIdentifier).

Parameters
root a root element, typically, an ICMethod
elt the element for which the first parent is to be found
Returns
  • the parent or null

public static boolean flipIfElse (ICStatement stm, ICOperatorFactory of)

Check if a statement is a if-statement with two blocks, including a default block, and reverse the condition. Effectively, if(A){X}else{Y} is transformed to if(!A){Y}else{X}}.

Returns
  • success indicator

public static ICOperation ge (ICMethod m, ICExpression a, ICExpression b)

public static CUtil.BreakFlowResult getBreakingFlowResult (ICStatement e)

Indicate if next instruction will be executed. This is not an in-depth analysis, meaning that if there if a if (true) return; b = 2;, the method will return BOTH (predicates are not examined, only branches and based on last instruction).

public static Long getConstantAsLong (ICConstant<?> expr)

Retrieve the value associated to an ICConstant as a Long.

Returns
  • constant value, null if none could be retrieved

public static ICDecl getDefinition (ICElement element)

Get the ICDecl possibly contained in a statement; either the statement itself, or the left-side of an assignment.

Returns
  • definition, null if it could not be retrieved

public static ICElement getDefinitionInitialValue (ICElement element)

Get the initial value of a defined variable, from its definition statement.

Returns
  • the initial value set to a defined variable, null if none

public static ICStatement getFirstRealStatement (ICBlock b, int i)

Get the next "real" statement to be executed in the block, starting at the given index inclusively.

In particular, this method skips over:

  • the do { part of a do-while block
  • the while(true) { part of a while block
  • the if(true) { part of a conditional statement
Note: label statements are not skipped over.

Parameters
i the index in the block, at which to start the search
Returns
  • the next statement to be executed, or null if none were found

public static ICStatement getFirstRealStatementEx (ICMethod m, ICStatement first)

This method does two things:

  • locating the statement provided, and skipping over it (note: if it is a compound statement, the entire compound is skipped)
  • the next real statement that immediately follows is returned (see #getFirstRealStatement()).
Examples:
 stm1;  // first
 while(true) {
   stm2;  // next
   ...
 
Or:
 stm1;  // first
 do {
   stm2;  // next
   ...
 
Or:
 if(...) {
   while(...) {  // first
      ...;
   }
 }
 stm2;  // next
 

Returns
  • the next statement to be executed, or null if none were found

public static ICLabel getGotoLabel (ICStatement stm)

Check if the statement is a ICGoto to return the label; else, return null.

public static ICLabel getIfGotoTarget (ICStatement stm)

Check if the statement follows the pattern if(P){goto x;} and if so, returns x.

Returns
  • the label pointed by the goto if the statement follows the pattern, null otherwise

public static COperatorType getOperation (ICExpression e, COperatorType... optypes)

public static COperatorType getOperation (ICOperation e, COperatorType... optypes)

public static ICStatement getParentBlock (ICMethod m, ICStatement b)

Retrieve parent block or null if no parent was found (or processed statement is the top body of the ICMethod)

Parameters
m method to check
b statement to look for parent
Returns
  • parent block, null if none were found

public static List<ICStatement> getPreviouslyExecutedStatements (ICMethod m, ICStatement first)

Attempt to retrieve the previously executed statements from a given statement.

Note that there is no look up for goto and jump so the previous statement of a label will only be the previous instruction as seen in the method. This method returns null when previous statements are too complicated to retrieve.

Returns
  • the possible previously executed statements, null if none were found

public static ICOperation gt (ICMethod m, ICExpression a, ICExpression b)

public static boolean hasEmptyBranch (ICIfStm stm)

Check if a if-statement has an empty branch (including the default one).

public static boolean hasNoCall (ICElement elem)

Check if no ICCall is done by this ICElement.

Returns
  • true if no ICCall is done, false otherwise

public static boolean hasNoSideEffects (ICElement elem)

Check if an ICElement can not possibly modify variables. This method is intended to be safe, e.g. it will return false on ICCall.

Returns
  • true if no variables would be modified by executing the element, false otherwise

public static boolean isBreakTo (ICStatement stm, ICLabel target)

Check if the statement is a ICBreak to the given label.

public static CUtil.BreakFlowStatus isBreakingFlow (ICStatement e)

public static boolean isClassMethodField (ICElement e)

Check if the element is either an ICClass, an ICMethod or an ICField

public static boolean isContainingLabel (ICStatement stm, ICLabel targetLabel)

Check if a statement contains a given label; compound statements are recursively processed to search for the label.

public static boolean isDeclareAndAssign (ICElement element)

public static boolean isDoWhileTrue (ICStatement stm)

Check if a statement is a do-while loop, whose predicate is literal True.

public static boolean isGotoTo (ICStatement stm, ICLabel target)

Check if the statement is a ICGoto to the given label.

public static boolean isIfBranch (ICStatement stm)

Check if the statement follows the pattern if(P){BRANCH;}, where BRANCH is a sole branch instruction.

public static boolean isIfElse (ICStatement stm)

Check if a statement is a if-statement with two blocks, including a default block.

public static boolean isIfNoElse (ICStatement stm)

Check if a statement follows the pattern if(P){ }[else if(...){}]*, i.e. a if-statement with possibly multiple blocks but no default else block.

public static boolean isIntegerConstant (ICExpression expr)

Check if the element is a ICConstantInteger

public static boolean isIntegerValue (ICExpression expr, long value)

Check if the element is a ICConstantInteger equals to the given value.

public static boolean isOperation (ICExpression e, COperatorType... optypes)

public static boolean isPlainBreak (ICStatement stm)

Check if the statement is a ICBreak without label.

public static boolean isWhileTrue (ICStatement stm)

Check if a statement is a while loop, whose predicate is literal True.

public static ICOperation le (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation lt (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation mul (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation ne (ICMethod m, ICExpression a, ICExpression b)

public static ICExpression notB (ICMethod m, ICExpression a)

public static ICExpression notL (ICMethod m, ICExpression a)

public static ICExpression notLDeepReplace (ICMethod m, ICExpression a)

public static ICOperation orB (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation orL (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation rem (ICMethod m, ICExpression a, ICExpression b)

public static int replaceSubElementRecurse (ICElement elt, ICExpression oldElement, ICExpression newElement)

Recursively replace sub-elements of an element , using equals() equality.

Returns
  • number of replacements done

public static ICExpression resolveNotOperation (ICMethod m, ICOperation e, ICElement parent)

public static ICOperation shl (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation shr (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation sub (ICMethod m, ICExpression a, ICExpression b)

public static ICOperation ushr (ICMethod m, ICExpression a, ICExpression b)

public static boolean visitICStatementDepthPost (ICVisitor visitor, ICBlock elt, int from, CVisitResults results)

public static boolean visitICStatementDepthPost (ICVisitor visitor, ICStatement elt, CVisitResults results)

public static ICOperation xorB (ICMethod m, ICExpression a, ICExpression b)