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 (%p %s %p)", #LEFT, #OP, #RIGHT, \ 111 (void *)__left, #OP, (void *)__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 #define VERIFY0P(LEFT) \ 124 do { \ 125 const uintptr_t __left = (uintptr_t)(LEFT); \ 126 if (!(__left == 0)) \ 127 libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \ 128 "%s == 0 (%p == 0)", #LEFT, \ 129 (void *)__left); \ 130 } while (0) 131 132 #ifdef assert 133 #undef assert 134 #endif 135 136 #ifdef NDEBUG 137 #define ASSERT3B(x, y, z) \ 138 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 139 #define ASSERT3S(x, y, z) \ 140 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 141 #define ASSERT3U(x, y, z) \ 142 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 143 #define ASSERT3P(x, y, z) \ 144 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 145 #define ASSERT0(x) ((void) sizeof ((uintptr_t)(x))) 146 #define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x))) 147 #define ASSERT(x) ((void) sizeof ((uintptr_t)(x))) 148 #define assert(x) ((void) sizeof ((uintptr_t)(x))) 149 #define IMPLY(A, B) \ 150 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 151 #define EQUIV(A, B) \ 152 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 153 #else 154 #define ASSERT3B VERIFY3B 155 #define ASSERT3S VERIFY3S 156 #define ASSERT3U VERIFY3U 157 #define ASSERT3P VERIFY3P 158 #define ASSERT0 VERIFY0 159 #define ASSERT0P VERIFY0P 160 #define ASSERT VERIFY 161 #define assert VERIFY 162 #define IMPLY(A, B) \ 163 ((void)(((!(A)) || (B)) || \ 164 libspl_assert("(" #A ") implies (" #B ")", \ 165 __FILE__, __FUNCTION__, __LINE__))) 166 #define EQUIV(A, B) \ 167 ((void)((!!(A) == !!(B)) || \ 168 libspl_assert("(" #A ") is equivalent to (" #B ")", \ 169 __FILE__, __FUNCTION__, __LINE__))) 170 171 #endif /* NDEBUG */ 172 173 #endif /* _LIBSPL_ASSERT_H */ 174