]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libspl/include/assert.h
ZIO: Add overflow checks for linear buffers
[mirror_zfs.git] / lib / libspl / include / assert.h
CommitLineData
a26baf28
BB
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
1d3ba0bf 10 * or https://opensource.org/licenses/CDDL-1.0.
a26baf28
BB
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
d1d7e268 30#define _LIBSPL_ASSERT_H
a26baf28
BB
31
32#include <stdio.h>
33#include <stdlib.h>
478e3ecf 34#include <stdarg.h>
63652e15 35#include <sys/types.h>
a26baf28 36
55d7afa4
RY
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
b6437ea4 49/* Set to non-zero to avoid abort()ing on an assertion failure */
63652e15 50extern void libspl_set_assert_ok(boolean_t val);
b6437ea4
AS
51
52/* printf version of libspl_assert */
53extern void libspl_assertf(const char *file, const char *func, int line,
55d7afa4 54 const char *format, ...) NORETURN __attribute__((format(printf, 4, 5)));
7c1bf0cf 55
726c4a25
BB
56static inline int
57libspl_assert(const char *buf, const char *file, const char *func, int line)
a26baf28 58{
b6437ea4
AS
59 libspl_assertf(file, func, line, "%s", buf);
60 return (0);
682ce104
TH
61}
62
726c4a25
BB
63#ifdef verify
64#undef verify
65#endif
a26baf28 66
adcea23c
AM
67#define PANIC(fmt, a...) \
68 libspl_assertf(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
69
726c4a25
BB
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__))
a26baf28 76
6e8b2688 77#define VERIFY3B(LEFT, OP, RIGHT) \
726c4a25 78do { \
6e8b2688
PD
79 const boolean_t __left = (boolean_t)(LEFT); \
80 const boolean_t __right = (boolean_t)(RIGHT); \
682ce104
TH
81 if (!(__left OP __right)) \
82 libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
314be68a
TC
83 "%s %s %s (0x%llx %s 0x%llx)", #LEFT, #OP, #RIGHT, \
84 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \
a26baf28 85} while (0)
a26baf28 86
6e8b2688
PD
87#define VERIFY3S(LEFT, OP, RIGHT) \
88do { \
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) \
98do { \
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) \
108do { \
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__, \
01d9283a
DES
113 "%s %s %s (%p %s %p)", #LEFT, #OP, #RIGHT, \
114 (void *)__left, #OP, (void *)__right); \
6e8b2688
PD
115} while (0)
116
117#define VERIFY0(LEFT) \
118do { \
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)
a26baf28 125
506fe78c
DES
126#define VERIFY0P(LEFT) \
127do { \
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
726c4a25
BB
135#ifdef assert
136#undef assert
137#endif
138
a26baf28 139#ifdef NDEBUG
a9e2788f
AZ
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)))
506fe78c 149#define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x)))
a9e2788f
AZ
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)))
a26baf28 156#else
6e8b2688
PD
157#define ASSERT3B VERIFY3B
158#define ASSERT3S VERIFY3S
159#define ASSERT3U VERIFY3U
160#define ASSERT3P VERIFY3P
161#define ASSERT0 VERIFY0
506fe78c 162#define ASSERT0P VERIFY0P
6e8b2688
PD
163#define ASSERT VERIFY
164#define assert VERIFY
fa720217
BB
165#define IMPLY(A, B) \
166 ((void)(((!(A)) || (B)) || \
726c4a25
BB
167 libspl_assert("(" #A ") implies (" #B ")", \
168 __FILE__, __FUNCTION__, __LINE__)))
fa720217
BB
169#define EQUIV(A, B) \
170 ((void)((!!(A) == !!(B)) || \
726c4a25
BB
171 libspl_assert("(" #A ") is equivalent to (" #B ")", \
172 __FILE__, __FUNCTION__, __LINE__)))
fa720217 173
a26baf28
BB
174#endif /* NDEBUG */
175
176#endif /* _LIBSPL_ASSERT_H */