UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.

Slides:



Advertisements
Similar presentations
© 2003 School of Computing, University of Leeds SY32 Secure Computing, Lecture 16 Secure Coding in Java and.NET Part 1: Fundamentals.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Virtual Machines Matthew Dwyer 324E Nichols Hall
Yoshi
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
BA1 JVM arkitektur BA2 Specifikation I The Java Virtual Machine Specification specificeres bl.a.: –De grundlæggende datatyper.
Java Applet Security Diana Dong CS 265 Spring 2004.
Java security (in a nutshell)
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
CMSC 330: Organization of Programming Languages Exceptions.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Lab 9 Java Bytecode & The Jasmin Assembler
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
An Approach to Safe Object Sharing Ciaran Bryce & Chrislain Razafimahefa University of Geneva, Switzerland.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Security Updated May Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
CIS 270—Application Development II Chapter 13—Exception Handling.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
1 The Java Virtual Machine Yearly Programming Project.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
1 Introduction to JVM Based on material produced by Bill Venners.
Java 2 security model Valentina Casola. Components of Java the development environment –development lifecycle –Java language features –class files and.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Programming Languages and Paradigms Activation Records in Java.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
RealTimeSystems Lab Jong-Koo, Lim
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Design issues for Object-Oriented Languages
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Java Programming Language
Java security (in a nutshell)
Topic: JAVA .Class File Structure
Topic: Java Security Models
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Java Programming Language
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Exception Handling.
Presentation transcript:

UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1

Pick up where we left off 2 Java program Java compiler Java bytecode (.class files) Interpret with JVMCompile for platform with JIT run native today last time

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

Instance Method Dispatch 4 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 stack Object x is always the first argument

Instance Method Dispatch 5 invokevirtual foo (...) 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(...) in one of those classes – gets bytecode from code attribute

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

Stack Frame of a Method 7 local variable array operand stack thisp0p0 p1p1 p2p2 parameters other locals maxLocals maxStack = reference type = integer (boolean, byte,...) = continuation String Hash- table Object String- Buffer User- Class int[ ] = useless

Instance Method Dispatch 8 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

Exception Handling 9 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

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

Exception Table Entry 11 startRange start of range handler is in effect endRange 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

Example 12 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!"); }

13 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

14 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

15 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

16 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

17 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

18 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

19 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

20 0: aconst_null 1: astore_1 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()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; 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 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 java.lang.ClassCastException java.lang.NullPointerException 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!"); }

Try/Catch/Finally 21 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

Try/Catch/Finally 22 try {p} catch (E) {q} finally {r} p r throws Fhalts normally F ≤ E ? yesno r throw F q r halts normallythrows G r throw G

Java Security Model 23  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

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

Bytecode Verification 25  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

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

Typing of Java Bytecode 27 local variable array operand stack thisp0p0 p1p1 p2p2 parameters other locals maxLocals maxStack = reference type = integer = continuation String Hash- table Object String- Buffer User- Class int[ ] = useless

Example locals stack locals stack iload 3 Preconditions for safe execution: local 3 is an integer stack is not full Effect: push integer in local 3 on stack

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

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

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

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

Example 33 locals stack locals stack locals stack locals stack iload 3 iload 4 iadd istore 3 locals stack goto ? locals stack

Example 34 locals stack locals stack locals stack locals stack iload 3 iload 4 iadd istore 3 locals stack goto locals stack

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

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

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

Mobile Code 38 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?

Mobile Code 39 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

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

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

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

Runtime Stack 43 top bottom R 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 System. cookie Writer

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

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

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

Stack Inspection 47 top bottom R When about to execute R, look down through the stack 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)

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

49 top bottom R top bottom R System. cookie Writer top bottom R Case B: R is executed permitpasspermitforbid

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

Conclusion 51 Java and the Java Virtual Machine: Full of interesting ideas Many systems have been built by taking an open source JVM and then somehow “doing surgery” on it. You can too!