/**
  * @param method
  * @return the receiver class for this method.
  */
 private IClass getReceiverClass(IMethod method) {
   TypeReference formalType = method.getParameterType(0);
   IClass C = cha.lookupClass(formalType);
   if (method.isStatic()) {
     Assertions.UNREACHABLE("asked for receiver of static method " + method);
   }
   if (C == null) {
     Assertions.UNREACHABLE("no class found for " + formalType + " recv of " + method);
   }
   return C;
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#isCall(java.lang.Object)
  */
 public boolean isCall(Statement n) {
   switch (n.getKind()) {
     case EXC_RET_CALLEE:
     case EXC_RET_CALLER:
     case HEAP_PARAM_CALLEE:
     case NORMAL_RET_CALLEE:
     case NORMAL_RET_CALLER:
     case PARAM_CALLEE:
     case PHI:
     case HEAP_RET_CALLEE:
     case HEAP_RET_CALLER:
     case METHOD_ENTRY:
     case METHOD_EXIT:
     case CATCH:
     case PI:
       return false;
     case HEAP_PARAM_CALLER:
     case PARAM_CALLER:
       return true;
     case NORMAL:
       if (sdg.getCOptions().equals(ControlDependenceOptions.NONE)) {
         return false;
       } else {
         NormalStatement s = (NormalStatement) n;
         return s.getInstruction() instanceof SSAAbstractInvokeInstruction;
       }
     default:
       Assertions.UNREACHABLE(n.getKind() + " " + n.toString());
       return false;
   }
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#getReturnSites(java.lang.Object)
  */
 public Iterator<? extends Statement> getReturnSites(Statement call, PDG callee) {
   switch (call.getKind()) {
     case PARAM_CALLER:
       {
         ParamCaller n = (ParamCaller) call;
         SSAAbstractInvokeInstruction st = n.getInstruction();
         PDG pdg = getProcOf(call);
         return pdg.getCallerReturnStatements(st).iterator();
       }
     case HEAP_PARAM_CALLER:
       {
         HeapStatement.HeapParamCaller n = (HeapStatement.HeapParamCaller) call;
         SSAAbstractInvokeInstruction st = n.getCall();
         PDG pdg = getProcOf(call);
         return pdg.getCallerReturnStatements(st).iterator();
       }
     case NORMAL:
       {
         NormalStatement n = (NormalStatement) call;
         SSAAbstractInvokeInstruction st = (SSAAbstractInvokeInstruction) n.getInstruction();
         PDG pdg = getProcOf(call);
         return pdg.getCallerReturnStatements(st).iterator();
       }
     default:
       Assertions.UNREACHABLE(call.getKind().toString());
       return null;
   }
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#getCallSites(java.lang.Object)
  */
 public Iterator<? extends Statement> getCallSites(Statement r, PDG callee) {
   switch (r.getKind()) {
     case EXC_RET_CALLER:
       {
         ExceptionalReturnCaller n = (ExceptionalReturnCaller) r;
         SSAAbstractInvokeInstruction call = n.getInstruction();
         PDG pdg = getProcOf(r);
         return pdg.getCallStatements(call).iterator();
       }
     case NORMAL_RET_CALLER:
       {
         NormalReturnCaller n = (NormalReturnCaller) r;
         SSAAbstractInvokeInstruction call = n.getInstruction();
         PDG pdg = getProcOf(r);
         return pdg.getCallStatements(call).iterator();
       }
     case HEAP_RET_CALLER:
       {
         HeapStatement.HeapReturnCaller n = (HeapStatement.HeapReturnCaller) r;
         SSAAbstractInvokeInstruction call = n.getCall();
         PDG pdg = getProcOf(r);
         return pdg.getCallStatements(call).iterator();
       }
     default:
       Assertions.UNREACHABLE(r.getKind().toString());
       return null;
   }
 }
 public boolean isExit(Statement n) {
   switch (n.getKind()) {
     case PARAM_CALLEE:
     case HEAP_PARAM_CALLEE:
     case HEAP_PARAM_CALLER:
     case PHI:
     case PI:
     case NORMAL_RET_CALLER:
     case PARAM_CALLER:
     case HEAP_RET_CALLER:
     case NORMAL:
     case EXC_RET_CALLER:
     case METHOD_ENTRY:
     case CATCH:
       return false;
     case HEAP_RET_CALLEE:
     case EXC_RET_CALLEE:
     case NORMAL_RET_CALLEE:
     case METHOD_EXIT:
       return true;
     default:
       Assertions.UNREACHABLE(n.toString());
       return false;
   }
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#getProcOf(java.lang.Object)
  */
 public PDG getProcOf(Statement n) {
   CGNode node = n.getNode();
   PDG result = sdg.getPDG(node);
   if (result == null) {
     Assertions.UNREACHABLE("panic: " + n + " " + node);
   }
   return result;
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#getNormalSuccessors(java.lang.Object)
  */
 public Iterator<Statement> getNormalSuccessors(Statement call) {
   if (!backward) {
     return EmptyIterator.instance();
   } else {
     Assertions.UNREACHABLE();
     return null;
   }
 }
 /**
  * @param type
  * @return a TypeAbstraction object representing this type. We just use ConeTypes by default,
  *     since we don't propagate information allowing us to distinguish between points and cones
  *     yet.
  */
 protected TypeAbstraction typeRef2TypeAbstraction(IClassHierarchy cha, TypeReference type) {
   IClass klass = cha.lookupClass(type);
   if (klass != null) {
     return new ConeType(klass);
   }
   Assertions.UNREACHABLE(type.toString());
   return null;
 }
 static {
   try {
     p = WalaProperties.loadProperties();
     p.putAll(WalaExamplesProperties.loadProperties());
   } catch (WalaException e) {
     e.printStackTrace();
     Assertions.UNREACHABLE();
   }
 }
 /*
  * @see com.ibm.capa.util.graph.AbstractGraph#getSuccNodeCount(java.lang.Object)
  */
 @Override
 public int getSuccNodeCount(Object N) throws UnimplementedError {
   if (N instanceof StaticFieldKey) {
     Assertions.UNREACHABLE();
     return -1;
   } else {
     return super.getSuccNodeCount(N);
   }
 }
 /*
  * @see com.ibm.capa.util.graph.AbstractNumberedGraph#getPredNodeNumbers(java.lang.Object)
  */
 @Override
 public IntSet getPredNodeNumbers(Object node) throws UnimplementedError {
   if (node instanceof StaticFieldKey) {
     Assertions.UNREACHABLE();
     return null;
   } else {
     return super.getPredNodeNumbers(node);
   }
 }
Beispiel #12
0
 public IInstruction[] getInstructions() {
   try {
     return method.getInstructions();
   } catch (InvalidClassFileException e) {
     e.printStackTrace();
     Assertions.UNREACHABLE();
     return null;
   }
 }
 /*
  * (non-Javadoc)
  *
  * @see com.ibm.domo.ssa.SSAInstruction.Visitor#visitPi(com.ibm.domo.ssa.SSAPiInstruction)
  */
 @Override
 public void visitPi(SSAPiInstruction instruction) {
   Assertions.UNREACHABLE();
   // int dir;
   // ControlFlowGraph CFG = ir.getControlFlowGraph();
   // PointerKey src = getPointerKeyForLocal(node, instruction.getVal());
   // if (hasNoInterestingUses(instruction.getDef(), du)) {
   // PointerKey dst = getPointerKeyForLocal(node, instruction.getDef());
   // system.recordImplicitPointsToSet(dst);
   // } else {
   // if (com.ibm.domo.cfg.Util.endsWithConditionalBranch(CFG,
   // getBasicBlock()) && CFG.getSuccNodeCount(getBasicBlock()) == 2) {
   // SSAConditionalBranchInstruction cond =
   // com.ibm.domo.cfg.Util.getConditionalBranch(CFG, getBasicBlock());
   // SSAInstruction cause = instruction.getCause();
   // BasicBlock target = (BasicBlock)
   // CFG.getNode(instruction.getSuccessor());
   // if ((cause instanceof SSAInstanceofInstruction) && ((dir =
   // booleanConstantTest(cond, cause.getDef())) != 0)) {
   // TypeReference type = ((SSAInstanceofInstruction)
   // cause).getCheckedType();
   // IClass cls = cha.lookupClass(type);
   // if (cls == null) {
   // getWarnings().add(ResolutionFailure.create(node, type));
   // PointerKey dst = getPointerKeyForLocal(node, instruction.getDef());
   // system.newConstraint(dst, assignOperator, src);
   // } else {
   // PointerKey dst = getFilteredPointerKeyForLocal(node,
   // instruction.getDef(), cls);
   // if ((target == com.ibm.domo.cfg.Util.getTrueSuccessor(CFG,
   // getBasicBlock()) && dir == 1)
   // || (target == com.ibm.domo.cfg.Util.getFalseSuccessor(CFG,
   // getBasicBlock()) && dir == -1)) {
   // system.newConstraint(dst, filterOperator, src);
   // // System.err.println("PI " + dst + " " + src);
   // } else {
   // system.newConstraint(dst, inverseFilterOperator, src);
   // }
   // }
   // } else if ((dir = nullConstantTest(cond, instruction.getVal())) != 0) {
   // if ((target == com.ibm.domo.cfg.Util.getTrueSuccessor(CFG,
   // getBasicBlock()) && dir == -1)
   // || (target == com.ibm.domo.cfg.Util.getFalseSuccessor(CFG,
   // getBasicBlock()) && dir == 1)) {
   // PointerKey dst = getPointerKeyForLocal(node, instruction.getDef());
   // system.newConstraint(dst, assignOperator, src);
   // }
   // } else {
   // PointerKey dst = getPointerKeyForLocal(node, instruction.getDef());
   // system.newConstraint(dst, assignOperator, src);
   // }
   // } else {
   // PointerKey dst = getPointerKeyForLocal(node, instruction.getDef());
   // system.newConstraint(dst, assignOperator, src);
   // }
   // }
 }
Beispiel #14
0
 /*
  * @see com.ibm.wala.cfg.ControlFlowGraph#getProgramCounter(int)
  */
 public int getProgramCounter(int index) {
   try {
     return method.getBytecodeIndex(index);
   } catch (InvalidClassFileException e) {
     e.printStackTrace();
     Assertions.UNREACHABLE();
     return -1;
   }
 }
Beispiel #15
0
 @Override
 public InputStream getInputStream() {
   try {
     return url.openConnection().getInputStream();
   } catch (IOException e) {
     Assertions.UNREACHABLE();
     return null;
   }
 }
Beispiel #16
0
 protected void declareFunction(CAstEntity N, WalkContext context) {
   String fnName = composeEntityName(context, N);
   if (N.getKind() == CAstEntity.SCRIPT_ENTITY) {
     ((JavaScriptLoader) loader).defineScriptType("L" + fnName, N.getPosition(), N, context);
   } else if (N.getKind() == CAstEntity.FUNCTION_ENTITY) {
     ((JavaScriptLoader) loader).defineFunctionType("L" + fnName, N.getPosition(), N, context);
   } else {
     Assertions.UNREACHABLE();
   }
 }
Beispiel #17
0
 /*
  * @see com.ibm.wala.classLoader.IClass#getDeclaredMethods()
  */
 @Override
 public Collection<IMethod> getDeclaredMethods() {
   try {
     computeMethodMapIfNeeded();
   } catch (InvalidClassFileException e) {
     e.printStackTrace();
     Assertions.UNREACHABLE();
   }
   return Collections.unmodifiableCollection(methodMap.values());
 }
Beispiel #18
0
 @Override
 public String getName() {
   try {
     URLConnection con = url.openConnection();
     if (con instanceof JarURLConnection) return ((JarURLConnection) con).getEntryName();
     else return (new FileProvider()).filePathFromURL(url);
   } catch (IOException e) {
     Assertions.UNREACHABLE();
     return null;
   }
 }
Beispiel #19
0
 private ExceptionHandler[] getExceptionHandlers() {
   ExceptionHandler[][] handlers;
   try {
     handlers = method.getHandlers();
   } catch (InvalidClassFileException e) {
     e.printStackTrace();
     Assertions.UNREACHABLE();
     handlers = null;
   }
   ExceptionHandler[] hs = handlers[getLastInstructionIndex()];
   return hs;
 }
Beispiel #20
0
  /*
   * @see com.ibm.wala.classLoader.IClass#getMethod(com.ibm.wala.types.Selector)
   */
  @Override
  public IMethod getMethod(Selector selector) {
    try {
      computeMethodMapIfNeeded();
    } catch (InvalidClassFileException e1) {
      e1.printStackTrace();
      Assertions.UNREACHABLE();
    }

    // my methods + cached parent stuff
    IMethod result = methodMap.get(selector);
    if (result != null) {
      return result;
    }
    if (inheritCache != null) {
      result = inheritCache.get(selector);
      if (result != null) {
        return result;
      }
    }

    // check parent, caching if found
    if (!selector.equals(MethodReference.clinitSelector)
        && !selector.equals(MethodReference.initSelector)) {
      IClass superclass = getSuperclass();
      if (superclass != null) {
        IMethod inherit = superclass.getMethod(selector);
        if (inherit != null) {
          if (inheritCache == null) {
            inheritCache = new BimodalMap<Selector, IMethod>(5);
          }
          inheritCache.put(selector, inherit);
          return inherit;
        }
      }
    }

    // didn't find it yet. special logic for interfaces
    if (isInterface() || isAbstract()) {
      final Iterator<IClass> it = getAllImplementedInterfaces().iterator();
      // try each superinterface
      while (it.hasNext()) {
        IClass k = it.next();
        result = k.getMethod(selector);
        if (result != null) {
          return result;
        }
      }
    }
    return null;
  }
  /**
   * @param args
   * @throws CancelException
   * @throws IllegalArgumentException
   * @throws IOException
   */
  public static void main(String[] args)
      throws IllegalArgumentException, CancelException, IOException {
    try {
      Properties p = new Properties();
      p.putAll(WalaProperties.loadProperties());
    } catch (WalaException e) {
      e.printStackTrace();
      Assertions.UNREACHABLE();
    }

    runTestCase(TestConstants.JLEX_MAIN, TestConstants.JLEX, "JLex");
    // runTestCase(TestConstants.HELLO_MAIN, TestConstants.HELLO, "Hello");

  }
 @Override
 public void visitLoadMetadata(SSALoadMetadataInstruction instruction) {
   Assertions.UNREACHABLE();
   // PointerKey def = getPointerKeyForLocal(node, instruction.getDef());
   // InstanceKey iKey =
   // getInstanceKeyForClassObject(instruction.getLoadedClass());
   //
   // if (!contentsAreInvariant(symbolTable, du, instruction.getDef())) {
   // system.newConstraint(def, iKey);
   // } else {
   // system.findOrCreateIndexForInstanceKey(iKey);
   // system.recordImplicitPointsToSet(def);
   // }
 }
Beispiel #23
0
 /**
  * Package-visible constructor; only for use by ArrayClassLoader class. 'loader' must be the
  * Primordial IClassLoader.
  *
  * <p>[WHY? -- array classes are loaded by the element classloader??]
  */
 ArrayClass(TypeReference type, IClassLoader loader, IClassHierarchy cha) {
   this.type = type;
   this.loader = loader;
   this.cha = cha;
   TypeReference elementType = type.getInnermostElementType();
   if (!elementType.isPrimitiveType()) {
     IClass klass = loader.lookupClass(elementType.getName());
     if (klass == null) {
       Assertions.UNREACHABLE("caller should not attempt to create an array with type " + type);
     }
   } else {
     // assert loader.getReference().equals(ClassLoaderReference.Primordial);
   }
 }
 /**
  * @param elements List<PathElement>
  * @return canonical AccessPath object
  */
 public AccessPath findOrCreate(List<PathElement> elements) {
   if (PARANOID) {
     for (Iterator<PathElement> it = elements.iterator(); it.hasNext(); ) {
       if (!(it.next() instanceof PathElement)) {
         Assertions.UNREACHABLE();
       }
     }
   }
   APKey k = new APKey(elements);
   AccessPath result = map.get(k);
   if (result == null) {
     int id = vector.getMaxIndex() + 1;
     result = new AccessPath(k, id);
     map.put(k, result);
     vector.set(id, result);
     findOrCreateStartsWith(result.getHead()).add(id);
   }
   return result;
 }
 /*
  * @see com.ibm.wala.dataflow.IFDS.ISupergraph#getCalledNodes(java.lang.Object)
  */
 public Iterator<? extends Statement> getCalledNodes(Statement call) {
   switch (call.getKind()) {
     case NORMAL:
       Filter f =
           new Filter() {
             public boolean accepts(Object o) {
               Statement s = (Statement) o;
               return isEntry(s);
             }
           };
       return new FilterIterator<Statement>(getSuccNodes(call), f);
     case PARAM_CALLER:
     case HEAP_PARAM_CALLER:
       return getSuccNodes(call);
     default:
       Assertions.UNREACHABLE(call.getKind().toString());
       return null;
   }
 }
 /**
  * @see
  *     com.ibm.wala.ipa.callgraph.propagation.SSAContextInterpreter#getIR(com.ibm.wala.ipa.callgraph.CGNode)
  */
 @Override
 public IR getIR(CGNode node) {
   if (node == null) {
     throw new IllegalArgumentException("node is null");
   }
   assert understands(node);
   if (DEBUG) {
     System.err.println("generating IR for " + node);
   }
   IMethod method = node.getMethod();
   GetMethodContext context = (GetMethodContext) node.getContext();
   Map<Integer, ConstantValue> constants = HashMapFactory.make();
   if (method.getReference().equals(GET_METHOD)) {
     Atom name = Atom.findOrCreateAsciiAtom(context.getName());
     SSAInstruction instrs[] = makeGetMethodStatements(context, constants, name);
     return new SyntheticIR(
         method,
         context,
         new InducedCFG(instrs, method, context),
         instrs,
         SSAOptions.defaultOptions(),
         constants);
   }
   if (method.getReference().equals(GET_DECLARED_METHOD)) {
     Atom name = Atom.findOrCreateAsciiAtom(context.getName());
     SSAInstruction instrs[] = makeGetDeclaredMethodStatements(context, constants, name);
     return new SyntheticIR(
         method,
         context,
         new InducedCFG(instrs, method, context),
         instrs,
         SSAOptions.defaultOptions(),
         constants);
   }
   Assertions.UNREACHABLE("Unexpected method " + node);
   return null;
 }
Beispiel #27
0
    /**
     * Add any exceptional edges generated by the last instruction in a basic block.
     *
     * @param last the last instruction in a basic block.
     */
    protected void addExceptionalEdges(IInstruction last) {
      IClassHierarchy cha = getMethod().getClassHierarchy();
      if (last.isPEI()) {
        Collection<TypeReference> exceptionTypes = null;
        boolean goToAllHandlers = false;

        ExceptionHandler[] hs = getExceptionHandlers();
        if (last instanceof ThrowInstruction) {
          // this class does not have the type information needed
          // to determine what the athrow throws. So, add an
          // edge to all reachable handlers. Better information can
          // be obtained later with SSA type propagation.
          // TODO: consider pruning to only the exception types that
          // this method either catches or allocates, since these are
          // the only types that can flow to an athrow.
          goToAllHandlers = true;
        } else {
          if (hs != null && hs.length > 0) {
            IClassLoader loader = getMethod().getDeclaringClass().getClassLoader();
            BytecodeLanguage l = (BytecodeLanguage) loader.getLanguage();
            exceptionTypes = l.getImplicitExceptionTypes(last);
            if (last instanceof IInvokeInstruction) {
              IInvokeInstruction call = (IInvokeInstruction) last;
              exceptionTypes = HashSetFactory.make(exceptionTypes);
              MethodReference target =
                  MethodReference.findOrCreate(
                      l,
                      loader.getReference(),
                      call.getClassType(),
                      call.getMethodName(),
                      call.getMethodSignature());
              try {
                exceptionTypes.addAll(l.inferInvokeExceptions(target, cha));
              } catch (InvalidClassFileException e) {
                e.printStackTrace();
                Assertions.UNREACHABLE();
              }
            }
          }
        }

        if (hs != null && hs.length > 0) {
          // found a handler for this PEI

          // create a mutable copy
          if (!goToAllHandlers) {
            exceptionTypes = HashSetFactory.make(exceptionTypes);
          }

          // this var gets set to false if goToAllHandlers is true but some enclosing exception
          // handler catches all
          // exceptions.  in such a case, we need not add an exceptional edge to the method exit
          boolean needEdgeToExitForAllHandlers = true;
          for (int j = 0; j < hs.length; j++) {
            if (DEBUG) {
              System.err.println(" handler " + hs[j]);
            }
            BasicBlock b = getBlockForInstruction(hs[j].getHandler());
            if (DEBUG) {
              System.err.println(" target " + b);
            }
            if (goToAllHandlers) {
              // add an edge to the catch block.
              if (DEBUG) {
                System.err.println(" gotoAllHandlers " + b);
              }
              addExceptionalEdgeTo(b);
              // if the handler catches all exceptions, we don't need to add an edge to the exit or
              // any other handlers
              if (hs[j].getCatchClass() == null) {
                needEdgeToExitForAllHandlers = false;
                break;
              }
            } else {
              TypeReference caughtException = null;
              if (hs[j].getCatchClass() != null) {
                ClassLoaderReference loader =
                    ShrikeCFG.this.getMethod().getDeclaringClass().getReference().getClassLoader();
                caughtException = ShrikeUtil.makeTypeReference(loader, hs[j].getCatchClass());
                if (DEBUG) {
                  System.err.println(" caughtException " + caughtException);
                }
                IClass caughtClass = cha.lookupClass(caughtException);
                if (caughtClass == null) {
                  // conservatively add the edge, and raise a warning
                  addExceptionalEdgeTo(b);
                  Warnings.add(FailedExceptionResolutionWarning.create(caughtException));
                  // null out caughtException, to avoid attempting to process it
                  caughtException = null;
                }
              } else {
                if (DEBUG) {
                  System.err.println(" catchClass() == null");
                }
                // hs[j].getCatchClass() == null.
                // this means that the handler catches all exceptions.
                // add the edge and null out all types
                if (!exceptionTypes.isEmpty()) {
                  addExceptionalEdgeTo(b);
                  exceptionTypes.clear();
                  caughtException = null;
                }
              }
              if (caughtException != null) {
                IClass caughtClass = cha.lookupClass(caughtException);
                // the set "caught" should be the set of exceptions that MUST
                // have been caught by the handlers in scope
                ArrayList<TypeReference> caught =
                    new ArrayList<TypeReference>(exceptionTypes.size());
                // check if we should add an edge to the catch block.
                for (TypeReference t : exceptionTypes) {
                  if (t != null) {
                    IClass klass = cha.lookupClass(t);
                    if (klass == null) {
                      Warnings.add(FailedExceptionResolutionWarning.create(caughtException));
                      // conservatively add an edge
                      addExceptionalEdgeTo(b);
                    } else {
                      boolean subtype1 = cha.isSubclassOf(klass, caughtClass);
                      if (subtype1 || cha.isSubclassOf(caughtClass, klass)) {
                        // add the edge and null out the type from the array
                        addExceptionalEdgeTo(b);
                        if (subtype1) {
                          caught.add(t);
                        }
                      }
                    }
                  }
                }
                exceptionTypes.removeAll(caught);
              }
            }
          }
          // if needed, add an edge to the exit block.
          if ((exceptionTypes == null && needEdgeToExitForAllHandlers)
              || (exceptionTypes != null && !exceptionTypes.isEmpty())) {
            BasicBlock exit = exit();
            addExceptionalEdgeTo(exit);
          }
        } else {
          // found no handler for this PEI ... link to the exit block.
          BasicBlock exit = exit();
          addExceptionalEdgeTo(exit);
        }
      }
    }
Beispiel #28
0
  private void makeBasicBlocks() {
    ExceptionHandler[][] handlers;
    try {
      handlers = method.getHandlers();
    } catch (InvalidClassFileException e) {
      e.printStackTrace();
      Assertions.UNREACHABLE();
      handlers = null;
    }
    boolean[] r = new boolean[getInstructions().length];
    boolean[] catchers = new boolean[getInstructions().length];
    // we initially start with both the entry and exit block.
    int blockCount = 2;

    // Compute r so r[i] == true iff instruction i begins a basic block.
    // While doing so count the number of blocks.
    r[0] = true;
    IInstruction[] instructions = getInstructions();
    for (int i = 0; i < instructions.length; i++) {
      int[] targets = instructions[i].getBranchTargets();

      // if there are any targets, then break the basic block here.
      // also break the basic block after a return
      if (targets.length > 0 || !instructions[i].isFallThrough()) {
        if (i + 1 < instructions.length && !r[i + 1]) {
          r[i + 1] = true;
          blockCount++;
        }
      }

      for (int j = 0; j < targets.length; j++) {
        if (!r[targets[j]]) {
          r[targets[j]] = true;
          blockCount++;
        }
      }
      if (instructions[i].isPEI()) {
        ExceptionHandler[] hs = handlers[i];
        // break the basic block here.
        if (i + 1 < instructions.length && !r[i + 1]) {
          r[i + 1] = true;
          blockCount++;
        }
        if (hs != null && hs.length > 0) {
          for (int j = 0; j < hs.length; j++) {
            exceptionHandlers.add(hs[j]);
            if (!r[hs[j].getHandler()]) {
              // we have not discovered the catch block yet.
              // form a new basic block
              r[hs[j].getHandler()] = true;
              blockCount++;
            }
            catchers[hs[j].getHandler()] = true;
          }
        }
      }
    }

    BasicBlock entry = new BasicBlock(-1);
    addNode(entry);

    int j = 1;
    for (int i = 0; i < r.length; i++) {
      if (r[i]) {
        BasicBlock b = new BasicBlock(i);
        addNode(b);
        if (catchers[i]) {
          setCatchBlock(j);
        }
        j++;
      }
    }

    BasicBlock exit = new BasicBlock(-1);
    addNode(exit);
  }
  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;
  }
Beispiel #30
0
 /*
  * @see com.ibm.wala.classLoader.IClass#getAllFields()
  */
 @Override
 public Collection<IField> getAllFields() {
   Assertions.UNREACHABLE();
   return null;
 }