Files
mirror_zfs/lib/libspl/include/assert.h
T

276 lines
8.7 KiB
C
Raw Normal View History

2025-01-04 11:04:27 +11:00
// SPDX-License-Identifier: CDDL-1.0
2010-08-26 11:50:56 -07:00
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2022-07-11 23:16:13 +02:00
* or https://opensource.org/licenses/CDDL-1.0.
2010-08-26 11:50:56 -07:00
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include_next <assert.h>
#ifndef _LIBSPL_ASSERT_H
2013-11-01 20:26:11 +01:00
#define _LIBSPL_ASSERT_H
2010-08-26 11:50:56 -07:00
#include <stdio.h>
#include <stdlib.h>
2017-07-06 00:39:13 +07:00
#include <stdarg.h>
#include <sys/types.h>
2010-08-26 11:50:56 -07:00
/* Workaround for non-Clang compilers */
#ifndef __has_feature
#define __has_feature(x) 0
#endif
/* We need to workaround libspl_set_assert_ok() that we have for zdb */
#if __has_feature(attribute_analyzer_noreturn) || defined(__COVERITY__)
#define NORETURN __attribute__((__noreturn__))
#else
#define NORETURN
#endif
2020-07-04 18:24:13 -04:00
/* Set to non-zero to avoid abort()ing on an assertion failure */
extern void libspl_set_assert_ok(boolean_t val);
2020-07-04 18:24:13 -04:00
/* printf version of libspl_assert */
extern void libspl_assertf(const char *file, const char *func, int line,
const char *format, ...) NORETURN __attribute__((format(printf, 4, 5)));
2019-11-27 13:45:56 -05:00
2016-03-01 15:45:43 +01:00
static inline int
libspl_assert(const char *buf, const char *file, const char *func, int line)
2010-08-26 11:50:56 -07:00
{
2020-07-04 18:24:13 -04:00
libspl_assertf(file, func, line, "%s", buf);
return (0);
2017-06-28 10:05:16 -07:00
}
2016-03-01 15:45:43 +01:00
#ifdef verify
#undef verify
#endif
2010-08-26 11:50:56 -07:00
2023-12-01 14:50:10 -05:00
#define PANIC(fmt, a...) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
2016-03-01 15:45:43 +01:00
#define VERIFY(cond) \
(void) ((!(cond)) && \
libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
2024-04-10 16:30:25 -04:00
#define VERIFYF(cond, STR, ...) \
do { \
if (!(cond)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
"%s " STR, #cond, \
__VA_ARGS__); \
} while (0)
2016-03-01 15:45:43 +01:00
#define verify(cond) \
(void) ((!(cond)) && \
libspl_assert(#cond, __FILE__, __FUNCTION__, __LINE__))
2010-08-26 11:50:56 -07:00
2018-10-03 20:16:45 -07:00
#define VERIFY3B(LEFT, OP, RIGHT) \
2016-03-01 15:45:43 +01:00
do { \
2025-04-16 12:01:32 -04:00
const boolean_t __left = (boolean_t)!!(LEFT); \
const boolean_t __right = (boolean_t)!!(RIGHT); \
2017-06-28 10:05:16 -07:00
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3B(%s, %s, %s) failed " \
"(%d %s %d)", #LEFT, #OP, #RIGHT, \
__left, #OP, __right); \
2010-08-26 11:50:56 -07:00
} while (0)
2018-10-03 20:16:45 -07:00
#define VERIFY3S(LEFT, OP, RIGHT) \
do { \
const int64_t __left = (int64_t)(LEFT); \
const int64_t __right = (int64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3S(%s, %s, %s) failed " \
"(%lld %s 0x%lld)", #LEFT, #OP, #RIGHT, \
(longlong_t)__left, #OP, (longlong_t)__right); \
2018-10-03 20:16:45 -07:00
} while (0)
#define VERIFY3U(LEFT, OP, RIGHT) \
do { \
const uint64_t __left = (uint64_t)(LEFT); \
const uint64_t __right = (uint64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3U(%s, %s, %s) failed " \
"(%llu %s %llu)", #LEFT, #OP, #RIGHT, \
2018-10-03 20:16:45 -07:00
(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
} while (0)
#define VERIFY3P(LEFT, OP, RIGHT) \
do { \
const uintptr_t __left = (uintptr_t)(LEFT); \
const uintptr_t __right = (uintptr_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3P(%s, %s, %s) failed " \
"(%p %s %p)", #LEFT, #OP, #RIGHT, \
2023-08-30 17:13:06 +02:00
(void *)__left, #OP, (void *)__right); \
2018-10-03 20:16:45 -07:00
} while (0)
#define VERIFY0(LEFT) \
do { \
const uint64_t __left = (uint64_t)(LEFT); \
if (!(__left == 0)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY0(%s) failed (%lld)", #LEFT, \
2018-10-03 20:16:45 -07:00
(u_longlong_t)__left); \
} while (0)
2010-08-26 11:50:56 -07:00
2023-08-30 17:13:09 +02:00
#define VERIFY0P(LEFT) \
do { \
const uintptr_t __left = (uintptr_t)(LEFT); \
if (!(__left == 0)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY0P(%s) failed (%p)", #LEFT, \
2023-08-30 17:13:09 +02:00
(void *)__left); \
} while (0)
2024-04-10 16:30:25 -04:00
/*
* This is just here because cstyle gets upset about #LEFT
* on a newline.
*/
/* BEGIN CSTYLED */
#define VERIFY3BF(LEFT, OP, RIGHT, STR, ...) \
do { \
2025-04-16 12:01:32 -04:00
const boolean_t __left = (boolean_t)!!(LEFT); \
const boolean_t __right = (boolean_t)!!(RIGHT); \
2024-04-10 16:30:25 -04:00
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3B(%s, %s, %s) failed " \
"(%d %s %d) " STR, #LEFT, #OP, #RIGHT, \
__left, #OP, __right, \
2024-04-10 16:30:25 -04:00
__VA_ARGS__); \
} while (0)
#define VERIFY3SF(LEFT, OP, RIGHT, STR, ...) \
do { \
const int64_t __left = (int64_t)(LEFT); \
const int64_t __right = (int64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3S(%s, %s, %s) failed " \
"(%lld %s %lld) " STR, #LEFT, #OP, #RIGHT, \
(longlong_t)__left, #OP, (longlong_t)__right, \
2024-04-10 16:30:25 -04:00
__VA_ARGS__); \
} while (0)
#define VERIFY3UF(LEFT, OP, RIGHT, STR, ...) \
do { \
const uint64_t __left = (uint64_t)(LEFT); \
const uint64_t __right = (uint64_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3U(%s, %s, %s) failed " \
"(%llu %s %llu) " STR, #LEFT, #OP, #RIGHT, \
2024-04-10 16:30:25 -04:00
(u_longlong_t)__left, #OP, (u_longlong_t)__right, \
__VA_ARGS__); \
} while (0)
#define VERIFY3PF(LEFT, OP, RIGHT, STR, ...) \
do { \
const uintptr_t __left = (uintptr_t)(LEFT); \
const uintptr_t __right = (uintptr_t)(RIGHT); \
if (!(__left OP __right)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY3P(%s, %s, %s) failed " \
"(%p %s %p) " STR, #LEFT, #OP, #RIGHT, \
(void *)__left, #OP, (void *)__right, \
2024-04-10 16:30:25 -04:00
__VA_ARGS__); \
} while (0)
/* END CSTYLED */
#define VERIFY0F(LEFT, STR, ...) \
do { \
2025-04-16 12:01:32 -04:00
const int64_t __left = (int64_t)(LEFT); \
2024-04-10 16:30:25 -04:00
if (!(__left == 0)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY0(%s) failed (%lld) " STR, #LEFT, \
(longlong_t)__left, __VA_ARGS__); \
2024-04-10 16:30:25 -04:00
} while (0)
#define VERIFY0PF(LEFT, STR, ...) \
do { \
const uintptr_t __left = (uintptr_t)(LEFT); \
if (!(__left == 0)) \
libspl_assertf(__FILE__, __FUNCTION__, __LINE__, \
2025-04-16 12:01:32 -04:00
"VERIFY0P(%s) failed (%p) " STR, #LEFT, \
(void *)__left, __VA_ARGS__); \
2024-04-10 16:30:25 -04:00
} while (0)
2016-03-01 15:45:43 +01:00
#ifdef assert
#undef assert
#endif
2010-08-26 11:50:56 -07:00
#ifdef NDEBUG
2022-01-25 02:05:42 +01:00
#define ASSERT3B(x, y, z) \
((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
#define ASSERT3S(x, y, z) \
((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
#define ASSERT3U(x, y, z) \
((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
#define ASSERT3P(x, y, z) \
((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
#define ASSERT0(x) ((void) sizeof ((uintptr_t)(x)))
2023-08-30 17:13:09 +02:00
#define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x)))
2024-04-10 16:30:25 -04:00
#define ASSERT3BF(x, y, z, str, ...) ASSERT3B(x, y, z)
#define ASSERT3SF(x, y, z, str, ...) ASSERT3S(x, y, z)
#define ASSERT3UF(x, y, z, str, ...) ASSERT3U(x, y, z)
#define ASSERT3PF(x, y, z, str, ...) ASSERT3P(x, y, z)
#define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x)))
#define ASSERT0PF(x, str, ...) ASSERT0P(x)
#define ASSERT0F(x, str, ...) ASSERT0(x)
2022-01-25 02:05:42 +01:00
#define ASSERT(x) ((void) sizeof ((uintptr_t)(x)))
2024-04-10 16:30:25 -04:00
#define ASSERTF(x, str, ...) ASSERT(x)
2022-01-25 02:05:42 +01:00
#define assert(x) ((void) sizeof ((uintptr_t)(x)))
#define IMPLY(A, B) \
((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
#define EQUIV(A, B) \
((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
2010-08-26 11:50:56 -07:00
#else
2018-10-03 20:16:45 -07:00
#define ASSERT3B VERIFY3B
#define ASSERT3S VERIFY3S
#define ASSERT3U VERIFY3U
#define ASSERT3P VERIFY3P
#define ASSERT0 VERIFY0
2023-08-30 17:13:09 +02:00
#define ASSERT0P VERIFY0P
2024-04-10 16:30:25 -04:00
#define ASSERT3BF VERIFY3BF
#define ASSERT3SF VERIFY3SF
#define ASSERT3UF VERIFY3UF
#define ASSERT3PF VERIFY3PF
#define ASSERT0PF VERIFY0PF
#define ASSERT0F VERIFY0F
2018-10-03 20:16:45 -07:00
#define ASSERT VERIFY
2024-04-10 16:30:25 -04:00
#define ASSERTF VERIFYF
2018-10-03 20:16:45 -07:00
#define assert VERIFY
2015-06-24 13:54:06 -07:00
#define IMPLY(A, B) \
((void)(((!(A)) || (B)) || \
2016-03-01 15:45:43 +01:00
libspl_assert("(" #A ") implies (" #B ")", \
__FILE__, __FUNCTION__, __LINE__)))
2025-04-16 12:01:32 -04:00
#define EQUIV(A, B) VERIFY3B(A, ==, B)
2015-06-24 13:54:06 -07:00
2010-08-26 11:50:56 -07:00
#endif /* NDEBUG */
#endif /* _LIBSPL_ASSERT_H */