Virtual Machines. Case Study: JVM. Virtual Machine, Intermediate Language. JVM Case Study. JVM: Java Byte-Code. JVM: Type System



Similar documents
The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

02 B The Java Virtual Machine

Java Virtual Machine, JVM

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Under the Hood: The Java Virtual Machine. Lecture 24 CS 2110 Fall 2011

picojava TM : A Hardware Implementation of the Java Virtual Machine

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Habanero Extreme Scale Software Research Project

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Java Interview Questions and Answers

Chapter 7D The Java Virtual Machine

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

1 The Java Virtual Machine

Structural Typing on the Java Virtual. Machine with invokedynamic

CSC 8505 Handout : JVM & Jasmin

- Applet java appaiono di frequente nelle pagine web - Come funziona l'interprete contenuto in ogni browser di un certo livello? - Per approfondire

Crash Course in Java

General Introduction

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

Checking Access to Protected Members in the Java Virtual Machine

Java Programming Fundamentals

Introduction to Java

Cloud Computing. Up until now

Java Memory Model: Content

Fundamentals of Java Programming

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Jonathan Worthington Scarborough Linux User Group

CS 111 Classes I 1. Software Organization View to this point:

Real-time Java Processor for Monitoring and Test

Java Crash Course Part I

Java Card 3 Platform. Virtual Machine Specification, Classic Edition. Version May 2015

An Overview of Java. overview-1

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Glossary of Object Oriented Terms

Chapter 1 Java Program Design and Development

C Compiler Targeting the Java Virtual Machine

Armed E-Bunny: A Selective Dynamic Compiler for Embedded Java Virtual Machine Targeting ARM Processors

The Java Virtual Machine Specification. Java SE 8 Edition

Restraining Execution Environments

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

IBM SDK, Java Technology Edition Version 1. IBM JVM messages IBM

Java Virtual Machine Locks

Instruction Set Architecture (ISA)

Replication on Virtual Machines

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Explain the relationship between a class and an object. Which is general and which is specific?

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

Chapter 3: Operating-System Structures. Common System Components

Fachbereich Informatik und Elektrotechnik SunSPOT. Ubiquitous Computing. Ubiquitous Computing, Helmut Dispert

A Java Virtual Machine Architecture for Very Small Devices

AVR 32-bit Microcontroller. Java Technical Reference. 1. Introduction. 1.1 Intended audience. 1.2 The AVR32 Java Extension Module

Java CPD (I) Frans Coenen Department of Computer Science

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Inside the Java Virtual Machine

Multi-core Programming System Overview

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

AP Computer Science Java Subset

System Structures. Services Interface Structure

HOTPATH VM. An Effective JIT Compiler for Resource-constrained Devices

HVM TP : A Time Predictable and Portable Java Virtual Machine for Hard Real-Time Embedded Systems JTRES 2014

Chapter 6, The Operating System Machine Level

Hardware/Software Co-Design of a Java Virtual Machine

Example of Standard API

Writing new FindBugs detectors

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

CS506 Web Design and Development Solved Online Quiz No. 01

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Pemrograman Dasar. Basic Elements Of Java

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

CSCI E 98: Managed Environments for the Execution of Programs

The Hotspot Java Virtual Machine: Memory and Architecture

Chapter 3 Operating-System Structures

The Java Virtual Machine Specification

The Design of the Inferno Virtual Machine. Introduction

Java Language Tools COPYRIGHTED MATERIAL. Part 1. In this part...

Stack Allocation. Run-Time Data Structures. Static Structures

Retargeting Android Applications to Java Bytecode

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Checking Access to Protected Members in the Java Virtual Machine

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

An Implementation Of Multiprocessor Linux

C++ INTERVIEW QUESTIONS

Instrumenting Java bytecode

Can You Trust Your JVM Diagnostic Tools?

Transcription:

