0

I am trying to use the ASM bytecode tree API for static analysis of Java Code. I have a ClassNode cn, MethodNode m and the list of instructions in that method say InsnList list.

Suppose for a given instruction( i.e. AbstractInsnNode) s, I need to find all the definitions/assignments of the variable at s in the above instruction list. To make it more clear, suppose a variable var is defined and initialized on line 2, then assigned some other value on line number 8 and then used on line number 12. Line number 12 is my s, in this case. Also, assume lots of conditional code in the lines in between.

Is this possible to do with ASM? How??

Thanks and Regards, SJ

For clarity,

public void funcToAnalyze(String k, SomeClass v) {
            int numIter = 0;

            /*
               Do cool stuff here.... modifies member variables and passed params too
            */

    if (v.rank > 1 || numIter>200) {
        magicFunction(k, 1);
    }
}

Here, suppose the conditional is the JumpInsnNode (current instruction) and I need to find if (and where) any of the variables in the conditional (v.rank and numIter in this case) are modified or assigned anywhere in the above code. Keep it simple, just member variables (no static function or delegation to function of another class).

dreamer13134
  • 471
  • 1
  • 6
  • 19
  • It is possible to do for the simple cases. It get complicated fair quickly depending on what you are trying to achieve. – Peter Lawrey Apr 18 '11 at 07:58
  • I assume we speak of member variables here? What you want is in general impossible. You can collect the putfield instructions that refer to this variable in the current method. But you have also to consider methods that are called and somehow may have access to this variable. Due to overloading you have to consider here all members from the compile time type class as well as from any overriding class. – Ingo Apr 18 '11 at 09:52
  • Yes I just want the simple case of member variables only. How do I collect the putfield instructions that refer to this variable in the current method. Acually, my current statement s, may have one or more variable how do I know each one. – dreamer13134 Apr 18 '11 at 15:06
  • I can only suggest you study the JVM, their instructions and data structures and the library you are using (ASM; BECEL, etc.). Short answer is, the putfield instruction references an item in the constant pool that gives you the name and class of the field. It should be not to hard to figure out how to extract this info from ASM, yet we can't spare you the effort to look at the javadoc/manual at least briefly. – Ingo Apr 20 '11 at 09:06

1 Answers1

1

The SourceInterpreter computes SourceValues for each Frame for a corresponding instruction in MethodNode. Basically it tells which instructions could place value to a given variable or stack slot.

Also see ASM User Guide for more information about ASM analysis package.

However if you just need to detect if certain variable been assigned, then all you have to do is to look for xSTORE instructions with corresponding variable indexes.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67