org.apache.bcel.generic.FieldInstruction Java Examples

The following examples show how to use org.apache.bcel.generic.FieldInstruction. 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: UnconditionalValueDerefAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * If this is a putfield or putstatic instruction, check to see if the field
 * is @NonNull, and treat it as dereferences.
 *
 * @param location
 *            the Location of the instruction
 * @param vnaFrame
 *            the ValueNumberFrame at the Location of the instruction
 * @param fact
 *            the dataflow value to modify
 * @throws DataflowAnalysisException
 */
private void checkNonNullPutField(Location location, ValueNumberFrame vnaFrame, UnconditionalValueDerefSet fact)
        throws DataflowAnalysisException {
    INullnessAnnotationDatabase database = AnalysisContext.currentAnalysisContext().getNullnessAnnotationDatabase();

    FieldInstruction fieldIns = (FieldInstruction) location.getHandle().getInstruction();

    XField field = XFactory.createXField(fieldIns, methodGen.getConstantPool());
    char firstChar = field.getSignature().charAt(0);
    if (firstChar != 'L' && firstChar != '[') {
        return;
    }
    NullnessAnnotation resolvedAnnotation = database.getResolvedAnnotation(field, true);
    if (resolvedAnnotation == NullnessAnnotation.NONNULL) {
        IsNullValueFrame invFrame = invDataflow.getFactAtLocation(location);
        if (!invFrame.isValid()) {
            return;
        }
        IsNullValue value = invFrame.getTopValue();
        if (reportDereference(value)) {
            ValueNumber vn = vnaFrame.getTopValue();
            fact.addDeref(vn, location);
        }
    }
}
 
Example #2
Source File: BackwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void modelFieldStore(Location location) throws DataflowAnalysisException {
    // Model field stores
    XField writtenField = XFactory.createXField((FieldInstruction) location.getHandle().getInstruction(), cpg);
    TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(writtenField,
            typeQualifierValue);
    When when = (tqa != null) ? tqa.when : When.UNKNOWN;

    // The ValueNumberFrame *before* the FieldInstruction should
    // have the ValueNumber of the stored value on the top of the stack.
    ValueNumberFrame vnaFrameAtStore = vnaDataflow.getFactAtLocation(location);
    if (vnaFrameAtStore.isValid()) {
        ValueNumber vn = vnaFrameAtStore.getTopValue();
        SourceSinkInfo sink = new SourceSinkInfo(SourceSinkType.FIELD_STORE, location, vn, when);
        registerSourceSink(sink);
    }
}
 
Example #3
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Look up the field referenced by given FieldInstruction, returning it as
 * an {@link XField XField} object.
 *
 * @param fins
 *            the FieldInstruction
 * @param cpg
 *            the ConstantPoolGen used by the class containing the
 *            instruction
 * @return an XField object representing the field, or null if no such field
 *         could be found
 */
public static @CheckForNull XField findXField(FieldInstruction fins, @Nonnull ConstantPoolGen cpg) {

    String className = fins.getClassName(cpg);
    String fieldName = fins.getFieldName(cpg);
    String fieldSig = fins.getSignature(cpg);

    boolean isStatic = (fins.getOpcode() == Const.GETSTATIC || fins.getOpcode() == Const.PUTSTATIC);

    XField xfield = findXField(className, fieldName, fieldSig, isStatic);
    short opcode = fins.getOpcode();
    if (xfield != null && xfield.isResolved()
            && xfield.isStatic() == (opcode == Const.GETSTATIC || opcode == Const.PUTSTATIC)) {
        return xfield;
    } else {
        return null;
    }
}
 
Example #4
Source File: ForwardTypeQualifierDataflowAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerFieldLoadSource(Location location) throws DataflowAnalysisException {
    XField loadedField = XFactory.createXField((FieldInstruction) location.getHandle().getInstruction(), cpg);
    if (loadedField.isResolved()) {
        TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(loadedField,
                typeQualifierValue);
        When when = (tqa != null) ? tqa.when : When.UNKNOWN;
        registerTopOfStackSource(SourceSinkType.FIELD_LOAD, location, when, false, null);
    }

}
 
Example #5
Source File: BCELFactory.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitFieldInstruction( final FieldInstruction i ) {
    final short opcode = i.getOpcode();
    final String class_name = i.getClassName(_cp);
    final String field_name = i.getFieldName(_cp);
    final Type type = i.getFieldType(_cp);
    _out.println("il.append(_factory.createFieldAccess(\"" + class_name + "\", \"" + field_name
            + "\", " + BCELifier.printType(type) + ", " + CONSTANT_PREFIX
            + Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));");
}
 
