烟灰 - 从CFG获取Jimplebody
我想从Java类获取unitgraph
。 我通过classfile
加载它,然后获取maws> main()main()
的method_info
。 然后,我创建一个cfg
,然后尝试将其转换为unitgraph
。 我的方法是获取
cfg
的jimplebody ,然后创建unitgraph
。 但是,我无法通过调用来获得
cfg.jimplify(...)
,因为它引发了以下错误:
Exception in thread "main" java.lang.RuntimeException: no method associated w/ body
at soot.Body.getMethod(Body.java:137)
at soot.coffi.CFG.jimplify(CFG.java:814)
at com.LiveVariableAnalysis.main(LiveVariableAnalysis.java:151)
我的代码如下:
String mainClassName = "Calculate";
String mainClassPath = String.format("./target/test-classes/%s.class", mainClassName);
ClassFile mainClassFile = new ClassFile(mainClassName);
FileInputStream is = new FileInputStream(mainClassPath);
mainClassFile.loadClassFile(is);
logger.info(String.format("Loading Class: %s ...", mainClassFile));
method_info methodInfo = null;
for (method_info method: mainClassFile.methods) {
if (Objects.equals(method.toName(mainClassFile.constant_pool), "main")) {
methodInfo = method;
}
}
logger.info(String.format("Loading method_info: %s ...", methodInfo.toName(mainClassFile.constant_pool)));
mainClassFile.parseMethod(methodInfo);
CFG cfg = new CFG(methodInfo);
JimpleBody jimpleBody = new JimpleBody();
// Error occurs here
cfg.jimplify(mainClassFile.constant_pool, mainClassFile.this_class, mainClassFile.bootstrap_methods_attribute, jimpleBody);
UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);
logger.info(String.format("Creating unitGraph with %d units ...", unitGraph.size()));
我知道还有其他创建unitgraph
的方法,例如:
String mainClassName = "Calculate";
SootClass mainClass = Scene.v().loadClassAndSupport(className);
Scene.v().setMainClass(mainClass);
soot.Main.main(args);
SootClass mainClass = Scene.v().getMainClass();
String methodSignature = "void main(java.lang.String[])";
SootMethod mainMethod = mainClass.getMethod(methodSignature);
Body jimpleBody = mainMethod.retrieveActiveBody();
UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);
以这种方式,我需要设置caste.v()。setSootClassPath(path)
for jce.jar.jar
和rt.jar
,我不想在代码中发生。因此,如果有另一种方法可以在没有设置这样的路径的情况下获得unitgraph
,请帮助我。
I'd like to get UnitGraph
from a Java Class.
I load it by ClassFile
and get the method_info
of main()
.
Then I create a CFG
and try to convert it into a UnitGraph
.
My method is to get JimpleBody
of the CFG
and then create a UnitGraph
.
However, I can't get JimpleBody
by invoking cfg.jimplify(...)
since it throws the following error:
Exception in thread "main" java.lang.RuntimeException: no method associated w/ body
at soot.Body.getMethod(Body.java:137)
at soot.coffi.CFG.jimplify(CFG.java:814)
at com.LiveVariableAnalysis.main(LiveVariableAnalysis.java:151)
My code is as follows:
String mainClassName = "Calculate";
String mainClassPath = String.format("./target/test-classes/%s.class", mainClassName);
ClassFile mainClassFile = new ClassFile(mainClassName);
FileInputStream is = new FileInputStream(mainClassPath);
mainClassFile.loadClassFile(is);
logger.info(String.format("Loading Class: %s ...", mainClassFile));
method_info methodInfo = null;
for (method_info method: mainClassFile.methods) {
if (Objects.equals(method.toName(mainClassFile.constant_pool), "main")) {
methodInfo = method;
}
}
logger.info(String.format("Loading method_info: %s ...", methodInfo.toName(mainClassFile.constant_pool)));
mainClassFile.parseMethod(methodInfo);
CFG cfg = new CFG(methodInfo);
JimpleBody jimpleBody = new JimpleBody();
// Error occurs here
cfg.jimplify(mainClassFile.constant_pool, mainClassFile.this_class, mainClassFile.bootstrap_methods_attribute, jimpleBody);
UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);
logger.info(String.format("Creating unitGraph with %d units ...", unitGraph.size()));
I know there are other ways to create a UnitGraph
, such as:
String mainClassName = "Calculate";
SootClass mainClass = Scene.v().loadClassAndSupport(className);
Scene.v().setMainClass(mainClass);
soot.Main.main(args);
SootClass mainClass = Scene.v().getMainClass();
String methodSignature = "void main(java.lang.String[])";
SootMethod mainMethod = mainClass.getMethod(methodSignature);
Body jimpleBody = mainMethod.retrieveActiveBody();
UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);
However, in this way, I need to set Scene.v().setSootClassPath(path)
for jce.jar
and rt.jar
, which I don't want to occur in my code. So if there is another way I can get UnitGraph
without setting such a path, please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我仍然无法将
method_info
转换为SootMathod
然后获得UnitGraph
,但我可以使用Options.v()。 set_prepend_classpath(true)
以避免直接设置jce.jar
和rt.jar
。这也达到了我的目的。Although I still can't turn
method_info
into aSootMathod
and then get aUnitGraph
, I can useOptions.v().set_prepend_classpath(true)
to avoid setjce.jar
andrt.jar
dirctly. This also achieves my goal.