Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generic Ownership for Generic Java Alex Potanin, Dave Clarke (CWI) James Noble, Robert Biddle (Carleton)

Similar presentations


Presentation on theme: "Generic Ownership for Generic Java Alex Potanin, Dave Clarke (CWI) James Noble, Robert Biddle (Carleton)"— Presentation transcript:

1 Generic Ownership for Generic Java Alex Potanin, Dave Clarke (CWI) James Noble, Robert Biddle (Carleton)

2 2 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)

3 3 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

4 4 Aliasing (The Good) Aliasing is widely used in programming A lot of data structures and design patterns need it List Head Node Tail Doubly Linked List

5 5 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);

6 6 Aliasing (The Ugly) Bug in Sun JDK v1.1.1 Reported by SIPG in ‘97 Identities[] all = SM.getAllSystemIdentities() Identities[] me = getSigners() // Say, item 42 is “allow all” me[0] = all[42]; java.security package All System Identities Identity … Some Applet Identity Malicious Applet Identity Malicious Applet getAllSystemIdentities() getSigners() // Copy extra permissions // (Identities) from a // complete list to // applet specific list! Example from Confined Types by Bokowski and Vitek (OOPSLA’99)

7 7 Aliasing “The big lie of object-oriented programming is that objects provide encapsulation” John Hogg Islands: Aliasing Protection in Object-Oriented Languages OOPSLA’91

8 8 Ownership Ownership Types allow us to ensure that objects can’t escape their owners because of irresponsible handling of references to them class Rectangle { private Point 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

9 9 Ownership HelloWorld.java LinkedList.java

10 10 Java 1.4 Box Book Box Book Box

11 11 Java 5 Generics

12 12 Java 5 Generics

13 13 Java 5 Generics

14 14 Java 5 Generics Box (Bird) Box (Book)

15 15 Ownership

16 16 Ownership Book (Library)

17 17 Ownership Book (Robert)

18 18 Ownership Book (Me) Book (Robert)

19 19 Generic Ownership Box of Robert’s Books

20 20 Generic Ownership Box of My Computer Books

21 21 Question How do we get this into a language?

22 22 Simple Map in Java 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; } Book Box 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! public class Map {

23 23 Generic Map in Java 5 Map books = new Map (); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector > aliasedNodes = books.expose(); // Exposed! class Node {... } public class Map { private Vector > nodes; public Vector > expose() { return this.nodes; } void put(Key key, Value value) { nodes.add(new Node (key, value)); } Value get(Key k) { Iterator > i = nodes.iterator(); while (i.hasNext()) { Node mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; } 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! public class Map {

24 24 Ownership Map in Safe Java Map books = new Map (); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector > aliasedNodes = books.expose(); // Exposed! Map books = new Map (); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Don’t know what get returns! Vector aliasedNodes = books.expose(); // books. is not this. 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 key) { Iterator i = nodes.iterator(); while (i.hasNext()) { Node mn = (Node ) i.next(); if (mn.key.equals(key)) return mn.value; } return null; } Book (Me) Book (Robert) public class Map {

25 25 Ownership + Generic Map Map [String, Book ] books = new Map [String, Book ] (); books.put(“Wisdom”, new Book ()); Book b = books.get(“Wisdom”); // Map type knows what get returns Vector [Node [String, Book ]] aliasedNodes = books.expose(); // books. is not this. 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 key) { Iterator [Nodes [Key, Value ]] = nodes.iterator(); while(i.hasNext()) { Node [Key, Value ] mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; } Map books = new Map (); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Don’t know what get returns! Vector aliasedNodes = books.expose(); // books. is not this. public class Map [Key, Value ] {

26 26 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

27 27 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

28 28 Ownership + Generic Map Map [String, Book ] books = new Map [String, Book ] (); books.put(“Wisdom”, new Book ()); Book b = books.get(“Wisdom”); // Map type knows what get returns Vector [Node [String, Book ]] aliasedNodes = books.expose(); // books. is not this. 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 key) { Iterator [Nodes [Key, Value ]] = nodes.iterator(); while(i.hasNext()) { Node [Key, Value ] mn = i.next(); if (mn.key.equals(k)) return mn.value; } return null; }

29 29 Generic Ownership TM Map class Node {... } public class Map { private Vector, This> nodes; public Vector, This> expose() { return this.nodes; } public void put(Key key, Value value) { nodes.add(new Node (key, value)); } public Value get(Key key) { Iterator, This> i = nodes.iterator(); while (i.hasNext()) { Node mn = i.next(); if (mn.key.equals(key)) return mn.value; } return null; } Box of My Computer Books Map books = new Map (); books.put(“Wisdom”, new Book()); Book b = books.get(“Wisdom”); // Type of Map knows what get returns Vector aliasedNodes = books.expose(); // books. is not this. public class Map {

30 30 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

31 31 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: 1.A “pure” FGJ+c system adds per-package ownership without affecting the soundness of FGJ 2.A “full” FGO system provides deep ownership together with generics in a fully imperative setting

32 32 Featherweight Generic Java (FGJ) class A extends Object { A() { super(); } } class B extends Object { B() { super(); } } class Pair extends Object { X fst; Y snd; Pair(X fst, Y snd) { super(); this.fst = fst; this.snd = snd; } Pair setfst(Z newfst) { return new Pair (newfst, this.snd); } Featherweight Java by Igarashi, Pierce, and Wadler in OOPSLA’99

33 33 FGO: Imperative FGJ + Ownership 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: 1.Owner Nesting: provides for owner parameter nesting that enforces ownership 2.Owner Preservation: ensures owner invariance over subtyping 3.“This” Rule: prevents non-this access to types owned by particular instances 4.Placeholder Owners: an owner initialisation mechanism FGJ =, null, ref’s locals, etc ++ Owners = FGO

34 34 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”

35 35 2. Owner Preservation The central idea of generic ownership is preservation of owners over the subtyping class Student extends Person {... } The owner parameter cannot be cast away or changed at any stage in the FGO program

36 36 3. “This” Rule This function is a mechanism preventing access to objects owned by the current object by anything other than this.* class Bar { public Foo secret = new Foo (); void m() { Foo f = this.secret; // OK Bar b = new Bar (); f = b.secret; // NOT OK b = this; f = b.secret; // ALSO NOT OK }

37 37 4. Placeholder Owners How do we allow a declaration of Map like this, when Key and Value also have their separate owners? class Map {... } We have a placeholder owners function that produces the missing owners for Key and Value used by the FGO type system

38 38 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)

39 39 Related Work Ownership –SafeJava Concurrency / Persistence –AliasJava / ArchJava –Ownership in JML Generic Ownership –Ownership Domains (see their demo!) –Generic Universes

40 40 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+


Download ppt "Generic Ownership for Generic Java Alex Potanin, Dave Clarke (CWI) James Noble, Robert Biddle (Carleton)"

Similar presentations


Ads by Google