hacking the rust object system at mozillalkuper/talks/rust... · why rust? what do we want in a...

64
Hacking the Rust object system at Mozilla Lindsey Kuper Grinnell College April 5, 2012 1

Upload: others

Post on 24-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Hacking the Rust object system at MozillaLindsey Kuper

Grinnell CollegeApril 5, 2012

1

Page 2: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

Page 3: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

■ Graduated from Grinnell (CS and music) in 2004

Page 4: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

■ Graduated from Grinnell (CS and music) in 2004

■ Web development at a (failed) startup, 2004–2006

Page 5: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

■ Graduated from Grinnell (CS and music) in 2004

■ Web development at a (failed) startup, 2004–2006

■ Perl plumbing at a publishing company, 2006–2008

Page 6: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

■ Graduated from Grinnell (CS and music) in 2004

■ Web development at a (failed) startup, 2004–2006

■ Perl plumbing at a publishing company, 2006–2008■ but in 2007, I moved in with a

couple of Haskell hackers...

Page 7: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

2

Me and how I got here

■ Graduated from Grinnell (CS and music) in 2004

■ Web development at a (failed) startup, 2004–2006

■ Perl plumbing at a publishing company, 2006–2008■ but in 2007, I moved in with a

couple of Haskell hackers...

■ Ph.D. student at Indiana studying PL since fall 2008

Page 8: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 9: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 10: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 11: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

■ Is one program indistinguishable from another?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 12: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

■ Is one program indistinguishable from another?

■ Is this optimization a safe program transformation?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 13: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

■ Is one program indistinguishable from another?

■ Is this optimization a safe program transformation?

■ Can programs written in this language crash?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 14: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

■ Is one program indistinguishable from another?

■ Is this optimization a safe program transformation?

■ Can programs written in this language crash?

■ Is this compiler translation correct?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 15: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

My field: programming language semantics

■ Questions we might want to answer:■ Is this program correct?

■ Will this program encounter a run-time type error?

■ Is one program indistinguishable from another?

■ Is this optimization a safe program transformation?

■ Can programs written in this language crash?

■ Is this compiler translation correct?

■ Can source language A be translated into target language B?

3

Source: Andrew Myers’ CS 611 course at Cornell

Page 16: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we want in a programming language?

4

Source: Michael Sullivan, “Closures for Rust”

Page 17: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we want in a programming language?■ Fast: generates efficient machine code

4

Source: Michael Sullivan, “Closures for Rust”

Page 18: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we want in a programming language?■ Fast: generates efficient machine code

■ Safe: type system provides guarantees that prevent certain bugs

4

Source: Michael Sullivan, “Closures for Rust”

Page 19: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we want in a programming language?■ Fast: generates efficient machine code

■ Safe: type system provides guarantees that prevent certain bugs

■ Concurrent: easy to build concurrent programs and to take advantage of parallelism

4

Source: Michael Sullivan, “Closures for Rust”

Page 20: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we want in a programming language?■ Fast: generates efficient machine code

■ Safe: type system provides guarantees that prevent certain bugs

■ Concurrent: easy to build concurrent programs and to take advantage of parallelism

■ “Systemsy”: fine-grained control, predictable performance characteristics

4

Source: Michael Sullivan, “Closures for Rust”

Page 21: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?

5

Source: Michael Sullivan, “Closures for Rust”

Page 22: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?■ Firefox is in C++, which is Fast and

Systemsy

5

Source: Michael Sullivan, “Closures for Rust”

Page 23: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?■ Firefox is in C++, which is Fast and

Systemsy

■ ML is (sometimes) Fast and (very) Safe

5

Source: Michael Sullivan, “Closures for Rust”

Page 24: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?■ Firefox is in C++, which is Fast and

Systemsy

■ ML is (sometimes) Fast and (very) Safe

■ Erlang is Safe and Concurrent

5

Source: Michael Sullivan, “Closures for Rust”

Page 25: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?■ Firefox is in C++, which is Fast and

Systemsy

■ ML is (sometimes) Fast and (very) Safe

■ Erlang is Safe and Concurrent

■ Haskell is (sometimes) Fast, (very) Safe, and Concurrent

5

Source: Michael Sullivan, “Closures for Rust”

Page 26: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

■ What do we have now?■ Firefox is in C++, which is Fast and

Systemsy

■ ML is (sometimes) Fast and (very) Safe

■ Erlang is Safe and Concurrent

■ Haskell is (sometimes) Fast, (very) Safe, and Concurrent

■ Java and C# are Fast and Safe

5

Source: Michael Sullivan, “Closures for Rust”

Page 27: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Why Rust?

6

A systems languagepursuing the trifecta:fast, concurrent, safe

Page 28: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

7

Page 29: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

7

Page 30: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

