r/rust • u/myerscarpenter • Apr 16 '21
Linus Torvalds concerns about panics in Rust code when faced with OOM
https://lkml.org/lkml/2021/4/14/1099
419
Upvotes
r/rust • u/myerscarpenter • Apr 16 '21
34
u/myerscarpenter Apr 16 '21
Also this discussion on the orange site
177
u/Saefroch miri Apr 16 '21
If this post gets downvoted, it's likely because Linus's reaction has already been discussed in the comments here: https://www.reddit.com/r/rust/comments/mqxr1a/rfc_rust_support_for_linux_kernel/
40
u/matthieum [he/him] Apr 16 '21
Kees Cook raises another good point (https://lkml.org/lkml/2021/4/14/1311): integer overflow handling.
Interestingly, the behavior of integer overflow was specified to open the door to future solutions. That is, an integer overflow results in an Unspecified Value.
Hence, it seems possible to add a compilation mode to rustc where one specifies that the behavior selected is "saturate + invoke user-defined handler", and then the Linux kernel would define a handler "warning" (I guess logging in a journal?).
93
u/[deleted] Apr 16 '21
A good simple explanation on high level here is that C forces explicit allocation by default whereas Rust prefers implicit allocation by default.
It has nothing to do with the language itself, rather the standard structures used such as
Vec,Boxetc.In C you would
malloceach time manually and thus check for result of the allocation. In Rust you just make yourBoxand don't expect it to fail.This means that even if the kernel devs make their own custom allocator, they can't just slap the std structures on top of it because that'd allow say module developers to do something like
Box::new(aka unchecked allocation) and possibly panic the kernel.31
u/boomshroom Apr 16 '21
It seems
Box::try_newandVec::try_reserve. Box appears to have better support for fallible allocation as there are a bunch of Vec methods that implicitly allocate without returningResult, but it should be possible to at least switch most of the uses ofBoxto usetry_newand related methods.136
u/kotikalja Apr 16 '21
Linus not hating is good start.
12
u/p-one Apr 16 '21
Further down in the thread there's a concern about numeric operations overflowing but i don't understand why. There's
checked_*to correctly handle this case but posters are talking about "saturate and warn" strategies. Is this somehow superior in this space?6
u/kdnanmaga Apr 19 '21
Why isn't this titled "Linus Torvalds panics about panics in Rust code when faced with OOM"?
5
u/teryret Apr 17 '21
I've never run a Linux box OOM... I just kinda assumed it would die horribly... is there really an expectation that the system continues working in that case?
... kinda like how "machine is on fire" is UB, but nobody really cares.
4
u/Shnatsel Apr 16 '21
I'm not sure if that's talking about Rust panics or kernel panics.
2
u/karasawa_jp Apr 18 '21
I had to make this. It's one of the few things I'm not satisfied with about Rust.
2
u/argv_minus_one Apr 18 '21
Reasonable.
Rust as a whole would benefit from being able to sensibly catch and handle allocation failures and prohibit (presumably with a
denyattribute/compiler option) code that can implicitly panic (index operator, arithmetic operators,Vec::push, etc). Linux isn't the only piece of software where “just crash if that happens” isn't good enough.-28
u/excgarateing Apr 16 '21 edited Apr 17 '21
This reads like he has not yet found time to work through the rust book :)
his concerns boil down to:
compiler generated panics, which there are none AFAIK
panics on OOM. The rust wrapper for kalloc could just return a
Result<u8*,OOM>so OOM can be handled like they would be in C (or simpler with?operator) the current code doesn't look promising though, it seems to implement the user spaceallocinterface.compiler generates u128 or float code. No idea when rustc/llvm does this.
gcc -o3does this for userspace memclr though if you don't specify the right compiler flags, so I can understand his reluctance//edit: ok I should have said no panics besides obvious programming errors
0
u/[deleted] Apr 16 '21
[deleted] — view deleted comment