Skip to content

Preconditions of cbmc library function memcpy are too strong #6472

Closed
@qinheping

Description

@qinheping

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.

void *memcpy(void *dst, const void *src, size_t n)

void *__builtin___memcpy_chk(void *dst, const void *src, __CPROVER_size_t n, __CPROVER_size_t size)

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");

Metadata

Metadata

Assignees

Labels

awsBugs or features of importance to AWS CBMC users

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions