oxide the essence of rust
play

OXIDE: THE ESSENCE OF RUST Aaron J. Weiss Northeastern University - PowerPoint PPT Presentation

OXIDE: THE ESSENCE OF RUST Aaron J. Weiss Northeastern University Rusts rich type system and ownership model guarantee memory-safety and thread-safety enabling you to eliminate many classes of bugs at compile-time. the


  1. OXIDE: THE ESSENCE OF RUST Aaron J. Weiss Northeastern University

  2. “ Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — enabling you to eliminate many classes of bugs at compile-time. – the official Rust website

  3. BORROW CHECKING

  4. WHAT IS A BORROW CHECKER? struct State { ��../ } fn main() { let mut state = State { ��../ }; let init = read_state(&state); update_state(&mut state); let f i n = read_state(&state); consume_state(state); �/0 cannot use `state` anymore }

  5. WHAT IS A BORROW CHECKER? struct State { ��../ } fn main() { let mut state = State { ��../ }; let init = read_state(&state); &state update_state(&mut state); borrow let f i n = read_state(&state); consume_state(state); �/0 cannot use `state` anymore }

  6. WHAT IS A BORROW CHECKER? struct State { ��../ } fn main() { let mut state = State { ��../ }; let init = read_state(&state); update_state(&mut state); &mut state borrow mutable borrow let f i n = read_state(&state); consume_state(state); �/0 cannot use `state` anymore }

  7. WHAT IS A BORROW CHECKER? struct State { ��../ } fn main() { let mut state = State { ��../ }; let init = read_state(&state); update_state(&mut state); borrow mutable borrow let f i n = read_state(&state); consume_state(state); state move �/0 cannot use `state` anymore }

  8. WHAT IS A BORROW CHECKER? fn update_state(state: &mut State) { if should_reset(state) { * state = State { ��../ }; } else { (*state).count += 1 } }

  9. A BORROW CHECKER IS... 🧿 Ownership & Flexible Alias Protection 📗 ✒ ⊕

  10. OXIDE IS OUR EFFORT TO FORMALIZE BORROW CHECKING

  11. OUR EXAMPLE PROGRAM IN OXIDE struct State { ��../ } fn main() { letprov<'a, 'b, 'c> { let state = State { ��../ }; let init = read_state �:; <'a>(&'a shrd state); update_state �:; <'b>(&'b uniq state); let f i n = read_state �:; <'c>(&'c shrd state); consume_state(state); } }

  12. OUR EXAMPLE PROGRAM IN OXIDE struct State { ��../ } fn main() { letprov<'a, 'b, 'c> { let state = State { ��../ }; let init = read_state �:; <'a>(&'a shrd state); shrd update_state �:; <'b>(&'b uniq state); uniq let f i n = read_state �:; <'c>(&'c shrd state); shrd consume_state(state); } }

  13. OUR EXAMPLE PROGRAM IN OXIDE struct State { ��../ } fn main() { letprov<'a, 'b, 'c> { letprov<'a, 'b, 'c> { let state = State { ��../ }; let init = read_state �:; <'a>(&'a shrd state); shrd update_state �:; <'b>(&'b uniq state); uniq let f i n = read_state �:; <'c>(&'c shrd state); shrd consume_state(state); } } }

  14. OUR EXAMPLE PROGRAM IN OXIDE struct State { ��../ } fn main() { letprov<'a, 'b, 'c> { letprov<'a, 'b, 'c> { let state = State { ��../ }; let init = read_state �:; <'a>(&'a shrd state); 'a shrd update_state �:; <'b>(&'b uniq state); 'b uniq let f i n = read_state �:; <'c>(&'c shrd state); 'c shrd consume_state(state); } } }

  15. OUR EXAMPLE PROGRAM IN OXIDE struct State { ��../ } fn main() { letprov<'a, 'b, 'c> { letprov<'a, 'b, 'c> { let state = State { ��../ }; let init = read_state �:; <'a>(&'a shrd state); 'a 'a shrd update_state �:; <'b>(&'b uniq state); 'b 'b uniq let f i n = read_state �:; <'c>(&'c shrd state); 'c 'c shrd consume_state(state); } } }

  16. PROVENANCES AS A ‘POINTS-TO’ ANALYSIS A reference with type… &'a shrd state &'b uniq state

  17. PROVENANCES AS A ‘POINTS-TO’ ANALYSIS A reference with type… &'a shrd state &'b uniq state points to 'a state

  18. PROVENANCES AS A ‘POINTS-TO’ ANALYSIS A reference with type… &'a shrd state &'b uniq state points to points to 'a 'b state state

  19. PROVENANCES AS A ‘POINTS-TO’ ANALYSIS A reference with type… &'a shrd state &'b uniq state points to points to 'a 'b state state Γ ::= ∙ | Γ ♮ ℱ x : T 'a ��|-? { p 1 … p n } ℱ ::= ∙ | ℱ , | ℱ ,

  20. TYPECHECKING A MOVE EXPRESSION struct State { ../ } fn main() { letprov<'a, 'b, 'c> { let state = State { ../ }; let init = read_state :; <'a>(&'a shrd state); update_state :; <'b>(&'b uniq state); let f i n = read_state :; <'c>(&'c shrd state); consume_state(state); } }

  21. TYPECHECKING A MOVE EXPRESSION consume_state(state);

  22. TYPECHECKING A MOVE EXPRESSION consume_state(state); state is not aliased Γ ( state ) = State state State Δ ; Γ ⊢ :

  23. TYPECHECKING A BORROW EXPRESSION struct State { ../ } fn main() { letprov<'a, 'b, 'c> { let state = State { ../ }; let init = read_state :; <'a>(&'a shrd state); update_state :; <'b>(&'b uniq state); let f i n = read_state :; <'c>(&'c shrd state); consume_state(state); } }

  24. TYPECHECKING A BORROW EXPRESSION update_state �:; <'b>(&'b uniq state);

  25. TYPECHECKING A BORROW EXPRESSION update_state �:; <'b>(&'b uniq state); state is not aliased Γ ( state ) = State Δ ; Γ ⊢ &'a uniq state : &'a uniq State

  26. TYPECHECKING A BORROW EXPRESSION state is not uniquely aliased Γ ( state ) = State Δ ; Γ ⊢ &'a shrd state : &'a shrd State update_state �:; <'b>(&'b uniq state); state is not aliased Γ ( state ) = State Δ ; Γ ⊢ &'a uniq state : &'a uniq State

  27. OWNERSHIP SAFETY: “IS NOT ALIASED” Δ ; Γ ⊢ x ⇒ { … } Δ ; Γ ⊢ x ⇒ { … } uniq shrd

  28. OWNERSHIP SAFETY: “IS NOT ALIASED” Δ ; Γ ⊢ x ⇒ { … } Δ ; Γ ⊢ x ⇒ { … } uniq shrd ω ::= uniq | shrd π ::= x | π . n | π . f Δ ; Γ ⊢ ω π ⇒ { p 1 … p n }

  29. OWNERSHIP SAFETY: “IS NOT ALIASED” Δ ; Γ ⊢ x ⇒ { … } Δ ; Γ ⊢ x ⇒ { … } uniq shrd ω ::= uniq | shrd π ::= x | π . n | π . f Δ ; Γ ⊢ ω π ⇒ { p 1 … p n } places

  30. OWNERSHIP SAFETY: “IS NOT ALIASED” Δ ; Γ ⊢ x ⇒ { … } Δ ; Γ ⊢ x ⇒ { … } uniq shrd ω ::= uniq | shrd π ::= x | π . n | π . f Δ ; Γ ⊢ ω π ⇒ { p 1 … p n } places place expressions

  31. THE STORY SO FAR Γ ( state ) = State state is not aliased state State Δ ; Γ ⊢ : Γ ( state ) = State state is not aliased Δ ; Γ ⊢ &'a uniq state : &'a uniq State Γ ( state ) = State state is not uniquely aliased Δ ; Γ ⊢ &'a shrd state : &'a shrd State

  32. THE STORY SO FAR Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } state State Δ ; Γ ⊢ : Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } Δ ; Γ ⊢ &'a uniq state : &'a uniq State Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ shrd } Δ ; Γ ⊢ &'a shrd state : &'a shrd State

  33. A BIT OF A SNAG Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } state State Δ ; Γ ⊢ : Can we use state again?

  34. A BIT OF A SNAG Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } state State Δ ; Γ ⊢ : Can we use state again?

  35. A CONVENTIONAL APPROACH... ? ⊢ Γ ↝ Γ 1 ⊞ Γ 2 Convention:

  36. A CONVENTIONAL APPROACH... ? ⊢ Γ ↝ Γ 1 ⊞ Γ 2 Convention: Rust: struct Point(i32, i32) let pt = Point(5, 6); ��../ add_one(pt.0); ��../ add_one(pt.1);

  37. AN (UN)CONVENTIONAL APPROACH Environment passing! Γ ( state ) = State state state Δ ; Γ ⊢ uniq ⇒ { } state State Δ ; Γ ⊢ :

  38. AN (UN)CONVENTIONAL APPROACH Environment passing! Γ ( state ) = State state state Δ ; Γ ⊢ uniq ⇒ { } state State Δ ; Γ ⊢ :

  39. AN (UN)CONVENTIONAL APPROACH Environment passing! Γ ( state ) = State state state Δ ; Γ ⊢ uniq ⇒ { } † state State ⇒ Γ [state ��|-? State ] state State Δ ; Γ ⊢ Δ ; Γ ⊢ : :

  40. EXAMPLE: PROJECTING OUT OF A TUPLE

  41. EXAMPLE: PROJECTING OUT OF A TUPLE •; x : (i32, i32)

  42. EXAMPLE: PROJECTING OUT OF A TUPLE •; x : (i32, i32) ⊢

  43. EXAMPLE: PROJECTING OUT OF A TUPLE •; x : (i32, i32) ⊢ x.0 : i32

  44. EXAMPLE: PROJECTING OUT OF A TUPLE •; x : (i32, i32) ⊢ x.0 : i32 ⇒

  45. EXAMPLE: PROJECTING OUT OF A TUPLE † •; x : (i32, i32) ⊢ x.0 : i32 ⇒ x : (i32, i32)

  46. GOOD FOR PROVENANCE TRACKING, TOO! Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } Δ ; Γ ⊢ &'a uniq state : &'a uniq State Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ shrd } Δ ; Γ ⊢ &'a shrd state : &'a shrd State

  47. GOOD FOR PROVENANCE TRACKING, TOO! Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ uniq } Δ ; Γ ⊢ &'a uniq state : &'a uniq State Δ ; Γ ⊢ &'a uniq state : &'a uniq State ⇒ Γ ['a ��|-? {state}] Γ ( state ) = State state state ⇒ { Δ ; Γ ⊢ shrd } Δ ; Γ ⊢ &'a shrd state : &'a shrd State

Recommend


More recommend