Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java bytecode fundamentals

Java bytecode fundamentals

Anton Arhipov

March 03, 2011
Tweet

More Decks by Anton Arhipov

Other Decks in Programming

Transcript

  1. 1 + 2 1 2 + 1 2 + PUSH

    1 PUSH 2 2 1
  2. 1 + 2 1 2 + 1 2 + PUSH

    1 PUSH 2 ADD 3
  3. 1 + 2 1 2 + 1 2 + ICONST_1

    ICONST_2 IADD 3
  4. 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
  5. C:\work\jug\classes>javap Hello -c Compiled 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: return public 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
  6. C:\work\jug\classes>javap Hello -verbose Compiled 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>":()V const #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;)V const #5 = class #26; // Hello const #6 = class #27; // java/lang/Object const #7 = Asciz <init>; const #8 = Asciz ()V;
  7. C:\work\jug\classes>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;
  8. C:\work\jug\classes>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;
  9. 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
  10. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  11. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet; aload_0 getfield 00 02 areturn 0 1 2 3 4
  12. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet; 2A B4 00 02 B0 0 1 2 3 4
  13. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  14. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  15. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  16. Local Variables public 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
  17. Local Variables public 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
  18. Local Variables public 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
  19. Local Variables public 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 The table maps numbers to names
  20. Local Variables public 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 Sized explicitly
  21. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables 0 1 2 3 4 0 1 2 3 4
  22. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables "Hello" 0 1 2 3 4 0 1 2 3 4
  23. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables "Hello" 0 1 2 3 4 0 1 2 3 4
  24. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables "Hello" 0 1 2 3 4 0 1 2 3 4 1
  25. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables "Hello" 0 1 2 3 4 0 1 2 3 4 1
  26. Local Variables ldc "Hello" astore_0 iconst_1 astore_1 aload_0 depth value

    Stack var value Local Variables "Hello" 0 1 2 3 4 0 1 2 3 4 1 "Hello"
  27. 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
  28. 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 <clinit>
  29. Object Initialization: new public 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>":()V 12: putfield #3; //Field o:Ljava/lang/Object; 15: return
  30. Object Initialization: new public 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>":()V 12: putfield #3; //Field o:Ljava/lang/Object; 15: return
  31. 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
  32. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables Stack 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute();
  33. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); this Stack
  34. 0: aload_0 1: invokespecial #2; //createRandomValue() 4: astore_1 5: aload_0

    6: aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref Stack
  35. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref Stack
  36. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); this objectref Stack
  37. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); this objectref objectref Stack
  38. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref Stack
  39. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref objectref Stack
  40. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; // Integer.intValue:() 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref X Stack
  41. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref 1 X Stack
  42. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref X + 1 Stack
  43. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; //Integer.valueOf 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref objectref Stack
  44. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; //incValue 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref objectref Stack
  45. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref objectref Stack
  46. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref Stack
  47. 0: aload_0 1: invokespecial #2; 4: astore_1 5: aload_0 6:

    aload_1 7: invokespecial #3; 10: areturn depth value 1 2 3 4 5 6 1 2 3 var value Local Variables 1 2 3 var value 0: aload_1 1: invokevirtual #8; 4: iconst_1 5: iadd 6: invokestatic #7; 9: areturn private java.lang.Integer incValue(java.lang.Integer); public java.lang.Object execute(); objectref Stack
  48. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack
  49. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack a
  50. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack 10 a
  51. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack 10 a
  52. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack
  53. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack 100
  54. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack 100
  55. public int decide(int); Code: 0: iload_1 1: bipush 10 3:

    if_icmpge 8 6: iconst_0 7: ireturn 8: bipush 100 10: ireturn 1 2 3 depth value Stack