Share
Embed
LaCasa: Lightweight Affinity and Object Capabilities in Scala
Philipp Haller
September 29, 2016
Transcript
106 slides
-
LaCasa: Lightweight Affinity and Object Capabilities in Scala 1 Philipp
Haller Northeastern University, USA September 29, 2016 KTH Royal Institute of Technology, Sweden -
Concurrency is Difficult Hazards: • Race conditions • Deadlocks •
Livelocks • etc. 2 -
Concurrency is Difficult Hazards: • Race conditions • Deadlocks •
Livelocks • etc. 2 Remedies? -
Concurrency is Difficult Hazards: • Race conditions • Deadlocks •
Livelocks • etc. 2 Monitors Futures, promises Actors CSP Join-calculus STM Async/await Reactive streams … Remedies? -
Concurrency in Scala • Scala = “Scalable Language” • cf.
Guy Steele. Growing a Language. OOPSLA ’98 keynote • Concurrency models as libraries • Reuse of compilers, debuggers, and IDEs • Flexible extension and adaptation 3 -
Concurrency in Scala • Scala = “Scalable Language” • cf.
Guy Steele. Growing a Language. OOPSLA ’98 keynote • Concurrency models as libraries • Reuse of compilers, debuggers, and IDEs • Flexible extension and adaptation 3 Do not have to guess the answer to the question: Which concurrency model is going to “win”? -
Background • Authored or co-authored: • Scala Actors (2006) •
Scala Joins (2008) • Scala futures/promises (2012) • FlowPools (2012) • Scala Async (2013) • Contributions to Akka, Akka.js projects at Typesafe 4 Haller and Sommers Artima Press, 2012 -
Background • Authored or co-authored: • Scala Actors (2006) •
Scala Joins (2008) • Scala futures/promises (2012) • FlowPools (2012) • Scala Async (2013) • Contributions to Akka, Akka.js projects at Typesafe 4 Haller and Sommers Artima Press, 2012 Production use at Twitter, The Guardian and others -
Background • Authored or co-authored: • Scala Actors (2006) •
Scala Joins (2008) • Scala futures/promises (2012) • FlowPools (2012) • Scala Async (2013) • Contributions to Akka, Akka.js projects at Typesafe 4 Haller and Sommers Artima Press, 2012 Production use at Twitter, The Guardian and others Production use at Morgan Stanley, Gawker and others -
Background • Authored or co-authored: • Scala Actors (2006) •
Scala Joins (2008) • Scala futures/promises (2012) • FlowPools (2012) • Scala Async (2013) • Contributions to Akka, Akka.js projects at Typesafe 4 Haller and Sommers Artima Press, 2012 Production use at Twitter, The Guardian and others Production use at Morgan Stanley, Gawker and others The programming-models-as-libraries approach has been successful in Scala! -
Example Image data apply filter Image processing pipeline: filter 1
filter 2 6 Pipeline stages run concurrently -
Example: Implementation • Assumptions: • Image data large • Main
memory expensive 7 -
Example: Implementation • Assumptions: • Image data large • Main
memory expensive • Approach for high performance: • In-place update of image buffers • Pass mutable buffers by-reference 7 -
Example: Problem Easy to produce data races: 1. Stage 1
sends a reference to a buffer to stage 2 2. Following the send, both stages have a reference to the same buffer 3. Stages can concurrently access the buffer 8 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership 9 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership • Compiler prevents sender from accessing objects that have been transferred 9 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership • Compiler prevents sender from accessing objects that have been transferred • Advantages: 9 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership • Compiler prevents sender from accessing objects that have been transferred • Advantages: • No run-time overhead 9 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership • Compiler prevents sender from accessing objects that have been transferred • Advantages: • No run-time overhead • Safety does not compromise performance 9 -
Preventing Data Races • Approach: safe transfer of ownership •
Sending stage loses ownership • Compiler prevents sender from accessing objects that have been transferred • Advantages: • No run-time overhead • Safety does not compromise performance • Errors caught at compile time 9 -
Ownership Transfer in Scala • Enter LaCasa • LaCasa: Scala
extension for affine references 10 -
Ownership Transfer in Scala • Enter LaCasa • LaCasa: Scala
extension for affine references • “Transferable” references 10 -
Ownership Transfer in Scala • Enter LaCasa • LaCasa: Scala
extension for affine references • “Transferable” references • At most one owner per transferable reference 10 -
Motivation • Interesting applications of linearity/affinity/ ownership transfer • Data-race
safety • Safe memory management without GC 11 -
Motivation • Interesting applications of linearity/affinity/ ownership transfer • Data-race
safety • Safe memory management without GC • Session types (with extensions) 11 -
Motivation • Interesting applications of linearity/affinity/ ownership transfer • Data-race
safety • Safe memory management without GC • Session types (with extensions) • Scala for embedded real-time systems? 11 -
Motivation • Interesting applications of linearity/affinity/ ownership transfer • Data-race
safety • Safe memory management without GC • Session types (with extensions) • Scala for embedded real-time systems? • No GC/run-time system 11 -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages 12 -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust 12 -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust 12 What about existing languages? -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust • Own system [1] influential, but: 12 [1] Haller and Odersky. Capabilities for uniqueness and borrowing. ECOOP ’10 What about existing languages? -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust • Own system [1] influential, but: 12 [2] Gordon et al. Uniqueness and reference immutability for safe parallelism. OOPSLA ’12 [1] Haller and Odersky. Capabilities for uniqueness and borrowing. ECOOP ’10 What about existing languages? Influenced M# language [2] of Microsoft’s Midori project -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust • Own system [1] influential, but: • Interaction with type inference unclear 12 [2] Gordon et al. Uniqueness and reference immutability for safe parallelism. OOPSLA ’12 [1] Haller and Odersky. Capabilities for uniqueness and borrowing. ECOOP ’10 What about existing languages? Influenced M# language [2] of Microsoft’s Midori project -
Isn’t it a Solved Problem? • Adoption surprisingly challenging •
Ownership not available in mainstream languages • Contender: Rust • Own system [1] influential, but: • Interaction with type inference unclear • Midori project not continued 12 [2] Gordon et al. Uniqueness and reference immutability for safe parallelism. OOPSLA ’12 [1] Haller and Odersky. Capabilities for uniqueness and borrowing. ECOOP ’10 What about existing languages? Influenced M# language [2] of Microsoft’s Midori project -
Affine References in Scala • LaCasa provides affine references by
combining two concepts: 13 -
Affine References in Scala • LaCasa provides affine references by
combining two concepts: • Access permissions 13 -
Affine References in Scala • LaCasa provides affine references by
combining two concepts: • Access permissions • Encapsulated boxes 13 -
Access Permissions • Access to transferable objects controlled by implicit
permissions CanAccess { type C } 14 -
Access Permissions • Access to transferable objects controlled by implicit
permissions • Type member C uniquely identifies box CanAccess { type C } 14 -
Access Permissions • Access to transferable objects controlled by implicit
permissions • Type member C uniquely identifies box CanAccess { type C } Box[T] { type C } 14 -
Matching Boxes and Permissions • Type members match permissions and
boxes • Via path-dependent types • Example: def method(box: Box[T]) (implicit p: CanAccess { type C = box.C }): Unit 15 -
Matching Boxes and Permissions • Type members match permissions and
boxes • Via path-dependent types • Example: def method(box: Box[T]) (implicit p: CanAccess { type C = box.C }): Unit 15 -
Creating Boxes and Permissions mkBox[Message] { packed => } class
Message { var arr: Array[Int] = _ } 16 -
Creating Boxes and Permissions mkBox[Message] { packed => } class
Message { var arr: Array[Int] = _ } 16 LaCasa library -
Creating Boxes and Permissions mkBox[Message] { packed => } class
Message { var arr: Array[Int] = _ } 16 -
Creating Boxes and Permissions mkBox[Message] { packed => } class
Message { var arr: Array[Int] = _ } implicit val access = packed.access val theBox = packed.box … 16 -
Creating Boxes and Permissions mkBox[Message] { packed => } class
Message { var arr: Array[Int] = _ } sealed trait Packed[+T] { val box: Box[T] val access: CanAccess { type C = box.C } } implicit val access = packed.access val theBox = packed.box … 16 -
Accessing Boxes • Boxes are encapsulated • Boxes must be
opened for access 17 -
Accessing Boxes • Boxes are encapsulated • Boxes must be
opened for access mkBox[Message] { packed => implicit val access = packed.access val theBox = packed.box theBox open { msg => msg.arr = Array(1, 2, 3, 4) } } 17 -
Accessing Boxes • Boxes are encapsulated • Boxes must be
opened for access mkBox[Message] { packed => implicit val access = packed.access val theBox = packed.box theBox open { msg => msg.arr = Array(1, 2, 3, 4) } } Requires implicit access permission 17 -
Consuming Permissions Example: transfering a box from one actor to
another consumes its access permission 18 -
Consuming Permissions Example: transfering a box from one actor to
another consumes its access permission mkBox[Message] { packed => implicit val access = packed.access val theBox = packed.box … someActor.send(theBox) // illegal to access `theBox` here! } 18 -
Consuming Permissions Example: transfering a box from one actor to
another consumes its access permission mkBox[Message] { packed => implicit val access = packed.access val theBox = packed.box … someActor.send(theBox) // illegal to access `theBox` here! } 18 How to enforce this? -
Permissions and Continuations • Make implicit permission unavailable in continuation
of permission-consuming call 19 -
Permissions and Continuations • Make implicit permission unavailable in continuation
of permission-consuming call • Scala’s type system is flow-insensitive => use continuation passing 19 -
Permissions and Continuations • Make implicit permission unavailable in continuation
of permission-consuming call • Scala’s type system is flow-insensitive => use continuation passing • Restrict continuation to exclude consumed permission 19 -
Continuation-Passing Style mkBox[Message] { packed => implicit val access =
packed.access val theBox = packed.box … someActor.send(theBox) { // make `access` unavailable … } } 20 -
Restricting Continuations • Continuation disallows capturing variables of the type
of access • Leverage spores 21 -
Restricting Continuations • Continuation disallows capturing variables of the type
of access • Leverage spores def send(msg: Box[T]) (cont: NullarySpore[Unit] { type Excluded = msg.C }) (implicit p: CanAccess { type C = msg.C }): Nothing 21 -
Restricting Continuations • Continuation disallows capturing variables of the type
of access • Leverage spores def send(msg: Box[T]) (cont: NullarySpore[Unit] { type Excluded = msg.C }) (implicit p: CanAccess { type C = msg.C }): Nothing 21 -
Intermezzo: Spores Spore [3] = closure • with explicit environment
• with function type refined by captured types val msg = "hello" val num = 5 spore { val s = msg val x = num (p: Int) => s”Captured vars and param: $s, $x, $p” } 22 [3] Miller, Haller and Odersky. Spores: a type-based foundation for closures in the age of concurrency and distribution. ECOOP ’14 -
Intermezzo: Spores Spore [3] = closure • with explicit environment
• with function type refined by captured types val msg = "hello" val num = 5 spore { val s = msg val x = num (p: Int) => s”Captured vars and param: $s, $x, $p” } Spore[Int, String] { type Captured = (String, Int) } 22 [3] Miller, Haller and Odersky. Spores: a type-based foundation for closures in the age of concurrency and distribution. ECOOP ’14 -
Spore Trait trait Spore[-T, +R] extends Function1[T, R] { type
Captured type Excluded } 23 -
Implicit Conversions • Goal: Implicitly convert function literals to spores
• Enables more lightweight syntax • Enables checking excluded types • Example: 24 type SafeSpore[A, B] = Spore[A, B] { type Excluded = SomeExcludedType } val s: SafeSpore[A, B] = (x: A) => { … } -
Recall Example mkBox[Message] { packed => implicit val access =
packed.access val theBox = packed.box … someActor.send(theBox) { // `access` unavailable … } } 25 -
Encapsulation Problem: not all types safe to transfer! 26 class
Message { var arr: Array[Int] = _ def leak(): Unit = { SomeObject.fld = arr } } object SomeObject { var fld: Array[Int] = _ } -
Encapsulation • Ensuring absence of data races (“concurrency safety”) requires
restricting types put into boxes • Requirements for “safe” classes:* • Methods only access parameters and this • Methods only instantiate “safe” classes • Types of fields are “safe” 27 * simplified -
Encapsulation • Ensuring absence of data races (“concurrency safety”) requires
restricting types put into boxes • Requirements for “safe” classes:* • Methods only access parameters and this • Methods only instantiate “safe” classes • Types of fields are “safe” 27 “Safe” = conforms to object capability model [3] * simplified -
Encapsulation • Ensuring absence of data races (“concurrency safety”) requires
restricting types put into boxes • Requirements for “safe” classes:* • Methods only access parameters and this • Methods only instantiate “safe” classes • Types of fields are “safe” 27 “Safe” = conforms to object capability model [3] * simplified [3] Mark S. Miller. Robust Composition: Towards a Unified Approach to Access Control and Concurrency Control. PhD dissertation, 2006 -
Object Capabilities in Scala • How common are object-capability safe
classes in Scala? • Results from empirical study: 28 -
LaCasa: Summary • Encapsulated boxes • Alias protection via object
capability model • Reusing a class requires only a single bit of information • Access permissions via implicits + path-dependent types • Can statically prevent data races • Sound integration with Scala’s local type inference • Not shown: unique fields 29 -
More Contributions • Formalization: object-oriented core languages • CLC1: type-based
notion of object capabilities 30 -
More Contributions • Formalization: object-oriented core languages • CLC1: type-based
notion of object capabilities • CLC2: uniqueness via flow-insensitive permissions 30 -
More Contributions • Formalization: object-oriented core languages • CLC1: type-based
notion of object capabilities • CLC2: uniqueness via flow-insensitive permissions • CLC3: concurrent extension 30 -
More Contributions • Formalization: object-oriented core languages • CLC1: type-based
notion of object capabilities • CLC2: uniqueness via flow-insensitive permissions • CLC3: concurrent extension • Soundness proof, establishing heap separation and uniqueness invariants 30 -
More Contributions • Formalization: object-oriented core languages • CLC1: type-based
notion of object capabilities • CLC2: uniqueness via flow-insensitive permissions • CLC3: concurrent extension • Soundness proof, establishing heap separation and uniqueness invariants • Isolation theorem for processes with shared heap 30 -
Details • Paper to appear at OOPSLA ’16 • Technical
report: [arXiv:1607.05609] • Mechanization of formal model in Coq 31 -
Status and Plans • Prototype implementation:1 • Compiler plug-in for
Scala 2.11 • Integration with actors (straw man) 32 1 https://github.com/phaller/lacasa -
Status and Plans • Prototype implementation:1 • Compiler plug-in for
Scala 2.11 • Integration with actors (straw man) • Ongoing and future work: • Adapters for Akka and other libraries • Empirical study on the effort to adapt existing code • Port to Dotty Scala Compiler 32 1 https://github.com/phaller/lacasa -
Status and Plans • Prototype implementation:1 • Compiler plug-in for
Scala 2.11 • Integration with actors (straw man) • Ongoing and future work: • Adapters for Akka and other libraries • Empirical study on the effort to adapt existing code • Port to Dotty Scala Compiler 32 Thank you! 1 https://github.com/phaller/lacasa