Java Virtual Machine
Java Virtual Machine
Java bytecode
(moves locally or via web)
Java Platform
Linux Win32/NT Solaris
Hardware
Java Platform
Java Platform & VM & Devices
Java 2 XXX Edition
Each edition is a developer treasure chest of tool
s and supplies that can be used with a particular
product:
Java virtual machines that fit inside the range of cons
umer devices
a library of APIs that are specialized for each type of
device
tools for deployment and device configuration
a profile, that is, a specification of the minimum set o
f APIs useful for a particular kind of consumer device
(set-top, screenphone, wireless, car, and digital assist
ant) and a specification of the Java virtual machine fu
nctions required to support those APIs
Java 2 Standard Edition
Java 2 Enterprise Edition
Java 2 Micro Edition
Various VM
JVM (J2EE & J2SE)
Well-known Java Virtual Machine.
CVM, KVM (J2ME)
Addresses on vast small devices.
Reduces some VM features to fit resource-
constrained devices.
JCVM (Java Card)
Addresses on smart cards.
It has least VM features.
J2ME : Configuration
J2ME technology has two design centers--
things that you hold in your hand and
things you plug into a wall.
Java bytecode
class m Exception,
(*.class)
ethodar Heap Thread,
Class Loader ea
& Verifier Security.
Java Stack,
Java Class
Register
Library
native
native execution engine
native method method
method linker
area
Operating System
Method Area & Heap
Each JVM implementation has a heap and
a method area.
Heap is shared among all threads. It is the
runtime data area from which for all class
instances and arrays is allocated.
Method area is shared among all threads.
It stores per-class structures such as the
runtime constant pool, field and method
data, the code for methods.
Class Loader & Verifier
Loading: Load binary representation of class (*.c
lass).
Linking:
Verification: Ensure that the binary representation is s
tructurally valid.
Preparation: Create the static fields for the class or int
erface and initializing those fields to their standard de
fault values.
Resolution: Determine concrete values from symbolic
references in the runtime constant pool .
Initializing: Invoke its static initializers and the in
itializers for static fields declared in class.
Register
PC (program counter) register. It contains
the address of the Java virtual machine
instruction currently being executed.
operand stack
others…
frame
Java Stack
Java Stack : frame (3)
class LocalVariableExample {
public static void runClassMethod(int i, long l, float f) { }
public void runInstanceMethod(char c, double d, boolean b) {
}
}
runClassMethod() runInstanceMethod
()
0 int i 0 reference
1 char c
1 long l
2 double d
3 float f
4 boolean b
Java Stack : frame (4)
iload_0
iload_1
iadd
istore_2
local variables
operand stack
Java Stack : frame (5)
class pointer
previous frame pointer
exception table
……
Execution Engine (1)
A virtual processor -- the core of JVM.
Programming aspect : interpreter.
Read bytecode stream then execute the in
structions.
The performance can be improved by Jus
t-In-Time(JIT) compiler.
Execution Engine (2)
iload 1
iinc 2 -1
iload_2
ifne Loop
return
.end method
Java Assembler
Not an official assembler. There is no stan
dard for java assembly language.
http://mrl.nyu.edu/~meyer/jasmin/
Class File Structure
Tables of Class File
Top Table:
Basic information of the whole class.
Constant Pool Table:
Records string, constant, reference, method name, field name…
Interface Table:
What interface are implemented by this class.
Field Table:
The fields which class owns.
Method Table:
The methods which class owns.
Attribute Table:
The extra information of class, field, method… and so on.
Magic Number
0xCA, 0xFE, 0xBA, 0xBE
Ex1: Ljava/lang/String;
Ex2: Ljava/io/PrintStream;
Type Descriptor (3)
Array Type:
[<type>
Ex1: [C char[]
Ex2: [[F float[][]
Ex3: [Ljava/lang/Thread; Thread[]
Type Descriptor (4)
Method:
(<argument_types>)<return_type>
Ex1: ()V
void xyz() {…}
Ex2: (SF[Ljava/lang/Thread;)I
int xyz(short x, float y, Thread[] z) {…}
Constant Pool
Tag Value Description
CONSTANT_Utf8 1 array of byte
CONSTANT_Integer 3 integer
CONSTANT_Float 4 float
CONSTANT_Long 5 long
CONSTANT_Double 6 Double
CONSTANT_Class 7 Tag-1 index
CONSTANT_String 8 Tag-1 index
CONSTANT_Fieldref 9 Tag-7 index + Tag-12 index
CONSTANT_Methodref 10 Tag-7 index + Tag-12 index
CONSTANT_InterfaceMethodref 11 Tag-7 index + Tag-12 index
CONSTANT_NameAndType 12 Tag-1 index(filed,method name)
+ Tag-1 index(type descriptor)
Access Flag
Flag Value
ACC_PUBLIC 0x0001
ACC_PRIVATE 0x0002
ACC_PROTECTED 0x0004
ACC_STATIC 0x0008
ACC_FINAL 0x0010
ACC_SUPER 0x0020
ACC_SYNCHRONIZED 0x0020
ACC_VOLATILE 0x0040
ACC_TRANSIENT 0x0080
ACC_NATIVE 0x0100
ACC_INTERFACE 0x0200
ACC_ABSTRACT 0x0400
out.j
.class public out
.super java/lang/Object
return
.end method
out.class
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
public + super
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
self class name
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
super class name
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
interface number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
field number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
public
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method name
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method type
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method length
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
stack size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
local variable size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode length
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode content
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
exception size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
extra attribute number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
public + static
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method name
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method type
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method length
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
stack size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
local variable size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode length
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode content
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
exception size
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
extra attribute number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
atttribute number
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute length
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute data
[ ][ ][ ] [1 ][
2
] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
The End