xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/assert.h (revision b197d4b893974c9eb4d7b38704c6d5c486235d6f)
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 /* Set to non-zero to avoid abort()ing on an assertion failure */
38 extern void libspl_set_assert_ok(boolean_t val);
39 
40 /* printf version of libspl_assert */
41 extern void libspl_assertf(const char *file, const char *func, int line,
42     const char *format, ...);
43 
44 static inline int
45 libspl_assert(const char *buf, const char *file, const char *func, int line)
46 {
47 	libspl_assertf(file, func, line, "%s", buf);
48 	return (0);
49 }
50 
51 #ifdef verify
52 #undef verify
53 #endif
54 
55 #define	VERIFY(cond)							\
56 	(void) ((!(cond)) &&						\
57 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
58 #define	verify(cond)							\
59 	(void) ((!(cond)) &&						\
60 	    libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
61 
62 #define	VERIFY3B(LEFT, OP, RIGHT)					\
63 do {									\
64 	const boolean_t __left = (boolean_t)(LEFT);			\
65 	const boolean_t __right = (boolean_t)(RIGHT);			\
66 	if (!(__left OP __right))					\
67 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
68 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
69 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
70 } while (0)
71 
72 #define	VERIFY3S(LEFT, OP, RIGHT)					\
73 do {									\
74 	const int64_t __left = (int64_t)(LEFT);				\
75 	const int64_t __right = (int64_t)(RIGHT);			\
76 	if (!(__left OP __right))					\
77 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
78 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
79 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
80 } while (0)
81 
82 #define	VERIFY3U(LEFT, OP, RIGHT)					\
83 do {									\
84 	const uint64_t __left = (uint64_t)(LEFT);			\
85 	const uint64_t __right = (uint64_t)(RIGHT);			\
86 	if (!(__left OP __right))					\
87 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
88 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
89 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
90 } while (0)
91 
92 #define	VERIFY3P(LEFT, OP, RIGHT)					\
93 do {									\
94 	const uintptr_t __left = (uintptr_t)(LEFT);			\
95 	const uintptr_t __right = (uintptr_t)(RIGHT);			\
96 	if (!(__left OP __right))					\
97 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
98 		    "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT,	\
99 		    (u_longlong_t)__left, #OP, (u_longlong_t)__right);	\
100 } while (0)
101 
102 #define	VERIFY0(LEFT)							\
103 do {									\
104 	const uint64_t __left = (uint64_t)(LEFT);			\
105 	if (!(__left == 0))						\
106 		libspl_assertf(__FILE__, __FUNCTION__, __LINE__,	\
107 		    "%s == 0 (0x%llx == 0)", #LEFT,			\
108 		    (u_longlong_t)__left);				\
109 } while (0)
110 
111 #ifdef assert
112 #undef assert
113 #endif
114 
115 #ifdef NDEBUG
116 #define	ASSERT3B(x, y, z)						\
117 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
118 #define	ASSERT3S(x, y, z)						\
119 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
120 #define	ASSERT3U(x, y, z)						\
121 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
122 #define	ASSERT3P(x, y, z)						\
123 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
124 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
125 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
126 #define	assert(x)		((void) sizeof ((uintptr_t)(x)))
127 #define	IMPLY(A, B)							\
128 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
129 #define	EQUIV(A, B)							\
130 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
131 #else
132 #define	ASSERT3B	VERIFY3B
133 #define	ASSERT3S	VERIFY3S
134 #define	ASSERT3U	VERIFY3U
135 #define	ASSERT3P	VERIFY3P
136 #define	ASSERT0		VERIFY0
137 #define	ASSERT		VERIFY
138 #define	assert		VERIFY
139 #define	IMPLY(A, B) \
140 	((void)(((!(A)) || (B)) || \
141 	    libspl_assert("(" #A ") implies (" #B ")", \
142 	    __FILE__, __FUNCTION__, __LINE__)))
143 #define	EQUIV(A, B) \
144 	((void)((!!(A) == !!(B)) || \
145 	    libspl_assert("(" #A ") is equivalent to (" #B ")", \
146 	    __FILE__, __FUNCTION__, __LINE__)))
147 
148 #endif  /* NDEBUG */
149 
150 #endif  /* _LIBSPL_ASSERT_H */
151