Skip to content

Commit 8a46b49

Browse files
authored
Merge pull request #757 from ojeda/tests
Support running documentation tests in-kernel
2 parents f063324 + d5e044e commit 8a46b49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+637
-273
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ jobs:
266266
! grep -v 'at mm/debug_vm_pgtable.c:' qemu-stdout.log | grep '] WARNING:'
267267
268268
# Check
269+
- run: |
270+
grep '] ok 1 - rust_kernel_doctests$' qemu-stdout.log
271+
269272
- run: |
270273
grep '] rust_minimal: Rust minimal sample (init)$' qemu-stdout.log
271274
grep '] rust_minimal: Am I built-in? false$' qemu-stdout.log

.github/workflows/kernel-arm-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ CONFIG_UNCOMPRESS_INCLUDE="debug/uncompress.h"
18021802
#
18031803
# Kernel Testing and Coverage
18041804
#
1805-
# CONFIG_KUNIT is not set
1805+
CONFIG_KUNIT=y
18061806
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
18071807
# CONFIG_FAULT_INJECTION is not set
18081808
CONFIG_ARCH_HAS_KCOV=y
@@ -1827,6 +1827,7 @@ CONFIG_RUST_OPT_LEVEL_2=y
18271827
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
18281828
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
18291829
CONFIG_RUST_BUILD_ASSERT_DENY=y
1830+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
18301831
# end of Rust hacking
18311832
# end of Kernel hacking
18321833

.github/workflows/kernel-arm-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ CONFIG_UNCOMPRESS_INCLUDE="debug/uncompress.h"
17251725
#
17261726
# Kernel Testing and Coverage
17271727
#
1728-
# CONFIG_KUNIT is not set
1728+
CONFIG_KUNIT=y
17291729
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
17301730
# CONFIG_FAULT_INJECTION is not set
17311731
CONFIG_ARCH_HAS_KCOV=y
@@ -1785,6 +1785,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
17851785
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
17861786
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
17871787
CONFIG_RUST_BUILD_ASSERT_DENY=y
1788+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
17881789
# end of Rust hacking
17891790
# end of Kernel hacking
17901791

.github/workflows/kernel-arm64-debug-thinlto.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ CONFIG_PID_IN_CONTEXTIDR=y
14501450
#
14511451
# Kernel Testing and Coverage
14521452
#
1453-
# CONFIG_KUNIT is not set
1453+
CONFIG_KUNIT=y
14541454
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
14551455
CONFIG_FUNCTION_ERROR_INJECTION=y
14561456
# CONFIG_FAULT_INJECTION is not set
@@ -1476,6 +1476,7 @@ CONFIG_RUST_OPT_LEVEL_1=y
14761476
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14771477
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14781478
CONFIG_RUST_BUILD_ASSERT_DENY=y
1479+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14791480
# end of Rust hacking
14801481
# end of Kernel hacking
14811482

.github/workflows/kernel-arm64-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ CONFIG_PID_IN_CONTEXTIDR=y
14451445
#
14461446
# Kernel Testing and Coverage
14471447
#
1448-
# CONFIG_KUNIT is not set
1448+
CONFIG_KUNIT=y
14491449
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
14501450
CONFIG_FUNCTION_ERROR_INJECTION=y
14511451
# CONFIG_FAULT_INJECTION is not set
@@ -1471,6 +1471,7 @@ CONFIG_RUST_OPT_LEVEL_1=y
14711471
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14721472
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14731473
CONFIG_RUST_BUILD_ASSERT_DENY=y
1474+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14741475
# end of Rust hacking
14751476
# end of Kernel hacking
14761477

.github/workflows/kernel-arm64-release-thinlto.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
13681368
#
13691369
# Kernel Testing and Coverage
13701370
#
1371-
# CONFIG_KUNIT is not set
1371+
CONFIG_KUNIT=y
13721372
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13731373
# CONFIG_FAULT_INJECTION is not set
13741374
CONFIG_ARCH_HAS_KCOV=y
@@ -1393,6 +1393,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
13931393
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
13941394
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
13951395
CONFIG_RUST_BUILD_ASSERT_DENY=y
1396+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13961397
# end of Rust hacking
13971398
# end of Kernel hacking
13981399

.github/workflows/kernel-arm64-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
13631363
#
13641364
# Kernel Testing and Coverage
13651365
#
1366-
# CONFIG_KUNIT is not set
1366+
CONFIG_KUNIT=y
13671367
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13681368
# CONFIG_FAULT_INJECTION is not set
13691369
CONFIG_ARCH_HAS_KCOV=y
@@ -1388,6 +1388,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
13881388
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
13891389
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
13901390
CONFIG_RUST_BUILD_ASSERT_DENY=y
1391+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13911392
# end of Rust hacking
13921393
# end of Kernel hacking
13931394

.github/workflows/kernel-ppc64le-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
15921592
# CONFIG_RUST_OPT_LEVEL_Z is not set
15931593
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
15941594
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1595+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
15951596
# end of Rust hacking
15961597
# end of Kernel hacking
15971598

.github/workflows/kernel-ppc64le-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ CONFIG_PRINT_STACK_DEPTH=64
14731473
#
14741474
# Kernel Testing and Coverage
14751475
#
1476-
# CONFIG_KUNIT is not set
1476+
CONFIG_KUNIT=y
14771477
CONFIG_ARCH_HAS_KCOV=y
14781478
CONFIG_CC_HAS_SANCOV_TRACE_PC=y
14791479
# CONFIG_KCOV is not set
@@ -1496,6 +1496,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
14961496
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14971497
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14981498
CONFIG_RUST_BUILD_ASSERT_DENY=y
1499+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14991500
# end of Rust hacking
15001501
# end of Kernel hacking
15011502

.github/workflows/kernel-riscv64-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
12961296
#
12971297
# Kernel Testing and Coverage
12981298
#
1299-
# CONFIG_KUNIT is not set
1299+
CONFIG_KUNIT=y
13001300
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13011301
# CONFIG_FAULT_INJECTION is not set
13021302
CONFIG_ARCH_HAS_KCOV=y
@@ -1320,6 +1320,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
13201320
# CONFIG_RUST_OPT_LEVEL_Z is not set
13211321
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
13221322
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1323+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13231324
# end of Rust hacking
13241325
# end of Kernel hacking
13251326

.github/workflows/kernel-riscv64-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
12121212
#
12131213
# Kernel Testing and Coverage
12141214
#
1215-
# CONFIG_KUNIT is not set
1215+
CONFIG_KUNIT=y
12161216
CONFIG_ARCH_HAS_KCOV=y
12171217
CONFIG_CC_HAS_SANCOV_TRACE_PC=y
12181218
# CONFIG_KCOV is not set
@@ -1235,6 +1235,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
12351235
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
12361236
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
12371237
CONFIG_RUST_BUILD_ASSERT_DENY=y
1238+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
12381239
# end of Rust hacking
12391240
# end of Kernel hacking
12401241

.github/workflows/kernel-x86_64-debug-thinlto.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
15431543
# CONFIG_RUST_OPT_LEVEL_Z is not set
15441544
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
15451545
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1546+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
15461547
# end of Rust hacking
15471548
# end of Kernel hacking
15481549

.github/workflows/kernel-x86_64-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
15461546
# CONFIG_RUST_OPT_LEVEL_Z is not set
15471547
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
15481548
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1549+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
15491550
# end of Rust hacking
15501551
# end of Kernel hacking
15511552

.github/workflows/kernel-x86_64-release-thinlto.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ CONFIG_UNWINDER_ORC=y
14201420
#
14211421
# Kernel Testing and Coverage
14221422
#
1423-
# CONFIG_KUNIT is not set
1423+
CONFIG_KUNIT=y
14241424
CONFIG_ARCH_HAS_KCOV=y
14251425
CONFIG_CC_HAS_SANCOV_TRACE_PC=y
14261426
# CONFIG_KCOV is not set
@@ -1443,6 +1443,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
14431443
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14441444
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14451445
CONFIG_RUST_BUILD_ASSERT_DENY=y
1446+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14461447
# end of Rust hacking
14471448
# end of Kernel hacking
14481449

.github/workflows/kernel-x86_64-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ CONFIG_UNWINDER_ORC=y
14151415
#
14161416
# Kernel Testing and Coverage
14171417
#
1418-
# CONFIG_KUNIT is not set
1418+
CONFIG_KUNIT=y
14191419
CONFIG_ARCH_HAS_KCOV=y
14201420
CONFIG_CC_HAS_SANCOV_TRACE_PC=y
14211421
# CONFIG_KCOV is not set
@@ -1438,6 +1438,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
14381438
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14391439
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14401440
CONFIG_RUST_BUILD_ASSERT_DENY=y
1441+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14411442
# end of Rust hacking
14421443
# end of Kernel hacking
14431444

include/kunit/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
774774
void kunit_do_failed_assertion(struct kunit *test,
775775
const struct kunit_loc *loc,
776776
enum kunit_assert_type type,
777-
struct kunit_assert *assert,
777+
const struct kunit_assert *assert,
778778
const char *fmt, ...);
779779

780780
#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \

lib/Kconfig.debug

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,18 @@ config RUST_BUILD_ASSERT_DENY
29002900

29012901
endchoice
29022902

2903+
config RUST_KERNEL_KUNIT_TEST
2904+
bool "KUnit test for the `kernel` crate" if !KUNIT_ALL_TESTS
2905+
depends on RUST && KUNIT=y
2906+
default KUNIT_ALL_TESTS
2907+
help
2908+
This builds the documentation tests of the `kernel` crate
2909+
as KUnit tests.
2910+
2911+
For more information on KUnit and unit tests in general,
2912+
please refer to the KUnit documentation in Documentation/dev-tools/kunit/.
2913+
2914+
If unsure, say N.
29032915

29042916
endmenu # "Rust"
29052917

lib/kunit/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void kunit_print_string_stream(struct kunit *test,
241241
}
242242

243243
static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
244-
enum kunit_assert_type type, struct kunit_assert *assert,
244+
enum kunit_assert_type type, const struct kunit_assert *assert,
245245
const struct va_format *message)
246246
{
247247
struct string_stream *stream;
@@ -281,7 +281,7 @@ static void __noreturn kunit_abort(struct kunit *test)
281281
void kunit_do_failed_assertion(struct kunit *test,
282282
const struct kunit_loc *loc,
283283
enum kunit_assert_type type,
284-
struct kunit_assert *assert,
284+
const struct kunit_assert *assert,
285285
const char *fmt, ...)
286286
{
287287
va_list args;

rust/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ target.json
44
bindings_generated.rs
55
bindings_helpers_generated.rs
66
exports_*_generated.h
7+
doctests_kernel_generated.rs
8+
doctests_kernel_generated_kunit.c
79
doc/
810
test/

rust/Makefile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ endif
2626

2727
obj-$(CONFIG_RUST) += exports.o
2828

29+
obj-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated.o
30+
obj-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated_kunit.o
31+
2932
# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
3033
ifdef CONFIG_RUST
3134

@@ -38,9 +41,11 @@ ifeq ($(quiet),silent_)
3841
cargo_quiet=-q
3942
rust_test_quiet=-q
4043
rustdoc_test_quiet=--test-args -q
44+
rustdoc_test_kernel_quiet=>/dev/null
4145
else ifeq ($(quiet),quiet_)
4246
rust_test_quiet=-q
4347
rustdoc_test_quiet=--test-args -q
48+
rustdoc_test_kernel_quiet=>/dev/null
4449
else
4550
cargo_quiet=--verbose
4651
endif
@@ -139,6 +144,23 @@ quiet_cmd_rustdoc_test = RUSTDOC T $<
139144
-L$(objtree)/$(obj)/test --output $(objtree)/$(obj)/doc \
140145
--crate-name $(subst rusttest-,,$@) $<
141146

147+
quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
148+
cmd_rustdoc_test_kernel = \
149+
rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
150+
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
151+
OBJTREE=$(abspath $(objtree)) \
152+
$(RUSTDOC) --test $(rust_flags) \
153+
@$(objtree)/include/generated/rustc_cfg \
154+
-L$(objtree)/$(obj) --extern alloc --extern kernel \
155+
--extern build_error --extern macros \
156+
--no-run --crate-name kernel -Zunstable-options \
157+
--test-builder $(srctree)/scripts/rustdoc_test_builder.py \
158+
$< $(rustdoc_test_kernel_quiet); \
159+
$(srctree)/scripts/rustdoc_test_gen.py
160+
161+
%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: $(src)/kernel/lib.rs $(obj)/kernel.o FORCE
162+
$(call if_changed,rustdoc_test_kernel)
163+
142164
# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
143165
# so for the moment we skip `-Cpanic=abort`.
144166
quiet_cmd_rustc_test = RUSTC T $<
@@ -217,7 +239,6 @@ rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
217239
rusttestlib-build_error rusttestlib-macros FORCE
218240
$(call if_changed,rustc_test)
219241
$(call if_changed,rustc_test_library)
220-
$(call if_changed,rustdoc_test)
221242

222243
filechk_rust_target = $(objtree)/scripts/generate_rust_target < $<
223244

rust/bindgen_parameters

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
--opaque-type arch_lbr_state
66
--opaque-type local_apic
77

8+
# `try` is a reserved keyword since Rust 2018; solved in `bindgen` v0.59.2,
9+
# commit 2aed6b021680 ("context: Escape the try keyword properly").
10+
--opaque-type kunit_try_catch
11+
812
# If SMP is disabled, `arch_spinlock_t` is defined as a ZST which triggers a Rust
913
# warning. We don't need to peek into it anyway.
1014
--opaque-type spinlock

rust/kernel/amba.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ unsafe impl device::RawDevice for Device {
204204
/// # Examples
205205
///
206206
/// ```ignore
207-
/// # use kernel::prelude::*;
208207
/// # use kernel::{amba, define_amba_id_table, module_amba_driver};
209208
/// #
210209
/// struct MyDriver;
@@ -237,7 +236,6 @@ macro_rules! module_amba_driver {
237236
/// # Examples
238237
///
239238
/// ```
240-
/// # use kernel::prelude::*;
241239
/// # use kernel::{amba, define_amba_id_table};
242240
/// #
243241
/// # struct Sample;

rust/kernel/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <asm/io.h>
10+
#include <kunit/test.h>
1011
#include <linux/amba/bus.h>
1112
#include <linux/cdev.h>
1213
#include <linux/clk.h>

rust/kernel/build_assert.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
/// fn foo(a: usize) -> usize {
1616
/// a.checked_add(1).unwrap_or_else(|| build_error!("overflow"))
1717
/// }
18+
///
19+
/// assert_eq!(foo(usize::MAX - 1), usize::MAX); // OK.
20+
/// // foo(usize::MAX); // Fails to compile.
1821
/// ```
1922
#[macro_export]
2023
macro_rules! build_error {
@@ -39,8 +42,8 @@ macro_rules! build_error {
3942
/// These examples show that different types of [`assert!`] will trigger errors
4043
/// at different stage of compilation. It is preferred to err as early as
4144
/// possible, so [`static_assert!`] should be used whenever possible.
42-
/// ```compile_fail
43-
/// # use kernel::prelude::*;
45+
// TODO: Could be `compile_fail` when supported.
46+
/// ```ignore
4447
/// fn foo() {
4548
/// static_assert!(1 > 1); // Compile-time error
4649
/// build_assert!(1 > 1); // Build-time error
@@ -50,8 +53,7 @@ macro_rules! build_error {
5053
///
5154
/// When the condition refers to generic parameters or parameters of an inline function,
5255
/// [`static_assert!`] cannot be used. Use `build_assert!` in this scenario.
53-
/// ```no_run
54-
/// # use kernel::prelude::*;
56+
/// ```
5557
/// fn foo<const N: usize>() {
5658
/// // `static_assert!(N > 1);` is not allowed
5759
/// build_assert!(N > 1); // Build-time check

0 commit comments

Comments
 (0)