KUNIT_EXPECT_KASAN_RESULT(9)
π 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: thoseREAD_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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms