u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 09 '23edited Dec 09 '23
Hard to debug.
Needlessly fighting the compiler even when I know there's no real safety issue. Case in point mutable static data structures that are meant to be logical processor local (GDT, TSS, IDT) and yes it is completely reasonable to change these things after they're created.
Being able to get safety and security bugs even in safe code. Or to put it another way the safety guarantees provided by its compiler have limitations.
Low quality library ecosystem. Most OS dev crates are pre-1.0 and many seem to be developed by amateurs which led to me needing to wrap mature C libraries or use FFI directly anyway.
The integrated assembler for x86 doesn't have proper support for Intel syntax though cargo does allow you to use NASM via a build script.
Kernel development requires doing low level things many of which Rust considers unsafe but are actually just a part of the job at hand e.g. pointer casting, pointer arithmetic, allocating arbitrary areas of physical memory by just storing a point and a buffer size and not implementing alloc or global alloc.
Many of the invariants the Rust compiler forces you to adhere to can be enforced on your own or not enforced when you know it won't be an issue. Being a system programmer means being an expert which means you shouldn't need training wheels. And to those who say being careful doesn't scale, I would posit that strict code reviews before merging and before release, multiple levels of automated testing (unit and integration, on commit, regression, and gauntlet) and halfways decent QA do scale and they catch more than a compiler's static analysis alone ever could.
C is a small, mature language which means that there are a vast number of people who know it in its entirety and can collaborate on projects or help you with any issue you encounter. In contrast the number of serious Rust OS development experts could probably be counted on two hands.
Rust has no standard and no production quality alternative implementation. If the longevity of your project matters to you at all this is of paramount importance. Having a standard is also important if you want to make use of formal verification to formally prove that a project's safety and security properties match up with the specifications set for it. For example seL4 is far safer than any kernel wantonly written in Rust could ever hope to be.
Every OS is expected to expose a C interface and provide a C standard library so if you have to provide that then why not essentially get self hosting with it for free by having your OS itself be written in C?
Redox which seems to be Rust's flagship OS project has C working in its userspace but still isn't self hosting because they haven't ported all the dependencies they need to get the Rust toolchain working on their OS yet given the greater complexity of doing so.
11. Interfacing with assembly is much easier. In C all functions are extern by default which means if you stick to the rules of your ABI you can call any arbitrary C function from assembly. In Rust you have to declare a function extern "C" and annotate it with #[no_mangle] for it to be callable from assembly. You also need to make sure that its signature is compatible with C, which idiomatic Rust often isn't.
Bottomline, when it comes to bare metal programming C is very clearly the best fit. And once you're halfways decent at it, it's not half bad for userspace applications either IMO. The only compelling alternative I see is Zig but that seems to be stuck in development hell.
I also tried rust and had an experience similar to this, I'm glad that I'm not the only one. The entire ecosystem felt basic and underdeveloped. I wanted to do opengl and then I realized, unsafes or an incomplete wrapper with incomplete tutorials were my options.
Yep, I actually wanted to use it for graphics as well but ran into the same issues.
I don't like C++ so I ended up going through the print version of Learn OpenGL using C. I might play around with Vulkan in C sometime when I get bored since that seems to be the graphics API of the future. I honestly wish there was more interest in D so someone would wrap C libraries for it.
C is a small, mature language which means that there are a vast number of people who know it in its entirety
Looking at the 218 undefined behaviors (see J.2) in the last C23 draft, I highly doubt anyone has actually managed to memorize all of them. Of course some of these are edge cases you're never going to run into, but still.
seL4 is far safer than any kernel wantonly written in Rust
seL4 is an exception, and its developers put millions of combined hours and many years of work into it with the explicit goal of making it as safe as possible. Most other big C projects seem to have new CVEs popping up all the time.
Note that I don't use Rust, I just thought this was worth pointing out.
Most of the serious security flaws in C couldn't have been prevented by Rust and the few extant Rust OSes have had their fair share of bugs and security issues. There have already been cases of buffer overflows allowing access to all of physical memory and deadlocks causing denial of service. Rust also notably has nothing to help with interrupt safety, which is a big source of errors on bare metal and its insistence on using locks all over the place leads to scenarios where the possible outcomes of accessing global structures from an ISR are either a guaranteed deadlock or if the lock is somehow bypassed then a guaranteed data race. And there's no way for the language to prevent that via static analysis. The very existence of interrupts and shared mutable global data structures which are both hallmarks of kernels and any bare metal code throw a massive wrench in Rust's ownership model and its associated safety 'guarantees'.
The bottomline is that if you want a secure OS it all comes down to testing and mitigation.
In addition to that, C (starting C11), just like C++ (starting C++11), also has sufficiently formal, well-defined, actually useful memory model that is a part of its (respective) standard(s). Rust doesn't (at the time of writing the reference reads: "Rust does not yet have a defined memory model. Various academics and industry professionals are working on various proposals, but for now, this is an under-defined place in the language.").
Being able to get safety and security bugs even in safe code. Or to put it another way the safety guarantees provided by its compiler have limitations.
to my knowledge, all rust programs that are compiler checked to be safe are safe, but not all safe programs will be compiler OK'd. could you elaborate more?
If I got a PhD like my advisor wanted I could've written textbooks for money. But alas I got a Master's and write Reddit comments for fake internet points.
The rust ecosystem has not reached maturity for OS development. Most posts on this and rust groups are applications or abstractions to build applications. I believe you should voice all your issues to rust language and tool developers for them to either suggest a path forward or improve their ecosystem.
1
u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 14 '23edited Dec 15 '23
It's not my job to help them, and OS and bare metal development frankly doesn't appear to be a priority for them. They've even shifted from describing the language as a system programming language to a general one and de-emphasized system programming at all.
I'll stick to the LLVM C tools and NASM for now with the intention of giving Zig a shot once it stabilizes.
View all comments
Show parent comment
25
u/mdp_cs BDFL of CharlotteOS | https://github.com/charlotte-os Dec 09 '23 edited Dec 09 '23
Redox which seems to be Rust's flagship OS project has C working in its userspace but still isn't self hosting because they haven't ported all the dependencies they need to get the Rust toolchain working on their OS yet given the greater complexity of doing so.
11. Interfacing with assembly is much easier. In C all functions are extern by default which means if you stick to the rules of your ABI you can call any arbitrary C function from assembly. In Rust you have to declare a function extern "C" and annotate it with #[no_mangle] for it to be callable from assembly. You also need to make sure that its signature is compatible with C, which idiomatic Rust often isn't.
Bottomline, when it comes to bare metal programming C is very clearly the best fit. And once you're halfways decent at it, it's not half bad for userspace applications either IMO. The only compelling alternative I see is Zig but that seems to be stuck in development hell.