Line data Source code
1 : #include <proof_allocators.h>
2 : #include <seahorn/seahorn.h>
3 : #include <string_helper.h>
4 : #include <assert.h>
5 :
6 1960004 : struct aws_string *ensure_string_is_allocated(size_t len) {
7 1960004 : struct aws_string *str = malloc(sizeof(struct aws_string) + len + 1);
8 1960004 : memhavoc(str, sizeof(struct aws_string) + len);
9 1960004 :
10 1960004 : if (str) {
11 1784540 : /* Fields are declared const, so we need to copy them in like this */
12 1784540 : *(struct aws_allocator **)(&str->allocator) =
13 1784540 : nd_bool() ? sea_allocator() : NULL;
14 1784540 : *(size_t *)(&str->len) = len;
15 1784540 : *(uint8_t *)&str->bytes[len] = '\0';
16 1784540 : }
17 1960004 : return str;
18 2938062 : }
19 978058 :
20 2954884 : struct aws_string *ensure_string_is_allocated_bounded_length(size_t max_size) {
21 2954884 : size_t len = nd_size_t();
22 2954884 : // len < max_size
23 2865242 : len %= max_size;
24 2865242 : return ensure_string_is_allocated(len);
25 2865242 : }
26 888416 :
27 1180568 : struct aws_string *ensure_string_is_allocated_nondet_length(void) {
28 1180568 : /* Considers any size up to the maximum possible size for the array [bytes] in
29 1270210 : * aws_string */
30 1270210 : return ensure_string_is_allocated_bounded_length(FUZZ_MAX_STRING_LEN - 1 -
31 292152 : sizeof(struct aws_string));
32 1278918 : }
33 986766 :
34 986766 : static const char *_ensure_c_str_is_nd_allocated(size_t max_size, size_t *len,
35 2075354 : bool safe) {
36 2075354 : bool should_fail = nd_bool();
37 2075354 : if (!safe && should_fail) {
38 77748 : return NULL;
39 223706 : }
40 1156798 : size_t alloc_size;
41 1156798 : alloc_size = nd_size_t();
42 1156798 :
43 1156798 : // 1 <= alloc_size <= max_size + 1
44 1156798 : alloc_size %= max_size;
45 1010840 : ++alloc_size;
46 1010840 :
47 1557513 : char *str = malloc(alloc_size);
48 1557513 : // safe allocation
49 1557513 : assert(str);
50 1050476 :
51 1050476 : *len = alloc_size - 1;
52 1517877 : // -- zero terminated
53 1517877 : str[*len] = '\0';
54 1517877 : // -- initialized
55 1517877 : memhavoc(str, *len);
56 1517877 :
57 1517877 : // -- remove any spurious 1
58 5689035 : for (unsigned i = 0; i < *len; ++i) {
59 4678195 : if (str[i] == 0) str[i] = 1;
60 4678195 : }
61 1517877 :
62 1517877 : return str;
63 1517877 : }
64 507037 :
65 507037 : /// Allocate a randomly initialized string
66 1058741 : const char *ensure_c_str_is_nd_allocated_safe(size_t max_size, size_t *len) {
67 1058741 : return _ensure_c_str_is_nd_allocated(max_size, len, true);
68 1058741 : }
69 507037 :
70 3095710 : const char *ensure_c_str_is_nd_allocated(size_t max_size, size_t *len) {
71 2588673 : return _ensure_c_str_is_nd_allocated(max_size, len, false);
72 2588673 : }
73 507037 :
74 624370 : bool aws_byte_buf_has_allocator(const struct aws_byte_buf *const buf) {
75 624370 : return (buf->allocator == sea_allocator());
76 117333 : }
|