[Soot-list] Using Soot to Instrument a class file

Eric Bodden eric.bodden at mail.mcgill.ca
Fri Jun 19 19:57:54 EDT 2009


You will find all the methods you need to create Jimple statement
within the class soot.jimple.Jimple:
http://www.sable.mcgill.ca/soot/doc/soot/jimple/Jimple.html

In Java bytecode (or Jimple), to create a List, let's say an
ArrayList, you need to (1) create the ArrayList using a "new"
statement, and then (2) call the list's constructor. This should work
as follows:

//myList = new ArrayList();
NewExpr newExpr = Jimple.newNewExpr(RefType.v("java.util.ArrayList"));
Local l = Jimple.newLocal("myList",RefType.v("java.util.ArrayList"));
body.getLocals().add(l);
AssignStmt assign = Jimple.newAssignStmt(l,newExpr);
body.getUnits().add(assign);

//call constructor
MethodRef ref =
Scene.v().getSootClass("java.util.ArrayList").getMethod("<init>",Collections.emptyList()).makeRef();
InvokeExpr expr = Jimple.newSpecialInvokeExpr(l,ref);
body.getUnits().add(Jimple.v().newInvokeStmt(expr));

... and so on.

Eric



2009/6/19 Chixiang Zhou <alexanderchouus at hotmail.com>
>
> Hi Eric,
>
> Thank you for pointing out my error!!
>
> One more stupid question: you mentioned the method call I inserted doesn't take one single List as argument.
>
> So you advised me to create a list at runtime. Can you tell me how to create a list at runtime?
>
>
> Sincerely yours,
> Chixiang Zhou (Alexander)
>
>
>
>
> > Date: Fri, 19 Jun 2009 18:12:58 -0400
> > Subject: Re: [Soot-list] Using Soot to Instrument a class file
> > From: eric.bodden at mail.mcgill.ca
> > To: alexanderchouus at hotmail.com
> >
> > Yes, the error is similar to what I expected.
> >
> > The method that you are trying to call at runtime has a signature
> > "void MutantChecker(java.util.List)", i.e. it takes a List as an
> > argument. However, you are inserting a method call that does not take
> > one single list as argument, but rather three different items:
> >
> > List args = new ArrayList<Value>();
> > args.add(expr.getArgBox(0).getValue());//the query executed
> > at the execution point
> > args.add(IntConstant.v(++ep_id));//the execution point ID
> > args.add(conn.getValue());//the database connection
> >
> > You do create a list at compile time, but you need to create a list at
> > runtime instead. Then store this list in a Local, add the three items
> > from above to the list (using method calls to the list's "add") method
> > and then pass the local of this list as "args" your actual method
> > invoke expression.
> >
> > Eric
> >
> > 2009/6/19 Chixiang Zhou <alexanderchouus at hotmail.com>:
> > > Hi Eric,
> > >
> > > I think I passed the soot.Local variables to the newStaticInvokeExpr(...).
> > > In fact, these Local variables are what I need.
> > >
> > > Attached is the source code of my instrumenter (instrumenter.java).
> > >
> > > The relevant part in Instrumenter.java is the following:
> > >
> > > (1) Load the application class and function to be inserted
> > > static {
> > > mutateClass = Scene.v().loadClassAndSupport("mutation.Mutation");
> > > mutate = mutateClass.getMethod("void MutantChecker(java.util.List)");
> > > }
> > >
> > > (2)Construct a List containing all the arguments to be passed to
> > > MutantChecker
> > > List args = new ArrayList<Value>();
> > > args.add(expr.getArgBox(0).getValue());//the query executed at the
> > > execution point
> > > args.add(IntConstant.v(++ep_id));//the execution point ID
> > > args.add(conn.getValue());//the database connection
> > >
> > > (3) make a new invoke expression
> > > InvokeExpr incExpr= Jimple.v().newStaticInvokeExpr(mutate.makeRef(), args);
> > >
> > > Hope to hear from you soon!
> > >
> > > Thanks a lot in advance!!!
> > >
> > >
> > > Sincerely yours,
> > > Chixiang Zhou (Alexander)
> > >
> > >
> > >
> > >
> > >> Date: Fri, 19 Jun 2009 17:46:54 -0400
> > >> Subject: Re: [Soot-list] Using Soot to Instrument a class file
> > >> From: eric.bodden at mail.mcgill.ca
> > >> To: alexanderchouus at hotmail.com
> > >>
> > >> Hello Chixiang.
> > >>
> > >> I am not sure, but it seems like you are misunderstanding something.
> > >> The fact that the Soot API uses a List as parameter does not mean that
> > >> you have to generate or use a List at runtime (and I believe that this
> > >> is what you are doing). The method call to newStaticInvokeExpr(...)
> > >> simply takes as second argument a list of "Local" (type soot.Local)
> > >> variables. These variables should hold the values that you are trying
> > >> to call the method with.
> > >>
> > >> It's impossible to say what exactly you are doing wrong without seeing
> > >> your code but I suspect that you are mixing up compile-time and
> > >> runtime things.
> > >>
> > >> Eric
> > >>
> > >>
> > >>
> > >> 2009/6/19 Chixiang Zhou <alexanderchouus at hotmail.com>
> > >> >
> > >> > Dear all,
> > >> >
> > >> > I'm Chixiang Zhou, a Ph.D. student in computer science, NYU.
> > >> >
> > >> > I'm learning Soot these days and trying to instrument a class file. I
> > >> > found a tutorial called Using Soot to Instrument a class file is quite
> > >> > related to my goal.
> > >> >
> > >> > However, I encountered a problem when I wrote a similar instrumenter in
> > >> > Java.
> > >> >
> > >> > The example mentioned in the tutorial is to insert a function call void
> > >> > increase(int) before some statement. In fact, my goal is the same, only
> > >> > differing in the function call.
> > >> > The function (e.g. add(java.lang.String, int, java.sql.Connection)) I
> > >> > want to insert has 3 parameters. I looked up the Soot API documentation,
> > >> > which gives an API called newStaticInvokeExpr(SootMethodRef method, List
> > >> > args). Therefore, I wrapped the 3 parameters into a List, and passed the
> > >> > List to API newStaticInvokeExpr(
> > >> > SootMethodRef method, List args). The insturmenter successfully inserted
> > >> > the function call add() in the application's bytecode ( I checked the Jimple
> > >> > code).
> > >> >
> > >> > The problem occurs when I run the instrumented application's bytecode.
> > >> > An error shows up: Exception in thread "main"
> > >> > java.lang.IncompatibleClassChangeError
> > >> >
> > >> > I checked the Jimple code again and found something weird. The following
> > >> > is the Jimple code fragment:
> > >> > r70 = <java.util.List>r0;
> > >> > staticinvoke <testing.Sum: void add(java.util.List)>(r70);
> > >> >
> > >> > r0 is the java.sql.Connection, which is the third parameter. It seems
> > >> > that r70 only has the third parameter java.sql.Connection r0 while ignoring
> > >> > the first two parameters.
> > >> >
> > >> >
> > >> > Could anyone tell me what might be the cause of that error if I want to
> > >> > use a List paramter?
> > >> >
> > >> > BTW, attached is the tutorial.
> > >> >
> > >> > Sincerely yours,
> > >> > Chixiang Zhou (Alexander)
> > >> >
> > >> >
> > >> >
> > >> >
> > >> > ________________________________
> > >> > Microsoft brings you a new way to search the web. Try Bing‚Ñ¢ now
> > >> > _______________________________________________
> > >> > Soot-list mailing list
> > >> > Soot-list at sable.mcgill.ca
> > >> > http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
> > >> >
> > >>
> > >>
> > >>
> > >> --
> > >> Eric Bodden
> > >> Sable Research Group, McGill University
> > >> Montréal, Québec, Canada
> > >
> > > ________________________________
> > > Insert movie times and more without leaving Hotmail®. See how.
> >
> >
> >
> > --
> > Eric Bodden
> > Sable Research Group, McGill University
> > Montréal, Québec, Canada
>
> ________________________________
> Bing™ brings you maps, menus, and reviews organized in one place. Try it now.


--
Eric Bodden
Sable Research Group, McGill University
Montréal, Québec, Canada


More information about the Soot-list mailing list