我想从Java类中获取UnitGraph
。我用ClassFile
加载它,得到main()
的method_info
。然后我创建一个CFG
,并尝试将其转换为UnitGraph
。我的方法是得到CFG
的JimpleBody
,然后创建一个UnitGraph
。然而,我无法通过调用cfg.jimplify(...)
来获得JimpleBody
,因为它会抛出以下错误:
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);
但是,通过这种方式,我需要为jce.jar
和rt.jar
设置Scene.v().setSootClassPath(path)
,我不希望在代码中出现这种情况。因此,如果有另一种方法可以在不设置这样的路径的情况下获得UnitGraph
,请帮助我。
虽然我仍然无法将method_info
转换为SootMathod
然后获得UnitGraph
,但我可以使用Options.v().set_prepend_classpath(true)
来避免直接设置jce.jar
和rt.jar
。这也达到了我的目的。