Java Code Examples for org.apache.bcel.Const#UNPREDICTABLE

The following examples show how to use org.apache.bcel.Const#UNPREDICTABLE . 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: 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 2
Source File: AbstractFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Handler for all instructions which pop values from the stack and store
 * them in a local variable. Note that two locals are stored into for long
 * and double stores.
 */
public void handleStoreInstruction(StoreInstruction obj) {
    try {
        int numConsumed = obj.consumeStack(cpg);
        if (numConsumed == Const.UNPREDICTABLE) {
            throw new InvalidBytecodeException("Unpredictable stack consumption");
        }

        int index = obj.getIndex();

        // Store values into consecutive locals corresponding
        // to the order in which the values appeared on the stack.
        while (numConsumed-- > 0) {
            Value value = frame.popValue();
            frame.setValue(index++, value);
        }
    } catch (DataflowAnalysisException e) {
        throw new InvalidBytecodeException(e.toString());
    }
}
 
Example 3
Source File: AbstractFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Handler for all instructions which load values from a local variable and
 * push them on the stack. Note that two locals are loaded for long and
 * double loads.
 */
public void handleLoadInstruction(LoadInstruction obj) {
    int numProduced = obj.produceStack(cpg);
    if (numProduced == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack production");
    }

    int index = obj.getIndex() + numProduced;

    // Load values from locals in reverse order.
    // This restores them to the stack in a way consistent
    // with visitStoreInstruction().
    while (numProduced-- > 0) {
        Value value = frame.getValue(--index);
        frame.pushValue(value);
    }
}
 
Example 4
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 5
Source File: TypeFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Consume stack. This is a convenience method for instructions where the
 * types of popped operands can be ignored.
 */
protected void consumeStack(Instruction ins) {
    ConstantPoolGen cpg = getCPG();
    TypeFrame frame = getFrame();

    int numWordsConsumed = ins.consumeStack(cpg);
    if (numWordsConsumed == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack consumption for " + ins);
    }
    if (numWordsConsumed > frame.getStackDepth()) {
        throw new InvalidBytecodeException("Stack underflow for " + ins + ", " + numWordsConsumed + " needed, " + frame.getStackDepth()
                + " avail, frame is " + frame);
    }
    try {
        while (numWordsConsumed-- > 0) {
            frame.popValue();
        }
    } catch (DataflowAnalysisException e) {
        throw new InvalidBytecodeException("Stack underflow for " + ins + ": " + e.getMessage());
    }
}
 
Example 6
Source File: TypeFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Handler for all instructions which load values from a local variable and
 * push them on the stack. Note that two locals are loaded for long and
 * double loads.
 */
@Override
public void handleLoadInstruction(LoadInstruction obj) {
    int numProduced = obj.produceStack(cpg);
    if (numProduced == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack production");
    }

    if (numProduced != 1) {
        super.handleLoadInstruction(obj);
        return;
    }
    int index = obj.getIndex();
    TypeFrame frame = getFrame();
    Type value = frame.getValue(index);
    if (value instanceof ReferenceType && !(value instanceof GenericObjectType)) {
        GenericObjectType gType = getLocalVariable(index,
                getLocation().getHandle().getPosition());
        value = GenericUtilities.merge(gType, value);
    }
    boolean isExact = frame.isExact(index);
    frame.pushValue(value);
    if (isExact) {
        setTopOfStackIsExact();
    }
}
 
Example 7
Source File: Stream.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ResourceValue getInstanceValue(ResourceValueFrame frame, InvokeInstruction inv, ConstantPoolGen cpg) {
    int numConsumed = inv.consumeStack(cpg);
    if (numConsumed == Const.UNPREDICTABLE) {
        throw new IllegalStateException();
    }
    return frame.getValue(frame.getNumSlots() - numConsumed);
}
 
Example 8
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 9
Source File: AbstractFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get the number of words consumed by given instruction.
 */
public int getNumWordsConsumed(Instruction ins) {
    int numWordsConsumed = ins.consumeStack(cpg);
    if (numWordsConsumed == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack consumption");
    }
    return numWordsConsumed;
}
 
Example 10
Source File: AbstractFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get the number of words produced by given instruction.
 */
public int getNumWordsProduced(Instruction ins) {

    int numWordsProduced = ins.produceStack(cpg);
    if (numWordsProduced == Const.UNPREDICTABLE) {
        throw new InvalidBytecodeException("Unpredictable stack productions");
    }
    return numWordsProduced;
}
 
