390 likes | 534 Views
Generic Ownership for Generic Java. Alex Potanin , Dave Clarke (CWI) James Noble, Robert Biddle (Carleton). Introduction. OO languages provide name-based encapsulation: class Rectangle { private Point topLeft; }
E N D
Generic Ownership for Generic Java Alex Potanin, Dave Clarke (CWI) James Noble, Robert Biddle (Carleton)
Introduction • OO languages provide name-based encapsulation: class Rectangle { private Point topLeft; } • Not a guarantee that an object pointed at by a private field is not referred to by another name (alias)
Introduction • Ownership allows a granular control of which objects are allowed to have references to which objects Generic Ownership is a unified approach of providing ownership and generics in a programming language
Aliasing (The Good) • Aliasing is widely used in programming • A lot of data structures and design patterns need it List Doubly Linked List Head Tail Node Node
Aliasing (The Bad) class Rectangle { private Point topLeft; private int width, height; } ... Point p = new Point(100, 50); Rectangle r = new Rectangle(p,300,200); p.setX(400);
Aliasing (The Ugly) • Bug in Sun JDK v1.1.1 • Reported by SIPG in ‘97 java.security package All System Identities Some Applet Identity Identity Malicious Applet Identity Identity Identity getAllSystemIdentities() getSigners() Identity Identity Malicious Applet Identity Identity Identities[] all = SM.getAllSystemIdentities() Identities[] me = getSigners() // Say, item 42 is “allow all” me[0] = all[42]; // Copy extra permissions // (Identities) from a // complete list to // applet specific list! Identity Identity Identity Identity Identity Identity … Example from Confined Types by Bokowski and Vitek (OOPSLA’99)
Aliasing “The big lie of object-oriented programming is that objects provide encapsulation” John Hogg Islands: Aliasing Protection in Object-Oriented Languages OOPSLA’91
Ownership • Ownership Types allow us to ensure that objects can’t escape their owners because of irresponsible handling of references to them class Rectangle<Owner extends World> { private Point<This> topLeft; } • This marks these fields as being accessible by the current instance of Rectangle only Dave Clarke, John Potter, James Noble Ownership Types for Flexible Aliasing Protection, OOPSLA’98
Ownership HelloWorld.java LinkedList.java
Java 1.4 Box Box Box Book Book Book Book Book Box Box
Java 5 Generics Box (Book) Box (Bird)
Ownership Book (Library)
Ownership Book (Robert)
Ownership Book (Robert) Book (Me)
Generic Ownership Box of Robert’s Books
Generic Ownership Box of My Computer Books
Question • How do we get this into a language?
Simple Map in Java Box Book Book class Node { . . . } public class Map { private Vector nodes; public Vector expose() { return this.nodes; } void put(Object key, Object value) { nodes.add(new Node(key, value)); } Object get(Object k) { Iterator i = nodes.iterator(); while (i.hasNext()) { Node mn = (Node) i.next(); if (mn.key.equals(k)) return mn.value; } return null; } } public class Map { Map books = new Map(); books.put(“Wisdom”, new Book()); Object b = books.get(“Wisdom”); // Don’t know what get returns! Vector aliasedNodes = books.expose(); // Private field exposed!
Generic Map in Java 5 class Node<Key, Value> { . . . } public class Map<Key, Value> { private Vector<Node<Key, Value>> nodes; public Vector<Node<Key, Value>> expose() { return this.nodes; } void put(Key key, Value value) { nodes.add(new Node<Key, Value>(key, value)); } Value get(Key k) { Iterator<Node<Key, Value>> i = nodes.iterator(); while (i.hasNext()) { Node<Key, Value> mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; } } public class Map<Key, Value> { Box (Bird) Box (Book) Map books = new Map(); books.put(“Wisdom”, new Book()); Object b = books.get(“Wisdom”); // Don’t know what get returns! Vector aliasedNodes = books.expose(); // Private field exposed! Map<String, Book> books = new Map<String, Book>(); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector<Node<String, Book>> aliasedNodes = books.expose(); // Exposed!
Ownership Map in Safe Java class Node<nodeOwner, kOwner, vOwner> { . . . } public class Map<mOwner, kOwner, vOwner> { private Vector<this, this> nodes; public Vector<this, this> expose() { return this.nodes; } void put(Object<kOwner> key, Object<vOwner> value) { nodes.add(new Node<this, kOwner, vOwner>(key, value)); } Object<vOwner> get(Object<kOwner> key) { Iterator<this, this> i = nodes.iterator(); while (i.hasNext()) { Node<this, kOwner, vOwner> mn = (Node<this, kOwner, vOwner>) i.next(); if (mn.key.equals(key)) return mn.value; } return null; } } public class Map <mOwner, kOwner, vOwner> { Book (Me) Book (Robert) Map<this, world, world> books = new Map<this, world, world>(); books.put(“Wisdom”, new Book()); Book<world> b = books.get(“Wisdom”); // Don’t know what get returns! Vector<this, this> aliasedNodes = books.expose(); // books. is not this. Map<String, Book> books = new Map<String, Book>(); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector<Node<String, Book>> aliasedNodes = books.expose(); // Exposed!
Ownership + Generic Map class Node<nodeOwner>[Key<kOwner>, Value<vOwner>] { . . . } public class Map<mOwner>[Key<kOwner>, Value<vOwner>] { private Vector<this>[Node<this>[Key<kOwner>, Value<vOwner>]] nodes; public Vector<this>[Node<this>[Key<kOwner>, Value<vOwner>]] expose() { return this.nodes; } void put(Key<kOwner> key, Value<vOwner> value) { nodes.add(new Node<this>[Key<kOwner>, Value<vOwner>](key, value)); } Value<vOwner> get(Key<kOwner> key) { Iterator<this>[Nodes<this>[Key<kOwner>, Value<vOwner>]] = nodes.iterator(); while(i.hasNext()) { Node<this>[Key<kOwner>, Value<vOwner>] mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; } } public class Map<mOwner> [Key<kOwner>, Value<vOwner>] { Map<this>[String<world>, Book<world>] books = new Map<this>[String<world>, Book<world>] (); books.put(“Wisdom”, new Book<world>()); Book<world> b = books.get(“Wisdom”); // Map type knows what get returns Vector<this>[Node<this>[String<world>, Book<world>]] aliasedNodes = books.expose(); // books. is not this. Map<this, world, world> books = new Map<this, world, world>(); books.put(“Wisdom”, new Book()); Book<world> b = books.get(“Wisdom”); // Don’t know what get returns! Vector<this, this> aliasedNodes = books.expose(); // books. is not this.
State of the Art • Aliasing is endemic in object-oriented programming • Ownership (and other schemes like confinement, universes, etc) allow us to control aliasing • The current systems are not very usable by typical programmers • Generics is a popular mechanism in modern OO languages
Generic Ownership • Generics and ownership are orthogonal and should not have anything to do with each other? • Our claim: • Generics and ownership are complementary • Merged into one with Generic Ownership (GO) • GO is the most practical way of having ownership in a modern OO programming language
Ownership + Generic Map class Node<nodeOwner>[Key<kOwner>, Value<vOwner>] { . . . } public class Map<mOwner>[Key<kOwner>, Value<vOwner>] { private Vector<this>[Node<this>[Key<kOwner>, Value<vOwner>]] nodes; public Vector<this>[Node<this>[Key<kOwner>, Value<vOwner>]] expose() { return this.nodes; } void put(Key<kOwner> key, Value<vOwner> value) { nodes.add(new Node<this>[Key<kOwner>, Value<vOwner>](key, value)); } Value<vOwner> get(Key<kOwner> key) { Iterator<this>[Nodes<this>[Key<kOwner>, Value<vOwner>]] = nodes.iterator(); while(i.hasNext()) { Node<this>[Key<kOwner>, Value<vOwner>] mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; } } Map<this>[String<world>, Book<world>] books = new Map<this>[String<world>, Book<world>] (); books.put(“Wisdom”, new Book<world>()); Book<world> b = books.get(“Wisdom”); // Map type knows what get returns Vector<this>[Node<this>[String<world>, Book<world>]] aliasedNodes = books.expose(); // books. is not this.
Generic OwnershipTM Map class Node<Key, Value, Owner extends World> { . . . } public class Map<Key, Value, Owner extends World> { private Vector<Node<Key, Value, This>, This> nodes; public Vector<Node<Key, Value, This>, This> expose() { return this.nodes; } public void put(Key key, Value value) { nodes.add(new Node<Key, Value, This>(key, value)); } public Value get(Key key) { Iterator<Node<Key, Value, This>, This> i = nodes.iterator(); while (i.hasNext()) { Node<Key, Value, This> mn = i.next(); if (mn.key.equals(key)) return mn.value; } return null; } } public class Map<Key, Value, Owner extends World> { Box of My Computer Books Map<String, Book, This> books = new Map<String, Book, This>(); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector<this, this> aliasedNodes = books.expose(); // books. is not this.
Generic Ownership • Proposes the unification of parameterised and ownership types • Starts with a generic type system and adds ownership • The results are surprising: per-package ownership (confinement) comes basically for free and other kinds of ownership are not hard to implement • Ownership shouldn’t be treated orthogonally to type information, rather there is a deep semantic connection between the two • GO is the least intrusive introduction of ownership into a programming language
Generic Ownership Results • Java 5 Extension: Ownership Generic Java: http://www.mcs.vuw.ac.nz/~alex/ogj/ • OGJ is backwards compatible with Java 5 and allows control of who is allowed to access which object • Formalised based on extended FGJ showing that OGJ supports ownership: • A “pure” FGJ+c system adds per-package ownership without affecting the soundness of FGJ • A “full” FGO system provides deep ownership together with generics in a fully imperative setting
Featherweight Generic Java (FGJ) class A extends Object { A() { super(); } } class B extends Object { B() { super(); } } class Pair<X extends Object, Y extends Object> extends Object { X fst; Y snd; Pair(X fst, Y snd) { super(); this.fst = fst; this.snd = snd; } <Z extends Object> Pair<Z,Y> setfst(Z newfst) { return new Pair<Z,Y>(newfst, this.snd); } } Featherweight Java by Igarashi, Pierce, and Wadler in OOPSLA’99
FGO: Imperative FGJ + Ownership =, null, ref’s locals, etc Owners FGO + + = FGJ • Separate hierarchy of owner classes rooted in World that are used to carry ownership for each FGO type • In addition to adding owners to types, we require: • Owner Nesting: provides for owner parameter nesting that enforces ownership • Owner Preservation: ensures owner invariance over subtyping • “This” Rule: prevents non-this access to types owned by particular instances • Placeholder Owners: an owner initialisation mechanism
1. Owner Nesting There is a standard approach to enforcing deep ownership: “Every FGO type has its owner inside other owners involved in the type”
2. Owner Preservation • The central idea of generic ownership is preservation of owners over the subtyping class Student<Owner extends World> extends Person<Owner> { ... } • The owner parameter cannot be cast away or changed at any stage in the FGO program
3. “This” Rule This function is a mechanism preventing access to objects owned by the current object by anything other than this.* class Bar<Owner extends World> { public Foo<this> secret = new Foo<this>(); void m() { Foo<this> f = this.secret; // OK Bar<Owner> b = new Bar<Owner>(); f = b.secret; // NOT OK b = this; f = b.secret; // ALSO NOT OK } }
4. Placeholder Owners • How do we allow a declaration of Map like this, when Key and Value also have their separate owners? class Map<Key, Value, Owner> { ... } • We have a placeholder owners function that produces the missing owners for Key and Value used by the FGO type system
State of the Generic Ownership • Generic Ownership Compiler Prototype (done) • Per Package Ownership Formalism (done) • Generic Ownership Formalism (done) • Implementing OGJ in JavaCOP (done) • OGJ Applications (partially done) • Ownership Inference Formalism (partially done) • Eclipse Full Ownership Inference Tool (to do) • Corpus Analysis of OGJ Effects (to do) • Generalising the Approach (C#, not just Java)
Summary • Ownership and Generics are closely complementary • Language support for ownership with generics is possible (and easier than without generics!) • Generic Ownership is: • First proposal to fully support ownership and generics • Backed by a full formalism and a working compiler • Adopted by other aliasing research groups • OGJ (Ownership Generic Java) is a compiler providing this support http://www.mcs.vuw.ac.nz/~alex/ O + G =