变量定义和赋值检测asm字节码
我正在尝试使用 ASM 字节码树 API 对 Java 代码进行静态分析。 我有一个 ClassNode cn、MethodNode m 和该方法中的指令列表,即 InsnList 列表。
假设对于给定的指令(即 AbstractInsnNode),我需要在上面的指令列表中找到 s 处变量的所有定义/赋值。为了更清楚地说明,假设在第 2 行定义并初始化变量 var,然后在第 8 行分配一些其他值,然后在第 12 行使用。在本例中,第 12 行是 my s。另外,假设中间的行中有大量条件代码。
这可能与 ASM 相关吗?如何??
谢谢和问候, SJ
为了清楚起见,
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);
}
}
这里假设条件是 JumpInsnNode (当前指令),我需要查找条件中的任何变量(在本例中为 v.rank 和 numIter)是否(以及在哪里)被修改或分配在上面的代码。保持简单,只是成员变量(没有静态函数或委托给另一个类的函数)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SourceInterpreter 计算SourceValues
对于每个 Frame MethodNode中的相应指令。基本上它告诉哪些指令可以为给定的变量或堆栈槽赋值。
另请参阅 ASM 用户指南,了解有关 ASM
然而,如果您只需要检测某个变量是否被分配,那么您所要做的就是查找具有相应变量索引的 xSTORE 指令。
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.