Decision Procedures and Verifjcation
NAIL094 Petr Kučera
Charles University
2019/20 (10th lecture)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 1 / 46
Decision Procedures and Verifjcation NAIL094 Petr Kuera Charles - - PowerPoint PPT Presentation
Decision Procedures and Verifjcation NAIL094 Petr Kuera Charles University 2019/20 (10th lecture) Petr Kuera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 1 / 46 Program analysis Motivation Is my
Charles University
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 1 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 3 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 3 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 3 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 4 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 4 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
restricting the type of programs restricting the inputs the verifjcation procedure can give up or not terminate
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 5 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 6 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 7 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 7 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 7 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 7 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 7 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for (; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 8 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for (; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 8 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for (; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 8 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 9 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 9 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 9 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 9 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for(; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
1 Run the for loop once,
3
i = 0;
7
next = data[i];
8
i < next && next < N
9
i = i + 1;
10
i < next;
11
data[i] != cookie;
14
Process(data[i]);
2 Exit the while loop in the
10
i = i + 1
10
!(i < next)
7
next = data[i];
8
!(i < next && next < N)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 10 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for(; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
1 Run the for loop once,
3
i = 0;
7
next = data[i];
8
i < next && next < N
9
i = i + 1;
10
i < next;
11
data[i] != cookie;
14
Process(data[i]);
2 Exit the while loop in the
10
i = i + 1
10
!(i < next)
7
next = data[i];
8
!(i < next && next < N)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 10 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for(; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
1 Run the for loop once,
3
i = 0;
7
next = data[i];
8
i < next && next < N
9
i = i + 1;
10
i < next;
11
data[i] != cookie;
14
Process(data[i]);
2 Exit the while loop in the
10
i = i + 1
10
!(i < next)
7
next = data[i];
8
!(i < next && next < N)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 10 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 11 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 11 / 46
use new version of a variable after each write (assignment) timestamped versions of variables — xt represents the value of variable x after t assignments
Replace assignments with equalities Form a conjunction of all assignments and branch subformulas For simplicity, we assume that the function call Process(data[i]); does not change the value of data[i]
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 12 / 46
use new version of a variable after each write (assignment) timestamped versions of variables — xt represents the value of variable x after t assignments
Replace assignments with equalities Form a conjunction of all assignments and branch subformulas For simplicity, we assume that the function call Process(data[i]); does not change the value of data[i]
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 12 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 13 / 46
i1 = 0; next1 = data0[i1]; i1 < next1 && next1 < N0 i2 = i1 + 1; i2 < next1; data0[i2] != cookie0; Process(data0[i2]); i3 = i2 + 1 !(i3 < next1) next2 = data0[i3]; !(i3 < next2 && next2 < N0)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 14 / 46
i1 = 0; next1 = data0[i1]; i1 < next1 && next1 < N0 i2 = i1 + 1; i2 < next1; data0[i2] != cookie0; Process(data0[i2]); i3 = i2 + 1 !(i3 < next1) next2 = data0[i3]; !(i3 < next2 && next2 < N0)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 14 / 46
1 Consider path leading to an assertion 2 Take the path constraint of that path 3 Add negation of the assertion to the path constraint
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 15 / 46
1 Consider path leading to an assertion 2 Take the path constraint of that path 3 Add negation of the assertion to the path constraint
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 15 / 46
1 Consider path leading to an assertion 2 Take the path constraint of that path 3 Add negation of the assertion to the path constraint
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 15 / 46
1 Consider path leading to an assertion 2 Take the path constraint of that path 3 Add negation of the assertion to the path constraint
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 15 / 46
1 Consider path leading to an assertion 2 Take the path constraint of that path 3 Add negation of the assertion to the path constraint
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 15 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
...
1 executes the assignment on Line 3 and 2 checks if i is within bounds
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 16 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for(; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
3
i = 0;
7
next = data[i];
8
i < next && next < N
9
i = i + 1;
10
i < next;
11
data[i] == cookie;
12
i = i + 1;
10
i = i + 1;
10
!(i < next)
7
assert(0 <= i && i < N)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 17 / 46
1
void ReadBlocks(int data[], int cookie)
2
{
3
int i = 0;
4
while (true)
5
{
6
int next;
7
next = data[i];
8
if (!(i < next && next < N)) return;
9
i = i + 1;
10
for(; i < next; i = i + 1) {
11
if (data[i] == cookie)
12
i = i + 1;
13
else
14
Process(data[i]);
15
}
16
}
17
}
3
i = 0;
7
next = data[i];
8
i < next && next < N
9
i = i + 1;
10
i < next;
11
data[i] == cookie;
12
i = i + 1;
10
i = i + 1;
10
!(i < next)
7
assert(0 <= i && i < N)
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 17 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 18 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 19 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 20 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 20 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 20 / 46
The whole program, not just a single path
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 21 / 46
The whole program, not just a single path
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 21 / 46
The whole program, not just a single path
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 21 / 46
The whole program, not just a single path
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 21 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1 Unfold loops k times (k given by the user) 2 Assign the condition of each if statement to a new variable
γ (for guard)
3 Identify points where the control-fmow reconverges 4 Add φ-instructions setting the correct values of variables
For variables that has been changed in either branch Conditional statement checking on the guard variable
5 Translate to formula as before
If-then-else operator to represent φ instructions
6 Satisfying assignment corresponds to one trace (of one path)
Assignment of guard variables determines the branches taken
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 22 / 46
1
for(; i<next; i=i+1){
2
if(data[i]==cookie)
3
i=i+1;
4
else
5
Process(data[i]);
6
}
1
if(i<next){
2
if(data[i]==cookie)
3
i=i+1;
4
else
5
Process(data[i]);
6 7
i=i+1;
8 9
if(i<next) {
10
if(data[i]==cookie)
11
i=i+1;
12
else
13
Process(data[i]);
14 15
i=i+1;
16
}
17
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 23 / 46
1
if(i<next){
2
if(data[i]==cookie)
3
i=i+1;
4
else
5
Process(data[i]);
6 7
i=i+1;
8 9
if(i<next) {
10
if(data[i]==cookie)
11
i=i+1;
12
else
13
Process(data[i]);
14 15
i=i+1;
16
}
17
}
1
γ1 = (i0 < next0);
2
γ2 = (data0[i0] == cookie0);
3
i1 = i0 + 1;
4 5 6
i2 = γ2 ? i1 : i0; //φ
7
i3 = i2 + 1;
8 9
γ3 = (i3 < next0);
10
γ4 = (data0[i3] == cookie0);
11
i4 = i3 + 1;
12 13 14
i5 = γ4 ? i4 : i3; //φ
15
i6 = i5 + 1;
16
i7 = γ3 ? i6 : i3; //φ
17
i8 = γ1 ? i7 : i0; //φ
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 24 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
Considers a subset of possible paths If it detects a bug, it is real Can declare program safe only up to given bound
Considers a superset of possible paths Detected bugs can be spurious If the over-approximation is safe, the original program is safe
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 25 / 46
1 For each loop and each program variable x
If x is modifjed by the loop, add a nondeterministic assignment x = ∗ to the beginning of the loop
2 After each loop, add an assumption that the negation of the loop
3 Replace each while loop with an if statement checking the
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 26 / 46
1 For each loop and each program variable x
If x is modifjed by the loop, add a nondeterministic assignment x = ∗ to the beginning of the loop
2 After each loop, add an assumption that the negation of the loop
3 Replace each while loop with an if statement checking the
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 26 / 46
1 For each loop and each program variable x
If x is modifjed by the loop, add a nondeterministic assignment x = ∗ to the beginning of the loop
2 After each loop, add an assumption that the negation of the loop
3 Replace each while loop with an if statement checking the
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 26 / 46
1
int i = 0;
2
int j = 0;
3 4
while(data[i] != ’\n’)
5
{
6
++i;
7
j = i;
8
}
9
assert(i == j);
1
int i = 0;
2
int j = 0;
3 4
if(data[i] != ’\n’)
5
{
6
i = *;
7
j = *;
8
++i;
9
j = i;
10
}
11
assume(data[i] == ’\n’)
12 13
assert(i == j);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 27 / 46
1
int i = 0;
2
int j = 0;
3 4
if(data[i] != ’\n’)
5
{
6
i = *;
7
j = *;
8
++i;
9
j = i;
10
}
11 12
assume(data[i] == ’\n’)
13 14
assert(i == j);
1
i1 = 0;
2
j1 = 0;
3 4
γ1 = (data0[i1] != ’\n’);
5 6
// i2 is unrestricted
7
// j2 is unrestricted
8
i3 = i2 + 1;
9
j3 = i3;
10
i4 = γ1 ? i3 : i1; // φ
11
j4 = γ1 ? j3 : j1; // φ
12
assume(data0[i4]==’\n’);
13 14
assert(i4==j4);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 28 / 46
1
i1 = 0;
2
j1 = 0;
3 4
γ1 = (data0[i1] != ’\n’);
5 6 7 8
i3 = i2 + 1;
9
j3 = i3;
10
i4 = γ1 ? i3 : i1; // φ
11
j4 = γ1 ? j3 : j1; // φ
12
assume(data0[i4]==’\n’);
13 14
assert(i4==j4);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 29 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 30 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 30 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 30 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
Nondeterministic assignment modelled by incrementing variable counter Assumption translated by conjoining its condition to the formula
Formula is unsatisfjable Program is safe for any number of iterations Abstraction worked, because the assertion does not depend on the previous iterations of the loop
Further assumptions and assertions Loop invariants
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 31 / 46
1
int square(int n) {
2
int r=0;
3
int i=0;
4
while(i!=n){
5
r+=n;
6
++i;
7
}
8
return r;
9
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 32 / 46
1
int square(int n) {
2
int r=0;
3
int i=0;
4
while(i!=n){
5
r+=n;
6
++i;
7
}
8
return r;
9
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 32 / 46
1
int square(int n) {
2
int r=0;
3
int i=0;
4
while(i!=n){
5
r+=n;
6
++i;
7
}
8
assert(r==n*n); // property
9
return r;
10
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 33 / 46
1
int square(int n) {
2
int r=0;
3
int i=0;
4
while(i!=n){
5
r+=n;
6
++i;
7
}
8
assert(r==n*n); // property
9
return r;
10
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 33 / 46
1
int r=0;
2
int i=0;
3
while(i!=n){
4
r+=n;
5
++i;
6
}
7
assert(r==n*n);
1
int r=0;
2
int i=0;
3
if(i!=n){
4
r=*;
5
i=*;
6
r+=n;
7
++i;
8
}
9 10
assume(i==n);
11
assert(r==n*n);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 34 / 46
1
int r=0;
2
int i=0;
3
if(i!=n){
4
r=*;
5
i=*;
6
r+=n;
7
++i;
8
}
9 10
assume(i==n);
11
assert(r==n*n);
1
r1 = 0;
2
i1 = 0;
3
γ1 = (i1 != n0);
4
// i2 is unrestricted
5
// r2 is unrestricted
6
r3 = r2 + n0;
7
i3 = i2 + 1;
8
r4 = γ1 ? r3 : r1; // φ
9
i4 = γ1 ? i3 : i1; // φ
10
assume(i4 == n0);
11
assert(r4 == n0 * n0);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 35 / 46
1
r1 = 0;
2
i1 = 0;
3
γ1 = (i1 != n0);
4
// i2 is unrestricted
5
// r2 is unrestricted
6
r3 = r2 + n0;
7
i3 = i2 + 1;
8
r4 = γ1 ? r3 : r1; // φ
9
i4 = γ1 ? i3 : i1; // φ
10
assume(i4 == n0);
11
assert(r4 == n0 * n0);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 36 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 37 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 37 / 46
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 37 / 46
1
int i = 0;
2
while(i != 10){
3
...
4
++i;
5
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 38 / 46
1
A;
2
while(C){
3
assert(I);
4
B;
5
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 39 / 46
1
A;
2
while(C){
3
assert(I);
4
B;
5
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 39 / 46
1
A;
2
assert(C => I);
1
assume(C & I);
2
B;
3
assert(C -> I);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 40 / 46
1
A;
2
assert(C => I);
1
assume(C & I);
2
B;
3
assert(C -> I);
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 40 / 46
1
int i = 0;
2
while(i != 10){
3
++i;
4
}
1
int i = 0;
2
assert(i != 10 -> (i >= 0 && i < 10));
1
assume(i != 10 && i >= 0 && i < 10);
2
++i;
3
assert(i != 10 -> (i >= 0 && i < 10));
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 41 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1 Assertion that Il holds before the nondeterministic assignments to
the loop variables
This establishes the base case
2 Assumption that C ∧ Il holds after the nondeterministic assignments
to the loop variables
This is the induction hypothesis
3 Assertion that C ⇒ Il holds at the end of the loop body
This proves the induction step
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 42 / 46
1
int i = 0;
2
while(i != 10){
3
++i;
4
}
1
int i = 0;
2
if(i != 10){
3
assert (i>=0 && i<10); // base case
4
i=*;
5
assume (i>=0 && i<10); // induction hypothesis
6
++i;
7
assert(i != 10 -> (i >= 0 && i < 10)); // step case
8
}
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 43 / 46
1
int r=0;
2
int i=0;
3
while(i!=n){
4
r+=n;
5
++i;
6
}
7
assert(r==n*n); // property
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 44 / 46
1
int r=0;
2
int i=0;
3
if(i!=n){
4
assert(r==i*n); // base case
5
r=*;
6
i=*;
7
assume(i!=n && r==i*n); // induction hypothesis
8
r+=n;
9
++i;
10
assert(i!=n -> r==i*n); // step case
11
}
12
assume(i==n); // cycle abstraction
13
assert(r==n*n); // property
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 45 / 46
TRUE is always an invariant, but not very useful
Constructing candidates from predicates appearing in the code Combining program variables with usual relational operators
Petr Kučera (Charles University) Decision Procedures and Verifjcation 2019/20 (10th lecture) 46 / 46