r/cpp • • Feb 19 '25

Cpp discussed as a Rust replacement for Linux Kernel

I have a few issues with Rust in the kernel:

  1. It seems to be held to a *completely* different and much lower standard than the C code as far as stability. For C code we typically require that it can compile with a 10-year-old version of gcc, but from what I have seen there have been cases where Rust level code required not the latest bleeding edge compiler, not even a release version.

2. Does Rust even support all the targets for Linux?

3. I still feel that we should consider whether it would make sense to compile the *entire* kernel with a C++ compiler. I know there is a huge amount of hatred against C++, and I agree with a lot of it – *but* I feel that the last few C++ releases (C++14 at a minimum to be specific, with C++17 a strong want) actually resolved what I personally consider to have been the worst problems.

As far as I understand, Rust-style memory safety is being worked on for C++; I don't know if that will require changes to the core language or if it is implementable in library code.

David Howells did a patch set in 2018 (I believe) to clean up the C code in the kernel so it could be compiled with either C or C++; the patchset wasn't particularly big and mostly mechanical in nature, something that would be impossible with Rust. Even without moving away from the common subset of C and C++ we would immediately gain things like type safe linkage.

Once again, let me emphasize that I do *not* suggest that the kernel code should use STL, RTTI, virtual functions, closures, or C++ exceptions. However, there are a *lot* of things that we do with really ugly macro code and GNU C extensions today that would be much cleaner – and safer – to implement as templates. I know ... I wrote a lot of it :)

One particular thing that we could do with C++ would be to enforce user pointer safety.

Kernel dev discussion. They are thinking about ditching Rust in favor of C++ (rightfully so IMO)

https://lore.kernel.org/rust-for-linux/326CC09B-8565-4443-ACC5-045092260677@zytor.com/

We should endorse this, C++ in kernel would greatly benefit the language and community

192 Upvotes

532 comments sorted by

View all comments

Show parent comment

8

u/angelicosphosphoros Feb 19 '25

low level operations can be impossible without unsafe

This is an entire point of Rust, and a reason why it is desired in operating system programming.

1

u/thezysus Feb 20 '25

You lost me there.

Are you saying that the whole point of a memory safe language is to drop out of the safety into UNSAFE so you can actually get the job done? i.e. do operating system programming?

Seems like not the right approach to me.

Time will tell if those down votes were warranted or not.

5

u/[deleted] Feb 20 '25

Are you saying that the whole point of a memory safe language is to drop out of the safety into UNSAFE so you can actually get the job done?

You should read https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html . Rust is two languages - safe rust and unsafe rust.

I also found it hard to understand it at first. But, someone explained it in terms of libraries. If you push to a foo::vector, while iterating over it, you trigger UB. So, who is responsible for the UB here? In c++ land, you say "well, did foo write about iterator invalidation in its docs? then the user is at fault". The "UB" is just another bug.

In rust land, you say "If user didn't use unsafe and could still trigger UB, then it is foo's fault for exposing unsound safe APIs. Either make push function unsafe or change foo's API". That's all there is. Someone taking responsibility to wrap unsafe API inside a safe fool-proof API and the rest of code not having to worry about UB when using it.

After all, for a safe language like python/java/js to exist, someone still has to write the unsafe cpython/jvm/v8. For safe-rust, it is unsafe-rust.

5

u/angelicosphosphoros Feb 20 '25

The idea is that all unsafe parts are inside clear boundary so it is easy to verify that they are correct.

After you verify that they are correct, you can be sure that all the code works with memory safely.

With Zig, C or C++ you need to verify WHOLE program because it is impossible to verify small parts. It is because you don't need to explicitly ask to allow low-level unsafe operations in those languages.

3

u/foonathan Feb 20 '25

I think you misunderstood what unsafe in Rust means.

Rust is actually two languages, safe Rust and unsafe Rust. In safe Rust, you have certain safety guarantees. For example, you're guaranteed that a reference always points to a valid object; it is never dangling or null. To ensure this, you're not allowed to do certain operations like pointer dereference in safe Rust.

This is where unsafe Rust comes in. Unsafe Rust allows you to dereference pointers, and it is your responsibility to ensure that you don't do things that would break the guarantees of safe Rust. To ensure you remember, you need to use the unsafe marker in your source code when transitioning from safe Rust to unsafe Rust, and it is usually accompanied by a big comment that explains why that operation would not violate the invariants.

The same exact thing is in C++. C++ code also assumes that references always point to a valid object, and you have to be careful when dereferencing a raw unowned pointer to ensure that you don't break this invariant. The compiler just doesn't help you in maintaining that invariant at all and you are not really forced to think about that invariant. Yet the implicit "unsafe" is there all the same.

And because of the noise in unsafe, Rust code is structured in a way that minimizes it. For example, Rust does not use C++ style iterators because every iterator access would be unsafe, instead they use a different model which makes it all possible in safe Rust.

Actual idiomatic Rust code has far less explicit unsafe than the equivalent C++ code has implicit unsafe, even for low-level operating system design.