| 1 |
|
| 2 | #ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H
|
| 3 | #define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H
|
| 4 |
|
| 5 | #include <stdio.h>
|
| 6 | #include <stdlib.h>
|
| 7 | #include <string.h>
|
| 8 | #include <sys/ioctl.h>
|
| 9 |
|
| 10 | #include "../../../../kselftest.h"
|
| 11 |
|
| 12 | #define VFIO_LOG_AND_EXIT(...) do { \
|
| 13 | fprintf(stderr, " " __VA_ARGS__); \
|
| 14 | fprintf(stderr, "\n"); \
|
| 15 | exit(KSFT_FAIL); \
|
| 16 | } while (0)
|
| 17 |
|
| 18 | #define VFIO_ASSERT_OP(_lhs, _rhs, _op, ...) do { \
|
| 19 | typeof(_lhs) __lhs = (_lhs); \
|
| 20 | typeof(_rhs) __rhs = (_rhs); \
|
| 21 | \
|
| 22 | if (__lhs _op __rhs) \
|
| 23 | break; \
|
| 24 | \
|
| 25 | fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__); \
|
| 26 | fprintf(stderr, " Expression: " #_lhs " " #_op " " #_rhs "\n"); \
|
| 27 | fprintf(stderr, " Observed: %#lx %s %#lx\n", \
|
| 28 | (u64)__lhs, #_op, (u64)__rhs); \
|
| 29 | fprintf(stderr, " [errno: %d - %s]\n", errno, strerror(errno)); \
|
| 30 | VFIO_LOG_AND_EXIT(__VA_ARGS__); \
|
| 31 | } while (0)
|
| 32 |
|
| 33 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
|
| 34 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
|
| 35 | #define VFIO_ASSERT_LT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__)
|
| 36 | #define VFIO_ASSERT_LE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__)
|
| 37 | #define VFIO_ASSERT_GT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__)
|
| 38 | #define VFIO_ASSERT_GE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__)
|
| 39 | #define VFIO_ASSERT_TRUE(_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__)
|
| 40 | #define VFIO_ASSERT_FALSE(_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__)
|
| 41 | #define VFIO_ASSERT_NULL(_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__)
|
| 42 | #define VFIO_ASSERT_NOT_NULL(_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__)
|
| 43 |
|
| 44 | #define VFIO_FAIL(_fmt, ...) do { \
|
| 45 | fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__); \
|
| 46 | VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \
|
| 47 | } while (0)
|
| 48 |
|
| 49 | #define malloc_assert(_size) ({ \
|
| 50 | size_t __size = (_size); \
|
| 51 | void *__ptr = malloc(__size); \
|
| 52 | VFIO_ASSERT_NOT_NULL(__ptr, "malloc(%zu) failed", \
|
| 53 | __size); \
|
| 54 | __ptr; \
|
| 55 | })
|
| 56 |
|
| 57 | #define calloc_assert(_nmemb, _size) ({ \
|
| 58 | size_t __nmemb = (_nmemb); \
|
| 59 | size_t __size = (_size); \
|
| 60 | void *__ptr = calloc(__nmemb, __size); \
|
| 61 | VFIO_ASSERT_NOT_NULL(__ptr, "calloc(%zu, %zu) failed", \
|
| 62 | __nmemb, __size); \
|
| 63 | __ptr; \
|
| 64 | })
|
| 65 |
|
| 66 | #define ioctl_assert(_fd, _op, _arg) do { \
|
| 67 | void *__arg = (_arg); \
|
| 68 | int __ret = ioctl((_fd), (_op), (__arg)); \
|
| 69 | VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
|
| 70 | } while (0)
|
| 71 |
|
| 72 | #define snprintf_assert(_s, _size, _fmt, ...) do { \
|
| 73 | int __ret = snprintf(_s, _size, _fmt, ##__VA_ARGS__); \
|
| 74 | VFIO_ASSERT_LT(__ret, _size); \
|
| 75 | } while (0)
|
| 76 |
|
| 77 | #endif
|