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

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


            ParameterNullnessPropertyDatabase unconditionalDerefParamDatabase = AnalysisContext.currentAnalysisContext()
                    .getUnconditionalDerefParamDatabase();
            Iterator<BasicBlock> bbIter = cfg.blockIterator();
            ConstantPoolGen cpg = classContext.getConstantPoolGen();
            ValueNumber valueNumberForThis = null;
            if (!method.isStatic()) {
                ValueNumberFrame frameAtEntry = vna.getStartFact(cfg.getEntry());
                valueNumberForThis = frameAtEntry.getValue(0);
            }

            NullnessAnnotation methodAnnotation = getMethodNullnessAnnotation(classContext, method);

            while (bbIter.hasNext()) {
                BasicBlock basicBlock = bbIter.next();

                if (basicBlock.isNullCheck()) {
                    InstructionHandle exceptionThrowerHandle = basicBlock.getExceptionThrower();
                    Instruction exceptionThrower = exceptionThrowerHandle.getInstruction();
                    ValueNumberFrame vnaFrame = vna.getStartFact(basicBlock);
                    if (!vnaFrame.isValid()) {
                        continue;
                    }
                    ValueNumber valueNumber = vnaFrame.getInstance(exceptionThrower, cpg);

                    Location location = new Location(exceptionThrowerHandle, basicBlock);
                    if (valueNumberForThis != valueNumber) {
                        derefs.add(location, valueNumber, PointerUsageRequiringNonNullValue.getPointerDereference());
                    }

                }
            }

            for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
                Location location = i.next();
                InstructionHandle handle = location.getHandle();
                Instruction ins = handle.getInstruction();
                ValueNumberFrame valueNumberFrame = vna.getFactAtLocation(location);
                TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
                if (ins instanceof InvokeInstruction) {
                    InvokeInstruction inv = (InvokeInstruction) ins;
                    XMethod m = XFactory.createXMethod(inv, cpg);
                    SignatureParser sigParser = new SignatureParser(m.getSignature());
                    int numParams = sigParser.getNumParameters();

                    // Check nonnull annotations

                    for (int j = 0; j < numParams; j++) {
                        if (db.parameterMustBeNonNull(m, j)) {
                            int slot = sigParser.getSlotsFromTopOfStackForParameter(j);
                            ValueNumber valueNumber = valueNumberFrame.getStackValue(slot);
                            if (valueNumberForThis != valueNumber) {
                                derefs.add(location, valueNumber,
                                        PointerUsageRequiringNonNullValue.getPassedAsNonNullParameter(m, j));
                            }
                        }
                    }

                    // Check actual targets
                    try {
                        Set<JavaClassAndMethod> targetMethodSet = Hierarchy.resolveMethodCallTargets(inv, typeFrame, cpg);
                        BitSet unconditionallyDereferencedNullArgSet = null;
                        for (JavaClassAndMethod targetMethod : targetMethodSet) {

                            ParameterProperty property = unconditionalDerefParamDatabase.getProperty(targetMethod
                                    .toMethodDescriptor());
                            if (property == null) {
                                unconditionallyDereferencedNullArgSet = null;
                                break;
                            }
                            BitSet foo = property.getAsBitSet();
                            if (unconditionallyDereferencedNullArgSet == null) {
                                unconditionallyDereferencedNullArgSet = foo;
                            } else {
                                unconditionallyDereferencedNullArgSet.intersects(foo);
                            }
                            if (unconditionallyDereferencedNullArgSet.isEmpty()) {
                                break;
                            }
                        }

                        if (unconditionallyDereferencedNullArgSet != null && !unconditionallyDereferencedNullArgSet.isEmpty()
                                && valueNumberFrame.isValid()) {
                            for (int j = unconditionallyDereferencedNullArgSet.nextSetBit(0); j >= 0; j = unconditionallyDereferencedNullArgSet
                                    .nextSetBit(j + 1)) {
                                int slot = sigParser.getSlotsFromTopOfStackForParameter(j);
                                ValueNumber valueNumber = valueNumberFrame.getStackValue(slot);
                                if (valueNumberForThis != valueNumber) {
                                    derefs.add(location, valueNumber,
                                            PointerUsageRequiringNonNullValue.getPassedAsNonNullParameter(m, j));
                                }
                            }
                        }

                    } catch (ClassNotFoundException e) {
                        AnalysisContext.reportMissingClass(e);
                    }

                } else if (ins instanceof IFNONNULL && UnconditionalValueDerefAnalysis.isNullCheck(handle, cpg)) {
                    ValueNumber valueNumber = valueNumberFrame.getTopValue();
                    derefs.add(location, valueNumber, PointerUsageRequiringNonNullValue.getPointerNullChecked());
                } else if (ins instanceof ARETURN && methodAnnotation == NullnessAnnotation.NONNULL) {
                    ValueNumber valueNumber = valueNumberFrame.getTopValue();
                    if (valueNumberForThis != valueNumber) {
                        derefs.add(location, valueNumber,
                                PointerUsageRequiringNonNullValue.getReturnFromNonNullMethod(thisMethod));
                    }

                } else if (ins instanceof PUTFIELD || ins instanceof PUTSTATIC) {
                    FieldInstruction inf = (FieldInstruction) ins;
                    XField field = XFactory.createXField(inf, cpg);
                    NullnessAnnotation annotation = AnalysisContext.currentAnalysisContext().getNullnessAnnotationDatabase()
                            .getResolvedAnnotation(field, false);
                    if (annotation == NullnessAnnotation.NONNULL) {
                        ValueNumber valueNumber = valueNumberFrame.getTopValue();
                        if (valueNumberForThis != valueNumber) {
                            derefs.add(location, valueNumber, PointerUsageRequiringNonNullValue.getStoredIntoNonNullField(field));
                        }
                    }
View Full Code Here


            }
            ValueNumberFrame vFrame = vnaDataflow.getFactAtLocation(location);
            if (paramValueNumberSet == null) {
                paramValueNumberSet = getParameterValueNumbers(classContext, method, cfg);
            }
            ValueNumber valueNumber = vFrame.getTopValue();
            BugAnnotation valueSource = ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, valueNumber, vFrame,
                    "VALUE_OF");
            // XXX call below causes 86% of all OpcodeStackDetector.EarlyExitException (getPC() == targetPC) thrown (13000 on java* JDK7 classes)
            BugAnnotation source = BugInstance.getSourceForTopStackValue(classContext, method, location);
            boolean isParameter = paramValueNumberSet.contains(valueNumber) && source instanceof LocalVariableAnnotation;

            try {
                JavaClass castJavaClass = Repository.lookupClass(castName);
                JavaClass refJavaClass = Repository.lookupClass(refName);

                boolean upcast = Repository.instanceOf(refJavaClass, castJavaClass);
                if (upcast || typesAreEqual) {
                    if (!isCast) {
                        accumulator.accumulateBug(new BugInstance(this, "BC_VACUOUS_INSTANCEOF", NORMAL_PRIORITY)
                        .addClassAndMethod(methodGen, sourceFile).addFoundAndExpectedType(refType, castType),
                        sourceLineAnnotation);
                    }
                } else {
                    boolean castMayThrow = !Repository.instanceOf(refJavaClass, castJavaClass);
                    boolean downCast = Repository.instanceOf(castJavaClass, refJavaClass);

                    if (!operandTypeIsExact && refName.equals("java.lang.Object")) {
                        continue;
                    }
                    double rank = 0.0;
                    boolean castToConcreteCollection = concreteCollectionClasses.contains(castName)
                            && abstractCollectionClasses.contains(refName);
                    boolean castToAbstractCollection = abstractCollectionClasses.contains(castName)
                            && veryAbstractCollectionClasses.contains(refName);
                    int position = location.getHandle().getPosition();
                    int catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(),
                            "java/lang/ClassCastException", position);



                    if (!operandTypeIsExact) {
                        rank = Analyze.deepInstanceOf(refJavaClass, castJavaClass);
                        if (castToConcreteCollection && rank > 0.6) {
                            rank = (rank + 0.6) / 2;
                        } else if (castToAbstractCollection && rank > 0.3) {
                            rank = (rank + 0.3) / 2;
                        }
                    }
                    /*
                    if (false) {
                        System.out.println("Rank:\t" + rank + "\t" + refName + "\t" + castName);
                    }
                     */
                    boolean completeInformation = (!castJavaClass.isInterface() && !refJavaClass.isInterface())
                            || refJavaClass.isFinal() || castJavaClass.isFinal();
                    if (DEBUG) {
                        System.out.println(" In " + classContext.getFullyQualifiedMethodName(method));
                        System.out.println("At pc: " + handle.getPosition());
                        System.out.println("cast from " + refName + " to " + castName);
                        System.out.println("  cast may throw: " + castMayThrow);
                        System.out.println("  is downcast: " + downCast);
                        System.out.println("  operand type is exact: " + operandTypeIsExact);

                        System.out.println("  complete information: " + completeInformation);
                        System.out.println("  isParameter: " + valueNumber);
                        System.out.println("  score: " + rank);
                        System.out.println("  source is: " + valueSource);
                        if (catchSize < Integer.MAX_VALUE) {
                            System.out.println("  catch block size is: " + catchSize);
                        }
                        if (constantClass != null) {
                            System.out.println("  constant class " + constantClass + " at " + pcForConstantClass);
                        }
                        if (handle.getPrev() == null) {
                            System.out.println("  prev is null");
                        } else {
                            System.out.println("  prev is " + handle.getPrev());
                        }
                    }
                    if (!isCast && castMayThrow && valueSource != null) {
                        String oldCheck = instanceOfChecks.get(valueSource);
                        if (oldCheck == null) {
                            instanceOfChecks.put(valueSource, castName);
                        } else if (!oldCheck.equals(castName)) {
                            instanceOfChecks.put(valueSource, "");
                        }
                    }
                    if (!downCast && completeInformation || operandTypeIsExact) {
                        String bugPattern;
                        if (isCast) {
                            if (downCast && operandTypeIsExact) {
                                if (refSig.equals("[Ljava/lang/Object;") && source instanceof MethodAnnotation
                                        && ((MethodAnnotation) source).getMethodName().equals("toArray")
                                        && ((MethodAnnotation) source).getMethodSignature().equals("()[Ljava/lang/Object;")) {
                                    bugPattern = "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY";
                                } else {
                                    bugPattern = "BC_IMPOSSIBLE_DOWNCAST";
                                }
                            } else {
                                bugPattern = "BC_IMPOSSIBLE_CAST";
                            }
                        } else {
                            bugPattern = "BC_IMPOSSIBLE_INSTANCEOF";
                        }

                        bugReporter.reportBug(new BugInstance(this, bugPattern, isCast ? HIGH_PRIORITY : NORMAL_PRIORITY)
                        .addClassAndMethod(methodGen, sourceFile)
                        .addFoundAndExpectedType(refType, castType).addOptionalUniqueAnnotations(valueSource, source)
                        .addSourceLine(sourceLineAnnotation));
                    } else if (isCast && rank < 0.9
                            && !valueNumber.hasFlag(ValueNumber.ARRAY_VALUE)) {

                        int priority = NORMAL_PRIORITY;

                        @CheckForNull String oldCheck = instanceOfChecks.get(valueSource);
                        if (DEBUG) {
                            System.out.println("Old check: " + oldCheck);
                        }
                        if (castName.equals(oldCheck)) {
                            priority += 1;
                        } else if ("".equals(oldCheck)) {
                            priority += 1;
                            if (!(source instanceof LocalVariableAnnotation)) {
                                continue;
                            }
                        }

                        if (rank > 0.75) {
                            priority += 2;
                        } else if (rank > 0.5) {
                            priority += 1;
                        } else if (rank > 0.25) {
                            priority += 0;
                        } else {
                            priority--;
                        }



                        if (DEBUG) {
                            System.out.println(" priority a: " + priority);
                        }
                        if (methodGen.getClassName().startsWith(refName) || methodGen.getClassName().startsWith(castName)) {
                            priority += 1;
                        }
                        if (DEBUG) {
                            System.out.println(" priority b: " + priority);
                        }
                        if (castJavaClass.isInterface() && !castToAbstractCollection) {
                            priority++;
                        }
                        if (DEBUG) {
                            System.out.println(" priority c: " + priority);
                        }
                        if (castToConcreteCollection && veryAbstractCollectionClasses.contains(refName)) {
                            priority--;
                        }
                        if (DEBUG) {
                            System.out.println(" priority d: " + priority);
                        }
                        if (priority <= LOW_PRIORITY && !castToAbstractCollection && !castToConcreteCollection
                                && (refJavaClass.isInterface() || refJavaClass.isAbstract())) {
                            priority++;
                        }
                        if (DEBUG) {
                            System.out.println(" priority e: " + priority);
                        }
                        if (DEBUG) {
                            System.out.println(" ref name: " + refName);
                        }
                        if (methodGen.getName().equals("compareTo")) {
                            priority++;
                        } else if (methodGen.isPublic() && isParameter && !castName.equals(oldCheck)) {
                            priority--;
                        }
                        if (wasMethodInvocationWasGeneric && valueNumber.hasFlag(ValueNumber.RETURN_VALUE)) {
                            continue;
                        }
                        if (constantClass != null && pcForConstantClass +20 >= pc && valueNumber.hasFlag(ValueNumber.RETURN_VALUE)
                                && ClassName.toDottedClassName(constantClass).equals(castName)) {
                            priority += 2;
                        }
                        if (DEBUG) {
                            System.out.println(" priority f: " + priority);
                        }
                        if (source instanceof MethodAnnotation) {
                            MethodAnnotation m = (MethodAnnotation) source;
                            XMethod xm = m.toXMethod();
                            if (xm != null && (xm.isPrivate() || xm.isStatic()) && priority == Priorities.LOW_PRIORITY) {
                                continue;
                            }
                        }

                        if (valueNumber.hasFlag(ValueNumber.RETURN_VALUE) &&  priority < Priorities.LOW_PRIORITY) {
                            priority = Priorities.LOW_PRIORITY;
                        }
                        if (DEBUG) {
                            System.out.println(" priority g: " + priority);
                        }

                        if (DEBUG) {
                            System.out.println(" priority h: " + priority);
                        }

                        if (catchSize < 15) {
                            return;
                        }
                        if (catchSize < 25) {
                            priority++;
                        }
                        if (DEBUG) {
                            System.out.println(" priority i: " + priority);
                        }


                        if (priority < HIGH_PRIORITY) {
                            priority = HIGH_PRIORITY;
                        }
                        if (priority <= LOW_PRIORITY) {
                            String bug = "BC_UNCONFIRMED_CAST";
                            if (valueNumber.hasFlag(ValueNumber.RETURN_VALUE) || valueSource instanceof MethodAnnotation) {
                                bug = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE";
                            } else if (castToConcreteCollection) {
                                bug = "BC_BAD_CAST_TO_CONCRETE_COLLECTION";
                            } else if (castToAbstractCollection) {
                                bug = "BC_BAD_CAST_TO_ABSTRACT_COLLECTION";
View Full Code Here

            valueNumbersUnconditionallyDereferenced.set(skipMe.getNumber(), resultForSkippedValue);
        }

        // For each unconditionally dereferenced value...
        for (int i = 0; i < numValueNumbersInMethod; i++) {
            ValueNumber vn = valueNumberFactory.forNumber(i);
            if (vn.equals(skipMe)) {
                continue;
            }
            Set<Location> factDerefLocationSet = fact.derefLocationSetMap.get(vn);
            if (valueNumbersUnconditionallyDereferenced.get(i)) {
                if (factDerefLocationSet != null && !factDerefLocationSet.isEmpty()) {
View Full Code Here

        // Compute the union of the unconditionally dereferenced value sets
        valueNumbersUnconditionallyDereferenced.or(fact.valueNumbersUnconditionallyDereferenced);

        // For each unconditionally dereferenced value...
        for (int i = 0; i < numValueNumbersInMethod; i++) {
            ValueNumber vn = valueNumberFactory.forNumber(i);

            if (fact.valueNumbersUnconditionallyDereferenced.get(i)) {
                // Compute the union of the dereference locations for
                // this value number.
                Set<Location> derefLocationSet = derefLocationSetMap.get(vn);
View Full Code Here

        return result;
    }

    public void retainOnlyTheseValueNumbers(Collection<ValueNumber> valueNumbers) {
        for (Iterator<ValueNumber> i = derefLocationSetMap.keySet().iterator(); i.hasNext();) {
            ValueNumber v = i.next();
            if (!valueNumbers.contains(v)) {
                i.remove();
                valueNumbersUnconditionallyDereferenced.clear(v.getNumber());
            }
        }
    }
View Full Code Here

        // if the target is an exception handler.
        // So, merge the minimum number of slots in either frame.
        int numSlotsToMerge = Math.min(sourceVnaFrame.getNumSlots(), targetVnaFrame.getNumSlots());

        for (int i = 0; i < numSlotsToMerge; i++) {
            ValueNumber targetVN = targetVnaFrame.getValue(i);
            ValueNumber sourceVN = sourceVnaFrame.getValue(i);

            if (!targetVN.equals(sourceVN) && targetVN.hasFlag(ValueNumber.PHI_NODE)) {
                // targetVN is a phi result
                if (DEBUG_VERBOSE) {
                    System.out.println("Phi node: " + fact.valueNumberToString(sourceVN) + " -> "
View Full Code Here

            InvokeInstruction ii = (InvokeInstruction) i;
            XMethod m = XFactory.createXMethod(ii, cpg);
            if (TypeQualifierDataflowAnalysis.isIdentifyFunctionForTypeQualifiers(m)) {
                ValueNumberFrame vnaFrameAtLocation = vnaDataflow.getFactAtLocation(location);
                ValueNumberFrame vnaFrameAfterInstruction = vnaDataflow.getFactAfterLocation(location);
                ValueNumber in = vnaFrameAtLocation.getStackValue(0);
                ValueNumber out = vnaFrameAfterInstruction.getStackValue(0);
                fact.copyInfo(in, out);
                return;
            }
        }
View Full Code Here

    public void initEntryFact(LockSet result) {
        result.clear();
        result.setDefaultLockCount(0);

        if (isSynchronized && !isStatic) {
            ValueNumber thisValue = vna.getThisValue();
            result.setLockCount(thisValue.getNumber(), 1);
        } else if (isSynchronized && isStatic) {
            ValueNumber thisValue = vna.getClassObjectValue(methodGen.getClassName());
            result.setLockCount(thisValue.getNumber(), 1);
        }
    }
View Full Code Here

            if (lockSet == null) {
                lockSet = new LockSet();
                lockSet.setDefaultLockCount(0);
                if (method.isSynchronized() && !method.isStatic()) {
                    // LockSet contains just the "this" reference
                    ValueNumber instance = vnaDataflow.getAnalysis().getThisValue();
                    lockSet.setLockCount(instance.getNumber(), 1);
                } else {
                    // LockSet is completely empty - nothing to do
                }
                cache.put(location, lockSet);
            }
View Full Code Here

        When when = (returnValueAnnotation != null) ? returnValueAnnotation.when : When.UNKNOWN;

        // Model return statement
        ValueNumberFrame vnaFrameAtReturn = vnaDataflow.getFactAtLocation(location);
        if (vnaFrameAtReturn.isValid()) {
            ValueNumber topValue = vnaFrameAtReturn.getTopValue();
            SourceSinkInfo sink = new SourceSinkInfo(SourceSinkType.RETURN_VALUE, location, topValue, when);
            registerSourceSink(sink);
        }
    }
View Full Code Here

TOP

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

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.