Package edu.umd.cs.findbugs.ba.type

Examples of edu.umd.cs.findbugs.ba.type.TypeDataflow$LocationAndFactPair


    }

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


        // We don't adequately model instanceof interfaces yet
        if (bytecodeSet.get(Constants.INSTANCEOF) || bytecodeSet.get(Constants.CHECKCAST)) {
            return;
        }
        CFG cfg = classContext.getCFG(method);
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
        ConstantPoolGen cpg = classContext.getConstantPoolGen();

        String sourceFile = classContext.getJavaClass().getSourceFileName();
        if (DEBUG) {
            String methodName = methodGen.getClassName() + "." + methodGen.getName();
            System.out.println("Checking " + methodName);
        }

        for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
            Location location = i.next();
            InstructionHandle handle = location.getHandle();
            int pc = handle.getPosition();
            Instruction ins = handle.getInstruction();

            if (!(ins instanceof InvokeInstruction)) {
                continue;
            }

            InvokeInstruction invoke = (InvokeInstruction) ins;
            String mName = invoke.getMethodName(cpg);
            if (!mName.equals("writeObject")) {
                continue;
            }
            String cName = invoke.getClassName(cpg);
            if (!cName.equals("java.io.ObjectOutput") && !cName.equals("java.io.ObjectOutputStream")) {
                continue;
            }

            TypeFrame frame = typeDataflow.getFactAtLocation(location);
            if (!frame.isValid()) {
                // This basic block is probably dead
                continue;
            }
            Type operandType = frame.getTopValue();
View Full Code Here

            cfg = classContext.getCFG(method);
        } catch (CFGBuilderException e1) {
            AnalysisContext.logError("Coult not get CFG", e1);
            return;
        }
        TypeDataflow typeDataflow;
        try {
            typeDataflow = classContext.getTypeDataflow(method);
        } catch (CheckedAnalysisException e1) {
            AnalysisContext.logError("Coult not get Type dataflow", e1);
            return;
        }

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

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

            if (ins.getOpcode() == Constants.INVOKEVIRTUAL) {
                INVOKEVIRTUAL iv = (INVOKEVIRTUAL) ins;

                String methodName = iv.getMethodName(cpg);
                String methodSig = iv.getSignature(cpg);
                if (methodName.equals("wait")
                        && (methodSig.equals("()V") || methodSig.equals("(J)V") || methodSig.equals("(JI)V"))
                        || (methodName.equals("notify") || methodName.equals("notifyAll")) && methodSig.equals("()V")) {
                    try {
                        TypeFrame frame = typeDataflow.getFactAtLocation(location);
                        if (!frame.isValid()) {
                            continue;
                        }
                        Type type = frame.getInstance(ins, cpg);
                        if (!(type instanceof ReferenceType)) {
                            // Something is deeply wrong if a non-reference type
                            // is used for a method invocation. But, that's
                            // really a
                            // verification problem.
                            continue;
                        }
                        ClassDescriptor classDescriptor = DescriptorFactory.createClassDescriptorFromSignature(type
                                .getSignature());
                        if (classDescriptor.equals(classContext.getClassDescriptor())) {
                            continue;
                        }
                        if (!classDescriptor.getClassName().startsWith("java/util/concurrent")) {
                            continue;
                        }
                        XClass c = Lookup.getXClass(classDescriptor);
                        XMethod m;
                        int priority = NORMAL_PRIORITY;
                        if (methodName.equals("wait")) {
                            m = c.findMethod("await", "()V", false);
                            priority = HIGH_PRIORITY;
                        } else if (methodName.equals("notify")) {
                            m = c.findMethod("signal", "()V", false);
                            if (m == null) {
                                m = c.findMethod("countDown", "()V", false);
                            }
                        } else if (methodName.equals("notifyAll")) {
                            m = c.findMethod("signalAll", "()V", false);
                            if (m == null) {
                                m = c.findMethod("countDown", "()V", false);
                            }
                        } else {
                            throw new IllegalStateException("Unexpected methodName: " + methodName);
                        }

                        if (m != null && m.isPublic() && c.isPublic()) {
                            bugReporter.reportBug(new BugInstance(this, "JML_JSR166_CALLING_WAIT_RATHER_THAN_AWAIT", priority)
                            .addClassAndMethod(classContext.getJavaClass(), method).addCalledMethod(cpg, iv).addMethod(m)
                            .describe(MethodAnnotation.METHOD_ALTERNATIVE_TARGET).addType(classDescriptor)
                            .describe(TypeAnnotation.FOUND_ROLE).addSourceLine(classContext, method, location));
                        }

                    } catch (CheckedAnalysisException e) {
                        AnalysisContext.logError("Coult not get Type dataflow", e);
                        continue;
                    }

                }

            }

            if (ins.getOpcode() != Constants.MONITORENTER) {
                continue;
            }
            Type type;
            try {
                TypeFrame frame = typeDataflow.getFactAtLocation(location);
                if (!frame.isValid()) {
                    continue;
                }
                type = frame.getInstance(ins, cpg);
            } catch (CheckedAnalysisException e) {
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;
                }
