Re: [PATCH v2 1/3] rust: kunit: add #[should_panic] support
From: Nicolás Antinori
Date: Mon Nov 02 2026 - 10:34:23 EST
On Thu Oct 29, 2026 at 1:59 PM -03, Nicolás Antinori via B4 Relay wrote:
> From: Nicolás Antinori <nico.antinori.7@xxxxxxxxx>
>
> 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@xxxxxxxxx>
Hello, Sashiko found two issues [1] with this patch:
> [Severity: High]
> Will these new selftests crash the kernel or hang when
> CONFIG_RUST_KUNIT_SELFTEST is enabled under CONFIG_UML=y,
> CONFIG_PANIC_ON_OOPS=y, or CONFIG_BUG=n?
This is true, I tested this with:
./tools/testing/kunit/kunit.py run \
--make_options LLVM=1 \
--kconfig_add CONFIG_RUST=y \
--kconfig_add CONFIG_RUST_KUNIT_TESTS=y \
--kconfig_add CONFIG_KUNIT=y \
--kconfig_add CONFIG_UML=y \
--arch=um rust_kernel_kunit
./tools/testing/kunit/kunit.py run \
--make_options LLVM=1 \
--kconfig_add CONFIG_RUST=y \
--kconfig_add CONFIG_RUST_KUNIT_TESTS=y \
--kconfig_add CONFIG_KUNIT=y \
--kconfig_add CONFIG_PANIC_ON_OOPS=y \
--arch=x86_64 rust_kernel_kunit
And some other configs using the configs causing problems.
To solve this I created a new config "RUST_KUNIT_SHOULD_PANIC_ATTRIBUTE"
that depends on !UML && !PANIC_ON_OOPS && BUG. If the test is annotated
with `#[should_panic]` and CONFIG_RUST_KUNIT_SHOULD_PANIC_ATTRIBUTE=n,
the test won't run and won't show up in the summary.
> [Severity: Medium]
> Does defining macro_rules! assert and macro_rules! assert_eq at module scope
> in kunit_tests() leak the preceding test's #should_panic value into helper
> functions or impl blocks defined later in the module?
This is also true, the solution was redefining the `assert!` and
`assert_eq!` macros inside the function's body (as Sashiko suggested).
I'll wait a bit in case there are any feedback on the current series and
send a v3 with these changes.
Best Regards,
Nicolás
[1] https://sashiko.dev/#/patchset/20261029-rust_kunit_v6_send-v2-0-5ce551efc48e@xxxxxxxxx?part=1