Example 11
Source File: FindMismatchedWaitOrNotify.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void analyzeMethod(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {

        MethodGen methodGen = classContext.getMethodGen(method);
        if (methodGen == null) {
            return;
        }
        ConstantPoolGen cpg = methodGen.getConstantPool();
        CFG cfg = classContext.getCFG(method);
        ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
        LockDataflow dataflow = classContext.getLockDataflow(method);

        for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
            Location location = i.next();

            InstructionHandle handle = location.getHandle();

            Instruction ins = handle.getInstruction();
            if (!(ins instanceof INVOKEVIRTUAL)) {
                continue;
            }
            INVOKEVIRTUAL inv = (INVOKEVIRTUAL) ins;

            String methodName = inv.getName(cpg);
            String methodSig = inv.getSignature(cpg);

            if (Hierarchy.isMonitorWait(methodName, methodSig) || Hierarchy.isMonitorNotify(methodName, methodSig)) {
                int numConsumed = inv.consumeStack(cpg);
                if (numConsumed == Const.UNPREDICTABLE) {
                    throw new DataflowAnalysisException("Unpredictable stack consumption", methodGen, handle);
                }

                ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);
                if (!frame.isValid()) {
                    // Probably dead code
                    continue;
                }
                if (frame.getStackDepth() - numConsumed < 0) {
                    throw new DataflowAnalysisException("Stack underflow", methodGen, handle);
                }
                ValueNumber ref = frame.getValue(frame.getNumSlots() - numConsumed);
                LockSet lockSet = dataflow.getFactAtLocation(location);
                int lockCount = lockSet.getLockCount(ref.getNumber());

                if (lockCount == 0) {
                    Collection<ValueNumber> lockedValueNumbers = lockSet.getLockedValueNumbers(frame);
                    boolean foundMatch = false;
                    for (ValueNumber v : lockedValueNumbers) {
                        if (frame.veryFuzzyMatch(ref, v)) {
                            foundMatch = true;
                            break;
                        }
                    }

                    if (!foundMatch) {

                        String type = "wait".equals(methodName) ? "MWN_MISMATCHED_WAIT" : "MWN_MISMATCHED_NOTIFY";
                        String sourceFile = classContext.getJavaClass().getSourceFileName();
                        // Report as medium priority only if the method is
                        // public.
                        // Non-public methods may be properly locked in a
                        // calling context.
                        int priority = method.isPublic() ? NORMAL_PRIORITY : LOW_PRIORITY;

                        bugAccumulator.accumulateBug(
                                new BugInstance(this, type, priority).addClassAndMethod(methodGen, sourceFile),
                                SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, handle));
                    }
                }
            }
        }
        bugAccumulator.reportAccumulatedBugs();
    }
 
Example 12
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;
}
 
Example 13
Source File: Frame.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Get the number of arguments passed to given method invocation, including
 * the object instance if the call is to an instance method.
 *
 * @param ins
 *            the method invocation instruction
 * @param cpg
 *            the ConstantPoolGen for the class containing the method
 * @return number of arguments, including object instance if appropriate
 * @throws DataflowAnalysisException
 */
public int getNumArgumentsIncludingObjectInstance(InvokeInstruction ins, ConstantPoolGen cpg)
        throws DataflowAnalysisException {
    int numConsumed = ins.consumeStack(cpg);
    if (numConsumed == Const.UNPREDICTABLE) {
        throw new DataflowAnalysisException("Unpredictable stack consumption in " + ins);
    }
    return numConsumed;
}
 
Example 14
Source File: Frame.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Get the <i>i</i>th operand used by given instruction.
 *
 * @param ins
 *            the instruction, which must be a StackConsumer
 * @param cpg
 *            the ConstantPoolGen
 * @param i
 *            index of operand to get: 0 for the first operand, etc.
 * @return the <i>i</i>th operand used by the given instruction
 * @throws DataflowAnalysisException
 */
public ValueType getOperand(StackConsumer ins, ConstantPoolGen cpg, int i) throws DataflowAnalysisException {
    int numOperands = ins.consumeStack(cpg);
    if (numOperands == Const.UNPREDICTABLE) {
        throw new DataflowAnalysisException("Unpredictable stack consumption in " + ins);
    }
    return getStackValue((numOperands - 1) - i);
}