Shape Analysis via Symbolic Memory Graphs and Conversion from Pointers to Containers
Kamil Dudka Luk´ aˇ s Hol´ ık Petr Peringer Marek Trt´ ık Tom´ aˇ s Vojnar
Brno University of Technology, Czech Republic Dagstuhl, 2/11/2015
Shape Analysis via Symbolic Memory Graphs and Conversion from - - PowerPoint PPT Presentation
Shape Analysis via Symbolic Memory Graphs and Conversion from Pointers to Containers Kamil Dudka Luk a s Hol k Petr Peringer Marek Trt k Tom a s Vojnar Brno University of Technology, Czech Republic Dagstuhl, 2/11/2015
Brno University of Technology, Czech Republic Dagstuhl, 2/11/2015
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 2 / 25
...
hfo nfo pfo
list_head custom_record next prev next prev next prev
size(ptr),
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 3 / 25
...
hfo nfo pfo
list_head custom_record next prev next prev next prev
hfo,lst hfo,fst 0,ptr 0,reg pfo,ptr size(ptr),ptr nfo,ptr
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 3 / 25
...
hfo nfo pfo
list_head custom_record next prev next prev next prev
hfo,lst hfo,fst 0,ptr 0,reg pfo,ptr size(ptr),ptr nfo,ptr
◮ objects (allocated space) and values (addresses, integers), ◮ has-value and points-to edges. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 3 / 25
hfo,lst hfo,fst 0,ptr 0,reg pfo,ptr size(ptr),ptr nfo,ptr
◮ regions, i.e., individual blocks of memory, ◮ optional regions, i.e., a region or NULL, ◮ doubly-linked list segments (DLSs), ◮ singly-linked list segments (SLSs),
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 4 / 25
...
hfo nfo pfo
list_head custom_record next prev next prev next prev
◮ N+ for any N ≥ 0 or ◮ 0 or 1. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 5 / 25
...
hfo nfo pfo
list_head custom_record next prev next prev next prev
◮ N+ for any N ≥ 0 or ◮ 0 or 1.
◮ shared: each node points to the same object, or ◮ nested: each node points to a separate copy of the object.
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 5 / 25
a1
region1 region2
size1
size2
a1
size=size1 size=size2
has-value points-to region1 region2 edge edge
◮ field offset, ◮ type of the value stored in the field. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 6 / 25
a1
region1 region2
size1
size2
a1
size=size1 size=size2
has-value points-to region1 region2 edge edge
◮ field offset, ◮ type of the value stored in the field.
◮ target offset, ◮ target specifier: first/last/each node of a DLS,
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 6 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 7 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 7 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 7 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 7 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 8 / 25
◮ ratio of loss of precision and number of collapsed objects. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 9 / 25
◮ pointer arithmetics, address alignment, ◮ interval-based pointers, interval-sized objects, ◮ block operations, re-intepretation of nullified blocks.
http://www.fit.vutbr.cz/research/groups/verifit/tools/predator
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 11 / 25
typedef struct SNode { int x; struct SNode *f; struct SNode *b; } Node; #define NEW(T) (T*)malloc(sizeof(T)) 1 Node *h=0, *t=0; list<Node> L; 2 while (nondet()) { while (nondet()) { 3 Node *p=NEW(Node); Node *p=L.push_back(); 4 if (h==NULL) 5 h=p; 6 else 7 t->f=p; 8 p->f=NULL; 9 p->x=0; p->x=0; 10 p->b=t; 11 t=p; 12 } } ... ... 13 while (t) { while (!L.empty()) 14 Node *p=t->b; L.pop_back(); 15 if (p) p->n=NULL; 16 else h=NULL; 17 free(t); 18 t=p; 19 } Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 13 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 14 / 25
◮ separation of pointer and non-pointer analyses. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 14 / 25
◮ separation of pointer and non-pointer analyses.
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 14 / 25
1 A CFG annotated by:
◮ shape invariants,
◮ source/target links between shape invariants and their elements:
11 t=p p h t
L
p h t
L
p h t p=
L
h t h t
L L
2
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 15 / 25
2 Container shapes to be searched for in shape invariants:
◮ Specified in the formalism used for encoding shape invariants. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 16 / 25
2 Container shapes to be searched for in shape invariants:
◮ Specified in the formalism used for encoding shape invariants. ◮ Currently, fixed to null-terminated doubly-linked lists,
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 16 / 25
2 Container shapes to be searched for in shape invariants:
◮ Specified in the formalism used for encoding shape invariants. ◮ Currently, fixed to null-terminated doubly-linked lists,
◮ In SMGs: null-terminated, doubly-linked sequences of
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 16 / 25
3 Specification of destructive container operations consisting of:
◮ destructive pointer updates x → sel = y, ◮ allocation and deallocation. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 17 / 25
3 Specification of destructive container operations consisting of:
◮ destructive pointer updates x → sel = y, ◮ allocation and deallocation.
◮ encoded using the chosen formalism, e.g., SMGs, ◮ with linked elements of the in/out configurations, ◮ sets: different variants of the operations,
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 17 / 25
3 Specification of destructive container operations consisting of:
◮ destructive pointer updates x → sel = y, ◮ allocation and deallocation.
◮ encoded using the chosen formalism, e.g., SMGs, ◮ with linked elements of the in/out configurations, ◮ sets: different variants of the operations,
4 Specification of non-destructive container operations:
◮ Currently, a fixed set of such operations – up to the selectors used:
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 17 / 25
◮ which pointer links change and ◮ which regions appear/disappear. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 18 / 25
◮ which pointer links change and ◮ which regions appear/disappear. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 18 / 25
◮ which pointer links change and ◮ which regions appear/disappear.
◮ memory-safe permutations of the implementing statements, ◮ possibly interleaved with other non-interfering statements. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 18 / 25
1 h= 2 nondet() 3 p=malloc() 4 h!= 7 t->f=p 8 p->f= 9 p->x=0 10 p->b=t t= 12 5 h=p p h t h== 11 t=p h t h t h t h t
L L
h t h t
L L
p h t p h t p h t p h t
L
p h t
L
p h t
L
p h t
L
p h t p h t p h t p h t p h t p h t p h t p h t
L
p h t
L
p h t 2 1 2 1 p=
L
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 19 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 20 / 25
◮ move non-implementing statements
◮ consider locations in between
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 20 / 25
◮ move non-implementing statements
◮ consider locations in between
1 h= 2 nondet() 3 p=malloc() 4 h!= 7 t->f=p 8 p->f= 9 p->x=0 10 p->b=t t= 12 5 h=p p h t h== 11 t=p h t h t h t h t
L L
h t h t
L L
p h t p h t p h t p h t
L
p h t
L
p h t
L
p h t
L
p h t p h t p h t p h t p h t p h t p h t p h t
L
p h t
L
p h t 2 1 2 1 p=
L
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 20 / 25
1 a container operation ∆τ, 2 a replacement location ℓτ, and 3 in/out parameter valuations σin
τ /σout τ
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 21 / 25
1 a container operation ∆τ, 2 a replacement location ℓτ, and 3 in/out parameter valuations σin
τ /σout τ
◮ each TC τ is minimal and implements ∆τ wrt. ℓτ and σin
τ /σout τ
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 21 / 25
1 a container operation ∆τ, 2 a replacement location ℓτ, and 3 in/out parameter valuations σin
τ /σout τ
◮ each TC τ is minimal and implements ∆τ wrt. ℓτ and σin
τ /σout τ
◮ In all traces, the same replacement locations and implementing edges. ◮ Overlapping TCs agree on replacement locations, implementing edges,
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 21 / 25
1 a container operation ∆τ, 2 a replacement location ℓτ, and 3 in/out parameter valuations σin
τ /σout τ
◮ each TC τ is minimal and implements ∆τ wrt. ℓτ and σin
τ /σout τ
◮ In all traces, the same replacement locations and implementing edges. ◮ Overlapping TCs agree on replacement locations, implementing edges,
◮ a container is manipulated destructively by container operations only. ◮ Natural: otherwise conversions from/to containers needed! Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 21 / 25
1 a container operation ∆τ, 2 a replacement location ℓτ, and 3 in/out parameter valuations σin
τ /σout τ
◮ each TC τ is minimal and implements ∆τ wrt. ℓτ and σin
τ /σout τ
◮ In all traces, the same replacement locations and implementing edges. ◮ Overlapping TCs agree on replacement locations, implementing edges,
◮ a container is manipulated destructively by container operations only. ◮ Natural: otherwise conversions from/to containers needed!
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 21 / 25
◮ Need to agree for all overlapping TCs: otherwise prune again.
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 22 / 25
◮ Need to agree for all overlapping TCs: otherwise prune again.
◮ Detected by looking at annotations of two neighbouring locations only. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 22 / 25
◮ Need to agree for all overlapping TCs: otherwise prune again.
◮ Detected by looking at annotations of two neighbouring locations only. ◮ Iteration:
◮ Iteration initialization:
◮ Tests:
(e.g., being empty).
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 22 / 25
◮ http://www.fit.vutbr.cz/research/groups/verifit/tools/predator-adt/
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 23 / 25
◮ http://www.fit.vutbr.cz/research/groups/verifit/tools/predator-adt/
◮ 18 programs using different implementations of typical list operations:
◮ Further variants generated by legal permutations of the statements. ◮ Programs creating, traversing, filtering, and searching lists taken from
◮ Programs using DLLs with head/tail pointers. ◮ Programs dealing with circular DLLs. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 23 / 25
◮ Predator: shape analysis, but (almost) no non-pointer data. ◮ J2BP: predicate abstraction over containers, no shape analysis. Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 24 / 25
◮ Predator: shape analysis, but (almost) no non-pointer data. ◮ J2BP: predicate abstraction over containers, no shape analysis. ◮ Due to separation of analyses, we verified programs that neither
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 24 / 25
Dudka, Hol´ ık, Peringer, Trt´ ık, Vojnar SMGs for going from Pointers to Containers 25 / 25