A Talk I gave at IBM.

Download Report

Transcript A Talk I gave at IBM.

A Framework for describing
recursive data structures
Kenneth Roe
Scott Smith
Shape analysis and Recursive
data structures
• The objective is to verify the integrity of
dynamic data structures such as lists and
trees
• Based on principles of separation logic
• Builds on work from Byron Cook and
company
• The key contribution is reasoning about data
structures more complex than linked lists
– Regular expressions are used to describe paths
through data structures
Sample progam Data
Structures
• struct list {
struct list *n;
struct tree *t;
int data; };
struct tree {
struct tree *l, *r;
int value;};
Sample program code
•
Struct list *p;
void build pre order(struct tree *r)
{
struct list *i = NULL, *n, *x;
struct tree *t = r;
p = NULL;
while (t) {
n=p;
p = malloc(sizeof(struct list));
p->l = t;
p->n = n;
if (t->l==NULL && t->r==NULL) {
if (i==NULL) { t = NULL;}
else { struct list *tmp = i->n; t = i->l; free(l); i = tmp;} }
else if (t->r==NULL) {t = t->l; } else
if (t->l==NULL) {t = t->r; }
else {n = i; i = malloc(sizeof(struct list)); i->n = n; x = t->r; i->t = x; t = t->l; } } }
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
…
• The program maintains two well formed
linked lists, the heads of which are pointed to
by i and p.
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
…
• The program maintains a well formed
tree pointed to by r.
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
…
• t always points to an element in the tree
rooted at r.
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
…
• The two lists and the tree do not share
any nodes.
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
…
• Other than the memory used for the two lists
and the tree, no other heap memory is
allocated.
Invariants
p
r
i
n t
l 4 r
n t
n t
…
l 2 r
l 1 r
l 6 r
l 3 r
l 5 r
n t
nil
• The l field of every element in both list
structures points to an element in the tree.
State representation
Rn (p, )
*
R(l |r) (r, )
n t
n t
…
* Rn (i, )
l 4 r
l 2 r
l 1 r
n t
l 6 r
l 3 r
l 5 r
n t
nil
• r ↝(l|r)* t ∧
(∀v.∃z.p ↝n*v → v↝tz∧ r↝(l |r)*z)∧
(∀v.∃z.i ↝n*v → v↝tz∧r↝(l |r)*z)
Backward reasoning
•
•
•
•
Logic rules for back propagation
Generated preconditions imply post condition
Not guaranteed to get weakest pre-condition
The system also contains rules for merging
states
– Becomes necessary when joining the branches of
an “if” statement
Back-chaining example
Last line of source code:
n = i;
i = malloc(sizeof(struct list));
i->n = n;
x = t->r;
i->t = x;
t = t->l;
Back-chaining example
p
r
n t
l 4 r
n t
…
l 2 r
l 1 r
t
n t
l 6 r
l 3 r
i
l 5 r
n t
nil
• r ↝(l |r)* t ∧
(∀v.∃z.p↝n*v → v↝tz∧ r↝(l |r)*z)∧ (∀v.∃z. i↝n*v
→ v↝tz∧r↝(l |r)*z)
• Rn (p, ) * R(l |r) (r, ) * Rn (i, )
Back-chaining example
t = t->l
p
r
n t
l 4 r
n t
…
l 2 r
l 1 r
t
n t
l 6 r
l 3 r
i
l 5 r
n t
nil
• t↝lq ∧r↝(l |r)*q ∧
(∀v.∃z.p↝n* v → v↝lz∧ r↝(l |r)*z)∧ (∀v.∃z.
i↝n*v → v↝lz ∧ r↝(l|r)*z)
• Rn (p, ) * R(l |r) (r, ) * Rn (i, )
Back-chaining example
t = t->l
p
r
n t
l 4 r
n t
…
l 2 r
l 1 r
t
n t
l 6 r
l 3 r
i
l 5 r
n t
nil
• t↝lq ∧r↝(l|r)* t ∧
(∀v.∃z.p↝n*v → v ↝l z∧ r↝(l|r)*z)∧ (∀v.∃z.i↝n*v
→ v ↝l z∧r↝(l|r)*z)
• Rn (p, ) * R(l|r) (r, ) * Rn (i, )
Back-chaining example
We have back propagated over the last statement. We have several more
statements to go
n = i;
i = malloc(sizeof(struct list));
i->n = n;
x = t->r;
i->t = x;
t = t->l;
Back-chaining example
After back-propagating over the remaining statements, we
end up with the following which is almost our original
invariant:
t ↝l q ∧ t ↝r e ∧
r↝(l|r)*t
∧(∀v.∃z. p↝n*v → v↝lz∧
r↝(l |r)*z)∧
(∀v.∃z. i↝n*v → v↝lz∧r↝(l |r)*z) |
Rn (p, ) * R(l |r) (r, ) * Rn (i, )
Future work
•
•
•
•
Arrays
Length predicate
Handling procedures
COQ verification (in progress)