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



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

Java Virtual Machine, JVM

Habanero Extreme Scale Software Research Project

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

The Java Virtual Machine (JVM) Pat Morin COMP 3002

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

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

02 B The Java Virtual Machine

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

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

Chapter 7D The Java Virtual Machine

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

Java Interview Questions and Answers

Introduction to Java

Inside the Java Virtual Machine

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

picojava TM : A Hardware Implementation of the Java Virtual Machine

General Introduction

Checking Access to Protected Members in the Java Virtual Machine

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

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

1 The Java Virtual Machine

Java Crash Course Part I

Checking Access to Protected Members in the Java Virtual Machine

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

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

Structural Typing on the Java Virtual. Machine with invokedynamic

C# and Other Languages

public static void main(string[] args) { System.out.println("hello, world"); } }

Section 1.4. Java s Magic: Bytecode, Java Virtual Machine, JIT,

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

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

Java and Java Virtual Machine security vulnerabilities and their exploitation techniques

C Compiler Targeting the Java Virtual Machine

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

1001ICT Introduction To Programming Lecture Notes

Crash Course in Java

Java and Java Virtual Machine Security

Java CPD (I) Frans Coenen Department of Computer Science

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

An Overview of Java. overview-1

Part I. Multiple Choice Questions (2 points each):

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

Java programming for C/C++ developers

Bypassing Memory Protections: The Future of Exploitation

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Programming the Android Platform. Logistics

CS 106 Introduction to Computer Science I

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

Instruction Set Architecture (ISA)

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

AP Computer Science Java Subset

Introduction: Abstract Data Types and Java Review

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

Stack Allocation. Run-Time Data Structures. Static Structures

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

Java Programming Fundamentals

Software. Programming Language. Software. Instructor Özgür ZEYDAN. Bülent Ecevit University Department of Environmental Engineering

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

Chapter 1 Java Program Design and Development

How To Write Portable Programs In C

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 1 Fundamentals of Java Programming

Portable Profiling of Memory Allocation in Java

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Pemrograman Dasar. Basic Elements Of Java

Introduction to Programming

Java Virtual Machine Locks

Introduction to Java. CS 3: Computer Programming in Java

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Bypassing Browser Memory Protections in Windows Vista

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Cloud Computing. Up until now

Security Vulnerability Notice

The Java Virtual Machine Specification

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

Object Oriented Software Design

1. Overview of the Java Language

An Incomplete C++ Primer. University of Wyoming MA 5310

Computing Concepts with Java Essentials

Semantic Analysis: Types and Type Checking

CSCI E 98: Managed Environments for the Execution of Programs

Security Architecture and Verification of Java Bytecode

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Multi-core Programming System Overview

Transcription:

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

Compiling for Different Platforms Program written in some high-level language (C, Fortran, ML,...) Compiled to intermediate form Optimized Code generated for various platforms (machine architecture + operating system) Consumers download code for their platform 2

Problem: Too Many Platforms! Operating systems DOS, Win95, 98, NT, ME, 2K, XP, Vista, 7,... Unix, Linux, FreeBSD, Aix, Ubuntu,... VM/CMS, OS/2, Solaris, Mac OS X,... Architectures Pentium, PowerPC, Alpha, SPARC, MIPS,... 3

Compiling for Different Platforms High-Level Program Compiler Intermediate-Level Program Code Gen 1 Code Gen 2 Code Gen 3 Machine Code 1 Machine Code 2 Machine Code 3 producer consumers 4

Dream: Platform Independence Compiler produces one low-level program for all platforms Executed on a virtual machine (VM) A different VM implementation needed for each platform, but installed once and for all 5

Platform Independence with Java Java Program Compiler Bytecode Program producer consumers JVM 1 JVM 2 JVM 3 Java Virtual Machines 6

Platform Independence with Java Java Program Compiler Bytecode Program producer consumers JIT 1 JIT 2 JIT 3 Machine Code 1 Machine Code 2 Machine Code 3 Just-in-Time Compilers 7

Java Bytecode Low-level compiled form of Java Platform-independent Compact Suitable for mobile code, applets Easy to interpret Java virtual machine (JVM) in your browser Simple stack-based semantics Support for objects 8

