CProver manual
|
Assertions are statements within the program that attempt to capture the programmer’s intent. The ANSI C standard defines a header file assert.h, which offers a macro assert(cond)
. When executing a statement such as:
the execution is aborted with an error message if the condition evaluates to false, that is, if p
is NULL in the example above. The CPROVER tools can check the validity of the programmer-annotated assertions statically. Specifically, the CPROVER tools will check that the assertions hold for any nondeterministic choice that the program can make. The static assertion checks can be disabled using the --no-assertions
command line option.
In addition, there is a CPROVER-specific way to specify assertions, using the built-in function __CPROVER_assert
:
The (mandatory) string that is passed as the second argument provides an informal description of the assertion. It is shown in the list of properties together with the condition.
The assertion language of the CPROVER tools is identical to the language used for expressions. Note that nondeterminism can be exploited in order to check a range of choices. As an example, the following code fragment asserts that all elements of the array are zero:
The nondeterministic choice will guess the element of the array that is nonzero. The code fragment above is therefore equivalent to:
CPROVER also supports writing function pre and postconditions, using the built-in functions __CPROVER_precondition
and __CPROVER_postcondition
. They can be used to express intent, and at the moment they are just transformed to assertions in the goto program. As such, they can be used as simple assertions in code. However, it is advised to use __CPROVER_precondition
at the beginning of a function’s body, and __CPROVER_postcondition
before the exit points in a function (either the return statements, or the end of the body if the function returns void). The following is an example usage:
A future release of CPROVER will support using these pre and postconditions to create a function contract, which can be used for modular verification.
Future CPROVER releases will support explicit quantifiers with a syntax that resembles Spec#:
Last modified: 2024-11-12 09:30:37 +0200