    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.verifier.structurals;
    019
    020
    021import java.io.PrintWriter;
    022import java.io.StringWriter;
    023import java.util.ArrayList;
    024import java.util.List;
    025import java.util.Random;
    026import java.util.Vector;
    027
    028import org.apache.bcel.Const;
    029import org.apache.bcel.Repository;
    030import org.apache.bcel.classfile.JavaClass;
    031import org.apache.bcel.classfile.Method;
    032import org.apache.bcel.generic.ConstantPoolGen;
    033import org.apache.bcel.generic.InstructionHandle;
    034import org.apache.bcel.generic.JsrInstruction;
    035import org.apache.bcel.generic.MethodGen;
    036import org.apache.bcel.generic.ObjectType;
    037import org.apache.bcel.generic.RET;
    038import org.apache.bcel.generic.ReferenceType;
    039import org.apache.bcel.generic.ReturnInstruction;
    040import org.apache.bcel.generic.ReturnaddressType;
    041import org.apache.bcel.generic.Type;
    042import org.apache.bcel.verifier.PassVerifier;
    043import org.apache.bcel.verifier.VerificationResult;
    044import org.apache.bcel.verifier.Verifier;
    045import org.apache.bcel.verifier.exc.AssertionViolatedException;
    046import org.apache.bcel.verifier.exc.StructuralCodeConstraintException;
    047import org.apache.bcel.verifier.exc.VerifierConstraintViolatedException;
    048
    049/**
    050 * This PassVerifier verifies a method of class file according to pass 3,
    051 * so-called structural verification as described in The Java Virtual Machine
    052 * Specification, 2nd edition.
    053 * More detailed information is to be found at the do_verify() method's
    054 * documentation.
    055 *
    056 * @see #do_verify()
    057 */
    058
    059public final class Pass3bVerifier extends PassVerifier{
    060    /* TODO:    Throughout pass 3b, upper halves of LONG and DOUBLE
    061                        are represented by Type.UNKNOWN. This should be changed
    062                        in favour of LONG_Upper and DOUBLE_Upper as in pass 2. */
    063
    064    /**
    065     * An InstructionContextQueue is a utility class that holds
    066     * (InstructionContext, ArrayList) pairs in a Queue data structure.
    067     * This is used to hold information about InstructionContext objects
    068     * externally --- i.e. that information is not saved inside the
    069     * InstructionContext object itself. This is useful to save the
    070     * execution path of the symbolic execution of the
    071     * Pass3bVerifier - this is not information
    072     * that belongs into the InstructionContext object itself.
    073     * Only at "execute()"ing
    074     * time, an InstructionContext object will get the current information
    075     * we have about its symbolic execution predecessors.
    076     */
    077    private static final class InstructionContextQueue {
    078        // The following two fields together represent the queue.
    079        /** The first elements from pairs in the queue. */
    080        private final List<InstructionContext> ics = new Vector<>();
    081        /** The second elements from pairs in the queue. */
    082        private final List<ArrayList<InstructionContext>> ecs = new Vector<>();
    083
    084        /**
    085         * Adds an (InstructionContext, ExecutionChain) pair to this queue.
    086         *
    087         * @param ic the InstructionContext
    088         * @param executionChain the ExecutionChain
    089         */
    090        public void add(final InstructionContext ic, final ArrayList<InstructionContext> executionChain) {
    091            ics.add(ic);
    092            ecs.add(executionChain);
    093        }
    094
    095        /**
    096         * Tests if InstructionContext queue is empty.
    097         *
    098         * @return true if the InstructionContext queue is empty.
    099         */
    100        public boolean isEmpty() {
    101            return ics.isEmpty();
    102        }
    103
    104        /**
    105         * Removes a specific (InstructionContext, ExecutionChain) pair from their respective queues.
    106         *
    107         * @param i the index of the items to be removed
    108         */
    109        public void remove(final int i) {
    110            ics.remove(i);
    111            ecs.remove(i);
    112        }
    113
    114        /**
    115         * Gets a specific InstructionContext from the queue.
    116         *
    117         * @param i the index of the item to be fetched
    118         * @return the indicated InstructionContext
    119         */
    120        public InstructionContext getIC(final int i) {
    121            return ics.get(i);
    122        }
    123
    124        /**
    125         * Gets a specific ExecutionChain from the queue.
    126         *
    127         * @param i the index of the item to be fetched
    128         * @return the indicated ExecutionChain
    129         */
    130        public ArrayList<InstructionContext> getEC(final int i) {
    131            return ecs.get(i);
    132        }
    133
    134        /**
    135         * Gets the size of the InstructionContext queue.
    136         *
    137         * @return the size of the InstructionQueue
    138         */
    139        public int size() {
    140            return ics.size();
    141        }
    142    } // end Inner Class InstructionContextQueue
    143
    144    /** In DEBUG mode, the verification algorithm is not randomized. */
    145    private static final boolean DEBUG = true;
    146
    147    /** The Verifier that created this. */
    148    private final Verifier myOwner;
    149
    150    /** The method number to verify. */
    151    private final int methodNo;
    152
    153    /**
    154     * This class should only be instantiated by a Verifier.
    155     *
    156     * @see org.apache.bcel.verifier.Verifier
    157     */
    158    public Pass3bVerifier(final Verifier owner, final int method_no) {
    159        myOwner = owner;
    160        this.methodNo = method_no;
    161    }
    162
    163    /**
    164     * Whenever the outgoing frame
    165     * situation of an InstructionContext changes, all its successors are
    166     * put [back] into the queue [as if they were unvisited].
    167     * The proof of termination is about the existence of a
    168     * fix point of frame merging.
    169     */
    170    private void circulationPump(final MethodGen m,final ControlFlowGraph cfg, final InstructionContext start,
    171            final Frame vanillaFrame, final InstConstraintVisitor icv, final ExecutionVisitor ev) {
    172        final Random random = new Random();
    173        final InstructionContextQueue icq = new InstructionContextQueue();
    174
    175        start.execute(vanillaFrame, new ArrayList<InstructionContext>(), icv, ev);
    176        // new ArrayList() <=>    no Instruction was executed before
    177        //                                    => Top-Level routine (no jsr call before)
    178        icq.add(start, new ArrayList<InstructionContext>());
    179
    180        // LOOP!
    181        while (!icq.isEmpty()) {
    182            InstructionContext u;
    183            ArrayList<InstructionContext> ec;
    184            if (!DEBUG) {
    185                final int r = random.nextInt(icq.size());
    186                u = icq.getIC(r);
    187                ec = icq.getEC(r);
    188                icq.remove(r);
    189            }
    190            else{
    191                u  = icq.getIC(0);
    192                ec = icq.getEC(0);
    193                icq.remove(0);
    194            }
    195
    196            @SuppressWarnings("unchecked") // ec is of type ArrayList<InstructionContext>
    197            final
    198            ArrayList<InstructionContext> oldchain = (ArrayList<InstructionContext>) (ec.clone());
    199            @SuppressWarnings("unchecked") // ec is of type ArrayList<InstructionContext>
    200            final
    201            ArrayList<InstructionContext> newchain = (ArrayList<InstructionContext>) (ec.clone());
    202            newchain.add(u);
    203
    204            if ((u.getInstruction().getInstruction()) instanceof RET) {
    205//System.err.println(u);
    206                // We can only follow _one_ successor, the one after the
    207                // JSR that was recently executed.
    208                final RET ret = (RET) (u.getInstruction().getInstruction());
    209                final ReturnaddressType t = (ReturnaddressType) u.getOutFrame(oldchain).getLocals().get(ret.getIndex());
    210                final InstructionContext theSuccessor = cfg.contextOf(t.getTarget());
    211
    212                // Sanity check
    213                InstructionContext lastJSR = null;
    214                int skip_jsr = 0;
    215                for (int ss=oldchain.size()-1; ss >= 0; ss--) {
    216                    if (skip_jsr < 0) {
    217                        throw new AssertionViolatedException("More RET than JSR in execution chain?!");
    218                    }
    219//System.err.println("+"+oldchain.get(ss));
    220                    if ((oldchain.get(ss)).getInstruction().getInstruction() instanceof JsrInstruction) {
    221                        if (skip_jsr == 0) {
    222                            lastJSR = oldchain.get(ss);
    223                            break;
    224                        }
    225                        skip_jsr--;
    226                    }
    227                    if ((oldchain.get(ss)).getInstruction().getInstruction() instanceof RET) {
    228                        skip_jsr++;
    229                    }
    230                }
    231                if (lastJSR == null) {
    232                    throw new AssertionViolatedException("RET without a JSR before in ExecutionChain?! EC: '"+oldchain+"'.");
    233                }
    234                final JsrInstruction jsr = (JsrInstruction) (lastJSR.getInstruction().getInstruction());
    235                if ( theSuccessor != (cfg.contextOf(jsr.physicalSuccessor())) ) {
    236                    throw new AssertionViolatedException("RET '"+u.getInstruction()+"' info inconsistent: jump back to '"+
    237                        theSuccessor+"' or '"+cfg.contextOf(jsr.physicalSuccessor())+"'?");
    238                }
    239
    240                if (theSuccessor.execute(u.getOutFrame(oldchain), newchain, icv, ev)) {
    241                    @SuppressWarnings("unchecked") // newchain is already of type ArrayList<InstructionContext>
    242                    final
    243                    ArrayList<InstructionContext> newchainClone = (ArrayList<InstructionContext>) newchain.clone();
    244                    icq.add(theSuccessor, newchainClone);
    245                }
    246            }
    247            else{// "not a ret"
    248
    249                // Normal successors. Add them to the queue of successors.
    250                final InstructionContext[] succs = u.getSuccessors();
    251                for (final InstructionContext v : succs) {
    252                    if (v.execute(u.getOutFrame(oldchain), newchain, icv, ev)) {
    253                        @SuppressWarnings("unchecked") // newchain is already of type ArrayList<InstructionContext>
    254                        final
    255                        ArrayList<InstructionContext> newchainClone = (ArrayList<InstructionContext>) newchain.clone();
    256                        icq.add(v, newchainClone);
    257                    }
    258                }
    259            }// end "not a ret"
    260
    261            // Exception Handlers. Add them to the queue of successors.
    262            // [subroutines are never protected; mandated by JustIce]
    263            final ExceptionHandler[] exc_hds = u.getExceptionHandlers();
    264            for (final ExceptionHandler exc_hd : exc_hds) {
    265                final InstructionContext v = cfg.contextOf(exc_hd.getHandlerStart());
    266                // TODO: the "oldchain" and "newchain" is used to determine the subroutine
    267                // we're in (by searching for the last JSR) by the InstructionContext
    268                // implementation. Therefore, we should not use this chain mechanism
    269                // when dealing with exception handlers.
    270                // Example: a JSR with an exception handler as its successor does not
    271                // mean we're in a subroutine if we go to the exception handler.
    272                // We should address this problem later; by now we simply "cut" the chain
    273                // by using an empty chain for the exception handlers.
    274                //if (v.execute(new Frame(u.getOutFrame(oldchain).getLocals(),
    275                // new OperandStack (u.getOutFrame().getStack().maxStack(),
    276                // (exc_hds[s].getExceptionType()==null? Type.THROWABLE : exc_hds[s].getExceptionType())) ), newchain), icv, ev) {
    277                    //icq.add(v, (ArrayList) newchain.clone());
    278                if (v.execute(new Frame(u.getOutFrame(oldchain).getLocals(),
    279                        new OperandStack (u.getOutFrame(oldchain).getStack().maxStack(),
    280                        exc_hd.getExceptionType()==null? Type.THROWABLE : exc_hd.getExceptionType())),
    281                        new ArrayList<InstructionContext>(), icv, ev)) {
    282                    icq.add(v, new ArrayList<InstructionContext>());
    283                }
    284            }
    285
    286        }// while (!icq.isEmpty()) END
    287
    288        InstructionHandle ih = start.getInstruction();
    289        do{
    290            if ((ih.getInstruction() instanceof ReturnInstruction) && (!(cfg.isDead(ih)))) {
    291                final InstructionContext ic = cfg.contextOf(ih);
    292                // TODO: This is buggy, we check only the top-level return instructions this way.
    293                // Maybe some maniac returns from a method when in a subroutine?
    294                final Frame f = ic.getOutFrame(new ArrayList<InstructionContext>());
    295                final LocalVariables lvs = f.getLocals();
    296                for (int i=0; i<lvs.maxLocals(); i++) {
    297                    if (lvs.get(i) instanceof UninitializedObjectType) {
    298                        this.addMessage("Warning: ReturnInstruction '"+ic+
    299                            "' may leave method with an uninitialized object in the local variables array '"+lvs+"'.");
    300                    }
    301                }
    302                final OperandStack os = f.getStack();
    303                for (int i=0; i<os.size(); i++) {
    304                    if (os.peek(i) instanceof UninitializedObjectType) {
    305                        this.addMessage("Warning: ReturnInstruction '"+ic+
    306                            "' may leave method with an uninitialized object on the operand stack '"+os+"'.");
    307                    }
    308                }
    309                //see JVM $4.8.2
    310                Type returnedType = null;
    311                final OperandStack inStack = ic.getInFrame().getStack();
    312                if (inStack.size() >= 1) {
    313                    returnedType = inStack.peek();
    314                } else {
    315                    returnedType = Type.VOID;
    316                }
    317
    318                if (returnedType != null) {
    319                    if (returnedType instanceof ReferenceType) {
    320                        try {
    321                            if (!((ReferenceType) returnedType).isCastableTo(m.getReturnType())) {
    322                                invalidReturnTypeError(returnedType, m);
    323                            }
    324                        } catch (final ClassNotFoundException e) {
    325                            // Don't know what do do now, so raise RuntimeException
    326                            throw new IllegalArgumentException(e);
    327                        }
    328                    } else if (!returnedType.equals(m.getReturnType().normalizeForStackOrLocal())) {
    329                        invalidReturnTypeError(returnedType, m);
    330                    }
    331                }
    332            }
    333        } while ((ih = ih.getNext()) != null);
    334
    335     }
    336
    337    /**
    338     * Throws an exception indicating the returned type is not compatible with the return type of the given method.
    339     *
    340     * @param returnedType the type of the returned expression
    341     * @param m the method we are processing
    342     * @throws StructuralCodeConstraintException always
    343     * @since 6.0
    344     */
    345    public void invalidReturnTypeError(final Type returnedType, final MethodGen m) {
    346        throw new StructuralCodeConstraintException(
    347            "Returned type "+returnedType+" does not match Method's return type "+m.getReturnType());
    348    }
    349
    350    /**
    351     * Pass 3b implements the data flow analysis as described in the Java Virtual
    352     * Machine Specification, Second Edition.
    353     * Later versions will use LocalVariablesInfo objects to verify if the
    354     * verifier-inferred types and the class file's debug information (LocalVariables
    355     * attributes) match [TODO].
    356     *
    357     * @see org.apache.bcel.verifier.statics.LocalVariablesInfo
    358     * @see org.apache.bcel.verifier.statics.Pass2Verifier#getLocalVariablesInfo(int)
    359     */
    360    @Override
    361    public VerificationResult do_verify() {
    362        if (! myOwner.doPass3a(methodNo).equals(VerificationResult.VR_OK)) {
    363            return VerificationResult.VR_NOTYET;
    364        }
    365
    366        // Pass 3a ran before, so it's safe to assume the JavaClass object is
    367        // in the BCEL repository.
    368        JavaClass jc;
    369        try {
    370            jc = Repository.lookupClass(myOwner.getClassName());
    371        } catch (final ClassNotFoundException e) {
    372            // FIXME: maybe not the best way to handle this
    373            throw new AssertionViolatedException("Missing class: " + e, e);
    374        }
    375
    376        final ConstantPoolGen constantPoolGen = new ConstantPoolGen(jc.getConstantPool());
    377        // Init Visitors
    378        final InstConstraintVisitor icv = new InstConstraintVisitor();
    379        icv.setConstantPoolGen(constantPoolGen);
    380
    381        final ExecutionVisitor ev = new ExecutionVisitor();
    382        ev.setConstantPoolGen(constantPoolGen);
    383
    384        final Method[] methods = jc.getMethods(); // Method no "methodNo" exists, we ran Pass3a before on it!
    385
    386        try{
    387
    388            final MethodGen mg = new MethodGen(methods[methodNo], myOwner.getClassName(), constantPoolGen);
    389
    390            icv.setMethodGen(mg);
    391
    392            ////////////// DFA BEGINS HERE ////////////////
    393            if (! (mg.isAbstract() || mg.isNative()) ) { // IF mg HAS CODE (See pass 2)
    394
    395                final ControlFlowGraph cfg = new ControlFlowGraph(mg);
    396
    397                // Build the initial frame situation for this method.
    398                final Frame f = new Frame(mg.getMaxLocals(),mg.getMaxStack());
    399                if ( !mg.isStatic() ) {
    400                    if (mg.getName().equals(Const.CONSTRUCTOR_NAME)) {
    401                        Frame.setThis(new UninitializedObjectType(ObjectType.getInstance(jc.getClassName())));
    402                        f.getLocals().set(0, Frame.getThis());
    403                    }
    404                    else{
    405                        Frame.setThis(null);
    406                        f.getLocals().set(0, ObjectType.getInstance(jc.getClassName()));
    407                    }
    408                }
    409                final Type[] argtypes = mg.getArgumentTypes();
    410                int twoslotoffset = 0;
    411                for (int j=0; j<argtypes.length; j++) {
    412                    if (argtypes[j] == Type.SHORT || argtypes[j] == Type.BYTE ||
    413                        argtypes[j] == Type.CHAR || argtypes[j] == Type.BOOLEAN) {
    414                        argtypes[j] = Type.INT;
    415                    }
    416                    f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), argtypes[j]);
    417                    if (argtypes[j].getSize() == 2) {
    418                        twoslotoffset++;
    419                        f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), Type.UNKNOWN);
    420                    }
    421                }
    422                circulationPump(mg,cfg, cfg.contextOf(mg.getInstructionList().getStart()), f, icv, ev);
    423            }
    424        }
    425        catch (final VerifierConstraintViolatedException ce) {
    426            ce.extendMessage("Constraint violated in method '"+methods[methodNo]+"':\n","");
    427            return new VerificationResult(VerificationResult.VERIFIED_REJECTED, ce.getMessage());
    428        }
    429        catch (final RuntimeException re) {
    430            // These are internal errors
    431
    432            final StringWriter sw = new StringWriter();
    433            final PrintWriter pw = new PrintWriter(sw);
    434            re.printStackTrace(pw);
    435
    436            throw new AssertionViolatedException("Some RuntimeException occured while verify()ing class '"+jc.getClassName()+
    437                "', method '"+methods[methodNo]+"'. Original RuntimeException's stack trace:\n---\n"+sw+"---\n", re);
    438        }
    439        return VerificationResult.VR_OK;
    440    }
    441
    442    /** Returns the method number as supplied when instantiating. */
    443    public int getMethodNo() {
    444        return methodNo;
    445    }
    446}