public void analyzeMethod() throws CheckedAnalysisException {
      if (DEBUG_METHOD != null && !methodDescriptor.getName().equals(DEBUG_METHOD)) {
        return;
      }

      if (DEBUG) {
        System.out.println("*** Analyzing method " + methodDescriptor);
      }

      xmethod = XFactory.createXMethod(methodDescriptor);
      analysisCache = Global.getAnalysisCache();

      //
      // Execute the obligation dataflow analysis
      //
      try {
        dataflow = analysisCache.getMethodAnalysis(ObligationDataflow.class, methodDescriptor);
      } catch (ObligationAcquiredOrReleasedInLoopException e) {
        // It is not possible to analyze this method.
        if (DEBUG) {
          System.out.println(
              "FindUnsatisifedObligation: " + methodDescriptor + ": " + e.getMessage());
        }
        return;
      }

      //
      // Additional analyses
      // needed these to apply the false-positive
      // suppression heuristics.
      //
      cpg =
          analysisCache.getClassAnalysis(
              ConstantPoolGen.class, methodDescriptor.getClassDescriptor());
      typeDataflow = analysisCache.getMethodAnalysis(TypeDataflow.class, methodDescriptor);
      subtypes2 = Global.getAnalysisCache().getDatabase(Subtypes2.class);

      //
      // Main loop: looking at the StateSet at the exit block of the CFG,
      // see if there are any states with nonempty obligation sets.
      //
      Map<Obligation, State> leakedObligationMap = new HashMap<Obligation, State>();
      StateSet factAtExit = dataflow.getResultFact(cfg.getExit());
      for (Iterator<State> i = factAtExit.stateIterator(); i.hasNext(); ) {
        State state = i.next();
        checkStateForLeakedObligations(state, leakedObligationMap);
      }

      //
      // Report a separate BugInstance for each Obligation,State pair.
      // (Two different obligations may be leaked in the same state.)
      //
      for (Map.Entry<Obligation, State> entry : leakedObligationMap.entrySet()) {
        Obligation obligation = entry.getKey();
        State state = entry.getValue();
        reportWarning(obligation, state, factAtExit);
      }
      // TODO: closing of nonexistent resources

    }
示例#2
0
  public boolean isTooBig(ClassDescriptor desc) {
    IAnalysisCache analysisCache = Global.getAnalysisCache();

    try {
      ClassContext classContext = analysisCache.getClassAnalysis(ClassContext.class, desc);
      ClassData classData = analysisCache.getClassAnalysis(ClassData.class, desc);
      if (classData.getData().length > 1000000) {
        return true;
      }
      try {
        JavaClass javaClass = classContext.getJavaClass();
        if (javaClass.getMethods().length > 1000) {
          return true;
        }
      } catch (RuntimeException e) {
        AnalysisContext.logError(
            "Error parsing class " + desc + " from " + classData.getCodeBaseEntry().getCodeBase(),
            e);
        return true;
      }

    } catch (RuntimeException e) {
      AnalysisContext.logError("Error getting class data for " + desc, e);
      return true;
    } catch (CheckedAnalysisException e) {
      AnalysisContext.logError("Could not get class context for " + desc, e);
      return true;
    }
    return false;
  }
  private void compute() {
    if (defined == null) {
      // System.out.println("Computing");
      defined = new HashSet<String>();

      Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();
      Collection<XClass> allClasses = subtypes2.getXClassCollection();

      IAnalysisCache analysisCache = Global.getAnalysisCache();

      for (XClass c : allClasses) {
        try {
          JavaClass jclass =
              analysisCache.getClassAnalysis(JavaClass.class, c.getClassDescriptor());
          addAllDefinitions(jclass);
        } catch (MissingClassException e) {
          bugReporter.reportMissingClass(e.getClassDescriptor());
        } catch (CheckedAnalysisException e) {
          bugReporter.logError(
              "Could not find class " + c.getClassDescriptor().toDottedClassName(), e);
        }
      }
      // System.out.println("Done Computing: " +
      // defined.contains("edu.umd.cs.findbugs.ba.IsNullValueAnalysis.UNKNOWN_VALUES_ARE_NSP : Z"));
    }
  }
  @Override
  public void visitClass(ClassDescriptor classDescriptor) throws CheckedAnalysisException {
    IAnalysisCache analysisCache = Global.getAnalysisCache();

    ObligationFactory factory = database.getFactory();

    JavaClass jclass = analysisCache.getClassAnalysis(JavaClass.class, classDescriptor);
    for (Constant c : jclass.getConstantPool().getConstantPool()) {
      if (c instanceof ConstantNameAndType) {
        ConstantNameAndType cnt = (ConstantNameAndType) c;
        String signature = cnt.getSignature(jclass.getConstantPool());
        if (factory.signatureInvolvesObligations(signature)) {
          super.visitClass(classDescriptor);
          return;
        }
      } else if (c instanceof ConstantClass) {
        String className = ((ConstantClass) c).getBytes(jclass.getConstantPool());
        if (factory.signatureInvolvesObligations(className)) {
          super.visitClass(classDescriptor);
          return;
        }
      }
    }
    if (DEBUG) System.out.println(classDescriptor + " isn't interesting for obligation analysis");
  }
  /* (non-Javadoc)
   * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#analyze(edu.umd.cs.findbugs.classfile.IAnalysisCache, java.lang.Object)
   */
  public Method analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor)
      throws CheckedAnalysisException {
    JavaClass jclass =
        analysisCache.getClassAnalysis(JavaClass.class, descriptor.getClassDescriptor());
    Method[] methodList = jclass.getMethods();

    Method result = null;

    // As a side-effect, cache all of the Methods for this JavaClass
    for (Method method : methodList) {
      MethodDescriptor methodDescriptor =
          DescriptorFactory.instance()
              .getMethodDescriptor(
                  descriptor.getSlashedClassName(),
                  method.getName(),
                  method.getSignature(),
                  method.isStatic());

      // Put in cache eagerly
      analysisCache.eagerlyPutMethodAnalysis(Method.class, methodDescriptor, method);

      if (methodDescriptor.equals(descriptor)) {
        result = method;
      }
    }

    return result;
  }
