r/rust • u/AcridWings_11465 • May 18 '22
Things Are Getting Rusty In Kernel Land (Hackaday)
https://hackaday.com/2022/05/17/things-are-getting-rusty-in-kernel-land/169
u/ondono May 18 '22
As someone working on embedded (and consulting to boot), I’ve had my fill of programmers claiming all those “writing good C is not that hard”, “that’s why we have code reviews”, etc… that you see on the comment section.
Nowadays I just give them a simple set of 10 questions with code snippets and ask them to give me the output of each program (on paper).
Unless your whole team of programmers can get all the questions right, I don’t even want to hear the excuses. So far I’ve yet to see a team that can do it, and it’s embarrassing to see how little some “expert programmers” know about the only language they know.
41
u/Caleb666 May 18 '22
“that’s why we have code reviews”
In my last job, I was the one who had to introduce C into a Golang codebase because I wrote a very low-level feature. Let me tell you, everyone dreaded doing code reviews for the C bits, including me. You had to be super duper careful....
38
u/aoeudhtns May 18 '22
To me these arguments are like "we don't need seat belts, air bags, or crumple zones. Just drive carefully."
Human meat computer in our skulls is gonna do its thing. My favorite anecdote is about the time Josh Bloch introduced in infinite recursion/stack overflow crash into the Java String class. And this is a guy that has written "the book" on Java.
Was once in a similar position to what you did. We actually considered using
nimbecause it can output C-style shared libraries (and interop trivially), but ultimately we just stuck with C and white gloved the whole component.29
u/Lucretiel Datadog May 18 '22
This is definitely it. I think it's easy to forget how much C gives you the power to screw up even relative to a hypothetical "idealized" C like language.
Like, sure, let's suppose that we wanted a language without implicit control flow (destructors, exceptions), with memory allocation & uninitialized memory & raw pointers, etc. You still don't have to land on shit like
strtok,gets; you don't have to have arrays just forget how long they are (requiring you to have pointer-length pairs everywhere), etc. You don't have to omit visibility controls or namespaces. You don't have to have (/me shudders) header files, or the insanity that is trying to parse it.There's so much space between the language that I feel like C advocates are talking about and the language that C actually is.
68
u/Gastredner May 18 '22
I'd be really interested to see these questions and their answers! Any chance you could share?
46
u/tanishaj May 18 '22
I would love to see them but there is no way I would share them if I was him. Forever more he would have to wonder if teams have a deep knowledge of C or just enough free time to cruise Reddit.
He would have to come up with entirely new questions.
16
May 18 '22
I'm sure that the answers aren't as simple as that. It may need deep knowledge of idiosyncrasies of the language. Honestly, they should release the questions with reasoning for the answer. If anybody uses it to answer them in a real interview, then that individual has put in effort to improve their knowledge and skills, even if from reddit. They may be worth hiring.
4
4
15
u/Thick-Pineapple666 May 18 '22
I've seen such questions for C++ and in 80% of the cases the code snippets are of the kind "I would never let that code go through code review", and 20% might be about legitimate knowledge you need to have in daily survival with the language..
16
u/WafflesAreDangerous May 18 '22
Can you look at a foreach loop and tell if the iterable lives until the end of the iteration or if it's a temporary and gets dropped early?
It's possible to construct very innocent looking code that is UB and needs extra information about the implementation of some method or function to decide.
2
u/Thick-Pineapple666 May 18 '22
That's very true. I hope the mentioned code snippets do not contain blackbox functions.
3
u/CornedBee May 19 '22
But especially in code review, you often get code snippets that call some function that already exists, somewhere, in that huge codebase. The review doesn't contain the code. Are you really going to look that function up just to see what it returns?
5
u/pjmlp May 19 '22
You know why Java lacks unsigned types (although Java 8 introduced unsigned arithmetic helper methods)?
Gosling asked around at Sun and no one got the unsigned arithmetic questions right.
https://www.artima.com/articles/james-gosling-on-java-may-2001
2
u/CornedBee May 19 '22
Weird, the rules of unsigned arithmetic are pretty trivial AFAIR. It's the signed arithmetic that you have to watch out for - and of course, the rules for integral promotion and mixed arithmetic. That's the really bad part. And that's solved by not doing that.
1
u/Zde-G May 21 '22
It's not about unsigned arithmetic. It's about how
unsignedworks in C. What would happen if you multiply 2147483648U by 2147483647? Is the result signed or unsigned? Is it defined behavior or undefined?There are a bazillion weird rules in C which govern that and people tend to forget about them and misinterpret them (add the fact that there mix of implementation-defined behaviors, undefined behaviors and different types sizes… lots of fun).
Rust has one simple rule which works surprisingly well: don't do that! Period. If you have to mix these — do explicit conversion. This works fabulously.
Java solved that in creating an unholy mess: it still have unsigned type (
char), but for other things there are two shifts (>>and>>>), and Java8 helper methods.I like Rust solution much better, if you haven't guessed yet.
1
u/James20k May 20 '22
Every non trivial C (or C++) program that has any level of public testing has been repeatedly shown to be an infinite source of security vulnerabilities. I have never once seen a public project that's widely used that can claim to be secure in concrete terms. They're all a bottomless pit of security vulns, its a well that never runs dry
So I don't know how the people claiming you can just write secure code in C can make that claim with a straight face. I've been writing C++ for 10 years and its just.. obviously not been true. Its never been true in any environment I've worked in, no matter the level of competence
Deep down, humans are humans and we make mistakes. There's no solving that by trying to force people to be better humans
2
u/Zde-G May 21 '22
Every non trivial C (or C++) program that has any level of public testing has been repeatedly shown to be an infinite source of security vulnerabilities. I have never once seen a public project that's widely used that can claim to be secure in concrete terms. They're all a bottomless pit of security vulns, its a well that never runs dry
Have you looked on qmail? It contains either a couple of security vulnerabilities or none (depending on who would you ask) and it was very extensively tested, reviewed (and, at one time, used) codebase.
But there are only one D. J. Bernstein (nobody knows where to find dozen such guys and it's not clear if they would still be able to keep that track record if they would be found and would work together) and even he admits that there are bugs (the question is whether they are exploitable or not is what's in doubt, the fact that they are there is not disputed).
1
u/ssokolow May 20 '22
I have never once seen a public project that's widely used that can claim to be secure in concrete terms. They're all a bottomless pit of security vulns, its a well that never runs dry
You're reminding me of this quote:
Honestly, after more than 25 years of C (and C++), I’ve become very frustrated with the average C code I seen in the wild. OpenSSL is fairly typical, in a lot of ways. So much C code has buffer overflows, numeric overflows, memory leaks, double frees, undefined behavior, and an an endless number of bugs. There are exceptions—djb’s code is quite good, dovecot seems reasonable, OpenBSD audits aggressively—but when I dive into most C code, I expect problems… I’m tired. I don’t want to rely on programmers practicing constant, flawless vigilance.
-- emk @ https://www.reddit.com/r/rust/comments/7na2xr/introducing_chttp_or_why_pure_rust_is_not_a/ds0u68p/
Its never been true in any environment I've worked in, no matter the level of competence
...and What science can tell us about C and C++'s security by Alex Gaynor.
178
u/timClicks rust in action May 18 '22
there’s a very good chance we’ll see Rust added to the Linux kernel in about three weeks!
Wow. I honestly thought that it wouldn't get this far. Well done for everyone who has put in so much effort over many years.
65
u/rebootyourbrainstem May 18 '22
It's by no means certain that it will be pulled for the next window, and if it causes anyone any trouble it'll likely be backed out super quick.
Also, as far as I understand it, getting merged would not be a definitive acceptance in this case. More of an extended trial to see how it goes, in recognition of the fact that it's hard to tell how things are going while it remains out of tree.
5
May 18 '22
Getting merged does not mean that Rust is required to build a regular configuration of the kernel
10
u/timClicks rust in action May 19 '22
No but it's a huge signal. It shifts the likelihood from possible to probable.
29
u/Sir_Omnomnom May 18 '22
There's a very interesting note in patch series v6 about bringing async support to the kernel. It's really cool to see the flexibility provided by the approach rust took with async being validated.
The beginning of
asyncsupport (kasyncmodule).Rust provides support for asynchronous programming in a way that can be used in constrained environments, including the kernel.
For instance, this allows us to write asynchronous TCP socket code within the kernel such as:
async fn echo_server(stream: TcpStream) -> Result { let mut buf = [0u8; 1024]; loop { let n = stream.read(&mut buf).await?; if n == 0 { return Ok(()); } stream.write_all(&buf[..n]).await?; } }This code looks very close to a synchronous version, yet it supports being driven to completion "step by step" by an executor. The
read()/write_all()calls above, instead of blocking the current thread, return a future which can be polled. The.awaitpoints poll the future and, if the result is not ready, suspend the state such that execution resumes there later on (the state machine needed for this gets implemented by the compiler). This allows an executor to drive multiple futures to completion concurrently on the same thread.An executor is not included yet, but
kasyncincludes async versions ofTcpListenerandTcpStream(based on the non-async ones) which employSocketFuture(which in turn uses astruct wait_queue_entry).
30
u/Voigt_K May 18 '22
Very interesting to see that coreutils has a rust rewriting project in development :)
0
May 18 '22
[deleted]
20
May 18 '22
Yea, it's unrelated to GNU but tries to re-implement all the GNU coreutils. Also it is under MIT license, and some of the tools is working on Windows.
22
u/nacaclanga May 18 '22
Using the Rust
stdlib rather them POSIX directly when possible is probably the big win here. This should make the tools more portible by default.15
1
1
u/rusty-roquefort May 20 '22
it’s impossible to write a kernel that is formally memory-safe.
seL4 would like a word with the author...
-100
u/cmplrs May 18 '22
We should focus on new operating systems, linux (kernel) is quite limited in what it can do with modern hardware.
86
u/AcridWings_11465 May 18 '22
quite limited in what it can do with modern hardware.
Is that why all of the top 500 supercomputers run linux?
21
u/lenscas May 18 '22
super computers are obviously made out of hardware from the future rather than modern hardware. Linux is not limited on this futuristic hardware so that is not a problem.
(I.. don't need a /s to make it clear that I am joking, right?)
1
67
u/ssokolow May 18 '22
Who's "we"?
You're free to work on what you want, and the people who build Linux-based solutions are free to do what they want.
21
u/Theemuts jlrs May 18 '22
11
u/ssokolow May 18 '22
I suppose I could do it... if you want a finger-painting toddler maintaining your vehicle's brakes.
I'm the Python developer who came to Rust for stronger compile-time guarantees and who uses
cargo-geigerto avoid dependencies containing non-FFIunsafewhere possible because I'm just responsible enough to recognize that I'm not qualified to audit my dependencies in that situation.10
u/seamsay May 18 '22
No, you have to be involved too. Specifically you.
4
u/ssokolow May 18 '22
I'm not even sure how to parse that.
[insert person name here] is free to do what they want, where that variable can be substituted with you, me, cmplrs, the kernel devs, or anyone else you want.
Personally, I completely and utterly lack the skills to do kernel development in any language, Rust, C, or otherwise.
5
10
9
u/diegovsky_pvp May 18 '22
could you give me an example of a new operating system?
3
u/toboRcinaM May 18 '22
I mean, there's RedoxOS, that's completely based on Rust, but it's probably not going to replace Linux any time soon
1
4
u/tanishaj May 18 '22 edited May 18 '22
People are working on new operating systems. In fact, they are working systems entirely in Rust.
My own view is that it will be a very long time until those systems reach the level of maturity, functionality, and ubiquity as Linux. It is not clear to me that it is any faster to replace Linux with a new system than it is to evolve Linux.
The good news is that both are happening. Time will tell.
2
u/Craksy May 18 '22
I personally know f all about OS dev but, like others in the comments, I'm curious what limitations you're talking about, and if there are any particular alternatives you think deserves more attention?
0
u/cmplrs May 18 '22
"Man, I really got whooped for this one, huh"
16
u/dontquestionmyaction May 18 '22
Yeah, because nobody understands what you're talking about. Tell us more.
147
u/ssokolow May 18 '22
Ouch. I hadn't realized Hackaday's comment-writing readership was that impulsive and uninformed.