| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef MLD_CBMC_H |
| 7 |
#define MLD_CBMC_H |
| 8 |
/*************************************************** |
| 9 |
* Basic replacements for __CPROVER_XXX contracts |
| 10 |
***************************************************/ |
| 11 |
#ifndef CBMC |
| 12 |
|
| 13 |
#define __contract__(x) |
| 14 |
#define __loop__(x) |
| 15 |
#define cassert(x) |
| 16 |
|
| 17 |
#else /* !CBMC */ |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#define __contract__(x) x |
| 22 |
#define __loop__(x) x |
| 23 |
|
| 24 |
/* https://diffblue.github.io/cbmc/contracts-assigns.html */ |
| 25 |
#define assigns(...) __CPROVER_assigns(__VA_ARGS__) |
| 26 |
|
| 27 |
/* https://diffblue.github.io/cbmc/contracts-requires-ensures.html */ |
| 28 |
#define requires(...) __CPROVER_requires(__VA_ARGS__) |
| 29 |
#define ensures(...) __CPROVER_ensures(__VA_ARGS__) |
| 30 |
/* https://diffblue.github.io/cbmc/contracts-loops.html */ |
| 31 |
#define invariant(...) __CPROVER_loop_invariant(__VA_ARGS__) |
| 32 |
#define decreases(...) __CPROVER_decreases(__VA_ARGS__) |
| 33 |
/* cassert to avoid confusion with in-built assert */ |
| 34 |
#define cassert(x) __CPROVER_assert(x, "cbmc assertion failed") |
| 35 |
#define assume(...) __CPROVER_assume(__VA_ARGS__) |
| 36 |
|
| 37 |
/*************************************************** |
| 38 |
* Macros for "expression" forms that may appear |
| 39 |
* _inside_ top-level contracts. |
| 40 |
***************************************************/ |
| 41 |
|
| 42 |
/* |
| 43 |
* function return value - useful inside ensures |
| 44 |
* https://diffblue.github.io/cbmc/contracts-functions.html |
| 45 |
*/ |
| 46 |
#define return_value (__CPROVER_return_value) |
| 47 |
|
| 48 |
/* |
| 49 |
* assigns l-value targets |
| 50 |
* https://diffblue.github.io/cbmc/contracts-assigns.html |
| 51 |
*/ |
| 52 |
#define object_whole(...) __CPROVER_object_whole(__VA_ARGS__) |
| 53 |
#define memory_slice(...) __CPROVER_object_upto(__VA_ARGS__) |
| 54 |
#define same_object(...) __CPROVER_same_object(__VA_ARGS__) |
| 55 |
|
| 56 |
/* |
| 57 |
* Pointer-related predicates |
| 58 |
* https://diffblue.github.io/cbmc/contracts-memory-predicates.html |
| 59 |
*/ |
| 60 |
#define memory_no_alias(...) __CPROVER_is_fresh(__VA_ARGS__) |
| 61 |
#define readable(...) __CPROVER_r_ok(__VA_ARGS__) |
| 62 |
#define writeable(...) __CPROVER_w_ok(__VA_ARGS__) |
| 63 |
|
| 64 |
/* Maximum supported buffer size |
| 65 |
* |
| 66 |
* Larger buffers may be supported, but due to internal modeling constraints |
| 67 |
* in CBMC, the proofs of memory- and type-safety won't be able to run. |
| 68 |
* |
| 69 |
* If you find yourself in need for a buffer size larger than this, |
| 70 |
* please contact the maintainers, so we can prioritize work to relax |
| 71 |
* this somewhat artificial bound. |
| 72 |
*/ |
| 73 |
#define MLD_MAX_BUFFER_SIZE (SIZE_MAX >> 12) |
| 74 |
|
| 75 |
|
| 76 |
/* |
| 77 |
* History variables |
| 78 |
* https://diffblue.github.io/cbmc/contracts-history-variables.html |
| 79 |
*/ |
| 80 |
#define old(...) __CPROVER_old(__VA_ARGS__) |
| 81 |
#define loop_entry(...) __CPROVER_loop_entry(__VA_ARGS__) |
| 82 |
|
| 83 |
/* |
| 84 |
* Quantifiers |
| 85 |
* Note that the range on qvar is _exclusive_ between qvar_lb .. qvar_ub |
| 86 |
* https://diffblue.github.io/cbmc/contracts-quantifiers.html |
| 87 |
*/ |
| 88 |
|
| 89 |
/* |
| 90 |
* Prevent clang-format from corrupting CBMC's special ==> operator |
| 91 |
*/ |
| 92 |
/* clang-format off */ |
| 93 |
#define forall(qvar, qvar_lb, qvar_ub, predicate) \ |
| 94 |
__CPROVER_forall \ |
| 95 |
{ \ |
| 96 |
unsigned qvar; \ |
| 97 |
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> (predicate) \ |
| 98 |
} |
| 99 |
|
| 100 |
#define exists(qvar, qvar_lb, qvar_ub, predicate) \ |
| 101 |
__CPROVER_exists \ |
| 102 |
{ \ |
| 103 |
unsigned qvar; \ |
| 104 |
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) && (predicate) \ |
| 105 |
} |
| 106 |
/* clang-format on */ |
| 107 |
|
| 108 |
/*************************************************** |
| 109 |
* Convenience macros for common contract patterns |
| 110 |
***************************************************/ |
| 111 |
/* |
| 112 |
* Prevent clang-format from corrupting CBMC's special ==> operator |
| 113 |
*/ |
| 114 |
/* clang-format off */ |
| 115 |
#define CBMC_CONCAT_(left, right) left##right |
| 116 |
#define CBMC_CONCAT(left, right) CBMC_CONCAT_(left, right) |
| 117 |
|
| 118 |
#define array_bound_core(qvar, qvar_lb, qvar_ub, array_var, \ |
| 119 |
value_lb, value_ub) \ |
| 120 |
__CPROVER_forall \ |
| 121 |
{ \ |
| 122 |
unsigned qvar; \ |
| 123 |
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> \ |
| 124 |
(((int)(value_lb) <= ((array_var)[(qvar)])) && \ |
| 125 |
(((array_var)[(qvar)]) < (int)(value_ub))) \ |
| 126 |
} |
| 127 |
|
| 128 |
#define array_bound(array_var, qvar_lb, qvar_ub, value_lb, value_ub) \ |
| 129 |
array_bound_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), (qvar_lb), \ |
| 130 |
(qvar_ub), (array_var), (value_lb), (value_ub)) |
| 131 |
|
| 132 |
#define array_unchanged_core(qvar, qvar_lb, qvar_ub, array_var) \ |
| 133 |
__CPROVER_forall \ |
| 134 |
{ \ |
| 135 |
unsigned qvar; \ |
| 136 |
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> \ |
| 137 |
((array_var)[(qvar)]) == (old(* (int32_t (*)[(qvar_ub)])(array_var)))[(qvar)] \ |
| 138 |
} |
| 139 |
|
| 140 |
#define array_unchanged(array_var, N) \ |
| 141 |
array_unchanged_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var)) |
| 142 |
|
| 143 |
#define array_unchanged_u64_core(qvar, qvar_lb, qvar_ub, array_var) \ |
| 144 |
__CPROVER_forall \ |
| 145 |
{ \ |
| 146 |
unsigned qvar; \ |
| 147 |
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> \ |
| 148 |
((array_var)[(qvar)]) == (old(* (uint64_t (*)[(qvar_ub)])(array_var)))[(qvar)] \ |
| 149 |
} |
| 150 |
|
| 151 |
#define array_unchanged_u64(array_var, N) \ |
| 152 |
array_unchanged_u64_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var)) |
| 153 |
/* clang-format on */ |
| 154 |
|
| 155 |
/* Wrapper around array_bound operating on absolute values. |
| 156 |
* |
| 157 |
* The absolute value bound `k` is exclusive. |
| 158 |
* |
| 159 |
* Note that since the lower bound in array_bound is inclusive, we have to |
| 160 |
* raise it by 1 here. |
| 161 |
*/ |
| 162 |
#define array_abs_bound(arr, lb, ub, k) \ |
| 163 |
array_bound((arr), (lb), (ub), -((int)(k)) + 1, (k)) |
| 164 |
|
| 165 |
#endif /* CBMC */ |
| 166 |
|
| 167 |
#endif /* !MLD_CBMC_H */ |