Case Study: JVM Virtual Machines What is a machine? does something (...useful) programmable concrete (hardware) What is a virtual machine? a machine that is not concrete a software emulation of a physical computing environment Reality is somewhat fuzzy! Is a Pentium-II a machine? RISC Core µop µop µop uctions decoder Hardware and software are logically equivalent (A. Tanenbaum) P. Reali / M. Corti Virtual Machine, Intermediate Language Pascal P-Code () stack-based processor strong type machine language compiler: one front end, many back ends UCSD Apple][ implementation, PDP, Z80 Modula M-Code (80) high code density Lilith as microprogrammed virtual processor JVM Java Virtual Machine () Write Once Run Everywhere interpreters, JIT compilers, Hot Spot Microsoft.NET (000) language interoperability JVM Case Study compiler (Java to bytecode) interpreter, ahead-of-time compiler, JIT dynamic loading and linking exception Handling memory management, garbage collection OO model with single inheritance and interfaces system classes to provide OS-like implementation compiler class loader runtime system P. Reali / M. Corti P. Reali / M. Corti JVM: Type System JVM: Java Byte-Code Primitive types byte short int long float double char reference boolean mapped to int Object types classes interfaces arrays Single class inheritance Multiple interface implementation Arrays anonymous types subclasses of java.lang.object Memory access tload / tstore ttload / ttstore tconst getfield / putfield getstatic / putstatic Operations tadd / tsub / tmul / tdiv tshifts Conversions fi / if / il /... Control ifeq / ifne / iflt /... if_icmpeq / if_acmpeq invokestatic invokevirtual invokeinterface athrow treturn Allocation new / newarray Casting dup / dup / dup_x /... checkcast / instanceof P. Reali / M. Corti P. Reali / M. Corti

JVM: Java Byte-Code Example bipush Operation Format Forms Push byte bipush = (0x0) Operand Stack... =>..., value Description bipush byte The immediate byte is sign-extended to an value. That value is pushed onto the operand stack. JVM: Machine Organization Virtual Processor stack machine no registers typed uctions no memory addresses, only symbolic names Runtime Data Areas pc register stack locals parameters return values heap method area code runtime constant pool native method stack P. Reali / M. Corti P. Reali / M. Corti JVM: Execution Example JVM: Reflection Load and manipulate unknown classes at runtime. program iload iload iadd istore iload locals v v v iload istore v iadd v v+v operand stack java.lang. getfields getmethods getconstructors java.lang.reflect.field setobjectgetobject setint getint setfloat getfloat... java.lang.reflect.method getmodifiers invoke java.lang.reflectconstructor Time 8 P. Reali / M. Corti P. Reali / M. Corti JVM: Reflection Example import java.lang.reflect.*; public class ReflectionExample { public static void main(string args[]) { try { c =.forname(args[0]); Method m[] = c.getdeclaredmethods(); for (int i = 0; i < m.length; i++) { System.out.println(m[i].toString()); catch (Throwable e) { System.err.println(e); JVM: Java Weaknesses Transitive closure of java.lang.object contains.. 8. 80. 8 (.) 80 classpath 0.0 class Object { public String tostring();... class String { public String touppercase(locale loc);... public final class Locale implements Serializable, Cloneable {... 0 P. Reali / M. Corti P. Reali / M. Corti

JVM: Java Weaknesses static initialization T is a class and an instance of T is created Problem circular dependencies in static initialization code JVM: Java Weaknesses interface Example { final static String labels[] = { A, B, C T tmp = new T(); T is a class and a static method of T is invoked T.staticMethod(); A nonconstant static field of T is used or assigned (field is not static, not final, and not initialized with compile-time constant) T.someField = ; A static { x = B.f(); B static { y = A.f(); hidden static initializer: labels = new String[]; labels[0] = A ; labels[] = B ; labels[] = C ; Warning: in Java final means write-once! interfaces may contain code P. Reali / M. Corti P. Reali / M. Corti JVM: Memory Model The JVM specs define a memory model: defines the relationship between variables and the underlying memory meant to guarantee the same behavior on every JVM The compiler is allowed to reorder operation unless synchronized or volatile is specified. JVM: Reordering read and writes to ordinary variables can be reordered. public class Reordering { int x = 0, y = 0; public void writer() { x = ; y = ; public void reader() { int r = y; int r = x; P. Reali / M. Corti P. Reali / M. Corti JVM: Memory Model synchronized: in addition to specify a monitor it defines a memory barrier: acquiring the lock implies an invalidation of the caches releasing the lock implies a write back of the caches synchronized blocks on the same object are ordered. order among accesses to volatile variables is guaranteed (but not among volatile and other variables). JVM: Double Checked Lock Singleton public class Some { private Resource resource = null; public Resource synchronized getresource() { resource = new Resource(); return resource; P. Reali / M. Corti P. Reali / M. Corti

JVM: Double Checked Lock Double checked locking JVM: Double Checked Lock Thread Thread public class Some { private Resource resource = null; public class Some { private Resource resource = null; public class Some { private Resource resource = null; public Resource getresource() { synchronized { resource = new Resource(); return resource; 8 P. Reali / M. Corti public Resource getresource() { synchronized { resource = new Resource(); return resource; public Resource getresource() { synchronized { resource = new Resource(); return resource; The object is instantiated but not yet initialized! P. Reali / M. Corti JVM: Immutable Objects are not Immutable Immutable objects: all types are primitives or references to immutable objects all fieds are final Example (simplified): java.lang.string contains an array of characters the length an offset example: s = abcd, length =, offset =, string = cd String s = /usr/tmp String s = s.substring(); //should contain /tmp Sequence: s is instantiated, the fields are initialized (to 0), the array is copied, the fields are written by the constructor. What happens if uctions are reordered? JVM: Reordering Volatile and Nonvolatile Stores volatile reads and writes are totally ordered among threads but not among normal variables example Thread Thread o = new SomeObject; initialized = true; volatile boolean initialized = false; SomeObject o = null;? while (!initialized) { sleep(); o.field = ; 0 P. Reali / M. Corti P. Reali / M. Corti JVM: JSR Java Community Process Java memory model revision Final means final Volatile fields cannot be reordered Java JVM: Execution Interpreted (e.g., Sun JVM) bytecode uctions are interpreted sequentially the VM emulates the Java Virtual Machine slower quick startup Just-in-time compilers (e.g., Sun JVM, IBM JikesVM) bytecode is compiled to native code at load time (or later) code can be optimized (at compile time or later) quicker slow startup Ahead-of time compilers (e.g., GCJ) bytecode is compiled to native code offline quick startup quick execution static compilation P. Reali / M. Corti P. Reali / M. Corti

JVM: The file Format File { version constant pool flags super class interfaces fields methods attributes Constants: Values String / Integer / Float /... References Field / Method / /... Attributes: ConstantValue Code Exceptions JVM: File Format class HelloWorld { public static void printhello() { System.out.println("hello, world"); public static void main (String[] args) { HelloWorld myhello = new HelloWorld(); myhello.printhello(); P. Reali / M. Corti P. Reali / M. Corti JVM: File (Constant Pool). String hello, world. HelloWorld. java/io/printstream. java/lang/object. java/lang/system. Methodref HelloWorld.<init>(). Methodref java/lang/object.<init>() 8. Fieldref java/io/printstream java/lang/system.out. Methodref HelloWorld.printHello() 0. Methodref java/io/printstream.println(ja va/lang/string ). NameAndType <init> ()V. NameAndType out Ljava/io/PrintStream;. NameAndType printhello ()V. NameAndType println (Ljava/lang/String;)V. Unicode ()V. Unicode (Ljava/lang/String;)V. Unicode ([Ljava/lang/String;)V 8. Unicode <init>. Unicode Code 0. Unicode ConstantValue. Unicode Exceptions. Unicode HelloWorld. Unicode HelloWorld.java. Unicode LineNumberTable. Unicode Ljava/io/PrintStream;. Unicode LocalVariables. Unicode SourceFile 8. Unicode hello, world. Unicode java/io/printstream 0. Unicode java/lang/object. Unicode java/lang/system. Unicode main. Unicode out. Unicode printhello JVM: File (Code) Methods 0 <init>() 0 ALOAD0 INVOKESPECIAL [] java/lang/object.<init>() RETURN PUBLIC STATIC main(java/lang/string []) 0 NEW [] HelloWorld DUP INVOKESPECIAL [] HelloWorld.<init>() ASTORE 8 INVOKESTATIC [] HelloWorld.printHello() RETURN PUBLIC STATIC printhello() 0 GETSTATIC [8] java/io/printstream java/lang/system.out LDC hello, world INVOKEVIRTUAL [0] java/io/printstream.println(java/lang/string ) 8 RETURN P. Reali / M. Corti P. Reali / M. Corti JVM: Compilation Pattern Expansion Each byte code is translated according to fix patterns + easy - limited knowledge Example (pseudocode) switch (o) { case ICONST<n>: generate( push n ); PC++; break; case ILOAD<n>: generate( push off_n[fp] ); PC++; break; case IADD: generate( pop -> R ); generate( pop -> R ); generate( add R, R -> R ); generate( push R ); PC++; break; 8 P. Reali / M. Corti JVM: Optimizing Pattern Expansion Main Idea: use internal virtual stack stack values are consts / fields / locals / array fields / registers /... flush stack as late as possible emitted code virtual stack P. Reali / M. Corti MOV EAX, off [FP] local local local ADD EAX, off [FP] local EAX EAX iload iload iadd istore iload iload iadd istore MOV off [FP], EAX

JVM: Comparison Linking (General) uctions memory accesses pattern expansion push off[fp] push off[fp] pop EAX add 0[SP], EAX pop off[fp] iload_ iload_ iadd istore_ uctions memory accesses optimized mov EAX, off[fp] add EAX, off[fp] mov off[fp], EAX A compiled program contains references to external code (libraries) After loading the code the system need to link the code to the library identify the calls to external code locate the callees (and load them if necessary) patch the loaded code Two options: the code contains a list of sites for each callee the calls to external code are s to a procedure linkage table which is then patched (double indirection) 0 P. Reali / M. Corti P. Reali / M. Corti Linking (General) Linking (General) 0 0 0 0 - &p - 00 &p0 00 &p 0 0 0 0 proc 0 00 proc 0 00 &p0 proc proc &p P. Reali / M. Corti P. Reali / M. Corti JVM: Linking Bytecode interpreter references to other objects are made through the JVM (e.g., invokevirtual, getfield, ) Native code (ahead of time compiler) static linking classic native linking JIT compiler only some classes are compiled calls could reference classes that are not yet loaded or compiled (delayed compilation) code umentation JVM: Methods and Fields Resolution method and fields are accessed through special VM functions (e.g., invokevirtual, getfield, ) the parameters of the special call defines the target the parameters are indexes in the constant pool the VM checks id the call is legal and if the target is presentl P. Reali / M. Corti P. Reali / M. Corti

JVM: JIT Linking and Instrumentation Use code umentation to detect first access of static fields and methods Compilation and Linking Overview C C header header C source class A {......B.x class B { int x; Object File Object File Object File Object file B.x Check(B); B.x IF ~B.initialized THEN Initialize(B) END; Code P. Reali / M. Corti P. Reali / M. Corti Compilation and Linking Overview Compilation and Linking Overview source Java source Object File Object File Object File Object & Symbol File Reflection API JIT 8 P. Reali / M. Corti P. Reali / M. Corti Jaos Jaos (Java on Active Object System) is a Java virtual machine for the Bluebottle system goals: implement a JVM for the Bluebottle system show that the Bluebottle kernel is generic enough to support more than one system interoperability between the Active and Java languages interoperability between the System and the Java APIs Jaos (Interoperability Framework) source Object & Symbol Browser Metadata Metadata Java Reflection API File Java Metadata JIT 0 P. Reali / M. Corti P. Reali / M. Corti

JVM: Verification JVM: Verification generates good code...... that could be changed before reaching the JVM need for verification Verification makes the VM simpler (less run-time checks): no operand stack overflow load / stores are valid VM types are correct no pointer forging no violation of access restrictions access objects as they are (type) local variable initialized before load Pass (Loading): class file version check class file format check class file complete Pass (Linking): final classes are not subclassed every class has a superclass (but Object) constant pool references constant pool names P. Reali / M. Corti P. Reali / M. Corti JVM: Verification Pass (Linking): For each operation in code (independent of the path): operation stack size is the same accessed variable types are correct method parameters are appropriate field assignment with correct types opcode arguments are appropriate Byte-Code Verification Delayed for performance reasons Pass (RunTime): First time a type is referenced: load types when referenced check access visibility class initialization First member access: member exists member type same as declared current method has right to access member JVM: Byte-Code Verification Verification: branch destination must exists opcodes must be legal access only existing locals code does not end in the middle of an uction types in byte-code must be respected execution cannot fall of the end of the code exception handler begin and end are sound P. Reali / M. Corti P. Reali / M. Corti JVM: Bootstrapping How to start a JVM? External help needed! Load core classes Compile classes Provide memory management Provide threads Solution: Implement Java on rd party system Linux Solaris Windows Bluebottle Java Bootstrapping: Jaos Example All native methods in Active Use Bluebottle run-time structures module (= class) type descriptor object (= object instance) active object (= thread) Bootstrap load core classes Object, String, System, Runtime, Threads,... Exception forward exception to java code allocate java classes from P. Reali / M. Corti P. Reali / M. Corti 8

Bootstrapping: Jnode VM Example JVM written in Java small core in assembler low-level functionalities that requires special assembler uctions some native methods inlined by the compiler Unsafe debug(string) int AddressToInt(Address) int getint(object, offset) Bootstrap compile to Java classes bootloader: native compilation code placement structure allocation make boot image boot with GNU/GRUB Bootstrapping: / Bluebottle Compile each module to machine code system calls for newrec (record) newsys (block, no ptrs) newarr (array) linker / bootlinker patches syscalls with real procedures bootlinker is same as linker, but uses different memory addresses simulates memory allocation start address configurable glue code to call all module bodies 8 P. Reali / M. Corti P. Reali / M. Corti Bootstrapping s bootstrapping Ober Asm A B Ober Ober Asm C Ober 0 This compiler is written in language C and translates Ober 0 Asm Ober 0 programs in A into programs in B Ober 0 Ober 0 Asm Asm Asm language X is already available X this compiler can be excuted 80 P. Reali / M. Corti