■ I was intrigued by the idea of a classless object model and flexible prototype-based objects

7

Page 31: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”

7

Page 32: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”

■ When I started: no object extension, method overriding, or self-dispatch

7

Page 33: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”

■ When I started: no object extension, method overriding, or self-dispatch

■ During my internship, I implemented those things

7

Page 34: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

You worked on the what system?!

■ I didn’t arrive with the intention of working on the object system, but...

■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”

■ When I started: no object extension, method overriding, or self-dispatch

■ During my internship, I implemented those things■ and learned that they interact with each other in

interesting ways

7

Page 35: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch

8

Page 36: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch

8

Page 37: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension

9

Page 38: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension

9

Page 39: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

A brainteaser...

10

After my first implementation attempt,

this assertion failed.Why?

Page 40: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

A hint...

11

Page 41: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

A brainteaser...

12

After my first implementation attempt,

this assertion failed.Why?

Page 42: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

A brainteaser...

12

After my first implementation attempt,

this assertion failed.Why?

longcat’s vtablelongcat’s vtablelongcat’s vtable

0 ack forward to shortcat.ack()1 lol ret “lol”

2 meow forward to shortcat.meow()3 nyan ret “nyan”

4 zzz forward to shortcat.zzz()

Page 43: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

A brainteaser...

12

After my first implementation attempt,

this assertion failed.Why?

longcat’s vtablelongcat’s vtablelongcat’s vtable

0 ack forward to shortcat.ack()1 lol ret “lol”

2 meow forward to shortcat.meow()3 nyan ret “nyan”

4 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

Page 44: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

How to fix it

13

longcat’s vtablelongcat’s vtablelongcat’s vtable

0 ack forward to shortcat.ack()1 lol ret “lol”

2 meow forward to shortcat.meow()3 nyan ret “nyan”

4 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

Page 45: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

How to fix it

13

longcat’s vtablelongcat’s vtablelongcat’s vtable

0 ack forward to shortcat.ack()1 lol ret “lol”

2 meow forward to shortcat.meow()3 nyan ret “nyan”

4 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable

0 ack backward to longcat.ack()

1 meow backward to longcat.meow()

2 zzz backward to longcat.zzz()

Page 46: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

How to fix it

13

longcat’s vtablelongcat’s vtablelongcat’s vtable

0 ack forward to shortcat.ack()1 lol ret “lol”

2 meow forward to shortcat.meow()3 nyan ret “nyan”

4 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable

0 ack backward to longcat.ack()

1 meow backward to longcat.meow()

2 zzz backward to longcat.zzz()

self

Page 47: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

Page 48: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

Page 49: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

Page 50: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

Caveat: Some disagreement on whether it should work this way (see: Aldrich, “Selective Open Recursion”)

Page 51: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

Page 52: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

longercat’s vtablelongercat’s vtablelongercat’s vtable

0 ack forward to shortcat.ack()1 meow ret “zzz”

2 zzz forward to shortcat.zzz()

Page 53: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

longercat’s vtablelongercat’s vtablelongercat’s vtable

0 ack forward to shortcat.ack()1 meow ret “zzz”

2 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

Page 54: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

longercat’s vtablelongercat’s vtablelongercat’s vtable

0 ack forward to shortcat.ack()1 meow ret “zzz”

2 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable

0 ack backward to longercat.ack()

1 meow backward to longercat.meow()

2 zzz backward to longercat.zzz()

Page 55: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

14

longercat’s vtablelongercat’s vtablelongercat’s vtable

0 ack forward to shortcat.ack()1 meow ret “zzz”

2 zzz forward to shortcat.zzz()

shortcat’s vtableshortcat’s vtableshortcat’s vtable

0 ack ret “ack”

1 meow ret “meow”

2 zzz ret self.meow()

shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable

0 ack backward to longercat.ack()

1 meow backward to longercat.meow()

2 zzz backward to longercat.zzz()

self

Page 56: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth

Page 57: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth

Page 58: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth

Page 59: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth

Page 60: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is

Page 61: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is

■ Solution: create a stack of “fake selves” threaded through the run-time stack

Page 62: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Self-dispatch + object extension + overriding

1511

to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is

■ Solution: create a stack of “fake selves” threaded through the run-time stack

■ Every forwarding function allocates space in its frame for a “fake self” comprising a backwarding vtable and an inner object body

Page 63: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

Go try it out!

16

rust-lang.org

Page 64: Hacking the Rust object system at Mozillalkuper/talks/rust... · Why Rust? What do we want in a programming language? Fast: generates efficient machine code Safe: type system provides

17Photo by jamesrbowe on Flickr. Thanks!

Questions?

Thanks to:Graydon Hoare and everyone on the Rust teamDave Herman and everyone at Mozilla Research