22
oop The JVM The JVM

The JVM

Embed Size (px)

DESCRIPTION

The JVM. What’s a JVM. A computing machine (just like 8086, but not produced by Intel) Abstract: machine is specified in a book. Concrete: anyone can implement Input: A “class” file a binary file with lots of numbers and instructions. A search path (to find more class files) Output - PowerPoint PPT Presentation

Citation preview

Page 1: The JVM

oop

The JVMThe JVM

Page 2: The JVM

oop

What’s a JVMWhat’s a JVM A computing machine (just like 8086, but not produced by Intel)

Abstract: machine is specified in a book. Concrete: anyone can implement

Input: A “class” file

a binary file with lots of numbers and instructions. A search path (to find more class files)

Output The “execution” of the class file.

How? It is all in The Java Virtual Machine Specification, by Tim Lindholm and

Frank Yellin

Start with? A method named “main” in a given class file.

The method must have certain properties Continue execution in other methods as necessary.

Page 3: The JVM

oop

Class File?Class File? The binary form for Java programs Represents a complete description of one Java

class or interface Platform independent – bytecodes are the machine

language of the JVM Not necessarily linked to the java language:

CompilerCompiler

CompilerCompiler

CompilerCompiler

Program in JavaLang.

Program in JavaLang.

Program in other

Lang.

Program in other

Lang.

Program in JavaLang.

Program in JavaLang.

JavaJava

classclass

filesfiles

JavaJava

classclass

filesfiles

OtherBinaryformat

OtherBinaryformat

Page 4: The JVM

oop

Class File StructureClass File Structure

Part I: PoolMaps strings to integers.Mostly symbolic names.

Part II: instructions for executionOrganized in “methods”Each reference to another method, or

another class file is through a “small integer” (index to the pool)

Page 5: The JVM

oop

Basic JVM ComponentsBasic JVM Components

The Java Virtual Machine

Classloader

Executionengine

Host operating system

ProgramClassfiles

The JavaAPI’s

class files

Native methods invocation

Page 6: The JVM

oop

Semantics of the Abstract MachineSemantics of the Abstract Machine Low level, Assembly Like:

no expression such as (a + b) *c No ordinary memory addressing (cannot access

address 1000) Use symbolic names, as defined in the pool. Garbage collected!

No registers (stack semantics) To do (a+b)*c:

Push aPush bAdd Push cMult

Scratch variables (used for storing arguments and local variables)

Each method defines how many it needs

Page 7: The JVM

oop

Typed AssemblyTyped Assembly

High Level Language

Class

f(int a, int b, int c, int d) { return (a + b) *c - d; }

0 :iload_1 1 :iload_2

2 :iadd 3 :iload_3

4 :imul 5 :iload 4

6 :isub 7 :ireturn

Page 8: The JVM

oop

JVM TypesJVM Types

Similar (but not identical) to Java Primitive data types.

byte: one byte short: two bytes int: four bytes long: eight bytes (2 entries on stack) float: four bytes double: eight bytes (2 entries on stack) char: two bytes

Reference types: Class reference Interface reference Array reference

Page 9: The JVM

oop

Object Oriented AssemblyObject Oriented Assembly

No ordinary memory model All memory references are through fields.

A class file defines a class, just like any other object oriented language:

Class has:Fields (also static)Methods (also static)Constructors, ...

Multi-threaded language Exception support

So that constructors can fail

Page 10: The JVM

oop

Memory ModelMemory Model Areas:

Stacks (no single stack, since we have threads)Usually organized as a linked listElements: method frames which include

• Local Variables Array (LVA)• Operand stack (OS)

Accessible by push/pop instructions.Garbage collected

HeapAll objects and all arraysNo object is allocated in the stackEach object is associated with a class stored in the

method areaGarbage collected.

Method area Information about types, constant pool, fields and method i Inaccessible to programmerGarbage collected

Page 11: The JVM

oop

Typed InstructionsTyped Instructions

Most JVM instructions are typed ! Enables bytecode verification at load time

“xload v” (x ∈ {a, i, l, f, d}) Loads (i.e. pushes) a variable v on the stack The prefix specifies the type If x = l (long) or x = d (double) then two words are pushed Otherwise, the type annotation is only for type checking

