xref: /freebsd/share/man/man9/KASSERT.9 (revision 83a426d13a6a384e63e75d8252c03dd40af3817e)
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.
5*83a426d1SMitchell 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.\"
32*83a426d1SMitchell 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
96*83a426d1SMitchell Hornethat automatically generates a simple assertion message including file and line
97*83a426d1SMitchell Horneinformation.
98*83a426d1SMitchell Horne.Ss Assertion Guidelines
99*83a426d1SMitchell HorneWhen adding new assertions, keep in mind their primary purpose: to aid in
100*83a426d1SMitchell Horneidentifying and debugging of complex error conditions.
101*83a426d1SMitchell Horne.Pp
102*83a426d1SMitchell HorneThe panic messages resulting from assertion failures should be useful without
103*83a426d1SMitchell Hornethe resulting kernel dump; the message may be included in a bug report, and
104*83a426d1SMitchell Horneshould contain the relevant information needed to discern how the assertion was
105*83a426d1SMitchell Horneviolated.
106*83a426d1SMitchell HorneThis is especially important when the error condition is difficult or
107*83a426d1SMitchell Horneimpossible for the developer to reproduce locally.
108*83a426d1SMitchell Horne.Pp
109*83a426d1SMitchell HorneTherefore, assertions should adhere to the following guidelines:
110*83a426d1SMitchell Horne.Bl -enum
111*83a426d1SMitchell Horne.It
112*83a426d1SMitchell HorneWhenever possible, the value of a runtime variable checked by an assertion
113*83a426d1SMitchell Hornecondition should appear in its message.
114*83a426d1SMitchell Horne.It
115*83a426d1SMitchell HorneUnrelated conditions must appear in separate assertions.
116*83a426d1SMitchell Horne.It
117*83a426d1SMitchell HorneMultiple related conditions should be distinguishable (e.g. by value), or split
118*83a426d1SMitchell Horneinto separate assertions.
119*83a426d1SMitchell Horne.It
120*83a426d1SMitchell HorneWhen in doubt, print more information, not less.
121*83a426d1SMitchell Horne.El
122*83a426d1SMitchell Horne.Pp
123*83a426d1SMitchell HorneCombined, this gives greater clarity into the exact cause of an assertion
124*83a426d1SMitchell Hornepanic; see
125*83a426d1SMitchell Horne.Sx EXAMPLES
126*83a426d1SMitchell 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,
138*83a426d1SMitchell Horne	    ("%s: fp %p is still active, flags=%x", __func__, fp,
139*83a426d1SMitchell Horne	    fp->foo_flags));
1406fc639dcSJonathan M. Bresler	...
1416fc639dcSJonathan M. Bresler}
1426fc639dcSJonathan M. Bresler.Ed
14387132d1dSMitchell Horne.Pp
144*83a426d1SMitchell HorneThis assertion provides the full flag set for the object, as well as the memory
145*83a426d1SMitchell Hornepointer, which may be used by a debugger to examine the object in detail
146*83a426d1SMitchell Horne.Po
147*83a426d1SMitchell Hornefor example with a 'show foo' command in
148*83a426d1SMitchell Horne.Xr ddb 4
149*83a426d1SMitchell Horne.Pc .
150*83a426d1SMitchell 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
161*83a426d1SMitchell Horne.Pp
162*83a426d1SMitchell HorneThis is a simple condition, and the message provides enough information to
163*83a426d1SMitchell Horneinvestigate the failure.
164*83a426d1SMitchell Horne.Pp
165*83a426d1SMitchell HorneThe assertion
166*83a426d1SMitchell Horne.Bd -literal -offset indent
167*83a426d1SMitchell HorneMPASS(td == curthread && (sz >= SIZE_MIN && sz <= SIZE_MAX));
168*83a426d1SMitchell Horne.Ed
169*83a426d1SMitchell Horne.Pp
170*83a426d1SMitchell Horneis
171*83a426d1SMitchell Horne.Em NOT
172*83a426d1SMitchell Horneuseful enough.
173*83a426d1SMitchell HorneThe message doesn't indicate which part of the assertion was violated, nor
174*83a426d1SMitchell Hornedoes it report the value of
175*83a426d1SMitchell Horne.Dv sz ,
176*83a426d1SMitchell Hornewhich may be critical to understanding
177*83a426d1SMitchell Horne.Em why
178*83a426d1SMitchell Hornethe assertion failed.
179*83a426d1SMitchell Horne.Pp
180*83a426d1SMitchell HorneAccording to the guidelines above, this would be correctly expressed as:
181*83a426d1SMitchell Horne.Bd -literal -offset indent
182*83a426d1SMitchell HorneMPASS(td == curthread);
183*83a426d1SMitchell HorneKASSERT(sz >= SIZE_MIN && sz <= SIZE_MAX,
184*83a426d1SMitchell Horne    ("invalid size argument: %u", sz));
185*83a426d1SMitchell Horne.Ed
1866fc639dcSJonathan M. Bresler.Sh SEE ALSO
1878abb3721SJeroen Ruigrok van der Werven.Xr panic 9
1886fc639dcSJonathan M. Bresler.Sh AUTHORS
189eb2630afSJeroen Ruigrok van der WervenThis manual page was written by
19087132d1dSMitchell Horne.An Jonathan M. Bresler Aq Mt jmb@FreeBSD.org
19187132d1dSMitchell Horneand
19287132d1dSMitchell Horne.An Mitchell Horne Aq Mt mhorne@FreeBSD.org .
193