Skip to content

Commit 93fb568

Browse files
Add tests for shadow memory of linked lists
Test deterministic and nondeterministic pointers to elements.
1 parent dcf2a81 commit 93fb568

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
struct STRUCTNAME
5+
{
6+
int data;
7+
struct STRUCTNAME *next;
8+
};
9+
10+
int main()
11+
{
12+
__CPROVER_field_decl_local("field1", (_Bool)0);
13+
14+
struct STRUCTNAME a;
15+
16+
assert(__CPROVER_get_field(&a, "field1") == 0);
17+
assert(__CPROVER_get_field(&(a.data), "field1") == 0);
18+
assert(__CPROVER_get_field(&(a.next), "field1") == 0);
19+
// Expect a warning here. We return 0 as default value.
20+
assert(__CPROVER_get_field(a.next, "field1") == 0);
21+
// Expect a warning here. We return 0 as default value.
22+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
23+
24+
__CPROVER_set_field(&(a.data), "field1", 1);
25+
__CPROVER_set_field(&(a.next), "field1", 1);
26+
// Expect a warning here. We cannot set SM because it doesn't exist.
27+
__CPROVER_set_field(&((*(a.next)).data), "field1", 1);
28+
// Expect a warning here. We cannot set SM because it doesn't exist.
29+
__CPROVER_set_field(&((*(*(a.next)).next).data), "field1", 1);
30+
31+
assert(__CPROVER_get_field(&a, "field1") == 1);
32+
assert(__CPROVER_get_field(&(a.data), "field1") == 1);
33+
assert(__CPROVER_get_field(&(a.next), "field1") == 1);
34+
// Expect a warning here. We return 0 as default value.
35+
assert(__CPROVER_get_field(a.next, "field1") == 0);
36+
// Expect a warning here. We return 0 as default value.
37+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
38+
39+
struct STRUCTNAME b;
40+
a.next = &b;
41+
42+
assert(__CPROVER_get_field(&a, "field1") == 1);
43+
assert(__CPROVER_get_field(&(a.data), "field1") == 1);
44+
assert(__CPROVER_get_field(&(a.next), "field1") == 1);
45+
// No warning here.
46+
assert(__CPROVER_get_field(a.next, "field1") == 0);
47+
// Expect a warning here. We return 0 as default value.
48+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
49+
50+
// No warning here.
51+
__CPROVER_set_field(&((*(a.next)).data), "field1", 1);
52+
// Expect a warning here. We cannot set SM because it doesn't exist.
53+
__CPROVER_set_field(&((*(*(a.next)).next).data), "field1", 1);
54+
55+
// No warning here.
56+
assert(__CPROVER_get_field(a.next, "field1") == 1);
57+
// Expect a warning here. We return 0 as default value.
58+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
59+
60+
struct STRUCTNAME c;
61+
b.next = &c;
62+
// No warning here.
63+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
64+
65+
// No warning here.
66+
__CPROVER_set_field(&((*(*(a.next)).next).data), "field1", 1);
67+
68+
// No warning here.
69+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 1);
70+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FUTURE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
struct STRUCTNAME
5+
{
6+
int data;
7+
struct STRUCTNAME *next;
8+
};
9+
10+
int main()
11+
{
12+
int x;
13+
int y;
14+
__CPROVER_assume(x > 0);
15+
__CPROVER_assume(y > x);
16+
17+
// This is always != 0, but symex doesn't know that.
18+
// So, we force it to produce a case split on shadow memory access.
19+
int choice = y / x;
20+
21+
__CPROVER_field_decl_local("field1", (_Bool)0);
22+
23+
struct STRUCTNAME a;
24+
a.next = (struct STRUCTNAME *)0;
25+
26+
assert(__CPROVER_get_field(&a, "field1") == 0);
27+
assert(__CPROVER_get_field(&(a.data), "field1") == 0);
28+
assert(__CPROVER_get_field(&(a.next), "field1") == 0);
29+
30+
__CPROVER_set_field(&(a.data), "field1", 1);
31+
__CPROVER_set_field(&(a.next), "field1", 1);
32+
33+
assert(__CPROVER_get_field(&a, "field1") == 1);
34+
assert(__CPROVER_get_field(&(a.data), "field1") == 1);
35+
assert(__CPROVER_get_field(&(a.next), "field1") == 1);
36+
37+
struct STRUCTNAME b;
38+
b.next = (struct STRUCTNAME *)0;
39+
if(choice)
40+
{
41+
a.next = &b;
42+
}
43+
44+
assert(__CPROVER_get_field(&a, "field1") == 1);
45+
assert(__CPROVER_get_field(&(a.data), "field1") == 1);
46+
assert(__CPROVER_get_field(&(a.next), "field1") == 1);
47+
// No warning here.
48+
assert(__CPROVER_get_field(a.next, "field1") == 0);
49+
50+
// No warning here.
51+
__CPROVER_set_field(&((*(a.next)).data), "field1", 1);
52+
53+
// No warning here.
54+
assert(__CPROVER_get_field(a.next, "field1") == 1);
55+
56+
struct STRUCTNAME c;
57+
c.next = (struct STRUCTNAME *)0;
58+
if(choice)
59+
{
60+
b.next = &c;
61+
}
62+
63+
// No warning here.
64+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 0);
65+
66+
// No warning here.
67+
__CPROVER_set_field(&((*(*(a.next)).next).data), "field1", 1);
68+
69+
// No warning here.
70+
assert(__CPROVER_get_field((*(a.next)).next, "field1") == 1);
71+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FUTURE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

0 commit comments

Comments
 (0)