示例#6
0
  public int getClassSize(ClassDescriptor desc) {
    IAnalysisCache analysisCache = Global.getAnalysisCache();

    try {
      /* ClassContext classContext =*/ analysisCache.getClassAnalysis(ClassContext.class, desc);
      ClassData classData = analysisCache.getClassAnalysis(ClassData.class, desc);
      return classData.getData().length;

    } catch (RuntimeException e) {
      AnalysisContext.logError("Error getting class data for " + desc, e);
      return 10000;
    } catch (CheckedAnalysisException e) {
      AnalysisContext.logError("Could not get class context for " + desc, e);
      return 10000;
    }
  }
  public When validate(Object constantValue) {
    if (validator == null) throw new IllegalStateException("No validator");
    IAnalysisCache analysisCache = Global.getAnalysisCache();
    Profiler profiler = analysisCache.getProfiler();
    profiler.start(validator.getClass());
    AtomicBoolean performing = performingValidation.get();
    try {
      if (!performing.compareAndSet(false, true)) {
        throw new IllegalStateException("recursive validation");
      }

      return validator.forConstantValue(proxy, constantValue);
    } catch (Exception e) {
      AnalysisContext.logError(
          "Error executing custom validator for " + typeQualifier + " " + constantValue, e);
      return When.UNKNOWN;
    } finally {
      if (!performing.compareAndSet(true, false)) {
        throw new IllegalStateException("performingValidation not set when validation completes");
      }
      profiler.end(validator.getClass());
    }
  }
 /* (non-Javadoc)
  * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#registerWith(edu.umd.cs.findbugs.classfile.IAnalysisCache)
  */
 public void registerWith(IAnalysisCache analysisCache) {
   analysisCache.registerClassAnalysisEngine(ConstantPoolGen.class, this);
 }
 /* (non-Javadoc)
  * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#analyze(edu.umd.cs.findbugs.classfile.IAnalysisCache, java.lang.Object)
  */
 public ConstantPoolGen analyze(IAnalysisCache analysisCache, ClassDescriptor descriptor)
     throws CheckedAnalysisException {
   ClassGen classGen = new ClassGen(analysisCache.getClassAnalysis(JavaClass.class, descriptor));
   return classGen.getConstantPool();
 }
  public FindUnsatisfiedObligation(BugReporter bugReporter) {
    this.bugReporter = bugReporter;
    IAnalysisCache analysisCache = Global.getAnalysisCache();

    database = analysisCache.getDatabase(ObligationPolicyDatabase.class);
  }
 /* (non-Javadoc)
  * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#registerWith(edu.umd.cs.findbugs.classfile.IAnalysisCache)
  */
 @Override
 public void registerWith(IAnalysisCache analysisCache) {
   analysisCache.registerMethodAnalysisEngine(Method.class, this);
 }