| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#define s2n_likely(x) __builtin_expect(!!(x), 1) |
| 19 |
#define s2n_unlikely(x) __builtin_expect(!!(x), 0) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define __S2N_ENSURE(cond, action) \ |
| 34 |
do { \ |
| 35 |
if (!(cond)) { \ |
| 36 |
action; \ |
| 37 |
} \ |
| 38 |
} while (0) |
| 39 |
|
| 40 |
#define __S2N_ENSURE_LIKELY(cond, action) \ |
| 41 |
do { \ |
| 42 |
if (s2n_unlikely(!(cond))) { \ |
| 43 |
action; \ |
| 44 |
} \ |
| 45 |
} while (0) |
| 46 |
|
| 47 |
#ifdef NDEBUG |
| 48 |
#define __S2N_ENSURE_DEBUG(cond, action) \ |
| 49 |
do { \ |
| 50 |
} while (0) |
| 51 |
#else |
| 52 |
#define __S2N_ENSURE_DEBUG(cond, action) __S2N_ENSURE_LIKELY((cond), action) |
| 53 |
#endif |
| 54 |
|
| 55 |
#define __S2N_ENSURE_PRECONDITION(result) (s2n_likely(s2n_result_is_ok(result)) ? S2N_RESULT_OK : S2N_RESULT_ERROR) |
| 56 |
|
| 57 |
#ifdef NDEBUG |
| 58 |
#define __S2N_ENSURE_POSTCONDITION(result) (S2N_RESULT_OK) |
| 59 |
#else |
| 60 |
#define __S2N_ENSURE_POSTCONDITION(result) (s2n_likely(s2n_result_is_ok(result)) ? S2N_RESULT_OK : S2N_RESULT_ERROR) |
| 61 |
#endif |
| 62 |
|
| 63 |
#define __S2N_ENSURE_SAFE_MEMMOVE(d, s, n, guard) \ |
| 64 |
do { \ |
| 65 |
__typeof(n) __tmp_n = (n); \ |
| 66 |
if (s2n_likely(__tmp_n)) { \ |
| 67 |
void *r = s2n_ensure_memmove_trace((d), (s), (__tmp_n)); \ |
| 68 |
guard(r); \ |
| 69 |
} \ |
| 70 |
} while (0) |
| 71 |
|
| 72 |
#define __S2N_ENSURE_SAFE_MEMSET(d, c, n, guard) \ |
| 73 |
do { \ |
| 74 |
__typeof(n) __tmp_n = (n); \ |
| 75 |
if (s2n_likely(__tmp_n)) { \ |
| 76 |
__typeof(d) __tmp_d = (d); \ |
| 77 |
guard(__tmp_d); \ |
| 78 |
memset(__tmp_d, (c), __tmp_n); \ |
| 79 |
} \ |
| 80 |
} while (0) |
| 81 |
|
| 82 |
#if defined(S2N_DIAGNOSTICS_PUSH_SUPPORTED) && defined(S2N_DIAGNOSTICS_POP_SUPPORTED) |
| 83 |
#define __S2N_ENSURE_CHECKED_RETURN(v) \ |
| 84 |
do { \ |
| 85 |
_Pragma("GCC diagnostic push") \ |
| 86 |
_Pragma("GCC diagnostic error \"-Wconversion\"") return v; \ |
| 87 |
_Pragma("GCC diagnostic pop") \ |
| 88 |
} while (0) |
| 89 |
#else |
| 90 |
#define __S2N_ENSURE_CHECKED_RETURN(v) return v |
| 91 |
#endif |
| 92 |
|
| 93 |
void *s2n_ensure_memmove_trace(void *to, const void *from, size_t size); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#ifdef CBMC |
| 106 |
#define S2N_MEM_IS_READABLE_CHECK(base, len) (((len) == 0) || __CPROVER_r_ok((base), (len))) |
| 107 |
#define S2N_MEM_IS_WRITABLE_CHECK(base, len) (((len) == 0) || __CPROVER_w_ok((base), (len))) |
| 108 |
#else |
| 109 |
|
| 110 |
|
| 111 |
#define S2N_MEM_IS_READABLE_CHECK(base, len) (((len) == 0) || (base) != NULL) |
| 112 |
#define S2N_MEM_IS_WRITABLE_CHECK(base, len) (((len) == 0) || (base) != NULL) |
| 113 |
#endif |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#define S2N_MEM_IS_READABLE(base, len) (((len) == 0) || (base) != NULL) |
| 119 |
#define S2N_MEM_IS_WRITABLE(base, len) (((len) == 0) || (base) != NULL) |
| 120 |
#define S2N_OBJECT_PTR_IS_READABLE(ptr) ((ptr) != NULL) |
| 121 |
#define S2N_OBJECT_PTR_IS_WRITABLE(ptr) ((ptr) != NULL) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#define S2N_IMPLIES(a, b) (!(a) || (b)) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#define S2N_IFF(a, b) (!!(a) == !!(b)) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#ifdef CBMC |
| 141 |
#define CONTRACT_ASSERT(...) __CPROVER_assert(__VA_ARGS__) |
| 142 |
#define CONTRACT_ASSIGNS(...) __CPROVER_assigns(__VA_ARGS__) |
| 143 |
#define CONTRACT_ASSIGNS_ERR(...) CONTRACT_ASSIGNS(__VA_ARGS__, _s2n_debug_info, s2n_errno) |
| 144 |
#define CONTRACT_ASSUME(...) __CPROVER_assume(__VA_ARGS__) |
| 145 |
#define CONTRACT_REQUIRES(...) __CPROVER_requires(__VA_ARGS__) |
| 146 |
#define CONTRACT_ENSURES(...) __CPROVER_ensures(__VA_ARGS__) |
| 147 |
#define CONTRACT_INVARIANT(...) __CPROVER_loop_invariant(__VA_ARGS__) |
| 148 |
#define CONTRACT_RETURN_VALUE (__CPROVER_return_value) |
| 149 |
#else |
| 150 |
#define CONTRACT_ASSERT(...) |
| 151 |
#define CONTRACT_ASSIGNS(...) |
| 152 |
#define CONTRACT_ASSIGNS_ERR(...) |
| 153 |
#define CONTRACT_ASSUME(...) |
| 154 |
#define CONTRACT_REQUIRES(...) |
| 155 |
#define CONTRACT_ENSURES(...) |
| 156 |
#define CONTRACT_INVARIANT(...) |
| 157 |
#define CONTRACT_RETURN_VALUE |
| 158 |
#endif |