Soot(Java静态分析框架)入门

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文件。
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值