Exemple #1
0
  @Override
  public IFigure getTooltip(Object element) {
    if (element instanceof BasicBlock) {
      BasicBlock bb = (BasicBlock) element;
      IR ir = irView.getIR();
      IDocument doc = irView.getDocument();
      IMethod method = ir.getMethod();

      StringBuffer result = new StringBuffer();

      int start = bb.getFirstInstructionIndex();
      int end = bb.getLastInstructionIndex();
      SSAInstruction[] instructions = ir.getInstructions();
      for (int j = start; j <= end; j++) {
        if (instructions[j] != null) {
          int sourceLineNum = method.getLineNumber(j);
          int lineNumber = sourceLineNum - 1; // IDocument indexing is 0-based
          try {
            int lineOffset = doc.getLineOffset(lineNumber);
            int lineLength = doc.getLineLength(lineNumber);
            String sourceCode = doc.get(lineOffset, lineLength).trim();
            result.append(sourceCode);
          } catch (BadLocationException e) {
          }
          result.append("\n");
        }
      }
      return new Label(result.toString());
    }
    return null;
  }
 private void addMatchHelper(LocalPointerKey pk, Collection<MemoryAccess> writes) {
   for (MemoryAccess a : writes) {
     addSubgraphForNode(a.getNode());
   }
   for (MemoryAccess a : writes) {
     IR ir = a.getNode().getIR();
     SSAPutInstruction s = (SSAPutInstruction) ir.getInstructions()[a.getInstructionIndex()];
     PointerKey r = heapModel.getPointerKeyForLocal(a.getNode(), s.getVal());
     assert containsNode(r);
     assert containsNode(pk);
     addMatchEdge(pk, r);
   }
 }
 /**
  * Compute the set of PointerKeys each statement refs.Be careful to avoid eager PDG construction
  * here! That means .. don't iterate over SDG statements!
  */
 public static Map<Statement, Set<PointerKey>> scanForRef(
     SDG sdg, PointerAnalysis pa, ModRef modRef) {
   if (pa == null) {
     throw new IllegalArgumentException("null pa");
   }
   ExtendedHeapModel h = new DelegatingExtendedHeapModel(pa.getHeapModel());
   Map<Statement, Set<PointerKey>> result = HashMapFactory.make();
   for (CGNode n : sdg.getCallGraph()) {
     IR ir = n.getIR();
     if (ir != null) {
       for (int i = 0; i < ir.getInstructions().length; i++) {
         SSAInstruction st = ir.getInstructions()[i];
         if (st != null) {
           Set<PointerKey> mod = modRef.getRef(n, h, pa, st, null);
           if (!mod.isEmpty()) {
             NormalStatement normal = new NormalStatement(n, i);
             result.put(normal, mod);
           }
         }
       }
     }
   }
   return result;
 }
 private void addArrayMatchEdges(LocalPointerKey pk) {
   Collection<MemoryAccess> arrayWrites = fam.getArrayWrites(null);
   for (MemoryAccess a : arrayWrites) {
     addSubgraphForNode(a.getNode());
   }
   for (MemoryAccess a : arrayWrites) {
     IR ir = a.getNode().getIR();
     SSAArrayStoreInstruction s =
         (SSAArrayStoreInstruction) ir.getInstructions()[a.getInstructionIndex()];
     PointerKey r = heapModel.getPointerKeyForLocal(a.getNode(), s.getValue());
     assert containsNode(r);
     assert containsNode(pk);
     addMatchEdge(pk, r);
   }
 }
  private static List<Pair<CGNode, SSACheckCastInstruction>> findFailingCasts(
      CallGraph cg, DemandRefinementPointsTo dmp) {
    final IClassHierarchy cha = dmp.getClassHierarchy();
    List<Pair<CGNode, SSACheckCastInstruction>> failing =
        new ArrayList<Pair<CGNode, SSACheckCastInstruction>>();

    int numSafe = 0, numMightFail = 0;
    outer:
    for (Iterator<? extends CGNode> nodeIter = cg.iterator(); nodeIter.hasNext(); ) {
      CGNode node = nodeIter.next();
      TypeReference declaringClass = node.getMethod().getReference().getDeclaringClass();
      // skip library classes
      if (declaringClass.getClassLoader().equals(ClassLoaderReference.Primordial)) {
        continue;
      }
      IR ir = node.getIR();
      if (ir == null) continue;
      SSAInstruction[] instrs = ir.getInstructions();
      for (int i = 0; i < instrs.length; i++) {
        if (numSafe + numMightFail > MAX_CASTS) break outer;
        SSAInstruction instruction = instrs[i];
        if (instruction instanceof SSACheckCastInstruction) {
          SSACheckCastInstruction castInstr = (SSACheckCastInstruction) instruction;
          final TypeReference[] declaredResultTypes = castInstr.getDeclaredResultTypes();

          boolean primOnly = true;
          for (TypeReference t : declaredResultTypes) {
            if (!t.isPrimitiveType()) {
              primOnly = false;
            }
          }
          if (primOnly) {
            continue;
          }

          System.err.println("CHECKING " + castInstr + " in " + node.getMethod());
          PointerKey castedPk = heapModel.getPointerKeyForLocal(node, castInstr.getUse(0));
          Predicate<InstanceKey> castPred =
              new Predicate<InstanceKey>() {

                @Override
                public boolean test(InstanceKey ik) {
                  TypeReference ikTypeRef = ik.getConcreteType().getReference();
                  for (TypeReference t : declaredResultTypes) {
                    if (cha.isAssignableFrom(cha.lookupClass(t), cha.lookupClass(ikTypeRef))) {
                      return true;
                    }
                  }
                  return false;
                }
              };
          long startTime = System.currentTimeMillis();
          Pair<PointsToResult, Collection<InstanceKey>> queryResult =
              dmp.getPointsTo(castedPk, castPred);
          long runningTime = System.currentTimeMillis() - startTime;
          System.err.println("running time: " + runningTime + "ms");
          final FieldRefinePolicy fieldRefinePolicy =
              dmp.getRefinementPolicy().getFieldRefinePolicy();
          switch (queryResult.fst) {
            case SUCCESS:
              System.err.println("SAFE: " + castInstr + " in " + node.getMethod());
              if (fieldRefinePolicy instanceof ManualFieldPolicy) {
                ManualFieldPolicy hackedFieldPolicy = (ManualFieldPolicy) fieldRefinePolicy;
                System.err.println(hackedFieldPolicy.getHistory());
              }
              System.err.println("TRAVERSED " + dmp.getNumNodesTraversed() + " nodes");
              numSafe++;
              break;
            case NOMOREREFINE:
              if (queryResult.snd != null) {
                System.err.println(
                    "MIGHT FAIL: no more refinement possible for "
                        + castInstr
                        + " in "
                        + node.getMethod());
              } else {
                System.err.println(
                    "MIGHT FAIL: exceeded budget for " + castInstr + " in " + node.getMethod());
              }
              failing.add(Pair.make(node, castInstr));
              numMightFail++;
              break;
            case BUDGETEXCEEDED:
              System.err.println(
                  "MIGHT FAIL: exceeded budget for " + castInstr + " in " + node.getMethod());
              failing.add(Pair.make(node, castInstr));
              numMightFail++;
              break;
            default:
              Assertions.UNREACHABLE();
          }
        }
      }
      // break outer;
    }
    System.err.println("TOTAL SAFE: " + numSafe);
    System.err.println("TOTAL MIGHT FAIL: " + numMightFail);
    return failing;
  }
