Package edu.umd.cs.findbugs.ba

Examples of edu.umd.cs.findbugs.ba.XField


        for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
            Location location = i.next();
            try {
                Instruction ins = location.getHandle().getInstruction();
                XField xfield = null;
                boolean isWrite = false;
                boolean isLocal = false;
                boolean isNullCheck = false;

                if (ins instanceof FieldInstruction) {
                    InstructionHandle n = location.getHandle().getNext();
                    isNullCheck = n.getInstruction() instanceof IFNONNULL || n.getInstruction() instanceof IFNULL;
                    if (DEBUG && isNullCheck) {
                        System.out.println("is null check");
                    }
                    FieldInstruction fins = (FieldInstruction) ins;
                    xfield = Hierarchy.findXField(fins, cpg);
                    if (xfield == null) {
                        continue;
                    }
                    isWrite = ins.getOpcode() == Constants.PUTFIELD;
                    isLocal = fins.getClassName(cpg).equals(classContext.getJavaClass().getClassName());
                    if (DEBUG) {
                        System.out.println("Handling field access: " + location.getHandle() + " (frame="
                                + vnaDataflow.getFactAtLocation(location) + ") :" + n);
                    }
                } else if (ins instanceof INVOKESTATIC) {
                    INVOKESTATIC inv = (INVOKESTATIC) ins;
                    InnerClassAccess access = icam.getInnerClassAccess(inv, cpg);
                    if (access != null && access.getMethodSignature().equals(inv.getSignature(cpg))) {
                        xfield = access.getField();
                        isWrite = !access.isLoad();
                        isLocal = false;
                        if (DEBUG) {
                            System.out.println("Handling inner class access: " + location.getHandle() + " (frame="
                                    + vnaDataflow.getFactAtLocation(location) + ")");
                        }
                    }
                }

                if (xfield == null) {
                    continue;
                }

                // We only care about mutable nonvolatile nonpublic instance
                // fields.
                if (xfield.isStatic() || xfield.isPublic() || xfield.isVolatile() || xfield.isFinal()) {
                    continue;
                }

                // The value number frame could be invalid if the basic
                // block became unreachable due to edge pruning (dead code).
                ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);
                if (!frame.isValid()) {
                    continue;
                }

                // Get lock set and instance value
                ValueNumber thisValue = !method.isStatic() ? vnaDataflow.getAnalysis().getThisValue() : null;
                LockSet lockSet = lockChecker.getFactAtLocation(location);
                InstructionHandle handle = location.getHandle();
                ValueNumber instance = frame.getInstance(handle.getInstruction(), cpg);
                if (DEBUG) {
                    System.out.println("Lock set: " + lockSet);
                    System.out.println("value number: " + instance.getNumber());
                    System.out.println("Lock count: " + lockSet.getLockCount(instance.getNumber()));
                }

                // Is the instance locked?
                // We consider the access to be locked if either
                // - the object is explicitly locked, or
                // - the field is accessed through the "this" reference,
                // and the method is in the locked method set, or
                // - any value returned by a called method is locked;
                // the (conservative) assumption is that the return lock object
                // is correct for synchronizing the access
                boolean isExplicitlyLocked = lockSet.getLockCount(instance.getNumber()) > 0;
                boolean isAccessedThroughThis = thisValue != null && thisValue.equals(instance);
                boolean isLocked = isExplicitlyLocked
                        || ((isConstructor(method.getName()) || lockedMethodSet.contains(method)) && isAccessedThroughThis)
                        || lockSet.containsReturnValue(vnaDataflow.getAnalysis().getFactory());

                // Adjust the field so its class name is the same
                // as the type of reference it is accessed through.
                // This helps fix false positives produced when a
                // threadsafe class is extended by a subclass that
                // doesn't care about thread safety.
                if (ADJUST_SUBCLASS_ACCESSES) {
                    // Find the type of the object instance
                    TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
                    TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
                    if (!typeFrame.isValid()) {
                        continue;
                    }
                    Type instanceType = typeFrame.getInstance(handle.getInstruction(), cpg);
                    if (instanceType instanceof TopType) {
                        if (DEBUG) {
                            System.out.println("Freaky: typeFrame is " + typeFrame);
                        }
                        continue;
                    }
                    // Note: instance type can be Null,
                    // in which case we won't adjust the field type.
                    if (instanceType != TypeFrame.getNullType() && instanceType != TypeFrame.getBottomType()) {
                        if (!(instanceType instanceof ObjectType)) {
                            throw new DataflowAnalysisException("Field accessed through non-object reference " + instanceType,
                                    methodGen, handle);
                        }
                        ObjectType objType = (ObjectType) instanceType;

                        // If instance class name is not the same as that of the
                        // field,
                        // make it so
                        String instanceClassName = objType.getClassName();
                        if (!instanceClassName.equals(xfield.getClassName())) {
                            xfield = XFactory.getExactXField(instanceClassName, xfield.getName(), xfield.getSignature(),
                                    xfield.isStatic());
                        }
                    }
                }

                int kind = 0;
