soot入门
1. HelloWorld生成Jimple
1.1 创建Helloworld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
1.2 编译java文件,生成bytecode
javac HelloWorld.java
此时src目录下,通过javac编译生成HelloWorld.class字节码文件
1.3 生成Jimple文件
拷贝soot.jar到当前目录
执行命令:
java -cp .\soot.jar soot.Main -pp -cp . -f J HelloWorld
其中:
-cp .\soot.jar
: 指定soot class路径
-pp
: 指定soot搜索的Java路径
-cp .
: 指定要分析项目的路径,.
表示当前路径
-f J
: 指定输出文件的类型,为Jimple
HelloWorld
: 指定需要分析的class文件
1.4 查看Jimple文件
当面目录下会自动生成sootOutPut文件夹,文件夹下会有刚生成好的HelloWorld.jimple文件
生成的Jimple文件内容如下:
public class HelloWorld extends java.lang.Object
{
public void <init>()
{
HelloWorld r0;
r0 := @this: HelloWorld;
specialinvoke r0.<java.lang.Object: void <init>()>();
return;
}
public static void main(java.lang.String[])
{
java.io.PrintStream $r0;
java.lang.String[] r1;
r1 := @parameter0: java.lang.String[];
$r0 = <java.lang.System: java.io.PrintStream out>;
virtualinvoke $r0.<java.io.PrintStream: void println(java.lang.String)>("Hello World!");
return;
}
}
2. 使用soot生成VFG控制流图
2.1 构建程序代码
为了使VFG流图更饱满,使用的代码参考Leetcode第4题(困难)的解析代码:
public class LeetCode {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] nums;
int m = nums1.length;
int n = nums2.length;
nums = new int[m + n];
if (m == 0) {
if (n % 2 == 0) {
return (nums2[n / 2 - 1] + nums2[n / 2]) / 2.0;
} else {
return nums2[n / 2];
}
}
if (n == 0) {
if (m % 2 == 0) {
return (nums1[m / 2 - 1] + nums1[m / 2]) / 2.0;
} else {
return nums1[m / 2];
}
}
int count = 0;
int i = 0, j = 0;
while (count != (m + n)) {
if (i == m) {
while (j != n) {
nums[count++] = nums2[j++];
}
break;
}
if (j == n) {
while (i != m) {
nums[count++] = nums1[i++];
}
break;
}
if (nums1[i] < nums2[j]) {
nums[count++] = nums1[i++];
} else {
nums[count++] = nums2[j++];
}
}
if (count % 2 == 0) {
return (nums[count / 2 - 1] + nums[count / 2]) / 2.0;
} else {
return nums[count / 2];
}
}
}
2.2 编译java文件,生成bytecode
javac LeetCode.java
2.3 使用soot.tools.CFGViewer生成控制流图
java -cp .\soot.jar soot.tools.CFGViewer -pp -cp . LeetCode
运行上述命令则会在SootOutPut目录下,生成dot文件。(p.s.需要修改一下dot文件名,自动生成的dot文件名中带有空格,会导致命令行操作失败)
进入sootOutPut目录,执行命令:
dot -Tpng -o LeetCode.png LeetCode.dot
则会在目录下生成相对应的png文件。