for sound object initialization
play

for Sound Object Initialization Xin Qi and Andrew C. Myers Cornell - PowerPoint PPT Presentation

for Sound Object Initialization Xin Qi and Andrew C. Myers Cornell University Friday, June 3, 2011 Fix the initialization problem Current mechanisms for object initialization are unsound This talk: a lightweight type system for sound


  1. for Sound Object Initialization Xin Qi and Andrew C. Myers Cornell University Friday, June 3, 2011

  2. Fix the initialization problem  Current mechanisms for object initialization are unsound  This talk: a lightweight type system for sound initialization  Gets rid of null-pointer exceptions  Handles inheritance and cycles  Implementation – J\mask Xin Qi Masked Types 2 Friday, June 3, 2011

  3. Alice wants a data structure… A data structure… … … Xin Qi Masked Types 3 Friday, June 3, 2011

  4. Alice wants a data structure… Remember: initialize before use! … Initialization Invariants established No access to Normal use uninitialized data  This methodology does not work! Xin Qi Masked Types 4 Friday, June 3, 2011

  5. An example with inheritance class ¡Point ¡{ ¡ ¡int ¡x, ¡y; ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡void ¡display() ¡{ Super ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y); constructor ¡ ¡} } Virtual method call class ¡CPoint ¡extends ¡Point ¡{ ¡ ¡Color ¡c; Field c not ¡ ¡CPoint(int ¡x, ¡int ¡y, ¡Color ¡c) ¡{ ¡ ¡ ¡ ¡super(x, ¡y); initialized yet! ¡ ¡ ¡ ¡this.c ¡= ¡c; ¡ ¡} ¡ ¡void ¡display() ¡{ ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y ¡+ ¡“ ¡“ ¡+ ¡c.name()); ¡ ¡} } Xin Qi Masked Types 5 Friday, June 3, 2011

  6. A bug with no one to blame class ¡Point ¡{ ¡ ¡int ¡x, ¡y;  Each individual ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; class looks OK ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡void ¡display() ¡{  Classes don’t ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y); ¡ ¡} agree on the } initialization class ¡CPoint ¡extends ¡Point ¡{ ¡ ¡Color ¡c; contract ¡ ¡CPoint(int ¡x, ¡int ¡y, ¡Color ¡c) ¡{ ¡ ¡ ¡ ¡super(x, ¡y); ¡ ¡ ¡ ¡this.c ¡= ¡c; ¡ ¡} ¡ ¡void ¡display() ¡{ ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y ¡+ ¡“ ¡“ ¡+ ¡c.name()); ¡ ¡} } Xin Qi Masked Types 6 Friday, June 3, 2011

  7. Unsound initialization  Problem: initialization is unsound : Can read uninitialized object fields   “Solution” (Java/C#): fields pre-initialized with default “null” values Null is a value of all object types  Ubiquitous null checks and possible null-pointer  exceptions  Result: unreliable software Xin Qi Masked Types 7 Friday, June 3, 2011

  8. Current language support  Object-oriented initialization is unsound  Inheritance  Cyclic data structures  Functional languages trade expressiveness for soundness  Cyclic data structures need encoding/refs Xin Qi Masked Types 8 Friday, June 3, 2011

  9.  T \ f  Base type T  Field mask on f Possibly uninitialized  Not readable   Assignments remove masks // x : CPoint \ c x.c = new Color(“Blue”); // x : CPoint  Typestates Xin Qi Masked Types 9 Friday, June 3, 2011

  10. More masks  T \ *  Disallows reading any field  Point \ Point.sub  Disallows reading fields declared in subclasses  Point \ * = Point \ x \ y \ Point.sub  Abstract masks for data abstraction Xin Qi Masked Types 10 Friday, June 3, 2011

  11. Inheritance  Make initialization contracts explicit  Methods and constructors have mask effects  Capture initialization contracts  Support modular type-checking Xin Qi Masked Types 11 Friday, June 3, 2011

  12. Back to the example class ¡Point ¡{ ¡ ¡int ¡x, ¡y; effect ¡* ¡-­‑> ¡Point.sub ¡{ ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ Point \ x \ y \ Point.sub Point \ * ¡ ¡ ¡ ¡this.x ¡= ¡x; Point \ y \ Point.sub ¡ ¡ ¡ ¡this.y ¡= ¡y;  If we blame the Point \ Point.sub ¡ ¡ ¡ ¡display(); ¡ ¡} Point class, … effect ¡{} ¡-­‑> ¡{} ¡{ ¡ ¡void ¡display() ¡{ ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y); ¡ ¡} } class ¡CPoint ¡extends ¡Point ¡{ ¡ ¡Color ¡c; ¡ ¡CPoint(int ¡x, ¡int ¡y, ¡Color ¡c) ¡{ ¡ ¡ ¡ ¡super(x, ¡y); ¡ ¡ ¡ ¡this.c ¡= ¡c; ¡ ¡} ¡ ¡void ¡display() ¡{ ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y ¡+ ¡“ ¡“ ¡+ ¡c.name()); ¡ ¡} } Xin Qi Masked Types 12 Friday, June 3, 2011

  13. Back to the example class ¡Point ¡{ ¡ ¡int ¡x, ¡y; effect ¡* ¡-­‑> ¡Point.sub ¡{ ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y;  If we blame the Point \ Point.sub ¡ ¡ ¡ ¡display(); ¡ ¡} Point class, … effect ¡{} ¡-­‑> ¡{} ¡{ ¡ ¡void ¡display() ¡{ Method call ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y); ¡ ¡} disallowed!  Compiler } class ¡CPoint ¡extends ¡Point ¡{ inserts default ¡ ¡Color ¡c; ¡ ¡CPoint(int ¡x, ¡int ¡y, ¡Color ¡c) ¡{ effects ¡ ¡ ¡ ¡super(x, ¡y); ¡ ¡ ¡ ¡this.c ¡= ¡c; ¡ ¡} ¡ ¡void ¡display() ¡{ ¡ ¡ ¡ ¡System.out.println(x ¡+ ¡“ ¡“ ¡+ ¡y ¡+ ¡“ ¡“ ¡+ ¡c.name()); ¡ ¡} } Xin Qi Masked Types 13 Friday, June 3, 2011

  14. Cyclic data structures  Cyclic data structures are common  Doubly-linked lists  Circular lists  Binary trees with parent pointers  Sound initialization is challenging  Disallow reading fields pointing to “incomplete” objects  Know when initialization completes Xin Qi Masked Types 14 Friday, June 3, 2011

  15. An example next class ¡Node ¡{ x y ¡ Node ¡next; } next Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); y.next uninitialized x.next ¡= ¡y; ⇒ not safe to read x.next … y.next ¡= ¡x; “ties the knot” ⇒ both objects are safe to use  Conditional masks  Dependencies between masks  Graph theory-based type checking Xin Qi Masked Types 15 Friday, June 3, 2011

  16. An example class ¡Node ¡{ ¡ Node ¡next; Conditionally } masked type Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); x.next ¡= ¡y; x : Node \ next[y.next] … y.next ¡= ¡x; x : Node \ next[y.next] y : Node \ next[x.next] Depends on Removal of circular dependencies x.next y.next x : Node Depends on y : Node Xin Qi Masked Types 16 Friday, June 3, 2011

  17. J\mask calculus  Object calculus with heap  No special value “null”  Uninitialized fields cannot be read  Object initialization is sound  Evaluation never gets stuck  Proof: Encoding of graph theoretical problems  progress + preservation  Xin Qi Masked Types 17 Friday, June 3, 2011

  18. J\mask language  Constructors not special  Default effects reduce annotation burden  Implementation  Polyglot compiler framework (Nystrom, Clarkson & Myers 03)  Flow-sensitive type system  Translation to Java by type erasure Xin Qi Masked Types 18 Friday, June 3, 2011

  19. Experience  Java Collections Framework (1.4.2) LinkedList, ArrayList, HashMap, TreeMap,  Stack, … 29 source files, 18,000 LOC   Results Handled JCF initialization patterns  Removed nulls for initialization  Low annotation burden  11 explicit effects  11 explicit masked types  Xin Qi Masked Types 19 Friday, June 3, 2011

  20. Related work  Non-null types @NonNull annotations (Java 6/7)  Delayed types (F ä hndrich & Xia 07)   Typestates Typestates for objects (DeLine & F ä hndrich 04)  Heap monotonic typestates (F ä hndrich & Leino 03)   Static analysis Detecting null-pointer exceptions (FindBugs)  Shape analysis  Xin Qi Masked Types 20 Friday, June 3, 2011

  21. Summary Sound and expressive initialization  Handles inheritance and cycles  Local, modular reasoning  Mask effects  Abstract masks  Lightweight  Low annotation burden  No aliasing information  Default annotation  No run-time overhead  Maybe the end of null-pointer exceptions!  Xin Qi Masked Types 21 Friday, June 3, 2011

  22. J\mask source code available at http://www.cs.cornell.edu/Projects/jmask/ Xin Qi Masked Types 22 Friday, June 3, 2011

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend