Java Code Examples for org.apache.bcel.generic.Instruction#consumeStack()

The following examples show how to use org.apache.bcel.generic.Instruction#consumeStack() . 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: RedundantConditions.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param methodGen method
 * @param start instruction to scan
 * @return instruction which consumes value which was on top of stack before start instruction
 * or null if cannot be determined
 */
private InstructionHandle getConsumer(MethodGen methodGen, InstructionHandle start) {
    int depth = 1;
    InstructionHandle cur = start;
    while (cur != null) {
        Instruction inst = cur.getInstruction();
        depth -= inst.consumeStack(methodGen.getConstantPool());
        if (depth <= 0) {
            return cur;
        }
        depth += inst.produceStack(methodGen.getConstantPool());
        if (inst instanceof BranchInstruction) {
            if (inst instanceof GotoInstruction) {
                cur = ((GotoInstruction) inst).getTarget();
                continue;
            }
            if (!(inst instanceof IfInstruction)) {
                return null;
            }
        }
        cur = cur.getNext();
    }
    return null;
}
 
Example 2
Source File: ValueNumberFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void checkConsumedAndProducedValues(Instruction ins, ValueNumber[] consumedValueList, ValueNumber[] producedValueList) {
    int numConsumed = ins.consumeStack(getCPG());
    int numProduced = ins.produceStack(getCPG());

    if (numConsumed == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack consumption for " + ins);
    }
    if (numProduced == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack production for " + ins);
    }

    if (consumedValueList.length != numConsumed) {
        throw new IllegalStateException("Wrong number of values consumed for " + ins + ": expected " + numConsumed + ", got "
                + consumedValueList.length);
    }

    if (producedValueList.length != numProduced) {
        throw new IllegalStateException("Wrong number of values produced for " + ins + ": expected " + numProduced + ", got "
                + producedValueList.length);
    }
}
 
Example 3
Source File: StackDepthAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, StackDepth fact)
        throws DataflowAnalysisException {
    Instruction ins = handle.getInstruction();
    int produced = ins.produceStack(cpg);
    int consumed = ins.consumeStack(cpg);
    if (produced == Const.UNPREDICTABLE || consumed == Const.UNPREDICTABLE) {
        throw new IllegalStateException("Unpredictable stack delta for instruction: " + handle);
    }
    int depth = fact.getDepth();
    depth += (produced - consumed);
    if (depth < 0) {
        fact.setDepth(BOTTOM);
    } else {
        fact.setDepth(depth);
    }
}
 
Example 4
Source File: Frame.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get the slot the object instance referred to by given instruction is
 * located in.
 *
 * @param ins
 *            the Instruction
 * @param cpg
 *            the ConstantPoolGen for the method
 * @return stack slot the object instance is in
 * @throws DataflowAnalysisException
 */
public int getInstanceSlot(Instruction ins, ConstantPoolGen cpg) throws DataflowAnalysisException {
    if (!isValid()) {
        throw new DataflowAnalysisException("Accessing invalid frame at " + ins);
    }
    int numConsumed = ins.consumeStack(cpg);
    if (numConsumed == Const.UNPREDICTABLE) {
        throw new DataflowAnalysisException("Unpredictable stack consumption in " + ins);
    }
    if (numConsumed > getStackDepth()) {
        throw new DataflowAnalysisException("Stack underflow " + ins);
    }
    return getNumSlots() - numConsumed;
}
 
Example 5
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Return whether or not the given instruction can throw exceptions.
 *
 * @param handle
 *            the instruction
 * @return true if the instruction can throw an exception, false otherwise
 * @throws CFGBuilderException
 */
private boolean isPEI(InstructionHandle handle) throws CFGBuilderException {
    Instruction ins = handle.getInstruction();

    if (!(ins instanceof ExceptionThrower)) {
        return false;
    }

    if (ins instanceof NEW) {
        return false;
    }
    // if (ins instanceof ATHROW) return false;
    if (ins instanceof GETSTATIC) {
        return false;
    }
    if (ins instanceof PUTSTATIC) {
        return false;
    }
    if (ins instanceof ReturnInstruction) {
        return false;
    }
    if (ins instanceof INSTANCEOF) {
        return false;
    }
    if (ins instanceof MONITOREXIT) {
        return false;
    }
    if (ins instanceof LDC) {
        return false;
    }
    if (ins instanceof GETFIELD && !methodGen.isStatic()) {
        // Assume that GETFIELD on this object is not PEI
        return !isSafeFieldSource(handle.getPrev());
    }
    if (ins instanceof PUTFIELD && !methodGen.isStatic()) {
        // Assume that PUTFIELD on this object is not PEI
        int depth = ins.consumeStack(cpg);
        for (InstructionHandle prev = handle.getPrev(); prev != null; prev = prev.getPrev()) {
            Instruction prevInst = prev.getInstruction();
            if (prevInst instanceof BranchInstruction) {
                if (prevInst instanceof GotoInstruction) {
                    // Currently we support only jumps to the PUTFIELD itself
                    // This will cover simple cases like this.a = flag ? foo : bar
                    if (((BranchInstruction) prevInst).getTarget() == handle) {
                        depth = ins.consumeStack(cpg);
                    } else {
                        return true;
                    }
                } else if (!(prevInst instanceof IfInstruction)) {
                    // As IF instructions may fall through then the stack depth remains unchanged
                    // Actually we should not go here for normal Java bytecode: switch or jsr should not appear in this context
                    return true;
                }
            }
            depth = depth - prevInst.produceStack(cpg) + prevInst.consumeStack(cpg);
            if (depth < 1) {
                throw new CFGBuilderException("Invalid stack at " + prev + " when checking " + handle);
            }
            if (depth == 1) {
                InstructionHandle prevPrev = prev.getPrev();
                if (prevPrev != null && prevPrev.getInstruction() instanceof BranchInstruction) {
                    continue;
                }
                return !isSafeFieldSource(prevPrev);
            }
        }
    }
    return true;
}
 
Example 6
Source File: Frame.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Get the stack location (counting down from top of stack, starting at 0)
 * containing the object instance referred to by given instruction. This
 * relies on the observation that in instructions which use an object
 * instance (such as getfield, invokevirtual, etc.), the object instance is
 * the first operand used by the instruction.
 *
 * <p>
 * The value returned may be passed to getStackValue(int).
 * </p>
 *
 * @param ins
 *            the Instruction
 * @param cpg
 *            the ConstantPoolGen for the method
 * @return stack location (counting down from top of stack, starting at 0)
 *         containing the object instance
 * @throws DataflowAnalysisException
 */
public int getInstanceStackLocation(Instruction ins, ConstantPoolGen cpg) throws DataflowAnalysisException {
    int numConsumed = ins.consumeStack(cpg);
    if (numConsumed == Const.UNPREDICTABLE) {
        throw new DataflowAnalysisException("Unpredictable stack consumption in " + ins);
    }
    return numConsumed - 1;
}