Class MarkFinallyVisitor

java.lang.Object
jadx.core.dex.visitors.AbstractVisitor
jadx.core.dex.visitors.finaly.MarkFinallyVisitor
All Implemented Interfaces:
IDexTreeVisitor

public class MarkFinallyVisitor extends AbstractVisitor
This visitor is responsible for extracting finally blocks from duplicated instructions located within the ends of each branch leading to the terminating point of all code paths. To do this, the terminating point of each handler / exit from try body is found relative to every other handler / exit from try body. This, in effect, is used to identify the "scopes" of each possible path within the try block and thus can be used to find a common series of included blocks within the "scope" of each handler and a block to start searching from in reverse to find common instructions between that and the "nominated finally" handler. These groups are described by the TryEdgeScopeGroupMap object. After this, the TraverserController is responsible for traversing the block graphs from each "scope terminus" along the blocks contained with each handlers "scope", comparing them against the "nominated finally" block. If the control flow and instructions of each block match, then they are added as duplicate instructions. At the end, the visitor will mark the identified duplicated instructions and identified finally instructions with the respective AFlag to be handled during regioning of the block graph.
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
  • Constructor Details

    • MarkFinallyVisitor

      public MarkFinallyVisitor()
  • Method Details

    • visit

      public void visit(MethodNode mth)
      Description copied from interface: IDexTreeVisitor
      Visit method
      Specified by:
      visit in interface IDexTreeVisitor
      Overrides:
      visit in class AbstractVisitor
    • resetTryBlocks

      private static void resetTryBlocks(MethodNode mth, List<TryCatchBlockAttr> tryBlocks)
    • getTryBlockData

      @Nullable private static @Nullable MarkFinallyVisitor.TryExtractInfo getTryBlockData(MethodNode mth, TryCatchBlockAttr tryBlock)
      For a given try block, attempts to calculate try block data. This includes the handler blocks for each try branch, data regarding the scope of each try branch relative to every other branch, and the blocks logically contained within each try branch. This information is stored via internal class members and is not returned by the function.
      Parameters:
      mth - The method containing the try block.
      tryBlock - The try block to determine the scope information of.
      Returns:
      The handler identified as the "all" handler.
    • processTryBlock

      private static boolean processTryBlock(MethodNode mth, MarkFinallyVisitor.TryExtractInfo tryInfo)
      Processes a try block, attempting to extract a finally by locating common instruction patterns between all try branches.
      Parameters:
      mth - The method containing the try block.
      tryInfo - The try block information.
      Returns:
      Whether a finally block has been successfully extracted.
    • cutHandlerBlocks

      @Nullable private static @Nullable List<BlockNode> cutHandlerBlocks(MethodNode mth, MarkFinallyVisitor.TryExtractInfo tryInfo, ExceptionHandler handler)
    • attemptRemoveImplicitHandlers

      private static boolean attemptRemoveImplicitHandlers(List<BlockNode> cutHandlerBlocks, MarkFinallyVisitor.TryExtractInfo tryInfo)
      Attempts to identify and remove an implicit try catch block.
      Parameters:
      cutHandlerBlocks - The cut handler blocks of the all handler.
      Returns:
      Whether the try block is implicit and has been removed.
    • extractFinally

      private static boolean extractFinally(MethodNode mth, MarkFinallyVisitor.TryExtractInfo tryInfo)
      Search and mark common code from 'try' block and 'handlers'.
    • getHandlersForTryCatch

      private static List<ExceptionHandler> getHandlersForTryCatch(TryCatchBlockAttr tryBlock)
      Gets a list of every exception handler attached to this try block, including handlers of inner try blocks.
      Parameters:
      tryBlock - The source try block to get the list of exception handlers for
      Returns:
      The list of exception handlers.
    • findCommonInsns

      @Nullable private static @Nullable Map<InsnNode,List<InsnNode>> findCommonInsns(MethodNode mth, MarkFinallyVisitor.TryExtractInfo tryInfo)
    • removeEmptyUpPath

      private static void removeEmptyUpPath(List<BlockNode> handlerBlocks, BlockNode startBlock)
    • copyCodeVars

      private static void copyCodeVars(InsnNode fromInsn, InsnNode toInsn)
    • copyCodeVars

      private static void copyCodeVars(InsnArg fromArg, InsnArg toArg)
    • undoFinallyVisitor

      private static void undoFinallyVisitor(MethodNode mth)
      Reload method without applying this visitor
    • getTerminusForHandler

      @Nullable private static @Nullable BlockNode getTerminusForHandler(ExceptionHandler handler, MarkFinallyVisitor.TryExtractInfo tryInfo)