4 ms·
I work on firmware and kernel-level code, in pure Rust, at my job. We don't do malloc at all. One of the key features Rust enables is that you can build safe a
by steveklabnik 6y ago
I work on firmware and kernel-level code, in pure Rust, at my job. We don't do malloc at all.
One of the key features Rust enables is that you can build safe abstractions on top of unsafe things. Another key feature is that those abstractions often mean compile-time, but not run-time, cost.
If you build the right abstractions, you can minimize the amount of unsafe code pretty dramatically. I don't have numbers handy for our codebase (and it's not open source yet, but will be eventually), but as an example, the Redox documentation (a Rust-based kernel that is, uh... linked... below) says: https://doc.redox-os.org/book/ch01-07-why-rust.html#unsafes https://doc.redox-os.org/book/ch01-07-why-rust.html#unsafes
> A quick grep gives us some stats: the kernel has about 300 invocations of unsafe in about 16,000 lines of code overall. Every one of these is carefully audited to ensure correctness.
The advantage here is that, if and when there are bugs in this code, you only have to look at the modules containing those unsafe invocations, rather than the entire codebase.
There are also other advantages to the Rust language that make it worthwhile too, regardless of safety percentage, but it is true that this is often people's first concerns. The code that landed in linux-next has a lot of unsafe in it, because it is specifically trying to build out the proper abstractions here. The idea is that drivers built on top of this code will not need nearly as much, generally.
- dahfizz 6y ago> The advantage here is that, if and when there are bugs in this code, you only have to look at the modules containing those unsafe invocations, rather than the entire codebase. That is a brilliant feature I had not thought of. Thanks!
- steveklabnik 6y agoNo problem! I think the skepticism is very warranted. We know that these systems can be created, but we don't have examples of this working at the same scale as projects like Linux yet. That takes time and effort, exactly as Linus and Greg Kroah-Hartman say. We'll see how it works out in practice. I also think the "de novo system" and "adding Rust into a large existing system" are two related, but different, problems, with their own unique challenges.
- xiphias2 6y agoOne great thing that I see with Rust is that the developers are open to extensions of the language that are provided just to be more compatible to the C memory model (like anonymous unions). I don't know many other languages like that.
- mcguire 6y ago"The advantage here is that, if and when there are bugs in this code, you only have to look at the modules containing those unsafe invocations, rather than the entire codebase." I would be cautious with statements like that. You can limit some kinds of operations to unsafe code, audit the daylights out of those, build abstractions that make it impossible to use that unsafe code from safe code in an unsafe manner, and still not be doing the right thing.
- steveklabnik 6y agoSure, I thought the context of this discussion was memory safety specifically, and so that's what I was talking about. In a less specific conversation I would have said "memory safety bugs," but thought it would be redundant here. I guess not.
- junon 6y agoWhich types of bugs? Please specify when talking about code safety.
- steveklabnik 6y agoI do generally, but given that this is a pretty focused topic on something very specific, I thought "memory safety" was that topic.
- DSingularity 6y agoFor example use after free or array indexing bugs (eg incorrect limit checks).
- alexfromapex 6y agoAnother advantage, when compared with C, is the package/dependency management and build tooling which makes compiling for targeted architectures a little easier
- jabedude 6y agoTo be honest this advantage is not being used in Linux. kbuild isn't going to use cargo but rather rustc directly