KUNIT_EXPECT_KASAN_RESULT(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… KUNIT_EXPECT_KASAN_RESULT πŸ’…

okay so bestie, this is a page from the Kernel API Manual, and trust me, it's giving "niche kernel lore that absolutely slays" ✨ stay with me.

🌸 NAME 🌸

KUNIT_EXPECT_KASAN_RESULT β€” checks whether the executed expression produces a KASAN report; causes a KUnit test failure when the result is different from @fail. πŸ’€ like, it literally fails your test on purpose. iconic behavior.


✨ SYNOPSIS ✨

KUNIT_EXPECT_KASAN_RESULT (test , expr , expr_str , fail );

πŸŽ€ ARGUMENTS πŸŽ€

  • test β€” Currently executing KUnit test. 🧚 the main character of this whole situation.
  • expr β€” Expression to be tested. πŸ¦‹ the thing you're putting under the microscope.
  • expr_str β€” Expression to be tested encoded as a string. like, the expression but in its string-font era πŸ’…
  • fail β€” Whether expression should produce a KASAN report. πŸ”₯ yes/no, true/false, does-it-explode energy.

πŸ’œ DESCRIPTION πŸ’œ

So here's the tea πŸ“ β€” For hardware tag-based KASAN, when a synchronous tag fault happens, tag checking is auto-disabled. Not me getting sidelined by a fault 😭. When this happens, this test handler reenables tag checking. As tag checking can be only disabled or enabled per CPU, this handler disables migration (preemption). yeah. per CPU. no exceptions, periodt. πŸ‘‘

Since the compiler doesn't see that the expression can change the test_status fields, it can reorder or optimize away the accesses to those fields. r00d πŸ™ˆ. Use READ/WRITE_ONCE for the accesses and compiler barriers around the expression to prevent that. like, actually do it, ngl, this is your vibe-check against the optimizer.

In between KUNIT_EXPECT_KASAN_RESULT checks, test_status.report_found is kept as false. This allows detecting KASAN reports that happen outside of the checks by asserting !test_status.report_found at the start of KUNIT_EXPECT_KASAN_RESULT and in kasan_test_exit. πŸ”₯ so sneaky out-of-band reports get caught too. we love accountability. πŸ’–

lowkey important: those READ_ONCE/WRITE_ONCE + barrier bits aren't optional decoration β€” skip them and the compiler will absolutely do crimes behind your back πŸ’€

🌈 SEE ALSO 🌈

  • Kernel file ./mm/kasan/kasan_test_c.c β€” go read it, iykyk 🫢

k byeeee πŸ’… that's the page β€” brought to you by the source at ./mm/kasan, dated October 2026, filed under KUNIT_EXPECT_KASAN_RESULT(9). slay, go write some tests bestie πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from