openjdk asmtools usage examples

asmtools

AsmTools 由一组(Java 类文件)汇编程序/反汇编程序组成:

  1. Jasm / Jdis: 一种汇编语言,提供类似 Java 的成员签名声明,同时提供符合 Java VM 规范的字节码指令助记符。Jasm 还为 classfile 属性中常见的构造提供高级语法。Jasm 编码测试对于对字节代码进行排序非常有用,因为 Javac 编译的代码通常不会对字节代码进行排序。

  2. JCod / JDec: 一种汇编语言,提供类文件结构的字节码容器。

AsmTools 是完全自反的,Java 二进制(.class)文件可以被反汇编成文本表示,而文本表示又可以汇编回同一个二进制文件。

Downloads

https://wiki.openjdk.java.net/display/CodeTools/asmtools 下载 jar 包或者在 github 上搜索下载 asmtools.jar

bash scripts for jdis

设置二个 bash 脚本,添加可执行权限,方便后面命令执行。

#!/bin/bashjava -jar asmtools.jar jdis $@

bash scripts for jasm

#!/bin/bashjava -jar asmtools.jar jasm $@

java 源码

public class HelloWorld {    public static void main(String[] args) {        System.out.println("Hello World");    }}

jdis / jasm

 jdis HelloWorld.classsuper public class HelloWorld        version 52:0{public Method "<init>":"()V"        stack 1 locals 1{                aload_0;                invokespecial   Method java/lang/Object."<init>":"()V";                return;}public static Method main:"([Ljava/lang/String;)V"        stack 2 locals 1{                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";                ldc     String "Hello World";                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";                return;}} // end Class HelloWorld jdis HelloWorld.class > HelloWorld.jdis# 删除原来的 HelloWorld.class 文件 rm -f HelloWorld.class# 用 jasm 生成 HelloWorld.class 文件 jasm HelloWorld.jdis ll HelloWorld.class-rw-r--r-- 1 yu staff 379 Apr 27 23:55 HelloWorld.class# 运行 jasm 生的 HelloWorld.class java HelloWorldHello World

修改 jdis 生成的文件

 awk 'NR==1,/Hello World/{sub(/Hello World/, "Hello World!!")} 1' HelloWorld.jdis > HelloWorld.jdis.2# 上面的命令修改了 ldc String "Hello World"; diff HelloWorld.jdis HelloWorld.jdis.220c20<               ldc     String "Hello World";--->               ldc     String "Hello World!!"; cat HelloWorld.jdis.2super public class HelloWorld        version 52:0{public Method "<init>":"()V"        stack 1 locals 1{                aload_0;                invokespecial   Method java/lang/Object."<init>":"()V";                return;}public static Method main:"([Ljava/lang/String;)V"        stack 2 locals 1{                getstatic       Field java/lang/System.out:"Ljava/io/PrintStream;";                ldc     String "Hello World!!";                invokevirtual   Method java/io/PrintStream.println:"(Ljava/lang/String;)V";                return;}} // end Class HelloWorld rm -f HelloWorld.class jasm HelloWorld.jdis.2 ll HelloWorld.class-rw-r--r-- 1 yu staff 383 Apr 28 00:15 HelloWorld.class java HelloWorldHello World!!

jdec / jcoder

jdec

#!/bin/bashjava -jar asmtools.jar jdec $@

jcoder

#!/bin/bashjava -jar asmtools.jar jcoder $@

jdec / jcoder example

 rm -f HelloWorld.class javac HelloWorld.java jdec HelloWorld.classclass HelloWorld {  0xCAFEBABE;  0; // minor version  52; // version  [] { // Constant Pool    ; // first element is empty    Method #6 #20; // #1    Field #21 #22; // #2    String #23; // #3    Method #24 #25; // #4    class #26; // #5    class #27; // #6    Utf8 "<init>"; // #7    Utf8 "()V"; // #8    Utf8 "Code"; // #9    Utf8 "LineNumberTable"; // #10    Utf8 "LocalVariableTable"; // #11    Utf8 "this"; // #12    Utf8 "LHelloWorld;"; // #13    Utf8 "main"; // #14    Utf8 "([Ljava/lang/String;)V"; // #15    Utf8 "args"; // #16    Utf8 "[Ljava/lang/String;"; // #17    Utf8 "SourceFile"; // #18    Utf8 "HelloWorld.java"; // #19    NameAndType #7 #8; // #20    class #28; // #21    NameAndType #29 #30; // #22    Utf8 "Hello World"; // #23    class #31; // #24    NameAndType #32 #33; // #25    Utf8 "HelloWorld"; // #26    Utf8 "java/lang/Object"; // #27    Utf8 "java/lang/System"; // #28    Utf8 "out"; // #29    Utf8 "Ljava/io/PrintStream;"; // #30    Utf8 "java/io/PrintStream"; // #31    Utf8 "println"; // #32    Utf8 "(Ljava/lang/String;)V"; // #33  } // Constant Pool  0x0021; // access  #5;// this_cpx  #6;// super_cpx  [] { // Interfaces  } // Interfaces  [] { // fields  } // fields  [] { // methods    { // Member      0x0001; // access      #7; // name_cpx      #8; // sig_cpx      [] { // Attributes        Attr(#9) { // Code          1; // max_stack          1; // max_locals          Bytes[]{            0x2AB70001B1;          }          [] { // Traps          } // end Traps          [] { // Attributes            Attr(#10) { // LineNumberTable              [] { // LineNumberTable                0  1;              }            } // end LineNumberTable            ;            Attr(#11) { // LocalVariableTable              [] { // LocalVariableTable                0 5 12 13 0;              }            } // end LocalVariableTable          } // Attributes        } // end Code      } // Attributes    } // Member    ;    { // Member      0x0009; // access      #14; // name_cpx      #15; // sig_cpx      [] { // Attributes        Attr(#9) { // Code          2; // max_stack          1; // max_locals          Bytes[]{            0xB200021203B60004;            0xB1;          }          [] { // Traps          } // end Traps          [] { // Attributes            Attr(#10) { // LineNumberTable              [] { // LineNumberTable                0  4;                8  5;              }            } // end LineNumberTable            ;            Attr(#11) { // LocalVariableTable              [] { // LocalVariableTable                0 9 16 17 0;              }            } // end LocalVariableTable          } // Attributes        } // end Code      } // Attributes    } // Member  } // methods  [] { // Attributes    Attr(#18) { // SourceFile      #19;    } // end SourceFile  } // Attributes} // end class HelloWorld jdec HelloWorld.class > HelloWorld.jdec rm -f HelloWorld.class jcoder HelloWorld.jdec ll HelloWorld.class-rw-r--r-- 1 yu staff 533 Apr 28 00:27 HelloWorld.class java HelloWorldHello World

References

  1. https://wiki.openjdk.java.net/display/CodeTools/asmtools