5 ms·
It is absolutely mind-boggling to me that anyone would start a new OS project today in C. Even if you're not a fan of Rust (which would be my choice), there ar
by kelnos 2y ago
It is absolutely mind-boggling to me that anyone would start a new OS project today in C. Even if you're not a fan of Rust (which would be my choice), there are other, safer, better languages to write an OS kernel in.
Even if you're going for a microkernel design, there are still consequences to writing memory-unsafe code outside the core.
- ranger_danger 2y agoI see no issues with it.
- ClassyJacket 2y agoI'm not knowledgeable in this low level systems stuff - which other, safer languages could you write a kernel in?
- kelnos 2y agoOh my, so many better options. D, Zig, Nim, to name a few. Even C++, for all its faults, would be a better choice than C!
- troad 2y agoI've been working full-time in Rust for several years, so I'm quite favourably disposed to Rust, but I don't think Rust would rank highly on my list of languages to start a kernel in. For me, the big advantages of Rust come from its assumptions about the memory model, and the rigorous checking it does to ensure compliance with those assumptions. But surely those assumptions cease to hold if you're the one defining the memory model, and most of your code involves shuffling memory around inside of `unsafe` blocks. What's the advantage of Rust at that point? Not to mention, if those guarantees break, Rust will panic and crash, which - in a kernel - would mean bringing down the entire system. In a kernel context, even things like 'let' might fail. Either you let the system panic, and have yourself a kernel that topples over in a slight breeze, or you check for and try to catch these errors, which - well - is no different from C at that point. Linus Torvalds has noted some of his issues with Rust in kernel dev, and he's in a pretty good place to render his opinion.[0][1][2] Imho, a better choice for a modern greenfield kernel would be one of the strictly functional languages: Haskell, OCaml, etc. [0] https://lkml.org/lkml/2022/9/19/1105 https://lkml.org/lkml/2022/9/19/1105 [1] https://lkml.org/lkml/2022/9/19/1250 https://lkml.org/lkml/2022/9/19/1250 [2] https://lkml.org/lkml/2022/9/19/1260 https://lkml.org/lkml/2022/9/19/1260
- kelnos 2y ago> But surely those assumptions cease to hold if you're the one defining the memory model, and most of your code involves shuffling memory around inside of `unsafe` blocks. True! But I think your characterization here of an OS kernel is incorrect. Certainly there will be more unsafe in an OS kernel than in your average user space application, but it should still be the minority of code. One thing that Rust documentation/tutorials frequently ram into your head in the chapters about unsafe is that you should be using unsafe only sparingly, to build small, auditable, hopefully-safe abstractions that the rest of your code -- safe code -- can use to get its work done. That's just as true in a kernel context (and perhaps even more critical to understand and internalize there) as in user space. > if those guarantees break, Rust will panic and crash, which - in a kernel - would mean bringing down the entire system [...] Linus Torvalds has noted some of his issues with Rust in kernel dev, and he's in a pretty good place to render his opinion. And this is why the team working on The Rust-in-Linux project have been working with the Rust maintainers to provide alternate APIs that don't panic, and instead return errors, in these situations. Torvalds' feedback has been instrumental in driving these changes. The fact that Rust is in the kernel now is a testament to the fact that this feedback has been taken to heart, and Rust has seen improvements because of it. > In a kernel context, even things like 'let' might fail No, that's absolutely false, and breathless statements like these feel a bit disingenuous. Even `let s = String::new()` cannot fail, as all it does is move a stack pointer. I think perhaps you intend to mean that the expression on the rhs of a let statement can sometimes fail? Sure, and that's just like any other statement or expression. But, again, this is why we now have a bunch of "try_"-prefixed variants of common memory-allocating things that return an error instead of panicking. > ... or you check for and try to catch these errors, which - well - is no different from C at that point That's not even a little bit true. C has no built-in error checking mechanisms, or even a way to build safe, ergonomic error checking. If I call kmalloc() from C, I have to explicitly check for NULL. I can very easily forget to do so, and the compiler won't help me. If I call a similar "try_alloc()"-style function from Rust, it will return a Result, and the compiler will not let me do something with that allocated memory without explicitly handling a possible error. > a better choice for a modern greenfield kernel would be one of the strictly functional languages: Haskell, OCaml, etc. I suppose it depends on what your goals are. If you want to build the next Linux (or something more modest, but still quite popular), then probably not; you're not going to find enough people who are competent in or even interested in languages Haskell or OCaml to build a strong contributor base. If you're instead doing research or are building something for niche use cases, then sure, that could work.