Class AbstractDOptimizer
- All Implemented Interfaces:
IPlugin
,IDOptimizer
dexdec
(DEX decompiler) IR optimizer plugins. Those plugins can access and
modify the intermediate representation of a method being decompiled. They can be used to
implement a wide range of optimizations, from trivial code clean-up to the most complex
deobfuscation passes. Internally, dexdec
uses IR optimizers to perform dead-code removal,
variable substitutions, immediate propagation, restructuring, constant folding, arithmetic
operation simplifications, deobfuscations such as control-flow unflattening, etc.
Optimizers are usually managed by master optimizers
(also called
orchestrators
). Third-party optimizer plugins can be automatically registered when
creating an orchestrator. Refer to createMasterOptimizer
.
Life-cycle information:
- plugins are reused (i.e. the perform()
method is called multiple times)
- thread-safety:
-- Python script plugins must be thread-safe: a plugin instance may be executed by multiple
threads concurrently
-- Java plugins (including Java script plugins) are not required to be thread-safe: a plugin
instance will not be executed by multiple threads concurrently
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionTarget method's IR CFG, set up for convenience (seeIDMethodContext.getCfg()
)Target method's IR contextManaging dex decompiler.Underlying dex code.The data flow analysis object is initially UNINITIALIZED.IR global context.static final ILogger
Public logger accessible by the implementing optimizer.IR/AST operation factory reference (for convenience).IR/AST type factory reference (for convenience).Fields inherited from interface com.pnfsoftware.jeb.core.units.code.android.ir.IDOptimizer
DEOBFUSCATOR, INLINER, REORDERER
-
Constructor Summary
ConstructorsConstructorDescriptionCreate astandard
optimizer.Create an optimizer.AbstractDOptimizer
(DOptimizerType type, String name) Create an optimizer. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected IDFA<IDInstruction>
protected IDFA<IDInstruction>
analyzeChains
(boolean redo) void
This convenience method can be used to assign the object's attributes (e.g.protected void
Verify if an interruption request was made by the managing decompiler.protected int
Perform clean-up after a change in CFG structure.protected int
cleanGraph
(boolean doRemoveUnreachableBlocks, boolean doSimplifyJCondsAndSwitches) Perform clean-up after a change in CFG structure.protected void
protected void
getName()
Retrieve the plugin name.Retrieve basic information about the plugin, such as name, version, author, and organization.double
Get the optimizer priority.getTags()
Get the optimizer tags.getType()
Get the optimizer type.protected void
Invalidate the DFA ofcfg
.boolean
Determine whether this optimizer performs on a single method or an a collection of methods.boolean
Determine whether the optimizer is enabled or not.abstract int
perform()
An optimizer must implement this method.final int
perform
(IDMethodContext ctx) Run the optimizer on the provided target method.int
performOnCollection
(List<IDMethodContext> ctxlist, Map<IDMethodContext, Integer> pmcntmap) Run the optimizer on the provided collection of methods.protected void
void
Reset this object's attributes.void
setEnabled
(boolean enabled) Enable or disable this optimizer.protected void
Set the optimizer name.protected void
setPriority
(double priority) Set the optimizer priority.protected void
setType
(DOptimizerType type) Set the optimizer type.Methods inherited from class com.pnfsoftware.jeb.core.AbstractPlugin
dispose, getData, setData
-
Field Details
-
logger
Public logger accessible by the implementing optimizer. Writing to the logger should be favored over writing directly tostdout
. -
tf
IR/AST type factory reference (for convenience). -
of
IR/AST operation factory reference (for convenience). -
g
IR global context. -
dex
Underlying dex code. -
decomp
Managing dex decompiler. -
ctxlist
-
ctx
Target method's IR context -
cfg
Target method's IR CFG, set up for convenience (seeIDMethodContext.getCfg()
) -
dfa
The data flow analysis object is initially UNINITIALIZED. To initialize one, callanalyzeChains(boolean)
.
-
-
Constructor Details
-
AbstractDOptimizer
public AbstractDOptimizer()Create astandard
optimizer. -
AbstractDOptimizer
Create an optimizer.- Parameters:
type
-
-
AbstractDOptimizer
Create an optimizer.- Parameters:
type
-name
-
-
-
Method Details
-
checkInterrupted
protected void checkInterrupted()Verify if an interruption request was made by the managing decompiler. This method may be called when potentially length computations are performed in a loop. -
getPluginInformation
Description copied from interface:IPlugin
Retrieve basic information about the plugin, such as name, version, author, and organization.- Specified by:
getPluginInformation
in interfaceIPlugin
- Returns:
- the plugin information
-
setName
Set the optimizer name. To be used by the constructor.- Parameters:
name
- if null, a name will be auto-generated
-
getName
Description copied from interface:IDOptimizer
Retrieve the plugin name. Should be consistent with the value returned bygetPluginInformation().getName()
.- Specified by:
getName
in interfaceIDOptimizer
- Returns:
-
isCollectionOptimizer
public boolean isCollectionOptimizer()Description copied from interface:IDOptimizer
Determine whether this optimizer performs on a single method or an a collection of methods.- Specified by:
isCollectionOptimizer
in interfaceIDOptimizer
- Returns:
-
setType
Set the optimizer type. To be used by the constructor.- Parameters:
type
- if null, astandard
optimizer is assumed
-
getType
Description copied from interface:IDOptimizer
Get the optimizer type. Types are used byoptimizer orchestrators
to determine whether an optimizer should run.- Specified by:
getType
in interfaceIDOptimizer
- Returns:
-
addTag
-
removeTag
-
getTags
Description copied from interface:IDOptimizer
Get the optimizer tags. An optimizer may have 0, 1, or more tags. A tag is a non-null, non-empty string.- Specified by:
getTags
in interfaceIDOptimizer
- Returns:
- a collection of tags; may be empty (tags are optional), but never null
-
setPriority
protected void setPriority(double priority) Set the optimizer priority. To be used by the constructor.- Parameters:
priority
- the new priority (high means higher priority). When optimizers are managed and run by an orchestrator, the optimizers with a higher priority are run before those having a lower priority. The default priority is 0.
-
getPriority
public double getPriority()Description copied from interface:IDOptimizer
Get the optimizer priority. A higher value means a higher priority. Priorities are used byoptimizer orchestrators
to determine in which order optimizers should be executed.- Specified by:
getPriority
in interfaceIDOptimizer
- Returns:
- the default priority
-
setEnabled
public void setEnabled(boolean enabled) Enable or disable this optimizer. This flag is checked by amaster optimizer
.- Parameters:
enabled
-
-
isEnabled
public boolean isEnabled()Description copied from interface:IDOptimizer
Determine whether the optimizer is enabled or not. This method is used byoptimizer orchestrators
to determine whether an optimizer can be scheduled for execution.- Specified by:
isEnabled
in interfaceIDOptimizer
- Returns:
- true if this optimizer is enabled
-
perform
Description copied from interface:IDOptimizer
Run the optimizer on the provided target method.For this method to be called by an
orchestrator
,IDOptimizer.isCollectionOptimizer()
must returnfalse
.- Specified by:
perform
in interfaceIDOptimizer
- Parameters:
ctx
- a method context- Returns:
- number of optimizations performed
-
perform
public abstract int perform()An optimizer must implement this method. This method is called by a master optimizer to perform the optimization on theselected
target.Note that the optimizer is responsible for returning a legal method context, e.g.: the method IR instructions must be consistent with the CFG; the CFG must adhere to certain rules (see
cleanGraph
); if the data flow analysis is no longer valid, it should be invalidated (seeresetDFA
); etc.- Returns:
- the number of optimizations performed, 0 if none
-
performOnCollection
public int performOnCollection(List<IDMethodContext> ctxlist, Map<IDMethodContext, Integer> pmcntmap) Description copied from interface:IDOptimizer
Run the optimizer on the provided collection of methods.For this method to be called by an
orchestrator
,IDOptimizer.isCollectionOptimizer()
must returntrue
.- Specified by:
performOnCollection
in interfaceIDOptimizer
- Parameters:
ctxlist
- a collection of method contextspmcntmap
- optional output map that will receive the per-method optimization counts- Returns:
- number of optimizations performed
-
assignLocalFields
This convenience method can be used to assign the object's attributes (e.g.g
,ctx
,cfg
, etc.) from the values of the provided IR context.- Parameters:
_ctx
- an IR context
-
resetLocalFields
public void resetLocalFields()Reset this object's attributes. Same asassignLocalFields(null)
. -
analyzeChains
- Parameters:
redo
- if true, a new object will be generated; otherwise, a previously generated DFA object may be returned- Returns:
- the DFA, also available as a protected field
dfa
-
analyzeChains
- Returns:
- the DFA, also available as a protected field
dfa
-
invalidateChains
protected void invalidateChains() -
cleanGraph
protected int cleanGraph()Perform clean-up after a change in CFG structure. This method will remove unreachable blocks and simplify conditional jumps and switches.- Returns:
- the number of clean-up operations performed
-
cleanGraph
protected int cleanGraph(boolean doRemoveUnreachableBlocks, boolean doSimplifyJCondsAndSwitches) Perform clean-up after a change in CFG structure. Convenience method.- Parameters:
doRemoveUnreachableBlocks
- if true, will callDUtil.removeUnreachableBlocks(IDMethodContext)
doSimplifyJCondsAndSwitches
- if true, will callDUtil.simplifyJCondsAndSwitches(IDMethodContext)
- Returns:
- the number of clean-up operations performed
-
debugDump
protected void debugDump() -
debugVerify
protected void debugVerify()
-