xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/assert.h (revision fa38579f317d5c2ff2926fab9b12ee6d429bd155)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or https://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include_next <assert.h>
29 
30 #ifndef _LIBSPL_ASSERT_H
31 #define	_LIBSPL_ASSERT_H
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <sys/types.h>
37 
38 /* Workaround for non-Clang compilers */
39 #ifndef __has_feature
40 #define	__has_feature(x) 0
41 #endif
42 
43 /* We need to workaround libspl_set_assert_ok() that we have for zdb */
44 #if __has_feature(attribute_analyzer_noreturn) || defined(__COVERITY__)
45 #define	NORETURN	__attribute__((__noreturn__))
46 #else
47 #define	NORETURN
48 #endif
49 
50 /* Set to non-zero to avoid abort()ing on an assertion failure */
51 extern void libspl_set_assert_ok(boolean_t val);
52 
53 /* printf version of libspl_assert */
54 extern void libspl_assertf(const char *file, const char *func, int line,
55     const char *format, ...) NORETURN __attribute__((format(printf, 4, 5)));
56 
57 static inline int
58 libspl_assert(const char *buf, const char *file, const char *func, int line)
59 {
60 	libspl_assertf(file, func, line, "%s", buf);
61 	return (0);
62 }
63 
64 #ifdef verify
65 #undef verify
66 #endif
67 
68 #define	PANIC(fmt, a...)						\
69 	libspl_assertf(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
70 
71 #define	VERIFY(cond)							\
72 	(void) ((!(cond)) &&						\
73 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
74 
75 #define	VERIFYF(cond, STR, ...)						\
76 do {									\
77 	if (!(cond))							\
78 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
79 		    "%s " STR, #cond,					\
80 		    __VA_ARGS__);					\
81 } while (0)
82 
83 #define	verify(cond)							\
84 	(void) ((!(cond)) &&						\
85 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
86 
87 #define	VERIFY3B(LEFT, OP, RIGHT)					\
88 do {									\
89 	const boolean_t __left = (boolean_t)(LEFT);			\
90 	const boolean_t __right = (boolean_t)(RIGHT);			\
91 	if (!(__left OP __right))					\
92 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
93 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
94 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
95 } while (0)
96 
97 #define	VERIFY3S(LEFT, OP, RIGHT)					\
98 do {									\
99 	const int64_t __left = (int64_t)(LEFT);				\
100 	const int64_t __right = (int64_t)(RIGHT);			\
101 	if (!(__left OP __right))					\
102 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
103 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
104 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
105 } while (0)
106 
107 #define	VERIFY3U(LEFT, OP, RIGHT)					\
108 do {									\
109 	const uint64_t __left = (uint64_t)(LEFT);			\
110 	const uint64_t __right = (uint64_t)(RIGHT);			\
111 	if (!(__left OP __right))					\
112 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
113 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
114 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
115 } while (0)
116 
117 #define	VERIFY3P(LEFT, OP, RIGHT)					\
118 do {									\
119 	const uintptr_t __left = (uintptr_t)(LEFT);			\
120 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
121 	if (!(__left OP __right))					\
122 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
123 		    "%s %s %s (%p %s %p)", #LEFT, #OP, #RIGHT,		\
124 		    (void *)__left, #OP, (void *)__right);		\
125 } while (0)
126 
127 #define	VERIFY0(LEFT)							\
128 do {									\
129 	const uint64_t __left = (uint64_t)(LEFT);			\
130 	if (!(__left == 0))						\
131 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
132 		    "%s == 0 (0x%llx == 0)", #LEFT,			\
133 		    (u_longlong_t)__left);				\
134 } while (0)
135 
136 #define	VERIFY0P(LEFT)							\
137 do {									\
138 	const uintptr_t __left = (uintptr_t)(LEFT);			\
139 	if (!(__left == 0))						\
140 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
141 		    "%s == 0 (%p == 0)", #LEFT,				\
142 		    (void *)__left);					\
143 } while (0)
144 
145 /*
146  * This is just here because cstyle gets upset about #LEFT
147  * on a newline.
148  */
149 
150 /* BEGIN CSTYLED */
151 #define	VERIFY3BF(LEFT, OP, RIGHT, STR, ...)				\
152 do {									\
153 	const boolean_t __left = (boolean_t)(LEFT);			\
154 	const boolean_t __right = (boolean_t)(RIGHT);			\
155 	if (!(__left OP __right))					\
156 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
157 		    "%s %s %s (0x%llx %s 0x%llx) " STR,			\
158 		    #LEFT, #OP, #RIGHT,					\
159 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right,	\
160 		    __VA_ARGS__);					\
161 } while (0)
162 
163 #define	VERIFY3SF(LEFT, OP, RIGHT, STR, ...)				\
164 do {									\
165 	const int64_t __left = (int64_t)(LEFT);				\
166 	const int64_t __right = (int64_t)(RIGHT);			\
167 	if (!(__left OP __right))					\
168 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
169 		    "%s %s %s (0x%llx %s 0x%llx) " STR,			\
170 		    #LEFT, #OP, #RIGHT,					\
171 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right,	\
172 		    __VA_ARGS__);					\
173 } while (0)
174 
175 #define	VERIFY3UF(LEFT, OP, RIGHT, STR, ...)				\
176 do {									\
177 	const uint64_t __left = (uint64_t)(LEFT);			\
178 	const uint64_t __right = (uint64_t)(RIGHT);			\
179 	if (!(__left OP __right))					\
180 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
181 		    "%s %s %s (0x%llx %s 0x%llx) " STR,			\
182 		    #LEFT, #OP, #RIGHT,					\
183 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right,	\
184 		    __VA_ARGS__);					\
185 } while (0)
186 
187 #define	VERIFY3PF(LEFT, OP, RIGHT, STR, ...)				\
188 do {									\
189 	const uintptr_t __left = (uintptr_t)(LEFT);			\
190 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
191 	if (!(__left OP __right))					\
192 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
193 		    "%s %s %s (0x%llx %s 0x%llx) " STR,			\
194 		    #LEFT, #OP, #RIGHT,					\
195 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right,	\
196 		    __VA_ARGS__);					\
197 } while (0)
198 /* END CSTYLED */
199 
200 #define	VERIFY0F(LEFT, STR, ...)					\
201 do {									\
202 	const uint64_t __left = (uint64_t)(LEFT);			\
203 	if (!(__left == 0))						\
204 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
205 		    "%s == 0 (0x%llx == 0) " STR, #LEFT,		\
206 		    (u_longlong_t)__left, __VA_ARGS__);			\
207 } while (0)
208 
209 #define	VERIFY0PF(LEFT, STR, ...)					\
210 do {									\
211 	const uintptr_t __left = (uintptr_t)(LEFT);			\
212 	if (!(__left == 0))						\
213 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
214 		    "%s == 0 (%p == 0) " STR, #LEFT,			\
215 		    (u_longlong_t)__left, __VA_ARGS__);			\
216 } while (0)
217 
218 #ifdef assert
219 #undef assert
220 #endif
221 
222 #ifdef NDEBUG
223 #define	ASSERT3B(x, y, z)						\
224 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
225 #define	ASSERT3S(x, y, z)						\
226 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
227 #define	ASSERT3U(x, y, z)						\
228 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
229 #define	ASSERT3P(x, y, z)						\
230 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
231 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
232 #define	ASSERT0P(x)		((void) sizeof ((uintptr_t)(x)))
233 #define	ASSERT3BF(x, y, z, str, ...)	ASSERT3B(x, y, z)
234 #define	ASSERT3SF(x, y, z, str, ...)	ASSERT3S(x, y, z)
235 #define	ASSERT3UF(x, y, z, str, ...)	ASSERT3U(x, y, z)
236 #define	ASSERT3PF(x, y, z, str, ...)	ASSERT3P(x, y, z)
237 #define	ASSERT0P(x)		((void) sizeof ((uintptr_t)(x)))
238 #define	ASSERT0PF(x, str, ...)		ASSERT0P(x)
239 #define	ASSERT0F(x, str, ...)		ASSERT0(x)
240 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
241 #define	ASSERTF(x, str, ...)	ASSERT(x)
242 #define	assert(x)		((void) sizeof ((uintptr_t)(x)))
243 #define	IMPLY(A, B)							\
244 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
245 #define	EQUIV(A, B)							\
246 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
247 #else
248 #define	ASSERT3B	VERIFY3B
249 #define	ASSERT3S	VERIFY3S
250 #define	ASSERT3U	VERIFY3U
251 #define	ASSERT3P	VERIFY3P
252 #define	ASSERT0		VERIFY0
253 #define	ASSERT0P	VERIFY0P
254 #define	ASSERT3BF	VERIFY3BF
255 #define	ASSERT3SF	VERIFY3SF
256 #define	ASSERT3UF	VERIFY3UF
257 #define	ASSERT3PF	VERIFY3PF
258 #define	ASSERT0PF	VERIFY0PF
259 #define	ASSERT0F	VERIFY0F
260 #define	ASSERT		VERIFY
261 #define	ASSERTF		VERIFYF
262 #define	assert		VERIFY
263 #define	IMPLY(A, B) \
264 	((void)(((!(A)) || (B)) || \
265 	    libspl_assert("(" #A ") implies (" #B ")", \
266 	    __FILE__, __FUNCTION__, __LINE__)))
267 #define	EQUIV(A, B) \
268 	((void)((!!(A) == !!(B)) || \
269 	    libspl_assert("(" #A ") is equivalent to (" #B ")", \
270 	    __FILE__, __FUNCTION__, __LINE__)))
271 
272 #endif  /* NDEBUG */
273 
274 #endif  /* _LIBSPL_ASSERT_H */
275