如何静态分析传递给每个字节码指令的引用类型?

Sam*_*ivu 14 java static-analysis bytecode

我已经重写了这个问题(问题仍然是相同的,只是背景噪音较小),希望能够减少针对所有错误事物的混淆 - 由于这一点,下面的一些评论似乎脱离了背景.

分析Java字节码,找到作为给定Java字节码指令的参数给出的所有可能的引用类型的最简单方法是什么?我对引用的类型感兴趣,也就是说,给定的putfield指令将接收一个Integer,或者它可能会收到一个Integer或Float等.

例如,考虑以下代码块:

   0:   aload_1
   1:   invokestatic    #21; //Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
   4:   astore_2
   5:   aload_2
   6:   ifnull  17
   9:   aload_0
   10:  aload_2
   11:  putfield    #27; //Field value:Ljava/lang/Number;
   14:  goto    25
   17:  aload_0
   18:  iconst_0
   19:  invokestatic    #29; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   22:  putfield    #27; //Field value:Ljava/lang/Number;
   25:  return
Run Code Online (Sandbox Code Playgroud)

我们可以推断pc 11上的putfield指令将接收ref类型的Integer.

0: aload pushes ref type of String (the method param)
1: invokestatic pops the ref type and pushes a ref type of Integer (invoked method return type)
4: astore pops the ref type of Integer and stores it in local variable 2
5: aload pushes the ref type of Integer from local variable 2
6: ifnull pops the ref type of Integer and conditionally jumps to pc 17
9: aload pushes "this"
10: aload pushes the ref type of Integer
11: putfield: we know we have a ref type of Integer that the instruction will put in field
Run Code Online (Sandbox Code Playgroud)

是否有任何字节码/代码分析库为我做这个,或者我必须自己写这个吗?该ASM项目有一个分析,这似乎像它可能为我做的工作的一部分,但真的没有足够的理由切换到使用它.

编辑:我完成了我的作业,并研究了Java VM Spec.

Ste*_*n C 3

Analyzer.analyze(...)方法似乎完全符合您的需要,如果没有,您可以选择破解它。这将是比重新开始更好的方法。

另一个想法是看看是否可以找到用 Java 实现的字节码验证器。验证者必须使用数据流分析来确保不会使用错误类型的参数调用方法。