Skip to content

Commit ecaa6dd

Browse files
nbdd0121ojeda
authored andcommitted
rust: add build_error crate
The `build_error` crate provides a function `build_error` which will panic at compile-time if executed in const context and, by default, will cause a build error if not executed at compile time and the optimizer does not optimise away the call. The `CONFIG_RUST_BUILD_ASSERT_ALLOW` kernel option allows to relax the default build failure and convert it to a runtime check. If the runtime check fails, `panic!` will be called. Its functionality will be exposed to users as a couple macros in the `kernel` crate in the following patch, thus some documentation here refers to them for simplicity. Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Wei Liu <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent ef9e379 commit ecaa6dd

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

Diff for: lib/Kconfig.debug

+16
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,22 @@ config RUST_OVERFLOW_CHECKS
28012801

28022802
If unsure, say Y.
28032803

2804+
config RUST_BUILD_ASSERT_ALLOW
2805+
bool "Allow unoptimized build-time assertions"
2806+
depends on RUST
2807+
help
2808+
Controls how are `build_error!` and `build_assert!` handled during build.
2809+
2810+
If calls to them exist in the binary, it may indicate a violated invariant
2811+
or that the optimizer failed to verify the invariant during compilation.
2812+
2813+
This should not happen, thus by default the build is aborted. However,
2814+
as an escape hatch, you can choose Y here to ignore them during build
2815+
and let the check be carried at runtime (with `panic!` being called if
2816+
the check fails).
2817+
2818+
If unsure, say N.
2819+
28042820
endmenu # "Rust"
28052821

28062822
source "Documentation/Kconfig"

Diff for: rust/Makefile

+17-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
1919
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
2020
exports_kernel_generated.h
2121

22+
ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
23+
obj-$(CONFIG_RUST) += build_error.o
24+
else
25+
always-$(CONFIG_RUST) += build_error.o
26+
endif
27+
2228
obj-$(CONFIG_RUST) += exports.o
2329

2430
# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
@@ -108,7 +114,7 @@ rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
108114
$(call if_changed,rustdoc)
109115

110116
rustdoc-kernel: private rustc_target_flags = --extern alloc \
111-
--extern macros=$(objtree)/$(obj)/libmacros.so \
117+
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
112118
--extern bindings
113119
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
114120
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
@@ -126,6 +132,9 @@ quiet_cmd_rustc_test_library = RUSTC TL $<
126132
-L$(objtree)/$(obj)/test \
127133
--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
128134

135+
rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
136+
$(call if_changed,rustc_test_library)
137+
129138
rusttestlib-macros: private rustc_target_flags = --extern proc_macro
130139
rusttestlib-macros: private rustc_test_library_proc = yes
131140
rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
@@ -216,9 +225,9 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
216225
$(call if_changed,rustdoc_test)
217226

218227
rusttest-kernel: private rustc_target_flags = --extern alloc \
219-
--extern macros --extern bindings
228+
--extern build_error --extern macros --extern bindings
220229
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
221-
rusttestlib-macros rusttestlib-bindings FORCE
230+
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
222231
$(call if_changed,rustc_test)
223232
$(call if_changed,rustc_test_library)
224233

@@ -366,15 +375,18 @@ $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
366375
$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
367376
$(call if_changed_dep,rustc_library)
368377

378+
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
379+
$(call if_changed_dep,rustc_library)
380+
369381
$(obj)/bindings.o: $(src)/bindings/lib.rs \
370382
$(obj)/compiler_builtins.o \
371383
$(obj)/bindings/bindings_generated.rs \
372384
$(obj)/bindings/bindings_helpers_generated.rs FORCE
373385
$(call if_changed_dep,rustc_library)
374386

375387
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
376-
--extern macros --extern bindings
377-
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o \
388+
--extern build_error --extern macros --extern bindings
389+
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
378390
$(obj)/libmacros.so $(obj)/bindings.o FORCE
379391
$(call if_changed_dep,rustc_library)
380392

Diff for: rust/build_error.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Build-time error.
4+
//!
5+
//! This crate provides a [const function][const-functions] `build_error`, which will panic in
6+
//! compile-time if executed in [const context][const-context], and will cause a build error
7+
//! if not executed at compile time and the optimizer does not optimise away the call.
8+
//!
9+
//! It is used by `build_assert!` in the kernel crate, allowing checking of
10+
//! conditions that could be checked statically, but could not be enforced in
11+
//! Rust yet (e.g. perform some checks in [const functions][const-functions], but those
12+
//! functions could still be called in the runtime).
13+
//!
14+
//! For details on constant evaluation in Rust, please see the [Reference][const-eval].
15+
//!
16+
//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
17+
//! [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
18+
//! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
19+
20+
#![no_std]
21+
22+
/// Panics if executed in [const context][const-context], or triggers a build error if not.
23+
///
24+
/// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
25+
#[inline(never)]
26+
#[cold]
27+
#[export_name = "rust_build_error"]
28+
#[track_caller]
29+
pub const fn build_error(msg: &'static str) -> ! {
30+
panic!("{}", msg);
31+
}

Diff for: rust/exports.c

+5
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@
1919
#include "exports_alloc_generated.h"
2020
#include "exports_bindings_generated.h"
2121
#include "exports_kernel_generated.h"
22+
23+
// For modules using `rust/build_error.rs`.
24+
#ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
25+
EXPORT_SYMBOL_RUST_GPL(rust_build_error);
26+
#endif

Diff for: scripts/generate_rust_analyzer.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
6767
)
6868
crates[-1]["proc_macro_dylib_path"] = "rust/libmacros.so"
6969

70+
append_crate(
71+
"build_error",
72+
srctree / "rust" / "build_error.rs",
73+
["core", "compiler_builtins"],
74+
)
75+
7076
append_crate(
7177
"bindings",
7278
srctree / "rust"/ "bindings" / "lib.rs",
@@ -78,7 +84,7 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
7884
append_crate(
7985
"kernel",
8086
srctree / "rust" / "kernel" / "lib.rs",
81-
["core", "alloc", "macros", "bindings"],
87+
["core", "alloc", "macros", "build_error", "bindings"],
8288
cfg=cfg,
8389
)
8490
crates[-1]["source"] = {

0 commit comments

Comments
 (0)