ControlFlowGraph.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.verifier.structurals;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;

  23. import org.apache.bcel.generic.ATHROW;
  24. import org.apache.bcel.generic.BranchInstruction;
  25. import org.apache.bcel.generic.GotoInstruction;
  26. import org.apache.bcel.generic.Instruction;
  27. import org.apache.bcel.generic.InstructionHandle;
  28. import org.apache.bcel.generic.JsrInstruction;
  29. import org.apache.bcel.generic.MethodGen;
  30. import org.apache.bcel.generic.RET;
  31. import org.apache.bcel.generic.ReturnInstruction;
  32. import org.apache.bcel.generic.Select;
  33. import org.apache.bcel.verifier.exc.AssertionViolatedException;
  34. import org.apache.bcel.verifier.exc.StructuralCodeConstraintException;

  35. /**
  36.  * This class represents a control flow graph of a method.
  37.  */
  38. public class ControlFlowGraph {

  39.     /**
  40.      * Objects of this class represent a node in a ControlFlowGraph. These nodes are instructions, not basic blocks.
  41.      */
  42.     private final class InstructionContextImpl implements InstructionContext {

  43.         /**
  44.          * The TAG field is here for external temporary flagging, such as graph coloring.
  45.          *
  46.          * @see #getTag()
  47.          * @see #setTag(int)
  48.          */
  49.         private int TAG;

  50.         /**
  51.          * The InstructionHandle this InstructionContext is wrapped around.
  52.          */
  53.         private final InstructionHandle instruction;

  54.         /**
  55.          * The 'incoming' execution Frames.
  56.          */
  57.         private final Map<InstructionContext, Frame> inFrames; // key: the last-executed JSR

  58.         /**
  59.          * The 'outgoing' execution Frames.
  60.          */
  61.         private final Map<InstructionContext, Frame> outFrames; // key: the last-executed JSR

  62.         /**
  63.          * The 'execution predecessors' - a list of type InstructionContext of those instances that have been execute()d before
  64.          * in that order.
  65.          */
  66.         private List<InstructionContext> executionPredecessors; // Type: InstructionContext

  67.         /**
  68.          * Creates an InstructionHandleImpl object from an InstructionHandle. Creation of one per InstructionHandle suffices.
  69.          * Don't create more.
  70.          */
  71.         public InstructionContextImpl(final InstructionHandle inst) {
  72.             if (inst == null) {
  73.                 throw new AssertionViolatedException("Cannot instantiate InstructionContextImpl from NULL.");
  74.             }

  75.             instruction = inst;
  76.             inFrames = new HashMap<>();
  77.             outFrames = new HashMap<>();
  78.         }

  79.         /**
  80.          * A utility method that calculates the successors of a given InstructionHandle That means, a RET does have successors
  81.          * as defined here. A JsrInstruction has its target as its successor (opposed to its physical successor) as defined
  82.          * here.
  83.          */
  84. // TODO: implement caching!
  85.         private InstructionHandle[] _getSuccessors() {
  86.             final InstructionHandle[] single = new InstructionHandle[1];

  87.             final Instruction inst = getInstruction().getInstruction();

  88.             if (inst instanceof RET) {
  89.                 final Subroutine s = subroutines.subroutineOf(getInstruction());
  90.                 if (s == null) { // return empty;
  91.                     // RET in dead code. "empty" would be the correct answer, but we know something about the surrounding project...
  92.                     throw new AssertionViolatedException("Asking for successors of a RET in dead code?!");
  93.                 }

  94. //TODO: remove. Only JustIce must not use it, but foreign users of the ControlFlowGraph
  95. //      will want it. Thanks Johannes Wust.
  96. //throw new AssertionViolatedException("DID YOU REALLY WANT TO ASK FOR RET'S SUCCS?");

  97.                 final InstructionHandle[] jsrs = s.getEnteringJsrInstructions();
  98.                 final InstructionHandle[] ret = new InstructionHandle[jsrs.length];
  99.                 Arrays.setAll(ret, i -> jsrs[i].getNext());
  100.                 return ret;
  101.             }

  102.             // Terminates method normally.
  103.             // Terminates method abnormally, because JustIce mandates
  104.             // subroutines not to be protected by exception handlers.
  105.             if (inst instanceof ReturnInstruction || inst instanceof ATHROW) {
  106.                 return InstructionHandle.EMPTY_ARRAY;
  107.             }

  108.             // See method comment.
  109.             if (inst instanceof JsrInstruction) {
  110.                 single[0] = ((JsrInstruction) inst).getTarget();
  111.                 return single;
  112.             }

  113.             if (inst instanceof GotoInstruction) {
  114.                 single[0] = ((GotoInstruction) inst).getTarget();
  115.                 return single;
  116.             }

  117.             if (inst instanceof BranchInstruction) {
  118.                 if (inst instanceof Select) {
  119.                     // BCEL's getTargets() returns only the non-default targets,
  120.                     // thanks to Eli Tilevich for reporting.
  121.                     final InstructionHandle[] matchTargets = ((Select) inst).getTargets();
  122.                     final InstructionHandle[] ret = new InstructionHandle[matchTargets.length + 1];
  123.                     ret[0] = ((Select) inst).getTarget();
  124.                     System.arraycopy(matchTargets, 0, ret, 1, matchTargets.length);
  125.                     return ret;
  126.                 }
  127.                 final InstructionHandle[] pair = new InstructionHandle[2];
  128.                 pair[0] = getInstruction().getNext();
  129.                 pair[1] = ((BranchInstruction) inst).getTarget();
  130.                 return pair;
  131.             }

  132.             // default case: Fall through.
  133.             single[0] = getInstruction().getNext();
  134.             return single;
  135.         }

  136.         /**
  137.          * "Merges in" (vmspec2, page 146) the "incoming" frame situation; executes the instructions symbolically and therefore
  138.          * calculates the "outgoing" frame situation. Returns: True iff the "incoming" frame situation changed after merging
  139.          * with "inFrame". The execPreds ArrayList must contain the InstructionContext objects executed so far in the correct
  140.          * order. This is just one execution path [out of many]. This is needed to correctly "merge" in the special case of a
  141.          * RET's successor. <B>The InstConstraintVisitor and ExecutionVisitor instances must be set up correctly.</B>
  142.          *
  143.          * @return true - if and only if the "outgoing" frame situation changed from the one before execute()ing.
  144.          */
  145.         @Override
  146.         public boolean execute(final Frame inFrame, final ArrayList<InstructionContext> execPreds, final InstConstraintVisitor icv, final ExecutionVisitor ev) {

  147.             @SuppressWarnings("unchecked") // OK because execPreds is compatible type
  148.             final List<InstructionContext> clone = (List<InstructionContext>) execPreds.clone();
  149.             executionPredecessors = clone;

  150.             // sanity check
  151.             if (lastExecutionJSR() == null && subroutines.subroutineOf(getInstruction()) != subroutines.getTopLevel()
  152.                 || lastExecutionJSR() != null && subroutines.subroutineOf(getInstruction()) == subroutines.getTopLevel()) {
  153.                 throw new AssertionViolatedException("Huh?! Am I '" + this + "' part of a subroutine or not?");
  154.             }

  155.             Frame inF = inFrames.get(lastExecutionJSR());
  156.             if (inF == null) { // no incoming frame was set, so set it.
  157.                 inFrames.put(lastExecutionJSR(), inFrame);
  158.                 inF = inFrame;
  159.             } else if (inF.equals(inFrame) || !mergeInFrames(inFrame)) { // if there was an "old" inFrame
  160.                 return false;
  161.             }

  162.             // Now we're sure the inFrame has changed!

  163.             // new inFrame is already merged in, see above.
  164.             final Frame workingFrame = inF.getClone();

  165.             try {
  166.                 // This verifies the InstructionConstraint for the current
  167.                 // instruction, but does not modify the workingFrame object.
  168. //InstConstraintVisitor icv = InstConstraintVisitor.getInstance(VerifierFactory.getVerifier(method_gen.getClassName()));
  169.                 icv.setFrame(workingFrame);
  170.                 getInstruction().accept(icv);
  171.             } catch (final StructuralCodeConstraintException ce) {
  172.                 ce.extendMessage("", "\nInstructionHandle: " + getInstruction() + "\n");
  173.                 ce.extendMessage("", "\nExecution Frame:\n" + workingFrame);
  174.                 extendMessageWithFlow(ce);
  175.                 throw ce;
  176.             }

  177.             // This executes the Instruction.
  178.             // Therefore the workingFrame object is modified.
  179. //ExecutionVisitor ev = ExecutionVisitor.getInstance(VerifierFactory.getVerifier(method_gen.getClassName()));
  180.             ev.setFrame(workingFrame);
  181.             getInstruction().accept(ev);
  182.             // getInstruction().accept(ExecutionVisitor.withFrame(workingFrame));
  183.             outFrames.put(lastExecutionJSR(), workingFrame);

  184.             return true; // new inFrame was different from old inFrame so merging them
  185.                          // yielded a different this.inFrame.
  186.         }

  187.         /**
  188.          * Extends the StructuralCodeConstraintException ("e") object with an at-the-end-extended message. This extended message
  189.          * will then reflect the execution flow needed to get to the constraint violation that triggered the throwing of the "e"
  190.          * object.
  191.          */
  192.         private void extendMessageWithFlow(final StructuralCodeConstraintException e) {
  193.             final String s = "Execution flow:\n";
  194.             e.extendMessage("", s + getExecutionChain());
  195.         }

  196.         /**
  197.          * Returns the exception handlers of this instruction.
  198.          */
  199.         @Override
  200.         public ExceptionHandler[] getExceptionHandlers() {
  201.             return exceptionhandlers.getExceptionHandlers(getInstruction());
  202.         }

  203.         /**
  204.          * Returns the control flow execution chain. This is built while execute(Frame, ArrayList)-ing the code represented by
  205.          * the surrounding ControlFlowGraph.
  206.          */
  207.         private String getExecutionChain() {
  208.             final StringBuilder s = new StringBuilder(toString());
  209.             for (int i = executionPredecessors.size() - 1; i >= 0; i--) {
  210.                 s.insert(0, executionPredecessors.get(i) + "\n");
  211.             }
  212.             return s.toString();
  213.         }

  214.         @Override
  215.         public Frame getInFrame() {
  216.             Frame org;

  217.             final InstructionContext jsr = lastExecutionJSR();

  218.             org = inFrames.get(jsr);

  219.             if (org == null) {
  220.                 throw new AssertionViolatedException("inFrame not set! This:\n" + this + "\nInFrames: '" + inFrames + "'.");
  221.             }
  222.             return org.getClone();
  223.         }

  224.         @Override
  225.         public InstructionHandle getInstruction() {
  226.             return instruction;
  227.         }

  228.         /**
  229.          * Returns a clone of the "outgoing" frame situation with respect to the given ExecutionChain.
  230.          */
  231.         @Override
  232.         public Frame getOutFrame(final ArrayList<InstructionContext> execChain) {
  233.             executionPredecessors = execChain;

  234.             Frame org;

  235.             final InstructionContext jsr = lastExecutionJSR();

  236.             org = outFrames.get(jsr);

  237.             if (org == null) {
  238.                 throw new AssertionViolatedException(
  239.                     "outFrame not set! This:\n" + this + "\nExecutionChain: " + getExecutionChain() + "\nOutFrames: '" + outFrames + "'.");
  240.             }
  241.             return org.getClone();
  242.         }

  243.         @Override
  244.         public InstructionContext[] getSuccessors() {
  245.             return contextsOf(_getSuccessors());
  246.         }

  247.         @Override
  248.         public int getTag() {
  249.             return TAG;
  250.         }

  251.         /**
  252.          * Returns the InstructionContextImpl with an JSR/JSR_W that was last in the ExecutionChain, without a corresponding
  253.          * RET, i.e. we were called by this one. Returns null if we were called from the top level.
  254.          */
  255.         private InstructionContextImpl lastExecutionJSR() {

  256.             final int size = executionPredecessors.size();
  257.             int retcount = 0;

  258.             for (int i = size - 1; i >= 0; i--) {
  259.                 final InstructionContextImpl current = (InstructionContextImpl) executionPredecessors.get(i);
  260.                 final Instruction currentlast = current.getInstruction().getInstruction();
  261.                 if (currentlast instanceof RET) {
  262.                     retcount++;
  263.                 }
  264.                 if (currentlast instanceof JsrInstruction) {
  265.                     retcount--;
  266.                     if (retcount == -1) {
  267.                         return current;
  268.                     }
  269.                 }
  270.             }
  271.             return null;
  272.         }

  273.         /**
  274.          * Does the actual merging (vmspec2, page 146). Returns true IFF this.inFrame was changed in course of merging with
  275.          * inFrame.
  276.          */
  277.         private boolean mergeInFrames(final Frame inFrame) {
  278.             // TODO: Can be performance-improved.
  279.             final Frame inF = inFrames.get(lastExecutionJSR());
  280.             final OperandStack oldstack = inF.getStack().getClone();
  281.             final LocalVariables oldlocals = inF.getLocals().getClone();
  282.             try {
  283.                 inF.getStack().merge(inFrame.getStack());
  284.                 inF.getLocals().merge(inFrame.getLocals());
  285.             } catch (final StructuralCodeConstraintException sce) {
  286.                 extendMessageWithFlow(sce);
  287.                 throw sce;
  288.             }
  289.             return !(oldstack.equals(inF.getStack()) && oldlocals.equals(inF.getLocals()));
  290.         }

  291.         /* Satisfies InstructionContext.setTag(int). */
  292.         @Override
  293.         public void setTag(final int tag) { // part of InstructionContext interface
  294.             TAG = tag;
  295.         }

  296.         /**
  297.          * Returns a simple String representation of this InstructionContext.
  298.          */
  299.         @Override
  300.         public String toString() {
  301.             // TODO: Put information in the brackets, e.g.
  302.             // Is this an ExceptionHandler? Is this a RET? Is this the start of
  303.             // a subroutine?
  304.             return getInstruction().toString(false) + "\t[InstructionContext]";
  305.         }

  306.     } // End Inner InstructionContextImpl Class.

  307.     /// ** The MethodGen object we're working on. */
  308.     // private final MethodGen method_gen;

  309.     /** The Subroutines object for the method whose control flow is represented by this ControlFlowGraph. */
  310.     private final Subroutines subroutines;

  311.     /** The ExceptionHandlers object for the method whose control flow is represented by this ControlFlowGraph. */
  312.     private final ExceptionHandlers exceptionhandlers;

  313.     /** All InstructionContext instances of this ControlFlowGraph. */
  314.     private final Map<InstructionHandle, InstructionContext> instructionContexts = new HashMap<>();

  315.     /**
  316.      * A Control Flow Graph; with additional JustIce checks
  317.      *
  318.      * @param methodGen the method generator instance
  319.      */
  320.     public ControlFlowGraph(final MethodGen methodGen) {
  321.         this(methodGen, true);
  322.     }

  323.     /**
  324.      * A Control Flow Graph.
  325.      *
  326.      * @param methodGen the method generator instance
  327.      * @param enableJustIceCheck if true, additional JustIce checks are performed
  328.      * @since 6.0
  329.      */
  330.     public ControlFlowGraph(final MethodGen methodGen, final boolean enableJustIceCheck) {
  331.         subroutines = new Subroutines(methodGen, enableJustIceCheck);
  332.         exceptionhandlers = new ExceptionHandlers(methodGen);

  333.         final InstructionHandle[] instructionhandles = methodGen.getInstructionList().getInstructionHandles();
  334.         for (final InstructionHandle instructionhandle : instructionhandles) {
  335.             instructionContexts.put(instructionhandle, new InstructionContextImpl(instructionhandle));
  336.         }

  337.         // this.method_gen = method_gen;
  338.     }

  339.     /**
  340.      * Returns the InstructionContext of a given instruction.
  341.      */
  342.     public InstructionContext contextOf(final InstructionHandle inst) {
  343.         final InstructionContext ic = instructionContexts.get(inst);
  344.         if (ic == null) {
  345.             throw new AssertionViolatedException("InstructionContext requested for an InstructionHandle that's not known!");
  346.         }
  347.         return ic;
  348.     }

  349.     /**
  350.      * Returns the InstructionContext[] of a given InstructionHandle[], in a naturally ordered manner.
  351.      */
  352.     public InstructionContext[] contextsOf(final InstructionHandle[] insts) {
  353.         final InstructionContext[] ret = new InstructionContext[insts.length];
  354.         Arrays.setAll(ret, i -> contextOf(insts[i]));
  355.         return ret;
  356.     }

  357.     /**
  358.      * Returns an InstructionContext[] with all the InstructionContext instances for the method whose control flow is
  359.      * represented by this ControlFlowGraph <B>(NOT ORDERED!)</B>.
  360.      */
  361.     public InstructionContext[] getInstructionContexts() {
  362.         final InstructionContext[] ret = new InstructionContext[instructionContexts.size()];
  363.         return instructionContexts.values().toArray(ret);
  364.     }

  365.     /**
  366.      * Returns true, if and only if the said instruction is not reachable; that means, if it is not part of this
  367.      * ControlFlowGraph.
  368.      */
  369.     public boolean isDead(final InstructionHandle i) {
  370.         return subroutines.subroutineOf(i) == null;
  371.     }
  372. }