Ownership-Based Alias Management
Total Page:16
File Type:pdf, Size:1020Kb
DSV Report series No. - ISBN --- ISSN - ISRN SU-KTH/DSV/R–/–SE Abstract Object-oriented programming relies on sharing and the mutable states of objects for common data structures, patterns and programming idioms. Sharing and mutable state is a powerful but dangerous combination. Uncontrolled aliasing risks causing representation exposure, where an object’s state is exposed and modifiable out of the control of its conceptually owning object. This breaks encapsulation, and hence, in extension, abstraction. Contemporary object-oriented programming languages’ support for alias encap- sulation is mediocre and easily circumvented. To this end, several proposals have been put forward that strengthen encapsulation to enable construction of more reliable sys- tems and formally reasoning about properties of programs. These systems are vastly superior to the constructs found in for example C++, Java or C#, but have yet to gain acceptance outside the research community. In this thesis, we present three constructs for alias management on top of a deep ownership types system in the context of the Joline programming language. Our con- structs are fully statically checkable and impose little run-time overhead. We show the formal semantics and soundness proof for our constructs as well as their formal and informal aliasing properties. We show applications and extensions and perform a practical evaluation of our system with our implemented Joline compiler. The evalua- tion suggests that our constructs are compatible with real-world programming, makes use of some of our own proposed patterns, and encourages further practical studies of programming with ownership-based constructs for alias management. ii Contents Introduction . Living with Aliasing ............................ .. Alias Encapsulation and Pointer Restrictions .......... . Contributions of This Dissertation .................... . Outline .................................. Background and Related Work . Aliasing .................................. .. Different Types of Aliases .................... .. Representation Exposure ..................... . Encapsulation ............................... .. Name-based Encapsulation and Selective Export ........ . Related Work on Encapsulation ..................... .. Unique Pointers ......................... .. Systems offering Strong Encapsulation ............. .. Systems offering Lightweight Encapsulation .......... .. Ownership-Based Systems .................... . Related Alias Management Techniques .................. .. Effects and Read-Only ...................... .. Separation Logic and Representation Independence ...... . Concluding Remarks ........................... The Joline Programming Language . Ownership Types in Joline ........................ iii CONTENTS .. Implementing Deep Ownership ................. .. Encapsulation Example ..................... . Joline, Statically .............................. .. Joline’s Syntax .......................... .. Joline’s Type System ....................... .. Well-formedness Rules ...................... . Joline, Dynamically ............................ .. Syntax definitions ........................ .. Store Type ............................ .. Configurations .......................... .. Operational Semantics for Joline ................ . Soundness of The Joline Language .................... .. Helper Functions ......................... .. Lemmas ............................. .. Subject Reduction ........................ .. Canonical Forms ......................... .. Progress ............................. . Owners-as-Dominators .......................... .. Helper Functions ......................... .. Owners-as-Dominators ..................... . Concluding Remarks ........................... Owner-Polymorphic Methods . Ownership and Dynamic Aliasing .................... .. Ownership and Argument-Polymorphic Methods ....... .. Borrowing and the Preservation of Separation ......... .. Problem Analysis ......................... . Owner-Polymorphic Methods ...................... .. Informal Syntax and Semantics ................. .. Solutions to the Problems in Sections .. and .. ...... .. Borrowing and Preservation of Separation ........... .. The “Hide Owner” Pattern ................... .. Discussion ............................ . Formalising Owner-Polymorphic Methods ................ iv CONTENTS .. Static Semantics ......................... .. Dynamic Semantics ....................... . Concluding Remarks ........................... Scoped Regions . Stack-Based Confinement ........................ .. Stack-Based Confinement in Joline ............... .. Value Objects ........................... . Scoped Regions .............................. . Formalising Scoped Regions ....................... .. Static Semantics ......................... .. Dynamic Semantics ....................... .. Related Work ........................... . Concluding Remarks ........................... External Uniqueness . Contribution ............................... . Recapping Uniqueness .......................... .. Uniqueness and Object-Orientation ............... .. Problems with Class-Level and Method-Level annotations .. .. Uniqueness and a Problem with Abstraction .......... .. Uniqueness and Aggregates ................... . External Uniqueness ........................... .. Unique Owners ......................... .. Operations on Externally Unique Pointers ........... .. Creating Unique Objects ..................... . Discussion ................................ .. Aggregate Uniqueness and Dominating Edges ......... .. Back-pointers and Effective Uniqueness ............. .. Overcoming the Abstraction Problem .............. . Formalising External Uniqueness .................... .. Static Semantics ......................... .. Dynamic Semantics ....................... .. Lemmas for Unique and Borrowing Pointers .......... .. Subject Reduction Proof ..................... v CONTENTS . External-Uniqueness-as-Dominating-Edges ............... . Concluding Remarks ........................... Discussion . Generational Ownership ......................... .. Stack Frames are Generations .................. .. Ownership is a dag ........................ . Orthogonality ............................... Applications . Applications for External Uniqueness .................. .. Transfer of Ownership ...................... .. Merging Representations ..................... .. Simulating Borrowing ...................... .. Movable Aliased Object Pattern ................. .. The Initialisation Problem .................... Extensions . Unique Borrowing ............................ .. Towards Unique Borrowing ................... .. Borrowing Blocks that Preserve Uniqueness ........... . Existential Downcasting ......................... .. The Importance of Downcasting ................ .. Existential Owners ........................ . Iteration Revisited ............................ Practical Evaluation . The Joline Compiler ........................... .. Class Variables .......................... .. String Literals .......................... .. Studies of Aliasing and Programming with Ownership ..... . Case Studies ................................ .. Case : Informal Ownership ................... .. Case : A Library System ..................... .. Case : SuDoku .......................... vi CONTENTS .. Case : Linked List ........................ . Results ................................... .. Design Delicacy ......................... .. The Importance of Generics and Downcasts .......... .. Id-aliases ............................. .. Unique Listener Proxy Pattern .................. .. Syntactic Overhead ........................ .. Concluding Remarks ....................... Conclusions . Critique .................................. .. The Joline Language ....................... .. Practical Evaluation ....................... . Summary of Conclusions ......................... . Future Work ............................... vii CONTENTS viii List of Figures . Alias types ................................. . Representation Exposure ......................... . Lack of expressiveness in contemporary programming languages .... . Selective Export in Eiffel ......................... . Programming with Unique Pointers ................... . Programming with Borrowing ...................... . Deep ownership .............................. . A linked list and links using ownership types .............. . Object graph for linked list example ................... . Syntax for stacks, heaps and values. ................... . Store type ................................. . Object graph and corresponding store-type. ............... . Possible paths. .............................. . Cannot express that arg should be borrowed .............. . Owner-Polymorphic Factory Method .................. . Scoped Region and owner-polymorphic methods ............ . Destructive reads and manual reinstatement ............... . Alias burying to avoid destructive reads ................. . Class-level annotations .......................... . Using method-level annotations ..................... . Subclassing with class-level annotations ................. ix LIST OF FIGURES . Sharing precludes thread-localness .................... . Moving causing slicing .......................... . External Uniqueness ..........................