2. Alexander Shopov
By day: Software Engineer at Cisco
By night: OSS contributor
Coordinator of Bulgarian Gnome TP
Contacts:
E-mail: ash@kambanaria.org
Jabber: al_shopov@jabber.minus273.org
LinkedIn: http://www.linkedin.com/in/alshopov
Google: Just search “al_shopov“
5. Contents
● Why read?
● How to read?
● JVM Internals;
● JVM Data Types;
● JVM Opcodes.
● Let's read some code.
● What next?
6. Why Read Byte code?
● Understand your platform
● It is interesting and not too hard
● How does Java function? How does X function?
● Job interviews
● Catch compiler bugs/optimizations
● Learn to read before you write
● Source may not correspond to binary
● C/C++ people know their assembler
● Java language evolution vs. Java platform evolution
7. Bad News And Good News
Bad: Good:
We will be Easiest
reading assembler
assembler in world
8. What Is The JVM?
● Stack based, byte oriented virtual machine
without registers easily implementable on 32 bit
hardware.
● 206 (<256) instructions that are easy to group
and there is no need to remember them all
● Some leeway in implementations (even with
Oracle)
9. Dramatis Personæ
● The JVM
● The threads
● The frames
● The stacks – LIFO
● The local variables – array of slots
● The runtime constant pool – array of values
● The bytecode – the instructions
● Class files – serialized form of constants and byte
code
17. Enter Stack
0 1 2 3 4 5 6 …
Local variables
F0
Stack
18. Enter Pool Of Constants
0 1 2 3 4 5 6 …
Local variables
F0
Pool of
constants
Stack
19. Where Is The Code?
0 1 2 3 4 5 6 …
Local variables
F0
Pool of
constants
Stack
20. Where Is The Code?
JVM (heap)
0 1 2 3 4 5 6 …
Local variables
F0
Pool of
constants
Stack
21. Where Is The Code?
JVM (heap)
0 1 2 3 4 5 6 … Class
PC
Local variables Method code
F0
Class
Pool of
constants
Stack
22. Where is the code?
JVM (heap)
0 1 2 3 4 5 6 … Class
6
PC
Local variables Method code
F0
Class
Pool of
constants
Stack
23. Load
JVM (heap)
0 1 2 3 4 5 6 … Class
6
PC
Local variables Method code
F0
Class
Pool of
constants
6
Stack
24. And…
JVM (heap)
0 1 2 3 4 5 6 … Class
6
PC
Local variables Method code
F0
Class
Pool of
8 constants
6
Stack
25. Store
JVM (heap)
0 1 2 3 4 5 6 … Class
6 8
Local variables PC Method code
F0
Class
Pool of
8 constants
6
Stack
26. JVM Datatypes
● Primitive types
● Java { numeric – integral: byte (±8), short (±16),
int (±32), long (±64), char (+16), floating point:
float (±32), double (±64); boolean (int or byte) }
● returnAddress – pointers to the opcodes of JVM
(jumps - loops)
● Reference types
● class, array, interface
● null
27. JVM Datatypes Descriptors
Java type Type descriptor
boolean Z
char C
byte B
short S
int I
float F
long J
double D
Object Ljava/lang/Object;
byte[] [B
String[][] [[Ljava/lang/String;
void V
45. Example 1
public static int whatIsThis(int, int, int);
Signature: (III)I
Code:
0: iload_0
1: iload_1
2: iadd
3: istore_3
4: iload_3
5: iload_2
public static int whatIsThis
6: iadd (int a, int b, int c) {
7: istore_3 int result = a + b;
8: iload_3 result += c;
9: ireturn return result;
}
46. Example 2
public static int whatIsThis(int, int, int);
Signature: (III)I
Code:
0: iload_0
1: iload_1
2: iadd
3: iload_2
4: iadd
5: ireturn
47. Example 2
public static int whatIsThis(int, int, int);
Signature: (III)I
Code:
0: iload_0
1: iload_1
2: iadd
3: iload_2
4: iadd
5: ireturn
public static int whatIsThis
(int a, int b, int c) {
result a + b + c;
}
48. Example 3
public static int whatIsThis(int, float, double);
Signature: (IFD)I
Code:
0: iload_0
1: i2f
2: fload_1
3: fadd
4: f2d
5: dload_2
6: dadd
7: d2i
8: ireturn
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 a I
0 9 1 b F
0 9 2 c D
49. Example 3
public static int whatIsThis(int, float, double);
Signature: (IFD)I
Code:
0: iload_0
1: i2f
2: fload_1
3: fadd
4: f2d
5: dload_2
6: dadd
7: d2i
8: ireturn
LineNumberTable:
line 6: 0
public static int whatIsThis
LocalVariableTable: (int a, float b, double c) {
Start Length Slot Name return (int) (a + b + c);
Signature
0 9 0 } I
a
0 9 1 b F
0 9 2 c D
50. Example 4
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
Code:
0: getstatic #16 // Field
java/lang/System.out:Ljava/io/PrintStream;
3: ldc #22 // String BGOUG
5: invokevirtual #24 // Method
java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
66. Further resources
● Oracle:
The JVM Specification, Java SE 7 Edition
● A. Arhipov:
Java Bytecode For Discriminating Developers
● Wikipedia: Java Bytecode Instruction Listings
● S. H. Park Understanding JVM Internals
● C. McGlone:
Looking "Under the Hood" with javap
● P. Haggar: Java bytecode