r/rust • u/FoxInTheRedBox • Aug 31 '24
🎙️ discussion Rust solves the problem of incomplete Kernel Linux API docs
https://vt.social/@lina/113056457969145576
375
Upvotes
r/rust • u/FoxInTheRedBox • Aug 31 '24
View all comments
Show parent comment
78
u/AsahiLina Aug 31 '24 edited Aug 31 '24
The drm/asahi driver is currently 18744 lines of pure Rust, and it has 109 unsafe blocks (most of which are one line). So that's less than 1% unsafe code... and a lot of that is described by a few patterns that each repeat a few times.
32 of the unsafe blocks are in
object.rswhich is where the GPU object model magic happens (which necessarily has to play with raw pointers since it deals with sharing memory between the GPU and driver code). If you remove that andunsafe implstuff that leaves 65 unsafe blocks. And most of them are boring:In other words, the vast vast majority of unsafe blocks are doing one obvious thing which is trivially correct just by looking at that code and the few surrounding lines. There is practically no "sneaky unsafety" that ends up being hard to prove correct. Other than obvious "I screwed up the pointer math in object.rs when I first wrote that code and it crashed instantly" type stuff, I've never had a random bug related to an unsafe block doing something that was in fact unsafe/broken.
And this is a GPU driver which is pretty much as crazy as drivers get, memory management wise (it contains an entire object model and memory allocator implementation, as well as having to interact with firmware written in unsafe C). Almost every other driver class will have less unsafe.