The Java Virtual Machine Yearly Programming Project 1

  • Slides: 18
Download presentation
The Java Virtual Machine Yearly Programming Project 1

The Java Virtual Machine Yearly Programming Project 1

Introduction • • An abstract computing machine. An instruction set Various memory areas Knows:

Introduction • • An abstract computing machine. An instruction set Various memory areas Knows: – Nothing of the Java programming language – Only a “class” file format: • Java Virtual Machine instructions (bytecodes) • a symbol table • other ancillary information. • Secure! 2

Data Types • Two kinds of types: primitive and reference. – Primitive: byte, short,

Data Types • Two kinds of types: primitive and reference. – Primitive: byte, short, int, long, char, float, double, return. Address • Two kinds of values: primitive and reference. • All type checking is done at compile time: – data is not tagged – data is not inspectable to determine types. • Security: the JVM checks prior to loading that the instructions are type correct 3

Objects • Explicit support for objects: instance or array. • All objects: JVM type

Objects • Explicit support for objects: instance or array. • All objects: JVM type reference. – Can be thought of as pointers to objects. – More than one reference may exist to an object. – Objects are only operated on via values of type reference. • Reference Types – Kinds: class, interface, array 4

Stack • Each thread has a private stack – No direct operations – Can

Stack • Each thread has a private stack – No direct operations – Can be implemented as a heap – Either fixed or varying size • A stack stores frames. Each frame has: • • local variables partial results operand stack data for method invocation and return. • There is always a current frame for each thread • Frames use only words. Two words for long and 5 double.

Other Areas of Memory • Heap – Shared among all threads – Pool for

Other Areas of Memory • Heap – Shared among all threads – Pool for all classes and arrays. – Reclaimed by an automatic storage management system • Method area – Similar to text segment – Shared among all threads – Subject to memory management! • Constant Pool • Native methods stack 6

Operand Stack – A stack machine: No registers! – Computation: operations on operand stack

Operand Stack – A stack machine: No registers! – Computation: operations on operand stack – IADD instruction: • pop first value • pop second value • push their sum – Type checked: • Illegal to push two ints and pop a long 7

Local Variables • Serve as registers • Indexed by numbers: 0, 1, 2, 3,

Local Variables • Serve as registers • Indexed by numbers: 0, 1, 2, 3, . . . • Operation (also for constant pool) – Loaded (pushed) into the stack – Stored from a stack • Similar operations for the constant pool 8

Instruction Set • • • Stack manipulation: Load/Store/Pop/Dup Arithmetic, type conversion Control: goto, if,

Instruction Set • • • Stack manipulation: Load/Store/Pop/Dup Arithmetic, type conversion Control: goto, if, call, exception, syncornization Object creation: new, newarray Object manipulation: – field access (static and instance) – array access – typing: check cast and object type, get array length 9

The class File • Contains one Java type, either a class or an interface.

The class File • Contains one Java type, either a class or an interface. • A stream of 8 -bit bytes • Described using C like structures – Uses types u 1, u 2, and u 4 for represent an unsigned one-, two-, or four-byte quantity. – No alignment or padding – Can be read using read. Unsigned. Byte, read. Unsigned. Short, and read. Int – Uses pseudo-array notation, even for varying size records 10

The Class File Structure Class. File { u 4 magic; u 2 minor_version; u

The Class File Structure Class. File { u 4 magic; u 2 minor_version; u 2 major_version; u 2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u 2 access_flags; // is-abstract, private, and other flags u 2 this_class; // index to constant pool u 2 super_class; // index to constant pool u 2 interfaces_count; u 2 interfaces[interfaces_count]; // indices of constant pool u 2 fields_count; field_info fields[fields_count]; u 2 methods_count; method_info methods[methods_count]; u 2 attributes_count; attribute_info attributes[attributes_count]; // Currently only source file information } 11

The Constant Pool cp_info { u 1 tag; u 1 info[]; } • Tags

The Constant Pool cp_info { u 1 tag; u 1 info[]; } • Tags for: class name, field and method references, strings of two kinds, various numerical constants, etc. • The info field varies accordingly, may also include other constant pool references. 12

Methods • Include both regular and construction methods method_info { u 2 access_flags; //

Methods • Include both regular and construction methods method_info { u 2 access_flags; // is-private, is-final, etc. u 2 name_index; // index into the constant pool u 2 descriptor_index; // index into the constant pool, giving method signature u 2 attributes_count; attribute_info attributes[attributes_count]; // currently, only Code and Exceptions are required // optional: line number information } 13

The Code Attribute Code_attribute { u 2 attribute_name_index; // Always the word “Code” u

The Code Attribute Code_attribute { u 2 attribute_name_index; // Always the word “Code” u 4 attribute_length; // Size of this structure u 2 max_stack; // The maximal # words in the operand stack u 2 max_locals; // Max # of registers used u 4 code_length; u 1 code[code_length]; // The byte codes, finally! u 2 exception_table_length; { u 2 start_pc; u 2 end_pc; u 2 handler_pc; u 2 catch_type; } exception_table[exception_table_length]; u 2 attributes_count; attribute_info attributes[attributes_count]; } 14

Verification of Class Files • Compiler: generate only valid class file • JVM: verify

Verification of Class Files • Compiler: generate only valid class file • JVM: verify that the class file is valid – Security – Version skew • Verification: – Static: basic structure – Static or Dynamic: type checking • Static implementation by Sun’s JVM 15

The Verification Process • Pass 1: ensures basic format integrity: magic number, lengths, etc.

The Verification Process • Pass 1: ensures basic format integrity: magic number, lengths, etc. • Pass 2: All additional verification that can be done without looking at the code array of the Code attribute. – Final class is not subclassed. – Has a super class. – References to constant pool. • Pass 3: Data-flow analysis on each method. 16

Data Flow Analysis: • At any given point in the program, no matter what

Data Flow Analysis: • At any given point in the program, no matter what code path is taken to reach that point: – The operand stack is always the same size and contains the same types of objects. – No local variable is accessed unless it is known to contain a value of an appropriate type. – Methods are invoked with the appropriate arguments. – Fields are assigned only using values of appropriate types. – All opcodes have appropriate type arguments on the operand stack and in the local variables. 17

Pass 4 • Only for efficiency reasons • Example: another method that returns an

Pass 4 • Only for efficiency reasons • Example: another method that returns an instance of class A. – Instance is only assigned to a field of the same type • No need to check that class A exists. – If instance is assigned to type B, • the definitions of both A and B must be loaded in to ensure that A is a subclass of B. • Implemented dynamically, at the first time a method is invoked or a field is accessed. 18