Example #6
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private ObjectType getObjectType(final FieldInstruction o) {
    final ReferenceType rt = o.getReferenceType(constantPoolGen);
    if(rt instanceof ObjectType) {
        return (ObjectType)rt;
    }
    constraintViolated(o, "expecting ObjectType but got "+rt);
    return null;
}
 
Example #7
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the instruction a write of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if instruction is a write of a field, null otherwise
 */
public static FieldAnnotation isWrite(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof PUTFIELD || ins instanceof PUTSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof PUTSTATIC);
    } else {
        return null;
    }
}
 
Example #8
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the given instruction a read of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if the instruction is a read of a field, null otherwise
 */
public static FieldAnnotation isRead(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof GETFIELD || ins instanceof GETSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof GETSTATIC);
    } else {
        return null;
    }
}
 
Example #9
Source File: FieldAccess.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get a Variable representing the stack value which will either be stored
 * into or loaded from a field.
 *
 * @param fieldIns
 *            the FieldInstruction accessing the field
 * @param cpg
 *            the ConstantPoolGen for the method
 * @param frame
 *            the ValueNumberFrame containing the value to be stored or the
 *            value loaded
 */
protected static Variable snarfFieldValue(FieldInstruction fieldIns, ConstantPoolGen cpg, ValueNumberFrame frame)
        throws DataflowAnalysisException {

    if (isLongOrDouble(fieldIns, cpg)) {
        int numSlots = frame.getNumSlots();
        ValueNumber topValue = frame.getValue(numSlots - 1);
        ValueNumber nextValue = frame.getValue(numSlots - 2);
        return new LongOrDoubleLocalVariable(topValue, nextValue);
    } else {
        return new LocalVariable(frame.getTopValue());
    }

}
 
Example #10
Source File: XFactory.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static XField createXField(FieldInstruction fieldInstruction, ConstantPoolGen cpg) {
    String className = fieldInstruction.getClassName(cpg);
    String fieldName = fieldInstruction.getName(cpg);
    String fieldSig = fieldInstruction.getSignature(cpg);

    int opcode = fieldInstruction.getOpcode();
    return createXField(className, fieldName, fieldSig, opcode == Const.GETSTATIC || opcode == Const.PUTSTATIC);
}
 
Example #11
Source File: ResourceValueFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleFieldStore(FieldInstruction ins) {
    try {
        // If the resource instance is stored in a field, then it escapes
        ResourceValueFrame frame = getFrame();
        ResourceValue topValue = frame.getTopValue();
        if (topValue.equals(ResourceValue.instance())) {
            frame.setStatus(ResourceValueFrame.ESCAPED);
        }
    } catch (DataflowAnalysisException e) {
        throw new InvalidBytecodeException("Stack underflow", e);
    }

    handleNormalInstruction(ins);
}
 
Example #12
Source File: FieldSetAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private @CheckForNull XField lookupField(InstructionHandle handle, FieldInstruction fins) {
    XField field = instructionToFieldMap.get(handle);
    if (field == null) {
        field = Hierarchy.findXField(fins, getCPG());
        instructionToFieldMap.put(handle, field);
    }
    return field;
}
 
Example #13
Source File: FieldSetAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleInstruction(InstructionHandle handle, BasicBlock basicBlock, FieldSet fact) {
    Instruction ins = handle.getInstruction();
    short opcode = ins.getOpcode();
    XField field;

    switch (opcode) {
    case Const.GETFIELD:
    case Const.GETSTATIC:
        field = lookupField(handle, (FieldInstruction) ins);
        if (field != null) {
            sawLoad(fact, field);
        }
        break;

    case Const.PUTFIELD:
    case Const.PUTSTATIC:
        field = lookupField(handle, (FieldInstruction) ins);
        if (field != null) {
            sawStore(fact, field);
        }
        break;

    case Const.INVOKEINTERFACE:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
    case Const.INVOKEVIRTUAL:
        // Assume that the called method assigns loads and stores all
        // possible fields
        fact.setBottom();
        break;
    default:
        break;
    }
}
 
Example #14
Source File: FindRefComparison.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleLoad(FieldInstruction obj) {
    consumeStack(obj);

    Type type = obj.getType(getCPG());
    if (!STRING_SIGNATURE.equals(type.getSignature())) {
        throw new IllegalArgumentException("type is not String: " + type);
    }
    try {
        String className = obj.getClassName(getCPG());
        String fieldName = obj.getName(getCPG());
        Field field = Hierarchy.findField(className, fieldName);

        if (field != null) {
            // If the field is final, we'll assume that the String value
            // is static.
            if (field.isFinal()) {
                pushValue(staticStringTypeInstance);
            } else {
                pushValue(type);
            }

            return;
        }
    } catch (ClassNotFoundException ex) {
        lookupFailureCallback.reportMissingClass(ex);
    }

    pushValue(type);
}
 
