4 ms·
"However BTreeMap is implemented using a modest spoonful of Unsafe Rust (most collections are)." Unsafe pure Rust code reflects a lack of expressive power. Ho
by Animats 10y ago
"However BTreeMap is implemented using a modest spoonful of Unsafe Rust (most collections are)."
Unsafe pure Rust code reflects a lack of expressive power. How raw memory becomes a typed object is a touchy area for arrays. To do collections safely, you have to be able to talk about an array which is partially valid. This is quite possible, but you need formal verification like constructs to do it. You need to be able to express that a slice of an array is valid, even though the whole array isn't.
If you can talk about a slice being valid, and an element being valid, you're almost there. It's a theorem that if an element is adjacent to the end of a slice, the element has some property, and all elements in the slice have a property, then a slice containing the original slice and the element has that property. Using that, you can prove by induction that as you grow a collection, the elements in use remain valid. If the access primitives don't let you access outside the valid range, they're safe.
With some extra predicates and a modest theorem prover, much unsafe code in the collection area could probably be proven safe.
Rather than describing unsafe code as a "dark art", it would be more useful to try to formalize safety in this way. The theory is well understood, and there are many proof of correctness systems around for other languages. It might require a lot of annotation in unsafe code, to help the prover along, but that's not a bad thing.
- ineol1 10y agoDerek Dreyer and collaborators are working on specifying what needs to be proven about unsafe code, see http://plv.mpi-sws.org/rustbelt/ http://plv.mpi-sws.org/rustbelt/
- Animats 10y agoThat's good. They may have trouble recruiting. They're looking for people who could make upwards of $150K in Silicon Valley and want to pay them at postdoc rates. Glassdoor says €41,595. Their own material has even lower rates.[1] [1] https://www.mpg.de/8746248/Foerderrichtlinien_Postdoc_en.pdf https://www.mpg.de/8746248/Foerderrichtlinien_Postdoc_en.pdf
- dikaiosune 10y agoThat's just the status quo for postdocs and PhD students, AFAICT. And yet universities everywhere still find people to take them up on the deal.
- nickpsecurity 10y agoI agree. I regularly post the amazing work such people produce. A good example is Cyclone, which contributed to Rust's success.
- semi-extrinsic 10y agoWell, the academic and implementation-wise freedoms are huge compared to any SV startup. And you don't need to worry much about financials, nor marketing, nor a million other things that many hackers dislike.
- programLyrique 10y agoAre there lots of opportunities to work on static analysis and theorem proving outside academia, especially in Silicon Valley?
- amenghra 10y agoStatic analysis: for sure. Companies with large codebases are interested to understand how their code works, build refactoring tools, find bugs, etc. They have dedicated teams doing this. For example https://github.com/facebook/pfff https://github.com/facebook/pfff. Formal proofs: there are specific applications (see Amazon's use of TLA+). I personally feel nearly every company has a small piece of code that you can justify spending a few weeks/months modeling to ensure its correctness. Also, engineers working on medical devices, avionics, robotics, finance, etc. probably get to prove the correctness of their code.
- adrianN 10y ago> Also, engineers working on medical devices, avionics, robotics, finance, etc. probably get to prove the correctness of their code. Some might, but the relevant ISO standards are ok with testing if you reach sufficient coverage, so generally testing is all that's happening.
- hinkley 10y agoOuch. If data structures require unsafe operations, doesn't this make Rust's safety a 90/10 solution, and leave a small but credible threat of what are effectively buffer overflow attacks? Look at all of the programming languages that have had to reimplement their hash tables because someone figured out that you can DDOS a web server by sending requests with just the right query parameters. If there's a bug in hash resizing and it's unsafe code, what could someone make of that?
- pcwalton 10y ago> If data structures require unsafe operations, doesn't this make Rust's safety a 90/10 solution, and leave a small but credible threat of what are effectively buffer overflow attacks? No worse than JavaScript, Perl, PHP, Python, Ruby, Go, ..., all of which implement their hash tables in C. How many production server side apps do you know of that have been compromised due to memory safety problems in the core hash table data structure?
- geodel 10y agoGo's implementation is in Go + may be some assembly.
- yberreby 10y agoThat doesn't make it immune to memory unsafety, because the initial implementation had to be written in something other than Go, namely C. And the current compiler was compiled by an older compiler, which was itself compiled by an older one, ..., which was itself compiled by the initial Go compiler, written in C.
- b2384912 10y agoAre they seriously not using an interpreter to compile the current version with itself?
- 10y ago
- Gankro 10y agoAgreed that there are more powerful systems to verify this code, but these quickly face diminishing returns relative to their costs. On the other hand, collections are embarassingly testable. It's never been clear to me that formal methods gain much over thorough fuzzing/unit testing in this context. (rust's testing falls a fair bit short of thorough unfortunately and there have been a handful of mem safety errors in their impls. although I'm not aware of this ever leading to an exploit in an application. mostly caught quite early with the release trains as a buffer)
- vvanders 10y agoYeah, was going to say something similar. You shouldn't be reimplementing linked list unless you have a very good reason and if so you should be taking a careful look at what you do. Rust does a great job of error checking in the 99% case and the 1% of unsafe should be from well vetted libs. No different than trusting the kernel to do the right thing.
- jerf 10y agohttp://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it/ http://envisage-project.eu/proving-android-java-and-python-s... https://research.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html https://research.googleblog.com/2006/06/extra-extra-read-all... Yes, you can asymptotically approach correct with testing, but it turns out that surprises can lurk for quite a long time if that's your approach. Of course, that's how most collections code works and what most of the world is built on, so yes, it is generally "good enough", but it is still worth pointing out that it is not necessarily correct, even after being out in the field for decades.
- foota 10y agoI don't know much about what I'm about to say, but I've always thought it would be cool to embed some proof language like coq inside the rust compiler so that devs can write proofs that their unsafe code obeys some properties that the rust compiler can use.
- __s 10y agoWouldn't it be better to skip writing the unsafe code, & have the proof generate proven-safe unsafe rust code?
- foota 10y agoAw, see, this is me showing what I don't know :) I was thinking it would be something similar to this: https://en.wikipedia.org/wiki/Proof-carrying_code https://en.wikipedia.org/wiki/Proof-carrying_code except at compile time.
- Manishearth 10y agoThere is ongoing work in formal verification of both Rust and the unsafe code in the stdlib. However, the average writer of unsafe code will not use formal verification. For them, the nomicon is a very useful resource. And describing it as a dark art is the best way to do it, because unsafe code can be hard and fraught with pitfalls.