3 ms·
The Rust for Linux implementation converts a Rust panic into a Linux kernel BUG macro call. I believe this will expand to an invalid CPU instruction (at least o
by tialaramex 4y ago
The Rust for Linux implementation converts a Rust panic into a Linux kernel BUG macro call. I believe this will expand to an invalid CPU instruction (at least on popular architectures), and if you're a kernel thread you die immediately with the kernel reporting the state where this happened. Obviously in some cases this is fatal and the report might only be seen via say, a serial port hooked up to a device in a test lab or whatever.
So, it's not a kernel panic, but it's extremely bad, which seems appropriate because your code is definitely wrong. If you're not sure whether the index is correct you can use get() or get_mut() to have an Option, which will be None if your index was wrong (or of course you could ask about the length of the array since Rust remembers how long arrays are).
- throwaway894345 4y agoWould a kernel module be written as a normal Linux-targeting Rust program, or would it be more like a bare metal target with its own (user-provided) panic handler?
- loeg 4y agoMore like the latter. Kernel modules don’t run in userspace.
- megous 4y agoBUG() will panic the kernel. https://elixir.bootlin.com/linux/latest/source/include/asm-generic/bug.h#L52 https://elixir.bootlin.com/linux/latest/source/include/asm-g... I guess that's quite drastic for a checked out of bound access, when there's no actual memory safety issue and the compiler can simply return an error from the function, or do something else less drastic.
- sealeck 4y agoIn which case you can call the get method (which returns an Option - i.e. either the value or null) rather than indexing and return an error value.
- couchand 4y agoIn Rust code, if you're not able to locally reason that an array index is valid, it should be written with .get() and the None case handled appropriately. It's impossible to claim there's "no actual memory safety issue" when a program's invariants have been broken: all bets are off at that point.
- megous 4y agoWhy allow indexed access at all if the compiler is emitting a conditional check anyway?
- vgel 4y agoSome people have argued that indexed access is a wart, but it would be quite heavyweight to always have to unwrap an option when accessing a known-good index: let foo = [0_u8, 1, 2]; foo[0].unwrap(); // really? Instead, indexing on arrays / vecs is (essentially) sugar for .get(index).unwrap(), if you don't want the unwrap behavior use get. This is very similar to Python, though Python throws an exception which obviously isn't available to Rust.
- couchand 4y agoBut in real code you never want unwrap, so why provide sugar for it?
- connicpu 4y agoBecause in the common case you assume that, if your code is correct, all of your indexing will be in bounds, but for memory safety reasons we need a bug to be reported if memory safety would have been violated. So we allow direct indexing with a panic on out of bounds because it's the most ergonomic for that common case
- megous 4y agoThe bug is not just reported here, the whole computer shuts down and all your unsaved work gets lost. That's not very ergonomic either.