Package edu.umd.cs.findbugs.ba.vna

Examples of edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow


            IAnalysisCache analysisCache = Global.getAnalysisCache();

            DepthFirstSearch dfs = analysisCache.getMethodAnalysis(DepthFirstSearch.class, methodDescriptor);
            XMethod xmethod = XFactory.createXMethod(methodDescriptor);
            CFG cfg = analysisCache.getMethodAnalysis(CFG.class, methodDescriptor);
            ValueNumberDataflow vnaDataflow = analysisCache.getMethodAnalysis(ValueNumberDataflow.class, methodDescriptor);
            ConstantPoolGen cpg = analysisCache.getClassAnalysis(ConstantPoolGen.class, methodDescriptor.getClassDescriptor());

            DataflowType dataflow = getDataflow(dfs, xmethod, cfg, vnaDataflow, cpg, analysisCache, methodDescriptor,
                    typeQualifierValue);
View Full Code Here


        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 == Constants.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) {
View Full Code Here

     * @throws CFGBuilderException
     * @throws DataflowAnalysisException
     */
    private BitSet findPreviouslyDeadBlocks() throws DataflowAnalysisException, CFGBuilderException {
        BitSet deadBlocks = new BitSet();
        ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
        for (Iterator<BasicBlock> i = vnaDataflow.getCFG().blockIterator(); i.hasNext();) {
            BasicBlock block = i.next();
            ValueNumberFrame vnaFrame = vnaDataflow.getStartFact(block);
            if (vnaFrame.isTop()) {
                deadBlocks.set(block.getLabel());
            }
        }

View Full Code Here

        BugAccumulator accumulator = new BugAccumulator(bugReporter);

        CFG cfg = classContext.getCFG(method);
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
        ValueNumberDataflow vnDataflow = classContext.getValueNumberDataflow(method);

        ConstantPoolGen cpg = classContext.getConstantPoolGen();
        MethodGen methodGen = classContext.getMethodGen(method);
        if (methodGen == null) {
            return;
        }
        String fullMethodName = methodGen.getClassName() + "." + methodGen.getName();

        String sourceFile = classContext.getJavaClass().getSourceFileName();
        if (DEBUG) {
            System.out.println("\n" + fullMethodName);
        }

        // Process each instruction
        for (Iterator<Location> iter = cfg.locationIterator(); iter.hasNext();) {
            Location location = iter.next();
            InstructionHandle handle = location.getHandle();
            Instruction ins = handle.getInstruction();

            // Only consider invoke instructions
            if (!(ins instanceof InvokeInstruction)) {
                continue;
            }

            InvokeInstruction inv = (InvokeInstruction) ins;

            XMethod invokedMethod = XFactory.createXMethod(inv, cpg);

            String invokedMethodName = invokedMethod.getName();
            String argSignature = invokedMethod.getSignature();
            argSignature = argSignature.substring(0, argSignature.indexOf(')') + 1);
            String call = invokedMethodName+argSignature;
            SignatureParser sigParser = new SignatureParser(inv.getSignature(cpg));

            Collection<Info> collection = callMap.get(call);
            if (!callMap.containsKey(call)) {
                continue;
            }
            for(Info info : collection) {
                Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();
                if (DEBUG) {
                    System.out.println("at " + handle.getPosition() + " Checking call to " + info.interfaceForCall + " : " + invokedMethod);
                }
                try {
                    if (!subtypes2.isSubtype(invokedMethod.getClassDescriptor(), info.interfaceForCall)) {
                        continue;
                    }
                } catch (ClassNotFoundException e) {
                    if (info.interfaceForCall.getClassName().equals("java/util/Collection")
                            && invokedMethod.getClassName().equals("com.google.common.collect.Multiset")) {
                        assert true;
                        // we know this is OK without needing to find definition of Multiset
                    } else {
                        AnalysisContext.reportMissingClass(e);
                        continue;
                    }
                }

                boolean allMethod;

                int typeArgument;
                if (info.typeIndex >= 0) {
                    allMethod = false;
                    typeArgument = info.typeIndex;
                } else {
                    allMethod = true;
                    typeArgument = -(1+info.typeIndex);
                }
                int pos = info.argumentIndex;


                int lhsPos;
                if (inv instanceof INVOKESTATIC) {
                    lhsPos = sigParser.getSlotsFromTopOfStackForParameter(0);
                } else {
                    lhsPos = sigParser.getTotalArgumentSize();
                }

                int stackPos = sigParser.getSlotsFromTopOfStackForParameter(pos);

                TypeFrame frame = typeDataflow.getFactAtLocation(location);
                if (!frame.isValid()) {
                    // This basic block is probably dead
                    continue;
                }


                Type operandType = frame.getStackValue(stackPos);
                if (operandType.equals(TopType.instance())) {
                    // unreachable
                    continue;
                }

                if (operandType.equals(NullType.instance())) {
                    // ignore
                    continue;
                }

                ValueNumberFrame vnFrame = vnDataflow.getFactAtLocation(location);

                if (!vnFrame.isValid()) {
                    AnalysisContext.logError("Invalid value number frame in " + xmethod);
                    continue;
                }

                ValueNumber objectVN = vnFrame.getStackValue(lhsPos);
                ValueNumber argVN = vnFrame.getStackValue(stackPos);

                if (objectVN.equals(argVN)) {
                    String bugPattern = "DMI_COLLECTIONS_SHOULD_NOT_CONTAIN_THEMSELVES";
                    int priority = HIGH_PRIORITY;
                    if (invokedMethodName.equals("removeAll")) {
                        bugPattern = "DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION";
                        priority = NORMAL_PRIORITY;
                    } else if (invokedMethodName.endsWith("All")) {
                        bugPattern = "DMI_VACUOUS_SELF_COLLECTION_CALL";
                        priority = NORMAL_PRIORITY;
                    }
                    if (invokedMethodName.startsWith("contains")) {
                        InstructionHandle next = handle.getNext();
                        if (next != null) {
                            Instruction nextIns = next.getInstruction();

                            if (nextIns instanceof InvokeInstruction) {
                                XMethod nextMethod = XFactory.createXMethod((InvokeInstruction) nextIns, cpg);
                                if (nextMethod.getName().equals("assertFalse")) {
                                    continue;
                                }
                            }
                        }
                    }
                    accumulator.accumulateBug(
                            new BugInstance(this, bugPattern, priority)
                            .addClassAndMethod(methodGen, sourceFile)
                            .addCalledMethod(methodGen, (InvokeInstruction) ins)
                            .addOptionalAnnotation(
                                    ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, objectVN,
                                            vnFrame, "INVOKED_ON")), SourceLineAnnotation.fromVisitedInstruction(
                                                    classContext, methodGen, sourceFile, handle));
                }

                // Only consider generic...
                Type objectType = frame.getStackValue(lhsPos);
                if (!(objectType instanceof GenericObjectType)) {
                    continue;
                }

                GenericObjectType operand = (GenericObjectType) objectType;

                int expectedTypeParameters = 1;
                String simpleName = info.interfaceForCall.getSimpleName();
                if ( simpleName.toLowerCase().endsWith("map") || simpleName.equals("Hashtable")) {
                    expectedTypeParameters = 2;
                } else if (simpleName.equals("Table")) {
                    expectedTypeParameters = 3;
                }

                // ... containers
                if (!operand.hasParameters()) {
                    continue;
                }
                if (operand.getNumParameters() != expectedTypeParameters) {
                    continue;
                }
                ClassDescriptor operandClass = DescriptorFactory.getClassDescriptor(operand);
                if (!isGenericCollection(operandClass)) {
                    continue;
                }

                if (expectedTypeParameters == 2 &&
                        Subtypes2.instanceOf(operandClass, Map.class)
                        && !TypeFrameModelingVisitor.isStraightGenericMap(operandClass)) {
                    continue;
                }
                Type expectedType;
                if (allMethod) {
                    expectedType = operand;
                } else {
                    expectedType = operand.getParameterAt(typeArgument);
                }
                Type actualType = frame.getStackValue(stackPos);
                Type equalsType = actualType;
                if (allMethod) {
                    if (!(actualType instanceof GenericObjectType)) {
                        continue;
                    }
                    equalsType = ((GenericObjectType)actualType).getParameterAt(typeArgument);
                }


                IncompatibleTypes matchResult = compareTypes(expectedType, actualType, allMethod);

                boolean parmIsObject = expectedType.getSignature().equals("Ljava/lang/Object;");
                boolean selfOperation = !allMethod && operand.equals(actualType) && !parmIsObject;
                if (!allMethod && !parmIsObject && actualType instanceof GenericObjectType) {

                    GenericObjectType p2 = (GenericObjectType) actualType;
                    List<? extends ReferenceType> parameters = p2.getParameters();
                    if (parameters != null && parameters.equals(operand.getParameters())) {
                        selfOperation = true;
                    }
                }

                if (!selfOperation && ( matchResult == IncompatibleTypes.SEEMS_OK || matchResult.getPriority() == Priorities.IGNORE_PRIORITY)) {
                    continue;
                }

                if (invokedMethodName.startsWith("contains") || invokedMethodName.equals("remove")) {
                    InstructionHandle next = handle.getNext();
                    if (next != null) {
                        Instruction nextIns = next.getInstruction();

                        if (nextIns instanceof InvokeInstruction) {
                            XMethod nextMethod = XFactory.createXMethod((InvokeInstruction) nextIns, cpg);
                            if (nextMethod.getName().equals("assertFalse")) {
                                continue;
                            }
                        }
                    }
                } else if (invokedMethodName.equals("get") || invokedMethodName.equals("remove")) {
                    InstructionHandle next = handle.getNext();
                    if (next != null) {
                        Instruction nextIns = next.getInstruction();

                        if (nextIns instanceof InvokeInstruction) {
                            XMethod nextMethod = XFactory.createXMethod((InvokeInstruction) nextIns, cpg);
                            if (nextMethod.getName().equals("assertNull")) {
                                continue;
                            }
                        }
                    }
                }
                boolean noisy = false;
                if (invokedMethodName.equals("get")) {
                    UnconditionalValueDerefDataflow unconditionalValueDerefDataflow = classContext
                            .getUnconditionalValueDerefDataflow(method);

                    UnconditionalValueDerefSet unconditionalDeref = unconditionalValueDerefDataflow.getFactAtLocation(location);
                    ValueNumberFrame vnAfter = vnDataflow.getFactAfterLocation(location);
                    ValueNumber top = vnAfter.getTopValue();
                    noisy = unconditionalDeref.getValueNumbersThatAreUnconditionallyDereferenced().contains(top);
                }
                // Prepare bug report
                SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen,
View Full Code Here

            checkWriteToStaticFromInstanceMethod: if (f.getName().indexOf("class$") != 0) {
                int priority = LOW_PRIORITY;
                if (f.isReferenceType()) {
                    try {
                        ValueNumberDataflow vnaDataflow = getClassContext().getValueNumberDataflow(getMethod());
                        IsNullValueDataflow invDataflow = getClassContext().getIsNullValueDataflow(getMethod());
                        ValueNumberFrame vFrame = vnaDataflow.getAnalysis().getFactAtPC(vnaDataflow.getCFG(), getPC());
                        IsNullValueFrame iFrame = invDataflow.getAnalysis().getFactAtPC(invDataflow.getCFG(), getPC());
                        AvailableLoad l = new AvailableLoad(f);
                        ValueNumber[] availableLoads = vFrame.getAvailableLoad(l);
                        if (availableLoads != null && iFrame.isTrackValueNumbers()) {
                            for (ValueNumber v : availableLoads) {
View Full Code Here

        ConstantPoolGen cpg = classContext.getConstantPoolGen();
        Dataflow<BitSet, LiveLocalStoreAnalysis> llsaDataflow = classContext.getLiveLocalStoreDataflow(method);

        MethodGen methodGen = classContext.getMethodGen(method);
        CFG cfg = classContext.getCFG(method);
        ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);

        String sourceFileName = javaClass.getSourceFileName();

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

            InstructionHandle handle = location.getHandle();
            Instruction ins = handle.getInstruction();

            if (ins instanceof InvokeInstruction) {
                InvokeInstruction invoke = (InvokeInstruction) ins;
                String className = invoke.getClassName(cpg);

                if (invoke.getMethodName(cpg).equals("putIfAbsent")) {
                    String signature = invoke.getSignature(cpg);
                    if (signature.equals("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
                            && !(invoke instanceof INVOKESTATIC) && extendsConcurrentMap(className)) {
                        InstructionHandle next = handle.getNext();
                        boolean isIgnored = next != null && next.getInstruction() instanceof POP;
                        //                        boolean isImmediateNullTest = next != null
                        //                                && (next.getInstruction() instanceof IFNULL || next.getInstruction() instanceof IFNONNULL);
                        if (countOtherCalls || isIgnored) {
                            BitSet live = llsaDataflow.getAnalysis().getFactAtLocation(location);
                            ValueNumberFrame vna = vnaDataflow.getAnalysis().getFactAtLocation(location);
                            ValueNumber vn = vna.getTopValue();

                            int locals = vna.getNumLocals();
                            //                            boolean isRetained = false;
                            for (int pos = 0; pos < locals; pos++) {
                                if (vna.getValue(pos).equals(vn) && live.get(pos)) {
                                    BugAnnotation ba = ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, vn,
                                            vnaDataflow.getFactAtLocation(location), "VALUE_OF");
                                    if (ba == null) {
                                        continue;
                                    }
                                    String pattern = "RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED";
                                    if (!isIgnored) {
View Full Code Here

    static boolean comparatorMethod(String methodName) {
        return  methodName.equals("compareTo") || methodName.equals("compareToIgnoreCase");
    }
    private void analyzeMethod(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {
        CFG cfg = classContext.getCFG(method);
        ValueNumberDataflow valueNumberDataflow = classContext.getValueNumberDataflow(method);
        ConstantPoolGen cpg = classContext.getConstantPoolGen();
        MethodGen methodGen = classContext.getMethodGen(method);
        String sourceFile = classContext.getJavaClass().getSourceFileName();

        for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
View Full Code Here

                }

                IfInstruction branchInstruction = (IfInstruction) branch.getHandle().getInstruction();

                IsNullValueDataflow isNullValueDataflow = getClassContext().getIsNullValueDataflow(getMethod());
                ValueNumberDataflow valueNumberDataflow = getClassContext().getValueNumberDataflow(getMethod());
                UnconditionalValueDerefDataflow unconditionalValueDerefDataflow = getClassContext()
                        .getUnconditionalValueDerefDataflow(getMethod());
                ValueNumberFrame valueNumberFact = valueNumberDataflow.getFactAtLocation(produced);
                IsNullValueFrame isNullFact = isNullValueDataflow.getFactAtLocation(produced);
                ValueNumber value = valueNumberFact.getTopValue();
                if (isNullFact.getTopValue().isDefinitelyNotNull()) {
                    return;
                }
View Full Code Here

        IAnalysisCache analysisCache = Global.getAnalysisCache();
        ForwardTypeQualifierDataflowFactory forwardDataflowFactory = analysisCache.getMethodAnalysis(
                ForwardTypeQualifierDataflowFactory.class, methodDescriptor);
        BackwardTypeQualifierDataflowFactory backwardDataflowFactory = analysisCache.getMethodAnalysis(
                BackwardTypeQualifierDataflowFactory.class, methodDescriptor);
        ValueNumberDataflow vnaDataflow = analysisCache.getMethodAnalysis(ValueNumberDataflow.class, methodDescriptor);

        for (TypeQualifierValue<?> typeQualifierValue : relevantQualifiers) {

            try {
                checkQualifier(xMethod, cfg, typeQualifierValue, forwardDataflowFactory, backwardDataflowFactory,
View Full Code Here

        JavaClass jclass = classContext.getJavaClass();
        XMethod xmethod = XFactory.createXMethod(jclass, method);
        try {
            CFG cfg = classContext.getCFG(method);

            ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
            UnconditionalValueDerefDataflow dataflow = classContext.getUnconditionalValueDerefDataflow(method);

            SignatureParser parser = new SignatureParser(method.getSignature());
            int paramLocalOffset = method.isStatic() ? 0 : 1;

            // Build BitSet of params that are unconditionally dereferenced
            BitSet unconditionalDerefSet = new BitSet();
            UnconditionalValueDerefSet entryFact = dataflow.getResultFact(cfg.getEntry());
            Iterator<String> paramIterator = parser.parameterSignatureIterator();
            int i = 0;
            while (paramIterator.hasNext()) {
                String paramSig = paramIterator.next();

                ValueNumber paramVN = vnaDataflow.getAnalysis().getEntryValue(paramLocalOffset);

                handleParameter: if (entryFact.isUnconditionallyDereferenced(paramVN)) {
                    TypeQualifierAnnotation directTypeQualifierAnnotation = TypeQualifierApplications
                            .getDirectTypeQualifierAnnotation(xmethod, i, nonnullTypeQualifierValue);
                    TypeQualifierAnnotation typeQualifierAnnotation = TypeQualifierApplications
View Full Code Here

TOP

Related Classes of edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow

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.