The Java VM
Architecture & APIs
2003-12087
Contents
Java VM Architecture
Java Runtime Structure
Memory Management
Execution Relatives
Exception and Error Management
Class File Structure
Class Verification
Native Method Support(JNI)
Java APIs
Java Platforms Overview
Java APIs(J2SE)
Java VM Architecture
- Java Runtime Structure
Java VM
Usually referred to Java Runtime(JRE)
Mainly used to execute programs written in Java
Typical runtime system includes:
Execution Engine Virtual(or real hardware ex. ARM)
processor for executing bytecodes
Memory Manager Allocate memory for instances and
arrays and perform garbage collection
Error and Exception Manager Deal with exception
Java VM Architecture
- Java Runtime Structure
Typical runtime system includes(contd):
Native Method Support for calling c/c++ methods
Threads Interface supporting threads and monitors
Class Loader dynamically load Java classes from Java
class files
Security Manager verify that classes are safe and
controlling access to system resources
Java VM Architecture
- Java Runtime structure
Java VM Architecture
- Memory Management
Memory Area
Divided into cells or slots
Slot can usually hold a single data item
All addressing is in terms of the logical memory cells.
Java VM Architecture
- Memory Management
The Method Area
Type Information
Fully qualified name of the type of itself, superclasses,
superinterfaces
Whether or not the type is a class or an interface
Types modifiers(public, abstract, final)
Constant Pool(more detail later)
Set of constants
Field Information
Symbolic references, literals
Name, type, modifiers
Method Information
Name, return & arg. type, modifiers
Bytecodes, exception table, stack frame size
(not native or abstract methods)
Java VM Architecture
- Memory Management
The Method Area
Class Variables
A Reference to Class ClassLoader
A Reference to Class Class
Class information can be accessed through class object
Method Table
Class variables are shared among all instances
Non-finals as part of data for the type that declares them
Finals(constants) as part of data for the type that uses them(get a
copy)
Data structures that speed up access to the raw data
ex) method table can have references to instance methods inherited
from superclass
Method area also can be garbage collected as an unreferenced
instance
Java VM Architecture
- Memory Management
The Heap
The memory for the new object is allocated from a single
heap.
Every application has its own heap
But, All threads share it!
So, careful synchronization of multi-threaded access to
object is needed.
Allocation instruction exists, but freeing instruction doesnt
exists!
Freeing memory occupied by objects that are no longer
referenced is responsible for a garbage collector.
Method area and heap may be on the same heap.
Java VM Architecture
- Memory Management
Object Representation in the heap
Objects can be freely represented in heap.
Two possible solution
- Divides the heap into two parts
- easy for VM to combat heap
fragmentation
- need dereferencing two pointers
(a)
Java VM Architecture
- Memory Management
Object
Representation in the heap
- dereference pointer only once
- make moving objects more
complicated
(b)
Java VM Architecture
- Memory Management
Method
table
Can play an important role in achieving good VM
performance.
May not exist in some implementation that have
extremely low memory requirements.
Method table includes :
Size of methods stack frame
Methods bytecodes
An exception table
Java VM Architecture
- Memory Management
Arrays
in heap
Java VM Architecture
- Memory Management
The Program Counter
Each thread has its own PC.
Can be a native pointer or an offset from the beginning of
methods bytecodes.
If a thread is executing a native method, pc is undefined.
The Stack
Each thread has its own stack area too.
Local variables and operands are thread safe.
Used for local, operand storage
References, not actual objects can exist in stack.
As each method is called, a stack frame is allocated.
Java VM Architecture
- Memory Management
Stack
Stack depth can be estimated at compiletime(will be discussed later)
Locals(include arguments) :
Frame Structure
Instance method has hidden this reference
on its first local slot.
Byte, short, char are converted into int
(due to asymmetry of instruction set)
Frame Data :
Data to support constant pool resolution
Exception table
Normal method return address
Arguments
Locals
Frame data
Operands
Stack Frame
Structure
Java VM Architecture
- Memory Management
Possible
Implementations of the Java Stack
Example code :
public static void addAndPrint() {
double result = addTwoTypes(1, 88.88);
System.out.println(result);
}
public static double addTwoTypes(int i, double d) {
return i + d;
}
Java VM Architecture
- Memory Management
Possible
Implementations of the Java Stack
- Right one uses stack area more efficiently.
- Also saves time because Java VM doesnt need to copy the parameter values.
Java VM Architecture
- Memory Management
Native Method Stack
A native method can access runtime data areas of VM and
also do anything else.
Native method calling is just calling another method within
itself, at the behest of the running Java program.
Java VM Architecture
- Memory Management
Memory
Hierachy
Java VM Architecture
- Execution Relatives
Data
Types
Java VM Architecture
- Execution Relatives
Data Types
Each data types are defined according to the values they
can have.
Every data type except Double and Long needs one
word(slot).
Boolean type
Treated as integer
Boolean arrays are implemented as byte array
Made by newarray 4, but handled by byte array instructions
ReturnAddress
Not visible to programmer
Used internally with subroutine instructions(jsr, ret)
Array Object
Special object support by instruction set
All of array elements have the same type
Java VM Architecture
- Execution Relatives
Instruction Set
Advantages
Stack based ISA - Stack is amenable to platform
independence.
Increase instruction set encoding density
- No instruction fields are needed for specifying registers
Disadvantages
Non-Orthogonal Instruction Set
8-bit opcode can only encode 256 instructions.
some datatypes(short, byte, char) are relegated to second class
status and receive less support in ISA
Hard to Extend
Extending the machine to support 96-bit or 128-bit floats and longs
cannot be done simply.
Use escape or wide opcode to create an extended instruction set.
Java VM Architecture
- Instruction Set
Instruction
Opcode byte + operand(more than zero)
Set Format
Operand can be either of index, immediate data or PCrelative offset.
Wide & escape code can be used to extend instruction
set.
Each of primitive types has its own instructions
that can operate on them.
Array access and type conversion instructions can only
operate on short, byte, and char type.
Java VM Architecture
- Instruction Set
Data-Movement Instructions
There can be different instructions for the same function.
- code density, interpretation performance are related
Pushing Constants onto the Stack
aconst_null, iconst_<n>, ldc(via constant pool),
bipush(direct)
Stack Manipulation
Local Variable relatives
iload (index), iload_<n>
iinc
Java VM Architecture
- Instruction Set
Data-Movement Instructions
Array relatives
newarray, anewarray, multianewarry, <x>aload, <x>astore,
arraylength
Object relatives
new <index(to constant pool)>
(get|put)(static|field)
(checkcast|instanceof) <index(to constant pool)>
Type Conversion
Functional Instructions
Only operates on int, float, double, long.
Operands are converted to standard number representation
before calculation
Convert back to platforms internal representation and be
pushed to stack after calculation.
Java VM Architecture
- Instruction Set
Control Flow Instructions
Designed to expose all control flow paths within a method
Method call
All control flow instructions use fixed, compile-time PC offset.(no
indirection)
Also, jump directly to a method via a fixed index into the constant
pool
This feature enables both complete code discovery and load-time
stack tracking.
invoke(virtual|static|special|interface) <index1,2>
Return PC is saved on a stack(in frame data area), but can not be
accessed directly(only through return)
* Quick instructions
Figure 5.11
Java VM Architecture
- Exceptions and Errors
Exceptions and Errors
Errors caused by either the application behavior and the
limitation of VM
Exception checked or unchecked
checked exception must be encapsulated by try/catch
clause.
unchecked(or runtime) exception caused by dynamic
behavior of program
All exceptions(and errors) must be handled somwhere.
If an exception is not handled by the method that throws the
exception, stack frame is popped until the exception is
handled by some handler.
Java VM Architecture
- Exceptions and Errors
Exception handler is implemented by miniature subroutines.
Use jsr, ret, goto instruction.
athrow throw exception dereferenced by class name on top of the stack
Use exception table to specify an exception handler.
ex)
From
To
Target
Type
12
96
Arithmetic
Exception
Internal data structure for exception table
ExceptionTable {
u2
from_pc;
u2
to_pc;
u2
handler_pc;
u2
catch_type;
}
Java VM Architecture
- Exceptions and Errors
Exception handler example
Example Java code
public class ExceptionTest {
public static void main(String args[]) {
try {
java.io.FileInputStream x
= new java.io.FileInputStream(myfile);
} catch(java.io.FileNotFoundException e) {
System.out.println(Not found);
} finally {
System.out.println(This must be executed);
}
Java VM Architecture
- Class File Structure
Magic Number
0xCAFEBABE(in big-endian order)
Constant Pool
constant - tag(u1) + length(u2) + bytes
Tag specify type of constant
CONST_???
Descriptors
BaseType : B, C, D, F, I, J(long), S, Z(boolean)
ObjectType : L<classname>
ArrayType : [(BaseType|ObjectType|ArrayType)
Method : (<argument_types>)<return_types>
ex) I
=>
int a;
[[J
[Ljava/lang/Object
[[[Z
=>
=>
=>
long[][] a;
java.lang.Object[] a;
boolean[][][] a;
()I
()Ljava/lang/String;
([BII)V
=>
=>
=>
int a();
String a();
void a(byte[], int, int)
Java VM Architecture
- Class File Structure
Access Flags
This, Super Classes, Interface
Specify modifier of class, interface, method, and
field - ACC_???
Specified by indexed constant in constant pool
Field, Method
access_flags(u2) + name_index(u2) +
descriptor_index(u2) + attribute_count(u2) +
attributes_info
name, descriptor are on the constant pool
Java VM Architecture
- Class File Structure
Attribute
Method code, constant value for finals, exception
that a method may throw
Innerclass, LineNumberTable, LocalVariableTable,
Source file name
Code attribute max_stack, max_locals, code,
exception_table
Class file limitation
u2 - constant pool entries, field count, method count,
bytecode length(per method), local variables, operand
stack, exception table length
u1 - array dimensions, arg. to a method
Java VM Architecture
- Class File Structure
ClassStruct_java.txt
ClassStruct.txt
Java VM Architecture
- Class Verification
Class Verification
Class binaries are sometimes unsafe and may crash VM.
Must take all control path and prove that the program is safe in
each case.
Halting Problem
Studied by Alan Turing and Kurt Godel
In general case, it is not possible to take a description of a
program and decide whether or not the program will complete, let
alone whether is behaves well or not.
Operand Stack Tracking
For each alternative way in a method for reaching an instruction X,
the stack state and the local variable state must be equivalent.
Figure 5.10
(b) stack size is different. (c) operand types are different
Java VM Architecture
- Class Verification
Operand Stack Tracking
ex) iconst_4
istore_1
Loop:
aconst_null
iinc 1 1
iload_1
Operand stack is not equivalent at Loop.
Stack tracking can be done in static-time
Because control flows are determined in static-time.
Execution engine doesnt need to perform runtime checks for
following items :
Stack limits
Types of arguments to JVM instructions
Accesses or assignments to local variables
Java VM Architecture
- Class Verification
Passing verification
Structure
Check that the class file structure is met.
Check also the contents of bytecode
ex) this_class field must be the index of a CONSTANT_Class
Magic field must be the value 0xCAFEBABE
Check that all byte code offsets are within method boundary.
Type of constant and constant referencing instruction must be the same.
Environment
Other classes that one class depends, and the methods and fields of
those
Type conflict and access conflict
Doesnt immediately check if the referenced class really exist.
The constant pool can also contain references to classes that havent been
loaded yet.
JVM verifier tries to delay the checks until they are necessary.
Speed up the initial loading time for a class
Java VM Architecture
- Class Verification
Passing verification
Environment
ex) invokenonvirtual myclass/funmethod()LFunClass
-> putfield myclass/myfield LFunClass;
-> putfield myclass/myfield LAnotherClass;
Content
Each instruction should bee invoked with the correct types
for its operands and stack values.
The maximum stack length must not be exceeded.
Use pop2, pop to retrieve long value from the stack
Stack size is specified in the Code attribute
Dont use the stack in complex ways.
Only push items onto the stack just before they are needed.
Java VM Architecture
- Class Verification
Working of bytecode verifier
Trace all the static control flow paths and simulate a stack
symbolically.
Steps :
When instruction is first encountered, stores stack and local
var. state in table
separate entry is maintained in the table for every instruction in
bytecode
Then check that Instruction is begin run with the correct
types
Emulate the instructions effect on the stack and local var.
When a branch instruction is met, look at all the possible
destinations
If a destination has not been seen previously, verifier recursively
examines.
Else, verifier compares the current state with recorded state
Java VM Architecture
- Class Verification
Working of bytecode verifier
State comparison
Two states are identical move forward
Two states are incompatible verifier complains!
Two state are compatible merges two states
int
float
DataInputStream
int
float
BufferedInputStream
int
float
Vector
int
float
int
merge
incompatible
int
float
FilterInputStream
Java VM Architecture
- Native Method Support
Java
Java side and native side can interoperate each
other by JNI.
Native side method invocation from Java side
Native Interface(JNI)
Use native keywords for modifier of function
Generate header file for native function by javah
ex) JNITest_java.txt ->JNITest_header.txt
Java side method invocation from Native side
Create JVM and call method through API
ex) CreateJVM.txt
Java APIs
- Java Platform Overview
J2SE(Standard
Edition)
API for developing general user app. or client app.
J2EE(Enterprise
API for developing large enterprise software
infrastructure
ex) EJB, servlet, JSP, JMS, etc.
J2ME(Micro
Edition)
Edition)
Light-weight platform for embedded system
Java APIs
- J2SE APIs
Serialization
RMI is used for communicating between objects in different VM.
Parameters or return values must be converted to
implementation-independent form in RMI.
Serialization may be used for object to be saved in persistent
storage.
In order to serialize an object, it must implements the Serializable
interface.
Reflection
Determine class information at run time
Classes in java.lang.reflect package
Array, Constructor, Field, Method, Modifier, etc.
Object.getClass() -> Class.get(Fields|Methods| )
ex) Method can be called by invoke method of Method class.
Java APIs
- J2SE APIs
Thread
Multithreading support is provided by java.lang.Thread
class(and Runnable interface)
Libraries can communicate with the underlying OS.
Thread execute run() method during its lifetime.
Synchronization through monitor
Suppported by instruction
monitorenter and monitorexit
Locks are associated with each object and each
class(through Class object).
Class Object declares five methods that enable
programmers to access the Java Virtual Machines support
for the coordination aspect of synchronization.
notify, notifyAll, wait
Java APIs
Synchronization Example