-
Notifications
You must be signed in to change notification settings - Fork 274
Preconditions of cbmc library function memcpy are too strong #6472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I am afraid that the behaviour of memcpy is undefined when either |
The wording in C99 is as follows (7.21.1 par 2): Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4. |
Hey @Herbping, can you give us trace to look at or an example complete enough for us to reproduce so we can see just how messed up the pointer in question is? Maybe it's right at that knife edge where it is 1 past the size of the array, or the array is of size 0, in which case we might want allow that. |
The failure can be reproduced by running unbounded proof for the benchmark
In the unbounded proof, I annotated the loop https://github.com/Herbping/aws-c-common/blob/unbounded_proofs/source/array_list.c#L178 with invariants and apply the invariants. I also removed the bound of length of array list, and set the bound of In the trace that violated the check "memcpy source region readable", the length of array is
In the loop body, we add a slice (128) to both pointers
|
So, I think the following.... |
I created a pull request awslabs/aws-c-common#874 to avoid calling |
I got failure "memcpy source region readable" when I call
memcpy(p,q,0)
whereq
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 whenn
(the number of bytes we want to copy) is greater than 0, the preconditions do not exclude the case ofn==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 ofmemcpy
can be following.The text was updated successfully, but these errors were encountered: