Java Code Examples for org.apache.bcel.generic.MethodGen#getMaxLocals()

The following examples show how to use org.apache.bcel.generic.MethodGen#getMaxLocals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValueNumberAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ValueNumberAnalysis(MethodGen methodGen, DepthFirstSearch dfs, LoadedFieldSet loadedFieldSet,
        RepositoryLookupFailureCallback lookupFailureCallback) {

    super(dfs);
    this.methodGen = methodGen;
    this.factory = new ValueNumberFactory();
    ValueNumberCache cache = new ValueNumberCache();
    this.visitor = new ValueNumberFrameModelingVisitor(methodGen, factory, cache, loadedFieldSet, lookupFailureCallback);

    int numLocals = methodGen.getMaxLocals();
    this.entryLocalValueList = new ValueNumber[numLocals];
    for (int i = 0; i < numLocals; ++i) {
        this.entryLocalValueList[i] = factory.createFreshValue();
    }

    this.exceptionHandlerValueNumberMap = new IdentityHashMap<>();

    // For non-static methods, keep track of which value represents the
    // "this" reference
    if (!methodGen.isStatic()) {
        this.thisValue = entryLocalValueList[0];
    }

    this.factAtLocationMap = new HashMap<>();
    this.factAfterLocationMap = new HashMap<>();
    if (DEBUG) {
        System.out.println("VNA Analysis " + methodGen.getClassName() + "." + methodGen.getName() + " : "
                + methodGen.getSignature());
    }

}
 
Example 2
Source File: maxstack.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] argv) throws Exception {
    for (final String class_name : argv) {
        JavaClass java_class = Repository.lookupClass(class_name);

        if (java_class == null) {
            java_class = new ClassParser(class_name).parse();
        }

        final ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());

        for (final Method m : java_class.getMethods()) {
            if (!(m.isAbstract() || m.isNative())) {
                final MethodGen mg = new MethodGen(m, class_name, cp);

                final int compiled_stack = mg.getMaxStack();
                final int compiled_locals = mg.getMaxLocals();
                mg.setMaxStack(); // Recompute value
                mg.setMaxLocals();
                final int computed_stack = mg.getMaxStack();
                final int computed_locals = mg.getMaxLocals();

                mg.getInstructionList().dispose(); // Reuse instruction handles

                System.out.println(m);

                if (computed_stack == compiled_stack) {
                    System.out.println("Stack ok(" + computed_stack + ")");
                } else {
                    System.out.println("\nCompiled stack size " + compiled_stack + " computed size " + computed_stack);
                }

                if (computed_locals == compiled_locals) {
                    System.out.println("Locals ok(" + computed_locals + ")");
                } else {
                    System.out.println("\nCompiled locals " + compiled_locals + " computed size " + computed_locals);
                }
            }
        }
    }
}
 
Example 3
Source File: LiveLocalStoreAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public LiveLocalStoreAnalysis(MethodGen methodGen, ReverseDepthFirstSearch rdfs, DepthFirstSearch dfs) {
    super(rdfs, dfs);
    this.topBit = methodGen.getMaxLocals() * 2;
    this.killedByStoreOffset = methodGen.getMaxLocals();
}
 
Example 4
Source File: Pass3bVerifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Pass 3b implements the data flow analysis as described in the Java Virtual
 * Machine Specification, Second Edition.
 * Later versions will use LocalVariablesInfo objects to verify if the
 * verifier-inferred types and the class file's debug information (LocalVariables
 * attributes) match [TODO].
 *
 * @see org.apache.bcel.verifier.statics.LocalVariablesInfo
 * @see org.apache.bcel.verifier.statics.Pass2Verifier#getLocalVariablesInfo(int)
 */
@Override
public VerificationResult do_verify() {
    if (! myOwner.doPass3a(methodNo).equals(VerificationResult.VR_OK)) {
        return VerificationResult.VR_NOTYET;
    }

    // Pass 3a ran before, so it's safe to assume the JavaClass object is
    // in the BCEL repository.
    JavaClass jc;
    try {
        jc = Repository.lookupClass(myOwner.getClassName());
    } catch (final ClassNotFoundException e) {
        // FIXME: maybe not the best way to handle this
        throw new AssertionViolatedException("Missing class: " + e, e);
    }

    final ConstantPoolGen constantPoolGen = new ConstantPoolGen(jc.getConstantPool());
    // Init Visitors
    final InstConstraintVisitor icv = new InstConstraintVisitor();
    icv.setConstantPoolGen(constantPoolGen);

    final ExecutionVisitor ev = new ExecutionVisitor();
    ev.setConstantPoolGen(constantPoolGen);

    final Method[] methods = jc.getMethods(); // Method no "methodNo" exists, we ran Pass3a before on it!

    try{

        final MethodGen mg = new MethodGen(methods[methodNo], myOwner.getClassName(), constantPoolGen);

        icv.setMethodGen(mg);

        ////////////// DFA BEGINS HERE ////////////////
        if (! (mg.isAbstract() || mg.isNative()) ) { // IF mg HAS CODE (See pass 2)

            final ControlFlowGraph cfg = new ControlFlowGraph(mg);

            // Build the initial frame situation for this method.
            final Frame f = new Frame(mg.getMaxLocals(),mg.getMaxStack());
            if ( !mg.isStatic() ) {
                if (mg.getName().equals(Const.CONSTRUCTOR_NAME)) {
                    Frame.setThis(new UninitializedObjectType(ObjectType.getInstance(jc.getClassName())));
                    f.getLocals().set(0, Frame.getThis());
                }
                else{
                    Frame.setThis(null);
                    f.getLocals().set(0, ObjectType.getInstance(jc.getClassName()));
                }
            }
            final Type[] argtypes = mg.getArgumentTypes();
            int twoslotoffset = 0;
            for (int j=0; j<argtypes.length; j++) {
                if (argtypes[j] == Type.SHORT || argtypes[j] == Type.BYTE ||
                    argtypes[j] == Type.CHAR || argtypes[j] == Type.BOOLEAN) {
                    argtypes[j] = Type.INT;
                }
                f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), argtypes[j]);
                if (argtypes[j].getSize() == 2) {
                    twoslotoffset++;
                    f.getLocals().set(twoslotoffset + j + (mg.isStatic()?0:1), Type.UNKNOWN);
                }
            }
            circulationPump(mg,cfg, cfg.contextOf(mg.getInstructionList().getStart()), f, icv, ev);
        }
    }
    catch (final VerifierConstraintViolatedException ce) {
        ce.extendMessage("Constraint violated in method '"+methods[methodNo]+"':\n","");
        return new VerificationResult(VerificationResult.VERIFIED_REJECTED, ce.getMessage());
    }
    catch (final RuntimeException re) {
        // These are internal errors

        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        re.printStackTrace(pw);

        throw new AssertionViolatedException("Some RuntimeException occured while verify()ing class '"+jc.getClassName()+
            "', method '"+methods[methodNo]+"'. Original RuntimeException's stack trace:\n---\n"+sw+"---\n", re);
    }
    return VerificationResult.VR_OK;
}