CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Similar documents
Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Problem: Too Many Platforms!

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Java Class Loading and Bytecode Verification

Plan for Today. Safe Programming Languages. What is a secure programming language?

Compiling Techniques

02 B The Java Virtual Machine

The Java Language Implementation

CMPSC 497: Java Security

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

Exercise 7 Bytecode Verification self-study exercise sheet

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Java byte code verification

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Improving Java Performance

Improving Java Code Performance. Make your Java/Dalvik VM happier

The Java Virtual Machine

Run-time Program Management. Hwansoo Han

Tutorial 3: Code Generation

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Compiler construction 2009

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Static Analysis of Dynamic Languages. Jennifer Strater

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Selected Java Topics

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Let s make some Marc R. Hoffmann Eclipse Summit Europe

Object Oriented Programming

Static Program Analysis

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Practical VM exploiting based on CACAO

Compilation 2012 Code Generation

Code Analysis and Optimization. Pat Morin COMP 3002

An Introduction to Multicodes. Ben Stephenson Department of Computer Science University of Western Ontario

Space Exploration EECS /25

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Running Mistyped Code. Lecture 19: Java Security. Running Mistyped Code. Java Security Architecture. JavaVM. Reference Monitors

JSR 292 Cookbook: Fresh Recipes with New Ingredients

Java Instrumentation for Dynamic Analysis

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CSE 431S Final Review. Washington University Spring 2013

Programming Language Systems

Oak Intermediate Bytecodes

Advances in Programming Languages: Generics, interoperability and implementation

Object Oriented Programming

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

301AA - Advanced Programming [AP-2017]

Plan for Today. Class 21: Hair-Dryer Attacks. Recap: Java Platform. Project Design Documents. Running Mistyped Code. Running Mistyped Code

ECE 122. Engineering Problem Solving with Java

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

High-Level Language VMs

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

What can go wrong in a Java program while running?

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Java: framework overview and in-the-small features

COMP1008 Exceptions. Runtime Error

LaboratoriodiProgrammazione III

Portable Resource Control in Java The J-SEAL2 Approach

Part VII : Code Generation

CMSC 330: Organization of Programming Languages. Polymorphism

JAM 16: The Instruction Set & Sample Programs

JRes: A Resource Accounting Interface for Java

BBM 102 Introduction to Programming II Spring Exceptions

CMSC 330: Organization of Programming Languages

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Delft-Java Dynamic Translation

CSC 4181 Handout : JVM

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

COMP 520 Fall 2013 Code generation (1) Code generation

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Providing Fine-Grained Access Control for Java Programs

Providing Fine-Grained Access Control For Mobile Programs Through Binary Editing

javac 29: pop 30: iconst_0 31: istore_3 32: jsr [label_51]

Special Topics: Programming Languages

CSc 453 Interpreters & Interpretation

Portable Resource Control in Java: Application to Mobile Agent Security

Just In Time Compilation

Code Generation Introduction

Chapter 5. A Closer Look at Instruction Set Architectures

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16

Chapter 14. Exception Handling and Event Handling ISBN

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

BBM 102 Introduction to Programming II Spring 2017

Mnemonics Type-Safe Bytecode Generation in Scala

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSCE 314 Programming Languages

Delft-Java Link Translation Buffer

CS11 Java. Fall Lecture 1

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Transcription:

CS2110 Fall 2011 Lecture 25 Under the Hood: The Java Virtual Machine, Part II 1

Java program last time Java compiler Java bytecode (.class files) Compile for platform with JIT Interpret with JVM run native today 2

Today Class file format Class loading and initialization Object initialization Method dispatch Exception handling Java security model Bytecode verification Stack inspection 3

Instance Method Dispatch x.foo(...) compiles to invokevirtual Every loaded class knows its superclass name of superclass is in the constant pool like a parent pointer in the class hierarchy bytecode evaluates arguments of x.foo(...), pushes them on the Object x is always the first argument 4

Instance Method Dispatch invokevirtual foo (...)V Name and type of foo(...) are arguments to invokevirtual (indices into constant pool) JVM retrieves them from constant pool Gets the dynamic (runtime) type of x Follows parent pointers until finds foo(...)v in one of those classes gets bytecode from code attribute 5

Instance Method Dispatch Creates a new frame on runtime around arguments already there Allocates space in frame for and operand Prepares (int=0, ref=null), empty Starts executing bytecode of the method When returns, pops frame, resumes in calling method after the invokevirtual instruction 6

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

Instance Method Dispatch byte[] data; void getdata() { String x = "Hello world"; data = x.getbytes(); } Code(maxStack = 2, maxlocals = 2, codelength = 12) 0: ldc "Hello world" 2: astore_1 3: aload_0 //object of which getdata is a method 4: aload_1 5: invokevirtual java.lang.string.getbytes ()[B 8: putfield A.data [B 11: return 8

Exception Handling Each method has an exception handler table (possibly empty) Compiled from try/catch/finally An exception handler is just a designated block of code When an exception is thrown, JVM searches the exception table for an appropriate handler that is in effect finally clause is executed last 9

Exception Handling Finds an exception handler empties, pushes exception object, executes handler No handler pops runtime, returns exceptionally to calling routine finally clause is always executed, no matter what 10

Exception Table Entry startrange endrange start of range handler is in effect end of range handler is in effect handlerentry entry point of exception handler catchtype exception handled startrange endrange give interval of instructions in which handler is in effect catchtype is any subclass of Throwable (which is a superclass of Exception) -- any subclass of catchtype can be handled by this handler 11

Example Integer x = null; Object y = new Object(); try { x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } finally { System.out.println("finally!"); } 12

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 13

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 14

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 15

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 16

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 17

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 18

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 19

