From: Matthew Maurer <mmaurer@google.com>
To: Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Masahiro Yamada <masahiroy@kernel.org>,
Miguel Ojeda <ojeda@kernel.org>,
Alex Gaynor <alex.gaynor@gmail.com>,
Wedson Almeida Filho <wedsonaf@gmail.com>,
Nathan Chancellor <nathan@kernel.org>
Cc: "Matthew Maurer" <mmaurer@google.com>,
"Alexander Potapenko" <glider@google.com>,
"Andrey Konovalov" <andreyknvl@gmail.com>,
"Dmitry Vyukov" <dvyukov@google.com>,
"Vincenzo Frascino" <vincenzo.frascino@arm.com>,
"Nicolas Schier" <nicolas@fjasle.eu>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Nick Desaulniers" <ndesaulniers@google.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>,
kasan-dev@googlegroups.com, linux-kbuild@vger.kernel.org,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
llvm@lists.linux.dev
Subject: [PATCH 2/2] kbuild: rust: Enable KASAN support
Date: Thu, 25 Jul 2024 23:20:47 +0000 [thread overview]
Message-ID: <20240725232126.1996981-3-mmaurer@google.com> (raw)
In-Reply-To: <20240725232126.1996981-1-mmaurer@google.com>
Rust supports KASAN via LLVM, but prior to this patch, the flags aren't
set properly.
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
scripts/Makefile.kasan | 46 +++++++++++++++++++++++++++++++++++++++++-
scripts/Makefile.lib | 3 +++
2 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 390658a2d5b7..84572c473e23 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -12,6 +12,7 @@ endif
KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
+rustc-param = $(call rustc-option, -Cllvm-args=-$(1),)
ifdef CONFIG_KASAN_STACK
stack_enable := 1
@@ -28,6 +29,7 @@ else
endif
CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
+RUSTFLAGS_KASAN_MINIMAL := -Zsanitizer=kernel-address -Zsanitizer-recover=kernel-address
# -fasan-shadow-offset fails without -fsanitize
CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
@@ -36,13 +38,36 @@ CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
+ KASAN_SHADOW_SUPPORTED := n
+else
+ KASAN_SHADOW_SUPPORTED := y
+endif
+
+ifdef CONFIG_RUST
+ RUSTFLAGS_KASAN_SHADOW := $(call rustc-option $(RUSTFLAGS_KASAN_MINIMAL) \
+ -Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET))
+ ifeq ($(strip $(RUSTFLAGS_KASAN_SHADOW)),)
+ KASAN_SHADOW_SUPPORTED := n
+ endif
+endif
+
+ifeq ($(KASAN_SHADOW_SUPPORTED),y)
CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
+ ifdef CONFIG_RUST
+ RUSTFLAGS_KASAN := $(RUSTFLAGS_KASAN_MINIMAL)
+ endif
else
# 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)
+ endif
endif
CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
@@ -52,6 +77,11 @@ CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
+ifdef CONFIG_RUST
+ RUSTFLAGS_KASAN += $(call rustc-param,asan-stack=$(stack_enable))
+ RUSTFLAGS_KASAN += $(call rustc-param,asan-kernel-mem-intrinsic-prefix=1)
+endif
+
endif # CONFIG_KASAN_GENERIC
ifdef CONFIG_KASAN_SW_TAGS
@@ -73,6 +103,20 @@ ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
endif
+ifdef CONFIG_RUST
+ ifdef CONFIG_KASAN_INLINE
+ rust_instrumentation_flags := $(call rustc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
+ else
+ rust_instrumentation_flags := $(call rustc-param,hwasan-instrument-with-calls=1)
+ endif
+ RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress -Zsanitizer-recover=kernel-hwaddress \
+ $(call rustc-param,hwasan-instrument-stack=$(stack_enable)) \
+ $(call rustc-param,hwasan-use-short-granules=0) \
+ $(call rustc-param,hwasan-inline-all-checks=0) \
+ $(call rustc-param,hwasan-kernel-mem-intrinsic-prefix=1) \
+ $(instrumentation_flags)
+endif
+
endif # CONFIG_KASAN_SW_TAGS
-export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
+export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 9f06f6aaf7fc..4a58636705e0 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -167,6 +167,9 @@ ifneq ($(CONFIG_KASAN_HW_TAGS),y)
_c_flags += $(if $(patsubst n%,, \
$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
+_rust_flags += $(if $(patsubst n%,, \
+ $(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
+ $(RUSTFLAGS_KASAN))
endif
endif
--
2.46.0.rc1.232.g9752f9e123-goog
next prev parent reply other threads:[~2024-07-25 23:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240725232126.1996981-1-mmaurer@google.com>
2024-07-25 23:20 ` [PATCH 1/2] kbuild: rust: Define probing macros for rustc Matthew Maurer
2024-07-25 23:20 ` Matthew Maurer [this message]
2024-07-25 23:57 ` [PATCH 2/2] kbuild: rust: Enable KASAN support Andrey Konovalov
2024-07-26 10:23 ` Dmitry Vyukov
2024-07-26 12:36 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240725232126.1996981-3-mmaurer@google.com \
--to=mmaurer@google.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=andreyknvl@gmail.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dvyukov@google.com \
--cc=gary@garyguo.net \
--cc=glider@google.com \
--cc=justinstitt@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=masahiroy@kernel.org \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nicolas@fjasle.eu \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=ryabinin.a.a@gmail.com \
--cc=vincenzo.frascino@arm.com \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox