Intro
在利用SootUp分析程序之前,需要了解它的一些核心数据结构:
AnalysisInputLocation:指向待载入View的目标代码的位置,是SoutClass的来源。这个位置可以是Java源代码的路径、Java类路径、JAR文件、Android APK文件、Jimple文件等View:处理待分析代码的表现形式SootClass:Soot中类的表示,通过ClassType从View获取SootMethod:Soot中类方法的表示,通过MethodSignature从View获取SootField:Soot中类字段的表示,通过FieldSignature从View获取Body:Soot中方法体的表示StmtGraph:方法体的控制流图
Create a View
首先得创建一个AnalysisInputLocation指定分析对象所在位置,再将其传入创建一个JavaView
查看AnalysisInputLocation的实现类对应不同的分析对象

以分析Java源代码为例:
JavaSourcePathAnalysisInputLocation inputLocation
    = new JavaSourcePathAnalysisInputLocation("src/main/resources/");
JavaView view = new JavaView(inputLocation);官方文档(v1.3.0)说直接分析源码目前是实验性阶段,通常应该自己先把源码编译成字节码再进行分析
JavaClassPathAnalysisInputLocation inputLocation
    = new JavaClassPathAnalysisInputLocation("target/classes/");
JavaView view = new JavaView(inputLocation);默认情况下,一个类被获取时会被永久存储到缓存中,如果我们不希望类被无限缓存,可以给View提供不同的CacheProvider。
比如下面的LRUCache最多存储50个类,当有一个新的类被获取时,会替代掉最近最少被使用到的类
JavaView view = new JavaView(Collections.singletonList(inputLocation),
                new LRUCacheProvider(50));Retrieving a Class
每个类都由一个独特的签名来标识,也就是类的全限定名(fully-qualified class name, aka fqcn),在SootUp中是通过ClassType来表示一个类的签名的
比如我们要分析的类为
package org.example;
public class HelloWorld {
    public HelloWorld(){}
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}先定义一个ClassType来标识HelloWorld类,再利用它从View中获取对应的SootClass对象。
JavaClassType classType = view.getIdentifierFactory().getClassType("org.example.HelloWorld");
Optional<JavaSootClass> clazzOpt = view.getClass(classType);
if(!clazzOpt.isPresent()){
    System.out.println("Class not found");
    return;
}
JavaSootClass clazz = clazzOpt.get();Retrieving a Method
类似地,方法也有签名,在SootUp中通过MethodSignature来表示
比如下面获取HelloWorld#main方法
MethodSignature methodSignature = view.getIdentifierFactory().getMethodSignature(
    classType,   // 类签名
    "main",      // 方法名
    "void",      // 返回类型
    Collections.singletonList("java.lang.String[]")  // 参数类型
);接着从View中获取对应的SootMethod
Optional<JavaSootMethod> methodOpt = view.getMethod(methodSignature);
if(!methodOpt.isPresent()){
    System.out.println("Method not found");
    return;
}
JavaSootMethod method = methodOpt.get();
System.out.printf("Method Signature: %s\n", method.getSignature());
System.out.println(method.getBody());Method Signature: <org.example.HelloWorld: void main(java.lang.String[])>
[PUBLIC, STATIC]
{
    java.io.PrintStream $stack1;
    java.lang.String[] args;
    args := @parameter0: java.lang.String[];
    $stack1 = <java.lang.System: java.io.PrintStream out>;
    virtualinvoke $stack1.<java.io.PrintStream: void println(java.lang.String)>("Hello World");
    return;
}这里method.getBody()返回的就是方法体对应的Jimple IR,下一节再细讲Jimple
当然也可以从SootClass获取它包含的SootMethod
MethodSubSignature subSignature = methodSignature.getSubSignature();
System.out.println(subSignature);
Optional<JavaSootMethod> clazzMethod = clazz.getMethod(subSignature);
if(!clazzMethod.isPresent()){
    System.out.println("Method not found");
    return;
}
JavaSootMethod method = clazzMethod.get();SubSignature为void main(java.lang.String[]),实际上就是去掉了签名的类名部分
Retrieving the CFG
每个SootMethod都包含一个控制流图CFG,由StmtGraph表示,这个结构是后面进行程序分析的基础。
StmtGraph<?> stmtGraph = method.getBody().getStmtGraph();DotExporter可以将CFG导出为dot文件,再通过graphviz等工具查看
以下面的程序为例
package org.example;
public class CFG {
    public void foo(int x){
        for (int i = 0; i < x; i++){
            System.out.println(i);
        }
    }
}生成的Jimple:
{
    int i, x;
    java.io.PrintStream $stack3;
    org.example.CFG this;
    this := @this: org.example.CFG;
    x := @parameter0: int;
    i = 0;
  label1:
    if i >= x goto label2;
    $stack3 = <java.lang.System: java.io.PrintStream out>;
    virtualinvoke $stack3.<java.io.PrintStream: void println(int)>(i);
    i = i + 1;
    goto label1;
  label2:
    return;
}下面会打印一个url,可以在线查看和编辑生成的CFG的dot文件
String urlToWebeditor = DotExporter.createUrlToWebeditor(stmtGraph);
        System.out.println(urlToWebeditor);
下面会将CFG写入一个dot文件
String dotStr = DotExporter.buildGraph(stmtGraph, false, null, method.getSignature());
Files.write(Paths.get("src/main/resources/CFG.dot"), dotStr.getBytes());dot -T png .\CFG.dot -o CFG.dot.png转png图片
Last updated
Was this helpful?