Re: Can the Kernel Concurrency Sanitizer Own Rust Code?
flat view
From: "Paul E. McKenney" <paulmck@kernel.org>
Date: 2021-10-08 17:40:50
On Fri, Oct 08, 2021 at 12:59:58AM +0100, Gary Guo wrote:
On Thu, 7 Oct 2021 16:42:47 -0700 "Paul E. McKenney" [off-list ref] wrote:quoted
quoted
I don't see why LTO is significant in the argument. Doing LTO or not wouldn't change the number of bugs. It could make a bug more or less visible, but buggy code remains buggy and bug-free code remains bug-free. If I have expose a safe `invoke_ub` function in a translation unit that internally causes UB using unsafe code, and have another all-safe-code crate calling it, then the whole program has UB regardless LTO is enabled or not.Here is the problem we face. The least buggy project I know of was a single-threaded safety-critical project that was subjected to stringent code-style constraints and heavy-duty formal verification. There was also a testing phase at the end of the validation process, but any failure detected by the test was considered to be a critical bug not only against the software under test, but also against the formal verification phase. The results were impressive, coming in at about 0.04 bugs per thousand lines of code (KLoC), that is, about one bug per 25,000 lines of code. But that is still way more than zero bugs. And I seriously doubt that Rust will be anywhere near this level. A more typical bug rate is about 1-3 bugs per KLoC. Suppose Rust geometrically splits the difference between the better end of typical experience (1 bug per KLoC) and that safety-critical project (again, 0.04 bugs per KLoC), that is to say 0.2 bugs per KLoC. (The arithmetic mean would give 0.52 bugs per KLoC, so I am being Rust-optimistic here.) In a project the size of the Linux kernel, that still works out to some thousands of bugs. So in the context of the Linux kernel, the propagation of bugs will still be important, even if the entire kernel were to be converted to Rust.There is a distinction between what is considered safe in Rust and what is considered safe in safety-critical systems. Miguel's LPC talk (https://youtu.be/ORwYx5_zmZo?t=1749) summarizes this really well. A large Rust program would no doubt contain bugs, but it is quite possible that it's UB-free.
The only purpose of my above wall of text was to assert that, as you said, "A large Rust program would no doubt contain bugs", so we are good on that point. Just in case there is lingering confusion, my purpose in providing an example from the field of safety-critical systems was nothing more or less than to derive an extreme lower bound for the expected bug rate in production software. Believe me, there is no way that I am advocating use of Rust as it currently exists for use in safety-critical systems! Not that this will necessarily prevent such use, mind you! ;-) OK, on to your point about UB-freedom. From what I have seen, people prevent unsafe Rust code from introducing UB by adding things, for example assertions and proofs of correctness. Each and every one of those added things have a non-zero probability of themselves containing bugs or mistakes. Therefore, a Rust program containing a sufficiently large quantity of unsafe code will with high probability invoke UB. Hopefully, a much lower UB-invocation probability than a similar quantity of C code, but nevertheless, a decidedly non-zero probability. So what am I missing here?
I should probably say that doing LTO or not wouldn't make a UB-free program exhibit UB (assuming LLVM doesn't introduce any during LTO).
I defer to comex's reply to this. Thanx, Paul