makiri 0.5.1-aarch64-linux → 0.7.0-aarch64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +2 -1
- data/.github/workflows/libfuzzer.yml +1 -1
- data/.github/workflows/release.yml +28 -13
- data/.github/workflows/verify.yml +37 -0
- data/CHANGELOG.md +52 -0
- data/README.md +16 -0
- data/Rakefile +14 -7
- data/lib/makiri/3.2/makiri.so +0 -0
- data/lib/makiri/3.3/makiri.so +0 -0
- data/lib/makiri/3.4/makiri.so +0 -0
- data/lib/makiri/4.0/makiri.so +0 -0
- data/lib/makiri/attr.rb +0 -3
- data/lib/makiri/comment.rb +1 -3
- data/lib/makiri/compat_aliases.rb +1 -12
- data/lib/makiri/version.rb +1 -1
- data/script/check_c_safety.rb +11 -0
- data/script/check_c_safety_allowlist.yml +7 -16
- data/suppressions/ruby.supp +22 -0
- data/verify/Makefile +237 -0
- data/verify/cbmc_models.c +38 -0
- data/verify/harness_alloc.c +257 -0
- data/verify/harness_buf.c +167 -0
- data/verify/harness_hash.c +28 -0
- data/verify/harness_span.c +213 -0
- data/verify/harness_spanbuf.c +98 -0
- data/verify/harness_utf8.c +70 -0
- data/verify/harness_utf8_chain.c +64 -0
- data/verify/harness_utf8_words.c +54 -0
- data/verify/harness_xml_chars.c +59 -0
- data/verify/harness_xml_expand.c +72 -0
- data/verify/harness_xml_parse.c +42 -0
- data/verify/harness_xpath_lex.c +51 -0
- data/verify/harness_xpath_number.c +68 -0
- data/verify/harness_xpath_parse.c +57 -0
- data/verify/selftest_main.c +16 -0
- data/verify/stub.c +10 -0
- data/verify/verify.h +54 -0
- metadata +21 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* Plain-C bodies for libc functions CBMC's built-in library does not model on
|
|
2
|
+
* this platform (a bodyless callee havocs its return value, turning every
|
|
3
|
+
* downstream pointer check into a false failure). Compiled ONLY into the CBMC
|
|
4
|
+
* runs, never into the smoke/selftest/fuzzer builds (those link the real libc).
|
|
5
|
+
* Each body is the obvious reference implementation, so proving against it is
|
|
6
|
+
* proving against the function's contract. */
|
|
7
|
+
#ifdef __CPROVER__
|
|
8
|
+
|
|
9
|
+
#include <stddef.h>
|
|
10
|
+
#include <stdlib.h> /* the SDK's strtod declaration carries an asm alias
|
|
11
|
+
* (_strtod on macOS); defining without it leaves the
|
|
12
|
+
* callee bodyless under its aliased name */
|
|
13
|
+
#include <string.h>
|
|
14
|
+
|
|
15
|
+
void *
|
|
16
|
+
memchr(const void *s, int c, size_t n)
|
|
17
|
+
{
|
|
18
|
+
const unsigned char *p = s;
|
|
19
|
+
for (size_t i = 0; i < n; ++i) {
|
|
20
|
+
if (p[i] == (unsigned char)c) return (void *)(p + i);
|
|
21
|
+
}
|
|
22
|
+
return NULL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* strtod, modeled as "libc conversion unavailable" (no bytes consumed). This
|
|
26
|
+
* is a deliberate choice, not a shortcut: correctly-rounded decimal parsing is
|
|
27
|
+
* libc territory we trust rather than verify (like memcpy), and this model
|
|
28
|
+
* steers every caller down its own fallback - for mkr_xpath_number_from_extent
|
|
29
|
+
* that means the isolating-reparse and hand-assembly paths, i.e. exactly OUR
|
|
30
|
+
* code, get proven on all inputs. */
|
|
31
|
+
double
|
|
32
|
+
strtod(const char *nptr, char **endptr)
|
|
33
|
+
{
|
|
34
|
+
if (endptr != NULL) *endptr = (char *)nptr;
|
|
35
|
+
return 0.0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#endif /* __CPROVER__ */
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/* CBMC harness: overflow-checked size arithmetic + allocators (core/mkr_alloc).
|
|
2
|
+
*
|
|
3
|
+
* The addition properties hold over ALL inputs. The multiplication properties
|
|
4
|
+
* are proved for an unbounded count times an element size drawn from the
|
|
5
|
+
* concrete set below. The reason is solver-shaped: relating the guard's 64-bit
|
|
6
|
+
* division to the multiplication at full width is intractable for bit-level
|
|
7
|
+
* solvers (measured: >8 min with MiniSat/kissat/Bitwuzla alike), while a
|
|
8
|
+
* few-valued elem turns the division into constant-threshold compares
|
|
9
|
+
* (measured: seconds). The restriction is also faithful to the code: every
|
|
10
|
+
* in-tree caller passes a compile-time sizeof as elem, and the set covers all
|
|
11
|
+
* of them with headroom - extend it if a bigger element type ever appears.
|
|
12
|
+
*/
|
|
13
|
+
#include "verify.h"
|
|
14
|
+
#include "core/mkr_alloc.h"
|
|
15
|
+
|
|
16
|
+
#include <string.h>
|
|
17
|
+
|
|
18
|
+
static int
|
|
19
|
+
elem_in_set(size_t e)
|
|
20
|
+
{
|
|
21
|
+
return e == 1 || e == 2 || e == 4 || e == 8 || e == 16 || e == 24 ||
|
|
22
|
+
e == 32 || e == 48 || e == 64 || e == 96 || e == 128 || e == 256;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
int
|
|
26
|
+
main(void)
|
|
27
|
+
{
|
|
28
|
+
/* --- size_add: unbounded --- */
|
|
29
|
+
{
|
|
30
|
+
size_t a = nondet_size_t(), b = nondet_size_t(), out = 0;
|
|
31
|
+
if (mkr_size_add(a, b, &out)) {
|
|
32
|
+
VERIFY_ASSERT(out == a + b, "add: exact");
|
|
33
|
+
VERIFY_ASSERT(out >= a && out >= b, "add: no wrap");
|
|
34
|
+
} else {
|
|
35
|
+
VERIFY_ASSERT(a > SIZE_MAX - b, "add: fails only on true overflow");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* --- size_mul: unbounded count x sizeof-set elem (see header comment) --- */
|
|
40
|
+
{
|
|
41
|
+
size_t count = nondet_size_t(), elem = nondet_size_t(), out = 0;
|
|
42
|
+
VERIFY_ASSUME(elem_in_set(elem));
|
|
43
|
+
if (mkr_size_mul(count, elem, &out)) {
|
|
44
|
+
VERIFY_ASSERT(out == count * elem, "mul: exact");
|
|
45
|
+
VERIFY_ASSERT((unsigned __int128)count * elem <= SIZE_MAX, "mul: no wrap");
|
|
46
|
+
} else {
|
|
47
|
+
VERIFY_ASSERT(count != 0 && (unsigned __int128)count * elem > SIZE_MAX,
|
|
48
|
+
"mul: fails only on true overflow");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* --- size_add / size_mul: failure leaves *out unchanged --- */
|
|
53
|
+
{
|
|
54
|
+
/* add's failure test is a subtraction (a > SIZE_MAX - b), tractable at
|
|
55
|
+
* full width, so both operands stay unbounded. */
|
|
56
|
+
size_t a = nondet_size_t(), b = nondet_size_t(); size_t out = (size_t)0x12345678u;
|
|
57
|
+
if (!mkr_size_add(a, b, &out)) VERIFY_ASSERT(out == (size_t)0x12345678u, "size_add: failure leaves out unchanged");
|
|
58
|
+
}
|
|
59
|
+
{
|
|
60
|
+
/* mul's failure test is a division (b > SIZE_MAX / a); constrain one
|
|
61
|
+
* operand to the sizeof-set (as the exact/no-wrap mul block does, §4.2)
|
|
62
|
+
* so it stays tractable. The failure path is still reached - a huge `a`
|
|
63
|
+
* overflows even a small b - so the invariance is genuinely exercised. */
|
|
64
|
+
size_t a = nondet_size_t(), b = nondet_size_t(); size_t out = (size_t)0x12345678u;
|
|
65
|
+
VERIFY_ASSUME(elem_in_set(b));
|
|
66
|
+
if (!mkr_size_mul(a, b, &out)) VERIFY_ASSERT(out == (size_t)0x12345678u, "size_mul: failure leaves out unchanged");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* --- grow_capacity: unbounded cap/need x sizeof-set elem --- */
|
|
70
|
+
{
|
|
71
|
+
size_t cap = nondet_size_t(), need = nondet_size_t(), elem = nondet_size_t();
|
|
72
|
+
size_t nc = 0;
|
|
73
|
+
VERIFY_ASSUME(elem_in_set(elem));
|
|
74
|
+
if (mkr_grow_capacity(cap, need, elem, &nc)) {
|
|
75
|
+
VERIFY_ASSERT(nc >= need, "grow_capacity: covers need");
|
|
76
|
+
VERIFY_ASSERT((unsigned __int128)nc * elem <= SIZE_MAX, "grow_capacity: nc*elem fits");
|
|
77
|
+
} else {
|
|
78
|
+
VERIFY_ASSERT((unsigned __int128)need * elem > SIZE_MAX,
|
|
79
|
+
"grow_capacity: fails only when need*elem cannot fit");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* --- heap paths: bounded sizes, malloc may fail --- */
|
|
84
|
+
{
|
|
85
|
+
size_t n = nondet_size_t();
|
|
86
|
+
VERIFY_ASSUME(n <= 8);
|
|
87
|
+
char *s = mkr_str_alloc(n);
|
|
88
|
+
if (s != NULL) {
|
|
89
|
+
VERIFY_ASSERT(s[n] == '\0', "str_alloc: terminator pre-set");
|
|
90
|
+
for (size_t i = 0; i < n; ++i) s[i] = 'x'; /* content writable */
|
|
91
|
+
free(s);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
{
|
|
95
|
+
unsigned char src[4];
|
|
96
|
+
for (size_t i = 0; i < sizeof src; ++i) src[i] = nondet_uchar();
|
|
97
|
+
size_t n = nondet_size_t();
|
|
98
|
+
VERIFY_ASSUME(n <= sizeof src);
|
|
99
|
+
char *s = mkr_strndup((const char *)src, n);
|
|
100
|
+
if (s != NULL) {
|
|
101
|
+
VERIFY_ASSERT(s[n] == '\0', "strndup: terminated");
|
|
102
|
+
VERIFY_ASSERT(n == 0 || memcmp(s, src, n) == 0, "strndup: copies");
|
|
103
|
+
free(s);
|
|
104
|
+
}
|
|
105
|
+
VERIFY_ASSERT(mkr_strndup(NULL, 1) == NULL, "strndup: NULL src with n>0 fails closed");
|
|
106
|
+
}
|
|
107
|
+
{
|
|
108
|
+
/* strdup: copies up to the first NUL, terminated; NULL -> NULL. The
|
|
109
|
+
* source is force-terminated inside the buffer so strlen is bounded.
|
|
110
|
+
* (unsigned char storage + a pointer cast: a VALUE cast of a >127 byte
|
|
111
|
+
* to char is implementation-defined and --conversion-check flags it.) */
|
|
112
|
+
unsigned char src[4];
|
|
113
|
+
for (size_t i = 0; i + 1 < sizeof src; ++i) src[i] = nondet_uchar();
|
|
114
|
+
src[sizeof src - 1] = '\0';
|
|
115
|
+
char *d = mkr_strdup((const char *)src);
|
|
116
|
+
if (d != NULL) {
|
|
117
|
+
size_t n = strlen((const char *)src);
|
|
118
|
+
VERIFY_ASSERT(d[n] == '\0', "strdup: terminated");
|
|
119
|
+
VERIFY_ASSERT(n == 0 || memcmp(d, src, n) == 0, "strdup: copies");
|
|
120
|
+
free(d);
|
|
121
|
+
}
|
|
122
|
+
VERIFY_ASSERT(mkr_strdup(NULL) == NULL, "strdup: NULL -> NULL");
|
|
123
|
+
}
|
|
124
|
+
{
|
|
125
|
+
/* strndup at ANY n over a heap source that really holds n bytes: no
|
|
126
|
+
* OOB access on either side, the copy matches, and the result is
|
|
127
|
+
* terminated. Closes the magnitude-dependent class (a truncating
|
|
128
|
+
* intermediate would break the alloc-length/copy-length relation only
|
|
129
|
+
* past 2^32) that the small-n block above cannot see; the source is
|
|
130
|
+
* symbolic-size, so the proof runs at full range up to CBMC's object
|
|
131
|
+
* ceiling (2^52 with --object-bits 12). */
|
|
132
|
+
size_t n = nondet_size_t();
|
|
133
|
+
char *src = malloc(n); /* nondet success under --malloc-may-fail */
|
|
134
|
+
if (src != NULL) {
|
|
135
|
+
char *s = mkr_strndup(src, n);
|
|
136
|
+
if (s != NULL) {
|
|
137
|
+
VERIFY_ASSERT(s[n] == '\0', "strndup: terminated at any n");
|
|
138
|
+
/* Content at a nondet index: i ranges over ALL positions, so
|
|
139
|
+
* this is whole-content equality without a loop (CBMC's own
|
|
140
|
+
* memcmp model unwinds a loop and cannot take a symbolic n). */
|
|
141
|
+
size_t i = nondet_size_t();
|
|
142
|
+
if (i < n)
|
|
143
|
+
VERIFY_ASSERT(s[i] == src[i], "strndup: copies at any n (every index)");
|
|
144
|
+
free(s);
|
|
145
|
+
}
|
|
146
|
+
free(src);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
{
|
|
150
|
+
/* str_alloc at ANY n: the n + 1 sizing is right (the terminator slot
|
|
151
|
+
* exists) or the call fails closed with NULL - the off-by-one class,
|
|
152
|
+
* proved at full range (the bounded block above covers the content
|
|
153
|
+
* writes). */
|
|
154
|
+
size_t n = nondet_size_t();
|
|
155
|
+
char *s = mkr_str_alloc(n);
|
|
156
|
+
if (s != NULL) {
|
|
157
|
+
VERIFY_ASSERT(s[n] == '\0', "str_alloc: terminator slot exists at any n");
|
|
158
|
+
if (n > 0) s[0] = 'x';
|
|
159
|
+
free(s);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
{
|
|
163
|
+
/* reallocarray's MOVE path: growing a live allocation preserves the
|
|
164
|
+
* prefix and makes the new tail writable; shrinking preserves the
|
|
165
|
+
* surviving prefix; a failed resize leaves the original owned and
|
|
166
|
+
* intact; count == 0 frees and returns NULL. */
|
|
167
|
+
uint32_t *p = mkr_reallocarray(NULL, 2, sizeof(uint32_t));
|
|
168
|
+
if (p != NULL) {
|
|
169
|
+
p[0] = 0x11111111u; p[1] = 0x22222222u;
|
|
170
|
+
uint32_t *q = mkr_reallocarray(p, 4, sizeof(uint32_t));
|
|
171
|
+
if (q != NULL) {
|
|
172
|
+
VERIFY_ASSERT(q[0] == 0x11111111u && q[1] == 0x22222222u,
|
|
173
|
+
"reallocarray: grow preserves content");
|
|
174
|
+
q[3] = 0x33333333u; /* new tail writable */
|
|
175
|
+
uint32_t *r = mkr_reallocarray(q, 1, sizeof(uint32_t));
|
|
176
|
+
if (r != NULL) {
|
|
177
|
+
VERIFY_ASSERT(r[0] == 0x11111111u,
|
|
178
|
+
"reallocarray: shrink preserves the surviving prefix");
|
|
179
|
+
VERIFY_ASSERT(mkr_reallocarray(r, 0, sizeof(uint32_t)) == NULL,
|
|
180
|
+
"reallocarray: count 0 frees and returns NULL");
|
|
181
|
+
} else {
|
|
182
|
+
VERIFY_ASSERT(q[0] == 0x11111111u && q[3] == 0x33333333u,
|
|
183
|
+
"reallocarray: failed shrink leaves the original intact");
|
|
184
|
+
free(q);
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
VERIFY_ASSERT(p[0] == 0x11111111u && p[1] == 0x22222222u,
|
|
188
|
+
"reallocarray: failed grow leaves the original intact");
|
|
189
|
+
free(p);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
{
|
|
194
|
+
void *arr = NULL;
|
|
195
|
+
size_t cap = 0;
|
|
196
|
+
size_t need = nondet_size_t();
|
|
197
|
+
VERIFY_ASSUME(need <= 4);
|
|
198
|
+
if (mkr_grow_reserve(&arr, &cap, need, sizeof(uint32_t)) == MKR_OK) {
|
|
199
|
+
VERIFY_ASSERT(cap >= need, "grow_reserve: capacity covers need");
|
|
200
|
+
uint32_t *w = (uint32_t *)arr;
|
|
201
|
+
for (size_t i = 0; i < need; ++i) w[i] = 0; /* in-bounds writes */
|
|
202
|
+
|
|
203
|
+
void *p0 = arr;
|
|
204
|
+
size_t c0 = cap;
|
|
205
|
+
/* no-op: need <= cap must succeed and leave ptr/cap unchanged */
|
|
206
|
+
VERIFY_ASSERT(mkr_grow_reserve(&arr, &cap, cap, sizeof(uint32_t)) == MKR_OK,
|
|
207
|
+
"grow_reserve: need==cap ok");
|
|
208
|
+
VERIFY_ASSERT(arr == p0 && cap == c0, "grow_reserve: no-op leaves ptr/cap");
|
|
209
|
+
|
|
210
|
+
/* overflow -> OOM, state left unchanged */
|
|
211
|
+
VERIFY_ASSERT(mkr_grow_reserve(&arr, &cap, SIZE_MAX, sizeof(uint32_t)) == MKR_ERR_OOM,
|
|
212
|
+
"grow_reserve: overflow OOM");
|
|
213
|
+
VERIFY_ASSERT(arr == p0 && cap == c0, "grow_reserve: failure leaves ptr/cap");
|
|
214
|
+
} else {
|
|
215
|
+
VERIFY_ASSERT(arr == NULL && cap == 0, "grow_reserve: fail leaves state unchanged");
|
|
216
|
+
}
|
|
217
|
+
free(arr);
|
|
218
|
+
}
|
|
219
|
+
{
|
|
220
|
+
size_t count = nondet_size_t(), elem = nondet_size_t();
|
|
221
|
+
VERIFY_ASSUME(count <= 4 && elem <= 4);
|
|
222
|
+
void *p = mkr_callocarray(count, elem);
|
|
223
|
+
if (p != NULL) {
|
|
224
|
+
const unsigned char *z = (const unsigned char *)p;
|
|
225
|
+
for (size_t i = 0; i < count * elem; ++i)
|
|
226
|
+
VERIFY_ASSERT(z[i] == 0, "callocarray: zeroed");
|
|
227
|
+
free(p);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
{
|
|
231
|
+
/* callocarray at ANY size (count unbounded x sizeof-set elem): zeroed
|
|
232
|
+
* at a nondet index - i ranges over all positions, so this is
|
|
233
|
+
* whole-allocation zeroing without a loop (same trick as the
|
|
234
|
+
* symbolic-n strndup content check). */
|
|
235
|
+
size_t count = nondet_size_t(), elem = nondet_size_t();
|
|
236
|
+
VERIFY_ASSUME(elem_in_set(elem));
|
|
237
|
+
unsigned char *p = mkr_callocarray(count, elem);
|
|
238
|
+
if (p != NULL) {
|
|
239
|
+
size_t i = nondet_size_t();
|
|
240
|
+
if (i < count * elem)
|
|
241
|
+
VERIFY_ASSERT(p[i] == 0, "callocarray: zeroed at any size (every index)");
|
|
242
|
+
free(p);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* --- fail-closed on a true overflow: allocators must return NULL.
|
|
247
|
+
* (Last: the overflow assumption is unreachable under the smoke build's
|
|
248
|
+
* small pseudo-inputs, and an unmet assume ends the smoke run.) --- */
|
|
249
|
+
{
|
|
250
|
+
size_t count = nondet_size_t(), elem = nondet_size_t();
|
|
251
|
+
VERIFY_ASSUME(elem_in_set(elem));
|
|
252
|
+
VERIFY_ASSUME((unsigned __int128)count * elem > SIZE_MAX);
|
|
253
|
+
VERIFY_ASSERT(mkr_callocarray(count, elem) == NULL, "callocarray: overflow fails closed");
|
|
254
|
+
VERIFY_ASSERT(mkr_reallocarray(NULL, count, elem) == NULL, "reallocarray: overflow fails closed");
|
|
255
|
+
}
|
|
256
|
+
return 0;
|
|
257
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/* CBMC harness: the growable capped buffer (core/mkr_buf.c) checked against a
|
|
2
|
+
* shadow reference model.
|
|
3
|
+
*
|
|
4
|
+
* A small nondet max (1..6) makes the LIMIT ceiling reachable; two nondet
|
|
5
|
+
* appends with a reserve in between drive the growth, clamp and failure paths
|
|
6
|
+
* (run with --malloc-may-fail --malloc-fail-null so every OOM branch is
|
|
7
|
+
* covered). Properties:
|
|
8
|
+
* - a successful append extends the shadow copy exactly (content equality),
|
|
9
|
+
* keeps the NUL terminator, and never takes len past the ceiling;
|
|
10
|
+
* - cap never exceeds max + 1 (the geometric-growth clamp: no ~2x overshoot
|
|
11
|
+
* near the limit - the property that bounds a real buffer's allocation by
|
|
12
|
+
* MKR_BUF_HARD_MAX);
|
|
13
|
+
* - every failure (INVALID / LIMIT / OOM) leaves len, cap and the content
|
|
14
|
+
* bytes untouched;
|
|
15
|
+
* - steal hands back the exact accumulated content, NUL-terminated, and
|
|
16
|
+
* resets the buffer to a usable empty state.
|
|
17
|
+
*/
|
|
18
|
+
#include "verify.h"
|
|
19
|
+
#include "core/mkr_buf.h"
|
|
20
|
+
|
|
21
|
+
#include <string.h>
|
|
22
|
+
|
|
23
|
+
#define MAXCAP 6
|
|
24
|
+
#define NSRC 4
|
|
25
|
+
|
|
26
|
+
/* One nondet append checked against the shadow model. */
|
|
27
|
+
static void
|
|
28
|
+
step_append(mkr_buf_t *b, char *shadow, size_t *slen, size_t maxlim)
|
|
29
|
+
{
|
|
30
|
+
unsigned char src[NSRC];
|
|
31
|
+
for (size_t i = 0; i < sizeof src; ++i) src[i] = nondet_uchar();
|
|
32
|
+
size_t n = nondet_size_t();
|
|
33
|
+
if (n > NSRC) n = NSRC;
|
|
34
|
+
|
|
35
|
+
size_t len0 = b->len, cap0 = b->cap;
|
|
36
|
+
mkr_status_t st = mkr_buf_append(b, src, n);
|
|
37
|
+
if (st == MKR_OK) {
|
|
38
|
+
VERIFY_ASSERT(b->len == len0 + n, "append: len advances by n");
|
|
39
|
+
VERIFY_ASSERT(b->len <= maxlim, "append: ceiling respected");
|
|
40
|
+
VERIFY_ASSERT(b->cap <= maxlim + 1, "append: growth clamped to max+1");
|
|
41
|
+
VERIFY_ASSERT(n == 0 || b->data[b->len] == '\0', "append: NUL-terminated");
|
|
42
|
+
memcpy(shadow + *slen, src, n);
|
|
43
|
+
*slen += n;
|
|
44
|
+
VERIFY_ASSERT(*slen == 0 || memcmp(b->data, shadow, *slen) == 0,
|
|
45
|
+
"append: content matches the shadow model");
|
|
46
|
+
} else {
|
|
47
|
+
VERIFY_ASSERT(st == MKR_ERR_LIMIT || st == MKR_ERR_OOM,
|
|
48
|
+
"append: non-NULL src fails only on limit/OOM");
|
|
49
|
+
VERIFY_ASSERT(st != MKR_ERR_LIMIT || len0 + n > maxlim,
|
|
50
|
+
"append: LIMIT only past the ceiling");
|
|
51
|
+
VERIFY_ASSERT(b->len == len0 && b->cap == cap0, "append: failure leaves size intact");
|
|
52
|
+
VERIFY_ASSERT(*slen == 0 || memcmp(b->data, shadow, *slen) == 0,
|
|
53
|
+
"append: failure leaves content intact");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
int
|
|
58
|
+
main(void)
|
|
59
|
+
{
|
|
60
|
+
size_t max = nondet_size_t();
|
|
61
|
+
VERIFY_ASSUME(max >= 1 && max <= MAXCAP); /* small ceiling so LIMIT is reachable */
|
|
62
|
+
|
|
63
|
+
mkr_buf_t b;
|
|
64
|
+
mkr_buf_init(&b, max);
|
|
65
|
+
|
|
66
|
+
char shadow[2 * NSRC];
|
|
67
|
+
size_t slen = 0;
|
|
68
|
+
|
|
69
|
+
/* NULL source with n > 0 is INVALID and touches nothing. */
|
|
70
|
+
VERIFY_ASSERT(mkr_buf_append(&b, NULL, 3) == MKR_ERR_INVALID, "append: NULL src fails closed");
|
|
71
|
+
VERIFY_ASSERT(b.len == 0 && b.cap == 0, "append: INVALID touches nothing");
|
|
72
|
+
|
|
73
|
+
step_append(&b, shadow, &slen, max);
|
|
74
|
+
|
|
75
|
+
/* reserve between appends: len/content unchanged, cap stays clamped. */
|
|
76
|
+
{
|
|
77
|
+
size_t len0 = b.len;
|
|
78
|
+
(void)mkr_buf_reserve(&b, nondet_size_t() % (2 * MAXCAP));
|
|
79
|
+
VERIFY_ASSERT(b.len == len0, "reserve: len untouched");
|
|
80
|
+
VERIFY_ASSERT(b.cap <= max + 1, "reserve: clamped to the buffer's ceiling");
|
|
81
|
+
VERIFY_ASSERT(slen == 0 || memcmp(b.data, shadow, slen) == 0,
|
|
82
|
+
"reserve: content untouched");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
step_append(&b, shadow, &slen, max);
|
|
86
|
+
|
|
87
|
+
/* reserve return-value contract: success leaves len/content untouched and
|
|
88
|
+
* never shrinks cap (still clamped to the buffer's own ceiling); failure
|
|
89
|
+
* (OOM only - reserve never fails INVALID/LIMIT) leaves len, cap AND
|
|
90
|
+
* content untouched. */
|
|
91
|
+
{
|
|
92
|
+
size_t len0 = b.len, cap0 = b.cap;
|
|
93
|
+
char before[2 * NSRC];
|
|
94
|
+
if (b.len > 0) memcpy(before, b.data, b.len);
|
|
95
|
+
mkr_status_t st = mkr_buf_reserve(&b, nondet_size_t() % (2 * MAXCAP));
|
|
96
|
+
if (st == MKR_OK) {
|
|
97
|
+
VERIFY_ASSERT(b.len == len0, "reserve: success leaves len unchanged");
|
|
98
|
+
VERIFY_ASSERT(b.cap >= cap0, "reserve: success does not shrink cap");
|
|
99
|
+
VERIFY_ASSERT(b.cap <= max + 1, "reserve: cap stays clamped");
|
|
100
|
+
} else {
|
|
101
|
+
VERIFY_ASSERT(st == MKR_ERR_OOM, "reserve: failure is OOM");
|
|
102
|
+
VERIFY_ASSERT(b.len == len0 && b.cap == cap0, "reserve: failure leaves len/cap");
|
|
103
|
+
VERIFY_ASSERT(b.len == 0 || memcmp(b.data, before, b.len) == 0,
|
|
104
|
+
"reserve: failure leaves content");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* Ceiling SELECTION at ANY max: max == 0 falls back to the default limit
|
|
109
|
+
* (not unbounded), max past MKR_BUF_HARD_MAX is clamped to it, and a huge
|
|
110
|
+
* reserve request never allocates past limit + 1 (the "no huge alloc"
|
|
111
|
+
* guarantee, at full range - the model-checked block above only sees
|
|
112
|
+
* max 1..6). Small appends keep the loops bounded; the ceiling arithmetic
|
|
113
|
+
* is what runs at full width. */
|
|
114
|
+
{
|
|
115
|
+
mkr_buf_t h;
|
|
116
|
+
mkr_buf_init(&h, nondet_size_t()); /* ANY max, 0 included */
|
|
117
|
+
size_t soft = (h.max != 0) ? h.max : MKR_BUF_DEFAULT_LIMIT;
|
|
118
|
+
size_t limit = (soft < MKR_BUF_HARD_MAX) ? soft : MKR_BUF_HARD_MAX;
|
|
119
|
+
|
|
120
|
+
if (mkr_buf_reserve(&h, nondet_size_t()) == MKR_OK) /* ANY request */
|
|
121
|
+
VERIFY_ASSERT(h.cap <= limit + 1, "reserve: any request clamps to limit+1");
|
|
122
|
+
VERIFY_ASSERT(h.len == 0, "reserve: len untouched at any max");
|
|
123
|
+
|
|
124
|
+
mkr_status_t st = mkr_buf_append(&h, "ab", 2);
|
|
125
|
+
if (st == MKR_OK) {
|
|
126
|
+
VERIFY_ASSERT(h.len == 2 && h.data[2] == '\0', "append: works under any ceiling");
|
|
127
|
+
VERIFY_ASSERT(h.cap <= limit + 1, "append: growth clamped at any max");
|
|
128
|
+
} else {
|
|
129
|
+
VERIFY_ASSERT(st == MKR_ERR_LIMIT ? limit < 2 : st == MKR_ERR_OOM,
|
|
130
|
+
"append: fails closed only on limit/OOM");
|
|
131
|
+
}
|
|
132
|
+
mkr_buf_free(&h);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* steal: exact content ownership transfer + reset to a usable buffer. */
|
|
136
|
+
{
|
|
137
|
+
size_t out_len = (size_t)-1;
|
|
138
|
+
char *s = mkr_buf_steal(&b, &out_len);
|
|
139
|
+
if (s != NULL) {
|
|
140
|
+
VERIFY_ASSERT(out_len == slen, "steal: reports the accumulated length");
|
|
141
|
+
VERIFY_ASSERT(s[out_len] == '\0', "steal: NUL-terminated");
|
|
142
|
+
VERIFY_ASSERT(slen == 0 || memcmp(s, shadow, slen) == 0, "steal: exact content");
|
|
143
|
+
free(s);
|
|
144
|
+
}
|
|
145
|
+
VERIFY_ASSERT(b.data == NULL && b.len == 0 && b.cap == 0, "steal: buffer reset");
|
|
146
|
+
VERIFY_ASSERT(mkr_buf_append(&b, "z", 1) != MKR_ERR_INVALID, "steal: buffer stays usable");
|
|
147
|
+
}
|
|
148
|
+
mkr_buf_free(&b);
|
|
149
|
+
|
|
150
|
+
/* steal on an empty (never-appended-to) buffer: success hands back an
|
|
151
|
+
* owned "", OOM (on the internal malloc(1)) leaves out_len untouched (the
|
|
152
|
+
* impl only sets *out_len on the success path) and the buffer empty. */
|
|
153
|
+
{
|
|
154
|
+
mkr_buf_t e;
|
|
155
|
+
mkr_buf_init(&e, 0);
|
|
156
|
+
size_t out_len = (size_t)0xABCD; /* sentinel */
|
|
157
|
+
char *s = mkr_buf_steal(&e, &out_len);
|
|
158
|
+
if (s != NULL) {
|
|
159
|
+
VERIFY_ASSERT(out_len == 0 && s[0] == '\0', "steal empty: owned \"\", len 0");
|
|
160
|
+
free(s);
|
|
161
|
+
} else {
|
|
162
|
+
VERIFY_ASSERT(out_len == (size_t)0xABCD, "steal empty OOM: out_len untouched");
|
|
163
|
+
VERIFY_ASSERT(e.data == NULL && e.len == 0, "steal empty OOM: buffer stays empty");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* CBMC harness: the power-of-two table sizer (core/mkr_hash.h), over ALL
|
|
2
|
+
* size_t inputs (the loop only shifts, so unlike the mul/div guards this
|
|
3
|
+
* closes at full width).
|
|
4
|
+
*
|
|
5
|
+
* mkr_pow2_ceil feeds the open-addressing hash tables; sizing below the
|
|
6
|
+
* element count would make linear probing never find a free slot, so the
|
|
7
|
+
* properties are: on success the result is a power of two, covers n, and is
|
|
8
|
+
* the SMALLEST such power (no oversized tables); failure happens exactly when
|
|
9
|
+
* no power of two >= n fits in size_t.
|
|
10
|
+
*/
|
|
11
|
+
#include "verify.h"
|
|
12
|
+
#include "core/mkr_hash.h"
|
|
13
|
+
|
|
14
|
+
#define TOP_POW2 ((size_t)1 << (sizeof(size_t) * 8 - 1))
|
|
15
|
+
|
|
16
|
+
int
|
|
17
|
+
main(void)
|
|
18
|
+
{
|
|
19
|
+
size_t n = nondet_size_t(), out = 0;
|
|
20
|
+
if (mkr_pow2_ceil(n, &out)) {
|
|
21
|
+
VERIFY_ASSERT(out != 0 && (out & (out - 1)) == 0, "pow2_ceil: a power of two");
|
|
22
|
+
VERIFY_ASSERT(out >= n, "pow2_ceil: covers n");
|
|
23
|
+
VERIFY_ASSERT(out == 1 || (out >> 1) < n, "pow2_ceil: the smallest such power");
|
|
24
|
+
} else {
|
|
25
|
+
VERIFY_ASSERT(n > TOP_POW2, "pow2_ceil: fails only when no power of two fits");
|
|
26
|
+
}
|
|
27
|
+
return 0;
|
|
28
|
+
}
|