Description
I got failure "memcpy source region readable" when I call memcpy(p,q,0)
where q
is an overflowing pointer (this happened in a c-common benchmark https://github.com/awslabs/aws-c-common/blob/main/source/array_list.c#L187). However, as the number of bytes we want to copy is 0, this call will not cause any memory safety problem.
When I looked the cbmc library function memcpy
, I found that, even though the function body will be executed only when n
(the number of bytes we want to copy) is greater than 0, the preconditions do not exclude the case of n==0
.
cbmc/src/ansi-c/library/string.c
Line 609 in d8296bd
cbmc/src/ansi-c/library/string.c
Line 655 in d8296bd
Modifying the preconditions to implications with premise n != 0
can avoid such false-negative error. For example, the preconditions of memcpy
can be following.
__CPROVER_precondition(
n == 0 || (__CPROVER_POINTER_OBJECT(dst) != __CPROVER_POINTER_OBJECT(src) ||
((const char *)src >= (const char *)dst + n) ||
((const char *)dst >= (const char *)src + n)),
"memcpy src/dst overlap");
__CPROVER_precondition(
n == 0 || (__CPROVER_r_ok(src, n)), "memcpy source region readable");
__CPROVER_precondition(
n == 0 || (__CPROVER_w_ok(dst, n)), "memcpy destination region writeable");