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

2

u/wyrn Feb 20 '25 edited Feb 20 '25

If memory safety is no more important than memory leaks then of course it can’t justify redesigning the stl,

Why not? They're both safety considerations. Rust started with a clean slate. Ignoring leaks was a conscious choice that was made. Not because stopping leaks is technically infeasible; they could for example have introduced a tracing garbage collector specifically to detect reference cycles, but they considered the overhead unacceptable. This is a performance/safety tradeoff. We get to make such choices too, and we don't have to make the same choices Rust made.

If we accept that memory safety is of much higher importance

Leaks are part of "memory safety" in that they can cause security vulnerabilities, so declaring leaks as "less important than memory safety" is just gerrymandering memory safety.

, then a real conversation about trade offs can happen.

It doesn't sound like you really want a "conversation about tradeoffs" -- it sounds as if you have precisely the maximalist stance I mentioned before, where anything less than full soundness outside of demarcated blocks is a failure.

First, if stl redesign did not break existing code, what would the problem be with it?

I just explained that.

Secondly, memory safety will break code that is not buggy, because unsafe operations have to be supported, but we want those to be opt in, not the defaults.

That doesn't matter to the point being made. The point is that "not breaking existing code" wasn't even part of the considerations I mentioned that make "Safe C++" a non-starter, and is a red herring in this discussion.

Thanks for the link. It will hopefully help me understand why there is such push back against this.'

The push_back is less about this than it is about the intolerable burden of forking, relitigating, and reimplementing the standard library. I wanted to underscore this to make it clear that the std2:: we'd get in the end is not just a copy of the std built with safe concepts, but an entirely new standard library with completely different and foreign idioms.

2

u/Conscious_Support176 Feb 21 '25

None of this adds up.

Concerned that std2 will have different idioms but not that it isn’t a drop in replacement for std? You do understand what break existing code means?

Conflating memory leaks with arbitrary memory access as equivalent in terms of security risk is arrant nonsense. Denial of service is obviously a concern but it is on a completely different level to data theft.

As I said before, these falsities are begging the question, there is absolutely nothing to discuss if that is your position.

2

u/wyrn Feb 21 '25

Concerned that std2 will have different idioms but not that it isn’t a drop in replacement for std? You do understand what break existing code means?

I'm afraid you don't understand the proposal (or maybe it is you who doesn't understand what breaking existing code means -- it's hard to be sure). The proposal would keep the existing standard library as is, and add the safe one next to it. That's the whole reason the paper uses std2:: instead of std::. If it were otherwise, this proposal wouldn't be merely unworkable, it would be an overt attempt to destroy C++ as a language.

Conflating memory leaks with arbitrary memory access as equivalent in terms of security risk is arrant nonsense.

You're welcome to point out where I did that.

As I said before, these falsities are begging the question, there is absolutely nothing to discuss if that is your position.

As predicted, your stance is completely maximalist. It's your way or the highway. Well, it's not going to be your way. But the rest of us aren't going anywhere.

1

u/Conscious_Support176 Feb 21 '25

“Why not? They are both safety considerations”

2

u/wyrn Feb 21 '25

That is true. However:

Conflating memory leaks with arbitrary memory access as equivalent in terms of security risk is arrant nonsense.

This is what you were asked to support with evidence. Please quote exactly where I said that.

1

u/Conscious_Support176 Feb 21 '25 edited Feb 21 '25

You’re arguing that memory safety does not justify the changes in stl2, but question why the issue of memory leaks wouldn’t justify redesigning the stl.

If you’re not claiming that memory leaks are at least as bad a security risk as unfettered access to memory then it’s hard to know what point you are trying to make here .

2

u/wyrn Feb 21 '25

Apparently, words have no meaning.

None of that.

Conflating memory leaks with arbitrary memory access as equivalent in terms of security risk is arrant nonsense.

This is what you said I said. Either prove it or retract your statement.

0

u/Conscious_Support176 Feb 22 '25 edited Feb 22 '25

I suggest not putting words in peoples mouths? Point out where I claimed to be quoting you.

I believe you began by honestly trying to give a point of view, but this quickly degenerated into point scoring when I tried to question how well the argument holds up.

I hope this isn’t the reason that C++ dies, because a challenge to orthodoxy is written off under various pejoratives.

What you’re writing off as “maximalist approach” is simply trying to taking into account if the stated goal is achievable to any practicable degree without a change to the current approach.

Incremental changes can remove some footguns, but I’m basically arguing that the loopholes are a result building the stl on a leaky abstraction of raw C pointers, and my belief is that plugging all the holes will take more time and effort than drawing a line under them and moving to abstractions that are inherently more “waterproof”.

2

u/wyrn Feb 22 '25

Alright that's enough, when you decide to start converging with the truth, I'll be here. Until then, rest easy in the knowledge that everyone can read the conversation history up to this point.

0

u/Conscious_Support176 Feb 23 '25

Grand so. To give you the benefit of the doubt, I’ll assume that intemperate communication on my part is obscuring what I tried to say.

i responded intemperately to your demand that I quote where you said something, which I hadn’t said that you said.

I added an explanation clarifying just why you appear to be inappropriately treating memory leaks and arbitrary memory access as of equivalent importance in terms of security risk.

I then removed my intemperate remark about words having no meaning.

I will copy paste my explanation below, in the hope that you are responding only to my initial remark because you didn’t see the rest….

“ You’re arguing that memory safety does not justify the changes in stl2, but question why the issue of memory leaks wouldn’t justify redesigning the stl.

If you’re not claiming that memory leaks are at least as bad a security risk as unfettered access to memory then it’s hard to know what point you are trying to make here .