Exemple #6
0
  /** Taken from com.ibm.wala.viz.PDFViewUtil */
  @Override
  public String getText(Object element) {
    if (element instanceof BasicBlock) {
      BasicBlock bb = (BasicBlock) element;
      IR ir = irView.getIR();

      StringBuilder result = new StringBuilder();

      int start = bb.getFirstInstructionIndex();
      int end = bb.getLastInstructionIndex();

      result.append("BB").append(bb.getNumber());

      if (bb.isEntryBlock()) {
        result.append(" (en)\n");
      } else if (bb.isExitBlock()) {
        result.append(" (ex)\n");
      }
      if (bb instanceof ExceptionHandlerBasicBlock) {
        result.append("<Handler>");
      }
      result.append("\n");

      for (Iterator<SSAPhiInstruction> it = bb.iteratePhis(); it.hasNext(); ) {
        SSAPhiInstruction phi = it.next();
        if (phi != null) {
          result.append(phi.toString(ir.getSymbolTable())).append("\n");
        }
      }
      if (bb instanceof ExceptionHandlerBasicBlock) {
        ExceptionHandlerBasicBlock ebb = (ExceptionHandlerBasicBlock) bb;
        SSAGetCaughtExceptionInstruction s = ebb.getCatchInstruction();
        if (s != null) {
          result.append(s.toString(ir.getSymbolTable())).append("\n");
        } else {
          result.append(" No catch instruction. Unreachable?\n");
        }
      }

      SSAInstruction[] instructions = ir.getInstructions();
      IMethod method = ir.getMethod();
      for (int j = start; j <= end; j++) {
        if (instructions[j] != null) {
          String x;
          int sourceLineNum = method.getLineNumber(j);
          x =
              String.format(
                  j + " [L%03d] " + instructions[j].toString(ir.getSymbolTable()), sourceLineNum);
          String padded = String.format("%1$-35s", x);
          result.append(padded);
          result.append("\n");
          result.append(SSAValuesToLocalVariables(instructions[j], j, ir));
          result.append("\n");
        }
      }
      for (Iterator<SSAPiInstruction> it = bb.iteratePis(); it.hasNext(); ) {
        SSAPiInstruction pi = it.next();
        if (pi != null) {
          result.append("           " + pi.toString(ir.getSymbolTable())).append("\n");
        }
      }
      return result.toString();
    }
    if (element instanceof EntityConnectionData) {
      return "\n";
    }
    return "";
  }