コード例 #1
0
  /**
   * If this is a putfield or putstatic instruction, check to see if the field is @NonNull, and
   * treat it as dereferences.
   *
   * @param location the Location of the instruction
   * @param vnaFrame the ValueNumberFrame at the Location of the instruction
   * @param fact the dataflow value to modify
   * @throws DataflowAnalysisException
   */
  private void checkNonNullPutField(
      Location location, ValueNumberFrame vnaFrame, UnconditionalValueDerefSet fact)
      throws DataflowAnalysisException {
    INullnessAnnotationDatabase database =
        AnalysisContext.currentAnalysisContext().getNullnessAnnotationDatabase();

    FieldInstruction fieldIns = (FieldInstruction) location.getHandle().getInstruction();

    XField field = XFactory.createXField(fieldIns, methodGen.getConstantPool());
    char firstChar = field.getSignature().charAt(0);
    if (firstChar != 'L' && firstChar != '[') {
      return;
    }
    NullnessAnnotation resolvedAnnotation = database.getResolvedAnnotation(field, true);
    if (resolvedAnnotation == NullnessAnnotation.NONNULL) {
      IsNullValueFrame invFrame = invDataflow.getFactAtLocation(location);
      if (!invFrame.isValid()) {
        return;
      }
      IsNullValue value = invFrame.getTopValue();
      if (reportDereference(value)) {
        ValueNumber vn = vnaFrame.getTopValue();
        fact.addDeref(vn, location);
      }
    }
  }
コード例 #2
0
  public static Set<ValueNumber> checkNonNullParams(
      Location location,
      ValueNumberFrame vnaFrame,
      ConstantPoolGen constantPool,
      @CheckForNull Method method,
      @CheckForNull IsNullValueFrame invFrame)
      throws DataflowAnalysisException {

    if (invFrame != null && !invFrame.isValid()) {
      return Collections.emptySet();
    }
    INullnessAnnotationDatabase database =
        AnalysisContext.currentAnalysisContext().getNullnessAnnotationDatabase();

    InvokeInstruction inv = (InvokeInstruction) location.getHandle().getInstruction();
    XMethod called = XFactory.createXMethod(inv, constantPool);
    SignatureParser sigParser = new SignatureParser(called.getSignature());
    int numParams = sigParser.getNumParameters();

    Set<ValueNumber> result = new HashSet<ValueNumber>();
    Iterator<String> parameterIterator = sigParser.parameterSignatureIterator();
    for (int i = 0; i < numParams; i++) {
      String parameterSignature = parameterIterator.next();
      char firstChar = parameterSignature.charAt(0);
      if (firstChar != 'L' && firstChar != '[') {
        continue;
      }
      int offset = sigParser.getSlotsFromTopOfStackForParameter(i);
      if (invFrame != null) {
        int slot = invFrame.getStackLocation(offset);
        if (!reportDereference(invFrame, slot)) {
          continue;
        }
      }
      if (database.parameterMustBeNonNull(called, i)) {
        int catchSizeNPE =
            Util.getSizeOfSurroundingTryBlock(
                method, "java/lang/NullPointerException", location.getHandle().getPosition());
        int catchSizeNFE =
            Util.getSizeOfSurroundingTryBlock(
                method, "java/lang/NumberFormatException", location.getHandle().getPosition());
        if (catchSizeNPE == Integer.MAX_VALUE
            && (!"java.lang.Integer".equals(called.getClassName())
                || catchSizeNFE == Integer.MAX_VALUE)) {
          // Get the corresponding value number
          ValueNumber vn = vnaFrame.getArgument(inv, constantPool, i, sigParser);
          result.add(vn);
        }
      }
    }
    return result;
  }
コード例 #3
0
 public static boolean reportPotentialDereference(Location location, IsNullValueFrame invFrame)
     throws DataflowAnalysisException {
   if (!invFrame.isValid()) {
     return false;
   }
   IsNullValue value = invFrame.getTopValue();
   if (value.isDefinitelyNotNull()) {
     return false;
   }
   if (value.isDefinitelyNull()) {
     return false;
   }
   return true;
 }
コード例 #4
0
  /**
   * Clear deref sets of values if this edge is the non-null branch of an if comparison.
   *
   * @param fact a datflow fact
   * @param edge edge to check
   * @return possibly-modified dataflow fact
   */
  private @CheckForNull ValueNumber findValueKnownNonnullOnBranch(
      UnconditionalValueDerefSet fact, Edge edge) {

    IsNullValueFrame invFrame = invDataflow.getResultFact(edge.getSource());
    if (!invFrame.isValid()) {
      return null;
    }
    IsNullConditionDecision decision = invFrame.getDecision();
    if (decision == null) {
      return null;
    }

    IsNullValue inv = decision.getDecision(edge.getType());
    if (inv == null || !inv.isDefinitelyNotNull()) {
      return null;
    }
    ValueNumber value = decision.getValue();
    if (DEBUG) {
      System.out.println("Value number " + value + " is known nonnull on " + edge);
    }

    return value;
  }
コード例 #5
0
 private static boolean reportDereference(IsNullValueFrame invFrameAtNullCheck, int instance) {
   return reportDereference(invFrameAtNullCheck.getValue(instance));
 }