0: aconst_null 1: astore_1 2: new java.lang.object 5: dup 6: invokespecial java.lang.object.<init> ()V 9: astore_2 10: aload_2 11: checkcast java.lang.integer 14: astore_1 15: getstatic java.lang.system.out Ljava/io/PrintStream; 18: aload_1 19: invokevirtual java.lang.integer.intvalue ()I 22: invokevirtual java.io.printstream.println (I)V 25: getstatic java.lang.system.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 33: goto #89 36: astore_3 37: getstatic java.lang.system.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 45: getstatic java.lang.system.out Ljava/io/PrintStream; try { 48: ldc "finally!" 50: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 53: goto #89 56: astore_3 57: getstatic java.lang.system.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 65: getstatic java.lang.system.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.printstream.println }(Ljava/lang/String;)V finally { 73: goto #89 76: astore 4 78: getstatic java.lang.system.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.printstream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.classcastexception 10 25 56 java.lang.nullpointerexception 10 25 76 <Any exception> 36 45 76 <Any exception> 56 65 76 <Any exception> 76 78 76 <Any exception> Integer x = null; Object y = new Object(); x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { System.out.println("y was null"); } System.out.println("finally!"); 20

Try/Catch/Finally try {p} catch (E) {q} finally {r} r is always executed, regardless of whether p and/or q halt normally or exceptionally If p throws an exception not caught by the catch clause, or if q throws an exception, that exception is rethrown upon normal termination of r 21

Try/Catch/Finally try {p} catch (E) {q} finally {r} halts normally p throws F r yes F E? no halts normally q throws G r r r throw F throw G 22

Java Security Model Bytecode verification Type safety Private/protected/package/final annotations Basis for the entire security model Prevents circumvention of higher-level checks Secure class loading Guards against substitution of malicious code for standard system classes Stack inspection Mediates access to critical resources 23

Bytecode Verification Performed at load time Enforces type safety All operations are well-typed (e.g., may not confuse refs and ints) Array bounds Operand overflow, underflow Consistent state over all dataflow paths Private/protected/package/final annotations 24

Bytecode Verification A form of dataflow analysis or abstract interpretation performed at load time Annotate the program with information about the execution state at each point Guarantees that values are used correctly 25

Types in the JVM Useless Object Integer int, short, byte, boolean, char Continuations Interface implements Array [ ] Array [ ][ ] Java class hierarchy Null 26

Typing of Java Bytecode String this p 0 p 1 p 2 local variable array Hashtable Object maxlocals parameters operand other String- Buffer User- Class int[ ] maxstack = reference type = integer = continuation = useless 27

Example 0 1 2 3 4 5 6 7 iload 3 Preconditions for safe execution: local 3 is an integer is not full 0 1 2 3 4 5 6 7 Effect: push integer in local 3 on 28

Example? iload 3?? iload 4 goto iadd? istore 3? 29

Example iload 3?? iload 4 goto iadd? istore 3? 30

Example iload 3? iload 4 goto iadd? istore 3? 31

Example iload 3? iload 4 goto iadd istore 3? 32

Example iload 3? iload 4 goto iadd istore 3 33

Example iload 3 iload 4 goto iadd istore 3 34

Example iload 3 reference iload 4 goto iadd integer istore 3 useless 35

Example iload 3 StringBuffer iload 4 goto iadd String istore 3 Object 36

Mobile Code Software producer (untrusted) Software consumer (trusted) Java program Java compiler trust boundary Java bytecode JVM or JIT 37

Mobile Code Problem: mobile code is not trustworthy! We often have trusted and untrusted code running together in the same virtual machine e.g., applets downloaded off the net and running in our browser Do not want untrusted code to perform critical operations (file I/O, net I/O, class loading, security management,...) How do we prevent this? 38

Mobile Code Early approach: signed applets Not so great everything is either trusted or untrusted, nothing in between a signature can only verify an already existing relationship of trust, it cannot create trust Would like to allow untrusted code to interact with trusted code just monitor its activity somehow 39

Mobile Code Q) Why not just let trusted (system) code do anything it wants, even in the presence of untrusted code? A) Because untrusted code calls system code to do stuff (file I/O, etc.) system code could be operating on behalf of untrusted code 40

Runtime Stack some restricted operation (e.g. write to disk) bottom R top frames of applet methods (untrusted) frames of system methods (trusted) 41

Runtime Stack bottom R top Maybe we want to disallow it the malicious applet may be trying to erase our disk it's calling system code to do that 42

Runtime Stack bottom System. cookie Writer R top Or, maybe we want to allow it it may just want to write a cookie it called System.cookieWriter System.cookieWriter knows it's ok 43

Runtime Stack bottom R top Maybe we want to allow it for another reason all running methods are trusted 44

bottom R top bottom System. cookie Writer R top bottom R top Q) How do we tell the difference between these scenarios? A) Stack inspection! 45

Stack Inspection bottom R top An invocation of a trusted method, when calling another method, may either: permit R on the above it forbid R on the above it pass permission from below (be transparent) An instantiation of an untrusted method must forbid R above it 46

Stack Inspection bottom R top When about to execute R, look down through the until we see either a system method permitting R -- do it a system method forbidding R -- don't do it an untrusted method -- don't do it If we get all the way to the bottom, do it (IE, Sun JDK) or don't do it (Netscape) 47

bottom permit forbid forbid pass pass R top bottom System. cookie Writer R top bottom R top Case A: R is not executed 48

bottom R top bottom permit forbid forbid permit System. cookie Writer pass R top bottom R top Case B: R is executed 49

bottom R top bottom System. cookie Writer R top bottom permit pass pass pass pass R top Case C: R is executed 50

Conclusion Java and the Java Virtual Machine: Lots of interesting ideas! 51