12 ms·
Does the "Rust standard of quality" for these crucial crates include "no unsafe code"? "Vec" currently needs unsafe code, because Rust doesn't have the express
by Animats 10y ago
Does the "Rust standard of quality" for these crucial crates include "no unsafe code"?
"Vec" currently needs unsafe code, because Rust doesn't have the expressive power to talk about a partially initialized array. Everything else with unsafe code is an optimization. Often a premature one. Maps should be built on "Vec", for example.
- eridius 10y agoWhy do you say premature? Maps, for example, being such a crucial and widely-used data type, should be optimized as much as possible. There's nothing premature about using `unsafe` to build a highly-efficient Map.
- Animats 10y agoLet's see the benchmarks justifying the use of "unsafe" for maps. Maybe there's a better way to do it without much of a performance penalty. It's a good way to find out what optimizations the compiler is missing. It may even turn out that unsafe code written early no longer is a performance win, since the Rust compiler is getting better at optimizing out redundant subscript checks. When you start looking through Rust libraries, "unsafe" turns up way too often.
- leshow 10y agoUnsafe rarely turns up in any of the libraries I've used. Where are you getting this from?
- Manishearth 10y ago(The hashmap that is used in the winning rust benchmarksgame entry is 100% safe, fwiw) > When you start looking through Rust libraries, "unsafe" turns up way too often. You keep making this claim without substantiation. Yes, there is some level of unnecessary unsafe, but certainly not "way too often". I recall going through all the crates in my .cargo and finding very little unnecessary unsafe, and showing you the audit: https://news.ycombinator.com/item?id=13280347 https://news.ycombinator.com/item?id=13280347 Please stop throwing around this claim without substantiation.
- yazaddaruvala 10y agoDo you think there is some room for education here? Would it be useful for Crates.io to show some "coverage" metric? eg. "unsafe lines = 5%" or "unsafe instructions = 0.01%" I think that would be a pretty easy way to put this convo to bed yea? Have there been talks about this before? Is it worth me opening an issue/RFC on Cargo?
- Manishearth 10y agoI don't think it's worth making changes like that to satisfy random misconceptions unless they are widespread. There are good reasons for having an unsafe-code flag on a crate. This is not one of those. This has been discussed in the past and the main concern IIRC is that this may make people afraid to use crates that contain unsafe code even if it is well audited. There's a finer balance you want to strike here.
- Animats 10y agoWe've seen this unsubstantiated denial before on YC, at "Why I’m dropping Rust"[1] and "Rust sucks if I fail to write X".[2] I once started going through the Rust library packages and listed uses of "unsafe". Try doing that. The previous discussions made a few things clear: - The big design-level problems with data structure safety are 1) partially initialized arrays, and 2) backlinks. The first is needed for growing arrays in Vec, and the second is needed for doubly-linked lists and some kinds of trees. It's very hard to handle either of those in safe Rust. A very small number of packages need unsafe code for those functions, and those should be tightly controlled. - Foreign code remains a problem, and is inherently unsafe when calling unsafe languages. The most downloaded Rust crate is "libc". What could possibly go wrong there? Was it really a good idea to import unsafe "strcpy" into Rust? pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char; - There's a lot of use of "unsafe" code that's not strictly necessary. You find unsafe code like "from_utf8_unchecked", which then turns up in the JSON decoder at (https://github.com/rust-lang-deprecated/rustc-serialize/blob/master/src/json.rs https://github.com/rust-lang-deprecated/rustc-serialize/blob...). There are no comments on the safety of that. Is there some way to create bad JSON, get bad UTF-8 into a string, and cause trouble further upstream? I don't know, but somebody "optimized" there, and created a potential problem. I could give many more examples. Most of these problems are fixable. They're not inherent in Rust. Fixing them is important to Rust's credibility. If Rust is going to replace C++, which it should, the holes have to be plugged. It only takes one hole to create a security vulnerability. Denial is not a river in Egypt. [1] https://news.ycombinator.com/item?id=12474445 https://news.ycombinator.com/item?id=12474445 [2] https://news.ycombinator.com/item?id=13670366 https://news.ycombinator.com/item?id=13670366
- bryanlarsen 10y agoI think the presence of unsafe code makes it more important that the lib goes through this process, rather than less. Provided the unsafe code is necessary, of course. That unsafe code is filling a want or need, so otherwise people are going to use crates that haven't gone through the process, or even worse, roll it themselves.
- Manishearth 10y agohttps://github.com/bluss/ordermap https://github.com/bluss/ordermap is 100% safe rust and pretty fast I believe the stdlib one could be refactored, but I'm not sure. There are a lot of optimizations in that one. IMO "no unsafe code" is a stretch. "no unnecessary unsafe code" is what it should be.
- Animats 10y agoThat "ordermap" is nice. Could that replace std::collections::hash_map as the main supported map crate? It imports std::collections::hash_map, which has unsafe code, but only for RandomState, which does not. If RandomState were pulled out of hash_map and moved to a crate with no unsafe code, that dependency could be removed and it would be safe crates all the way down. IMO "no unsafe code" is a stretch. "no unnecessary unsafe code" is what it should be. The author of the code doesn't get to determine "unnecessary". That should require extensive and hard to get justification.
- kzrdude 10y agoSure, it could replace the stdlib hash map. It would regress performance for some usage and improve it for others. (And this makes it an unlikely candidate; pure improvements are a lot easier to get in.)
- Manishearth 10y ago> Could that replace std::collections::hash_map as the main supported map crate? No, it is more specialized for certain kinds of loads. > It imports std::collections::hash_map, which has unsafe code, but only for RandomState, which does not. If RandomState were pulled out of hash_map and moved to a crate with no unsafe code, that dependency could be removed and it would be safe crates all the way down. That's a very vacuous distinction. This is all in the same crate (std) anyway, it's just a different module. > The author of the code doesn't get to determine "unnecessary". The auditors (proposed in this blog post) do. I've audited unsafe in the past to ensure our dependencies are fine, there are some pretty reasonable ways to define "unnecessary" here.
- epage 10y agoI suspect removing unsafe is outside of the scope of Rust. Rust tries to help you as much as possible but recognizes that it sometimes gets in the way and provides an escape hatch. The idea is to minimize and abstract the use of the escape hatch so its easily auditable. I wish they hadn't named the escape hatch "unsafe". I think I heard the name came from PL theory but it makes it sound scarier than it is. I remember hearing something about wanting to improve documentation around unsafe for how to best use it without running into problems.
- Manishearth 10y agoIMO it's good that it's scarier than it actually is :) http://doc.rust-lang.org/doc/stable/nomicon/ http://doc.rust-lang.org/doc/stable/nomicon/ is the documentation for unsafe. I do plan on improving it heavily, but I'm very busy right now, and I'd like to wait for some of the unsafe semantics stuff to be pinned down so that I can go in full depth when I write this.
- epage 10y agoThanks for your work > I'd like to wait for some of the unsafe semantics stuff to be pinned down so that I can go in full depth when I write this. Thats the work I was thinking of.
- eximius 10y agoIn Rust, 'unsafe code' is really 'partially compiler check exempt'. It does not mean the code is unsafe. You can manually verify the 'unsafe' parts. It is very unfortunately named.
- hyperpape 10y agoIt's unsafe in roughly the same way that Rust developers call C and C++ unsafe, right? The advantage is that it's a special, relatively uncommon mode, and it's explicitly marked. But I don't think you can minimize the reality that it's unsafe without also minimizing one of the major selling points of the language.
- epage 10y agoBecause only 10% of your code isn't verified by the compiler but instead needs extra review and tests doesn't eliminate the benefit that 90% of your code is verified by the compiler.
- eximius 10y agoUnsafe blocks are typically as small as necessary, so the burden is small to perform extensive manual verification
- Manishearth 10y agoYes, but the few exemptions means that you can effectively do basically anything. If you needed unsafe {} for something that block definitely needs more scrutiny and if possible should be avoided.
- Animats 10y agoRight. That's the road to buffer overflow exploits. The most recent CERT advisory reporting a buffer overflow exploit was April 17th, 2017.[1] About one per week is reported, year after year. Others not reported are probably being exploited. Rust can stop that, but one "unsafe" declaration can break Rust's safety. Hash maps need to be 100% safe code. They're complicated, and involve elaborate calculations that output subscripts. [1] http://www.kb.cert.org/vuls/id/676632 http://www.kb.cert.org/vuls/id/676632
- lmkg 10y agoI would actually hope for the opposite. If your application needs unsafe code, it would be better for that code to be isolated into a "blessed" (i.e. widely vetted by those with the relevant expertise) library with a safe interface, rather than rolling your own.
- pcwalton 10y ago> Everything else with unsafe code is an optimization. You need unsafe code to invoke a syscall. Doing I/O is not an "optimization".
- burntsushi 10y ago> Often a premature one. Can you share the ecosystem wide analysis that you did that led to this conclusion?
- cyphar 10y ago> Does the "Rust standard of quality" for these crucial crates include "no unsafe code"? > Everything else with unsafe code is an optimization. Often a premature one. Rc, Arc and Box require unsafe code for the obvious reasons that are not premature optimisations. Any attempt to use a syscall (look at the nix crate which wraps libc with safe APIs) requires unsafe code. Unsafe doesn't mean "this code is bad". It is true that unsafe code should be treated very carefully, but it's purpose is so that a sufficiently clever human can implement safe code that the insufficiently clever compiler cannot verify. Really it should've been called trustme rather than unsafe. ;)
- the_why_of_y 10y agoThe roughly equivalent annotation in D is called "@trusted", but IMHO "unsafe" is better because it sounds scarier :-) https://dlang.org/blog/2016/09/28/how-to-write-trusted-code-in-d/ https://dlang.org/blog/2016/09/28/how-to-write-trusted-code-...