2

Hey all, I am trying to use the ASM bytecode Tree Api to do static analysis for a class. I guess I have a pretty basic question. In a method say foobar(), I have a list of instructions within foobar (InsnList which has a List). Now I want to check if at instruction number 10, whether a function "barfoo(String args)" is being invoked.

Also seperatly, I need to verify whether a particular instruction is a conditional.

Thanks and Regards, SJ

Note: I can already read a class and reach the particular method I am interested in and iterate through each intruction of that method.

Solved (see: Greg's comments):

    AbstractInsnNode s = ...
    if(s.getType()==AbstractInsnNode.METHOD_INSN){
        MethodInsnNode methodInvocationNode = (MethodInsnNode) s;
        if(methodInvocationNode.name.equals("barfoo"))
        {
            return true;
        }
    }
dreamer13134
  • 471
  • 1
  • 6
  • 19
  • 1
    thanks Chris.... dint knw we are suppsd to do that.... doing nw – dreamer13134 Apr 16 '11 at 20:47
  • Do you know what the individual JVM instructions mean? Just trying to figure out where you're starting from here. – Greg Hewgill Apr 16 '11 at 20:47
  • Yes I do.... Ive gone through the ASM manual.... – dreamer13134 Apr 16 '11 at 20:51
  • Then what's the question? Given the instruction at offset 10, you should know how to determine whether the bytecode there is invoking a specific function. Similarly, given a specific instruction it should be trivial to determine whether it's a conditional or not. – Greg Hewgill Apr 16 '11 at 20:57
  • greg, Im glad its trivial. Using asm for the first time. Suppose ive an AbstractInsnNode instruction (the current instruction), how do I perform the check for invocation of a function or conditional? Do i compare the opcodes? – dreamer13134 Apr 16 '11 at 21:08
  • Yes, it looks like `AbstractInsnNode` has a `getOpcode()` method, which returns the bytecode of that instruction. Look up that instruction in the JVM reference, and see whether it's a conditional instruction (for example). – Greg Hewgill Apr 16 '11 at 21:11
  • I guess I could do that for the conditional. But if I need to check an invocation of a userdefined method, its opcode would be? I can see something like "invokespecial : call method in a specific class" in the reference. – dreamer13134 Apr 16 '11 at 21:16
  • 1
    That's right, invokespecial is one of the bytecodes that can invoke a method (there are a few more). It looks like you'll have to call `getType()` on your instruction node, and if the type is `METHOD_INSN` then you can cast to a [`MethodInsnNode`](http://asm.ow2.org/asm30/javadoc/user/org/objectweb/asm/tree/MethodInsnNode.html). From there, look at `.name` to see the method name. – Greg Hewgill Apr 16 '11 at 21:19
  • I added an answer to your question with the above info, to give you something you can accept :) – Greg Hewgill Apr 16 '11 at 21:51

2 Answers2

2

From looking at the ASM javadoc, it looks like you'll have to call getType() on your instruction node, and if the type is METHOD_INSN then you can cast to a MethodInsnNode. From there, look at .name to see the method name.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

Simple - once you've determined that you've invoked foobar, start incrementing a counter on each instruction you visit. On the tenth invocation, perform your check for the invocation of barfoo.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124