I use CBMC to verify my Pthreads program, it detected some assert errors which I don't think would exist. The error only occur when I run the two threads at the same time. That is to say, when I put one of the statement which calls the thread function(func
or func1
) into comment, CBMC can then verify it successful. Is there any conflict in the assignment of array a
and b
?
int a[4], b[4];
static void * func(void * me)
{
int i;
for(i=0; i<2; i++){
a[i] = b[i] = i;
assert( a[i] == i ); //failed
}
return ((void *) 0);
}
static void * func1(void * me)
{
int i;
for(i=2; i<4; i++){
a[i] = b[i] = i;
assert( a[i] == i ); //failed
}
return ((void *) 0);
}
int main(){
pthread_t thr1;
pthread_create(&thr1, NULL, func1, (void *)0);
(*func)(0);
pthread_join(thr1,NULL);
return 0;
}
The output of CBMC is as following:
Violated property:
file pthreads4.c line 25 function func1
assertion a[i] == i
a[(signed long int)i] == i
VERIFICATION FAILED
This looks to be a false positive on CBMC's part.
We can see that the main thread will modify
a[0]
,a[1]
,b[0]
, andb[1]
.Thread
thr1
modifiesa[2]
,a[3]
,b[2]
, andb[3]
.There is actually no overlapping accesses between the the threads and so this program should behave as if it is being run sequentially.
The error trace produced by CBMC does not make much sense either:
This counter example claims that
a[1] == 0
. However, state 64 shows0
being assigned toa[1]
even though the last written value tob[1]
is1
in state 57.