Class MutableBlockControlFlowGraph
Every mutation is built from three structural primitives on top of the edge primitives of
MutableBasicBlockImpl:
split(sootup.core.graph.MutableBasicBlockImpl, int): cut a block so that a given stmt becomes the head of a block,merge(sootup.core.graph.MutableBasicBlockImpl, sootup.core.graph.MutableBasicBlockImpl): join a block with its only, fall-through successor,normalize(sootup.core.graph.MutableBasicBlockImpl): merge a block with its neighbours where possible.
beginBatch()/endBatch() merging is deferred.
Costs: a stmt index (IdentityHashMap) maps stmts to blocks; blocks store stmts in a
deque with a position cache, so lookups and head/tail changes are O(1) and a split or merge moves
the smaller part. The linearized block order is cached and survives changes inside a block.
- Author:
- Markus Schmidt
-
Constructor Summary
ConstructorsConstructorDescriptionMutableBlockControlFlowGraph(boolean isStatic, MethodSignature sig, LocalGenerator localgen) MutableBlockControlFlowGraph(@NonNull ControlFlowGraph<? extends BasicBlock<?>> graph) copies a ControlFlowGraph into this Mutable instance, block by block -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a block.voidaddExceptionalEdge(@NonNull Stmt stmt, @NonNull ClassType exceptionType, @NonNull Stmt traphandlerStmt) Adds an exceptional flow with the type "exception" to a "stmt" which will reach "traphandlerStmt"voidinserts a "stmt" with exceptional flows "traps" into the ControlFlowGraphvoidStarts a batch of changes: blocks that become mergeable are not merged until the matchingendBatch(), which saves repeated split/merge work in a sequence of edits.voidclearExceptionalEdges(@NonNull Stmt node) Modifications of exceptional flows removes all exceptional flows from "stmt"booleancontainsNode(@NonNull Stmt node) static ControlFlowGraph<?>createUnmodifiableControlFlowGraph(ControlFlowGraph<?> controlFlowGraph) voidendBatch()ends a batch; the outermost one merges the blocks touched since itsbeginBatch()List<? extends BasicBlock<?>>exceptionalPredecessorBlocks(@NonNull BasicBlock<?> block) exceptionalPredecessors(@NonNull MutableBasicBlock block) exceptionalPredecessors(@NonNull Stmt node) it is possible to reach traphandlers through inline code i.e. without any exceptional flowexceptionalSuccessors(@NonNull Stmt node) Iterator<BasicBlock<?>>blocks in linearized order; iterates a snapshot, the graph may change meanwhile@NonNull BasicBlock<?>getBlockOf(@NonNull Stmt stmt) @NonNull Set<? extends BasicBlock<?>>@NonNull List<? extends BasicBlock<?>>getNodes()returns the nodes in this graph in a non-deterministic order (->Set) to get the nodes in linearized, ordered manner use iterator() or getStmts.@Nullable Stmt@Nullable BasicBlock<?>getStmts()The statements inControlFlowGraph.iterator()order.@NonNull List<BasicBlock<?>>booleanhasEdgeConnecting(@NonNull Stmt source, @NonNull Stmt target) returns true if there is a flow between source and target throws an Exception if at least one of the parameters is not contained in the graph.intreturns the amount of ingoing flows into nodevoidinitializeWith(@NonNull List<List<Stmt>> blocks, @NonNull Map<BranchingStmt, List<Stmt>> successorMap, @NonNull List<Trap> traps) Creates a Graph representation from the 'legacy' representation i.e. a List of Stmts and Traps.@NonNull BasicBlock<?>insertAfter(@NonNull Stmt existingStmt, @NonNull List<FallsThroughStmt> stmts, @NonNull Map<ClassType, Stmt> exceptionMap) @NonNull BasicBlock<?>insertBefore(@NonNull Stmt existingStmt, @NonNull List<FallsThroughStmt> stmts, @NonNull Map<ClassType, Stmt> exceptionMap) intreturns the amount of flows that start from nodepredecessors(@NonNull Stmt node) returns the ingoing flows to node as an List with no reliable/specific order and possibly duplicate entries i.e. if a JSwitchStmt has multiple cases that brnach to `node`voidputEdge(@NonNull BranchingStmt stmtA, int succesorIdx, @NonNull Stmt stmtB) voidputEdge(@NonNull FallsThroughStmt stmtA, @NonNull Stmt stmtB) Modifications of unexceptional flowsvoidremoveBlock(BasicBlock<?> block) removeEdge(@NonNull Stmt from, @NonNull Stmt to) removes the current outgoing flows of "from" to "to"voidremoveExceptionalEdge(@NonNull Stmt node, @NonNull ClassType exceptionType) removes an exceptional flow of the type "exception" flow from "stmt"voidremoveExceptionalFlowFromAllBlocks(@NonNull ClassType exceptionType, @NonNull Stmt exceptionHandlerStmt) Removes the specified exceptional flow from all blocks.voidremoveNode(@NonNull Stmt stmt) removes "stmt" from the ControlFlowGraphvoidremoveNode(@NonNull Stmt stmt, boolean keepFlow) Removes a Stmt from the ControlFlowGraph.voidreplaceNode(@NonNull Stmt oldStmt, @NonNull Stmt newStmt) Modification of stmts (without manipulating any flows; possible assigned exceptional flows stay the same as well)booleanreplaceSuccessorEdge(@NonNull Stmt from, @NonNull Stmt oldTo, @NonNull Stmt newTo) Replaces all SuccessorEdge(s) of from to oldTo by newTovoidsetEdges(@NonNull BranchingStmt fromStmt, @NonNull List<Stmt> targets) replaces the current outgoing flows of "from" to "targets"voidsetStartingStmt(@NonNull Stmt startingStmt) successors(@NonNull Stmt node) returns the outgoing flows of node as ordered List.@NonNull ControlFlowGraph<?>voidchecks the internal consistency; throws an IllegalStateException on a violationMethods inherited from class sootup.core.graph.MutableControlFlowGraph
addBlock, addNode, insertAfter, insertBefore, replaceSucessorEdge, setEdgesMethods inherited from class sootup.core.graph.ControlFlowGraph
degree, equals, getAllSuccessors, getBranchTargetsOf, getEntrypoints, getExtendedBasicBlockPathBetween, getTails, hashCode, isStmtBranchTarget, iterator, toString, validateStmtConnectionsInGraphMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
MutableBlockControlFlowGraph
public MutableBlockControlFlowGraph() -
MutableBlockControlFlowGraph
-
MutableBlockControlFlowGraph
copies a ControlFlowGraph into this Mutable instance, block by block
-
-
Method Details
-
createUnmodifiableControlFlowGraph
public static ControlFlowGraph<?> createUnmodifiableControlFlowGraph(ControlFlowGraph<?> controlFlowGraph) -
initializeWith
public void initializeWith(@NonNull List<List<Stmt>> blocks, @NonNull Map<BranchingStmt, List<Stmt>> successorMap, @NonNull List<Trap> traps) Creates a Graph representation from the 'legacy' representation i.e. a List of Stmts and Traps. -
beginBatch
public void beginBatch()Starts a batch of changes: blocks that become mergeable are not merged until the matchingendBatch(), which saves repeated split/merge work in a sequence of edits. Within a batch the graph is consistent and its stmt-level flows are exact, only blocks may be smaller than necessary. Batches nest.- Overrides:
beginBatchin classMutableControlFlowGraph
-
endBatch
public void endBatch()ends a batch; the outermost one merges the blocks touched since itsbeginBatch()- Overrides:
endBatchin classMutableControlFlowGraph
-
addExceptionalEdge
public void addExceptionalEdge(@NonNull Stmt stmt, @NonNull ClassType exceptionType, @NonNull Stmt traphandlerStmt) Description copied from class:MutableControlFlowGraphAdds an exceptional flow with the type "exception" to a "stmt" which will reach "traphandlerStmt"- Specified by:
addExceptionalEdgein classMutableControlFlowGraph
-
removeExceptionalEdge
Description copied from class:MutableControlFlowGraphremoves an exceptional flow of the type "exception" flow from "stmt"- Specified by:
removeExceptionalEdgein classMutableControlFlowGraph
-
clearExceptionalEdges
Description copied from class:MutableControlFlowGraphModifications of exceptional flows removes all exceptional flows from "stmt"- Specified by:
clearExceptionalEdgesin classMutableControlFlowGraph
-
removeExceptionalFlowFromAllBlocks
public void removeExceptionalFlowFromAllBlocks(@NonNull ClassType exceptionType, @NonNull Stmt exceptionHandlerStmt) Description copied from class:ControlFlowGraphRemoves the specified exceptional flow from all blocks.- Specified by:
removeExceptionalFlowFromAllBlocksin classControlFlowGraph<MutableBasicBlock>- Parameters:
exceptionType- The class type of the exceptional flow.exceptionHandlerStmt- The handler statement of the exceptional flow.
-
getBlocks
- Specified by:
getBlocksin classControlFlowGraph<MutableBasicBlock>
-
getBlocksSorted
- Specified by:
getBlocksSortedin classControlFlowGraph<MutableBasicBlock>
-
getBlockIterator
blocks in linearized order; iterates a snapshot, the graph may change meanwhile- Overrides:
getBlockIteratorin classControlFlowGraph<MutableBasicBlock>
-
addBlock
Adds a block. Its Stmts must be new to the graph, except: the first one may be an existing single-Stmt block without successors, the last one may be the head of an existing block (it is then linked as successor). Only the last Stmt may branch or end the flow.- Specified by:
addBlockin classMutableControlFlowGraph
-
removeBlock
- Specified by:
removeBlockin classMutableControlFlowGraph
-
addNode
Description copied from class:MutableControlFlowGraphinserts a "stmt" with exceptional flows "traps" into the ControlFlowGraph- Specified by:
addNodein classMutableControlFlowGraph
-
removeNode
Description copied from class:MutableControlFlowGraphremoves "stmt" from the ControlFlowGraph- Specified by:
removeNodein classMutableControlFlowGraph
-
removeNode
Removes a Stmt from the ControlFlowGraph.It can optionally keep the flow (edges) of the statement by connecting the predecessors of the statement with successors of the statement. Keeping the flow does not work when the statement has multiple successors.
- Specified by:
removeNodein classMutableControlFlowGraph- Parameters:
stmt- the Stmt to be removedkeepFlow- flag indicating whether to keep the flow or not- Throws:
IllegalArgumentException- if keepFlow is true but the stmt has multiple successors
-
replaceNode
Description copied from class:MutableControlFlowGraphModification of stmts (without manipulating any flows; possible assigned exceptional flows stay the same as well)- Specified by:
replaceNodein classMutableControlFlowGraph
-
validateBlocks
public void validateBlocks()checks the internal consistency; throws an IllegalStateException on a violation -
insertBefore
public @NonNull BasicBlock<?> insertBefore(@NonNull Stmt existingStmt, @NonNull List<FallsThroughStmt> stmts, @NonNull Map<ClassType, Stmt> exceptionMap) - Specified by:
insertBeforein classMutableControlFlowGraph
-
insertAfter
public @NonNull BasicBlock<?> insertAfter(@NonNull Stmt existingStmt, @NonNull List<FallsThroughStmt> stmts, @NonNull Map<ClassType, Stmt> exceptionMap) - Specified by:
insertAfterin classMutableControlFlowGraph
-
replaceSuccessorEdge
Replaces all SuccessorEdge(s) of from to oldTo by newTo- Specified by:
replaceSuccessorEdgein classMutableControlFlowGraph- Returns:
- false if there is no such flow
-
putEdge
Description copied from class:MutableControlFlowGraphModifications of unexceptional flowsAdds a flow "from" to "to". if at least one of the parameter Stmts is not already in the ControlFlowGraph it will be added. if "to" needs to be added to the ControlFlowGraph i.e. "to" is not already in the ControlFlowGraph the method assumes "to" has the same exceptional flows as "from".
- Specified by:
putEdgein classMutableControlFlowGraph
-
putEdge
- Specified by:
putEdgein classMutableControlFlowGraph
-
removeEdge
Description copied from class:MutableControlFlowGraphremoves the current outgoing flows of "from" to "to"- Specified by:
removeEdgein classMutableControlFlowGraph- Returns:
- returns List of the successor indices of "from" that were connected to "to" - items are 0 in case of FallsThroughStmts or idx > 0 in case of BranchingStmts with multiple successors
-
setEdges
Description copied from class:MutableControlFlowGraphreplaces the current outgoing flows of "from" to "targets"- Specified by:
setEdgesin classMutableControlFlowGraph
-
getStartingStmt
- Specified by:
getStartingStmtin classControlFlowGraph<MutableBasicBlock>
-
getStartingStmtBlock
- Specified by:
getStartingStmtBlockin classControlFlowGraph<MutableBasicBlock>
-
getTailStmtBlocks
- Specified by:
getTailStmtBlocksin classControlFlowGraph<MutableBasicBlock>
-
getBlockOf
- Specified by:
getBlockOfin classControlFlowGraph<MutableBasicBlock>
-
unmodifiableControlFlowGraph
- Specified by:
unmodifiableControlFlowGraphin classMutableControlFlowGraph
-
setStartingStmt
- Specified by:
setStartingStmtin classMutableControlFlowGraph
-
getStmts
The statements inControlFlowGraph.iterator()order. The order is computed once until the graph changes; each call returns a new list, as before.- Overrides:
getStmtsin classControlFlowGraph<MutableBasicBlock>
-
getNodes
Description copied from class:ControlFlowGraphreturns the nodes in this graph in a non-deterministic order (->Set) to get the nodes in linearized, ordered manner use iterator() or getStmts.- Specified by:
getNodesin classControlFlowGraph<MutableBasicBlock>
-
containsNode
- Specified by:
containsNodein classControlFlowGraph<MutableBasicBlock>
-
predecessors
Description copied from class:ControlFlowGraphreturns the ingoing flows to node as an List with no reliable/specific order and possibly duplicate entries i.e. if a JSwitchStmt has multiple cases that brnach to `node`- Specified by:
predecessorsin classControlFlowGraph<MutableBasicBlock>
-
exceptionalPredecessors
Description copied from class:ControlFlowGraphit is possible to reach traphandlers through inline code i.e. without any exceptional flow- Specified by:
exceptionalPredecessorsin classControlFlowGraph<MutableBasicBlock>
-
exceptionalPredecessors
-
exceptionalPredecessorBlocks
-
successors
Description copied from class:ControlFlowGraphreturns the outgoing flows of node as ordered List. The List can have duplicate entries!- Specified by:
successorsin classControlFlowGraph<MutableBasicBlock>
-
exceptionalSuccessors
- Specified by:
exceptionalSuccessorsin classControlFlowGraph<MutableBasicBlock>
-
inDegree
Description copied from class:ControlFlowGraphreturns the amount of ingoing flows into node- Specified by:
inDegreein classControlFlowGraph<MutableBasicBlock>
-
outDegree
Description copied from class:ControlFlowGraphreturns the amount of flows that start from node- Specified by:
outDegreein classControlFlowGraph<MutableBasicBlock>
-
hasEdgeConnecting
Description copied from class:ControlFlowGraphreturns true if there is a flow between source and target throws an Exception if at least one of the parameters is not contained in the graph.- Specified by:
hasEdgeConnectingin classControlFlowGraph<MutableBasicBlock>
-