xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/assert.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include_next <assert.h>
28 
29 #ifndef _LIBSPL_ASSERT_H
30 #define	_LIBSPL_ASSERT_H
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 
37 /* Workaround for non-Clang compilers */
38 #ifndef __has_feature
39 #define	__has_feature(x) 0
40 #endif
41 
42 /* We need to workaround libspl_set_assert_ok() that we have for zdb */
43 #if __has_feature(attribute_analyzer_noreturn) || defined(__COVERITY__)
44 #define	NORETURN	__attribute__((__noreturn__))
45 #else
46 #define	NORETURN
47 #endif
48 
49 /* Set to non-zero to avoid abort()ing on an assertion failure */
50 extern void libspl_set_assert_ok(boolean_t val);
51 
52 /* printf version of libspl_assert */
53 extern void libspl_assertf(const char *file, const char *func, int line,
54     const char *format, ...) NORETURN __attribute__((format(printf, 4, 5)));
55 
56 static inline int
57 libspl_assert(const char *buf, const char *file, const char *func, int line)
58 {
59 	libspl_assertf(file, func, line, "%s", buf);
60 	return (0);
61 }
62 
63 #ifdef verify
64 #undef verify
65 #endif
66 
67 #define	PANIC(fmt, a...)						\
68 	libspl_assertf(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
69 
70 #define	VERIFY(cond)							\
71 	(void) ((!(cond)) &&						\
72 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
73 #define	verify(cond)							\
74 	(void) ((!(cond)) &&						\
75 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
76 
77 #define	VERIFY3B(LEFT, OP, RIGHT)					\
78 do {									\
79 	const boolean_t __left = (boolean_t)(LEFT);			\
80 	const boolean_t __right = (boolean_t)(RIGHT);			\
81 	if (!(__left OP __right))					\
82 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
83 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
84 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
85 } while (0)
86 
87 #define	VERIFY3S(LEFT, OP, RIGHT)					\
88 do {									\
89 	const int64_t __left = (int64_t)(LEFT);				\
90 	const int64_t __right = (int64_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	VERIFY3U(LEFT, OP, RIGHT)					\
98 do {									\
99 	const uint64_t __left = (uint64_t)(LEFT);			\
100 	const uint64_t __right = (uint64_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	VERIFY3P(LEFT, OP, RIGHT)					\
108 do {									\
109 	const uintptr_t __left = (uintptr_t)(LEFT);			\
110 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
111 	if (!(__left OP __right))					\
112 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
113 		    "%s %s %s (%p %s %p)", #LEFT, #OP, #RIGHT,		\
114 		    (void *)__left, #OP, (void *)__right);		\
115 } while (0)
116 
117 #define	VERIFY0(LEFT)							\
118 do {									\
119 	const uint64_t __left = (uint64_t)(LEFT);			\
120 	if (!(__left == 0))						\
121 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
122 		    "%s == 0 (0x%llx == 0)", #LEFT,			\
123 		    (u_longlong_t)__left);				\
124 } while (0)
125 
126 #define	VERIFY0P(LEFT)							\
127 do {									\
128 	const uintptr_t __left = (uintptr_t)(LEFT);			\
129 	if (!(__left == 0))						\
130 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
131 		    "%s == 0 (%p == 0)", #LEFT,				\
132 		    (void *)__left);					\
133 } while (0)
134 
135 #ifdef assert
136 #undef assert
137 #endif
138 
139 #ifdef NDEBUG
140 #define	ASSERT3B(x, y, z)						\
141 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
142 #define	ASSERT3S(x, y, z)						\
143 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
144 #define	ASSERT3U(x, y, z)						\
145 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
146 #define	ASSERT3P(x, y, z)						\
147 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
148 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
149 #define	ASSERT0P(x)		((void) sizeof ((uintptr_t)(x)))
150 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
151 #define	assert(x)		((void) sizeof ((uintptr_t)(x)))
152 #define	IMPLY(A, B)							\
153 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
154 #define	EQUIV(A, B)							\
155 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
156 #else
157 #define	ASSERT3B	VERIFY3B
158 #define	ASSERT3S	VERIFY3S
159 #define	ASSERT3U	VERIFY3U
160 #define	ASSERT3P	VERIFY3P
161 #define	ASSERT0		VERIFY0
162 #define	ASSERT0P	VERIFY0P
163 #define	ASSERT		VERIFY
164 #define	assert		VERIFY
165 #define	IMPLY(A, B) \
166 	((void)(((!(A)) || (B)) || \
167 	    libspl_assert("(" #A ") implies (" #B ")", \
168 	    __FILE__, __FUNCTION__, __LINE__)))
169 #define	EQUIV(A, B) \
170 	((void)((!!(A) == !!(B)) || \
171 	    libspl_assert("(" #A ") is equivalent to (" #B ")", \
172 	    __FILE__, __FUNCTION__, __LINE__)))
173 
174 #endif  /* NDEBUG */
175 
176 #endif  /* _LIBSPL_ASSERT_H */
177