Add __CPROVER_ensures_frees(ptr; ... )
clauses to loop contracts
#7210
Labels
aws
Bugs or features of importance to AWS CBMC users
Code Contracts
Function and loop contracts
feature request
CBMC version: 5.67.0
Operating system: N/A
tl/dr. #6887 brings __CPROVER_frees clauses and related predicates to function contracts only, this needs to be extended to loop contracts
Problem description
It is currently is not possible to specify that a memory location can/must be freed.
Proposal
1. Add a new clause
__CPROVER_frees(ptr)
Specifies which memory locations may be freed by the function. We reuse the assigns clause syntax for conditional targets.
Just like an assigns clause, the contents of the clause is interpreted at the function call site, pre-invocation.
1.1. Contract checking
Only locations listed in the frees clause can be freed, when their condition holds.
1.2. Contract replacement
For each conditional target
cond: target
we introduce a witness variable (initially false):Each conditional target gets turned into a conditional non-deterministic
free
instruction:To handle aliasing and avoid double free errors, every time free is invoked we update the witness variable of other targets if they alias with the freed pointer.
2. Add a new predicate
bool __CPROVER_freeable(void *ptr)
In an assertion context,
__CPROVER_freeable(ptr)
holds ifptr
points to a live heap-allocated object with offset 0.In an assumption, a pointer can be made freeable in multiple different ways:
We let the user specify how to do that by also adding
ptr
to the contract assigns clause (will make it nondet), by usingis_fresh(ptr)
, etc.3. Add a new predicate
bool __CPROVER_freed(void *ptr)
In a post condition,
__CPROVER_freed(ptr)
holds the object pointed to byptr
has been freed during the execution of the function.Cannot be used in a pre-condition.
If a pointer is reallocated, one might use
__CPROVER_freed(__CPROVER_old(ptr))
to refer to the old value of the pointer.3.1 Contract checking
We introduce a witness variable for each target:
We instrument each call to the
free
(realloc
, etc.) with a witness variable.Each target gets turned into a GOTO assertion inserted as a post-condition:
3.2. Contract replacement
We use the witness variables introduced in 1.2 for contract replacement.
to turn each conditional target an assumption:
The text was updated successfully, but these errors were encountered: