Java Bytecode for Discriminating Developers - JavaZone 2011
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
7,483
On Slideshare
2,932
From Embeds
4,551
Number of Embeds
51

Actions

Shares
Downloads
99
Comments
0
Likes
6

Embeds 4,551

http://arhipov.blogspot.com 3,596
http://arhipov.blogspot.in 194
http://arhipov.blogspot.de 136
http://feeds.feedburner.com 90
http://arhipov.blogspot.co.uk 90
http://arhipov.blogspot.fr 49
http://arhipov.blogspot.ru 43
http://arhipov.blogspot.ca 32
http://arhipov.blogspot.com.es 28
http://lanyrd.com 25
http://arhipov.blogspot.com.au 24
http://arhipov.blogspot.it 24
http://arhipov.blogspot.se 19
http://arhipov.blogspot.com.br 18
http://arhipov.blogspot.jp 16
http://arhipov.blogspot.co.il 14
http://arhipov.blogspot.cz 13
http://paper.li 11
http://arhipov.blogspot.ch 10
http://arhipov.blogspot.co.at 10
http://twitter.com 8
http://arhipov.blogspot.dk 8
http://arhipov.blogspot.pt 7
http://arhipov.blogspot.co.nz 7
http://arhipov.blogspot.be 7
http://arhipov.blogspot.ro 7
http://arhipov.blogspot.nl 7
http://arhipov.blogspot.mx 5
http://arhipov.blogspot.com.ar 5
http://arhipov.blogspot.hk 5
http://arhipov.blogspot.kr 4
http://arhipov.blogspot.tw 4
http://arhipov.blogspot.hu 4
http://www.arhipov.blogspot.ru 3
http://arhipov.blogspot.fi 3
http://arhipov.blogspot.sk 2
http://arhipov.blogspot.no 2
http://arhipov.blogspot.sg 2
http://www.arhipov.blogspot.com 2
http://arhipov.blogspot.com.tr 2
http://us-w1.rockmelt.com 2
http://www.hanrss.com 2
http://webcache.googleusercontent.com 2
http://arhipov.blogspot.ie 2
http://reader.aol.com 1
http://feedproxy.google.com 1
http://hghltd.yandex.net 1
http://tweetedtimes.com 1
https://twitter.com 1
http://localhost 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Java Bytecode forDiscriminating Developers JavaZone’11, Oslo
  • 2. whoamiAnton ArhipovJava dev. / Product LeadJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  • 3. Java… Do you speak it!?
  • 4. Java… Do you speak it!?
  • 5. The Master Plan Examples Bytecode 101
  • 6. THE INTRO
  • 7. 1+2
  • 8. +1+2 1 2
  • 9. +1+2 1 212+
  • 10. +1+2 1 212+
  • 11. +1+2 1 212+ PUSH 1 1
  • 12. +1+2 1 212+ PUSH 1 PUSH 2 2 1
  • 13. +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
  • 14. +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
  • 15. ?=1+2
  • 16. TAXONOMY
  • 17. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  • 18. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  • 19. TYPE OPERATION
  • 20. TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, a
  • 21. TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)
  • 22. TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)Local variables and stack interaction (load/store)Array operations (aload, astore)Math (add, sub, mul, div)Boolean/bitwise operations (iand, ixor)Comparisons (cmpg, cmpl, ifne, ifeq)Conversions (l2d, i2l)
  • 23. Bytecode Taxonomy Stack Manipulation
  • 24. Bytecode Taxonomy Stack Flow Manipulation Control
  • 25. Bytecode Taxonomy Stack Flow Manipulation Control Object Model
  • 26. Bytecode Taxonomy Stack Flow Manipulation Control Object Arithmetics Model
  • 27. Bytecode Taxonomy Stack Flow Manipulation Control monitorenter monitorexit Object Arithmetics Model
  • 28. TOOLING
  • 29. javap -c -verbose Clazz
  • 30. HELLO, WORLD!
  • 31. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); }}
  • 32. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c}
  • 33. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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
  • 34. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: the default constructor 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
  • 35. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: push this to stack 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
  • 36. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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 invoke <init> on this
  • 37. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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
  • 38. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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
  • 39. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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 get static field 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
  • 40. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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 load string to the stack
  • 41. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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 invoke method with parameter
  • 42. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>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
  • 43. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c What’s #1,#2, etc ?} 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
  • 44. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
  • 45. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
  • 46. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -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;
  • 47. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -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;
  • 48. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -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;
  • 49. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -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;
  • 50. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -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;
  • 51. STACKMACHINE
  • 52. Stack Machine
  • 53. Stack Machine JVM is a stack-based machine
  • 54. Stack Machine JVM is a stack-based machine Each thread has a stack
  • 55. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames
  • 56. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation
  • 57. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation Frame consists of: Operand stack Array of local variables
  • 58. Local variables0 1 2 … NOperand stack #1 Constant Pool
  • 59. public class Get { String name; public String getName() { return name; }}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;
  • 60. public class Get { String name; public String getName() { return name; }}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;
  • 61. 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;
  • 62. 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;
  • 63. STACKJUGGLING
  • 64. dup Apop Bswapdup_x1dup2_x1
  • 65. dup Apop Aswap Bdup_x1dup2_x1
  • 66. dup Apop Bswapdup_x1dup2_x1
  • 67. dup Bpop Aswapdup_x1dup2_x1
  • 68. dup Bpop Aswap Bdup_x1dup2_x1
  • 69. dup Bpop Aswap Bdup_x1 Bdup2_x1 A
  • 70. How do youswap doubles?
  • 71. dup2_x2
  • 72. dup2_x2 dconst_0 0.0
  • 73. dup2_x2 dconst_0 dconst_1 1.0 0.0
  • 74. dup2_x2 dconst_0 dconst_1 1.0 swap 0.0
  • 75. dup2_x2 dconst_0 dconst_1 1.0 swap not allowed! 0.0
  • 76. dup2_x2 dconst_0 dconst_1 1.0 swap2 0.0
  • 77. dup2_x2 dconst_0 dconst_1 1.0 doesn’t swap2 exist 0.0
  • 78. dup2_x2 dconst_0 dconst_1 1.0 dup2_x2 0.0 1.0
  • 79. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0
  • 80. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0 profit! 
  • 81. LOCALVARIABLES
  • 82. Local Variablespublic int calculate(int value) { return value + 42;}
  • 83. Local Variablespublic int calculate(int value) { return value + 42;}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
  • 84. Local Variablespublic int calculate(int value) { return value + 42;}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
  • 85. Local Variablespublic int calculate(int value) { return value + 42;}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
  • 86. Local Variablespublic int calculate(int value) { return value + 42;} 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
  • 87. Local Variablespublic int calculate(int value) { return value + 42;}public 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
  • 88. Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  • 89. Local Variables Stackvar value depth value ldc "Hello"0 0 "Hello" astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  • 90. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  • 91. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 1 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  • 92. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
  • 93. 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
  • 94. load LocalVariables Stack Table store
  • 95. OBJECTS
  • 96. Object Initializationnew 0xBB <init> Instance initialization method <clinit> Class and interface initialization method
  • 97. Object Initialization: static {}public class Initializer { static int a; static int b; static { a = 1; } static { b = 2; } static {}; Code:} 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  • 98. Object Initialization: static {}public class Initializer { static int a; static int b; <clinit> static { a = 1; } static { b = 2; } static {}; Code:} 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  • 99. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); } }
  • 100. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: }
  • 101. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 }
  • 102. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V
  • 103. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0
  • 104. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();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
  • 105. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();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>":()V12: putfield #3; //Field o:Ljava/lang/Object;
  • 106. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();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>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  • 107. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();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>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  • 108. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  • 109. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  • 110. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  • 111. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  • 112. Object Initialization: {}public class Initializer { int a; public Initializer(int); int b; Code: int c; 0: aload_0 1: invokespecial #1; // ..<init> 4: aload_0 { a = 1; } 5: iconst_1 6: putfield #2; //Field a:I public Initializer(int b) { 9: aload_0 this.b = b; 10: iconst_2 } 11: putfield #3; //Field c:I 14: aload_0 { c = 2; } 15: iload_1} 16: putfield #4; //Field b:I 19: return
  • 113. There’s no initializer
  • 114. METHOD INVOCATION & PARAMETER PASSINGMETHODINVOCATION
  • 115. Java Method Invocation
  • 116. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamic
  • 117. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface Integer.valueOf(“42”) invokedynamic
  • 118. Java Method Invocation invokestatic invokespecial <init> invokevirtual invokeinterface invokedynamic private void foo(); super.method();
  • 119. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface invokedynamic
  • 120. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B invokedynamic
  • 121. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B A/method1 invokedynamic B/method2 B/method3
  • 122. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX
  • 123. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX class D impl X D/method1 X/methodX
  • 124. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX class D impl X D/method1 X/methodX
  • 125. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamicEfficient Implementation of Java Interfaces:Invokeinterface Considered Harmless, BowenAlpern, Anthony Cocchi, Stephen Fink, DavidGrove, and Derek Lieber, OOPSLA’01
  • 126. Method Invocationobj.method(param1, param2);
  • 127. Method Invocationobj.method(param1, param2); push obj push param1 push param2 call method
  • 128. Method Invocationobj.method(param1, param2); obj push obj push param1 push param2 call method
  • 129. Method Invocationobj.method(param1, param2); param1 push obj obj push param1 push param2 call method
  • 130. Method Invocationobj.method(param1, param2); param2 push obj param1 push param1 obj push param2 call method
  • 131. Method Invocationobj.method(param1, param2); obj? push obj push param1 push param2 call method
  • 132. INNERCLASSES
  • 133. Inner Classes public class Car { class Engine { public void start() { move(); } } private void move() { } }
  • 134. Inner Classesclass Car$Engine extends j.l.Object{final Car this$0;Car$Engine(Car);public void start(); Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  • 135. Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{ static void access$000(Car);final Car this$0; Code: 0: aload_0Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: returnpublic void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  • 136. Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{ static void access$000(Car);final Car this$0; Code: 0: aload_0Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: returnpublic void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  • 137. “HOW DO THEY DO THAT?”
  • 138. object Singleton { def test={}}
  • 139. object Singleton { def test={} } $> scalac Singleton.scalaSingleton.class Singleton$.class
  • 140. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
  • 141. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
  • 142. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
  • 143. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  • 144. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  • 145. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  • 146. public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {};public void test();private Singleton$();Code: 0: aload_0 1: invokespecial #17; //Method java/lang/Object."<init>":()V 4: aload_0 5: putstatic #19; //Field MODULE$:LSingleton$; 8: return
  • 147. object Singleton { def test={}}
  • 148. public class Singleton { public void test(){ Singleton$.MODULE$.test(); } }object Singleton { def test={}}
  • 149. public class Singleton { public void test(){ Singleton$.MODULE$.test(); } } public final class Singleton$object Singleton { implements scala.ScalaObject { def test={} public static final Singleton$ MODULE$;} static { new Singleton$(); } private Singleton$(){ MODULE$ = this; } public void test() { } }
  • 150. CRASH!BOOM!BANG!
  • 151. Javassist Bytecode manipulation made easy Source-level and bytecode-level API Uses the vocabulary of Java language On-the-fly compilation of the injected code http://www.jboss.org/javassist
  • 152. for(int i = 0; i < 100; i++){ int a = 0; try { while (true) { a++; foo(a); if(a > 1) break; } } catch (Exception e) { }}
  • 153. JavassistCtMethod method = …method.setBody(“ for(int i = 0; i < 100; i++){ int a = 0; try { while (true) { a++; foo(a); if(a > 1) break; } } catch (Exception e) { } }”);
  • 154. -Xverify:allException in thread "main" java.lang.VerifyError:(class: zt/javassist/My, method: test signature:()V) Inconsistent stack height 0 != 1
  • 155. public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 22: iconst_1 23: if_icmple 29 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
  • 156. public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 No local 22: iconst_1 23: if_icmple 29 variables table 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
  • 157. public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 No local 22: iconst_1 23: if_icmple 29 variables table 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
  • 158. Bytecode is fun! Know your platform! Maybe, you can build your own language? You may need to read bytecode someday.
  • 159. http://arhipov.blogspot.com ant.arhipov@gmail.com @antonarhipov @javarebel