xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/assert.h (revision c7a063741720ef81d4caa4613242579d12f1d605)
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	VERIFY(cond)							\
68 	(void) ((!(cond)) &&						\
69 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
70 #define	verify(cond)							\
71 	(void) ((!(cond)) &&						\
72 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
73 
74 #define	VERIFY3B(LEFT, OP, RIGHT)					\
75 do {									\
76 	const boolean_t __left = (boolean_t)(LEFT);			\
77 	const boolean_t __right = (boolean_t)(RIGHT);			\
78 	if (!(__left OP __right))					\
79 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
80 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
81 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
82 } while (0)
83 
84 #define	VERIFY3S(LEFT, OP, RIGHT)					\
85 do {									\
86 	const int64_t __left = (int64_t)(LEFT);				\
87 	const int64_t __right = (int64_t)(RIGHT);			\
88 	if (!(__left OP __right))					\
89 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
90 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
91 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
92 } while (0)
93 
94 #define	VERIFY3U(LEFT, OP, RIGHT)					\
95 do {									\
96 	const uint64_t __left = (uint64_t)(LEFT);			\
97 	const uint64_t __right = (uint64_t)(RIGHT);			\
98 	if (!(__left OP __right))					\
99 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
100 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
101 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
102 } while (0)
103 
104 #define	VERIFY3P(LEFT, OP, RIGHT)					\
105 do {									\
106 	const uintptr_t __left = (uintptr_t)(LEFT);			\
107 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
108 	if (!(__left OP __right))					\
109 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
110 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
111 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
112 } while (0)
113 
114 #define	VERIFY0(LEFT)							\
115 do {									\
116 	const uint64_t __left = (uint64_t)(LEFT);			\
117 	if (!(__left == 0))						\
118 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
119 		    "%s == 0 (0x%llx == 0)", #LEFT,			\
120 		    (u_longlong_t)__left);				\
121 } while (0)
122 
123 #ifdef assert
124 #undef assert
125 #endif
126 
127 #ifdef NDEBUG
128 #define	ASSERT3B(x, y, z)						\
129 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
130 #define	ASSERT3S(x, y, z)						\
131 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
132 #define	ASSERT3U(x, y, z)						\
133 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
134 #define	ASSERT3P(x, y, z)						\
135 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
136 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
137 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
138 #define	assert(x)		((void) sizeof ((uintptr_t)(x)))
139 #define	IMPLY(A, B)							\
140 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
141 #define	EQUIV(A, B)							\
142 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
143 #else
144 #define	ASSERT3B	VERIFY3B
145 #define	ASSERT3S	VERIFY3S
146 #define	ASSERT3U	VERIFY3U
147 #define	ASSERT3P	VERIFY3P
148 #define	ASSERT0		VERIFY0
149 #define	ASSERT		VERIFY
150 #define	assert		VERIFY
151 #define	IMPLY(A, B) \
152 	((void)(((!(A)) || (B)) || \
153 	    libspl_assert("(" #A ") implies (" #B ")", \
154 	    __FILE__, __FUNCTION__, __LINE__)))
155 #define	EQUIV(A, B) \
156 	((void)((!!(A) == !!(B)) || \
157 	    libspl_assert("(" #A ") is equivalent to (" #B ")", \
158 	    __FILE__, __FUNCTION__, __LINE__)))
159 
160 #endif  /* NDEBUG */
161 
162 #endif  /* _LIBSPL_ASSERT_H */
163