コード例 #6
0
  /**
   * Check to see if the instruction has a null check associated with it, and if so, add a
   * dereference.
   *
   * @param location the Location of the instruction
   * @param vnaFrame ValueNumberFrame at the Location of the instruction
   * @param fact the dataflow value to modify
   * @throws DataflowAnalysisException
   */
  private void checkInstance(
      Location location, ValueNumberFrame vnaFrame, UnconditionalValueDerefSet fact)
      throws DataflowAnalysisException {
    // See if this instruction has a null check.
    // If it does, the fall through predecessor will be
    // identify itself as the null check.
    if (!location.isFirstInstructionInBasicBlock()) {
      return;
    }
    if (invDataflow == null) {
      return;
    }
    BasicBlock fallThroughPredecessor =
        cfg.getPredecessorWithEdgeType(location.getBasicBlock(), EdgeTypes.FALL_THROUGH_EDGE);
    if (fallThroughPredecessor == null || !fallThroughPredecessor.isNullCheck()) {
      return;
    }

    // Get the null-checked value
    ValueNumber vn =
        vnaFrame.getInstance(location.getHandle().getInstruction(), methodGen.getConstantPool());

    // Ignore dereferences of this
    if (!methodGen.isStatic()) {
      ValueNumber v = vnaFrame.getValue(0);
      if (v.equals(vn)) {
        return;
      }
    }
    if (vn.hasFlag(ValueNumber.CONSTANT_CLASS_OBJECT)) {
      return;
    }

    IsNullValueFrame startFact = null;

    startFact = invDataflow.getStartFact(fallThroughPredecessor);

    if (!startFact.isValid()) {
      return;
    }

    int slot =
        startFact.getInstanceSlot(
            location.getHandle().getInstruction(), methodGen.getConstantPool());
    if (!reportDereference(startFact, slot)) {
      return;
    }
    if (DEBUG) {
      System.out.println("FOUND GUARANTEED DEREFERENCE");
      System.out.println("Load: " + vnaFrame.getLoad(vn));
      System.out.println("Pred: " + fallThroughPredecessor);
      System.out.println("startFact: " + startFact);
      System.out.println("Location: " + location);
      System.out.println("Value number frame: " + vnaFrame);
      System.out.println("Dereferenced valueNumber: " + vn);
      System.out.println("invDataflow: " + startFact);
      System.out.println("IGNORE_DEREF_OF_NCP: " + IGNORE_DEREF_OF_NCP);
    }
    // Mark the value number as being dereferenced at this location
    fact.addDeref(vn, location);
  }
コード例 #7
0
  public static Set<ValueNumber> checkUnconditionalDerefDatabase(
      Location location,
      ValueNumberFrame vnaFrame,
      ConstantPoolGen constantPool,
      @CheckForNull IsNullValueFrame invFrame,
      TypeDataflow typeDataflow)
      throws DataflowAnalysisException {
    if (invFrame != null && !invFrame.isValid()) {
      return Collections.emptySet();
    }

    InvokeInstruction inv = (InvokeInstruction) location.getHandle().getInstruction();

    SignatureParser sigParser = new SignatureParser(inv.getSignature(constantPool));
    int numParams = sigParser.getNumParameters();
    if (numParams == 0 || !sigParser.hasReferenceParameters()) {
      return Collections.emptySet();
    }
    ParameterNullnessPropertyDatabase database =
        AnalysisContext.currentAnalysisContext().getUnconditionalDerefParamDatabase();
    if (database == null) {
      if (DEBUG_CHECK_CALLS) {
        System.out.println("no database!");
      }
      return Collections.emptySet();
    }

    TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
    if (!typeFrame.isValid()) {
      if (DEBUG_CHECK_CALLS) {
        System.out.println("invalid type frame!");
      }
      return Collections.emptySet();
    }

    try {
      Set<XMethod> targetSet = Hierarchy2.resolveMethodCallTargets(inv, typeFrame, constantPool);

      if (targetSet.isEmpty()) {
        return Collections.emptySet();
      }

      if (DEBUG_CHECK_CALLS) {
        System.out.println("target set size: " + targetSet.size());
      }
      // Compute the intersection of all properties
      ParameterProperty derefParamSet = null;
      for (XMethod target : targetSet) {
        if (target.isStub()) {
          continue;
        }
        if (DEBUG_CHECK_CALLS) {
          System.out.print("Checking: " + target + ": ");
        }

        ParameterProperty targetDerefParamSet = database.getProperty(target.getMethodDescriptor());
        if (targetDerefParamSet == null) {
          // Hmm...no information for this target.
          // assume it doesn't dereference anything
          if (DEBUG_CHECK_CALLS) {
            System.out.println("==> no information, assume no guaranteed dereferences");
          }
          return Collections.emptySet();
        }

        if (DEBUG_CHECK_CALLS) {
          System.out.println("==> " + targetDerefParamSet);
        }
        if (derefParamSet == null) {
          derefParamSet = new ParameterProperty();
          derefParamSet.copyFrom(targetDerefParamSet);
        } else {
          derefParamSet.intersectWith(targetDerefParamSet);
        }
      }

      if (derefParamSet == null || derefParamSet.isEmpty()) {
        if (DEBUG) {
          System.out.println("** Nothing");
        }
        return Collections.emptySet();
      }
      if (DEBUG_CHECK_CALLS) {
        System.out.println(
            "** Summary of call @ " + location.getHandle().getPosition() + ": " + derefParamSet);
      }

      HashSet<ValueNumber> requiredToBeNonnull = new HashSet<ValueNumber>();
      for (int i = 0; i < numParams; i++) {
        if (!derefParamSet.hasProperty(i)) {
          continue;
        }
        int argSlot = vnaFrame.getStackLocation(sigParser.getSlotsFromTopOfStackForParameter(i));
        if (invFrame != null && !reportDereference(invFrame, argSlot)) {
          continue;
        }
        if (DEBUG_CHECK_CALLS) {
          System.out.println(
              "  dereference @ " + location.getHandle().getPosition() + " of parameter " + i);
        }

        requiredToBeNonnull.add(vnaFrame.getValue(argSlot));
      }
      return requiredToBeNonnull;

    } catch (ClassNotFoundException e) {
      AnalysisContext.reportMissingClass(e);
    }
    return Collections.emptySet();
  }