LatJUG. Java Bytecode Fundamentals

1,620
-1

Published on

Java Bytecode Fundamentals
Anton Arhipov [Estonia]
http://goo.gl/GO6Mm

Java User Group Latvia
March 2011
http://goo.gl/77MhT

0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,620
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
76
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

LatJUG. Java Bytecode Fundamentals

  1. 1. Java BytecodeFundamentals JUG.LV 2011, Riga
  2. 2. whoamiAnton ArhipovZeroTurnaroundJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  3. 3. who-are-you?
  4. 4. 1+2
  5. 5. +1+2 1 2
  6. 6. +1+2 1 212+
  7. 7. +1+2 1 212+
  8. 8. +1+2 1 212+ PUSH 1 1
  9. 9. +1+2 1 212+ PUSH 1 PUSH 2 2 1
  10. 10. +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
  11. 11. +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
  12. 12. ?=1+2
  13. 13. Byte Code One-byte instructions 256 possible opcodes ~200 in use
  14. 14. Byte Code One-byte instructions 256 possible opcodes ~200 in use
  15. 15. The Master Plan javap Stack Machine Objects and Methods Flow Control
  16. 16. javap Java class file disassembler Used with no options shows class structure only Methods, superclass, interfaces, etc -c – shows the bytecode -private – shows all classes and members -s – prints internal types signatures -l – prints lines numbers and local variable tables
  17. 17. C:workjugclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: returnpublic static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
  18. 18. C:workjugclasses>javap Hello -verboseCompiled from "Hello.java“public class Hello extends java.lang.Object SourceFile: "Hello.java" minor version: 0 major version: 50 Constant pool:const #1 = Method #6.#20; // java/lang/Object."<init>":()Vconst #2 = Field #21.#22; // java/lang/System.out:Ljava/io/PrintStream;const #3 = String #23; // Hello, World!const #4 = Method #24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)Vconst #5 = class #26; // Helloconst #6 = class #27; // java/lang/Objectconst #7 = Asciz <init>;const #8 = Asciz ()V;
  19. 19. C:workjugclasses>javap Hello -verbose…public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHello;
  20. 20. C:workjugclasses>javap Hello -verbose…public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 4: 0 line 5: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 args [Ljava/lang/String;
  21. 21. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation Frame: Operand stack Array of local variables
  22. 22. Frame
  23. 23. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  24. 24. 0 1 2 3 4 aload_0 getfield 00 02 areturnpublic java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  25. 25. 0 1 2 3 4 2A B4 00 02 B0public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  26. 26. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  27. 27. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  28. 28. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  29. 29. Stack Operations dup A pop B swap dup_x1 dup_x2
  30. 30. Stack Operations dup A pop A swap B dup_x1 dup_x2
  31. 31. Stack Operations dup A pop B swap dup_x1 dup_x2
  32. 32. Stack Operations dup B pop A swap dup_x1 dup_x2
  33. 33. Stack Operations dup B pop A swap B dup_x1 dup_x2
  34. 34. Stack Operations dup B pop A swap B dup_x1 B dup_x2 A
  35. 35. Local Variables
  36. 36. Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  37. 37. Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  38. 38. Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  39. 39. Local Variables The tablepublic int calculate(int); Code: maps Stack=2, Locals=2, Args_size=2 numbers to … names LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  40. 40. Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 Sized explicitly … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  41. 41. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  42. 42. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 0 "Hello" astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  43. 43. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  44. 44. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 1 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  45. 45. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
  46. 46. Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 "Hello" astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
  47. 47. Local Variables & Stack load Local Variables Stack Table store
  48. 48. Object Initializationnew <init> Instance initialization method <clinit> Class and interface initialization method
  49. 49. Object Initialization: static {} static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  50. 50. Object Initialization: static {} <clinit> static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  51. 51. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  52. 52. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  53. 53. Object Initialization: {}
  54. 54. Object Initialization: {}
  55. 55. Object Initialization: {} public Initializer(int); Code: 0: aload_0 1: invokespecial #1; // ..<init> 4: aload_0 5: iconst_1 6: putfield #2; //Field a:I 9: aload_0 10: iconst_2 11: putfield #3; //Field c:I 14: aload_0 15: iload_1 16: putfield #4; //Field b:I 19: return
  56. 56. Method Invocation invokestatic invokeinterface invokevirtual invokespecial invokedynamic
  57. 57. Parameter Passing
  58. 58. Parameter Passing parameter return value
  59. 59. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  60. 60. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 this 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  61. 61. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2; //createRandomValue()1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  62. 62. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  63. 63. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 this 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  64. 64. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 this 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  65. 65. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  66. 66. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  67. 67. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 X 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; // Integer.intValue:() 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  68. 68. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 1 3 7: invokespecial #3; //incValue 10: areturn 2 X 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  69. 69. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 X+1 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  70. 70. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; //Integer.valueOf 9: areturn
  71. 71. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  72. 72. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  73. 73. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  74. 74. Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
  75. 75. Flow Control
  76. 76. Flow Control GOTO
  77. 77. Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  78. 78. Stack depth valuepublic int decide(int); Code: 0: iload_1 1 a 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  79. 79. Stack depth valuepublic int decide(int); Code: 0: iload_1 1 10 1: bipush 3: if_icmpge 8 10 2 a 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  80. 80. Stack depth valuepublic int decide(int); Code: 0: iload_1 1 10 1: bipush 3: if_icmpge 8 10 2 a 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  81. 81. Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  82. 82. Stack depth valuepublic int decide(int); Code: 0: iload_1 1 100 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  83. 83. Stack depth valuepublic int decide(int); Code: 0: iload_1 1 100 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  84. 84. Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
  85. 85. ant.arhipov@gmail.comhttp://arhipov.blogspot.com@antonarhipov@javarebel
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×