4 ms·
> But it's apparently necessary in Rust to use "unsafe" > code in libraries to implement basic structures like > re-sizable arrays. Efficient data struct
by kibwen 12y ago
> But it's apparently necessary in Rust to use "unsafe"
> code in libraries to implement basic structures like
> re-sizable arrays.
Efficient data structures are one domain where single-ownership prevents barriers to implementation, true. Your domain will determine how much unsafe code you need to deal with. For efficient data structures, you will need a large amount of unsafe code (probably a third to half of your code). For applications that need to really push the performance envelope, I expect you'll need around 10% unsafe code (this is a ballpark estimate for the amount of unsafe code in Servo, last I checked). For most domains I'd expect the volume of unsafe code to be less than 1% of your codebase (contrast this with C and C++, where by Rust's definition 100% of the codebase is unsafe).
> Living with strict single ownership semantics is hard.
This is why Rust provides a reference-counted smart pointer (somewhat like C++'s shared_ptr) in the standard library, for those cases where multiple ownership is necessary and you don't want to manually wrangle raw pointers. There's unsafe code underlying it (the aforementioned raw pointer wrangling), but putting it in the stdlib means that it can be well-audited.
> This may be a way to avoid using "unsafe" so much.
Rust tries very hard to give you the tools to avoid using `unsafe` blocks, but it's a thoroughly pragmatic language and so it knows that sometimes such code is unavoidable. If you could afford inefficiency in the name of 100% safety, you'd just be using a GC'd language. If it denied you efficiency in the name of dogmatic safety, you'd just go back to using C++. The complexity here is inherent to the domain. There must exist a compromise if we expect to ever make progress.
- pcwalton 12y ago> For applications that need to really push the performance envelope, I expect you'll need around 10% unsafe code (this is a ballpark estimate for the amount of unsafe code in Servo, last I checked). 10% seems higher than any number I've seen. In any case, it matters quite a bit where the unsafe code is, and that's why I don't like just counting numbers of lines of unsafe code. Empirically, a large number of security-critical bugs in browsers are in layout code, often due to use-after-free. Servo core layout code denies unsafe via `#[deny(unsafe_block)]`. We do not allow unsafe core layout code to be checked into the tree; the only unsafe code in layout is general code that manages relatively simple invariants. The question that matters is: Can you write high-performance code in Rust without writing unsafe code of your own? I'm confident that the answer is yes.
- Animats 12y agoIf it denied you efficiency in the name of dogmatic safety, you'd just go back to using C++. For new work, most efforts are going forward to C# or D or Go or Java, all of which solve this problem by using a garbage collector. The claim for Rust is that it's memory-safe without a garbage collector. This claim does not appear to stand scrutiny. If 10% of a browser has to be "unsafe", the language is seriously flawed.
- pcwalton 12y ago> For new work, most efforts are going forward to C# or D or Go or Java, all of which solve this problem by using a garbage collector. The claim for Rust is that it's memory-safe without a garbage collector. In C#, D, Go, and Java, arrays are implemented as language primitives in the runtime. Their runtimes are implemented in raw C or C++ and are not formally verified. So my question is: Why is implementing an array in C as a language primitive safer than implementing it in the standard library via unsafe code? There's a similar story for the swap operation. Rust in fact used to have a swap operation, as you suggested, but it was removed because such an operation can easily be implemented in the library. We could have moved the code back into the compiler, but how would that have made the language safer? (In fact, I would be tempted to argue that implementing language primitives in codegen is less safe, since programmatically generating LLVM IR is more difficult than writing essentially C with a different syntax.)
- tomp 12y agoYou didn't really answer the question. The GP was asking, how can Rust claim to be "memory-safe" if 10% of application code (e.g. Servo) is unsafe code. We can accept unsafe code in the standard libraries (equivalent to how C# or Java or Go are implemented), as it's reasonable to assume it will be extensively peer-reviewed and battle-tested.
- pcwalton 12y agoYou shouldn't be writing unsafe application code. If your applications needs new unsafe abstractions (which you can think of as language extensions), then you can write those. But unsafe is absolutely not for application code. (This is, as I explained downthread, part of the reason why I dislike going around quoting "X% of the code in Y app is unsafe." Your application code shouldn't be unsafe.)
- arthursilva 12y ago10% is totally unrealistic, and you also have to consider that most unsafe usage is actually interacting with C libraries and custom low-level data structures. Projects like cargo don't have a single unsafe.
- JoshTriplett 12y ago> For applications that need to really push the performance envelope, I expect you'll need around 10% unsafe code (this is a ballpark estimate for the amount of unsafe code in Servo, last I checked). Servo isn't just "a browser engine written in Rust"; it's a project that serves as the largest driving force for the language. As a result, many abstractions that will in the future end up in the standard library or one or more packages will show up in Servo first. And I'd expect the implementation of the standard library to contain a significant amount of unsafe code, as part of implementing abstractions usable without invoking "unsafe". I would expect other application projects to have far less unsafe code on average. New Rust libraries wrapping C libraries, or creating new low-level abstractions around memory, will have quite a bit of unsafe code, with a safe exported API.