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
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include_next <assert.h>
29 #ifndef _LIBSPL_ASSERT_H
30 #define _LIBSPL_ASSERT_H
35 #include <sys/types.h>
37 /* Workaround for non-Clang compilers */
39 #define __has_feature(x) 0
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__))
49 /* Set to non-zero to avoid abort()ing on an assertion failure */
50 extern void libspl_set_assert_ok(boolean_t val
);
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)));
57 libspl_assert(const char *buf
, const char *file
, const char *func
, int line
)
59 libspl_assertf(file
, func
, line
, "%s", buf
);
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__))
74 #define VERIFY3B(LEFT, OP, RIGHT) \
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); \
84 #define VERIFY3S(LEFT, OP, RIGHT) \
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); \
94 #define VERIFY3U(LEFT, OP, RIGHT) \
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); \
104 #define VERIFY3P(LEFT, OP, RIGHT) \
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); \
114 #define VERIFY0(LEFT) \
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); \
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)))
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__)))
162 #endif /* _LIBSPL_ASSERT_H */