by ilovecaching · 4y ago · 0 comments
I am surprised Linus agreed to this given that he doesn’t like C++ and usually errs on the side of clear over clever, and we have yet to see tangible benefits of using Rust.
As a long-time C and C++ programmer, I have to disagree. C and C++ have been great and so much software has been written using them, but the time has come to move on.
The majority of software security bugs are due to the lack of controls in C and C++. This is not an opinion or an anecdote, it is a fact:
https://www.zdnet.com/article/microsoft-70-percent-of-all-se...
https://www.zdnet.com/article/chrome-70-of-all-security-bugs...
https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI...
Operating systems need to be secure. Operating system kernels even more so.
The Linux kernel could move to Rust or Ada or Nim or something else, but keeping it in C is an invitation for continued security problems:
https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=%22linux+ke...
Time will tell, but Rust support for use in Linux kernel development is increasing.
If it solves problems and is easier to use, it will gain traction and see wider adoption.
It seems that the primary security argument is that N-1 types of issues is better than N. I would argue that the formula is really N - 1 + X, and that we're all focused on the very positive "-1" aspect, but what's the value of X? In other words, what are we trading off by using Rust instead of C in the kernel? Why trade a language which we know the pitfalls of (N) for a language that's potentially adding X pitfalls, and if you're going to argue that Rust has no pitfalls, then to me that's just the kool-aid talking. Realistically, in a new language, you don't know if X is 0, and adding unknown unknowns are precisely the one thing you do not want in a critical piece of software like a kernel.
Furthermore, if OSes need to be secure, and system kernels need to be even more secure, which we all agree on, then all you're doing by advocating against using a stable language with known pitfalls like C is you're lowering the knowledge bar ("hey look I can write kernel code now because I won't introduce memory issues!"), thereby increasing the pool of people who will make logical mistakes out of lack of knowledge, which you will not eliminate with static analysis.
C is not ideal, but it's a practical way to gate the entry to critical software, because
1. C, the language, has a handful of pitfalls, while the software written in any language has orders of magnitude more avenues to exploit it (e.g. access permissions, communication protocols, environmental assumptions, etc).
2. Most of what's difficult in C stems from the realities of dealing with computer architectures. If you can't be bothered to learn the ins and outs of C, you're not fit to write portable low level machine code in a kernel just yet. Just because we can be reasonably sure you won't introduce a memory problem is not sufficient guarantee that you won't do stupid things in the kernel that will cause security problems in a different way.
In the end, I hope I'm wrong about Rust and X is 0, but it seems to me that there's a much better way to ensure we get to an N-1 situation without an X: introduce the borrow checker in C.
Why throw the baby with the bath water? The software industry is very aware by now of the pitfalls of rewriting a project instead of making incremental changes. Bring the memory safety to C. You get 50 years of stability and well documented issues, while still removing a whole class of bugs. Now that would be an absolute win.
Aw, yes, the C makes genius programmers meme. I think we've seen very little evidence this is the case. C may be useful to know. Learning C well, because it is difficult, may be useful. These things are speculative, but let's grant them. Unfortunately for you, the proof of the pudding is in the eating? The fact that lots of C code has lots of problems should be an indication that, not only is C not working to solve many modern problems, but that, if C gives us geniuses, we aren't seeing the results.
> introduce the borrow checker in C
You: Just glue on some wings and see if it flies...
Me: That's not how this works! That's not how any of this works!
> Aw, yes, the C makes genius programmers meme
Not claiming that. I'm only claiming that to be effective at C you must understand computer architecture well. You did set up a strawman here.
> if C makes gives us geniuses, we aren't seeing the results
Perfect strawman execution.
> Just glue on some wings and see if it flies...
Right, because the only way to fix problems is to rewrite? Typical inexperienced programmer attitude.
Sure, and we have driving licenses to make sure people know how to handle a car, but you won't find a race driver who doesn't use seatbelts. Because even the best humans are still human. Arguing against safety measures because you think you're too skilled to make mistakes is also something to be avoided.
We agree though that adding memory safety to C would be the equivalent of racecars with seatbelts.
If professional chefs would constantly maim themself and each other despite extensive occupancy safety training, knives have no place in the hands of a responsible professional.
Using C is the SE equivalent of taping down one button of the two-hand control device of a die cutter.
This is patently untrue. What do malloc and free have to do with computer architecture? Most of C's difficulty is deficiency of the language unrelated to its low level nature.
proof or gtfo.
> What do malloc and free have to do with computer architecture?
You're talking about a library at this point, but ultimately the root cause for allocating memory is because memory is limited, unlike, say CPU time. We do not allocate CPU time explicitly, even though there could be a CPU architecture that could support that, and C would run on it.
> Most of C's difficulty is deficiency of the language unrelated to its low level nature
Do you think that comparing signed and unsigned integers is also unrelated to CPU architectures?
Anyway, in terms of bug severity that 70% is also way worse than the other 30%.
The rest of your post is all just silly "people should code better" and isn't worth responding to.
I don't disagree, but this is objectively just hope. The only reason N*7 is huge is because C's adoption. There is nothing to suggest that Rust's N*(bad issue %) is smaller than Cs if it had the same adoption as C. It's fanbase is just trying to increase its presence at the expense of C, to then be able to say "see, we did it, we replaced C and we're better off!", but today, it's equally likely that the outcome is going to be "well, we didn't know about <insert issue here> at the time".
I don't think it's fair to blame the issues that code written in C can have on incompetence.
Is it? Rewrites only succeed if the original people rewrite the original code, because code is just a serialization of someone's thoughts. That's why it's the people who are the most important assets in a project, not the code. When the original people leave, projects typically fail.
Rust is not being written by the same people who made GCC. It's a different group of people who are trying to do better than C. I wish them best of luck, but to say that they're "building on top of 50 years of experience" is implying that the software they generate is as good as software that has been battle tested for decades on numerous architectures.
> I don't think it's fair to blame the issues that code written in C can have on incompetence.
It's not a blaming game. Code written in C has issues because C has issues, but are you then saying that Rust has no issues? Will never have any issues? What's the bar here?
Great! So we agree that no person should have any business writing C. It's proven time and time again that even the best of the best C developers can't write safe C code.
If you want robust and well known, pick ada/spark. That language and it's culture is all about security and has been for decades. It also doesn't have quite the performance of c and rust.
And about the sharp knife... I have worked embedded c++ for decades now, some safety critical where we inspected the assembly generated and verified 100% code and branch coverage, on the assembly. I have made one memory safety error that escaped test in those years. That has been found yet...
I'm not in the kernel culture so I don't know. But it doesn't seem like it values things like static analysis, unit test coverage, san builds etc. It seems from the outside they are simply relying on their knife skills instead of measuring the quality of their work. Microsoft and Google both say over 70% of their exploits have come from memory safety errors. So it isnt M-1 + x. It is M0.3 + X. And rust isn't super new, so we have some ideas about X from early production use in firefox and dropbox. And we have an idea about how many knife users never cut themselves given even just the recent history like beacown.
It is time to start the incremental change.
std::string_view make_view(const std::string& in)
{
return in;
}
Is an absolute minefield, as is: std::vector<int> vec = {1, 2};
std::span<int> sp = vec;
vec.push-back(3);
These are using safe, modern C++ techniques that are designed to help catch errors, but are still absolutely lethal.Basically all the ways that ranges::view can go wrong and even trigger UB.
Finally, the address sanitizer helps you with that, however I wonder how many actually do use sanitizers?
Overall, I agree that something could have been done to prevent temporary objects at all. From my newbie point over view, it seems the standard API has the tendency to be lower level rather than ergonomic: it's up to you to know how to use it (and fuck up with it). Quite sad, because that kind of makes the language more difficult to use than it should be. I don't have enough knowledge, and I wonder if making the constructor on (const std::string&&) private/deleted would have been enough to prevent it?
The second case I guess the principle is similar. Not sure if the sanitizer finds the error there, but still scary.
and we have yet to see tangible benefits of using Rust
Is that the royal we? Have you actually (successfully) tried writing something in it? I am surprised Linus agreed to this given that he doesn’t like C++
Maybe Linus has lost the plot after decades of careful stewardship. Or maybe it is an opportune moment for a closer second look. Who knows.Sometimes that is what I try to do when my expectations are subverted if I care enough about the subject.
Right on point that kernel style C is its own unique world, with a steep learning curve, so lets add Rust with its own steep learning curve. Rust readability isn't any better than kernel C, guess I'm missing the 10,000 hours of reading Rust.
./KISS
Rust is relatively easy to read I think. The borrow checker is what gets all the hate, but that is an issue for writing, not reading. If anything, things like pattern matching make otherwise long if chains easier to read IMO
> we have yet to see tangible benefits of using Rust
Rust eradicates whole classes of errors in safe code, and unsafe code is more safe than the equivalent C or C++. The only theoretical way to not have a tangible reduction in errors would be to believe that a C programmer is good enough to never commit the error classes Rust eradicates, and believe Rust usage would increase the number of logic errors. This seems incredibly unlikely to me.
I write C++, Kotlin and Go code day to day, and I respectfully disagree. the syntax is incredibly terse. Most rust code I've encountered sprinkles macros with functions, which have slightly different semantics (note this is not an indictment of C or C++'s macros, far from it), and the combination of macros, pattern matching and unwrap lead to code like [0], which frankly I find incredibly difficult to work with.
> Rust eradicates whole classes of errors in safe code, and unsafe code is more safe than the equivalent C or C++.
Completely agree here, and nopt only does it do that it does so _without sacrificing performance_ in many cases.
[0] https://github.com/mozilla/sccache/blob/main/src/compiler/gc...
need_explicit_dep_target = true;
if let DepArgumentRequirePath::NotNeeded = need_explicit_dep_argument_path {
need_explicit_dep_argument_path = DepArgumentRequirePath::Missing;
}
I am currently trying to translate "simple" Rust code to C but I am having difficulties.https://github.com/RustCrypto/formats/blob/master/base32ct/s...
Where is `threshold` and `offset` coming from? How come running `cargo test` in directory `base32ct` actually tests nothing (I get 0 everywhere) despite there being some test code? Messed up `Cargo.toml` file? sighs
Lots of mysteries in Rust, at least for me.
let my_option = Some("test");
if let Some(my_str) = my_option {
println!("I matched this string: {my_str}");
}My take however is that C++ is inherently a complex language due to deliberately not sacrificing backwards-compatability of the syntax and semantics, which has been creating a mess as the language has evolved: there are many, many things to define semantically in order to "fit" a new piece in this old already-elaborate puzzle that is the C++ standard.
You cannot say the same thing about Rust.
Please correct me if you see things differently.
At least not yet. My fear is it's _very_ easy to fall in to the same trap as C++ has, and become a big mess. Scott Meyers have a very nice talk here: https://www.youtube.com/watch?v=KAWA1DuvCnQ about all these traps and pifalls that we have to deal with in C++.
That said, I'm starting a bit with rust and enjoy it so a lot, especially the tooling alone is worth it - and if I never have to write another line of C++, I would be very happy (ofc the real world argues I have a lot of C++ code that will need maintenance).
Some devs thrive on that, but most people can’t and shouldn’t have to. And you can see that contrary to what some in the Rust community are saying, there’s a push to use Rust for web. Bonkers.
When you boil code down to its bare essentials it is moving stuff from one part of memory with a possible transform to another. C does that exceedingly well and gets out of the way. However, the heavy price we pay for that is low type/bounds checking. Pretty much all other languages have tried to get the 'get out the way of part' but with type checking. Rust has taken the approach of you ask for memory and it is tracked at compile time. Not a bad idea. But it seems to trip some people up. Especially if you do not 'get' the idea of object/memory ownership. Which is something you have to do manually in C/C++. C++ has started to put wrappers around all this junk which helps.
And the rust ORM libraries are catching up with other languages so there is no additional advantage from picking a gc collected language.
Tell that to Asahi Lina: https://twitter.com/LinaAsahi/status/1577667445719912450
Have you actually tried Rust or are you just copying talking points?
And none of the existing DRM drivers are likely to be rewritten in Rust.
In the medium term, maybe some in-kernel dependencies might end up getting rewritten in Rust, or maybe some brand-new architecture may have a greenfield development in Rust.
I'm going to speculate that FreeBSD on commodity graphics hardware is safe enough for a few years, but that longer term Rust compatibility might have to be added to the Linux KPI.
This is fine for many use cases, but not for the Linux Kernel IMO.
correct
> unstable
incorrect
> There are lots of features you need the nightly compiler for
inaccurate
There are lots of features that are currently nightly-only. you may or may not need these depending on what you are doing. In particular kernel development does require quite a few nightly-only features, but there is no reason to think that all of these won't make it into stable rust in due course.
> and there are large breaking changes with every edition every few years
totally incorrect
And yes, presumably at some stage there will be a migration of the kernel from nightly to stable. There may be breaking changes. This will be a one time thing, and, (based on the current difference between nightly and stable and rate of change) not much more of a hurdle than changing C editions or gcc versions.
See my other comment.
Features land in all editions unless they specifically would be backwards incompatible otherwise.
...but then you go on to agree?
Yes, you can stay on an old edition forever and not experience any of the breaking changes. But there are features that the kernel needs that rust does not support yet, such as a fallible allocation API. These features will trickle in over the next few years. In order to use them, all of the kernel's rust code will have to jump versions and deal with the breaking changes.
https://lwn.net/Articles/885941/
https://lwn.net/Articles/855095/
This happens rarely and is not considered especially burdensome. Currently it is believed that Rust editions would be handled similarly and be no more burdensome.
New language features may require a new edition, though the vast majority of them don't - here's the list of all the changes in Edition 2021 (a new one is released every 3 years): https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
My understanding is that it might not be entirely settled yet, and is still being discussed. That could explain why there isn't much solid documentation written yet
Isn't that already the case? Rust now is very different from Rust in the early days and is also exponentially more unintuitive. Maybe that's fine, I don't know, but I'm personally very much struggling with its quirks.
Rust pre 1.0 kinda doesn't count.
The whole point of 1.0 is that there is a guarantee that there will be no more dramatic changes to the language.
> exponentially more unintuitive
Almost nothing about any programming language is really intuitive.
You mean that C uses paradigms that are intuitive to you due to your experience with it or similar languages.
Rust uses different paradigms that are more immediately intuitive to people with experience in functional languages. And the borrow checker. Which is probably immediately intuitive to nobody, but worth it.