Class Files InputFile.java class Foo {... class Bar {... class Baz {... Compiler Foo.class... Bar.class... Baz.class... 9

What s in a Class File? Magic number, version info Constant pool Super class Access flags (public, private,...) Interfaces Fields Name and type Access flags (public, private, static,...) Methods Name and signature (argument and return types) Access flags (public, private, static,...) Bytecode Exception tables Other stuff (source file, line number table,...) 10

Class File Format magic number 4 bytes 0xCAFEBABE major version 2 bytes 0x0021 minor version 2 bytes 0x0000 magic number identifies the file as a Java class file version numbers inform the JVM whether it is able to execute the code in the file 11

Constant Pool CP length CP entry 1 CP entry 2 2 bytes (variable) (variable)...... constant pool consists of up to 65536 = 2 16 entries entries can be of various types, thus of variable length 12

Constant Pool Entries Utf8 (unicode) literal string (2 bytes length, characters) Integer Java int (4 bytes) Float Java float (4 bytes) Long Java long (8 bytes) Double Java double (8 bytes) Class class name String String constant -- index of a Utf8 entry Fieldref field reference -- name and type, class Methodref method reference -- name and type, class InterfaceMethodref interface method reference NameAndType Name and Type of a field or method 13

Constant Pool Entries Many constant pool entries refer to other constant pool entries Fieldref index to a Class index to a Utf8 (name of class containing it) index to a NameAndType index to a Utf8 (name of field) index to a Utf8 (type descriptor) Simple text (Utf8) names used to identify classes, fields, methods -- simplifies linking 14

Example class Foo { public static void main(string[] args) { System.out.println("Hello world"); Q) How many entries in the constant pool? 15

Example class Foo { public static void main(string[] args) { System.out.println("Hello world"); Q) How many entries in the constant pool? A) 33 16

1)CONSTANT_Methodref[10](class_index = 6, name_and_type_index = 20) 2)CONSTANT_Fieldref[9](class_index = 21, name_and_type_index = 22) 3)CONSTANT_String[8](string_index = 23) 4)CONSTANT_Methodref[10](class_index = 24, name_and_type_index = 25) 5)CONSTANT_Class[7](name_index = 26) 6)CONSTANT_Class[7](name_index = 27) 7)CONSTANT_Utf8[1]("<init>") 8)CONSTANT_Utf8[1]("()V") 9)CONSTANT_Utf8[1]("Code") 10)CONSTANT_Utf8[1]("LineNumberTable") 11)CONSTANT_Utf8[1]("LocalVariableTable") 12)CONSTANT_Utf8[1]("this") 13)CONSTANT_Utf8[1]("LFoo;") 14)CONSTANT_Utf8[1]("main") 15)CONSTANT_Utf8[1]("([Ljava/lang/String;)V") 16)CONSTANT_Utf8[1]("args") 17)CONSTANT_Utf8[1]("[Ljava/lang/String;") 18)CONSTANT_Utf8[1]("SourceFile") 19)CONSTANT_Utf8[1]("Foo.java") 20)CONSTANT_NameAndType[12](name_index = 7, signature_index = 8) 21)CONSTANT_Class[7](name_index = 28) 22)CONSTANT_NameAndType[12](name_index = 29, signature_index = 30) 23)CONSTANT_Utf8[1]("Hello world") 24)CONSTANT_Class[7](name_index = 31) 25)CONSTANT_NameAndType[12](name_index = 32, signature_index = 33) 26)CONSTANT_Utf8[1]("Foo") 27)CONSTANT_Utf8[1]("java/lang/Object") 28)CONSTANT_Utf8[1]("java/lang/System") 29)CONSTANT_Utf8[1]("out") 30)CONSTANT_Utf8[1]("Ljava/io/PrintStream;") 31)CONSTANT_Utf8[1]("java/io/PrintStream") 32)CONSTANT_Utf8[1]("println") 33)CONSTANT_Utf8[1]("(Ljava/lang/String;)V") 17

Field Table count 2 bytes length of table Field Table 1 variable index into CP Field Table 2 variable index into CP......... table of field table entries, one for each field defined in the class 18

Field Table Entry access flags 2 bytes e.g. public, static,... name index 2 bytes index of a Utf8 descriptor index 2 bytes index of a Utf8 attributes count 2 bytes number of attributes attribute 1 variable e.g. constant value attribute 2 variable............ 19

Method Table count 2 bytes length of table Method Table 1 variable index into CP Method Table 2 variable index into CP......... table of method table entries, one for each method defined in the class 20

Method Table Entry access flags 2 bytes e.g. public, static,... name index 2 bytes index of a Utf8 descriptor index 2 bytes index of a Utf8 attributes count 2 bytes number of attributes code attribute variable... attribute 2 variable............ 21

