KASSERT(9)

πŸ’– KASSERT β€” the kernel expression verification macro πŸ’–

okay bestie, sit DOWN, because we're talking about KASSERT(9) from the Kernel Developer's Manual (yes, section 9, that's the kernel-dev zone, iykyk). This little macro is the vibe check your kernel runs on itself, and honestly? she's iconic. ✨


✨ SYNOPSIS ✨

First things first, she only really shows up if you built your kernel like this:

options INVARIANTS

Then you gotta pull in the headers:

#include <sys/param.h>
#include <sys/systm.h>

And the call itself, the moment we've all been waiting for:

KASSERT(expression, msg);

🌸 DESCRIPTION 🌸

So here's the tea. If your kernel was compiled with options INVARIANTS, then KASSERT() takes the expression you handed her and tests it. And if that expression comes back false? She calls panic(9). Like, immediately. And that panic terminates the running system. No soft launch, no gentle exit, just πŸ’€. She does not do half measures, periodt.

But if your kernel was built without options INVARIANTS? Then KASSERT() is defined to be a no-op. She's literally just standing there, doing nothing, being a chill little ghost. πŸ‘»

Oh and the second argument, msg? That's a printf(9) format string and its arguments, all wrapped up together in parentheses. Don't forget the parentheses, bestie, that's the whole point. πŸŽ€

ngl, the no-op version is lowkey the most unhinged part β€” your assertion just evaporates when INVARIANTS isn't set, so don't be out here thinking it's always checking. It is NOT. πŸ™ˆ

πŸ”₯ EXAMPLES πŸ”₯

The kernel function vput() must not be called with a NULL pointer. She said what she said. So we assert it, like this:

void
vput(vp)
        struct vnode *vp;
{
        struct proc *p = curproc;
        KASSERT(vp != NULL, ("vput: null vp"));
        ...
}

See how the message is ("vput: null vp")? Parentheses. Around the whole thing. Because it's a printf(9) format string plus args, remember? Slay. πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹


πŸ‘‘ AUTHORS πŸ‘‘

This manual page was written by Jonathan M. Bresler <jmb@FreeBSD.org>. Thank you for your service, king. 🫢


okay and that's the whole page!! this was the KASSERT(9) man page from the Kernel Developer's Manual, brought to you by the Debian crew, dated January 14, 2000 β€” yes, a vintage. she's been serving since Y2K and she's still booked and busy. πŸŒˆβ­πŸ’œ

πŸ“¦ where this page comes from