Skip to content

Commit de3de81

Browse files
committed
Use array_{copy,replace,set} in C library to avoid loops
1 parent cf51b59 commit de3de81

File tree

5 files changed

+72
-20
lines changed

5 files changed

+72
-20
lines changed

regression/cbmc/memset1/main.c

+29
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)*sizeof(A));
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

+10
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/library/stdlib.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ inline void *malloc(__CPROVER_size_t malloc_size);
6767
inline void *calloc(__CPROVER_size_t nmemb, __CPROVER_size_t size)
6868
{
6969
__CPROVER_HIDE:;
70-
__CPROVER_size_t total_size=nmemb*size;
7170
void *res;
72-
res=malloc(total_size);
71+
res=malloc(nmemb*size);
7372
#ifdef __CPROVER_STRING_ABSTRACTION
7473
__CPROVER_is_zero_string(res)=1;
7574
__CPROVER_zero_string_length(res)=0;
7675
//for(int i=0; i<nmemb*size; i++) res[i]=0;
7776
#else
78-
// there should be memset here
79-
//char *p=res;
80-
//for(int i=0; i<total_size; i++) p[i]=0;
77+
if(nmemb>1)
78+
__CPROVER_array_set(res, 0);
79+
else if(nmemb==1)
80+
for(__CPROVER_size_t i=0; i<size; ++i)
81+
((char*)res)[i]=0;
8182
#endif
8283
return res;
8384
}

src/ansi-c/library/string.c

+21-14
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,10 @@ inline void *memcpy(void *dst, const void *src, size_t n)
535535
#else
536536
__CPROVER_assert(__CPROVER_POINTER_OBJECT(dst)!=
537537
__CPROVER_POINTER_OBJECT(src), "memcpy src/dst overlap");
538-
for(__CPROVER_size_t i=0; i<n ; i++) ((char *)dst)[i]=((const char *)src)[i];
538+
//for(__CPROVER_size_t i=0; i<n ; i++) ((char *)dst)[i]=((const char *)src)[i];
539+
char src_n[n];
540+
__CPROVER_array_copy(src_n, (char*)src);
541+
__CPROVER_array_replace((char*)dst, src_n);
539542
#endif
540543
return dst;
541544
}
@@ -563,7 +566,10 @@ void *__builtin___memcpy_chk(void *dst, const void *src, __CPROVER_size_t n, __C
563566
__CPROVER_assert(__CPROVER_POINTER_OBJECT(dst)!=
564567
__CPROVER_POINTER_OBJECT(src), "memcpy src/dst overlap");
565568
(void)size;
566-
for(__CPROVER_size_t i=0; i<n ; i++) ((char *)dst)[i]=((const char *)src)[i];
569+
//for(__CPROVER_size_t i=0; i<n ; i++) ((char *)dst)[i]=((const char *)src)[i];
570+
char src_n[n];
571+
__CPROVER_array_copy(src_n, (char*)src);
572+
__CPROVER_array_replace((char*)dst, src_n);
567573
#endif
568574
return dst;
569575
}
@@ -596,8 +602,11 @@ inline void *memset(void *s, int c, size_t n)
596602
else
597603
__CPROVER_is_zero_string(s)=0;
598604
#else
599-
char *sp=s;
600-
for(__CPROVER_size_t i=0; i<n ; i++) sp[i]=c;
605+
//char *sp=s;
606+
//for(__CPROVER_size_t i=0; i<n ; i++) sp[i]=c;
607+
unsigned char s_n[n];
608+
__CPROVER_array_set(s_n, (unsigned char)c);
609+
__CPROVER_array_replace((unsigned char*)s, s_n);
601610
#endif
602611
return s;
603612
}
@@ -625,8 +634,11 @@ void *__builtin___memset_chk(void *s, int c, __CPROVER_size_t n, __CPROVER_size_
625634
__CPROVER_is_zero_string(s)=0;
626635
#else
627636
(void)size;
628-
char *sp=s;
629-
for(__CPROVER_size_t i=0; i<n ; i++) sp[i]=c;
637+
//char *sp=s;
638+
//for(__CPROVER_size_t i=0; i<n ; i++) sp[i]=c;
639+
unsigned char s_n[n];
640+
__CPROVER_array_set(s_n, (unsigned char)c);
641+
__CPROVER_array_replace((unsigned char*)s, s_n);
630642
#endif
631643
return s;
632644
}
@@ -655,14 +667,9 @@ inline void *memmove(void *dest, const void *src, size_t n)
655667
else
656668
__CPROVER_is_zero_string(dest)=0;
657669
#else
658-
if((const char *)dest>=(const char *)src+n)
659-
{
660-
for(__CPROVER_size_t i=0; i<n; i++) ((char *)dest)[i]=((const char *)src)[i];
661-
}
662-
else
663-
{
664-
for(__CPROVER_size_t i=n; i>0; i--) ((char *)dest)[i-1]=((const char *)src)[i-1];
665-
}
670+
char src_n[n];
671+
__CPROVER_array_copy(src_n, (char*)src);
672+
__CPROVER_array_replace((char*)dest, src_n);
666673
#endif
667674
return dest;
668675
}

src/ansi-c/library/unistd.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,19 @@ ssize_t read(int fildes, void *buf, size_t nbyte)
198198
if((fildes>=0 && fildes<=2) || fildes < __CPROVER_pipe_offset)
199199
{
200200
ssize_t nread;
201-
size_t i;
202201
__CPROVER_assume(0<=nread && (size_t)nread<=nbyte);
203202

203+
#if 0
204+
size_t i;
204205
for(i=0; i<nbyte; i++)
205206
{
206207
char nondet_char;
207208
((char *)buf)[i]=nondet_char;
208209
}
210+
#else
211+
char nondet_bytes[nbyte];
212+
__CPROVER_array_replace((char*)buf, nondet_bytes);
213+
#endif
209214

210215
__CPROVER_bool error;
211216
return error ? -1 : nread;

0 commit comments

Comments
 (0)