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

for sound object initialization
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

for Sound Object Initialization

Xin Qi and Andrew C. Myers Cornell University

Friday, June 3, 2011

slide-2
SLIDE 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

2 Masked Types Xin Qi

Friday, June 3, 2011

slide-3
SLIDE 3

Alice wants a data structure…

A data structure… … …

3 Masked Types Xin Qi

Friday, June 3, 2011

slide-4
SLIDE 4

Alice wants a data structure…

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

slide-5
SLIDE 5

An example with inheritance

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

slide-6
SLIDE 6

A bug with no one to blame

 Each individual

class looks OK

 Classes don’t

agree on the initialization contract

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

slide-7
SLIDE 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

7 Masked Types Xin Qi

Friday, June 3, 2011

slide-8
SLIDE 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

8 Masked Types Xin Qi

Friday, June 3, 2011

slide-9
SLIDE 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

9 Masked Types Xin Qi

Friday, June 3, 2011

slide-10
SLIDE 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

10 Masked Types Xin Qi

Friday, June 3, 2011

slide-11
SLIDE 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

slide-12
SLIDE 12

Back to the example

Xin Qi Masked Types 12

 If we blame the

Point class, …

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

slide-13
SLIDE 13

Back to the example

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

Point class, …

 Compiler

inserts default effects

Friday, June 3, 2011

slide-14
SLIDE 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

14 Masked Types Xin Qi

Friday, June 3, 2011

slide-15
SLIDE 15

class ¡Node ¡{ ¡ Node ¡next; } Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); x.next ¡= ¡y; … y.next ¡= ¡x;

An example

15 Masked Types Xin Qi

x y

next next y.next uninitialized ⇒ not safe to read x.next “ties the knot” ⇒ both objects are safe to use

 Conditional masks

 Dependencies between masks  Graph theory-based type checking

Friday, June 3, 2011

slide-16
SLIDE 16

class ¡Node ¡{ ¡ Node ¡next; } Node ¡x ¡= ¡new ¡Node(); Node ¡y ¡= ¡new ¡Node(); x.next ¡= ¡y; … y.next ¡= ¡x;

An example

x : Node \ next[y.next] x : Node \ next[y.next] y : Node \ next[x.next]

x.next y.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

slide-17
SLIDE 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

17 Masked Types Xin Qi

Friday, June 3, 2011

slide-18
SLIDE 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

18 Masked Types Xin Qi

Friday, June 3, 2011

slide-19
SLIDE 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

19 Masked Types Xin Qi

Friday, June 3, 2011

slide-20
SLIDE 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

20 Masked Types Xin Qi

Friday, June 3, 2011

slide-21
SLIDE 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!

21 Masked Types Xin Qi

Friday, June 3, 2011

slide-22
SLIDE 22

J\mask source code available at http://www.cs.cornell.edu/Projects/jmask/

22 Masked Types Xin Qi

Friday, June 3, 2011