java bytecode for discriminating developers - javazone 2011

Download Java Bytecode for Discriminating Developers - JavaZone 2011

Post on 10-May-2015

7.162 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1.Java Bytecode forDiscriminating DevelopersJavaZone11, 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 ExamplesBytecode 101 6. THE INTRO 7. 1+2 8. +1+21 2 9. +1+21 212+ 10. +1+21 212+ 11. +1+2 1 212+ PUSH 1 1 12. +1+2 1 212+ PUSH 1PUSH 2 2 1 13. +1+2 1 212+ PUSH 1PUSH 2 3ADD 14. +1+2 1 212+ ICONST_1ICONST_2 3IADD 15. ?=1+2 16. TAXONOMY 17. BytecodeOne-byte instructions256 possible opcodes200+ in use 18. BytecodeOne-byte instructions256 possible opcodes200+ in use 19. TYPE OPERATION 20. TYPE OPERATION ::= b, s, c, i, l, f, d, a 21. TYPE OPERATION ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1) 22. TYPE OPERATION ::= 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 ControlObjectModel 26. Bytecode Taxonomy Stack Flow Manipulation ControlObject ArithmeticsModel 27. Bytecode Taxonomy StackFlow ManipulationControlmonitorentermonitorexit 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."":()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."":()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."":()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."":()V 4: returninvoke 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."":()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."":()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."":()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."":()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."":()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."":()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 -cWhats #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."":()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.ObjectSourceFile: "Hello.java"minor version: 0major version: 50Constant pool: const #1 = Method #6.#20; // java/lang/Object."":()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 ; 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.ObjectSourceFile: "Hello.java"minor version: 0major version: 50Constant pool: const #1 = Method #6.#20; // java/lang/Object."":()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 ; 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=10: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnLineNumberTable:line 1: 0LocalVariableTable:Start Length Slot Name Signature050 thisLHello; 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=10: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnLineNumberTable:line 1: 0LocalVariableTable:Start Length Slot Name Signature050 thisLHello; 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=10: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnLineNumberTable:line 1: 0LocalVariableTable:Start Length Slot Name Signature050 thisLHello; 51. STACKMACHINE 52. Stack Machine 53. Stack MachineJVM is a stack-based machine 54. Stack MachineJVM is a stack-based machineEach thread has a stack 55. Stack MachineJVM is a stack-based machineEach thread has a stackStack stores frames 56. Stack MachineJVM is a stack-based machineEach thread has a stackStack stores framesFrame is created on methodinvocation 57. Stack MachineJVM is a stack-based machineEach thread has a stackStack stores framesFrame is created on methodinvocationFrame consists of:Operand stackArray of local variables 58. Local variables0 1 2 NOperand stack#