private static DefinitelyDerefedParams getAnalysisDriver( IMethod mtd, AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha) { IR ir = cache.getIRFactory().makeIR(mtd, Everywhere.EVERYWHERE, options.getSSAOptions()); ControlFlowGraph<SSAInstruction, ISSABasicBlock> cfg = ir.getControlFlowGraph(); accountCodeBytes(mtd); return new DefinitelyDerefedParams(mtd, ir, cfg, cha); }
/** * @param m a "normal" (bytecode-based) method * @param options options governing ssa construction * @return an IR for m, built according to the specified options. null if m is abstract or native. * @throws IllegalArgumentException if m is null */ public synchronized IR findOrCreateIR(final IMethod m, Context c, final SSAOptions options) { if (m == null) { throw new IllegalArgumentException("m is null"); } if (m.isAbstract() || m.isNative()) { return null; } if (factory.contextIsIrrelevant(m)) { c = Everywhere.EVERYWHERE; } if (DISABLE) { return factory.makeIR(m, c, options); } IR ir = (IR) irCache.find(m, c, options); if (ir == null) { ir = factory.makeIR(m, c, options); irCache.cache(m, c, options, ir); } return ir; }
/** * @param m a method * @param options options governing ssa construction * @return DefUse information for m, built according to the specified options. null if unavailable * @throws IllegalArgumentException if m is null */ public synchronized DefUse findOrCreateDU(IMethod m, Context c, SSAOptions options) { if (m == null) { throw new IllegalArgumentException("m is null"); } if (m.isAbstract() || m.isNative()) { return null; } if (factory.contextIsIrrelevant(m)) { c = Everywhere.EVERYWHERE; } DefUse du = (DefUse) duCache.find(m, c, options); if (du == null) { IR ir = findOrCreateIR(m, c, options); du = new DefUse(ir); duCache.cache(m, c, options, du); } return du; }
/** * @param m a "normal" (bytecode-based) method * @param options options governing ssa construction * @return an IR for m, built according to the specified options. null if m is abstract or native. * @throws IllegalArgumentException if m is null */ public synchronized IR findOrCreateIR(final IMethod m, Context c, final SSAOptions options) { if (m == null) { throw new IllegalArgumentException("m is null"); } if (m.isAbstract() || m.isNative()) { return null; } if (factory.contextIsIrrelevant(m)) { c = Everywhere.EVERYWHERE; } if (DISABLE) { return factory.makeIR(m, c, options); } IR ir = (IR) irCache.find(m, c, options); if (ir == null) { ir = factory.makeIR(m, c, options); irCache.cache(m, c, options, ir); } return ir; }
/** * @param m a method * @param options options governing ssa construction * @return DefUse information for m, built according to the specified options. null if unavailable * @throws IllegalArgumentException if m is null */ public synchronized DefUse findOrCreateDU(IMethod m, Context c, SSAOptions options) { if (m == null) { throw new IllegalArgumentException("m is null"); } if (m.isAbstract() || m.isNative()) { return null; } if (factory.contextIsIrrelevant(m)) { c = Everywhere.EVERYWHERE; } DefUse du = (DefUse) duCache.find(m, c, options); if (du == null) { IR ir = findOrCreateIR(m, c, options); du = new DefUse(ir); duCache.cache(m, c, options, du); } return du; }
private static IR getIr(IMethod method) { return irFactory.makeIR(method, Everywhere.EVERYWHERE, options.getSSAOptions()); }
public static IR getIr(IMethod method) { return irFactory.makeIR(method, Everywhere.EVERYWHERE, options.getSSAOptions()); }
private boolean returnsThis(IMethod method) { MethodReference mref = method.getReference(); if(method.getNumberOfParameters() < 1) return false; Boolean b = returnsThis_cache.get(mref); if(b != null) return b; for(SSAInstruction inst : factory.makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions()).getInstructions()) { if(inst instanceof SSAReturnInstruction) { SSAReturnInstruction ret = (SSAReturnInstruction)inst; if(ret.getResult() == 2) { returnsThis_cache.put(mref, true); return true; } } } returnsThis_cache.put(mref, false); return false; }
private boolean returnsThis(IMethod method) { MethodReference mref = method.getReference(); if(method.getNumberOfParameters() < 1) return false; Boolean b = returnsThis_cache.get(mref); if(b != null) return b; for(SSAInstruction inst : factory.makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions()).getInstructions()) { if(inst instanceof SSAReturnInstruction) { SSAReturnInstruction ret = (SSAReturnInstruction)inst; if(ret.getResult() == 2) { returnsThis_cache.put(mref, true); return true; } } } returnsThis_cache.put(mref, false); return false; }
public Map<IMethod, CorrelationSummary> findCorrelatedAccesses(SourceModule[] scripts_array) throws ClassHierarchyException { JSCallGraphUtil.setTranslatorFactory(translatorFactory); JavaScriptLoaderFactory loaders = JSCallGraphUtil.makeLoaders(null); CAstAnalysisScope scope = new CAstAnalysisScope(scripts_array, loaders, Collections.singleton(JavaScriptLoader.JS)); IClassHierarchy cha = ClassHierarchyFactory.make(scope, loaders, JavaScriptLoader.JS); try { com.ibm.wala.cast.util.Util.checkForFrontEndErrors(cha); } catch (WalaException e) { return Collections.emptyMap(); } IRFactory<IMethod> factory = AstIRFactory.makeDefaultFactory(); Map<IMethod, CorrelationSummary> correlations = HashMapFactory.make(); for(IClass klass : cha) { for(IMethod method : klass.getAllMethods()) { IR ir = factory.makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions()); CorrelationSummary summary = findCorrelatedAccesses(method, ir); if(!summary.getCorrelations().isEmpty()) correlations.put(method, summary); } } return correlations; }
public Map<IMethod, CorrelationSummary> findCorrelatedAccesses(SourceModule[] scripts_array) throws ClassHierarchyException { JSCallGraphUtil.setTranslatorFactory(translatorFactory); JavaScriptLoaderFactory loaders = JSCallGraphUtil.makeLoaders(null); CAstAnalysisScope scope = new CAstAnalysisScope(scripts_array, loaders, Collections.singleton(JavaScriptLoader.JS)); IClassHierarchy cha = ClassHierarchyFactory.make(scope, loaders, JavaScriptLoader.JS); try { com.ibm.wala.cast.util.Util.checkForFrontEndErrors(cha); } catch (WalaException e) { return Collections.emptyMap(); } IRFactory<IMethod> factory = AstIRFactory.makeDefaultFactory(); Map<IMethod, CorrelationSummary> correlations = HashMapFactory.make(); for(IClass klass : cha) { for(IMethod method : klass.getAllMethods()) { IR ir = factory.makeIR(method, Everywhere.EVERYWHERE, SSAOptions.defaultOptions()); CorrelationSummary summary = findCorrelatedAccesses(method, ir); if(!summary.getCorrelations().isEmpty()) correlations.put(method, summary); } } return correlations; }
protected void dump(ClassHierarchy cha) { for (Object name : cha) { IClass cls = (IClass) name; System.err.println(("class " + cls)); for (Object name2 : cls.getDeclaredInstanceFields()) { IField fld = (IField) name2; System.err.println(("instance field " + fld)); } for (Object name2 : cls.getDeclaredStaticFields()) { IField fld = (IField) name2; System.err.println(("static field " + fld)); } for (Object name2 : cls.getDeclaredMethods()) { IMethod mth = (IMethod) name2; if (mth.isStatic()) System.err.print("static "); System.err.println(("method " + mth + " with " + mth.getNumberOfParameters() + " parameters")); for (int i = 0; i < mth.getNumberOfParameters(); i++) { System.err.println(("param " + i + ": " + mth.getParameterType(i))); } System.err.println(factory.makeIR(mth, Everywhere.EVERYWHERE, options)); } } }
protected static void printIRsForCHA(IClassHierarchy cha, Predicate<String> exclude) { // for constructing IRs IRFactory<IMethod> factory = AstIRFactory.makeDefaultFactory(); for (IClass klass : cha) { // ignore models of built-in JavaScript methods String name = klass.getName().toString(); if (exclude.test(name)) continue; // get the IMethod representing the code IMethod m = klass.getMethod(AstMethodReference.fnSelector); if (m != null) { IR ir = factory.makeIR(m, Everywhere.EVERYWHERE, new SSAOptions()); System.out.println(ir); if (m instanceof AstMethod) { AstMethod astMethod = (AstMethod) m; System.out.println(astMethod.getSourcePosition()); } System.out.println("===================================================\n"); } } }
@Test public void test3() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("inferInt"), new ImmutableByteArray(UTF8Convert.toUTF8("()V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, true); TypeAbstraction type = ti.getType(7); Assert.assertNotNull("null type abstraction", type); Assert.assertTrue("inferred wrong type", type.toString().equals("int")); }
@Test public void test4() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("useCast"), new ImmutableByteArray(UTF8Convert.toUTF8("(Ljava/lang/Object;)V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, false); TypeAbstraction type = ti.getType(4); Assert.assertNotNull("null type abstraction", type); Assert.assertTrue("inferred wrong type " + type, type instanceof ConeType && ((ConeType)type).getTypeReference().getName().toString().equals("Ljava/lang/String")); }
@Test public void test1() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("()V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, false); for (int i = 1; i <= ir.getSymbolTable().getMaxValueNumber(); i++) { System.err.println(i + " " + ti.getType(i)); } }
@Test public void test2() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("bar"), new ImmutableByteArray(UTF8Convert.toUTF8("(I)V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, true); Assert.assertNotNull("null type abstraction for parameter", ti.getType(2)); }
private IR doMethod(MethodReference method) { IAnalysisCacheView cache = makeAnalysisCache(); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir1 = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); cache.clear(); checkNotAllNull(ir1.getInstructions()); checkNoneNull(ir1.iterateAllInstructions()); try { GraphIntegrity.check(ir1.getControlFlowGraph()); } catch (UnsoundGraphException e) { System.err.println(ir1); e.printStackTrace(); Assert.assertTrue("unsound CFG for ir1", false); } IR ir2 = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); cache.clear(); try { GraphIntegrity.check(ir2.getControlFlowGraph()); } catch (UnsoundGraphException e1) { System.err.println(ir2); e1.printStackTrace(); Assert.assertTrue("unsound CFG for ir2", false); } Assert.assertEquals(ir1.toString(), ir2.toString()); return ir1; }
@Test public void testLocalNamesWithoutPiNodes() { SSAPiNodePolicy save = options.getSSAOptions().getPiNodePolicy(); options.getSSAOptions().setPiNodePolicy(null); MethodReference mref = scope.findMethod(AnalysisScope.APPLICATION, "LcornerCases/Locals", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("([Ljava/lang/String;)V"))); Assert.assertNotNull("method not found", mref); IMethod imethod = cha.resolveMethod(mref); Assert.assertNotNull("imethod not found", imethod); IAnalysisCacheView cache = new AnalysisCacheImpl(options.getSSAOptions()); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); options.getSSAOptions().setPiNodePolicy(save); // v1 should be the parameter "a" at pc 0 String[] names = ir.getLocalNames(0, 1); Assert.assertTrue("failed local name resolution for v1@0", names != null); Assert.assertTrue("incorrect number of local names for v1@0: " + names.length, names.length == 1); Assert.assertTrue("incorrect local name resolution for v1@0: " + names[0], names[0].equals("a")); // v2 is a compiler-induced temporary Assert.assertTrue("didn't expect name for v2 at pc 2", ir.getLocalNames(2, 2) == null); // at pc 5, v1 should represent the locals "a" and "b" names = ir.getLocalNames(5, 1); Assert.assertTrue("failed local name resolution for v1@5", names != null); Assert.assertTrue("incorrect number of local names for v1@5: " + names.length, names.length == 2); Assert.assertTrue("incorrect local name resolution #0 for v1@5: " + names[0], names[0].equals("a")); Assert.assertTrue("incorrect local name resolution #1 for v1@5: " + names[1], names[1].equals("b")); }
@Test public void testLocalNamesWithPiNodes() { SSAPiNodePolicy save = options.getSSAOptions().getPiNodePolicy(); options.getSSAOptions().setPiNodePolicy(SSAOptions.getAllBuiltInPiNodes()); MethodReference mref = scope.findMethod(AnalysisScope.APPLICATION, "LcornerCases/Locals", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("([Ljava/lang/String;)V"))); Assert.assertNotNull("method not found", mref); IMethod imethod = cha.resolveMethod(mref); Assert.assertNotNull("imethod not found", imethod); IAnalysisCacheView cache = new AnalysisCacheImpl(options.getSSAOptions()); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); options.getSSAOptions().setPiNodePolicy(save); // v1 should be the parameter "a" at pc 0 String[] names = ir.getLocalNames(0, 1); Assert.assertTrue("failed local name resolution for v1@0", names != null); Assert.assertTrue("incorrect number of local names for v1@0: " + names.length, names.length == 1); Assert.assertTrue("incorrect local name resolution for v1@0: " + names[0], names[0].equals("a")); // v2 is a compiler-induced temporary Assert.assertTrue("didn't expect name for v2 at pc 2", ir.getLocalNames(2, 2) == null); // at pc 5, v1 should represent the locals "a" and "b" names = ir.getLocalNames(5, 1); Assert.assertTrue("failed local name resolution for v1@5", names != null); Assert.assertTrue("incorrect number of local names for v1@5: " + names.length, names.length == 2); Assert.assertTrue("incorrect local name resolution #0 for v1@5: " + names[0], names[0].equals("a")); Assert.assertTrue("incorrect local name resolution #1 for v1@5: " + names[1], names[1].equals("b")); } }