18
1 The Java Virtual Machine Yearly Programming Project

1 The Java Virtual Machine Yearly Programming Project

Embed Size (px)

Citation preview

Page 1: 1 The Java Virtual Machine Yearly Programming Project

1

The Java Virtual Machine

Yearly Programming Project

Page 2: 1 The Java Virtual Machine Yearly Programming Project

2

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!

Page 3: 1 The Java Virtual Machine Yearly Programming Project

3

Data Types

• Two kinds of types: primitive and reference. – Primitive: byte, short, int, long, char, float, double,

returnAddress• 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

Page 4: 1 The Java Virtual Machine Yearly Programming Project

4

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

Page 5: 1 The Java Virtual Machine Yearly Programming Project

5

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 double.

Page 6: 1 The Java Virtual Machine Yearly Programming Project

6

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

Page 7: 1 The Java Virtual Machine Yearly Programming Project

7

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

Page 8: 1 The Java Virtual Machine Yearly Programming Project

8

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

Page 9: 1 The Java Virtual Machine Yearly Programming Project

9

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

Page 10: 1 The Java Virtual Machine Yearly Programming Project

10

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 u1, u2, and u4 for represent an unsigned one-, two-, or four-byte quantity.

– No alignment or padding– Can be read using readUnsignedByte,

readUnsignedShort, and readInt– Uses pseudo-array notation, even for varying size

records

Page 11: 1 The Java Virtual Machine Yearly Programming Project

11

The Class File Structure ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; // is-abstract, private, and other flags u2 this_class; // index to constant pool u2 super_class; // index to constant pool u2 interfaces_count; u2 interfaces[interfaces_count]; // indices of constant pool u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count];

// Currently only source file information }

Page 12: 1 The Java Virtual Machine Yearly Programming Project

12

The Constant Pool cp_info { u1 tag; u1 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.

Page 13: 1 The Java Virtual Machine Yearly Programming Project

13

Methods

• Include both regular and construction methods method_info { u2 access_flags; // is-private, is-final, etc. u2 name_index; // index into the constant pool u2 descriptor_index;

// index into the constant pool, giving method signature u2 attributes_count; attribute_info attributes[attributes_count];

// currently, only Code and Exceptions are required// optional: line number information

}

Page 14: 1 The Java Virtual Machine Yearly Programming Project

14

The Code Attribute

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

Page 15: 1 The Java Virtual Machine Yearly Programming Project

15

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

Page 16: 1 The Java Virtual Machine Yearly Programming Project

16

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.

Page 17: 1 The Java Virtual Machine Yearly Programming Project

17

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.

Page 18: 1 The Java Virtual Machine Yearly Programming Project

18

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.