Java Bytecode For Discriminating Developers - GeeCON 2011

8,854 views
8,772 views

Published on

Published in: Technology, News & Politics
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,854
On SlideShare
0
From Embeds
0
Number of Embeds
4,940
Actions
Shares
0
Downloads
209
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Java Bytecode For Discriminating Developers - GeeCON 2011

  1. 1. Java Bytecode for Discriminating Developers<br />Anton Arhipov<br />
  2. 2. whoami<br />Anton Arhipov<br />Java dev. / Product Lead<br />JRebel<br />http://arhipov.blogspot.com<br />@antonarhipov<br />@javarebel<br />
  3. 3. Who-are-you?<br />
  4. 4. The Master Plan<br />80%: Bytecode 101<br />20%: Examples<br />
  5. 5. THE INTRO<br />
  6. 6. 1 + 2<br />
  7. 7. +<br />1 + 2<br />1<br />2<br />
  8. 8. +<br />1 + 2<br />1<br />2<br />1 2 + <br />
  9. 9. +<br />1 + 2<br />1<br />2<br />1 2 + <br />
  10. 10. +<br />1 + 2<br />1<br />2<br />1 2 + <br />1<br />PUSH 1<br />
  11. 11. +<br />1 + 2<br />1<br />2<br />1 2 + <br />2<br />PUSH 1<br />PUSH 2<br />1<br />
  12. 12. +<br />1 + 2<br />1<br />2<br />1 2 + <br />3<br />PUSH 1<br />PUSH 2<br />ADD<br />
  13. 13. +<br />1 + 2<br />1<br />2<br />1 2 + <br />3<br />ICONST_1<br />ICONST_2<br />IADD<br />
  14. 14. ? = 1 + 2<br />
  15. 15. TAXONOMY<br />
  16. 16. Bytecode<br />One-byte instructions<br />256 possible opcodes<br />200+ in use<br />
  17. 17. Bytecode<br />One-byte instructions<br />256 possible opcodes<br />200+ in use<br />
  18. 18. TYPE<br />OPERATION<br />
  19. 19. TYPE<br />OPERATION<br /><TYPE> ::= b, s, c, i, l, f, d, a<br />
  20. 20. TYPE<br />OPERATION<br /><TYPE> ::= b, s, c, i, l, f, d, a<br />Operations with constant values (ldc, iconst_1)<br />
  21. 21. TYPE<br />OPERATION<br /><TYPE> ::= b, s, c, i, l, f, d, a<br />Operations with constant values (ldc, iconst_1)<br />Local variables and stack interaction (load/store)<br />Array operations (aload, astore)<br />Math (add, sub, mul, div)<br />Boolean/bitwise operations (iand, ixor)<br />Comparisons (cmpg, cmpl, ifne, ifeq)<br />Conversions (l2d, i2l)<br />
  22. 22. Bytecode Taxonomy<br />Stack <br />Manipulation<br />
  23. 23. Bytecode Taxonomy<br />Stack <br />Manipulation<br />Flow<br />Control<br />
  24. 24. Bytecode Taxonomy<br />Stack <br />Manipulation<br />Flow<br />Control<br />Object <br />Model<br />
  25. 25. Bytecode Taxonomy<br />Stack <br />Manipulation<br />Flow<br />Control<br />Object <br />Model<br />Arithmetics<br />
  26. 26. Bytecode Taxonomy<br />Stack <br />Manipulation<br />Flow<br />Control<br />Object <br />Model<br />Arithmetics<br />monitorenter<br />monitorexit<br />
  27. 27. TOOLING<br />
  28. 28. javap<br />Java class file disassembler<br />Used with no options shows class structure only<br />Methods, superclass, interfaces, etc<br />-c shows the bytecode<br />-private shows all methods and members<br />-s prints internal signatures<br />-l prints line numbers and local variable tables<br />
  29. 29. HELLO WORLD!<br />
  30. 30.
  31. 31. C:workgeeconclasses>javap Hello -c<br />
  32. 32. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />
  33. 33. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />the default constructor<br />
  34. 34. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />push this to stack<br />
  35. 35. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />invoke <init> on this<br />
  36. 36. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />
  37. 37. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />
  38. 38. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />get static field<br />
  39. 39. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />load string to the stack<br />
  40. 40. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />invoke method with parameter<br />
  41. 41. C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />
  42. 42. What’s #1,#2, etc ?<br />C:workgeeconclasses>javap Hello -c<br />Compiled from "Hello.java"<br />public class Hello extends java.lang.Object{<br />public Hello();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />4: return<br />public static void main(java.lang.String[]);<br />Code:<br />0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #3; //String Hello, World!<br />5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br />
  43. 43. C:workgeeconclasses>javap Hello -c -verbose<br />
  44. 44. C:workgeeconclasses>javap Hello -c -verbose<br />
  45. 45. C:workgeeconclasses>javap Hello -c -verbose<br />Compiled from "Hello.java“<br />public class Hello extends java.lang.Object<br /> SourceFile: "Hello.java"<br /> minor version: 0<br /> major version: 50<br /> Constant pool:<br />const #1 = Method#6.#20; // java/lang/Object."<init>":()V<br />const #2 = Field#21.#22; // java/lang/System.out:Ljava/io/PrintStream;<br />const #3 = String#23; // Hello, World!<br />const #4 = Method#24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)V<br />const #5 = class#26; // Hello<br />const #6 = class#27; // java/lang/Object<br />const #7 = Asciz<init>;<br />const #8 = Asciz()V;<br />
  46. 46. C:workgeeconclasses>javap Hello -c -verbose<br />Compiled from "Hello.java“<br />public class Hello extends java.lang.Object<br /> SourceFile: "Hello.java"<br /> minor version: 0<br /> major version: 50<br /> Constant pool:<br />const #1 = Method#6.#20; // java/lang/Object."<init>":()V<br />const #2 = Field#21.#22; // java/lang/System.out:Ljava/io/PrintStream;<br />const #3 = String#23; // Hello, World!<br />const #4 = Method#24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)V<br />const #5 = class#26; // Hello<br />const #6 = class#27; // java/lang/Object<br />const #7 = Asciz<init>;<br />const #8 = Asciz()V;<br />
  47. 47. C:workgeeconclasses>javap Hello -c -verbose<br />…<br />public Hello();<br />Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: return<br />LineNumberTable:<br /> line 1: 0<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LHello;<br />
  48. 48. C:workgeeconclasses>javap Hello -c -verbose<br />…<br />public Hello();<br />Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: return<br />LineNumberTable:<br /> line 1: 0<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LHello;<br />
  49. 49. C:workgeeconclasses>javap Hello -c -verbose<br />…<br />public Hello();<br />Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: return<br />LineNumberTable:<br /> line 1: 0<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LHello;<br />
  50. 50. C:workgeeconclasses>javap Hello -c -verbose<br />…<br />public static void main(java.lang.String[]);<br /> Code:<br /> Stack=2, Locals=1, Args_size=1<br /> 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;<br /> 3: ldc #3; //String Hello, World!<br /> 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V<br /> 8: return<br /> LineNumberTable:<br /> line 4: 0<br /> line 5: 8<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 9 0 args [Ljava/lang/String;<br />
  51. 51. STACK MACHINE<br />
  52. 52. Stack Machine<br />
  53. 53. Stack Machine<br />JVM is a stack-based machine<br />
  54. 54. Stack Machine<br />JVM is a stack-based machine<br />Each thread has a stack<br />
  55. 55. Stack Machine<br />JVM is a stack-based machine<br />Each thread has a stack<br />Stack stores frames<br />
  56. 56. Stack Machine<br />JVM is a stack-based machine<br />Each thread has a stack<br />Stack stores frames<br />Frame is created on method invocation<br />
  57. 57. Stack Machine<br />JVM is a stack-based machine<br />Each thread has a stack<br />Stack stores frames<br />Frame is created on method invocation<br />Frame consists of:<br />Operand stack<br />Array of local variables<br />
  58. 58. The Frame<br />Local variables<br />N<br />1<br />2<br />3<br />…<br />Operand stack<br />#1<br />Constant <br />Pool<br />
  59. 59. public java.lang.String getName();<br /> Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: getfield #2; //Field name:Ljava/lang/String;<br /> 4: areturn<br />LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LGet;<br />
  60. 60. 0 1 2 3 4<br />areturn<br />aload_0<br />00<br />02<br />getfield<br />public java.lang.String getName();<br /> Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: getfield #2; //Field name:Ljava/lang/String;<br /> 4: areturn<br />LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LGet;<br />
  61. 61. 0 1 2 3 4<br />B0<br />2A<br />00<br />02<br />B4<br />public java.lang.String getName();<br /> Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: getfield #2; //Field name:Ljava/lang/String;<br /> 4: areturn<br />LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LGet;<br />
  62. 62. public java.lang.String getName();<br /> Code:<br /> Stack=1, Locals=1, Args_size=1<br /> 0: aload_0<br /> 1: getfield #2; //Field name:Ljava/lang/String;<br /> 4: areturn<br />LocalVariableTable:<br /> Start Length Slot Name Signature<br /> 0 5 0 this LGet;<br />
  63. 63. STACK CRUNCHING<br />
  64. 64. A<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />B<br />
  65. 65. A<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />A<br />B<br />
  66. 66. A<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />B<br />
  67. 67. B<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />A<br />
  68. 68. B<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />A<br />B<br />
  69. 69. B<br />dup<br />pop<br />swap<br />dup_x1<br />dup2_x2<br />A<br />B<br />B<br />A<br />
  70. 70. dup2_x2<br />How do you <br />swap doubles?<br />
  71. 71. dup2_x2<br />
  72. 72. dup2_x2<br />dconst_0<br />0.0<br />
  73. 73. dup2_x2<br />dconst_0<br />dconst_1<br />1.0<br />0.0<br />
  74. 74. dup2_x2<br />dconst_0<br />dconst_1<br />swap<br />1.0<br />0.0<br />
  75. 75. dup2_x2<br />dconst_0<br />dconst_1<br />swap<br />1.0<br />not allowed!<br />0.0<br />
  76. 76. dup2_x2<br />dconst_0<br />dconst_1<br />swap2<br />1.0<br />0.0<br />
  77. 77. dup2_x2<br />dconst_0<br />dconst_1<br />swap2<br />1.0<br />doesn’t<br />exist<br />0.0<br />
  78. 78. dup2_x2<br />dconst_0<br />dconst_1<br />dup2_x2<br />1.0<br />0.0<br />1.0<br />
  79. 79. dup2_x2<br />dconst_0<br />dconst_1<br />dup2_x2<br />pop2<br />0.0<br />1.0<br />
  80. 80. dup2_x2<br />dconst_0<br />dconst_1<br />dup2_x2<br />pop2<br />0.0<br />1.0<br />profit!<br />
  81. 81. LOCAL VARIABLES<br />
  82. 82. Local Variables<br />
  83. 83. public int calculate(int);<br />Code:<br /> Stack=2, Locals=2, Args_size=2<br /> …<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br />0 5 0 this LLocalVariables;<br />0 5 1 value I<br />Local Variables<br />
  84. 84. public int calculate(int);<br />Code:<br /> Stack=2, Locals=2, Args_size=2<br /> …<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br />0 5 0 this LLocalVariables;<br />0 5 1 value I<br />numbered from 0<br />Local Variables<br />
  85. 85. public int calculate(int);<br />Code:<br /> Stack=2, Locals=2, Args_size=2<br /> …<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br />0 5 0 this LLocalVariables;<br />0 5 1 value I<br />Local Variables<br />instance methods<br />have this at 0<br />
  86. 86. The table maps numbers to names<br />public int calculate(int);<br />Code:<br /> Stack=2, Locals=2, Args_size=2<br /> …<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br />0 5 0 this LLocalVariables;<br />0 5 1 value I<br />Local Variables<br />
  87. 87. Sized explicitly<br />public int calculate(int);<br />Code:<br /> Stack=2, Locals=2, Args_size=2<br /> …<br /> LocalVariableTable:<br /> Start Length Slot Name Signature<br />0 5 0 this LLocalVariables;<br />0 5 1 value I<br />Local Variables<br />
  88. 88. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />0<br />0<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  89. 89. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />0<br />0<br />"Hello"<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  90. 90. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />"Hello"<br />0<br />0<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  91. 91. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />"Hello"<br />0<br />0<br />1<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  92. 92. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />"Hello"<br />0<br />0<br />1<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  93. 93. Stack<br />Local Variables<br />value<br />var<br />value<br />depth<br />ldc"Hello"<br />astore_0<br />iconst_1<br />astore_1<br />aload_0<br />"Hello"<br />0<br />0<br />"Hello"<br />1<br />1<br />1<br />2<br />2<br />3<br />3<br />4<br />4<br />
  94. 94. load<br />Stack<br />Local Variables Table<br />store<br />
  95. 95. OBJECTS<br />
  96. 96. new<br /><init><br /><clinit><br />0xBB<br />Instance initialization method<br />Class and interface initialization method<br />Object Initialization<br />
  97. 97. Object Initialization: static {}<br />static {};<br /> Code:<br />0: iconst_1<br /> 1: putstatic #2; //Field a:I<br /> 4: iconst_2<br /> 5: putstatic #3; //Field b:I<br /> 8: return<br />
  98. 98. Object Initialization: static {}<br /><clinit><br />static {};<br /> Code:<br />0: iconst_1<br /> 1: putstatic #2; //Field a:I<br /> 4: iconst_2<br /> 5: putstatic #3; //Field b:I<br /> 8: return<br />
  99. 99. Object Initialization: new<br />
  100. 100. Object Initialization: new<br />public Initializer();<br />Code:<br />
  101. 101. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br />
  102. 102. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br />
  103. 103. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br />
  104. 104. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br /> 5: new#2; //class java/lang/Object<br /> 8: dup<br />
  105. 105. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br /> 5: new#2; //class java/lang/Object<br /> 8: dup<br /> 9: invokespecial #1; //Method java/lang/Object."<init>":()V<br />12: putfield #3; //Field o:Ljava/lang/Object;<br />
  106. 106. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br /> 5: new#2; //class java/lang/Object<br /> 8: dup<br /> 9: invokespecial #1; //Method java/lang/Object."<init>":()V<br />12: putfield #3; //Field o:Ljava/lang/Object;<br />15: return<br />
  107. 107. Object Initialization: new<br />public Initializer();<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br /> 5: new#2; //class java/lang/Object<br /> 8: dup<br /> 9: invokespecial #1; //Method java/lang/Object."<init>":()V<br />12: putfield #3; //Field o:Ljava/lang/Object;<br />15: return<br />
  108. 108. Object Initialization: {}<br />
  109. 109. Object Initialization: {}<br />?<br />
  110. 110. Object Initialization: {}<br />public Initializer(int);<br /> Code:<br />0:aload_0<br /> 1:invokespecial #1; // ..<init><br />4:aload_0<br /> 5:iconst_1<br /> 6:putfield #2; //Field a:I<br /> 9:aload_0<br />10:iconst_2<br /> 11:putfield #3; //Field c:I<br /> 14:aload_0<br />15:iload_1<br />16:putfield #4; //Field b:I<br />19:return<br />
  111. 111.
  112. 112. There’s no initializer<br />
  113. 113. METHOD INVOCATION&PARAMETER PASSING<br />
  114. 114. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  115. 115. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />Integer.valueOf(“42”)<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  116. 116. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br /><init><br />private void foo();<br />super.method();<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  117. 117. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  118. 118. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />class B<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  119. 119. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />class B<br />A/method1<br />B/method2<br />B/method3<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  120. 120. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />class B impl X<br />A/method1<br />B/method2<br />B/method3<br />X/methodX<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  121. 121. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />class B impl X<br />A/method1<br />B/method2<br />B/method3<br />X/methodX<br />class D impl X<br />D/method1<br />X/methodX<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  122. 122. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />class A<br />A/method1<br />A/method2<br />class B impl X<br />A/method1<br />B/method2<br />B/method3<br />X/methodX<br />class D impl X<br />D/method1<br />X/methodX<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  123. 123. Methods & Parameters<br />invokestatic<br />invokespecial<br />invokevirtual<br />invokeinterface<br />invokedynamic<br />Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless,  Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01<br />
  124. 124. Method Invocation<br />obj.method(param1, param2);<br />
  125. 125. Method Invocation<br />obj.method(param1, param2);<br />push obj<br />push param1<br />push param2<br />call method<br />
  126. 126. Method Invocation<br />obj.method(param1, param2);<br />obj<br />push obj<br />push param1<br />push param2<br />call method<br />
  127. 127. Method Invocation<br />obj.method(param1, param2);<br />param1<br />push obj<br />push param1<br />push param2<br />call method<br />obj<br />
  128. 128. Method Invocation<br />obj.method(param1, param2);<br />param2<br />push obj<br />push param1<br />push param2<br />call method<br />param1<br />obj<br />
  129. 129. Method Invocation<br />obj.method(param1, param2);<br />obj?<br />push obj<br />push param1<br />push param2<br />call method<br />
  130. 130. Method Invocation<br />this.add(1, 2);<br />0: aload_0<br />1: iconst_1<br />2: iconst_2<br />3: invokevirtual #2; //Method add:(II)I<br />
  131. 131. INNER CLASSES<br />
  132. 132. Inner Classes<br />
  133. 133. Inner Classes<br />
  134. 134. Inner Classes<br />class Car$Engine extends j.l.Object{<br />final Car this$0;<br />Car$Engine(Car);<br />public void start();<br />Code:<br /> 0: aload_0<br /> 1: getfield #1; //Field this$0:LCar;<br /> 4: invokestatic #3; //Car.access$000:(LCar;)V<br /> 7: return<br />}<br />
  135. 135. Inner Classes<br />public class Car extends j.l.Object{<br />public Car();<br />private void move();<br />static void access$000(Car);<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; // move: ()V;<br /> 4: return<br />}<br />class Car$Engine extends j.l.Object{<br />final Car this$0;<br />Car$Engine(Car);<br />public void start();<br />Code:<br /> 0: aload_0<br /> 1: getfield #1; //Field this$0:LCar;<br /> 4: invokestatic #3; //Car.access$000:(LCar;)V<br /> 7: return<br />}<br />
  136. 136. Inner Classes<br />public class Car extends j.l.Object{<br />public Car();<br />private void move();<br />static void access$000(Car);<br />Code:<br /> 0: aload_0<br /> 1: invokespecial #1; // move: ()V;<br /> 4: return<br />}<br />class Car$Engine extends j.l.Object{<br />final Car this$0;<br />Car$Engine(Car);<br />public void start();<br />Code:<br /> 0: aload_0<br /> 1: getfield #1; //Field this$0:LCar;<br /> 4: invokestatic #3; //Car.access$000:(LCar;)V<br /> 7: return<br />}<br />
  137. 137. “HOW DO THEY DO THAT?” <br />
  138. 138. object Singleton {<br />def test={}<br />}<br />
  139. 139. object Singleton {<br />def test={}<br />}<br />$> scalacSingleton.scala<br />Singleton.class<br />Singleton$.class<br />
  140. 140. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />
  141. 141. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />
  142. 142. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />
  143. 143. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />public final class Singleton$ extends java.lang.Object implements scala.ScalaObject{<br />public static final Singleton$ MODULE$;<br />public static {};<br /> Code:<br /> 0: new #9; //class Singleton$<br /> 3: invokespecial #12; //Method "<init>":()V<br /> 6: return<br />public void test();<br />private Singleton$();<br />}<br />
  144. 144. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />public final class Singleton$ extends java.lang.Object implements scala.ScalaObject{<br />public static final Singleton$ MODULE$;<br />public static {};<br /> Code:<br /> 0: new #9; //class Singleton$<br /> 3: invokespecial #12; //Method "<init>":()V<br /> 6: return<br />public void test();<br />private Singleton$();<br />}<br />
  145. 145. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />public final class Singleton$ extends java.lang.Object implements scala.ScalaObject{<br />public static final Singleton$ MODULE$;<br />public static {};<br /> Code:<br /> 0: new #9; //class Singleton$<br /> 3: invokespecial #12; //Method "<init>":()V<br /> 6: return<br />public void test();<br />private Singleton$();<br />}<br />
  146. 146. public final class Singleton extends java.lang.Object{<br />public static final void test();<br />Code:<br />0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;<br /> 3: invokevirtual #13; //Method Singleton$.test:()V<br /> 6: return<br />}<br />public final class Singleton$ extends java.lang.Object implements scala.ScalaObject{<br />public static final Singleton$ MODULE$;<br />public static {};<br />public void test();<br />private Singleton$();<br /> Code:<br /> 0: aload_0<br /> 1: invokespecial#17; //Method java/lang/Object."<init>":()V<br /> 4: aload_0<br /> 5: putstatic#19; //Field MODULE$:LSingleton$;<br /> 8: return<br />
  147. 147. object Singleton {<br />def test={}<br />}<br />
  148. 148. publicclassSingleton {<br />publicvoid test(){<br />Singleton$.MODULE$.test();<br /> }<br />}<br />object Singleton {<br />def test={}<br />}<br />
  149. 149. publicclassSingleton {<br />publicvoid test(){<br />Singleton$.MODULE$.test();<br /> }<br />}<br />public final class Singleton$ <br />implements scala.ScalaObject{<br />public static final Singleton$ MODULE$;<br /> static { newSingleton$(); }<br />private Singleton$(){<br />MODULE$ = this;<br /> }<br />public void test() { <br /> }<br />}<br />object Singleton {<br />def test={}<br />}<br />
  150. 150. classGroovy {}<br />
  151. 151. classGroovy {}<br />$> groovycGroovy.groovy<br />$> javap –c –p Groovy<br />
  152. 152. classGroovy {}<br />$> groovycGroovy.groovy<br />$> javap –c –p Groovy<br />public class Test extends java.lang.Object implements groovy.lang.GroovyObject{<br /> private static org.codehaus.groovy.reflection.ClassInfo $staticClassInfo;<br /> private transient groovy.lang.MetaClass metaClass;<br /> public static java.lang.Long __timeStamp;<br /> public static java.lang.Long __timeStamp__239_neverHappen1304807931117;<br /> private static java.lang.ref.SoftReference $callSiteArray;<br /> private static java.lang.Class $class$groovy$lang$MetaClass;<br /> private static java.lang.Class $class$Test;<br /> private static java.lang.Class $class$java$lang$String;<br />public java.lang.Object this$dist$invoke$2(java.lang.String, java.lang.Object);<br /> public void this$dist$set$2(java.lang.String, java.lang.Object);<br /> public java.lang.Object this$dist$get$2(java.lang.String);<br /> protected groovy.lang.MetaClass $getStaticMetaClass();<br /> public groovy.lang.MetaClass getMetaClass();<br /> public void setMetaClass(groovy.lang.MetaClass);<br /> public java.lang.Object invokeMethod(java.lang.String, java.lang.Object);<br /> public java.lang.Object getProperty(java.lang.String);<br /> public void setProperty(java.lang.String, java.lang.Object);<br />
  153. 153. CRASH! BOOM! BANG!<br />
  154. 154. Javassist<br />Bytecode manipulation made easy<br />Source-level and bytecode-level API<br />Uses the vocabulary of Java language<br />On-the-fly compilation of the injected code<br />http://www.jboss.org/javassist<br />
  155. 155. for(inti = 0; i < 100; i++){ <br />inta = 0;<br /> try { <br /> while (true) {<br /> a++;<br /> foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}<br />
  156. 156. for(inti = 0; i < 100; i++){ <br />inta = 0;<br />try { <br /> while (true) {<br /> a++;<br /> foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}<br />
  157. 157. for(inti = 0; i < 100; i++){ <br />inta = 0;<br />try{ <br />while (true) {<br /> a++;<br /> foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}<br />
  158. 158. for(inti = 0; i < 100; i++){ <br />inta = 0;<br />try{ <br />while(true) {<br />a++;<br /> foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}<br />
  159. 159. for(inti = 0; i < 100; i++){ <br />inta = 0;<br />try{ <br />while(true) {<br /> a++;<br />foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}<br />
  160. 160. Javassist<br />CtMethod method = …<br />method.setBody(“<br />for(inti = 0; i < 100; i++){ <br />inta = 0;<br /> try { <br /> while (true) {<br /> a++;<br /> foo(a);<br /> if(a > 1) break;<br /> } <br /> } catch (Exception e) { <br /> }<br />}”);<br />
  161. 161. -noverify<br />
  162. 162. #<br /># A fatal error has been detected by the Java Runtime Environment:<br />#<br /># EXCEPTION_ACCESS_VIOLATION (0xc0000005) at <br /># pc=0x01adff06, pid=7732, tid=868<br />#<br /># JRE version: 6.0_21-b07<br /># Java VM: Java HotSpot(TM) Client VM (17.0-b17 mixed mode, <br /># sharing windows-x86 )<br /># Problematic frame:<br /># j zt.javassist.My.test()V+20<br />#<br /># An error report file with more information is saved as:<br /># C:workpuzzlershs_err_pid7732.log<br />#<br /># If you would like to submit a bug report, please visit:<br /># http://java.sun.com/webapps/bugreport/crash.jsp<br />#<br />-noverify<br />
  163. 163. Exception in thread "main" java.lang.VerifyError: (class: zt/javassist/My, method: test signature: ()V) Inconsistent stack height 0 != 1<br />-Xverify:all<br />
  164. 164. public void test();<br /> Code:<br /> Stack=2, Locals=4, Args_size=1<br /> 0: iconst_0<br /> 1: istore_1<br /> 2: iload_1<br /> 3: bipush 100<br /> 5: if_icmpge 42<br /> 8: iconst_0<br /> 9: istore_2<br /> 10: goto 29<br /> 13: iinc 2, 1<br /> 16: aload_0<br /> 17: iload_2<br /> 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V<br /> 21: iload_2<br /> 22: iconst_1<br /> 23: if_icmple 29<br /> 26: goto 32<br /> 29: goto 13<br /> 32: astore_3<br /> 33: goto 36<br /> 36: iinc 1, 1<br /> 39: goto 2<br /> 42: return<br /> Exception table:<br /> from to target type<br /> 10 32 32 Class java/lang/Exception<br />
  165. 165. public void test();<br /> Code:<br /> Stack=2, Locals=4, Args_size=1<br /> 0: iconst_0<br /> 1: istore_1<br /> 2: iload_1<br /> 3: bipush 100<br /> 5: if_icmpge 42<br /> 8: iconst_0<br /> 9: istore_2<br /> 10: goto 29<br /> 13: iinc 2, 1<br /> 16: aload_0<br /> 17: iload_2<br /> 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V<br /> 21: iload_2<br /> 22: iconst_1<br /> 23: if_icmple 29<br /> 26: goto 32<br /> 29: goto 13<br /> 32: astore_3<br /> 33: goto 36<br /> 36: iinc 1, 1<br /> 39: goto 2<br /> 42: return<br /> Exception table:<br /> from to target type<br /> 10 32 32 Class java/lang/Exception<br />No local <br />variables table<br />
  166. 166. public void test();<br /> Code:<br /> Stack=2, Locals=4, Args_size=1<br /> 0: iconst_0<br /> 1: istore_1<br /> 2: iload_1<br /> 3: bipush 100<br /> 5: if_icmpge 42<br /> 8: iconst_0<br /> 9: istore_2<br /> 10: goto 29<br /> 13: iinc 2, 1<br /> 16: aload_0<br /> 17: iload_2<br /> 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V<br /> 21: iload_2<br /> 22: iconst_1<br /> 23: if_icmple 29<br /> 26: goto 32<br /> 29: goto 13<br /> 32: astore_3<br /> 33: goto 36<br /> 36: iinc 1, 1<br /> 39: goto 2<br /> 42: return<br /> Exception table:<br /> from to target type<br /> 10 32 32 Class java/lang/Exception<br />No local <br />variables table<br />
  167. 167. Q/A<br />

×