-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_list.c
151 lines (129 loc) · 5.04 KB
/
array_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/array_list.h>
#include <aws/common/private/array_list.h>
#include "proof_helpers/make_common_data_structures.h"
#include "aws_seahorn.h"
extern void ndset(char*, int);
extern size_t nd_sizet();
extern char* nd_ptr();
bool aws_array_list_is_bounded(
const struct aws_array_list *const list,
const size_t max_initial_item_allocation,
const size_t max_item_size) {
bool item_size_is_bounded = list->item_size <= max_item_size;
bool length_is_bounded = list->length <= max_initial_item_allocation;
return item_size_is_bounded && length_is_bounded;
}
void ensure_array_list_has_allocated_data_member(struct aws_array_list *const list) {
list->current_size = nd_sizet();
list->length = nd_sizet();
list->item_size = nd_sizet();
/// XXX Cannot do this for now
//list->data = bounded_malloc(list->current_size);
list->data = bounded_malloc(MAX_INITIAL_ITEM_ALLOCATION * MAX_ITEM_SIZE);
/// XXX Need to take care of fail allocator
if (list->current_size == 0 && list->length == 0) {
assume(list->data == NULL);
list->alloc = NULL;//can_fail_allocator();
} else {
list->alloc = NULL;//nondet_bool() ? NULL : can_fail_allocator();
}
}
int aws_array_list_copy(const struct aws_array_list *AWS_RESTRICT from, struct aws_array_list *AWS_RESTRICT to) {
AWS_FATAL_PRECONDITION(from->item_size == to->item_size);
AWS_FATAL_PRECONDITION(from->data);
AWS_PRECONDITION(aws_array_list_is_valid(from));
AWS_PRECONDITION(aws_array_list_is_valid(to));
size_t copy_size = 0;
if (aws_mul_size_checked(from->length, from->item_size, ©_size)) {
AWS_POSTCONDITION(aws_array_list_is_valid(from));
AWS_POSTCONDITION(aws_array_list_is_valid(to));
return AWS_OP_ERR;
}
if (to->current_size >= copy_size) {
if (copy_size > 0) {
/// XXX Not needed for now
//memcpy(to->data, from->data, copy_size);
}
to->length = from->length;
AWS_POSTCONDITION(aws_array_list_is_valid(from));
AWS_POSTCONDITION(aws_array_list_is_valid(to));
return AWS_OP_SUCCESS;
}
/* if to is in dynamic mode, we can just reallocate it and copy */
if (to->alloc != NULL) {
void *tmp = aws_mem_acquire(to->alloc, copy_size);
if (!tmp) {
AWS_POSTCONDITION(aws_array_list_is_valid(from));
AWS_POSTCONDITION(aws_array_list_is_valid(to));
return AWS_OP_ERR;
}
memcpy(tmp, from->data, copy_size);
if (to->data) {
aws_mem_release(to->alloc, to->data);
}
to->data = tmp;
to->length = from->length;
to->current_size = copy_size;
AWS_POSTCONDITION(aws_array_list_is_valid(from));
AWS_POSTCONDITION(aws_array_list_is_valid(to));
return AWS_OP_SUCCESS;
}
return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL);
}
int main() {
/* data structure */
struct aws_array_list from;
struct aws_array_list to;
#ifdef INIT
from.current_size = nd_sizet();
from.length = nd_sizet();
assume (from.current_size > 0 && from.length > 0);
from.item_size = nd_sizet();
from.data = bounded_malloc(MAX_INITIAL_ITEM_ALLOCATION * MAX_ITEM_SIZE);
from.alloc = NULL;
to.current_size = nd_sizet();
to.length = nd_sizet();
assume (to.current_size > 0 && to.length > 0);
to.item_size = nd_sizet();
to.data = bounded_malloc(MAX_INITIAL_ITEM_ALLOCATION * MAX_ITEM_SIZE);
to.alloc = NULL;
#endif
/* assumptions */
assume(aws_array_list_is_bounded(&from, MAX_INITIAL_ITEM_ALLOCATION, MAX_ITEM_SIZE));
#ifndef INIT
ensure_array_list_has_allocated_data_member(&from);
#endif
assume(aws_array_list_is_valid(&from));
assume(aws_array_list_is_bounded(&to, MAX_INITIAL_ITEM_ALLOCATION, MAX_ITEM_SIZE));
#ifndef INIT
ensure_array_list_has_allocated_data_member(&to);
#endif
assume(aws_array_list_is_valid(&to));
assume(from.item_size == to.item_size);
assume(from.data != NULL);
/* perform operation under verification */
if (!aws_array_list_copy(&from, &to)) {
/* In the case aws_array_list_copy is successful, both lists have the same length */
sassert(to.length == from.length);
sassert(to.current_size >= (from.length * from.item_size));
}
/* assertions */
sassert(aws_array_list_is_valid(&from));
sassert(aws_array_list_is_valid(&to));
sassert(from.item_size == to.item_size);
return 0;
}