Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust: low-level programming without the segfaults

Rust: low-level programming without the segfaults

Presented at OSCON 2013.

dherman

July 25, 2013
Tweet

More Decks by dherman

Other Decks in Programming

Transcript

  1. View Slide

  2. View Slide

  3. View Slide

  4. The price of performance shouldn’t have to be
    crashes and exploits.

    View Slide

  5. • Safe unions and pointers
    • Explicit memory layout & stack allocation
    • Optional garbage collection
    • Mutability control & race-free concurrency
    Rust: perf + safety

    View Slide

  6. ALGEBRAIC
    DATA TYPES

    View Slide

  7. enum Color {
    Red,
    Green,
    Blue
    }

    View Slide

  8. enum Tree {
    Empty,
    Leaf(int),
    Node(~Tree, ~Tree)
    }

    View Slide

  9. tag tag
    int
    tag
    ~Tree
    ~Tree
    Empty Leaf(int)
    Node(~Tree,
    ~Tree)

    View Slide

  10. struct Tree {
    enum TreeTag tag;
    union {
    int leaf;
    Pair node;
    } data;
    };
    enum TreeTag { Empty, Leaf, Node };
    struct Pair { Tree *left, *right; };

    View Slide

  11. PATTERN
    MATCHING

    View Slide

  12. match tree {
    Empty => 0,
    Leaf(_) => 1,
    Node(~ref left, ~ref right) =>
    f(left) + f(right)
    }

    View Slide

  13. enum Option {
    None,
    Some(T)
    }

    View Slide

  14. View Slide

  15. fn double(p: ~int) -> int {
    *p * 2
    }

    View Slide

  16. fn double(p: Option<~int>) -> int {
    match p {
    None => 0,
    Some(~n) => n * 2
    }
    }

    View Slide

  17. OWNERSHIP

    View Slide

  18. void *realloc(void *, ...);
    void free(void *);
    Owned pointers

    View Slide

  19. void *realloc(void *, ...);
    void free(void *);
    template
    class Hashtable {
    vector> buckets;
    ...
    };
    Owned pointers

    View Slide

  20. memcpy(void *dest,
    const void *src,
    uintptr_t count);
    Temporary pointers

    View Slide

  21. memcpy(void *dest,
    const void *src,
    uintptr_t count);
    template
    class Hashtable {
    ...
    V& find(K& key);
    };
    Temporary pointers

    View Slide

  22. the right to free memory
    What is ownership?

    View Slide

  23. struct Hashtable {
    buckets: ~[Option>]
    }
    struct Bucket {
    key: K,
    value: V
    }

    View Slide

  24. {
    let b = ~[];
    let table = Hashtable {
    buckets: b
    };
    ...
    }

    View Slide

  25. {
    let b = ~[];
    let table = Hashtable {
    buckets: b
    };
    ...
    }
    heap
    allocate

    View Slide

  26. {
    let b = ~[];
    let table = Hashtable {
    buckets: b
    };
    ...
    }
    heap
    allocate
    stack
    allocate

    View Slide

  27. {
    let b = ~[];
    let table = Hashtable {
    buckets: b
    };
    ...
    }
    heap
    allocate
    stack
    allocate
    move

    View Slide

  28. fn move_from() {
    let x: ~int = ~22;
    move_to(x);
    }
    fn move_to(p: ~int) {
    printfln!(“%d”, *p);
    }

    View Slide

  29. fn move_from() {
    let x: ~int = ~22;
    move_to(x);
    }
    fn move_to(p: ~int) {
    printfln!(“%d”, *p);
    }
    heap
    allocate

    View Slide

  30. fn move_from() {
    let x: ~int = ~22;
    move_to(x);
    }
    fn move_to(p: ~int) {
    printfln!(“%d”, *p);
    }
    heap
    allocate
    move

    View Slide

  31. fn move_from() {
    let x: ~int = ~22;
    move_to(x);
    }
    fn move_to(p: ~int) {
    printfln!(“%d”, *p);
    }
    heap
    allocate
    free p
    move

    View Slide

  32. fn move_from() {
    let x: ~int = ~22;
    move_to(x);
    }
    fn move_to(p: ~int) {
    printfln!(“%d”, *p);
    }
    heap
    allocate
    free p
    move
    printfln!(“%d”, *x); // error

    View Slide

  33. BORROWING

    View Slide

  34. fn f() {
    let x: ~int = ~22;
    borrow(x);
    printfln!(“%d”, *x);
    }
    fn borrow(x: &int) {
    printfln!(“borrowing: %d”, *x);
    }

    View Slide

  35. fn f() {
    let x: int = 22;
    borrow(&x);
    printfln!(“%d”, *x);
    }
    fn borrow(x: &int) {
    printfln!(“borrowing: %d”, *x);
    }

    View Slide

  36. let w = ~22;
    {
    let x = &w;
    let y = &*x;
    }
    ‘a
    ‘b
    ‘c
    ‘d
    Lifetimes

    View Slide

  37. fn find<‘a,K,V>(tbl: &’a Hashtable,
    key: &K)
    -> Option<&’a V>
    {
    ...
    }

    View Slide

  38. fn find<‘a,K,V>(tbl: &’a Hashtable,
    key: &K)
    -> Option<&’a V>
    {
    ...
    }
    pointer with
    lifetime ‘a to table

    View Slide

  39. fn find<‘a,K,V>(tbl: &’a Hashtable,
    key: &K)
    -> Option<&’a V>
    {
    ...
    }
    pointer with
    lifetime ‘a to table
    pointer with lifetime ‘a
    to value

    View Slide

  40. template
    class Hashtable
    V& Hashtable::find(K& key);
    unsafe!

    View Slide

  41. CONCURRENCY

    View Slide

  42. Heap

    View Slide

  43. Heap

    View Slide

  44. let (port, chan) = stream();
    do spawn {
    let p = ~Point { x: 1.0, y: 2.0 };
    chan.send(p);
    }
    let p = port.recv();
    printfln!(p.to_str());

    View Slide

  45. • Pluggable I/O
    • Optional scheduler
    • Data race-free shared data structures
    • Potential for race-free fork-join parallelism
    • Working on SIMD and GPU libs
    There’s more

    View Slide

  46. UNSAFE

    View Slide

  47. unsafe {
    transmute::<&str,&[u8]>(“L”)
    == &[76, 0]
    }
    unsafe fn f(...) { ... }

    View Slide

  48. unsafe {
    transmute::<&str,&[u8]>(“L”)
    == &[76, 0]
    }
    unsafe fn f(...) { ... }

    View Slide

  49. 1. Don’t use unsafe code.
    2. If you absolutely must break Rule 1,
    • get out of unsafe code ASAP, and
    • always provide a safe interface at the
    boundaries.
    The rules

    View Slide

  50. Low-level programming… without the segfaults.

    View Slide

  51. Thank you.

    View Slide

  52. Image credits
    Sean Martell
    http://blog.seanmartell.com
    Ben Clinch
    http://www.flickr.com/photos/benclinch/3048906022/
    juicyrai
    http://www.flickr.com/photos/wink/180979062/
    Rama, Wikimedia Commons
    http://en.wikipedia.org/wiki/File:Sir_Tony_Hoare_IMG_5125.jpg
    Jared Zimmerman
    http://www.flickr.com/photos/spoinknet/7932382540/
    CCAC North Library
    http://www.flickr.com/photos/ccacnorthlib/3553821699/
    Mike Hayes
    http://www.flickr.com/photos/rotties/449650275/

    View Slide