LatJUG. Java Bytecode Fundamentals
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

LatJUG. Java Bytecode Fundamentals

on

  • 1,877 views

Java Bytecode Fundamentals

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

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

Statistics

Views

Total Views
1,877
Views on SlideShare
1,816
Embed Views
61

Actions

Likes
6
Downloads
68
Comments
0

5 Embeds 61

http://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 47
http://www.jug.lv 7
https://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 3
http://jug.lv 3
http://jugpreview.tumblr.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

LatJUG. Java Bytecode Fundamentals Presentation Transcript

  • 1. Java BytecodeFundamentals JUG.LV 2011, Riga
  • 2. whoamiAnton ArhipovZeroTurnaroundJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  • 3. who-are-you?
  • 4. 1+2
  • 5. +1+2 1 2
  • 6. +1+2 1 212+
  • 7. +1+2 1 212+
  • 8. +1+2 1 212+ PUSH 1 1
  • 9. +1+2 1 212+ PUSH 1 PUSH 2 2 1
  • 10. +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
  • 11. +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
  • 12. ?=1+2
  • 13. Byte Code One-byte instructions 256 possible opcodes ~200 in use
  • 14. Byte Code One-byte instructions 256 possible opcodes ~200 in use
  • 15. The Master Plan javap Stack Machine Objects and Methods Flow Control
  • 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. 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. 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. 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. 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. 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. Frame
  • 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. 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. 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. 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. 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. 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. Stack Operations dup A pop B swap dup_x1 dup_x2
  • 30. Stack Operations dup A pop A swap B dup_x1 dup_x2
  • 31. Stack Operations dup A pop B swap dup_x1 dup_x2
  • 32. Stack Operations dup B pop A swap dup_x1 dup_x2
  • 33. Stack Operations dup B pop A swap B dup_x1 dup_x2
  • 34. Stack Operations dup B pop A swap B dup_x1 B dup_x2 A
  • 35. Local Variables
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Local Variables & Stack load Local Variables Stack Table store
  • 48. Object Initializationnew <init> Instance initialization method <clinit> Class and interface initialization method
  • 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. 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. 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. 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. Object Initialization: {}
  • 54. Object Initialization: {}
  • 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. Method Invocation invokestatic invokeinterface invokevirtual invokespecial invokedynamic
  • 57. Parameter Passing
  • 58. Parameter Passing parameter return value
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Flow Control
  • 76. Flow Control GOTO
  • 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. 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. 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. 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. 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. 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. 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. 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. ant.arhipov@gmail.comhttp://arhipov.blogspot.com@antonarhipov@javarebel