View Full Code Here


            possibleOverwrite = null;

            if (stack.getStackDepth() >= 4 && getNextOpcode() == PUTFIELD) {
                OpcodeStack.Item third = stack.getStackItem(2);
                OpcodeStack.Item fourth = stack.getStackItem(3);
                XField f2 = third.getXField();
                int registerNumber2 = fourth.getRegisterNumber();
                if (f2 != null && f2.equals(getXFieldOperand()) && registerNumber2 >= 0
                        && registerNumber2 == third.getFieldLoadedFromRegister()
                        && !third.equals(top) && (third.getPC() == -1 || third.getPC() > lastMethodCall)) {
                    possibleOverwrite = f2;
                }
            }

            XField f = top.getXField();
            int registerNumber = next.getRegisterNumber();
            if (f != null && f.equals(getXFieldOperand()) && registerNumber >= 0
                    && registerNumber == top.getFieldLoadedFromRegister() && (top.getPC() == -1 || top.getPC() > lastMethodCall)) {
                int priority = NORMAL_PRIORITY;

                LocalVariableAnnotation possibleMatch = LocalVariableAnnotation.findMatchingIgnoredParameter(getClassContext(),
                        getMethod(), getNameConstantOperand(), getSigConstantOperand());
View Full Code Here

        int priority = HIGH_PRIORITY;
        if (opcode == ISUB || opcode == LSUB || opcode == INVOKEINTERFACE || opcode == INVOKEVIRTUAL) {
            priority = NORMAL_PRIORITY;
        }
        XField field = ValueNumberSourceInfo.findXFieldFromValueNumber(method, location, v0, frame);
        BugAnnotation annotation;
        String prefix;
        if (field != null) {
            if (field.isVolatile()) {
                return;
            }
            if (true) {
                return; // don't report these; too many false positives
            }
View Full Code Here

        // System.out.println("saw\t" + OPCODE_NAMES[seen] + "\t" + zeroOnTOS);
        switch (seen) {
        case GETSTATIC:
        case PUTSTATIC:

            XField xField = getXFieldOperand();
            if (xField == null) {
                break;
            }
            if (!interesting(xField)) {
                break;
View Full Code Here

    @Override
    public void sawOpcode(int seen) {
        switch (state) {
        case START:
            if (seen == GETFIELD) {
                XField f = getXFieldOperand();
                if (isVolatile(f)) {
                    incrementField = f;
                    state = IncrementState.GETFIELD;
                }
            }
            break;
        case GETFIELD:
            if (seen == ICONST_1 || seen == LCONST_1 || seen == ICONST_M1) {
                state = IncrementState.LOADCONSTANT;
            } else {
                resetIncrementState();
            }

            break;
        case LOADCONSTANT:
            if (seen == IADD || seen == ISUB || seen == LADD || seen == LSUB) {
                state = IncrementState.ADD;
            } else {
                resetIncrementState();
            }
            break;
        case ADD:
            if (seen == PUTFIELD && incrementField.equals(getXFieldOperand())) {
                bugReporter.reportBug(new BugInstance(this, "VO_VOLATILE_INCREMENT",
                        incrementField.getSignature().equals("J") ? Priorities.HIGH_PRIORITY : Priorities.NORMAL_PRIORITY)
                .addClassAndMethod(this).addField(incrementField).addSourceLine(this));
            }
            resetIncrementState();
            break;
        }
        switch (seen) {
        case PUTSTATIC: {
            XField f = getXFieldOperand();
            if (!isVolatileArray(f)) {
                return;
            }
            if (getMethodName().equals("<clinit>")) {
                initializationWrites.add(f);
            } else {
                otherWrites.add(f);
            }
            break;
        }
        case PUTFIELD: {
            XField f = getXFieldOperand();
            if (!isVolatileArray(f)) {
                return;
            }

            if (getMethodName().equals("<init>")) {
View Full Code Here

        writer.write(",");
        writer.write(key.getName());
        writer.write(",");
        writer.write(key.getSignature());
        writer.write(",");
        XField xField = XFactory.createXField(key);
        int flags = xField.getAccessFlags() & 0xf;
        writer.write(String.valueOf(flags));
    }
View Full Code Here

                pushByLocalLoad("J", seen - LLOAD_0);
                break;

            case GETSTATIC: {
                FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();
                XField fieldOperand = dbc.getXFieldOperand();

                if (fieldOperand != null && fieldSummary.isComplete() && !fieldOperand.isPublic()) {
                    OpcodeStack.Item item = fieldSummary.getSummary(fieldOperand);
                    if (item != null) {
                        Item itm = new Item(item);
                        itm.setLoadedFromField(fieldOperand, Integer.MAX_VALUE);
                        itm.setPC(dbc.getPC());
                        push(itm);
                        break;
                    }
                }
                FieldAnnotation field = FieldAnnotation.fromReferencedField(dbc);
                Item i = new Item(dbc.getSigConstantOperand(), field, Integer.MAX_VALUE);
                if (field.getFieldName().equals("separator") && field.getClassName().equals("java.io.File")) {
                    i.setSpecialKind(Item.FILE_SEPARATOR_STRING);
                }
                i.setPC(dbc.getPC());
                push(i);
                break;
            }

            case LDC:
            case LDC_W:
            case LDC2_W:
                cons = dbc.getConstantRefOperand();
                pushByConstant(dbc, cons);
                break;

            case INSTANCEOF:
                pop();
                push(new Item("I"));
                break;

            case IFNONNULL:
            case IFNULL:
                // {
                // Item topItem = pop();
                // if (seen == IFNONNULL && topItem.isNull())
                // break;
                // seenTransferOfControl = true;
                // addJumpValue(dbc.getPC(), dbc.getBranchTarget());
                //
                // break;
                // }

            case IFEQ:
            case IFNE:
            case IFLT:
            case IFLE:
            case IFGT:
            case IFGE:

                seenTransferOfControl = true;
                {
                    Item topItem = pop();

                    // System.out.printf("%4d %10s %s%n",
                    // dbc.getPC(),OPCODE_NAMES[seen], topItem);
                    if (seen == IFLT || seen == IFLE) {
                        registerTestedFoundToBeNonnegative = topItem.registerNumber;
                    }
                    // if we see a test comparing a special negative value with
                    // 0,
                    // reset all other such values on the opcode stack
                    if (topItem.valueCouldBeNegative() && (seen == IFLT || seen == IFLE || seen == IFGT || seen == IFGE)) {
                        int specialKind = topItem.getSpecialKind();
                        for (Item item : stack) {
                            if (item != null && item.getSpecialKind() == specialKind) {
                                item.setSpecialKind(Item.NOT_SPECIAL);
                            }
                        }
                        for (Item item : lvValues) {
                            if (item != null && item.getSpecialKind() == specialKind) {
                                item.setSpecialKind(Item.NOT_SPECIAL);
                            }
                        }

                    }
                }
                addJumpValue(dbc.getPC(), dbc.getBranchTarget());

                break;
            case LOOKUPSWITCH:

            case TABLESWITCH:
                seenTransferOfControl = true;
                setReachOnlyByBranch(true);
                pop();
                addJumpValue(dbc.getPC(), dbc.getBranchTarget());
                int pc = dbc.getBranchTarget() - dbc.getBranchOffset();
                for (int offset : dbc.getSwitchOffsets()) {
                    addJumpValue(dbc.getPC(), offset + pc);
                }

                break;
            case ARETURN:
            case DRETURN:
            case FRETURN:

            case IRETURN:
            case LRETURN:

                seenTransferOfControl = true;
                setReachOnlyByBranch(true);
                pop();
                break;
            case MONITORENTER:
            case MONITOREXIT:
            case POP:
                pop();
                break;

            case PUTSTATIC:
                pop();
                eraseKnowledgeOf(dbc.getXFieldOperand());
                break;
            case PUTFIELD:
                pop(2);
                eraseKnowledgeOf(dbc.getXFieldOperand());
                break;

            case IF_ACMPEQ:
            case IF_ACMPNE:
            case IF_ICMPEQ:
            case IF_ICMPNE:
            case IF_ICMPLT:
            case IF_ICMPLE:
            case IF_ICMPGT:
            case IF_ICMPGE:

            {
                seenTransferOfControl = true;
                Item right = pop();
                Item left = pop();

                Object lConstant = left.getConstant();
                Object rConstant = right.getConstant();
                boolean takeJump = false;
                boolean handled = false;
                if (seen == IF_ACMPNE || seen == IF_ACMPEQ) {
                    if (lConstant != null && rConstant != null && !lConstant.equals(rConstant) || lConstant != null
                            && right.isNull() || rConstant != null && left.isNull()) {
                        handled = true;
                        takeJump = seen == IF_ACMPNE;
                    }
                } else if (lConstant instanceof Integer && rConstant instanceof Integer) {
                    int lC = ((Integer) lConstant).intValue();
                    int rC = ((Integer) rConstant).intValue();
                    switch (seen) {
                    case IF_ICMPEQ:
                        takeJump = lC == rC;
                        handled = true;
                        break;
                    case IF_ICMPNE:
                        takeJump = lC != rC;
                        handled = true;
                        break;
                    case IF_ICMPGE:
                        takeJump = lC >= rC;
                        handled = true;
                        break;
                    case IF_ICMPGT:
                        takeJump = lC > rC;
                        handled = true;
                        break;
                    case IF_ICMPLE:
                        takeJump = lC <= rC;
                        handled = true;
                        break;
                    case IF_ICMPLT:
                        takeJump = lC < rC;
                        handled = true;
                        break;
                    default:
                        assert false;
                    }

                }
                if (handled) {
                    if (takeJump) {
                        int branchTarget = dbc.getBranchTarget();
                        addJumpValue(dbc.getPC(), branchTarget);
                        setTop(true);
                        break;
                    } else {
                        break;
                    }
                }
                if (right.hasConstantValue(Integer.MIN_VALUE) && left.mightRarelyBeNegative()
                        || left.hasConstantValue(Integer.MIN_VALUE) && right.mightRarelyBeNegative()) {
                    for (Item i : stack) {
                        if (i != null && i.mightRarelyBeNegative()) {
                            i.setSpecialKind(Item.NOT_SPECIAL);
                        }
                    }
                    for (Item i : lvValues) {
                        if (i != null && i.mightRarelyBeNegative()) {
                            i.setSpecialKind(Item.NOT_SPECIAL);
                        }
                    }
                }
                int branchTarget = dbc.getBranchTarget();
                addJumpValue(dbc.getPC(), branchTarget);
                break;
            }

            case POP2:
                it = pop();
                if (it.getSize() == 1) {
                    pop();
                }
                break;


            case IALOAD:
            case SALOAD:
                pop(2);
                push(new Item("I"));
                break;

            case DUP:
                handleDup();
                break;

            case DUP2:
                handleDup2();
                break;

            case DUP_X1:
                handleDupX1();
                break;

            case DUP_X2:

                handleDupX2();
                break;

            case DUP2_X1:
                handleDup2X1();
                break;

            case DUP2_X2:
                handleDup2X2();
                break;

            case IINC:
                register = dbc.getRegisterOperand();
                it = getLVValue(register);
                it2 = new Item("I", dbc.getIntConstant());
                pushByIntMath(dbc, IADD, it2, it);
                pushByLocalStore(register);
                break;

            case ATHROW:
                pop();
                seenTransferOfControl = true;
                setReachOnlyByBranch(true);
                setTop(true);
                break;

            case CHECKCAST: {
                String castTo = dbc.getClassConstantOperand();

                if (castTo.charAt(0) != '[') {
                    castTo = "L" + castTo + ";";
                }
                it = pop();

                if (!it.signature.equals(castTo)) {
                    it = new Item(it, castTo);
                }
                push(it);
                break;

            }
            case NOP:
                break;
            case RET:
            case RETURN:
                seenTransferOfControl = true;
                setReachOnlyByBranch(true);
                break;

            case GOTO:
            case GOTO_W:
                seenTransferOfControl = true;
                setReachOnlyByBranch(true);
                addJumpValue(dbc.getPC(), dbc.getBranchTarget());
                stack.clear();
                setTop(true);

                break;

            case SWAP:
                handleSwap();
                break;

            case ICONST_M1:
            case ICONST_0:
            case ICONST_1:
            case ICONST_2:
            case ICONST_3:
            case ICONST_4:
            case ICONST_5:
                push(new Item("I", (seen - ICONST_0)));
                break;

            case LCONST_0:
            case LCONST_1:
                push(new Item("J", Long.valueOf(seen - LCONST_0)));
                break;

            case DCONST_0:
            case DCONST_1:
                push(new Item("D", Double.valueOf(seen - DCONST_0)));
                break;

            case FCONST_0:
            case FCONST_1:
            case FCONST_2:
                push(new Item("F", Float.valueOf(seen - FCONST_0)));
                break;

            case ACONST_NULL:
                push(new Item());
                break;

            case ASTORE:
            case DSTORE:
            case FSTORE:
            case ISTORE:
            case LSTORE:
                pushByLocalStore(dbc.getRegisterOperand());
                break;

            case ASTORE_0:
            case ASTORE_1:
            case ASTORE_2:
            case ASTORE_3:
                pushByLocalStore(seen - ASTORE_0);
                break;

            case DSTORE_0:
            case DSTORE_1:
            case DSTORE_2:
            case DSTORE_3:
                pushByLocalStore(seen - DSTORE_0);
                break;

            case FSTORE_0:
            case FSTORE_1:
            case FSTORE_2:
            case FSTORE_3:
                pushByLocalStore(seen - FSTORE_0);
                break;

            case ISTORE_0:
            case ISTORE_1:
            case ISTORE_2:
            case ISTORE_3:
                pushByLocalStore(seen - ISTORE_0);
                break;

            case LSTORE_0:
            case LSTORE_1:
            case LSTORE_2:
            case LSTORE_3:
                pushByLocalStore(seen - LSTORE_0);
                break;

            case GETFIELD: {
                FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();
                XField fieldOperand = dbc.getXFieldOperand();
                if (fieldOperand != null && fieldSummary.isComplete() && !fieldOperand.isPublic()) {
                    OpcodeStack.Item item = fieldSummary.getSummary(fieldOperand);
                    if (item != null) {
                        Item addr = pop();
                        Item itm = new Item(item);
                        itm.setLoadedFromField(fieldOperand, addr.getRegisterNumber());
View Full Code Here

    }

    private void handleSuspiciousRefComparison(JavaClass jclass, Method method, MethodGen methodGen,
            List<WarningWithProperties> refComparisonList, Location location, String lhs, ReferenceType lhsType,
            ReferenceType rhsType) {
        XField xf = null;
        if (lhsType instanceof FinalConstant) {
            xf = ((FinalConstant) lhsType).getXField();
        } else if (rhsType instanceof FinalConstant) {
            xf = ((FinalConstant) rhsType).getXField();
        }
        String sourceFile = jclass.getSourceFileName();
        String bugPattern = "RC_REF_COMPARISON";
        int priority = Priorities.HIGH_PRIORITY;
        if (lhs.equals("java.lang.Boolean")) {
            bugPattern = "RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN";
            priority = Priorities.NORMAL_PRIORITY;
        } else if (xf != null && xf.isStatic() && xf.isFinal()) {
            bugPattern = "RC_REF_COMPARISON_BAD_PRACTICE";
            if (xf.isPublic() || !methodGen.isPublic()) {
                priority = Priorities.NORMAL_PRIORITY;
            }
        }
        BugInstance instance = new BugInstance(this, bugPattern, priority).addClassAndMethod(methodGen, sourceFile)
                .addType("L" + lhs.replace('.', '/') + ";").describe(TypeAnnotation.FOUND_ROLE);
View Full Code Here

    }

    @Override
    public void sawOpcode(int opcode) {
        if (opcode == PUTFIELD) {
            XField f = getXFieldOperand();
            OpcodeStack.Item item = stack.getStackItem(1);
            if (item.getRegisterNumber() != 0) {
                return;
            }
            initializedFields.add(f);
            return;
        }
        if (opcode != GETFIELD) {
            return;
        }
        OpcodeStack.Item item = stack.getStackItem(0);
        if (item.getRegisterNumber() != 0) {
            return;
        }
        XField f = getXFieldOperand();

        if (f == null || !f.getClassDescriptor().equals(getClassDescriptor())) {
            return;
        }
        if (f.isSynthetic() || f.getName().startsWith("this$")) {
            return;
        }
        if (initializedFields.contains(f)) {
            return;
        }
        FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();

        ClassDescriptor superClassDescriptor = DescriptorFactory.createClassDescriptor(getSuperclassName());
        Set<ProgramPoint> calledFrom = fieldSummary.getCalledFromSuperConstructor(superClassDescriptor, getXMethod());
        if (calledFrom.isEmpty()) {
            return;
        }
        UnreadFieldsData unreadFields = AnalysisContext.currentAnalysisContext().getUnreadFieldsData();

        int priority;
        if (!unreadFields.isWrittenInConstructor(f)) {
            return;
        }

        if (f.isFinal()) {
            priority = HIGH_PRIORITY;
        } else if (unreadFields.isWrittenDuringInitialization(f) || unreadFields.isWrittenOutsideOfInitialization(f)) {
            priority = NORMAL_PRIORITY;
        } else {
            priority = HIGH_PRIORITY;
View Full Code Here

    @Override
    public void sawOpcode(int seen) {

        if ((seen == PUTFIELD || seen == PUTSTATIC)) {
            XField f = getXFieldOperand();
            if (f != null) {
                if (f.isFinal() || !f.isProtected() && !f.isPublic()) {
                    if (emptyArrayOnTOS) {
                        emptyArray.add(f);
                    } else {
                        nonEmptyArray.add(f);
                    }
                }
            }

        }
        emptyArrayOnTOS = (seen == ANEWARRAY || seen == NEWARRAY || seen == MULTIANEWARRAY && getIntConstant() == 1)
                && getPrevOpcode(1) == ICONST_0;

        if (seen == GETSTATIC || seen == GETFIELD) {
            XField f = getXFieldOperand();
            if (emptyArray.contains(f) && !nonEmptyArray.contains(f) && f.isFinal()) {
                emptyArrayOnTOS = true;
            }
        }
        switch (seen) {
        case INVOKEVIRTUAL:
View Full Code Here

TOP

Related Classes of edu.umd.cs.findbugs.ba.XField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.