for Sound Object Initialization
Xin Qi and Andrew C. Myers Cornell University
Friday, June 3, 2011
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
Friday, June 3, 2011
Gets rid of null-pointer exceptions Handles inheritance and cycles
2 Masked Types Xin Qi
Friday, June 3, 2011
A data structure… … …
3 Masked Types Xin Qi
Friday, June 3, 2011
Remember: initialize before use! Initialization … Normal use Invariants established No access to uninitialized data
This methodology does not work!
4 Masked Types Xin Qi
Friday, June 3, 2011
Super constructor Field c not initialized yet!
5 Masked Types Xin Qi
class ¡Point ¡{ ¡ ¡int ¡x, ¡y; ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡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()); ¡ ¡} }
Virtual method call
Friday, June 3, 2011
Each individual
Classes don’t
6 Masked Types Xin Qi
class ¡Point ¡{ ¡ ¡int ¡x, ¡y; ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡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()); ¡ ¡} }
Friday, June 3, 2011
Problem: initialization is unsound:
“Solution” (Java/C#): fields pre-initialized
Result: unreliable software
7 Masked Types Xin Qi
Friday, June 3, 2011
Inheritance Cyclic data structures
Cyclic data structures need encoding/refs
8 Masked Types Xin Qi
Friday, June 3, 2011
Base type T Field mask on f
9 Masked Types Xin Qi
Friday, June 3, 2011
Disallows reading any field
Disallows reading fields declared in
Point \ * = Point \ x \ y \ Point.sub
10 Masked Types Xin Qi
Friday, June 3, 2011
Capture initialization contracts Support modular type-checking
Xin Qi Masked Types 11
Friday, June 3, 2011
Xin Qi Masked Types 12
If we blame the
Point \ * Point \ y \ Point.sub Point \ Point.sub Point \ x \ y \ Point.sub
class ¡Point ¡{ ¡ ¡int ¡x, ¡y; ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡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()); ¡ ¡} }
effect ¡* ¡-‑> ¡Point.sub ¡{ effect ¡{} ¡-‑> ¡{} ¡{
Friday, June 3, 2011
Xin Qi Masked Types 13
class ¡Point ¡{ ¡ ¡int ¡x, ¡y; ¡ ¡Point(int ¡x, ¡int ¡y) ¡{ ¡ ¡ ¡ ¡this.x ¡= ¡x; ¡ ¡ ¡ ¡this.y ¡= ¡y; ¡ ¡ ¡ ¡display(); ¡ ¡} ¡ ¡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()); ¡ ¡} }
effect ¡* ¡-‑> ¡Point.sub ¡{ effect ¡{} ¡-‑> ¡{} ¡{
Point \ Point.sub Method call disallowed!
If we blame the
Compiler
Friday, June 3, 2011
Doubly-linked lists Circular lists Binary trees with parent pointers
Disallow reading fields pointing to
Know when initialization completes
14 Masked Types Xin Qi
Friday, June 3, 2011
class ¡Node ¡{ ¡ Node ¡next; } Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); x.next ¡= ¡y; … y.next ¡= ¡x;
15 Masked Types Xin Qi
Dependencies between masks Graph theory-based type checking
Friday, June 3, 2011
class ¡Node ¡{ ¡ Node ¡next; } Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); x.next ¡= ¡y; … y.next ¡= ¡x;
x : Node \ next[y.next] x : Node \ next[y.next] y : Node \ next[x.next]
Depends on Depends on
x : Node y : Node Removal of circular dependencies
16 Masked Types Xin Qi
Conditionally masked type
Friday, June 3, 2011
No special value “null” Uninitialized fields cannot be read
Evaluation never gets stuck Proof:
17 Masked Types Xin Qi
Friday, June 3, 2011
Polyglot compiler framework (Nystrom,
Flow-sensitive type system Translation to Java by type erasure
18 Masked Types Xin Qi
Friday, June 3, 2011
Java Collections Framework (1.4.2)
Results
19 Masked Types Xin Qi
Friday, June 3, 2011
Non-null types
Typestates
Static analysis
20 Masked Types Xin Qi
Friday, June 3, 2011
No aliasing information
Default annotation
21 Masked Types Xin Qi
Friday, June 3, 2011
22 Masked Types Xin Qi
Friday, June 3, 2011