Skip to content

Confused about getInvokedMethodOnInstance and exclusion behaviors #5

Closed

Description

@copumpkin

As I mentioned on the other tracker, I'm still a bit confused about how Boomerang's forward queries interact with exclusions. I've crafted a more complete small reproduction that I think illustrates my confusion.

Here's a summary:

  1. If I craft a ForwardQuery pointing to new ArrayList, Boomerang finds the methods invoked on that instance correctly.
  2. If I do the same thing pointing to my custom Foo class (defined below), Boomerang finds no invoked methods on it.
  3. If I change my Soot setup to exclude Foo from analysis (just uncomment the commented line below), then Boomerang successfully finds the invoked methods on it.

For a simple example like this I can obviously just exclude Foo, but in the real world I often want to analyze aspects of Foo (and my Boomerang backward queries might jump into it) as well as getting methods invoked on its instances.

Can you help me understand why forward queries seem so sensitive to exclusions?

Harness code (Boomerang 3.0.2 from this repo + Soot 4.1)

import boomerang.Boomerang;
import boomerang.BoomerangOptions;
import boomerang.ForwardQuery;
import boomerang.scene.*;
import boomerang.scene.jimple.*;
import soot.*;
import soot.jimple.AssignStmt;
import soot.options.Options;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

public class Repro {
  static BoomerangOptions opts = new IntAndStringBoomerangOptions();

  private static void setupSoot(String classPath) {
    Options.v().set_whole_program(true);
    Options.v().setPhaseOption("cg.spark", "on");
    Options.v().set_no_bodies_for_excluded(true);
    Options.v().set_allow_phantom_refs(true);
    Options.v().set_keep_line_number(true);

    /* ********* Uncomment this line to see methods invoked on Foo! ********* */
    // Options.v().set_exclude(Collections.singletonList("Foo"));

    Options.v().setPhaseOption("jb", "use-original-names:true");
    Options.v().set_soot_classpath(classPath);
    Options.v().set_prepend_classpath(true);
    Options.v().set_process_dir(Arrays.asList(classPath.split(":")));
    Scene.v().loadNecessaryClasses();
  }

  private static void analyze() {
    PackManager.v().getPack("wjtp").add(new Transform("wjtp.repro", new ReproTransformer()));
    PackManager.v().getPack("cg").apply();
    PackManager.v().getPack("wjtp").apply();
  }

  public static void main(String[] args) {
    setupSoot("Test.jar");
    analyze();
  }

  private static Map<Statement, DeclaredMethod> getMethodsInvokedFromInstanceInStatement(Method method, AssignStmt as) {
    Boomerang solver = new Boomerang(new SootCallGraph(), SootDataFlowScope.make(Scene.v()), opts);
    Statement stmt = JimpleStatement.create(as, method)[0];
    Val var = new AllocVal(new JimpleVal(as.getLeftOp(), method), stmt, new JimpleVal(as.getRightOp(), method));
    ForwardQuery fwq = new ForwardQuery(stmt, var);
    return solver.solve(fwq).getInvokedMethodOnInstance();
  }

  static class ReproTransformer extends SceneTransformer {
    @Override
    protected void internalTransform(String name, Map<String, String> options) {
      BoomerangPretransformer.v().reset();
      BoomerangPretransformer.v().apply();

      SootMethod m = Scene.v().getMethod("<Test: java.util.List foos()>");
      Method method = JimpleMethod.of(m);

      System.out.println("All method units:");
      for (Unit u : m.getActiveBody().getUnits()) {
        System.out.println("\t" + u.toString());
      }
      AssignStmt newFoo = (AssignStmt) m.getActiveBody().getUnits().stream().filter(
          x -> x.toString().contains("$stack2 = new Foo")).findFirst().get();
      AssignStmt newList = (AssignStmt) m.getActiveBody().getUnits().stream().filter(
          x -> x.toString().contains("$stack4 = new java.util.LinkedList")).findFirst().get();

      // This will only show results if set_exclude above gets uncommented
      System.out.println("\nFoo invoked methods:");
      for (Map.Entry<Statement, DeclaredMethod> e : getMethodsInvokedFromInstanceInStatement(method, newFoo).entrySet()) {
        System.out.println("\t" + e.getKey().toString());
        System.out.println("\t\t" + e.getValue().toString());
      }

      // This will show results regardless of the exclusion
      System.out.println("\nList invoked methods:");
      for (Map.Entry<Statement, DeclaredMethod> e : getMethodsInvokedFromInstanceInStatement(method, newList).entrySet()) {
        System.out.println("\t" + e.getKey().toString());
        System.out.println("\t\t" + e.getValue().toString());
      }
    }
  }
}

Code for analysis

import java.util.LinkedList;
import java.util.List;

class Foo {
	void bar() {
		System.out.println("zomg bar");
	}

	public void baz() {
		System.out.println("zomg baz");
	}
}

public class Test {
	public static List<Foo> foos() {
		Foo foo = new Foo();
		foo.baz();
		System.out.println(foo);
		List<Foo> x = new LinkedList<>();
		x.add(foo);
		foo.bar();
		return x;
	}

	public static void main(String[] args) {
		System.out.println(foos());
	}
}

How I produced Test.jar that the harness consumes

javac Test.java
jar cvf Test.jar *.class

Activity

  1. johspaeth commented on Aug 22, 2020

    @johspaeth
    Member

    Great, thanks for sharing a reproducible example that helps a lot. I'll have a look at it and get back to you.

  2. johspaeth commented on Aug 26, 2020

    @johspaeth
    Member

    Thanks for the example. It allowed me to quickly look into the code.

    There is a bug in the implementation of getInvokedMethodOnInstance. I push a fix and a new version by the end of the week.

  3. copumpkin commented on Aug 26, 2020

    @copumpkin
    Author

    Thanks! That's great news, I'll look forward to updating 😄

    While you were looking, is there anything obviously silly or wrong I was doing in my setup that will cause me issues down the line? I simplified the core Boomerang querying code for this repro, but the setup stuff is mostly the same.

  4. copumpkin commented on Sep 2, 2020

    @copumpkin
    Author

    Hi @johspaeth, do you know if there's a simple workaround I could use until it's fixed? Or if an old version didn't have this issue?

  5. added 3 commits that reference this issue on Nov 8, 2020
  6. johspaeth commented on Nov 9, 2020

    @johspaeth
    Member

    @copumpkin The issue is solved in the latest version (3.1.1).
    I added a test for the missing reported call and used your code for testing. Thanks for reporting.

  7. copumpkin commented on Nov 9, 2020

    @copumpkin
    Author

    Thank you! I'll give it a go in the coming week 👍

  8. added a commit that references this issue on Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions