* [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]
@ 2026-09-30 13:23 Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori via B4 Relay
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Nicolás Antinori via B4 Relay @ 2026-09-30 13:23 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
This patch series intends to implement two features for KUnit tests
written in Rust. The work is based on a TODO comment made in the
`bitmap.rs` module [1].
Changelog:
- RFC patch: [3]
1. Supporting `#[should_panic]` [2]:
KUnit tests in Rust follow the user-space syntax, but at the moment
`#[should_panic]` is not supported. The first patch of this series adds
support for the attribute (only in its basic form, `#[should_panic =
"message"]` is not supported yet).
The way it is supported is by adding a new boolean property called
`rust_should_panic` to `kunit_attributes` struct in kunit/test.h and
check for the `try_catch->try_result` code and the named flag to detect
if there was a panic in `lib/kunit/test.c::kunit_run_case_catch_errors`
function.
The difference with the previous version [3] is that now, almost
everything is handled in the C side, meaning that we don't have a
separate `#[panic_handler]` when KUnit tests are active. In other words
there's no need of a Rust side panic handler because the C side catches
Rust's panics too, since the `#[panic_hanlder]` executes a
bindings::BUG().
2. Allow same test name with different #[cfg(...)]:
When testing `#[should_panic]` in `bitmap.rs` (check the last patch of
the series) I found that the test that was supposed to panic had the
same name as another one, but they were run on different configurations.
This caused the following compilation error:
ERROR:root:error[E0428]: the name `kunit_rust_wrapper_owned_bitmap_out_of_bounds` is defined multiple times
--> ../rust/kernel/bitmap.rs:503:1
|
503 | #[macros::kunit_tests(rust_kernel_bitmap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `kunit_rust_wrapper_owned_bitmap_out_of_bounds` redefined here
|
= note: `kunit_rust_wrapper_owned_bitmap_out_of_bounds` must be defined only once in the value namespace of this module
= note: this error originates in the attribute macro `macros::kunit_tests` (in Nightly builds, run with -Z macro-backtrace for more info)
Now, only the tests with an active `#[cfg(...)]` will be executed by the
runner. Tests with an inactive `#[cfg(...)]` will not appear in the
report since they are not added to the TEST_CASES array.
[1] https://github.com/Rust-for-Linux/linux/blob/fd73f4a6659897191fa0d40695fe370925dd3780/rust/kernel/bitmap.rs#L592-L600
[2] https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html#testing-panics
[3] https://lore.kernel.org/rust-for-linux/cover.1789500084.git.nico.antinori.7@gmail.com/
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
Nicolás Antinori (3):
rust: kunit: add #[should_panic] support
rust: kunit: allow same test name with different #[cfg(...)]
rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case
include/kunit/test.h | 1 +
lib/kunit/attributes.c | 18 ++++++++++++++++++
lib/kunit/test.c | 5 +++++
rust/kernel/bitmap.rs | 40 ++++++++++++++++++----------------------
rust/kernel/kunit.rs | 37 +++++++++++++++++++++++++++++++++++++
rust/macros/kunit.rs | 48 ++++++++++++++++++++++++++++++++++++++----------
6 files changed, 117 insertions(+), 32 deletions(-)
---
base-commit: d266640c6c760c9bc215bf5a3ece122ca488b6f5
change-id: 20260930-rust_kunit_v4-7abaee11e6ac
Best regards,
--
Nicolás Antinori <nico.antinori.7@gmail.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/3] rust: kunit: add #[should_panic] support
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
@ 2026-09-30 13:23 ` Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 2/3] rust: kunit: allow same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Nicolás Antinori via B4 Relay @ 2026-09-30 13:23 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
From: Nicolás Antinori <nico.antinori.7@gmail.com>
KUnit tests in Rust are written using user-space like syntax. This patch
adds support for the `#[should_panic]` attribute, enabling the user to
test conditions that are expected to cause a panic and report the test
as successful.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
include/kunit/test.h | 1 +
lib/kunit/attributes.c | 18 ++++++++++++++++++
lib/kunit/test.c | 5 +++++
rust/kernel/kunit.rs | 15 +++++++++++++++
rust/macros/kunit.rs | 28 +++++++++++++++++++++++-----
5 files changed, 62 insertions(+), 5 deletions(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h
index da5312e0dfa57f37a7e96c9e618b03b0947ede8b..f2c640718d12258c183817be08a091b8cf316d16 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -83,6 +83,7 @@ enum kunit_speed {
/* Holds attributes for each test case and suite */
struct kunit_attributes {
enum kunit_speed speed;
+ bool rust_should_panic;
};
/**
diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c
index 6d7a53af94a9acf3cf336409462059b972efeb22..8a9dc526b3cb2d363d42087f8666a03e9bc2bc4b 100644
--- a/lib/kunit/attributes.c
+++ b/lib/kunit/attributes.c
@@ -246,6 +246,16 @@ static void *attr_is_init_get(void *test_or_suite, bool is_test)
return ((void *) suite->is_init);
}
+static void *attr_rust_should_panic_get(void *test_or_suite, bool is_test)
+{
+ struct kunit_case *test = is_test ? test_or_suite : NULL;
+
+ if (test)
+ return ((void *) test->attr.rust_should_panic);
+ else
+ return ((void *) NULL);
+}
+
/* List of all Test Attributes */
static struct kunit_attr kunit_attr_list[] = {
@@ -272,6 +282,14 @@ static struct kunit_attr kunit_attr_list[] = {
.filter = attr_bool_filter,
.attr_default = (void *)false,
.print = PRINT_SUITE,
+ },
+ {
+ .name = "rust_should_panic",
+ .get_attr = attr_rust_should_panic_get,
+ .to_string = attr_bool_to_string,
+ .filter = attr_bool_filter,
+ .attr_default = (void *)false,
+ .print = PRINT_NEVER,
}
};
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 09e3dabfac0c945b2608feb88c1b1d46fb807edc..2140841b7dc9ca45346159683d8f2b85b6e11381 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -590,6 +590,11 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
context.test_case = test_case;
kunit_try_catch_run(try_catch, &context);
+ if (try_catch->try_result == -EINTR && test_case->attr.rust_should_panic) {
+ test->status = KUNIT_SUCCESS;
+ test_case->status = KUNIT_SUCCESS;
+ }
+
/* Now run the cleanup */
kunit_try_catch_init(try_catch,
test,
diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 91eaff8c186afd09333a525bc106d39391d30a92..5fa4d994e0f7ebf5bbe5fd3571a372548cc8499b 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -207,6 +207,7 @@ pub const fn kunit_case(
name: kernel::str::as_char_ptr_in_const_context(name),
attr: kernel::bindings::kunit_attributes {
speed: kernel::bindings::kunit_speed_KUNIT_SPEED_NORMAL,
+ rust_should_panic: false,
},
generate_params: None,
status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
@@ -282,6 +283,7 @@ macro_rules! kunit_unsafe_test_suite {
exit: None,
attr: ::kernel::bindings::kunit_attributes {
speed: ::kernel::bindings::kunit_speed_KUNIT_SPEED_NORMAL,
+ rust_should_panic: false,
},
status_comment: [0; 256usize],
debugfs: ::core::ptr::null_mut(),
@@ -345,6 +347,19 @@ fn rust_test_kunit_in_kunit_test() {
assert!(in_kunit_test());
}
+ #[test]
+ #[should_panic]
+ fn rust_test_kunit_panic_in_kunit_test() {
+ panic!("This test should panic and pass");
+ }
+
+ #[test]
+ #[should_panic]
+ fn rust_test_kunit_panic_in_kunit_test_bug() {
+ // The panic is produced in a C call. This should succeed.
+ unsafe { bindings::BUG() };
+ }
+
#[test]
#[cfg(not(all()))]
fn rust_test_kunit_always_disabled_test() {
diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index ae20ed6768f1564439acf0cadc72a787bada8473..f1d0210e6a49468876d69821cc559002ad12da50 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -106,6 +106,11 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke
.cloned()
.collect();
+ let should_panic = f
+ .attrs
+ .iter()
+ .any(|attr| attr.path().is_ident("should_panic"));
+
// Before the test, override usual `assert!` and `assert_eq!` macros with ones that call
// KUnit instead.
let test_str = test.to_string();
@@ -135,25 +140,38 @@ macro_rules! assert_eq {
&CString::new(test_str.as_str()).expect("identifier cannot contain NUL"),
test.span(),
);
+ let assertion = if should_panic {
+ quote!(
+ let _ = #test();
+ (*_test).status = ::kernel::bindings::kunit_status_KUNIT_FAILURE;
+ )
+ } else {
+ quote!(
+ use ::kernel::kunit::is_test_result_ok;
+ assert!(is_test_result_ok(#test()));
+ )
+ };
processed_items.push(parse_quote! {
unsafe extern "C" fn #kunit_wrapper_fn_name(_test: *mut ::kernel::bindings::kunit) {
(*_test).status = ::kernel::bindings::kunit_status_KUNIT_SKIPPED;
+
// Append any `cfg` attributes the user might have written on their tests so we
// don't attempt to call them when they are `cfg`'d out. An extra `use` is used
// here to reduce the length of the assert message.
#(#cfg_attrs)*
{
(*_test).status = ::kernel::bindings::kunit_status_KUNIT_SUCCESS;
- use ::kernel::kunit::is_test_result_ok;
- assert!(is_test_result_ok(#test()));
+ #assertion
}
}
});
- test_cases.push(quote!(
- ::kernel::kunit::kunit_case(#test_cstr, #kunit_wrapper_fn_name)
- ));
+ test_cases.push(quote!({
+ let mut case = ::kernel::kunit::kunit_case(#test_cstr, #kunit_wrapper_fn_name);
+ case.attr.rust_should_panic = #should_panic;
+ case
+ }));
}
let num_tests_plus_1 = test_cases.len() + 1;
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] rust: kunit: allow same test name with different #[cfg(...)]
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori via B4 Relay
@ 2026-09-30 13:23 ` Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori via B4 Relay
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Nicolás Antinori via B4 Relay @ 2026-09-30 13:23 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
From: Nicolás Antinori <nico.antinori.7@gmail.com>
Sometimes it is necessary to test the same code paths under different
configurations. The `#[cfg(...)]` macro can be used to check if specific
configurations are enabled and run tests accordingly.
Currently, defining multiple tests with the same name under different
`#[cfg(...)]` attributes results in a compilation error. This patch
removes that restriction, allowing identical test names across different
configurations.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
rust/kernel/kunit.rs | 22 ++++++++++++++++++++++
rust/macros/kunit.rs | 20 +++++++++++++++-----
2 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 5fa4d994e0f7ebf5bbe5fd3571a372548cc8499b..5ff988ab76a408751d1f57b59cc82a79c6c2534a 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -347,6 +347,28 @@ fn rust_test_kunit_in_kunit_test() {
assert!(in_kunit_test());
}
+ // Tests with cfg have the same name on purpose because we are implicitly testing that
+ // tests with the same name but different configs do not throw a compilation error
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "y")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ assert!(in_kunit_test());
+ }
+
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "n")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ // This test should never run because of the `cfg`.
+ assert!(false)
+ }
+
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "INVALID_VALUE")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ // This test should never run because of the `cfg`.
+ assert!(false)
+ }
+
#[test]
#[should_panic]
fn rust_test_kunit_panic_in_kunit_test() {
diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index f1d0210e6a49468876d69821cc559002ad12da50..18cfa463c8dbaa216160a686b1cd6c144986568c 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -45,6 +45,8 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke
let mut processed_items = Vec::new();
let mut test_cases = Vec::new();
+ let mut test_cases_cfgs = Vec::new();
+ let mut test_case_number: u32 = 1;
// Generate the test KUnit test suite and a test case for each `#[test]`.
//
@@ -135,7 +137,7 @@ macro_rules! assert_eq {
// Add back the test item.
processed_items.push(Item::Fn(f));
- let kunit_wrapper_fn_name = format_ident!("kunit_rust_wrapper_{test}");
+ let kunit_wrapper_fn_name = format_ident!("kunit_rust_wrapper_{test}_{test_case_number}");
let test_cstr = LitCStr::new(
&CString::new(test_str.as_str()).expect("identifier cannot contain NUL"),
test.span(),
@@ -172,15 +174,23 @@ macro_rules! assert_eq {
case.attr.rust_should_panic = #should_panic;
case
}));
+ test_cases_cfgs.push(cfg_attrs);
+ test_case_number += 1;
}
- let num_tests_plus_1 = test_cases.len() + 1;
+ // This dummy array is used to calculate the exact number of tests cases that are active
+ // depending on their #[cfg(...)] attribute.
+ processed_items.push(parse_quote!(
+ const TEST_CASES_COUNT: &[()] = &[#(#(#test_cases_cfgs)* (),)* ()];
+ ));
+
processed_items.push(parse_quote! {
- static mut TEST_CASES: [::kernel::bindings::kunit_case; #num_tests_plus_1] = [
- #(#test_cases,)*
- ::pin_init::zeroed(),
+ static mut TEST_CASES: [::kernel::bindings::kunit_case; TEST_CASES_COUNT.len()] = [
+ #(#(#test_cases_cfgs)* { #test_cases },)*
+ ::pin_init::zeroed()
];
});
+
processed_items.push(parse_quote! {
::kernel::kunit_unsafe_test_suite!(#test_suite, TEST_CASES);
});
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 2/3] rust: kunit: allow same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
@ 2026-09-30 13:23 ` Nicolás Antinori via B4 Relay
2026-09-30 14:41 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Miguel Ojeda
2026-09-30 14:54 ` Gary Guo
4 siblings, 0 replies; 7+ messages in thread
From: Nicolás Antinori via B4 Relay @ 2026-09-30 13:23 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
From: Nicolás Antinori <nico.antinori.7@gmail.com>
Rust KUnit tests now support `#[should_panic]` attribute. Uncomment the
test so it can be run when CONFIG_RUST_BITMAP_HARDENED=y.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
rust/kernel/bitmap.rs | 40 ++++++++++++++++++----------------------
1 file changed, 18 insertions(+), 22 deletions(-)
diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index b27e0ec80d643da5d5510ee79e24493323e61b72..a9935b2be8c54d1b66d1c1cade1eba1fb06b6cd3 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -571,33 +571,29 @@ fn bitmap_set_clear_find() -> Result<(), AllocError> {
Ok(())
}
+ #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
#[test]
fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // TODO: Kunit #[test]s do not support `cfg` yet,
- // so we add it here in the body.
- #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
- {
- let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- b.set_bit(2048);
- b.set_bit_atomic(2048);
- b.clear_bit(2048);
- b.clear_bit_atomic(2048);
- assert_eq!(None, b.next_bit(2048));
- assert_eq!(None, b.next_zero_bit(2048));
- assert_eq!(None, b.last_bit());
- }
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+ b.set_bit(2048);
+ b.set_bit_atomic(2048);
+ b.clear_bit(2048);
+ b.clear_bit_atomic(2048);
+ assert_eq!(None, b.next_bit(2048));
+ assert_eq!(None, b.next_zero_bit(2048));
+ assert_eq!(None, b.last_bit());
Ok(())
}
- // TODO: uncomment once kunit supports [should_panic] and `cfg`.
- // #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
- // #[test]
- // #[should_panic]
- // fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- //
- // b.set_bit(2048);
- // }
+ #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
+ #[test]
+ #[should_panic]
+ fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+ b.set_bit(2048);
+ Ok(())
+ }
#[test]
fn bitmap_copy_and_extend() -> Result<(), AllocError> {
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
` (2 preceding siblings ...)
2026-09-30 13:23 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori via B4 Relay
@ 2026-09-30 14:41 ` Miguel Ojeda
2026-09-30 14:54 ` Gary Guo
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2026-09-30 14:41 UTC (permalink / raw)
To: nico.antinori.7
Cc: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda,
Alexandre Courbot, Andreas Hindborg, Benno Lossin,
Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
On Wed, Sep 30, 2026 at 3:23 PM Nicolás Antinori via B4 Relay
<devnull+nico.antinori.7.gmail.com@kernel.org> wrote:
>
> This patch series intends to implement two features for KUnit tests
> written in Rust. The work is based on a TODO comment made in the
> `bitmap.rs` module [1].
I am glad it worked with `b4`! Hopefully no more Gmail issues :)
(Nit: in cases like this, I would suggest increasing tagging with
RESEND or maybe increasing the version to avoid the same subject line
in Lore and other clients.)
Cheers,
Miguel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
` (3 preceding siblings ...)
2026-09-30 14:41 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Miguel Ojeda
@ 2026-09-30 14:54 ` Gary Guo
4 siblings, 0 replies; 7+ messages in thread
From: Gary Guo @ 2026-09-30 14:54 UTC (permalink / raw)
To: nico.antinori.7, Alice Ryhl, Burak Emir, Brendan Higgins,
David Gow, Miguel Ojeda
Cc: Alexandre Courbot, Andreas Hindborg, Benno Lossin,
Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
On Wed Sep 30, 2026 at 2:23 PM BST, Nicolás Antinori via B4 Relay wrote:
> This patch series intends to implement two features for KUnit tests
> written in Rust. The work is based on a TODO comment made in the
> `bitmap.rs` module [1].
>
> Changelog:
> - RFC patch: [3]
>
> 1. Supporting `#[should_panic]` [2]:
>
> KUnit tests in Rust follow the user-space syntax, but at the moment
> `#[should_panic]` is not supported. The first patch of this series adds
> support for the attribute (only in its basic form, `#[should_panic =
> "message"]` is not supported yet).
>
> The way it is supported is by adding a new boolean property called
> `rust_should_panic` to `kunit_attributes` struct in kunit/test.h and
This is now more general than Rust panics? Perhaps this should just be
"should_bug"?
Best,
Gary
> check for the `try_catch->try_result` code and the named flag to detect
> if there was a panic in `lib/kunit/test.c::kunit_run_case_catch_errors`
> function.
>
> The difference with the previous version [3] is that now, almost
> everything is handled in the C side, meaning that we don't have a
> separate `#[panic_handler]` when KUnit tests are active. In other words
> there's no need of a Rust side panic handler because the C side catches
> Rust's panics too, since the `#[panic_hanlder]` executes a
> bindings::BUG().
>
> 2. Allow same test name with different #[cfg(...)]:
>
> When testing `#[should_panic]` in `bitmap.rs` (check the last patch of
> the series) I found that the test that was supposed to panic had the
> same name as another one, but they were run on different configurations.
> This caused the following compilation error:
>
> ERROR:root:error[E0428]: the name `kunit_rust_wrapper_owned_bitmap_out_of_bounds` is defined multiple times
> --> ../rust/kernel/bitmap.rs:503:1
> |
> 503 | #[macros::kunit_tests(rust_kernel_bitmap)]
> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `kunit_rust_wrapper_owned_bitmap_out_of_bounds` redefined here
> |
> = note: `kunit_rust_wrapper_owned_bitmap_out_of_bounds` must be defined only once in the value namespace of this module
> = note: this error originates in the attribute macro `macros::kunit_tests` (in Nightly builds, run with -Z macro-backtrace for more info)
>
> Now, only the tests with an active `#[cfg(...)]` will be executed by the
> runner. Tests with an inactive `#[cfg(...)]` will not appear in the
> report since they are not added to the TEST_CASES array.
>
> [1] https://github.com/Rust-for-Linux/linux/blob/fd73f4a6659897191fa0d40695fe370925dd3780/rust/kernel/bitmap.rs#L592-L600
> [2] https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html#testing-panics
> [3] https://lore.kernel.org/rust-for-linux/cover.1789500084.git.nico.antinori.7@gmail.com/
>
> Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
> ---
> Nicolás Antinori (3):
> rust: kunit: add #[should_panic] support
> rust: kunit: allow same test name with different #[cfg(...)]
> rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case
>
> include/kunit/test.h | 1 +
> lib/kunit/attributes.c | 18 ++++++++++++++++++
> lib/kunit/test.c | 5 +++++
> rust/kernel/bitmap.rs | 40 ++++++++++++++++++----------------------
> rust/kernel/kunit.rs | 37 +++++++++++++++++++++++++++++++++++++
> rust/macros/kunit.rs | 48 ++++++++++++++++++++++++++++++++++++++----------
> 6 files changed, 117 insertions(+), 32 deletions(-)
> ---
> base-commit: d266640c6c760c9bc215bf5a3ece122ca488b6f5
> change-id: 20260930-rust_kunit_v4-7abaee11e6ac
>
> Best regards,
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case
2026-09-29 14:07 Nicolás Antinori
@ 2026-09-29 14:07 ` Nicolás Antinori
0 siblings, 0 replies; 7+ messages in thread
From: Nicolás Antinori @ 2026-09-29 14:07 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
Rust KUnit tests now support `#[should_panic]` attribute. Uncomment the
test so it can be run when CONFIG_RUST_BITMAP_HARDENED=y.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
rust/kernel/bitmap.rs | 40 ++++++++++++++++++----------------------
1 file changed, 18 insertions(+), 22 deletions(-)
diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index b27e0ec80d64..a9935b2be8c5 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -571,33 +571,29 @@ fn bitmap_set_clear_find() -> Result<(), AllocError> {
Ok(())
}
+ #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
#[test]
fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // TODO: Kunit #[test]s do not support `cfg` yet,
- // so we add it here in the body.
- #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
- {
- let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- b.set_bit(2048);
- b.set_bit_atomic(2048);
- b.clear_bit(2048);
- b.clear_bit_atomic(2048);
- assert_eq!(None, b.next_bit(2048));
- assert_eq!(None, b.next_zero_bit(2048));
- assert_eq!(None, b.last_bit());
- }
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+ b.set_bit(2048);
+ b.set_bit_atomic(2048);
+ b.clear_bit(2048);
+ b.clear_bit_atomic(2048);
+ assert_eq!(None, b.next_bit(2048));
+ assert_eq!(None, b.next_zero_bit(2048));
+ assert_eq!(None, b.last_bit());
Ok(())
}
- // TODO: uncomment once kunit supports [should_panic] and `cfg`.
- // #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
- // #[test]
- // #[should_panic]
- // fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- //
- // b.set_bit(2048);
- // }
+ #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
+ #[test]
+ #[should_panic]
+ fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+ b.set_bit(2048);
+ Ok(())
+ }
#[test]
fn bitmap_copy_and_extend() -> Result<(), AllocError> {
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-09-30 14:54 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-30 13:23 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 2/3] rust: kunit: allow same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-09-30 13:23 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori via B4 Relay
2026-09-30 14:41 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Miguel Ojeda
2026-09-30 14:54 ` Gary Guo
-- strict thread matches above, loose matches on Subject: below --
2026-09-29 14:07 Nicolás Antinori
2026-09-29 14:07 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®