-
Notifications
You must be signed in to change notification settings - Fork 65
/
loop_invariants.patch
194 lines (179 loc) · 6.23 KB
/
loop_invariants.patch
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
diff --git a/source/core_json.c b/source/core_json.c
index d8694f0..761c44b 100644
--- a/source/core_json.c
+++ b/source/core_json.c
@@ -63,6 +63,21 @@ typedef union
#define isCurlyOpen_( x ) ( ( x ) == '{' )
#define isCurlyClose_( x ) ( ( x ) == '}' )
+/**
+ * Renaming all loop-contract clauses from CBMC for readability.
+ * For more information about loop contracts in CBMC, see
+ * https://diffblue.github.io/cbmc/contracts-user.html.
+ */
+#ifdef CBMC
+ #define loopInvariant(...) __CPROVER_loop_invariant(__VA_ARGS__)
+ #define decreases(...) __CPROVER_decreases(__VA_ARGS__)
+ #define assigns(...) __CPROVER_assigns(__VA_ARGS__)
+#else
+ #define loopInvariant(...)
+ #define decreases(...)
+ #define assigns(...)
+#endif
+
/**
* @brief Advance buffer index beyond whitespace.
*
@@ -79,6 +94,9 @@ static void skipSpace( const char * buf,
coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
for( i = *start; i < max; i++ )
+ assigns( i )
+ loopInvariant( *start <= i && i <= max )
+ decreases( max - i )
{
if( !isspace_( buf[ i ] ) )
{
@@ -103,6 +121,13 @@ static size_t countHighBits( uint8_t c )
size_t i = 0;
while( ( n & 0x80U ) != 0U )
+ assigns( i, n )
+ loopInvariant (
+ ( 0U <= i ) && ( i <= 8U )
+ && ( n == ( c & ( 0xFF >> i ) ) << i )
+ && ( ( ( c >> ( 8U - i ) ) + 1U ) == ( 1U << i ) )
+ )
+ decreases( 8U - i )
{
i++;
n = ( n & 0x7FU ) << 1U;
@@ -211,6 +236,13 @@ static bool skipUTF8MultiByte( const char * buf,
/* The bit count is 1 greater than the number of bytes,
* e.g., when j is 2, we skip one more byte. */
for( j = bitCount - 1U; j > 0U; j-- )
+ assigns( j, i, value, c.c )
+ loopInvariant(
+ ( 0 <= j ) && ( j <= bitCount - 1 )
+ && ( *start <= i ) && ( i <= max )
+ && ( ( i == max ) ==> ( j > 0 ) )
+ )
+ decreases( j )
{
i++;
@@ -346,6 +378,12 @@ static bool skipOneHexEscape( const char * buf,
if( ( end < max ) && ( buf[ i ] == '\\' ) && ( buf[ i + 1U ] == 'u' ) )
{
for( i += 2U; i < end; i++ )
+ assigns( value, i )
+ loopInvariant(
+ ( *start + 2U <= i ) && ( i <= end ) &&
+ ( 0U <= value ) && ( value < ( 1U << ( 4U * ( i - ( 2U + *start ) ) ) ) )
+ )
+ decreases( end - i )
{
uint8_t n = hexToInt( buf[ i ] );
@@ -523,6 +561,9 @@ static bool skipString( const char * buf,
i++;
while( i < max )
+ assigns( i )
+ loopInvariant( *start + 1U <= i && i <= max )
+ decreases( max - i )
{
if( buf[ i ] == '"' )
{
@@ -581,6 +622,9 @@ static bool strnEq( const char * a,
coreJSON_ASSERT( ( a != NULL ) && ( b != NULL ) );
for( i = 0; i < n; i++ )
+ assigns( i )
+ loopInvariant( i <= n )
+ decreases( n - i )
{
if( a[ i ] != b[ i ] )
{
@@ -696,6 +740,9 @@ static bool skipDigits( const char * buf,
saveStart = *start;
for( i = *start; i < max; i++ )
+ assigns( value, i )
+ loopInvariant( *start <= i && i <= max )
+ decreases( max - i )
{
if( !isdigit_( buf[ i ] ) )
{
@@ -945,6 +992,9 @@ static void skipArrayScalars( const char * buf,
i = *start;
while( i < max )
+ assigns( i )
+ loopInvariant( *start <= i && i <= max )
+ decreases( max - i )
{
if( skipAnyScalar( buf, &i, max ) != true )
{
@@ -991,6 +1041,13 @@ static bool skipObjectScalars( const char * buf,
i = *start;
while( i < max )
+ assigns( i, *start, comma )
+ loopInvariant(
+ i >= *start
+ && __CPROVER_loop_entry( i ) <= i && i <= max
+ && __CPROVER_loop_entry( *start ) <= *start && *start <= max
+ )
+ decreases( max - i )
{
if( skipString( buf, &i, max ) != true )
{
@@ -1118,6 +1175,14 @@ static JSONStatus_t skipCollection( const char * buf,
i = *start;
while( i < max )
+ assigns( i, depth, c, __CPROVER_object_whole( stack ), ret )
+ loopInvariant(
+ -1 <= depth && depth <= JSON_MAX_DEPTH
+ && *start <= i && i <= max
+ && ( ( ret == JSONSuccess ) ==> i >= *start + 2U )
+ && ( ret == JSONSuccess || ret == JSONPartial || ret == JSONIllegalDocument || ret == JSONMaxDepthExceeded )
+ )
+ decreases( max - i )
{
c = buf[ i ];
i++;
@@ -1407,6 +1472,9 @@ static bool objectSearch( const char * buf,
skipSpace( buf, &i, max );
while( i < max )
+ assigns( i, key, keyLength, value, valueLength )
+ loopInvariant( __CPROVER_loop_entry( i ) <= i && i <= max )
+ decreases( max - i )
{
if( nextKeyValuePair( buf, &i, max, &key, &keyLength,
&value, &valueLength ) != true )
@@ -1474,6 +1542,9 @@ static bool arraySearch( const char * buf,
skipSpace( buf, &i, max );
while( i < max )
+ assigns( i, currentIndex, value, valueLength )
+ loopInvariant( __CPROVER_loop_entry( i ) <= i && i <= max && currentIndex < i )
+ decreases( max - i )
{
if( nextValue( buf, &i, max, &value, &valueLength ) != true )
{
@@ -1539,6 +1610,9 @@ static bool skipQueryPart( const char * buf,
while( ( i < max ) &&
!isSeparator_( buf[ i ] ) &&
!isSquareOpen_( buf[ i ] ) )
+ assigns( i )
+ loopInvariant( i <= max )
+ decreases( max - i )
{
i++;
}
@@ -1585,6 +1659,17 @@ static JSONStatus_t multiSearch( const char * buf,
coreJSON_ASSERT( ( max > 0U ) && ( queryLength > 0U ) );
while( i < queryLength )
+ assigns( i, start, queryStart, value, length )
+ loopInvariant(
+ 0U <= start && start < max
+ && 0U < length && length <= max
+ && start + length <= max
+ && ( ( i == queryLength && ret == JSONSuccess && buf[ start ] == '"' ) ==> length >= 2U )
+ && 0U <= value && value < max
+ && 0U <= i && i <= queryLength
+ && 0U <= queryStart && queryStart <= queryLength
+ )
+ decreases( queryLength - i )
{
bool found = false;