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

The following examples show how to use org.apache.bcel.generic.Instruction#produceStack() . 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: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerInstructionSources() throws DataflowAnalysisException {
    for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
        Location location = i.next();
        Instruction instruction = location.getHandle().getInstruction();
        short opcode = instruction.getOpcode();

        int produces = instruction.produceStack(cpg);
        if (instruction instanceof InvokeInstruction) {
            // Model return value
            registerReturnValueSource(location);
        } else if (opcode == Const.GETFIELD || opcode == Const.GETSTATIC) {
            // Model field loads
            registerFieldLoadSource(location);
        } else if (instruction instanceof LDC) {
            // Model constant values
            registerLDCValueSource(location);
        } else if (instruction instanceof LDC2_W) {
            // Model constant values
            registerLDC2ValueSource(location);
        } else if (instruction instanceof ConstantPushInstruction) {
            // Model constant values
            registerConstantPushSource(location);
        } else if (instruction instanceof ACONST_NULL) {
            // Model constant values
            registerPushNullSource(location);
        } else if ((produces == 1 || produces == 2) && !(instruction instanceof LocalVariableInstruction)
                && !(instruction instanceof CHECKCAST)) {
            // Model other sources
            registerOtherSource(location);
        }
    }
}
 
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;
}