xref: /freebsd/share/man/man9/KASSERT.9 (revision 5d956e11ed3713ff769d51b71f7b4ee6ce0085fe)
187132d1dSMitchell Horne.\" SPDX-License-Identifier: BSD-2-Clause
26fc639dcSJonathan M. Bresler.\"
3eb2630afSJeroen Ruigrok van der Werven.\" Copyright (c) 2000 Jonathan M. Bresler
46fc639dcSJonathan M. Bresler.\" All rights reserved.
583a426d1SMitchell Horne.\" Copyright (c) 2023-2024 The FreeBSD Foundation
687132d1dSMitchell Horne.\"
787132d1dSMitchell Horne.\" Portions of this documentation were written by Mitchell Horne
887132d1dSMitchell Horne.\" under sponsorship from the FreeBSD Foundation.
96fc639dcSJonathan M. Bresler.\"
106fc639dcSJonathan M. Bresler.\" This program is free software.
116fc639dcSJonathan M. Bresler.\"
126fc639dcSJonathan M. Bresler.\" Redistribution and use in source and binary forms, with or without
136fc639dcSJonathan M. Bresler.\" modification, are permitted provided that the following conditions
146fc639dcSJonathan M. Bresler.\" are met:
156fc639dcSJonathan M. Bresler.\" 1. Redistributions of source code must retain the above copyright
166fc639dcSJonathan M. Bresler.\"    notice, this list of conditions and the following disclaimer.
176fc639dcSJonathan M. Bresler.\" 2. Redistributions in binary form must reproduce the above copyright
186fc639dcSJonathan M. Bresler.\"    notice, this list of conditions and the following disclaimer in the
196fc639dcSJonathan M. Bresler.\"    documentation and/or other materials provided with the distribution.
206fc639dcSJonathan M. Bresler.\"
216fc639dcSJonathan M. Bresler.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
226fc639dcSJonathan M. Bresler.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
236fc639dcSJonathan M. Bresler.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
246fc639dcSJonathan M. Bresler.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
256fc639dcSJonathan M. Bresler.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
266fc639dcSJonathan M. Bresler.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
276fc639dcSJonathan M. Bresler.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
286fc639dcSJonathan M. Bresler.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
296fc639dcSJonathan M. Bresler.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
306fc639dcSJonathan M. Bresler.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
316fc639dcSJonathan M. Bresler.\"
3283a426d1SMitchell Horne.Dd March 19, 2024
336fc639dcSJonathan M. Bresler.Dt KASSERT 9
34aa12cea2SUlrich Spörlein.Os
356fc639dcSJonathan M. Bresler.Sh NAME
366fc639dcSJonathan M. Bresler.Nm KASSERT
3787132d1dSMitchell Horne.Nd kernel expression verification macros
386fc639dcSJonathan M. Bresler.Sh SYNOPSIS
390cf72940SRuslan Ermilov.Cd "options INVARIANTS"
400cf72940SRuslan Ermilov.Pp
41f16b3c0dSChad David.In sys/param.h
42f16b3c0dSChad David.In sys/systm.h
43f16b3c0dSChad David.Fn KASSERT expression msg
4487132d1dSMitchell Horne.Fn MPASS expression
456fc639dcSJonathan M. Bresler.Sh DESCRIPTION
4687132d1dSMitchell HorneAssertions are widely used within the
4787132d1dSMitchell Horne.Fx
4887132d1dSMitchell Hornekernel to verify programmatic assumptions.
4987132d1dSMitchell HorneFor violations of run-time assumptions and invariants, it is desirable to fail
5087132d1dSMitchell Horneas soon and as loudly as possible.
5187132d1dSMitchell HorneAssertions are optional code; for non-recoverable error conditions an explicit
5287132d1dSMitchell Hornecall to
530cf72940SRuslan Ermilov.Xr panic 9
5487132d1dSMitchell Horneis usually preferred.
556fc639dcSJonathan M. Bresler.Pp
5687132d1dSMitchell HorneThe
5787132d1dSMitchell Horne.Fn KASSERT
5887132d1dSMitchell Hornemacro tests the given boolean
5987132d1dSMitchell Horne.Fa expression .
6087132d1dSMitchell HorneIf
6187132d1dSMitchell Horne.Fa expression
6287132d1dSMitchell Horneevaluates to
6387132d1dSMitchell Horne.Dv false ,
6487132d1dSMitchell Horneand the kernel is compiled with
650cf72940SRuslan Ermilov.Cd "options INVARIANTS" ,
666fc639dcSJonathan M. Breslerthe
6787132d1dSMitchell Horne.Xr panic 9
6887132d1dSMitchell Hornefunction is called.
6987132d1dSMitchell HorneThis terminates the running system at the point of the error, possibly dropping
7087132d1dSMitchell Horneinto the kernel debugger or initiating a kernel core dump.
7187132d1dSMitchell HorneThe second argument,
7287132d1dSMitchell Horne.Fa msg ,
7387132d1dSMitchell Horneis a
745466eeeaSHiten Pandya.Xr printf 9
755466eeeaSHiten Pandyaformat string and its arguments,
765466eeeaSHiten Pandyaenclosed in parentheses.
7787132d1dSMitchell HorneThe formatted string will become the panic string.
7887132d1dSMitchell Horne.Pp
7987132d1dSMitchell HorneIn a kernel that is built without
8087132d1dSMitchell Horne.Cd "options INVARIANTS" ,
8187132d1dSMitchell Hornethe assertion macros are defined to be no-ops.
8287132d1dSMitchell HorneThis eliminates the runtime overhead of widespread assertions from release
8387132d1dSMitchell Hornebuilds of the kernel.
8487132d1dSMitchell HorneTherefore, checks which can be performed in a constant amount of time can be
8587132d1dSMitchell Horneadded as assertions without concern about their performance impact.
8687132d1dSMitchell HorneMore expensive checks, such as those that output to console, or verify the
8787132d1dSMitchell Horneintegrity of a chain of objects are generally best hidden behind the
8887132d1dSMitchell Horne.Cd DIAGNOSTIC
8987132d1dSMitchell Hornekernel option.
9087132d1dSMitchell Horne.Pp
9187132d1dSMitchell HorneThe
9287132d1dSMitchell Horne.Fn MPASS
9387132d1dSMitchell Hornemacro (read as: "must-pass")
9487132d1dSMitchell Horneis a convenience wrapper around
9587132d1dSMitchell Horne.Fn KASSERT
9683a426d1SMitchell Hornethat automatically generates a simple assertion message including file and line
9783a426d1SMitchell Horneinformation.
9883a426d1SMitchell Horne.Ss Assertion Guidelines
9983a426d1SMitchell HorneWhen adding new assertions, keep in mind their primary purpose: to aid in
10083a426d1SMitchell Horneidentifying and debugging of complex error conditions.
10183a426d1SMitchell Horne.Pp
10283a426d1SMitchell HorneThe panic messages resulting from assertion failures should be useful without
10383a426d1SMitchell Hornethe resulting kernel dump; the message may be included in a bug report, and
10483a426d1SMitchell Horneshould contain the relevant information needed to discern how the assertion was
10583a426d1SMitchell Horneviolated.
10683a426d1SMitchell HorneThis is especially important when the error condition is difficult or
10783a426d1SMitchell Horneimpossible for the developer to reproduce locally.
10883a426d1SMitchell Horne.Pp
10983a426d1SMitchell HorneTherefore, assertions should adhere to the following guidelines:
11083a426d1SMitchell Horne.Bl -enum
11183a426d1SMitchell Horne.It
11283a426d1SMitchell HorneWhenever possible, the value of a runtime variable checked by an assertion
11383a426d1SMitchell Hornecondition should appear in its message.
11483a426d1SMitchell Horne.It
11583a426d1SMitchell HorneUnrelated conditions must appear in separate assertions.
11683a426d1SMitchell Horne.It
11783a426d1SMitchell HorneMultiple related conditions should be distinguishable (e.g. by value), or split
11883a426d1SMitchell Horneinto separate assertions.
11983a426d1SMitchell Horne.It
12083a426d1SMitchell HorneWhen in doubt, print more information, not less.
12183a426d1SMitchell Horne.El
12283a426d1SMitchell Horne.Pp
12383a426d1SMitchell HorneCombined, this gives greater clarity into the exact cause of an assertion
12483a426d1SMitchell Hornepanic; see
12583a426d1SMitchell Horne.Sx EXAMPLES
12683a426d1SMitchell Hornebelow.
12725216c5cSJeroen Ruigrok van der Werven.Sh EXAMPLES
12887132d1dSMitchell HorneA hypothetical
12987132d1dSMitchell Horne.Vt struct foo
13087132d1dSMitchell Horneobject must not have its 'active' flag set when calling
13187132d1dSMitchell Horne.Fn foo_dealloc :
1326fc639dcSJonathan M. Bresler.Bd -literal -offset indent
1336fc639dcSJonathan M. Breslervoid
13487132d1dSMitchell Hornefoo_dealloc(struct foo *fp)
1356fc639dcSJonathan M. Bresler{
13687132d1dSMitchell Horne
13787132d1dSMitchell Horne	KASSERT((fp->foo_flags & FOO_ACTIVE) == 0,
13883a426d1SMitchell Horne	    ("%s: fp %p is still active, flags=%x", __func__, fp,
13983a426d1SMitchell Horne	    fp->foo_flags));
1406fc639dcSJonathan M. Bresler	...
1416fc639dcSJonathan M. Bresler}
1426fc639dcSJonathan M. Bresler.Ed
14387132d1dSMitchell Horne.Pp
14483a426d1SMitchell HorneThis assertion provides the full flag set for the object, as well as the memory
14583a426d1SMitchell Hornepointer, which may be used by a debugger to examine the object in detail
14683a426d1SMitchell Horne.Po
14783a426d1SMitchell Hornefor example with a 'show foo' command in
14883a426d1SMitchell Horne.Xr ddb 4
14983a426d1SMitchell Horne.Pc .
15083a426d1SMitchell Horne.Pp
15187132d1dSMitchell HorneThe assertion
15287132d1dSMitchell Horne.Bd -literal -offset indent
15387132d1dSMitchell HorneMPASS(td == curthread);
15487132d1dSMitchell Horne.Ed
15587132d1dSMitchell Horne.Pp
15687132d1dSMitchell Hornelocated on line 87 of a file named foo.c would generate the following panic
15787132d1dSMitchell Hornemessage:
15887132d1dSMitchell Horne.Bd -literal -offset indent
15987132d1dSMitchell Hornepanic: Assertion td == curthread failed at foo.c:87
16087132d1dSMitchell Horne.Ed
16183a426d1SMitchell Horne.Pp
16283a426d1SMitchell HorneThis is a simple condition, and the message provides enough information to
16383a426d1SMitchell Horneinvestigate the failure.
16483a426d1SMitchell Horne.Pp
16583a426d1SMitchell HorneThe assertion
16683a426d1SMitchell Horne.Bd -literal -offset indent
16783a426d1SMitchell HorneMPASS(td == curthread && (sz >= SIZE_MIN && sz <= SIZE_MAX));
16883a426d1SMitchell Horne.Ed
16983a426d1SMitchell Horne.Pp
17083a426d1SMitchell Horneis
17183a426d1SMitchell Horne.Em NOT
17283a426d1SMitchell Horneuseful enough.
17383a426d1SMitchell HorneThe message doesn't indicate which part of the assertion was violated, nor
17483a426d1SMitchell Hornedoes it report the value of
17583a426d1SMitchell Horne.Dv sz ,
17683a426d1SMitchell Hornewhich may be critical to understanding
17783a426d1SMitchell Horne.Em why
17883a426d1SMitchell Hornethe assertion failed.
17983a426d1SMitchell Horne.Pp
18083a426d1SMitchell HorneAccording to the guidelines above, this would be correctly expressed as:
18183a426d1SMitchell Horne.Bd -literal -offset indent
18283a426d1SMitchell HorneMPASS(td == curthread);
18383a426d1SMitchell HorneKASSERT(sz >= SIZE_MIN && sz <= SIZE_MAX,
18483a426d1SMitchell Horne    ("invalid size argument: %u", sz));
18583a426d1SMitchell Horne.Ed
186*5d956e11SMitchell Horne.Sh HISTORY
187*5d956e11SMitchell HorneThe
188*5d956e11SMitchell Horne.Nm MPASS
189*5d956e11SMitchell Hornemacro first appeared in
190*5d956e11SMitchell Horne.Bsx
191*5d956e11SMitchell Horneand was imported into
192*5d956e11SMitchell Horne.Fx 5.0 .
193*5d956e11SMitchell HorneThe name originates as an acronym of "multi-processor assert", but has evolved
194*5d956e11SMitchell Horneto mean "must pass", or "must-pass assert".
1956fc639dcSJonathan M. Bresler.Sh SEE ALSO
1968abb3721SJeroen Ruigrok van der Werven.Xr panic 9
1976fc639dcSJonathan M. Bresler.Sh AUTHORS
198eb2630afSJeroen Ruigrok van der WervenThis manual page was written by
19987132d1dSMitchell Horne.An Jonathan M. Bresler Aq Mt jmb@FreeBSD.org
20087132d1dSMitchell Horneand
20187132d1dSMitchell Horne.An Mitchell Horne Aq Mt mhorne@FreeBSD.org .
202