Skip to content

Commit d23aa7e

Browse files
authored
Merge pull request #795 from tautschnig/array-replace
Implement C library string functions via array_{copy,replace,set}
2 parents 928c28f + ed0c88f commit d23aa7e

30 files changed

+638
-208
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int x=0x01020304;
6+
short *p=((short*)&x)+1;
7+
*p=0xABCD;
8+
assert(x==0xABCD0304);
9+
p=(short*)(((char*)&x)+1);
10+
*p=0xABCD;
11+
assert(x==0xABABCD04);
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int x=0x01020304;
6+
short *p=((short*)&x)+1;
7+
*p=0xABCD;
8+
assert(x==0x0102ABCD);
9+
p=(short*)(((char*)&x)+1);
10+
*p=0xABCD;
11+
assert(x==0x01ABCDCD);
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--big-endian
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdlib.h>
2+
3+
int *array;
4+
5+
int main()
6+
{
7+
unsigned size;
8+
__CPROVER_assume(size==1);
9+
10+
// produce unbounded array that does not have byte granularity
11+
array=malloc(size*sizeof(int));
12+
array[0]=0x01020304;
13+
14+
int array0=array[0];
15+
__CPROVER_assert(array0==0x01020304, "array[0] matches");
16+
17+
char *p=(char *)array;
18+
char p0=p[0];
19+
char p1=p[1];
20+
char p2=p[2];
21+
char p3=p[3];
22+
__CPROVER_assert(p0==4, "p[0] matches");
23+
__CPROVER_assert(p1==3, "p[1] matches");
24+
__CPROVER_assert(p2==2, "p[2] matches");
25+
__CPROVER_assert(p3==1, "p[3] matches");
26+
27+
unsigned short *q=(unsigned short *)array;
28+
unsigned short q0=q[0];
29+
__CPROVER_assert(q0==0x0304, "p[0,1] matches");
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--little-endian
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

regression/cbmc/memset1/main.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
int main()
6+
{
7+
int A[5];
8+
memset(A, 0, sizeof(int)*5);
9+
assert(A[0]==0);
10+
assert(A[1]==0);
11+
assert(A[2]==0);
12+
assert(A[3]==0);
13+
assert(A[4]==0);
14+
15+
A[3]=42;
16+
memset(A, 0xFFFFFF01, sizeof(int)*3);
17+
assert(A[0]==0x01010101);
18+
assert(A[1]==0x01010111);
19+
assert(A[2]==0x01010101);
20+
assert(A[3]==42);
21+
assert(A[4]==0);
22+
23+
int *B=malloc(sizeof(int)*2);
24+
memset(B, 2, sizeof(int)*2);
25+
assert(B[0]==0x02020202);
26+
assert(B[1]==0x02020202);
27+
28+
return 0;
29+
}

regression/cbmc/memset1/test.desc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
\[main.assertion.7\] assertion A\[1\]==0x01010111: FAILURE
8+
\*\* 1 of 12 failed \(2 iterations\)
9+
--
10+
^warning: ignoring

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ void ansi_c_internal_additions(std::string &code)
244244
// arrays
245245
// NOLINTNEXTLINE(whitespace/line_length)
246246
"__CPROVER_bool __CPROVER_array_equal(const void *array1, const void *array2);\n"
247+
// overwrite all of *dest (possibly using nondet values), even
248+
// if *src is smaller
247249
"void __CPROVER_array_copy(const void *dest, const void *src);\n"
250+
// replace at most size-of-*src bytes in *dest
251+
"void __CPROVER_array_replace(const void *dest, const void *src);\n"
248252
"void __CPROVER_array_set(const void *dest, ...);\n"
249253

250254
// k-induction

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,10 +2473,12 @@ exprt c_typecheck_baset::do_special_functions(
24732473
throw 0;
24742474
}
24752475

2476-
exprt pointer_offset_expr=exprt(ID_pointer_offset, expr.type());
2477-
pointer_offset_expr.operands()=expr.arguments();
2476+
exprt pointer_offset_expr=pointer_offset(expr.arguments().front());
24782477
pointer_offset_expr.add_source_location()=source_location;
24792478

2479+
if(expr.type()!=pointer_offset_expr.type())
2480+
pointer_offset_expr.make_typecast(expr.type());
2481+
24802482
return pointer_offset_expr;
24812483
}
24822484
else if(identifier==CPROVER_PREFIX "POINTER_OBJECT")

0 commit comments

Comments
 (0)