View Full Code Here

    DataflowAnalysisException {
        if (BCELUtil.isSynthetic(method)|| (method.getAccessFlags() & Constants.ACC_BRIDGE) == Constants.ACC_BRIDGE) {
            return;
        }
        CFG cfg = classContext.getCFG(method);
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
        ConstantPoolGen constantPoolGen = classContext.getConstantPoolGen();

        locationLoop: 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;
            }
            if (ins instanceof INVOKEINTERFACE) {
                continue;
            }

            InvokeInstruction inv = (InvokeInstruction) ins;
            TypeFrame frame = typeDataflow.getFactAtLocation(location);

            String methodName = inv.getMethodName(constantPoolGen);
            if (methodName.toLowerCase().indexOf("unsupported") >= 0) {
                continue;
            }
View Full Code Here

                // 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) {
View Full Code Here

        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) {
                                        pattern = "UNKNOWN";
                                    }
                                    Type type = typeDataflow.getAnalysis().getFactAtLocation(location).getTopValue();
                                    int priority = getPriorityForBeingMutable(type);
                                    BugInstance bugInstance = new BugInstance(this, pattern, priority)
                                    .addClassAndMethod(methodGen, sourceFileName).addCalledMethod(methodGen, invoke)
                                    .add(new TypeAnnotation(type)).add(ba);
                                    SourceLineAnnotation where = SourceLineAnnotation.fromVisitedInstruction(classContext,
View Full Code Here

            return;
        }
        CFG cfg = classContext.getCFG(method);

        ConstantPoolGen cpg = classContext.getConstantPoolGen();
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);

        for (Iterator<BasicBlock> i = cfg.blockIterator(); i.hasNext();) {
            BasicBlock basicBlock = i.next();

            // Check if it's a method invocation.
            if (!basicBlock.isExceptionThrower()) {
                continue;
            }
            InstructionHandle thrower = basicBlock.getExceptionThrower();
            Instruction ins = thrower.getInstruction();
            if (!(ins instanceof InvokeInstruction)) {
                continue;
            }

            InvokeInstruction inv = (InvokeInstruction) ins;
            boolean foundThrower = false;
            boolean foundNonThrower = false;

            if (inv instanceof INVOKEINTERFACE) {
                continue;
            }

            String className = inv.getClassName(cpg);

            Location loc = new Location(thrower, basicBlock);
            TypeFrame typeFrame = typeDataflow.getFactAtLocation(loc);
            XMethod primaryXMethod = XFactory.createXMethod(inv, cpg);
            // if (primaryXMethod.isAbstract()) continue;
            Set<XMethod> targetSet = null;
            try {
View Full Code Here

        if (cfg.isFlagSet(CFG.FOUND_INEXACT_UNCONDITIONAL_THROWERS)) {
            return;
        }
        BitSet liveStoreSetAtEntry = llsaDataflow.getAnalysis().getResultFact(cfg.getEntry());
        BitSet complainedAbout = new BitSet();
        TypeDataflow typeDataflow = classContext.getTypeDataflow(method);

        // Get number of locals that are parameters.
        int localsThatAreParameters = PreorderVisitor.getNumberArguments(method.getSignature());
        if (!method.isStatic()) {
            localsThatAreParameters++;
        }

        // Scan method to determine number of loads, stores, and increments
        // of local variables.
        countLocalStoresLoadsAndIncrements(localStoreCount, localLoadCount, localIncrementCount, cfg);
        for (int i = 0; i < localsThatAreParameters; i++) {
            localStoreCount[i]++;
        }

        // For each source line, keep track of # times
        // the line was a live store. This can eliminate false positives
        // due to inlining of finally blocks.
        BitSet liveStoreSourceLineSet = new BitSet();

        // Scan method for
        // - dead stores
        // - stores to parameters that are dead upon entry to the method
        for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
            Location location = i.next();

            BugInstance pendingBugReportAboutOverwrittenParameter = null;
            try {
                WarningPropertySet<WarningProperty> propertySet = new WarningPropertySet<WarningProperty>();
                // Skip any instruction which is not a store
                if (!isStore(location)) {
                    continue;
                }

                // Heuristic: exception handler blocks often contain
                // dead stores generated by the compiler.
                if (location.getBasicBlock().isExceptionHandler()) {
                    propertySet.addProperty(DeadLocalStoreProperty.EXCEPTION_HANDLER);
                }
                InstructionHandle handle = location.getHandle();
                int pc = handle.getPosition();
                IndexedInstruction ins = (IndexedInstruction) location.getHandle().getInstruction();

                int local = ins.getIndex();

                // Get live stores at this instruction.
                // Note that the analysis also computes which stores were
                // killed by a subsequent unconditional store.
                BitSet liveStoreSet = llsaDataflow.getAnalysis().getFactAtLocation(location);

                // Is store alive?
                boolean storeLive = llsaDataflow.getAnalysis().isStoreAlive(liveStoreSet, local);

                LocalVariableAnnotation lvAnnotation = LocalVariableAnnotation.getLocalVariableAnnotation(method, location, ins);

                String sourceFileName = javaClass.getSourceFileName();
                if (lvAnnotation.getName().equals("?")) {
                    if (sourceFileName.endsWith(".groovy")) {
                        continue;
                    }
                    if (method.getCode().getLocalVariableTable() != null) {
                        continue;
                    }
                }

                SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen,
                        sourceFileName, location.getHandle());

                if (DEBUG) {
                    System.out.println("    Store at " + sourceLineAnnotation.getStartLine() + "@"
                            + location.getHandle().getPosition() + " is " + (storeLive ? "live" : "dead"));
                    System.out.println("Previous is: " + location.getHandle().getPrev());
                }

                // Note source lines of live stores.
                if (storeLive && sourceLineAnnotation.getStartLine() > 0) {
                    liveStoreSourceLineSet.set(sourceLineAnnotation.getStartLine());
                }

                String lvName = lvAnnotation.getName();
                if (lvName.charAt(0) == '$' || lvName.charAt(0) == '_') {
                    propertySet.addProperty(DeadLocalStoreProperty.SYNTHETIC_NAME);
                }
                if (EXCLUDED_LOCALS.contains(lvName)) {
                    continue;
                }
                propertySet.setProperty(DeadLocalStoreProperty.LOCAL_NAME, lvName);

                boolean isParameter = local < localsThatAreParameters;
                if (isParameter) {
                    propertySet.addProperty(DeadLocalStoreProperty.IS_PARAMETER);
                }

                Field shadowedField = null;

                for (Field f : javaClass.getFields()) {
                    if (f.getName().equals(lvName) && f.isStatic() == method.isStatic()) {
                        shadowedField = f;
                        propertySet.addProperty(DeadLocalStoreProperty.SHADOWS_FIELD);
                        break;
                    }
                }

                // Is this a store to a parameter which was dead on entry to the
                // method?
                boolean parameterThatIsDeadAtEntry = isParameter
                        && !llsaDataflow.getAnalysis().isStoreAlive(liveStoreSetAtEntry, local);
                if (parameterThatIsDeadAtEntry && !complainedAbout.get(local)) {

                    int priority = storeLive ? LOW_PRIORITY : NORMAL_PRIORITY;
                    if (shadowedField != null) {
                        priority--;
                    }
                    pendingBugReportAboutOverwrittenParameter = new BugInstance(this, "IP_PARAMETER_IS_DEAD_BUT_OVERWRITTEN",
                            priority).addClassAndMethod(methodGen, sourceFileName).add(lvAnnotation);

                    if (shadowedField != null) {
                        pendingBugReportAboutOverwrittenParameter.addField(
                                FieldAnnotation.fromBCELField(classContext.getJavaClass(), shadowedField)).describe(
                                        FieldAnnotation.DID_YOU_MEAN_ROLE);
                    }

                    pendingBugReportAboutOverwrittenParameter.addSourceLine(classContext, methodGen, sourceFileName,
                            location.getHandle());
                    complainedAbout.set(local);
                }

                if (storeLive) {
                    continue;
                }

                TypeFrame typeFrame = typeDataflow.getAnalysis().getFactAtLocation(location);
                Type typeOfValue = null;
                if (typeFrame.isValid() && typeFrame.getStackDepth() > 0) {
                    typeOfValue = typeFrame.getTopValue();
                }
View Full Code Here

                    String methodName = inv.getMethodName(cpg);
                    String className = inv.getClassName(cpg);
                    if (methodName.equals("valueOf") && className.equals("java.lang.String")
                            && sig1.equals("(Ljava/lang/Object;)Ljava/lang/String;")) {
                        try {
                            TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
                            TypeFrame frame = typeDataflow.getFactAtLocation(location);
                            if (!frame.isValid()) {
                                // This basic block is probably dead
                                continue;
                            }
                            Type operandType = frame.getTopValue();
View Full Code Here

TOP

Related Classes of edu.umd.cs.findbugs.ba.type.TypeDataflow$LocationAndFactPair

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.