Java Bytecode forDiscriminating Developers      JavaZone’11, Oslo
whoamiAnton ArhipovJava dev. / Product LeadJRebel                           http://arhipov.blogspot.com                   ...
Java… Do you speak it!?
Java… Do you speak it!?
The Master Plan           Examples          Bytecode 101
THE INTRO
1+2
+1+2      1       2
+1+2      1       212+
+1+2      1       212+
+1+2           1           212+   PUSH 1                   1
+1+2           1           212+   PUSH 1      PUSH 2                   2                   1
+1+2           1           212+   PUSH 1      PUSH 2                   3      ADD
+1+2           1             212+   ICONST_1      ICONST_2                     3      IADD
?=1+2
TAXONOMY
Bytecode  One-byte instructions  256 possible opcodes  200+ in use
Bytecode  One-byte instructions  256 possible opcodes  200+ in use
TYPE OPERATION
TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, a
TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)
TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)Local variables and stack in...
Bytecode Taxonomy           Stack         Manipulation
Bytecode Taxonomy           Stack         Flow         Manipulation   Control
Bytecode Taxonomy           Stack         Flow         Manipulation   Control                        Object               ...
Bytecode Taxonomy           Stack         Flow         Manipulation   Control                        Object         Arithm...
Bytecode Taxonomy           Stack                    Flow         Manipulation              Control                    mon...
TOOLING
javap -c -verbose Clazz
HELLO, WORLD!
public class Hello {  public static void main(String[] args) {    System.out.println ( “Hello, World!” );  }}
public class Hello {  public static void main(String[] args) {    System.out.println ( “Hello, World!” );  }    C:workgeec...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello {  public static void main(String[] args) {    System.out.println ( “Hello, World!” );  }    C:workgeec...
public class Hello {  public static void main(String[] args) {    System.out.println ( “Hello, World!” );  }    C:workgeec...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
public class Hello { public static void main(String[] args) {   System.out.println ( “Hello, World!” ); }   C:workgeeconcl...
STACKMACHINE
Stack Machine
Stack Machine  JVM is a stack-based machine
Stack Machine  JVM is a stack-based machine  Each thread has a stack
Stack Machine  JVM is a stack-based machine  Each thread has a stack  Stack stores frames
Stack Machine  JVM is a stack-based machine  Each thread has a stack  Stack stores frames  Frame is created on method  inv...
Stack Machine  JVM is a stack-based machine  Each thread has a stack  Stack stores frames  Frame is created on method  inv...
Local variables0 1 2      … NOperand stack                  #1                       Constant                         Pool
public class Get { String name;    public String getName() {      return name;    }}public java.lang.String getName(); Cod...
public class Get { String name;    public String getName() {      return name;    }}public java.lang.String getName(); Cod...
0                     1             2         3   4   aload_0 getfield 00 02 areturnpublic java.lang.String getName(); Cod...
0                     1             2         3   4         2A                   B4             00 02          B0public ja...
STACKJUGGLING
dup       Apop       Bswapdup_x1dup2_x1
dup       Apop       Aswap      Bdup_x1dup2_x1
dup       Apop       Bswapdup_x1dup2_x1
dup       Bpop       Aswapdup_x1dup2_x1
dup       Bpop       Aswap      Bdup_x1dup2_x1
dup       Bpop       Aswap      Bdup_x1    Bdup2_x1   A
How do youswap doubles?
dup2_x2
dup2_x2 dconst_0            0.0
dup2_x2 dconst_0 dconst_1   1.0            0.0
dup2_x2 dconst_0 dconst_1   1.0 swap            0.0
dup2_x2 dconst_0 dconst_1                  1.0 swap       not allowed!                           0.0
dup2_x2 dconst_0 dconst_1   1.0 swap2            0.0
dup2_x2 dconst_0 dconst_1             1.0            doesn’t swap2       exist                      0.0
dup2_x2 dconst_0 dconst_1   1.0 dup2_x2            0.0            1.0
dup2_x2 dconst_0 dconst_1   0.0 dup2_x2 pop2       1.0
dup2_x2 dconst_0 dconst_1    0.0 dup2_x2 pop2        1.0 profit! 
LOCALVARIABLES
Local Variablespublic int calculate(int value) {  return value + 42;}
Local Variablespublic int calculate(int value) {  return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, A...
Local Variablespublic int calculate(int value) {  return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, A...
Local Variablespublic int calculate(int value) {  return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, A...
Local Variablespublic int calculate(int value) {  return value + 42;}                                              The tab...
Local Variablespublic int calculate(int value) {  return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, A...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
load  LocalVariables           Stack  Table            store
OBJECTS
Object Initializationnew 0xBB               <init>       Instance initialization method                              <clin...
Object Initialization: static {}public class Initializer { static int a; static int b;    static { a = 1; }    static { b ...
Object Initialization: static {}public class Initializer { static int a; static int b;                                    ...
Object Initialization: new                             public class Initializer {                              Object o;  ...
Object Initialization: new                               public class Initializer {                                Object ...
Object Initialization: new                               public class Initializer {                                Object ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: new                                   public class Initializer {                                   ...
Object Initialization: {}public class Initializer { int a; int b; int c;    { a = 1; }    public Initializer(int b) {     ...
Object Initialization: {}public class Initializer { int a; int b; int c;    { a = 1; }    public Initializer(int b) {     ...
Object Initialization: {}public class Initializer { int a; int b; int c;    { a = 1; }    public Initializer(int b) {     ...
Object Initialization: {}public class Initializer { int a; int b; int c;    { a = 1; }    public Initializer(int b) {     ...
Object Initialization: {}public class Initializer { int a;                           public Initializer(int); int b;      ...
There’s no initializer
METHOD INVOCATION & PARAMETER PASSINGMETHODINVOCATION
Java Method Invocation
Java Method Invocation  invokestatic  invokespecial  invokevirtual  invokeinterface  invokedynamic
Java Method Invocation  invokestatic  invokespecial  invokevirtual  invokeinterface        Integer.valueOf(“42”)  invokedy...
Java Method Invocation  invokestatic  invokespecial                               <init>  invokevirtual  invokeinterface  ...
Java Method Invocation  invokestatic           class A  invokespecial           A/method1                          A/metho...
Java Method Invocation  invokestatic           class A  invokespecial           A/method1                          A/metho...
Java Method Invocation  invokestatic                class A  invokespecial                 A/method1                      ...
Java Method Invocation  invokestatic                class A  invokespecial                 A/method1                      ...
Java Method Invocation  invokestatic                class A  invokespecial                 A/method1                      ...
Java Method Invocation  invokestatic                class A  invokespecial                 A/method1                      ...
Java Method Invocation  invokestatic  invokespecial  invokevirtual  invokeinterface  invokedynamicEfficient Implementation...
Method Invocationobj.method(param1, param2);
Method Invocationobj.method(param1, param2);      push obj      push param1      push param2      call method
Method Invocationobj.method(param1, param2);                              obj      push obj      push param1      push par...
Method Invocationobj.method(param1, param2);                              param1      push obj                            ...
Method Invocationobj.method(param1, param2);                              param2      push obj                            ...
Method Invocationobj.method(param1, param2);                              obj?      push obj      push param1      push pa...
INNERCLASSES
Inner Classes            public class Car {                class Engine {                  public void start() {          ...
Inner Classesclass Car$Engine extends j.l.Object{final Car this$0;Car$Engine(Car);public void start();  Code:  0: aload_0 ...
Inner Classes                                        public class Car extends j.l.Object{                                 ...
Inner Classes                                        public class Car extends j.l.Object{                                 ...
“HOW DO THEY DO THAT?”
object Singleton {  def test={}}
object Singleton {        def test={}      } $> scalac Singleton.scalaSingleton.class       Singleton$.class
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
object Singleton {  def test={}}
public class Singleton {                       public void test(){                         Singleton$.MODULE$.test();     ...
public class Singleton {                       public void test(){                         Singleton$.MODULE$.test();     ...
CRASH!BOOM!BANG!
Javassist  Bytecode manipulation made easy  Source-level and bytecode-level API  Uses the vocabulary of Java language  On-...
for(int i = 0; i < 100; i++){  int a = 0;  try {    while (true) {      a++;      foo(a);      if(a > 1) break;    }  } ca...
JavassistCtMethod method = …method.setBody(“ for(int i = 0; i < 100; i++){                  int a = 0;                  tr...
-Xverify:allException in thread "main" java.lang.VerifyError:(class: zt/javassist/My, method: test signature:()V) Inconsis...
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    ...
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    ...
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    ...
Bytecode is fun!  Know your platform!  Maybe, you can build your own language?  You may need to read bytecode someday.
http://arhipov.blogspot.com    ant.arhipov@gmail.com             @antonarhipov                 @javarebel
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
Upcoming SlideShare
Loading in...5
×

Java Bytecode for Discriminating Developers - JavaZone 2011

7,204

Published on

Published in: Technology, News & Politics
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,204
On Slideshare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
105
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Java Bytecode for Discriminating Developers - JavaZone 2011

  1. 1. Java Bytecode forDiscriminating Developers JavaZone’11, Oslo
  2. 2. whoamiAnton ArhipovJava dev. / Product LeadJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  3. 3. Java… Do you speak it!?
  4. 4. Java… Do you speak it!?
  5. 5. The Master Plan Examples Bytecode 101
  6. 6. THE INTRO
  7. 7. 1+2
  8. 8. +1+2 1 2
  9. 9. +1+2 1 212+
  10. 10. +1+2 1 212+
  11. 11. +1+2 1 212+ PUSH 1 1
  12. 12. +1+2 1 212+ PUSH 1 PUSH 2 2 1
  13. 13. +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
  14. 14. +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
  15. 15. ?=1+2
  16. 16. TAXONOMY
  17. 17. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  18. 18. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  19. 19. TYPE OPERATION
  20. 20. TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, a
  21. 21. TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)
  22. 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. 23. Bytecode Taxonomy Stack Manipulation
  24. 24. Bytecode Taxonomy Stack Flow Manipulation Control
  25. 25. Bytecode Taxonomy Stack Flow Manipulation Control Object Model
  26. 26. Bytecode Taxonomy Stack Flow Manipulation Control Object Arithmetics Model
  27. 27. Bytecode Taxonomy Stack Flow Manipulation Control monitorenter monitorexit Object Arithmetics Model
  28. 28. TOOLING
  29. 29. javap -c -verbose Clazz
  30. 30. HELLO, WORLD!
  31. 31. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); }}
  32. 32. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c}
  33. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 44. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
  45. 45. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
  46. 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. 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. 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. 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. 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. 51. STACKMACHINE
  52. 52. Stack Machine
  53. 53. Stack Machine JVM is a stack-based machine
  54. 54. Stack Machine JVM is a stack-based machine Each thread has a stack
  55. 55. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames
  56. 56. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation
  57. 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. 58. Local variables0 1 2 … NOperand stack #1 Constant Pool
  59. 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. 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. 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. 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. 63. STACKJUGGLING
  64. 64. dup Apop Bswapdup_x1dup2_x1
  65. 65. dup Apop Aswap Bdup_x1dup2_x1
  66. 66. dup Apop Bswapdup_x1dup2_x1
  67. 67. dup Bpop Aswapdup_x1dup2_x1
  68. 68. dup Bpop Aswap Bdup_x1dup2_x1
  69. 69. dup Bpop Aswap Bdup_x1 Bdup2_x1 A
  70. 70. How do youswap doubles?
  71. 71. dup2_x2
  72. 72. dup2_x2 dconst_0 0.0
  73. 73. dup2_x2 dconst_0 dconst_1 1.0 0.0
  74. 74. dup2_x2 dconst_0 dconst_1 1.0 swap 0.0
  75. 75. dup2_x2 dconst_0 dconst_1 1.0 swap not allowed! 0.0
  76. 76. dup2_x2 dconst_0 dconst_1 1.0 swap2 0.0
  77. 77. dup2_x2 dconst_0 dconst_1 1.0 doesn’t swap2 exist 0.0
  78. 78. dup2_x2 dconst_0 dconst_1 1.0 dup2_x2 0.0 1.0
  79. 79. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0
  80. 80. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0 profit! 
  81. 81. LOCALVARIABLES
  82. 82. Local Variablespublic int calculate(int value) { return value + 42;}
  83. 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. 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. 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. 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. 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. 88. Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  89. 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. 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. 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. 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. 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. 94. load LocalVariables Stack Table store
  95. 95. OBJECTS
  96. 96. Object Initializationnew 0xBB <init> Instance initialization method <clinit> Class and interface initialization method
  97. 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. 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. 99. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); } }
  100. 100. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: }
  101. 101. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 }
  102. 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. 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. 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. 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. 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. 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. 108. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  109. 109. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  110. 110. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  111. 111. Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
  112. 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. 113. There’s no initializer
  114. 114. METHOD INVOCATION & PARAMETER PASSINGMETHODINVOCATION
  115. 115. Java Method Invocation
  116. 116. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamic
  117. 117. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface Integer.valueOf(“42”) invokedynamic
  118. 118. Java Method Invocation invokestatic invokespecial <init> invokevirtual invokeinterface invokedynamic private void foo(); super.method();
  119. 119. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface invokedynamic
  120. 120. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B invokedynamic
  121. 121. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B A/method1 invokedynamic B/method2 B/method3
  122. 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. 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. 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. 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. 126. Method Invocationobj.method(param1, param2);
  127. 127. Method Invocationobj.method(param1, param2); push obj push param1 push param2 call method
  128. 128. Method Invocationobj.method(param1, param2); obj push obj push param1 push param2 call method
  129. 129. Method Invocationobj.method(param1, param2); param1 push obj obj push param1 push param2 call method
  130. 130. Method Invocationobj.method(param1, param2); param2 push obj param1 push param1 obj push param2 call method
  131. 131. Method Invocationobj.method(param1, param2); obj? push obj push param1 push param2 call method
  132. 132. INNERCLASSES
  133. 133. Inner Classes public class Car { class Engine { public void start() { move(); } } private void move() { } }
  134. 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. 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. 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. 137. “HOW DO THEY DO THAT?”
  138. 138. object Singleton { def test={}}
  139. 139. object Singleton { def test={} } $> scalac Singleton.scalaSingleton.class Singleton$.class
  140. 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. 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. 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. 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. 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. 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. 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. 147. object Singleton { def test={}}
  148. 148. public class Singleton { public void test(){ Singleton$.MODULE$.test(); } }object Singleton { def test={}}
  149. 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. 150. CRASH!BOOM!BANG!
  151. 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. 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. 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. 154. -Xverify:allException in thread "main" java.lang.VerifyError:(class: zt/javassist/My, method: test signature:()V) Inconsistent stack height 0 != 1
  155. 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. 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. 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. 158. Bytecode is fun! Know your platform! Maybe, you can build your own language? You may need to read bytecode someday.
  159. 159. http://arhipov.blogspot.com ant.arhipov@gmail.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.

×