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
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用 Soot 进行静态分析找出代码中的空指针,可以按照以下步骤进行: 1. 使用 Soot 的解析器解析目标类文件。您可以使用以下代码来解析一个类文件: ``` String className = "com.example.MyClass"; SootClass sootClass = Scene.v().loadClassAndSupport(className); ``` 这将加载类“com.example.MyClass”并构建其 SootClass 对象。 2. 将 SootClass 对象转换为 SootMethod 对象。您可以使用以下代码获取类的所有方法: ``` List<SootMethod> methods = sootClass.getMethods(); ``` 您可以遍历该列表并对每个方法执行以下步骤。 3. 使用 Soot 的 BodyTransformer 类来获取方法体。例如: ``` BodyTransformer bodyTransformer = new BodyTransformer() { protected void internalTransform(Body body, String phaseName, Map options) { // 代码分析逻辑 } }; ``` 您可以覆盖 internalTransform() 方法并在其中编写代码分析逻辑。这个方法将被 Soot 自动调用,并传递 Body 对象。 4. 在 internalTransform() 方法中,使用 Soot 的 Jimple 语言分析方法体。例如: ``` UnitPatchingChain units = body.getUnits(); Iterator<Unit> unitIt = units.snapshotIterator(); while (unitIt.hasNext()) { Stmt stmt = (Stmt) unitIt.next(); // 代码分析逻辑 } ``` 这将获取方法体中的所有语句,并将其转换为 Jimple 语言中的 Stmt 对象。您可以遍历这些语句,查找可能导致空指针的代码逻辑。 5. 编写代码分析逻辑。您可以使用 Soot 的 API 检测代码中的空指针。例如,以下代码将检查一个引用是否为空: ``` Value ref = ...; if (ref instanceof Local || ref instanceof FieldRef || ref instanceof ArrayRef) { if (RefType.typeOf(ref).getClassName().equals("java.lang.Object")) { // 检测到可能的空指针异常 } } ``` 您可以在代码中使用类似的逻辑来检测其他类型的空指针异常。 以上是使用 Soot 进行静态分析找出代码中的空指针的基本步骤。由于每个应用程序都有不同的结构和逻辑,因此您可能需要根据自己的情况进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值