“xstore” Stores in an array (the array, the index and the stored value are

popped from the operand stack)

This is the first successful attempt to bring type safety to a lower level language

Page 12: The JVM

oop

JVM Instruction SetJVM Instruction Set

1. Arithmetic

2. Stack Manipulation

3. Variables and Constants

4. Conversions

5. Arrays

6. Objects (methods and fields)

7. Control

Page 13: The JVM

oop

Some InstructionsSome Instructions

Arithmetic: iadd, isub, imul, idiv, ineg, irem Bitwise (ints & longs) : iand, ior, ixor, ishl, ishr, iushr

Stack Manipulation: Swap, pop, dup Versions that work on two words at a time: pop2, dup2

Load and Store: Locals -> Stack: [i/f/l/d/a]load n Stack ->Locals: [i/f/l/d/a]store n Specialized load and store instructions:

iload_1 pushes int from local variable position one onto stack

Page 14: The JVM

oop

Some More InstructionsSome More Instructions

Type Conversions (casts) The JVM pops the value at the top of the stack, converts it, and

pushes the result back onto the stack. i2f converts int to float

Instructions for Arrays newarrray <type>: Allocates an array of primitives anewarrray <classname>: Allocates an array of references

Instructions for Objects new <classname>

Followed (separately) by call to constructor, getfield <full-fieldname> <field-type> invokevirtual <full-methodname> <method-type>

Stack: ... object-reference params -> ... returned-value

Page 15: The JVM

oop

Some More InstructionsSome More Instructions

Control Instructions All control structures (if, switch, while, for, break,

continue) are translated to labels and branches to labelsLabels have method scope

Labels are translated into offsets from the beginning of the branch instruction to the beginning of the labeled instruction

goto, if_icmpeq, if_acmpeq, ifnull…

Page 16: The JVM

oop

Type Checking StrategiesType Checking Strategies

None (e.g., PDP11 assembly) Compile time only (e.g., C++) Runtime only (e.g., Smalltalk) Compile time and runtime (e.g., C#) Load time: JVM

Rationale:No compilation processAny hacker can mess with the bytecodes.

Page 17: The JVM

oop

Type Checking in the JVMType Checking in the JVM At each code location (byte offset of the

method code) The number of cells used in LVA and OS is known. Each cell has one, and only one type

Primitive/reference. Only instructions that treat the cells “as they

should” are allowed:No arithmetical operations on characters.Cannot push two integers and then pop a long.Only apply methods if the object knows about them.

Page 18: The JVM

oop

VerificationVerification

When? Mainly during the load and link process

Why? No guarantee that the class file was generated by

a Java compiler Enhance runtime performance

Examples There are no operand stack overflows or

underflows. All local variable uses and stores are valid. The arguments to all the Java Virtual Machine

instructions are of valid types.

Page 19: The JVM

oop

Verification ProcessVerification Process

Pass 1 – when the class file is loaded The file is properly formatted, and all its data

is recognized by the JVM

Pass 2 – when the class file is linked All checks that do not involve instructions

final classes are not subclassed, final methods are not overridden.

Every class (except Object) has a superclass.

All field references and method references in the constant pool have valid names, valid classes, and a valid type descriptor.

Page 20: The JVM

oop

Verification Process – cont.Verification Process – cont.

Pass 3 – still during linking Data-flow analysis on each method . Ensure that 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 Pass 4 - the first time a method is actually invoked

a virtual pass whose checking is done by JVM instructionsThe referenced method or field exists in the given class. The currently executing method has access to the referenced

method or field.

Page 21: The JVM

oop

JVM in Java ArchitectureJVM in Java Architecture Java’s architecture main technologies:

The Java programming languageSource files

The Java class file formatCompiled files

The Java APIProvide access to system resources

The Java virtual machineRuns the class files

Page 22: The JVM

oop

The Java Programming EnvironmentThe Java Programming Environment

Compile time environment run time environment

A.JavaA.Java B.JavaB.Java C.JavaC.Java

JavaCompiler

A.classA.class B.classB.class C.classC.class

A.classA.class B.classB.class C.classC.class

JavaVirtual

Machine

Object classObject class String classString class