Re: Can the Kernel Concurrency Sanitizer Own Rust Code?
flat view
From: Boqun Feng <hidden>
Date: 2021-10-07 14:16:13
Hi Marco, On Thu, Oct 07, 2021 at 03:01:07PM +0200, Marco Elver wrote:
Hi Paul, Thanks for writing up https://paulmck.livejournal.com/64970.html -- these were also my thoughts. Similarly for KASAN. Sanitizer integration will also, over time, provide quantitative data on the rate of bugs in C code, unsafe-Rust, and of course safe-Rust code as well as any number of interactions between them once the fuzzers are let loose on Rust code. Re integrating KCSAN with Rust, this should be doable since rustc does support ThreadSanitizer instrumentation: https://rustc-dev-guide.rust-lang.org/sanitizers.html Just need to pass all the rest of the -mllvm options to rustc as well, and ensure it's not attempting to link against compiler-rt. I haven't tried, so wouldn't know how it currently behaves.
Thanks for looking into this, and I think you're right: if rustc supports ThreadSanitizer, then basic features os KCSAN should work.
Also of importance will be the __tsan_atomic*() instrumentation, which KCSAN already provides: my guess is that whatever subset of the LKMM Rust initially provides (looking at the current version it certainly is the case), the backend will lower them to LLVM atomic intrinsics [1], which ThreadSanitizer instrumentation turns into __tsan_atomic*() calls. [1] https://llvm.org/docs/Atomics.html
Besides atomics, the counterpart of READ_ONCE() and WRITE_ONCE() should
also be looked into, IOW the core::ptr::{read,write}_volatile()
(although I don't think their semantics is completely defined since the
memory model of Rust is incomplete). There could easily be cases where
Rust-side do writes with lock critical sections while C-side do reads
out of the lock critical sections, so Rust-side need to play the
volatile game.
I'm not sure whether rustc will generate special instrumentation for
{read,write}_volatile(), if not, we need to provide something similar to
KCSAN does for READ_ONCE() and WRITE_ONCE().
Regards,
Boqun
Thanks, -- Marco