The Binder Linux driver is being rewritten in Rust
lore.kernel.org
lore.kernel.org
The wrappers have no additional cost when compiled: A nested structure of Wrapper<OtherWrapper<OverSomePointer>> has a size of the flattened contained data, all the type info will be stripped away.
This part is the hardest, but the reward is that you can then use these wrappers without thinking about lower-level details. Of course, it is possible to do that in other languages, but rust type system can encode more information in types. That means more checks at compile time and less cautionary instructions are needed in the code comments.
I always hear arguments that "Rust still does not prevent X!". That's not the point. Rust type system simply can prevent more if used as intended.
That's just the Nirvana fallacy. Just because it's not perfect shouldn't prevent you from improving stasus quo.
Absurd example: "Seatbelts don't prevent impalements or drowning in car, ergo they are useless.
Second, the nirvana fallacy is comparing actual things to unrealistic, idealized alternatives. You're comparing actual (seat belts) to an idealized alterative (something that prevents all car crashes, no matter the cause).
Third, it's a less generalized example. Stuff seatbelts don't protect and might even lessen chances of survival - rockets, gunfire, rhinoceros, trains, meteors, nuclear blasts, black holes, multiversal collapse.
could have been this one: https://www.snopes.com/fact-check/seat-belt-advocate-killed/
In Rust, an enum can also carry data along with the tag, and the type of the data can be different for each value of the tag. A Rust enum is more like a "tagged union" (a struct containing both an enum as the tag and a union for the extra data) in C.
Perhaps the most practical example of this is the Option type, where you can say an Optional value is either `Some(value)` or `Nothing`.
With this, you don't have do deal with null pointers; a `null` pointer would instead be `Nothing`, and a non-null value would be `Some(value)`, and the compiler can enforce you're sticking to this.
Most C libraries and system calls return integers not enums. Often the meaning of those integers is hard coded in a header somewhere. There's no guarantee that the number returned will be one of the values in the header.
When a library does return enums, there's no guarantee the value returned is actually in the enum. It could be anything.
When you pattern match an enum in C, you have no checks to make sure you catch all the cases, or don't drop a break;.
All of the above means that in C, you're relying on the programmer to cross check they haven't made a mistake. Rust does it all for you - if you match against an enum type, you have to handle all the cases. You can't return anything other than the type you said you would. It basically does all the work that the programmer has to do in C. And most importantly, it does it every time you recompile. Even if the first person to call a function in C is meticulous, the members in the enum might change later.
I agree that Rust has more comprehensive guard railes, but except for pointer ownership and sharing where C is missing tools (although model checking tools can achieve this too), I think you could basically have everything else in C as well by building similar abstractions building on existing compiler warnings and maybe adding a couple of new ones.
So while I like some ideas of Rust, I think in the discussions proponents often compare it to a worst-case C without old programming style and no static analysis applied and comparing to an ideal Rust with no use of unsafe (and also this binder rewrite uses unsafe).
[1]: https://cs.opensource.google/fuchsia/fuchsia/+/main:src/star...
> the mechanisms provided by an operating system for processes to manage shared data
"OpenBinder is the core technology that ex-Be engineers started at Be, Inc. as the “next generation BeOS”, finished implementing at PalmSource "
https://www.osnews.com/story/13674/introduction-to-openbinde...
They say 6,000 lines of code. That's not very much.
I think putting this stuff in the kernel is rather silly, but I'd rather have it in the kernel than bring back the rift between Android and Linux.
Hence why modern Android drivers are called Binderized drivers.
https://lpc.events/event/16/contributions/1180/attachments/1...
Could I theoretically use it in my linux program just like any other IPC mechanism?
Yes, absolutely.
However, binder is implemented as a kernel module not enabled by default, so it depends on the build time configuration of your system's target kernel (eg., your distro's).
That given, it's important to note that the binder driver/module is just one piece of a greater framework and it's raw IPC features aren't as simple to use as SysV or POSIX's. For example, it requires a userspace process called context (or service) manager. Android has 3 different binder device instances and it builds a big framework on top of them wiring things like an interface definition language (AIDL), a set of libraries and SELinux permissions.
I'd argue that "It prevents mistakes with ref counting, locking, bounds checking..." implies "all" mistakes, but hey, maybe not...
The main missing piece in rust locks is inter-lock dependencies when they are not nested.
This locking pattern is quite old and frequently available in safe languages.
Ada calls it "protected objects" and has had it since Ada 95:
https://learn.adacore.com/courses/intro-to-ada/chapters/task...
Java calls it "synchronized" and has had it since Java 5 or 6:
https://docs.oracle.com/javase/tutorial/essential/concurrenc...
The Rust (also C++) mechanism is that when you lock a mutex you get a lock guard object and when the scope ends the guard is dropped and therefore the mutex is released; you don't need to put the code accessing the locked data inside a particular locking object.
For example, how would one implement https://doc.rust-lang.org/std/sync/struct.RwLock.html with that?
However, you do get to choose what kind of locking scheme you use and, crucially, it is impossible to access said data without holding a lock. Indeed it is not possible to have concurrent access on data without having some locking implemented and used.
Is rewriting something in Rust a guarantee of no bugs? Nope. But it does likely make it easier for the rewriters to reduce the number of bugs.
I recall reading somewhere that a lot of the ideas for Rust's safety system came from a Firefox analysis of bugs that had been reported and fixed, where something like 70% of bugs fell into a few broad categories (mostly memory safety, like use-after-free, buffer overruns, off-by-one, etc) which they could solve in a new language that could enforce correctness. The idea of being able to remove 70% of bugs, and to effectively guarantee that any bugs that do occur happen in those remaining 30% of areas, sounds like it could save a lot of developer time.
However, Rust can prevent quite a lot of common mistakes. Getting rid of UAF, data races, and having deterministic destruction that unlocks locks is already a major quality improvement.
Rust can’t prevent deadlocks caused by wrong architecture, but of all concurrency issues deadlocks are the easiest to diagnose.
Uhh, no. That is an amazing programmer! Why? Because truly terrible programmers imagine a subset of every possible failure case and simply refuse to acknowledge other failure cases, especially the ones that are particularly common with particularly severe consequences.
However, there are schedulers that do guarantee deadlock free operation written in Rust, see RTIC for example.
Second, moves by default. They make building wrappers that depend on creation and destruction of a value much easier. They can track various things: memory usage, threads, temporary pointers, or whatever else. Unlike unique_ptr, they are on stack and part of the type system.
Also, Rust explicitly puts safety first, and C++ cannot give the same guarantees for normal code. The uses of `unsafe` in Rust should be relatively uncommon and deserve extra scrutiny.
Can you give me example of a normal C++ code where compiler and/or language will not guarantee safety? Let's put aside UBs, because they are everything but normal code.
Why would you avoid saying that? Rust does give guarantees: about memory safety and concurrency primarily, but also regarding the lack of undefined behavior.
> It paints Rust as this magical thing that will solve anything.
It does not, the above are not magical they are just challenging problems (although at some point they may have been deemed impossible problems and hence magical, I don't know)
> My preferred explanation is that Rust provides better tools to build wrappers that can't be misused.
"wrappers that can't be misused" sounds a LOT like it "gives guarantees".
* you will not be able to compile undefined behavior
* you will not be able to create a data race
* a shared reference is read-only
* you cannot write past the end of an array
There are others, but those are pretty big ones that both guarantees and part of the language itself, independent of stdlib.
Almost all C++ code interacts with bare pointers at some point. And that’s just one of the places where you get into issues with safety.
You can also run into problems with iterators being invalidated: your iterator into a std::vector is no longer valid after a call to push_back().
Anything that stores a reference and can exist after the lifetime of the object, really. Lambdas are a problem here. But so are instances with reference members.
C++ is littered with these things.
This is a device driver. If it has to talk to memory-mapped hardware, that's a raw pointer, inherently, inevitably.
Other things are language issues, and can be solved by changing languages, but raw pointers are inherent in the task.
This is an IPC "device driver". It does not talk to any hardware, it's used to pass messages between processes.
It's not, it's an IPC subsystem. It doesn't talk to any hardware.
An easy way to trigger undefined behavior in C++ is by using a pointer after the thing it's pointing to has been freed. Doing this in a process that takes any user input can easily create a vulnerability that allows an attacker to inject code into the process. There are many ways to accidentally do this while thinking you're safe. A pointer to an item inside a data structure like a vector or hash table is dangerous if you use it after some items may have been inserted into the data structure, because inserts can cause the data structure to reallocate and expand its backing memory. This mistake can easily go unnoticed for a long time because only a small fraction of inserts will cause that. This mistake isn't possible to make in safe Rust.
If you look at Linus’s initial reasoning [0] (which was from a long time ago, nearly 20 years now, way before C++11), it was because the abstractions it offered/encouraged could make it hard to reason about what’s really happening, which may be bad for kernel code, and that allowing C++ would open the floodgates for C++ programmers to contribute, and Linus famously hated C++ programmers.
Rust-in-the-kernel came up many years later, and IMO if Linus was really honest with himself he’d either (1) not allow rust for essentially the same reasons he disallowed C++, or (2) admit his views have evolved, and say that C++ should be allowed on the same tentative basis that Rust is.
I get that rust offers safety advantages that C++ doesn’t, but I don’t think that’s the complete picture… modern C++ offers so many of the same advantages (although obviously not all) that if we’re really being honest with ourselves, it would seem to be unfair that Rust is let in while C++ is not. Both of them are enormous steps up in safety over C.
I would argue one (Rust) is, but not C++. Rust is more "batteries included" and doesn't require specific compiler flags or complicated tooling, it just works and gives you guarantees out of the box. C++ seems to allow escape hatches by default while Rust requires you to call that out via `unsafe`.
Unless what he disliked was the inheritance/OOP stuff in C++, which isn't an issue in Rust.
Interfaces/traits, dynamic dispatch, static dispatch, encapsulation, polymorphism, traits inheritance, is all there.
Additionally macro system as powerful as Common Lisp, which allows to do stuff that would make Linus blowup on the spot.
As for the complaints, they are kind of doable in Rust as well.
Use traits with function pointers, empty types, and some macros.
I think it's fair to say that Rust is a lot less 'OOP' than C++. There is no concept of 'protected', traits are separate from structs, there's no data inheritance, there's no 'isinstance', dynamic dispatch is explicitly behind 'dyn' keyword, etc.
> Additionally macro system as powerful as Common Lisp,
It's not like the Linux kernel doesn't make use of C's macros.
Plain textual text replacement isn't the same as a proper macro system.
How would C++ features like RTTI, exceptions, and the std template library be used? What about the insane ideas of classes and inheritance which induce mind numbing impossible to comprehend code?
Rust is actually pretty readable and reviewable. C++ tends to be mind numbingly hard to review.
You mean time to rot?
"Experience shows that this [memory safety] cannot be done without static analysis and run-time support. Furthermore, for fundamental reasons this cannot done even with such support if arbitrary legal language constructs are accepted while conventional good performance must be maintained."
[0] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p24...I find statements like this humorous. Who is "the programmer" in the above sentence, is it the "it" in "it is raining"?
It doesn't take it out of the hands of the programmer, it separates/delegates it to the programmer creating the types vs the programmer creating the implementation. They might be the same programmer and that programmer might be very happy they could separately encode such checks, but it doesn't take it out of anyone's hands.
I would agree that lifetimes are taken out of "the programmer's" hands in Rust for all "programmers" who are not working on the Rust lifetime compiler :D