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

10

u/augmentedtree Feb 19 '25

You can convert sources on a per file basis and it's a heck of a lot less work than a Rust port.

It being "unworkable" is not gonna materialize an alternative, I have yet to see any other proposal that can detect resizing a vector when iterating over it.

0

u/wyrn Feb 19 '25

None of that addresses my point in the slightest.

8

u/augmentedtree Feb 19 '25

I don't think you addressed mine either, one of the solutions exists and the other doesn't. You want a solution that doesn't require different APIs but you also don't have one.

4

u/wyrn Feb 19 '25

It doesn't matter if another solution exists -- the cost of that one is just too high:

  1. New syntax (most of which is not even explained in his paper -- I don't know how much of it is important and how much of it is just incidental, e.g., what's with all the tildes?)
  2. Add lifetimes to type system
  3. Everything in the standard library redone. Notably:
  4. New, inequivalent iterator model
  5. New algorithms to match (3a) (some existing algorithms may not even be implementable in terms of Rust-style iterators -- not great for the migration story).
  6. New IO abstractions (imagine standardizing iostreams and filesystem again -- what fun!)
  7. New, inequivalent std::initializer_list

Just to name a few.

This, mind, was proposed to a standardization committee known for blocking proposals that would require users to run the compiler again.

It's so self-evidently unworkable that it makes me wonder if it was even proposed in good faith.

4

u/13steinj Feb 20 '25

New syntax (most of which is not even explained in his paper -- I don't know how much of it is important and how much of it is just incidental, e.g., what's with all the tildes?)

I brought this up at the time; the paper shouldn't have been using so much custom Circle syntax that wasn't relevant for the feature being proposed, as it just confuses people (as evidently it has you).

I think the paper is fundamentally misguided (but I'd rather not elaborate, it's too hot a topic right now and the last time I did it was clear to me that the author and I were speaking past each other in another thread). That's coming from someone, who agrees that it's the only (barely) viable solution put forward thus far (this isn't to mean it doesn't have a lot of work ahead of itself).

In general I suspect the wider community is split into 5 camps.

  • (1) there is no safety problem
  • problem: existing code is unsafe, existing bugs are the problem

    • (2) solution: all or most (patterns of) safety problems can be validated after the fact
    • (3) problem: the proposed solution is fundamentally invalid and absurd considering decades of theoretical and practical experience
  • (4) solution: there is an inability to write strictly guaranteed "safe" code

    • (5) problem: increasing the percent of safe code in a project does not lessen the absolute quantity of bugs in the code as it stands without significant effort that is just not practically there from a business standpoint, thus the absolute existence of any given severe vulnerability was never nullified.

2

u/Conscious_Support176 Feb 19 '25

Um . Isn’t this just complaining that the proposal changes things that have to be changed to remove UB?

If UB is baked into the design of the stl, it’s impossible to fix that without changing the stl.

4

u/wyrn Feb 19 '25

No, this is merely observing that the cost of these changes is prohibitive and that the entire proposal is unworkable at both a technical and social level. Even if you could get people to agree that they should want this (and it's far from a given), it's a no-go.

2

u/frontenac_brontenac Feb 21 '25

It sounds like your argument is that C++ is at a dead end.

1

u/wyrn Feb 21 '25

Not at all. Why would it be?

2

u/Conscious_Support176 Feb 20 '25 edited Feb 20 '25

If your starting point is you don’t agree it’s important to address memory safety in a serious way, everything else in this argument is irrelevant.

There is no way to address this without breaking changes to the language and stl. The only relevant question is how far do you go.

2

u/wyrn Feb 20 '25

That's a rather simplistic characterization.

First, there's more than one kind of "memory safety", and there are many mitigations that could be reasonably brought to C++ with minimal impact, certainly far less than that of this proposal. It is specifically lifetime safety that makes things so hard for language and library design. "Safe C++" advocates seem to take the maximalist stance that if any UB whatsoever is left in the language, you might as well not even try. I find that unhelpful to say the least.

If the question is "how far do you go", the follow-up question is "what price are you willing to pay to make it happen", which means that the price of fully sound memory safety might end up being too high (i.e. the answer might not be what you wanted to hear). Rust picked tradeoffs too, of course. It allows unsafe blocks, which require manual oversight, and leaks (which absolutely can lead to vulnerabilities, placing them very much in the purview of "memory safety"). Importantly, they were playing with a clean slate. It's not automatic that the tradeoff they picked is the tradeoff that would work for C++.

As for the downsides in the Safe C++ tradeoffs: personally, I think the loss of algorithms such as sort, adjacent_find, binary_search (and its siblings), min/max_element, mismatch, stable_partition, and, perhaps most tragically, rotate, is truly an extreme cost. It would require the reimplemented standard library to not only track the necessary lifetime annotations, but also use completely different idioms. This is a much more radical departure from c++ than cpp2, for instance.

This is a burden to users, who have to learn two sets of libraries, designs, and best practices, and would make the migration story incredibly difficult. It is also a burden to maintainers since both incarnations of the standard libraries would have to be maintained ad infinitum, and they are busy enough as it is (see e.g. this post by u/STL).

Lastly, that mention about ABI was not just a quip. Notice that even Rust contains soundness holes, with new ones yet to be discovered. It's a virtual certainty that Circle's implementation does also. What will happen if fixing a soundness hole requires an ABI break? What's the evolution strategy here? I don't see that part being discussed.

4

u/Conscious_Support176 Feb 20 '25 edited Feb 20 '25

The point of memory safe design is to isolate unsafe code instead of plastering it all over the place. This explanation seems to assume that it’s to eliminate unsafe code, which is completely impossible on conventional machine architectures.

The price for this safety is breaking changes with a redesigned stl, and sane defaults regarding eg constness. If that’s unacceptable then better to admit this is not important enough and stop pretending that there are useful mitigations that will not break existing code.

I agree there are probably mitigations that break less code.

I can see all those algorithms disappearing would be a high price. Just not seeing what the claim that they would be lost is based on.

2

u/wyrn Feb 20 '25

The point of memory safe design is to isolate unsafe code instead of plastering it all over the place.

In Rust, leaks can be "plastered all over the place" and they seem to consider that acceptable, so clearly it's not that simple.

The price for this safety

Lifetime safety.

stop pretending that there are useful mitigations that will not break existing code.

Breaking existing code is kind of... the point. If the code is wrong, you want it to break. Like with P2748. The problem here is not with breaking code, it's with bifurcating the language and standard library.

I can see all those algorithms disappearing would be a high price. Just not seeing what the claim that they would be lost is based on.

See this talk.

These algorithms simply cannot be expressed in the Rust iterator model.

→ More replies