Skip to content

Commit 4cdb871

Browse files
author
Enrico Steffinlongo
committed
Incremental smt2 backend documentation draft
1 parent 20535ad commit 4cdb871

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

doc/smt2-incr.md

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

0 commit comments

Comments
 (0)