TeamStation AI System Report LATAM IT Salaries 2024
Lifting The Veil – Reading Java Byte Code During Lunchtime
1. Lifting The Veil – Reading Java Byte
Code During Lunchtime
Alexander Shopov
Cisco Lunch&Learn
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. Load
JVM (heap)
0 1 2 3 4 5 6 … Class
6
PC
Local variables Method code
F0
Class
Pool of
constants
6
Stack
25. 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
26. 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
27. 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
28. 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
46. Example 1
JVM (heap)
0 1 2 3 Class
PC 0: iload_0
3 7 4 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
Pool of
constants
Stack
47. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 PC 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
Pool of
3 constants
Stack
48. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 1: iload_1
PC 2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
7 Pool of
3 constants
Stack
49. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 1: iload_1
2: iadd
Local variables PC 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
Pool of
10 constants
Stack
50. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 10 1: iload_1
2: iadd
Local variables 3: istore_3
PC 4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
Pool of
constants
Stack
51. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 10 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
PC 5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
9: ireturn
Pool of
10 constants
Stack
52. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 10 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
PC 6: iadd
7: istore_3
8: iload_3
9: ireturn
4 Pool of
10 constants
Stack
53. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 10 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
PC 7: istore_3
8: iload_3
9: ireturn
Pool of
14 constants
Stack
54. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 14 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
PC 8: iload_3
9: ireturn
Pool of
constants
Stack
55. Example 1
JVM (heap)
0 1 2 3 Class
0: iload_0
3 7 4 14 1: iload_1
2: iadd
Local variables 3: istore_3
4: iload_3
5: iload_2
F0
Class
6: iadd
7: istore_3
8: iload_3
PC 9: ireturn
Pool of
14 constants
Stack
56. 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 //
6: iadd whatIsThis(int a, int b, int c) {
7: istore_3 int result = a + b;
8: iload_3 result += c;
9: ireturn return result;
}
57. 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
58. 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) {
return a + b + c;
}
59. 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
60. 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: public static int //
line 6: 0 whatIsThis(int a, float b, //
LocalVariableTable: double c) {
Start Length Slot Name Signature
0 9 0 a
return (int) (a
I
+ b + c);
0 9 1 b} F
0 9 2 c D
61. Example 4
public static void main(java.lang.String[]);
Code:
0: getstatic #16
// Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #22 // String There
5: invokevirtual #24
// Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
79. Example 9
package org.kambanaria.readbytecode.bgoug;
public class Example9 {
public class Inner {}
public static void //
main(String[] args) throws Exception {
Example9 exmpl = Example9.class.newInstance();
Inner innr = Inner.class.newInstance();
}
}
java -cp bin/ org.kambanaria.readbytecode.bgoug.Example9
Exception in thread "main" java.lang.InstantiationException:
org.kambanaria.readbytecode.bgoug.Example9$Inner
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at org.kambanaria.readbytecode.bgoug.Example9.main(Example9.java:9)
80. Example 9
public class org.kambanaria.readbytecode.bgoug.Example9 {
public OKRB.Example9();
Code:
0:aload_0
1:invokespecial #8 // Method java/lang/Object."<init>":()V
4:return
…
}
public class org.kambanaria.readbytecode.bgoug.Example9$Inner {
final OKRB.Example9 this$0;
public OKRB.Example9$Inner(OKRB.Example9);
Code:
0:aload_0
1:aload_1
2:putfield #10 //Field this$0:Lorg/kambanaria/readbytecode/bgoug/Example9;
5:aload_0
6:invokespecial #12 // Method java/lang/Object."<init>":()V
9:return
}
82. 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
● C. Nutter: JVM Bytecode for Dummies