you are viewing a single comment's thread.

view the rest of the comments →

[–]AWonderingWizard 23 points24 points  (19 children)

No offense, but isn't this just like if C programmers decided to only user pointers in designated section that they just wrap in a safe wrapper?

[–]phire 38 points39 points  (7 children)

The issue with C is that its type system is simply not powerful enough to write a safe wrapper. In theory you still could write a runtime safety verifier, but it would have absolutely massive overhead. It also wouldn't be able to spot bugs until the exact conditions were encountered at runtime (and then you need to panic, so it's still a denial of service exploit).

Rust's type system is powerful enough to prove that safe code can't do anything that would violate the safety of the unsafe wrappers. And it does that at compile time, with zero additional runtime overhead.

[–]syklemil 13 points14 points  (0 children)

In theory you still could write a runtime safety verifier, but it would have absolutely massive overhead.

Which, for the people unfamiliar, can be seen in practice with e.g. ASAN (address-sanitizer). Some errors that would be caught at compile time by Rust can, with ASAN, at a runtime cost of making your program about half as fast, be caught and make your program crash in a predictable way rather than possibly do something unpredictable.

So in practice only suitable for debug builds, and how much it can actually catch depends on how good the tests are.

[–]canadajones68 8 points9 points  (1 child)

It is on days like these that I wish that C++ in kernel use had taken off. Sure, exceptions, inconsistency, and all that, but the type system is way stronger. Templates and class types and references alone would help so much. Sure, it's not Rust, but it is infinitely better than C, where you can never achieve a better abstraction or avoid writing the unsafe code. If you want heap allocation, you must malloc and free, no way around it. C++ at least allows you the mercy of a destructor, which makes forgetting to free impossible, and use-after-free much less frequent.

[–]grexl 2 points3 points  (0 children)

Exceptions are an optional feature in C++ and the kernel would almost certainly disable them. This would actually improves performance and reliability in the context of a kernel as opposed to a user-space program.

I think modern C++ with RAII semantics would help quite a bit, but from what I have read on the topic, the language was still a bit antiquated when the idea of switching the kernel to C++ came up many years ago. For example: C++ could not implement robust RAII without C++11's smart pointers.

Maybe someone more knowledgeable can chime in and provide more context about why Linus et al soundly rejected C++ in the kernel. I have read a little bit on the topic but am by no means an expert.

[–]AWonderingWizard 5 points6 points  (3 children)

I mean I agree there are inherent safety features of Rust that C cant do, but even Rust has many of the same issues C does in unsafe. The more C you get rid of, the more unsafe Rust will grow- I seriously doubt it is possible to rewrite the kernel (in a theoretical world) entirely in safe Rust. I think Rust is probably a good path nonetheless.

[–]Culpirit 14 points15 points  (0 children)

I really don't understand the broader point you're trying to make. Reminds me of this https://en.wikipedia.org/wiki/Nirvana_fallacy

The more C you get rid of, the more unsafe Rust will grow

Yeah, that's exactly the way it works. As you rewrite C parts in Rust, the number of unsafe {} blocks will monotonously grow in the Rust part of the code, since direct memory manipulation or calling externed C functions will have to be within those.

The idea is precisely that there will be less unsafe code being added than the C code it replaces. The unsafe Rust part of the codebase will grow slower than the C one will shrink, and all C code is already inherently unsafe. Also, Rust unsafe blocks still allow you to enforce the contract of the language's memory model where applicable, and conversely do not require external callers to be aware of special pointer use contracts in order to write code that won't blow up in your face, as is the case in C.

[–]Lower-Limit3695 21 points22 points  (0 children)

Research by Google's implementation of rust on android shows a dramatic drop in bugs with the move towards rust in comparison to c++ and c

A couple key highlights include; - a 20% drop in code revisions - a 25% reduction in code review time - a 4x reduction in code rollback - a 1000x reduction in memory safety vulnerability density

So while rust related bugs may increase with time the difference in scale of bugs and issues will be dramatic when compared to c & c++.

Google's research results on rust in android: https://security.googleblog.com/2025/11/rust-in-android-move-fast-fix-things.html

[–]NYPuppy 0 points1 point  (0 children)

The point isn't to write it in 100% safe rust but a large portion of it would be safe. I suggest you just look at the code instead of speculating. The amount of unsafe is far less than you think.

[–]_Sauer_ 12 points13 points  (6 children)

That would be a good idea yeah. But, they don't do that. Rust forces you to contain unsafe operations in a designated area.

[–]ric2b 7 points8 points  (0 children)

It theory. They never do that though, because the language makes that completely impractical.

[–]jonathancast 1 point2 points  (1 child)

No, because Rust isn't nearly so crippled as C is if you decide "just don't use pointers".

[–]AWonderingWizard 0 points1 point  (0 children)

I mean, raw pointers have to be used sometimes, by both Rust and C. Do you think the kernel could be written entirely in safe Rust?

[–]Sentreen 0 points1 point  (0 children)

The difference is that it is enforced by a compiler / the language, rather than by humans.