| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#if !SD_BOOT |
| 5 |
# include <assert.h> |
| 6 |
#endif |
| 7 |
|
| 8 |
#include "macro.h" |
| 9 |
|
| 10 |
#if SD_BOOT |
| 11 |
_noreturn_ void efi_assert(const char *expr, const char *file, unsigned line, const char *function); |
| 12 |
|
| 13 |
#ifdef NDEBUG |
| 14 |
#define assert(expr) ({ if (!(expr)) __builtin_unreachable(); }) |
| 15 |
#define assert_not_reached() __builtin_unreachable() |
| 16 |
#else |
| 17 |
#define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __func__); }) |
| 18 |
#define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __func__) |
| 19 |
#endif |
| 20 |
#define assert_se(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __func__); }) |
| 21 |
#else |
| 22 |
|
| 23 |
_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func); |
| 24 |
_noreturn_ void log_assert_failed_unreachable(const char *file, int line, const char *func); |
| 25 |
|
| 26 |
#ifdef __COVERITY__ |
| 27 |
|
| 28 |
/* Use special definitions of assertion macros in order to prevent |
| 29 |
* false positives of ASSERT_SIDE_EFFECT on Coverity static analyzer |
| 30 |
* for uses of assert_se() and assert_return(). |
| 31 |
* |
| 32 |
* These definitions make expression go through a (trivial) function |
| 33 |
* call to ensure they are not discarded. Also use ! or !! to ensure |
| 34 |
* the boolean expressions are seen as such. |
| 35 |
* |
| 36 |
* This technique has been described and recommended in: |
| 37 |
* https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects |
| 38 |
*/ |
| 39 |
|
| 40 |
extern void __coverity_panic__(void); |
| 41 |
|
| 42 |
static inline void __coverity_check__(int condition) { |
| 43 |
if (!condition) |
| 44 |
__coverity_panic__(); |
| 45 |
} |
| 46 |
|
| 47 |
static inline int __coverity_check_and_return__(int condition) { |
| 48 |
return condition; |
| 49 |
} |
| 50 |
|
| 51 |
#define assert_message_se(expr, message) __coverity_check__(!!(expr)) |
| 52 |
|
| 53 |
#define assert_log(expr) __coverity_check_and_return__(!!(expr)) |
| 54 |
|
| 55 |
#else /* ! __COVERITY__ */ |
| 56 |
|
| 57 |
#define assert_message_se(expr, message) \ |
| 58 |
do { \ |
| 59 |
if (_unlikely_(!(expr))) \ |
| 60 |
log_assert_failed(message, PROJECT_FILE, __LINE__, __func__); \ |
| 61 |
} while (false) |
| 62 |
|
| 63 |
#endif /* __COVERITY__ */ |
| 64 |
|
| 65 |
#define assert_se(expr) assert_message_se(expr, #expr) |
| 66 |
|
| 67 |
/* We override the glibc assert() here. */ |
| 68 |
#undef assert |
| 69 |
#ifdef NDEBUG |
| 70 |
#define assert(expr) ({ if (!(expr)) __builtin_unreachable(); }) |
| 71 |
#else |
| 72 |
#define assert(expr) assert_message_se(expr, #expr) |
| 73 |
#endif |
| 74 |
|
| 75 |
#define assert_not_reached() \ |
| 76 |
log_assert_failed_unreachable(PROJECT_FILE, __LINE__, __func__) |
| 77 |
|
| 78 |
#endif |
| 79 |
|
| 80 |
/* This passes the argument through after (if asserts are enabled) checking that it is not null. */ |
| 81 |
#define ASSERT_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert) |
| 82 |
#define ASSERT_SE_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert_se) |
| 83 |
#define _ASSERT_PTR(expr, var, check) \ |
| 84 |
({ \ |
| 85 |
typeof(expr) var = (expr); \ |
| 86 |
check(var); \ |
| 87 |
var; \ |
| 88 |
}) |
| 89 |
|
| 90 |
#define ASSERT_NONNEG(expr) \ |
| 91 |
({ \ |
| 92 |
typeof(expr) _expr_ = (expr), _zero = 0; \ |
| 93 |
assert(_expr_ >= _zero); \ |
| 94 |
_expr_; \ |
| 95 |
}) |
| 96 |
|
| 97 |
#define ASSERT_SE_NONNEG(expr) \ |
| 98 |
({ \ |
| 99 |
typeof(expr) _expr_ = (expr), _zero = 0; \ |
| 100 |
assert_se(_expr_ >= _zero); \ |
| 101 |
_expr_; \ |
| 102 |
}) |
| 103 |
|
| 104 |
/* Mark a pointer parameter as intentionally nullable. This is a no-op at runtime but suppresses |
| 105 |
* the coccinelle check-pointer-deref warning for parameters that are safely handled before any |
| 106 |
* dereference (e.g. passed to a NULL-safe helper like iovec_is_set()). */ |
| 107 |
#define POINTER_MAY_BE_NULL(ptr) ({ (void) (ptr); }) |
| 108 |
|
| 109 |
/* sizeof() does not evaluate its argument - it is a compile-time constant expression - so *ptr |
| 110 |
* inside sizeof() is not a real dereference. However, coccinelle cannot distinguish this from an |
| 111 |
* actual dereference, and when sizeof(*ptr) appears in a variable initializer the assert(ptr) that |
| 112 |
* follows cannot come first (declarations must precede statements). Use this macro in place |
| 113 |
* of sizeof() to avoid the false positive - coccinelle sees SIZEOF() as a function call (via |
| 114 |
* parsing_hacks.h) and never looks inside the argument. */ |
| 115 |
#define SIZEOF(x) sizeof(x) |