Re: [PATCH 2/2] kbuild: rust: Enable KASAN support

Dmitry Vyukov
2024-07-26T10:23:14Z

On Fri, 26 Jul 2024 at 01:57, Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> On Fri, Jul 26, 2024 at 1:21 AM Matthew Maurer <mmaurer@google.com> wrote:
> >
> > Rust supports KASAN via LLVM, but prior to this patch, the flags aren't
> > set properly.

This is great, thanks, Matthew!

Does Rust support KUnit tests?
It would be good to add at least a simple positive test similar to the
existing ones so that the support does not get rotten soon.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/kasan/kasan_test.c

> > Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> > Signed-off-by: Matthew Maurer <mmaurer@google.com>
>
> Hi Matthew,
>
> > CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
> > +RUSTFLAGS_KASAN_MINIMAL := -Zsanitizer=kernel-address -Zsanitizer-recover=kernel-address
>
> If I recall correctly, the reason we need CFLAGS_KASAN_MINIMAL is
> because older compilers don't support some of the additional options.
> With Rust, this shouldn't be needed, as it requires a modern compiler
> that does support all needed options. E.g., for CONFIG_KASAN_SW_TAGS,
> we also don't have the MINIMAL thing for the same reason. (Possibly,
> we also already don't need this for GENERIC KASAN, as the GCC version
> requirement was raised a few times since KASAN was introduced.)
>
> > # Now add all the compiler specific options that are valid standalone
> > CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
> > $(call cc-param,asan-globals=1) \
> > $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
> > $(call cc-param,asan-instrument-allocas=1)
> > + ifdef CONFIG_RUST
> > + RUSTFLAGS_KASAN := $(RUSTFLAGS_KASAN_SHADOW) \
> > + $(call rustc-param,asan-globals=1) \
> > + $(call rustc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
> > + $(call rustc-param,asan-instrument-allocas=1)
>
> I'm wondering if there's a way to avoid duplicating all options for
> Rust. Perhaps, some kind of macro?
>
> Thanks!