Skip to content

Commit 0a8784a

Browse files
ojedaintel-lab-lkp
authored andcommitted
rust: support the new -Zub-checks flag
Rust 1.79.0 has introduced a new codegen flag, `-Zub-checks` [1], to allow to independently configure (from `-Cdebug-assertions`) whether the extra runtime checks for UB are emitted, in a similar fashion to `-Coverflow-checks`. This allows to configure the kernel with only the UB checks enabled, but not the `debug_assert!`s; or vice versa, e.g. [2]. It also showcases how `RUSTC_VERSION` and the Kbuild macros, introduced in the previous commit, can be used. Link: rust-lang/compiler-team#725 [1] Link: https://godbolt.org/z/jY69ezx5K [2] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 5c46d94 commit 0a8784a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,15 @@ KBUILD_CFLAGS += -Os
820820
KBUILD_RUSTFLAGS += -Copt-level=s
821821
endif
822822

823-
# Always set `debug-assertions` and `overflow-checks` because their default
824-
# depends on `opt-level` and `debug-assertions`, respectively.
823+
# Always set `debug-assertions` because its default depends on `opt-level`.
825824
KBUILD_RUSTFLAGS += -Cdebug-assertions=$(if $(CONFIG_RUST_DEBUG_ASSERTIONS),y,n)
825+
826+
# Always set `overflow-checks` and `ub-checks` because their default depends on
827+
# `debug-assertions`.
826828
KBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
829+
ifeq ($(call rustc-min-version, 107900),y)
830+
KBUILD_RUSTFLAGS += -Zub-checks=$(if $(CONFIG_RUST_UNDEFINED_BEHAVIOR_CHECKS),y,n)
831+
endif
827832

828833
# Tell gcc to never replace conditional load with a non-conditional one
829834
ifdef CONFIG_CC_IS_GCC

lib/Kconfig.debug

+18
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,24 @@ config RUST_OVERFLOW_CHECKS
30203020

30213021
If unsure, say Y.
30223022

3023+
config RUST_UNDEFINED_BEHAVIOR_CHECKS
3024+
bool "Undefined Behavior checks"
3025+
depends on RUST && RUSTC_VERSION >= 107900
3026+
help
3027+
Enables rustc's `-Zub-checks` codegen option.
3028+
3029+
This flag allows you to control whether additional runtime checks that
3030+
detect some causes of Undefined Behavior at runtime will be emitted.
3031+
When enabled, a Rust panic will occur if UB is detected.
3032+
3033+
All checks are generated on a best-effort basis; even if there is a check
3034+
implemented for some cause of Undefined Behavior, it may be possible for
3035+
the check to not fire.
3036+
3037+
Note that this will apply to all Rust code, including `core`.
3038+
3039+
If unsure, say N.
3040+
30233041
config RUST_BUILD_ASSERT_ALLOW
30243042
bool "Allow unoptimized build-time assertions"
30253043
depends on RUST

0 commit comments

Comments
 (0)