Code Attribute of a Method maxstack 2 bytes max operand stack depth maxlocals 2 bytes number of local variables codelength 2 bytes length of bytecode array code codelength the executable bytecode exctablelength 2 bytes number of exception handlers exceptiontable exctablelength exception handler info attributescount 2 bytes number of attributes attributes variable e.g. LineNumberTable 22

Stack Frame of a Method String this p 0 p 1 p 2 local variable array Hashtable Object maxlocals String- Buffer parameters operand stack User- Class int[ ] other locals = reference type = integer (boolean, byte,...) = continuation = useless maxstack 23

Example Bytecode if (b) x = y + 1; else x = z; 5: iload_1 //load b 6: ifeq 16 //if false, goto else 9: iload_3 //load y 10: iconst_1 //load 1 11: iadd //y+1 then clause 12: istore_2 //save x 13: goto 19 //skip else 16: iload 4 //load z 18: istore_2 //save x else clause 19:... 24

Examples class Foo { public static void main(string[] args) { System.out.println("Hello world"); Q) How many methods? 25

Examples class Foo { public static void main(string[] args) { System.out.println("Hello world"); Q) How many methods? A) 2 26

public static void main (String[] args) Code: maxstack=2 maxlocals=1 length=9 exceptions=0 attributes=2 source lines=2 local variables=1 java/lang/string[] args startpc=0 length=9 index=0 ---------------------------------------------------------------- 0: getstatic java/lang/system.out 3: ldc "Hello world" 5: invokevirtual java/io/printstream.println(ljava/lang/string;)v 8: return ================================================================ void <init> () Code: maxstack=1 maxlocals=1 length=5 exceptions=0 attributes=2 source lines=1 local variables=1 Foo this startpc=0 length=5 index=0 ---------------------------------------------------------------- 0: aload_0 1: invokespecial java/lang/object.<init>()v 4: return 27

Exception Table Entry start end entry 2 bytes start of range handler is in effect 2 bytes end of range handler is in effect 2 bytes entry point of exception handler catchtype 2 bytes type of exception handled An exception handler is just a designated block of code When an exception is thrown, table is searched in order for a handler that can handle the exception 28

Class Loading Java class loading is lazy A class is loaded and initialized when it (or a subclass) is first accessed Classname must match filename so class loader can find it Superclasses are loaded and initialized before subclasses Loading = reading in class file, verifying bytecode, integrating into the JVM 29

Class Initialization Prepare static fields with default values 0 for primitive types null for reference types Run static initializer <clinit> performs programmer-defined initializations only time <clinit> is ever run only the JVM can call it 30

Class Initialization class Staff { static Staff Dexter = new Staff(); static Staff Basu = new Staff(); static Staff Craig = new Staff(); static Staff Sarah = new Staff(); static Map<Staff,Job> h = new HashMap<Staff,Job>(); static { h.put(dexter, INSTRUCTOR); h.put(basu, TA); h.put(craig, TA); h.put(sarah, TA);... Compiled to Staff.<clinit> 31

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = 42; //code in B.<clinit> Initialization of A will be suspended while B is loaded and initialized 32

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = A.a + 1; //code in B.<clinit> Q) Is this legal Java? If so, does it halt? 33

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = A.a + 1; //code in B.<clinit> Q) Is this legal Java? If so, does it halt? A) yes and yes 34

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = A.a + 1; //code in B.<clinit> Q) So what are the values of A.a and B.b? 35

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = A.a + 1; //code in B.<clinit> Q) So what are the values of A.a and B.b? A) A.a = 1 B.b = 2 36

Initialization Dependencies class A { static int a = B.b + 1; //code in A.<clinit> class B { static int b = A.a + 1; //code in B.<clinit> Q) So what are the values of A.a and B.b? A) A.a = 1 2 B.b = 2 1 37

Object Initialization Object creation initiated by new (sometimes implicitly, e.g. by +) JVM allocates heap space for object room for all instance (non-static) fields of the class, including inherited fields, dynamic type info Instance fields prepared with default values 0 for primitive types null for reference types 38

Object Initialization Call to object initializer <init>(...) explicit in the compiled code <init> compiled from constructor if none provided, use default <init>() first operation of <init> must be a call to the corresponding <init> of superclass either done explicitly by the programmer using super(...) or implicitly by the compiler 39

Object Initialization class A { String name; A(String s) { name = s; <init>(java.lang.string)v 0: aload_0 //this 1: invokespecial java.lang.object.<init>()v 4: aload_0 //this 5: aload_1 //parameter s 6: putfield A.name 9: return 40

Next Time Class file format Class loading and initialization Object initialization Method dispatch Exception handling Bytecode verification & stack inspection 41