|
| 1 | +## CBMC Assigns Clause |
| 2 | + |
| 3 | + |
| 4 | +### Introduction |
| 5 | +The _assigns_ clause allows the user to specify a list of memory |
| 6 | +locations which may be written within a particular scope. While an assigns |
| 7 | +clause may, in general, be interpreted with either _writes_ or _modifies_ |
| 8 | +semantics, this design is based on the former. This means that memory not |
| 9 | +captured by the assigns clause must not be written within the given scope, even |
| 10 | +if the value(s) therein are not modified. |
| 11 | + |
| 12 | + |
| 13 | +### Scalar Variables |
| 14 | +The initial iteration of this design focuses on specifying an assigns clause for |
| 15 | +primitive types and their pointers. Arrays, structured data, and pointers are |
| 16 | +left to future contributions. |
| 17 | + |
| 18 | + |
| 19 | +##### Syntax |
| 20 | +A special construct is introduced to specify assigns clauses. Its syntax is |
| 21 | +defined as follows. |
| 22 | + |
| 23 | +``` |
| 24 | + <assigns_clause> := __CPROVER_assigns ( <target_list> ) |
| 25 | +``` |
| 26 | +``` |
| 27 | + <target_list> := <target> |
| 28 | + | <target> , <target_list> |
| 29 | +``` |
| 30 | +``` |
| 31 | + <target> := <identifier> |
| 32 | + | * <target> |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +The `<assigns_clause>` states that only the memory identified by the dereference |
| 37 | +expressions and identifiers listed in the contained `<target_list>` may be |
| 38 | +written within the associated scope, as follows. |
| 39 | + |
| 40 | + |
| 41 | +##### Semantics |
| 42 | +The semantics of an assigns clause *c* of some function *f* can be understood in |
| 43 | +two contexts. First, one may consider how the expressions in *c* are treated |
| 44 | +when a call to *f* is replaced by its contract specification, assuming the |
| 45 | +contract specification is a sound characterization of the behavior of *f*. |
| 46 | +Second, one may consider how *c* is applied to the function body of *f* in order |
| 47 | +to determine whether *c* is a sound characterization of the behavior of *f*. We |
| 48 | +begin by exploring these two perspectives for assigns clauses which contain only |
| 49 | +scalar expressions. |
| 50 | + |
| 51 | +Let the i<sup>th</sup> expression in some assigns clause *c* be denoted |
| 52 | +*exprs*<sub>*c*</sub>[i], the j<sup>th</sup> formal parameter of some function |
| 53 | +*f* be denoted *params*<sub>*f*</sub>[j], and the k<sup>th</sup> argument passed |
| 54 | +in a call to function *f* be denoted *args*<sub>*f*</sub>[k] (an identifying |
| 55 | +index may be added to distinguish a *particular* call to *f*, but for simplicity |
| 56 | +it is omitted here). |
| 57 | + |
| 58 | + |
| 59 | +###### Replacement |
| 60 | +Assuming an assigns clause *c* is a sound characterization of the behavior of |
| 61 | +the function *f*, a call to *f* may be replaced a series of non-deterministic |
| 62 | +assignments. For each expression *e* ∈ *exprs*<sub>*c*</sub>, let there be |
| 63 | +an assignment ɸ := ∗, where ɸ is *args*<sub>*f*</sub>[i] if *e* |
| 64 | +is identical to some *params*<sub>*f*</sub>[i], and *e* otherwise. |
| 65 | + |
| 66 | + |
| 67 | +###### Enforcement |
| 68 | +In order to determine whether an assigns clause *c* is a sound characterization |
| 69 | +of the behavior of a function *f*, the body of the function should be |
| 70 | +instrumented with additional statements as follows. |
| 71 | + |
| 72 | +- For each expression *e* ∈ *exprs*<sub>*c*</sub>, create a temporary |
| 73 | + variable *tmp*<sub>*e*</sub> to store \&(*e*), the address of *e*, at the |
| 74 | +start of *f*. |
| 75 | +- Before each assignment statement, *lhs* := *rhs*, add an assertion (structured |
| 76 | + as a disjunction) |
| 77 | +assert(∃ *tmp*<sub>*e*</sub>. \_\_CPROVER\_same\_object(\&(*lhs*), |
| 78 | +*tmp*<sub>*e*</sub>) |
| 79 | +- Before each function call with an assigns clause *a*, add an assertion for |
| 80 | + each *e*<sub>*a*</sub> ∈ *exprs*<sub>*a*</sub> (also formulated as a |
| 81 | +disjunction) |
| 82 | +assert(∃ *tmp*<sub>*e*</sub>. |
| 83 | +\_\_CPROVER\_same\_object(\&(*e*<sub>*a*</sub>), *tmp*<sub>*e*</sub>) |
| 84 | + |
| 85 | +Here, \_\_CPROVER\_same\_object returns true if two pointers |
| 86 | +reference the same object in the CBMC memory model. Additionally, for each |
| 87 | +function call without an assigns clause, CBMC adds an assertion assert(*false*) |
| 88 | +to ensure that the assigns contract will only hold if all called functions |
| 89 | +have an assigns clause which is compatible with that of the calling function. |
0 commit comments