KUNIT_STATIC_STUB_REDIRECT(9)
π KUNIT_STATIC_STUB_REDIRECT(9) β like, the whole vibe
okay bestie so this is a page from the Kernel API Manual (yes the Kernel API Manual, we're in the big leagues now) and it's all about KUNIT_STATIC_STUB_REDIRECT β a macro that lets a KUnit test swap out a function on the fly. iconic behaviour, honestly. π
β¨ NAME β¨
KUNIT_STATIC_STUB_REDIRECT β call a replacement "static stub" if one exists π«Ά
πΈ SYNOPSIS πΈ
KUNIT_STATIC_STUB_REDIRECT (real_fn_name , args... );
that's the whole shape of it, periodt β¨
π¦ ARGUMENTS π¦
real_fn_nameβ the name of this function, and no cap it has to be as an identifier, not a string π like, don't go putting quotes on it.argsβ literally all of the arguments you passed to this function. every single one. bring the whole squad. π
π₯ DESCRIPTION π₯
so this thing is a function prologue. it's giving "bouncer at the door of your function" energy π β it's there so that a KUnit test can redirect calls to the current function somewhere else.
here's the tea: KUnit tests can call kunit_activate_static_stub to pass a replacement function in, and that replacement function gets called by KUNIT_STATIC_STUB_REDIRECT, which then returns from the function β like, dips out immediately, bye π
.
but if the caller is not in a KUnit context? then the function just keeps executing as normal, no drama, business as usual. slay. β¨
π EXAMPLE π
okay watch, this is the cute part:
int real_func(int n)
{
KUNIT_STATIC_STUB_REDIRECT(real_func, n);
return 0;
}
int replacement_func(int n)
{
return 42;
}
void example_test(struct kunit *test)
{
kunit_activate_static_stub(test, real_func, replacement_func);
KUNIT_EXPECT_EQ(test, real_func(1), 42);
}
see how real_func(1) comes back as 42? that's the stub doing its little magic trick π§ nobody touch anything, it's working.
π SEE ALSO π
- the kernel file
./include/kunit/static_stub.hβ go read it, iykyk π kunit_activate_static_stub(9)kunit_deactivate_static_stub(9)
and that's the page, bestie!! no notes, 10/10, ate and left no crumbs π
source/version: ./include/kunit Β· date: October 2026 Β· KUNIT_STATIC_STUB_REDIRECT(9) β mwah, see u in the next manpage ππ¦πΈ
π¦ 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