Skip to content

Commit 5bde5d4

Browse files
author
Enrico Steffinlongo
committed
Incremental smt2 backend documentation draft
1 parent 8848ad8 commit 5bde5d4

File tree

2 files changed

+204
-1
lines changed

2 files changed

+204
-1
lines changed

doc/cprover-manual/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@
3232
* [Integration into Build Systems with goto-cc](goto-cc/)
3333
* [Integration with Visual Studio builds](visual-studio/)
3434

35-
10. [The CPROVER API Reference](api/)
35+
10. Solvers
36+
37+
* [Incremental SMT solver](smt2-incr/)
38+
39+
11. [The CPROVER API Reference](api/)

doc/cprover-manual/smt2-incr.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
[CPROVER Manual TOC](../)
2+
3+
## SMT 2 incremental backend
4+
5+
CBMC supports many different smt solver backends. This document describes how to use the incremental
6+
smt2 solver. For other solver usages use `cbmc --help`.
7+
8+
The incremental smt2 solver backend of CBMC is a solver-agnostic smt backend that allows the user to
9+
specify which smtlib 2.6 compliant solver CBMC should use. This choice increases the flexibility of
10+
CBMC as the user can easily switch solver at runtime also allowing custom parametrization of the
11+
solver to be used. Further, the backend exploits the incremental mode of the smt solver, so subsequent
12+
incremental SMT solver queries do not require a full re-evaluation of the entire SMT formula.
13+
14+
### Supported features
15+
16+
The incremental smt2 solver supports the following CBMC C features:
17+
18+
* Integral types. These are encoded into smt by using bit-vectors,
19+
* Pointers. Encoded by using smt bit-vectors,
20+
* Arrays. Encoded by using smt array theory.
21+
22+
Currently unupported features include floating-point types, structs and unions.
23+
24+
For more details on which features are supported see regression
25+
tests in [`regression/cbmc-incr-smt2`](../regression/cbmc-incr-smt2).
26+
27+
### Requisites
28+
29+
To run the incremental smt2 backend it is necessary to have an smt solver that:
30+
31+
* is runnable from the command line,
32+
* supports interactive mode from the command line,
33+
* supports smtlib 2.6 input,
34+
* supports incremental mode.
35+
36+
Because of the way supported features are encoded the smt solver should be capable to handle arrays,
37+
bit-vectors, uninterpreted functions and quantifiers. Also, the solver should work with the `ALL`
38+
smt theory.
39+
40+
At the time of writing the smt solvers tested in CI
41+
are [Z3](https://github.com/Z3Prover/z3)
42+
and [CVC5](https://cvc5.github.io/).
43+
44+
### Usage
45+
46+
To use the incremental SMT2 backend it is enough to add the `--incremental-smt2-solver` argument to
47+
the CBMC command line followed by the command to invoke the chosen solver using smtlib 2.6 input in
48+
interactive mode.
49+
50+
Note that the use of the `--slice-formula` option is recommended at this time to slice out
51+
unnecessary code (that may not be supported at the moment) and this can also improve performance.
52+
53+
Here are two examples to show how to analyse a file `program.c` using Z3 and CVC5 solvers.
54+
55+
To use the Z3 SMT solver:
56+
57+
```shell
58+
cbmc --slice-formula --incremental-smt2-solver 'z3 -smt2 -in' program.c
59+
```
60+
61+
If `z3` is not on the `PATH`, then it is enough to replace `'z3 -smt2 -in'`
62+
with `'<command-to-execute-z3> -smt2 -in'`,
63+
64+
Similarly to execute CBMC using the CVC5 solver:
65+
66+
```shell
67+
cbmc --slice-formula --incremental-smt2-solver 'cvc5 --lang=smtlib2.6 --incremental' program.c
68+
```
69+
70+
As the command to execute the solver is left to the user, it is possible to fine-tune it by passing
71+
extra parameters (when the solver allows so). For example Z3 allows to set certain parameters by
72+
adding `param_name=value` to the command line, so to use the Z3 solver with `well_sorted_check`
73+
property set to `false` the following has to be run:
74+
75+
```shell
76+
cbmc --slice-formula --incremental-smt2-solver 'z3 -smt2 -in well_sorted_check=false' program.c
77+
```
78+
79+
### Examples
80+
81+
Given a C program `program.c` as follows:
82+
83+
```C
84+
int main()
85+
{
86+
int x, y;
87+
if(x != 0)
88+
__CPROVER_assert(y != 4, "Assert of inequality to 4.");
89+
else
90+
__CPROVER_assert(y != 2, "Assert of inequality to 2.");
91+
int z = y;
92+
return 0;
93+
}
94+
```
95+
96+
To analyze it we should run:
97+
98+
```shell
99+
cbmc --incremental-smt2-solver 'z3 -smt2 -in' --slice-formula program.c
100+
```
101+
102+
We will get the verification results as follows:
103+
104+
```text
105+
CBMC version 5.70.0 (cbmc-5.70.0-53-g20535ad14d) 64-bit x86_64 macos
106+
Parsing program.c
107+
Converting
108+
Type-checking program
109+
Generating GOTO Program
110+
Adding CPROVER library (x86_64)
111+
Removal of function pointers and virtual functions
112+
Generic Property Instrumentation
113+
Running with 8 object bits, 56 offset bits (default)
114+
Starting Bounded Model Checking
115+
Runtime Symex: 0.00168879s
116+
size of program expression: 45 steps
117+
slicing removed 23 assignments
118+
Generated 2 VCC(s), 2 remaining after simplification
119+
Runtime Postprocess Equation: 2.1865e-05s
120+
Passing problem to incremental SMT2 solving via "z3 -smt2 -in"
121+
```
122+
123+
Note here that the solver used by CBMC is `"z3 -smt2 -in"` as specified by the user.
124+
125+
```text
126+
[continues]
127+
converting SSA
128+
Runtime Convert SSA: 0.00130809s
129+
Running incremental SMT2 solving via "z3 -smt2 -in"
130+
Runtime Solver: 0.0738685s
131+
Runtime decision procedure: 0.0765297s
132+
Running incremental SMT2 solving via "z3 -smt2 -in"
133+
Runtime Solver: 0.00535358s
134+
Runtime decision procedure: 0.00570765s
135+
136+
** Results:
137+
program.c function main
138+
[main.assertion.1] line 5 Assert of inequality to 4.: FAILURE
139+
[main.assertion.2] line 7 Assert of inequality to 2.: FAILURE
140+
141+
** 2 of 2 failed (3 iterations)
142+
VERIFICATION FAILED
143+
```
144+
145+
As expected CBMC returns `FAILURE` for both assertions at lines `5` and `7`.
146+
147+
The incremental smt2 backend also supports trace generation, so to get a trace that fails the
148+
assertions the `--trace` argument should be added, so the command to run is:
149+
150+
```shell
151+
cbmc --incremental-smt2-solver 'z3 -smt2 -in' --slice-formula --trace program.c
152+
```
153+
154+
This will append the trace to CBMC's output as follows:
155+
156+
```text
157+
CBMC version 5.70.0 (cbmc-5.70.0-53-g20535ad14d) 64-bit x86_64 macos
158+
Parsing program.c
159+
[...]
160+
[main.assertion.1] line 5 Assert of inequality to 4.: FAILURE
161+
[main.assertion.2] line 7 Assert of inequality to 2.: FAILURE
162+
163+
Trace for main.assertion.1:
164+
165+
State 6 file program.c function main line 3 thread 0
166+
----------------------------------------------------
167+
x=-1 (11111111 11111111 11111111 11111111)
168+
169+
State 7 file program.c function main line 3 thread 0
170+
----------------------------------------------------
171+
y=4 (00000000 00000000 00000000 00000100)
172+
173+
Violated property:
174+
file program.c function main line 5 thread 0
175+
Assert of inequality to 4.
176+
y != 4
177+
178+
179+
180+
Trace for main.assertion.2:
181+
182+
State 6 file program.c function main line 3 thread 0
183+
----------------------------------------------------
184+
x=0 (00000000 00000000 00000000 00000000)
185+
186+
State 7 file program.c function main line 3 thread 0
187+
----------------------------------------------------
188+
y=2 (00000000 00000000 00000000 00000010)
189+
190+
Violated property:
191+
file program.c function main line 7 thread 0
192+
Assert of inequality to 2.
193+
y != 2
194+
195+
196+
197+
** 2 of 2 failed (3 iterations)
198+
VERIFICATION FAILED
199+
```

0 commit comments

Comments
 (0)