KUNIT_STATIC_STUB_REDIRECT(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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 🌈


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