Outline: Reading: Chapter 13
Outline: Reading: Chapter 13
Outline Java
Language Overview
History and design goals
John Mitchell
Method lookup
four different bytecodes
Design Goals
Portability
Internet-wide distribution: PC, Unix, Mac
Reliability
Avoid program crashes and error messages
Safety
Programmer may be malicious
Internet application
simple language for writing programs that can be transmitted over network
Efficiency
Important but secondary
Java System
The Java programming language Compiler and run-time system
Programmer compiles code Compiled code transmitted on network Receiver executes on interpreter (JVM) Safety checks made before/during execution
Static Import
Enables tools to generate code from annotations (JSR 175) Concurrency utility library, led by Doug Lea (JSR-166)
Annotations (Metadata)
Outline
Objects in Java
Classes, encapsulation, inheritance
Language Terminology
Class, object - as in other languages Field data member Method - member function Static members - class fields and methods this - self Package - set of classes in shared namespace Native method - method written in another language, often C
Type system
Primitive types, interfaces, arrays, exceptions
Virtual machine
Loader, verifier, linker, interpreter Bytecodes for method lookup
Security issues
Point Class
class Point { private int x; protected void setX (int y) {x = y;} public int getX() {return x;} Point(int xval) {x = xval;} // constructor };
Dynamic lookup
Similar in behavior to other languages Static typing => more efficient than Smalltalk Dynamic linking, interfaces => slower than C++ Visibility similar to C++, but not exactly (later slide)
Object initialization
Java guarantees constructor call for each object
Memory allocated Constructor called to initialize memory Some interesting issues related to inheritance
Well discuss later
Problem
What if object has opened file or holds lock?
Solution
finalize method, called by the garbage collector
Before space is reclaimed, or when virtual machine exits Space overflow is not really the right condition to trigger finalization when an object holds a lock...)
Inheritance
Similar to Smalltalk, C++ Subclass inherits from superclass
Single inheritance only (but Java has interfaces)
Example subclass
class ColorPoint extends Point { // Additional fields and methods private Color c; protected void setC (Color d) {c = d;} public Color getC() {return c;} // Define constructor ColorPoint(int xval, Color cval) { super(xval); // call Point constructor c = cval; } // initialize ColorPoint field };
Class Object
Every class extends another class
Superclass is Object if no other class named
Outline
Objects in Java
Classes, encapsulation, inheritance
Example
java.lang.String
Type system
Primitive types, interfaces, arrays, exceptions
Virtual machine
Loader, verifier, linker, interpreter Bytecodes for method lookup
Security issues
Java Types
Two general kinds of types
Primitive types not objects
Integers, Booleans, etc
Reference types
Classes, interfaces, arrays No syntax distinguishing Object * from Object
Exception types
float
long
Subtyping
Primitive types
Conversions: int -> long, double -> long,
Interfaces
Completely abstract classes
no implementation
Multiple subtyping
Interface can have multiple subtypes (implements, extends)
Java rule
Java 1.1: Arguments and returns must have identical types, may remove exceptions Java 1.5: covariant return type specialization
Arrays
Covariant subtyping not consistent with semantic principles
Properties of interfaces
Flexibility
Allows subtype graph instead of tree Avoids problems with multiple inheritance of implementations (remember C++ diamond)
Cost
Offset in method lookup table not known at compile Different bytecodes for method lookup
one when class is known one when only interface is known search for location of method cache for use next time this call is made (from this line) More about this later
Array types
Automatically defined
Array type T[ ] exists for each class, interface type T Cannot extended array types (array types are final) Multi-dimensional arrays are arrays of arrays: T[ ] [ ]
Array subtyping
Covariance
if S <: T then S[ ] <: T[ ]
Explanation
interface of T reference cell is
put : T T ref get : T ref T
Java Exceptions
Similar basic functionality to ML, C++
Constructs to throw and catch exceptions Dynamic scoping of handler
Exception Classes
Throwable Exception checked exceptions Runtime Exception Error
Some differences
An exception is an object from an exception class Subtyping between exception classes
Use subtyping to match type of exception or pass it on Similar functionality to ML pattern matching in handler
Unchecked exceptions
If a method may throw a checked exception, then this must be in the type of the method
Try/finally blocks
Exceptions are caught in try blocks
try { statements } catch (ex-type1 identifier1) { statements } catch (ex-type2 identifier2) { statements } finally { statements }
Outline
Objects in Java
Classes, encapsulation, inheritance
Type system
Primitive types, interfaces, arrays, exceptions
Virtual machine
Loader, verifier, linker, interpreter Bytecodes for method lookup
Security issues
Java 1.0
vs Generics
class Stack<A> { void push(A a) { ... } A pop() { ... } ...} String s = "Hello"; Stack<String> st = new Stack<String>(); st.push(s); ... s = st.pop();
class Stack { void push(Object o) { ... } Object pop() { ... } ...} String s = "Hello"; Stack st = new Stack(); ... st.push(s); ... s = (String) st.pop();
Another example
interface LessAndEqual<I> { boolean lessThan(I); boolean equal(I); } class Relations<C extends LessAndEqual<C>> extends C { boolean greaterThan(Relations<C> a) { return a.lessThan(this); } boolean greaterEqual(Relations<C> a) { return greaterThan(a) || equal(a); } boolean notEqual(Relations<C> a) { ... } boolean lessEqual(Relations<C> a) { ... } ... }
Implementing Generics
Type erasure
Compile-time type checking uses generics Compiler eliminates generics by erasing them
Compile List<T> to List, T to Object, insert casts
Outline
Objects in Java
Classes, encapsulation, inheritance
Type system
Primitive types, interfaces, arrays, exceptions
Virtual machine
Loader, verifier, linker, interpreter Bytecodes for method lookup Bytecode verifier (example: initialize before use) Implementation of generics
Security issues
Java Implementation
Compiler and Virtual Machine
Compiler produces bytecode Virtual machine loads classes on demand, verifies bytecode properties, interprets bytecode
A.class
B.class
Class loader
Runtime system loads classes as needed
When class is referenced, loader searches for file of compiled bytecode instructions
method area
heap
Java stacks
PC registers
Verifier
Check bytecode of a class or interface before loaded Throw VerifyError exception if error occurs
Initialization is important
Cannot initialize class fields until loaded
Verifier
Bytecode may not come from standard compiler
Evil hacker may write dangerous bytecode
Bytecode interpreter
Standard virtual machine interprets instructions
Perform run-time checks such as array bounds Possible to compile bytecode class file to native code
Bytecode
Method void f(int) aload 0 ; object ref this iload 1 ; int val iconst 1 iadd ; add val +1 putfield #4 <Field int i> return refers to const pool
Additional features
Automatic garbage collection No pointer arithmetic If program accesses memory, that memory is allocated to the program and declared with correct type
invokeinterface <method-spec>
Sample code
void add2(Incrementable x) { x.inc(); x.inc(); }
First execution
Use symbolic name to find field or method
Second execution
Use modified quick instruction to simplify search
search the method table for this class find method with the given name and signature
10
invokevirtual <method-spec>
Similar to invokeinterface, but class is known Search for method
search the method table of this class find method with the given name and signature
inv_int_quick
A.foo()
After search, rewrite bytcode to use fixed offset into the vtable. No search on second execution.
Bytecode Verifier
Lets look at one example to see how this works Correctness condition
No operations should be invoked on an object until it has been initialized
Object creation
Example:
Point p = new Point(3) 1: new Point 2: dup 3: iconst 3 4: init Point Java source
Bytecode instructions
new class allocate memory for object init class initialize object on top of stack use class use object on top of stack
bytecode
11
Alias Analysis
Other situations:
1: new P 2: new P 3: init P
Tracking initialize-before-use
Alias analysis uses line numbers or
new P init P Two pointers to unitialized object created at line 47 are assumed to point to same object All accessible objects must be initialized before jump backwards (possible loop)
Implementing Generics
Two possible implementations
Heterogeneous: instantiate generics Homogeneous: translate generic class to standard class
Homogeneous Implementation
Heterogeneous Implementation
next
next
next next
12
Issues
Data on heap, manipulated by pointer
Every list cell has two pointers, data and next All pointers are same size Can use same representation, code for all types
Heterogeneous Implementation
Compile generic class C<param>
Check use of parameter type according to constraints Produce extended form of bytecode class file
Store constraints, type parameter names in bytecode file
13
Algorithm
replace class parameter <A> by Object, insert casts if <A extends B>, replace A by B
Resolve overloading
Heterogeneous: could try to resolve at instantiation time (C++) Homogenous: no information about type parameter
Outline
Objects in Java
Classes, encapsulation, inheritance
Java Security
Security
Prevent unauthorized use of computational resources
Type system
Primitive types, interfaces, arrays, exceptions
Java security
Java code can read input from careless user or malicious attacker Java code can be transmitted over network code may be written by careless friend or malicious attacker
Virtual machine
Loader, verifier, linker, interpreter Bytecodes for method lookup Bytecode verifier (example: initialize before use) Implementation of generics
Security issues
Code signing
Use cryptography to establish origin of class file
This info can be used by security manager
14
Java Sandbox
Four complementary mechanisms
Class loader
Separate namespaces for separate class loaders Associates protection domain with each class
Function
Copies str into buffer until null character found Could write past end of buffer,
Calling program
Writes 'A' over f activation record Function f returns to location 0x4141414141 This causes segmentation fault
Security Manager
Called by library functions to decide if request is allowed Uses protection domain associated with code, user policy Recall: stack inspection problem on midterm
Variations
Put meaningful address in string Put code in string and jump to it !!
Security Manager
Java library functions call security manager Security manager object answers at run time
Decide if calling code is allowed to do operation Examine protection domain of calling class
Signer: organization that signed code before loading Location: URL where the Java classes came from
/* assign address stored in an integer var */ /* call the function at this address */
Stack Inspection
Permission depends on
Permission of calling method Permission of all methods above it on stack
Up to method that is trusted and asserts this trust
15
Java Summary
Objects
have fields and methods alloc on heap, access by pointer, garbage collected
Classes
Public, Private, Protected, Package (not exactly C++) Can have static (class) members Constructors and finalize methods
Virtual machine
Load bytecode for classes at run time Verifier checks bytecode Interpreter also makes run-time checks
type casts array bounds
Inheritance
Single inheritance Final classes and methods
Some Highlights
Dynamic lookup
Different bytecodes for by-class, by-interface Search vtable + Bytecode rewriting or caching
Subtyping
Interfaces instead of multiple inheritance Awkward treatment of array subtyping (my opinion)
Type safe
Generics
Type checked, not instantiated, some limitations (<T>new T)
+ Safety +/- Code complexity - Efficiency Arrays are bounds checked No pointer arithmetic, no unchecked type casts Garbage collected
Bytecode-based JVM
Bytcode verifier Security: security manager, stack inspection
Interpreted
+ Portability + Safety - Efficiency Compiled to byte code: a generalized form of assembly language designed to interpret quickly. Byte codes contain type information
Comparison
Objects accessed by ptr
(contd)
Generics
http://www.langer.camelot.de/Resources/Links/Java Generics.htm
Exceptions
As in C++, integral part of language design
16
17