Example #15
Source File: NoiseNullDeref.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void foundNullDeref(Location location, ValueNumber valueNumber, IsNullValue refValue, ValueNumberFrame vnaFrame,
        boolean isConsistent) {
    if (!refValue.isNullOnComplicatedPath23()) {
        return;
    }
    WarningPropertySet<WarningProperty> propertySet = new WarningPropertySet<>();
    if (valueNumber.hasFlag(ValueNumber.CONSTANT_CLASS_OBJECT)) {
        return;
    }

    boolean onExceptionPath = refValue.isException();
    if (onExceptionPath) {
        propertySet.addProperty(GeneralWarningProperty.ON_EXCEPTION_PATH);
    }
    BugAnnotation variable = ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, valueNumber, vnaFrame,
            "VALUE_OF");
    addPropertiesForDereferenceLocations(propertySet, Collections.singleton(location));
    Instruction ins = location.getHandle().getInstruction();
    BugAnnotation cause;
    final ConstantPoolGen cpg = classContext.getConstantPoolGen();

    if (ins instanceof InvokeInstruction) {
        if (ins instanceof INVOKEDYNAMIC) {
            return;
        }
        InvokeInstruction iins = (InvokeInstruction) ins;
        XMethod invokedMethod = XFactory.createXMethod((InvokeInstruction) ins, cpg);
        cause = MethodAnnotation.fromXMethod(invokedMethod);
        cause.setDescription(MethodAnnotation.METHOD_CALLED);

        if ("close".equals(iins.getMethodName(cpg)) && "()V".equals(iins.getSignature(cpg))) {
            propertySet.addProperty(NullDerefProperty.CLOSING_NULL);
        }
    } else if (ins instanceof FieldInstruction) {
        FieldInstruction fins = (FieldInstruction) ins;
        XField referencedField = XFactory.createXField(fins, cpg);
        cause = FieldAnnotation.fromXField(referencedField);

    } else {
        cause = new StringAnnotation(ins.getName());
    }

    boolean caught = inCatchNullBlock(location);
    if (caught && skipIfInsideCatchNull()) {
        return;
    }

    int basePriority = Priorities.NORMAL_PRIORITY;

    if (!refValue.isNullOnComplicatedPath2()) {
        basePriority--;
    }
    reportNullDeref(propertySet, location, "NOISE_NULL_DEREFERENCE", basePriority, cause, variable);

}
 
Example #16
Source File: FieldReference.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected FieldReference(
    FieldInstruction aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #17
Source File: FieldReference.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected FieldReference(
    FieldInstruction aInstruction,
    ConstantPoolGen aPoolGen)
{
    super(aInstruction, aPoolGen);
}
 
Example #18
Source File: TrainFieldStoreTypes.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 {
    CFG cfg = classContext.getCFG(method);
    TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
    ConstantPoolGen cpg = classContext.getConstantPoolGen();

    for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
        Location location = i.next();
        Instruction ins = location.getHandle().getInstruction();
        short opcode = ins.getOpcode();

        // Field store instruction?
        if (opcode != Const.PUTFIELD && opcode != Const.PUTSTATIC) {
            continue;
        }

        // Check if field type is a reference type
        FieldInstruction fins = (FieldInstruction) ins;
        Type fieldType = fins.getType(cpg);
        if (!(fieldType instanceof ReferenceType)) {
            continue;
        }

        // Find the exact field being stored into
        XField xfield = Hierarchy.findXField(fins, cpg);
        if (xfield == null) {
            continue;
        }

        // Skip public and protected fields, since it is reasonable to
        // assume
        // we won't see every store to those fields
        if (xfield.isPublic() || xfield.isProtected()) {
            continue;
        }

        // The top value on the stack is the one which will be stored
        // into the field
        TypeFrame frame = typeDataflow.getFactAtLocation(location);
        if (!frame.isValid()) {
            continue;
        }
        Type storeType = frame.getTopValue();
        if (!(storeType instanceof ReferenceType)) {
            continue;
        }

        // Get or create the field store type set
        FieldStoreType property = database.getProperty(xfield.getFieldDescriptor());
        if (property == null) {
            property = new FieldStoreType();
            database.setProperty(xfield.getFieldDescriptor(), property);
        }

        // Add the store type to the set
        property.addTypeSignature(storeType.getSignature());
    }
}
 
Example #19
Source File: FieldAccess.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Return whether the given FieldInstruction accesses a long or double
 * field.
 *
 * @param fieldIns
 *            the FieldInstruction
 * @param cpg
 *            the ConstantPoolGen for the method
 */
protected static boolean isLongOrDouble(FieldInstruction fieldIns, ConstantPoolGen cpg) {
    Type type = fieldIns.getFieldType(cpg);
    int code = type.getType();
    return code == Const.T_LONG || code == Const.T_DOUBLE;
}