[Soot-list] Some Problem use Jimple to create class from scrap

Jian Xu fightmyway at gmail.com
Sun Dec 14 22:37:34 EST 2008


Hi:
Problem 1:I use Jimple API to modify one method in class. The class looks
like this.

Class B extends A{
public void testX(){
  Class c = this.getClass()
  String b = c.toString();
  String a = this.getName();
}
}

Class A{
String name;

public void getName(){
  return name;
}

}


I use soot to create Jimple file of B. It looks like this
public void testX(){
        B r0;
        java.lang.Class r1;
        java.lang.String r2, r3;

        r0 := @this: B;
        r1 = virtualinvoke r0.<java.lang.Object: java.lang.Class
getClass()>();
        r2 = virtualinvoke r1.<java.lang.Class: java.lang.String
toString()>();
        r3 = virtualinvoke r0.<B: java.lang.String getName()>();
        return;
}

if I use API to create that method, I write code like this:

                SootClass A  = Scene.v().loadClassAndSupport("A");
SootClass object = Scene.v().loadClassAndSupport("java.lang.Object");
SootClass klass = Scene.v().loadClassAndSupport("java.lang.Class");

                        IdentityStmt is =
Jimple.v().newIdentityStmt(tmpRef1,

 Jimple.v().newThisRef(RefType.v(m.getDeclaringClass())));
 Local arg2, arg3;
 arg2 = Jimple.v().newLocal("l0", RefType.v("java.lang.String"));
                body.getLocals().add(arg2);
                arg3 = Jimple.v().newLocal("l1",
RefType.v("java.lang.String"));
                body.getLocals().add(arg3);

                Local tmpClass;

                tmpClass = Jimple.v().newLocal("tmpClass",
RefType.v("java.lang.Class"));
                body.getLocals().add(tmpClass);


                SootMethod toCall1 = object.getMethodByName("getClass");
                AssignStmt tmpAssign = Jimple.v().newAssignStmt(tmpClass,
         Jimple.v().newVirtualInvokeExpr
                        (tmpRef1, toCall1.makeRef(), new ArrayList(0)));


                SootMethod toCall2 = klass.getMethodByName("toString");
                AssignStmt arg2Assign = Jimple.v().newAssignStmt(arg2,
         Jimple.v().newVirtualInvokeExpr
                        (tmpClass, toCall2.makeRef(), new ArrayList(0)));



                SootMethod toCall3 = A.getMethodByName("getName");
                AssignStmt arg3Assign = Jimple.v().newAssignStmt(arg3,
                 Jimple.v().newVirtualInvokeExpr
                                (tmpRef1, toCall3.makeRef(), new
ArrayList(0)));


if I want to call the getName() method, because it extends from the
superclass. so if I use B.getMethodByName("getName");, it will say no method
found. I use this way successfully. But at last the Jimple code looks a
little different.

public void testX(){
        B r0;
        java.lang.Class r1;
        java.lang.String r2, r3;

        r0 := @this: B;
        r1 = virtualinvoke r0.<java.lang.Object: java.lang.Class
getClass()>();
        r2 = virtualinvoke r1.<java.lang.Class: java.lang.String
toString()>();
        r3 = virtualinvoke r0.<A: java.lang.String getName()>();
        return;
}

Yes, it seems A not B as the bytecode generated by javac. I wonder is this
the right style?

Problem 2: when I create new method, whether I must add the r0 := @this: B;?
if I don't uset this.xx() in my new method?
Problem 3: when I modify a exited method, I want to use "this" variable in
method, I find I can create a new newIdentityStmt(), but I can't add it to
units chain, even I put this line in the head of the chain. Because there is
already a newIdentityStmt. So I tmpRef1 = body.getLocals().getFirst(); to
get the "this" varable. I wonder is it the right way to get "this" variable?
That line will always exit and be at the head of unit chain?
Problem 4: when I want to get SootMethod, and the method has no arguments. I
use A.getMethod("getName", new ArrayList(0), VoidType.v()), it report error
and say no method. But know the Problem but I wonder how to set the second
parameter? And when at Jimple.v().newVirtualInvokeExpr(tmpRef1,
toCall3.makeRef(), new ArrayList(0)), I use new ArrayList(0) to represent no
argument,why it calls right?
Problem 5: if I use this.getClass().toString(), it will generate stack
variables in Jimple. Could please tell me how to declare a stack variable?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.CS.McGill.CA/pipermail/soot-list/attachments/20081215/52bd2076/attachment-0001.htm


More information about the Soot-list mailing list