Skip to content

Commit b0e105e

Browse files
Add tests for shadow memory for function parameters
Shadow memory mirrors pass-by-value and pass-by-reference semantics. Value parameters have their own shadow memory instances.
1 parent 8695e6b commit b0e105e

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
struct STRUCTNAME
5+
{
6+
int x1;
7+
int B1[3];
8+
};
9+
10+
void f_int_val(int x)
11+
{
12+
// x has its own shadow memory
13+
assert(__CPROVER_get_field(&x, "field1") == 0);
14+
__CPROVER_set_field(&x, "field1", 1);
15+
assert(__CPROVER_get_field(&x, "field1") == 1);
16+
}
17+
18+
void f_intptr_ptr0(int *x)
19+
{
20+
// we access the argument's shadow memory
21+
assert(__CPROVER_get_field(x, "field1") == 255);
22+
__CPROVER_set_field(x, "field1", 1);
23+
assert(__CPROVER_get_field(x, "field1") == 1);
24+
}
25+
26+
void f_intptr_ptr1(int *x)
27+
{
28+
// we access the argument's shadow memory
29+
assert(__CPROVER_get_field(x, "field1") == 1);
30+
__CPROVER_set_field(x, "field1", 2);
31+
assert(__CPROVER_get_field(x, "field1") == 2);
32+
}
33+
34+
void f_intptr_val(int *x)
35+
{
36+
// x has its own shadow memory
37+
assert(__CPROVER_get_field(&x, "field1") == 0);
38+
__CPROVER_set_field(&x, "field1", 3);
39+
assert(__CPROVER_get_field(&x, "field1") == 3);
40+
}
41+
42+
void f_intarray_ptr0(int x[])
43+
{
44+
// we access the argument's shadow memory
45+
assert(__CPROVER_get_field(&(x[0]), "field1") == 255);
46+
__CPROVER_set_field(&(x[0]), "field1", 1);
47+
assert(__CPROVER_get_field(&(x[0]), "field1") == 1);
48+
}
49+
50+
void f_intarray_ptr1(int x[])
51+
{
52+
// we access the argument's shadow memory
53+
assert(__CPROVER_get_field(&(x[0]), "field1") == 1);
54+
__CPROVER_set_field(&(x[0]), "field1", 2);
55+
assert(__CPROVER_get_field(&(x[0]), "field1") == 2);
56+
}
57+
58+
void f_struct_val(struct STRUCTNAME x)
59+
{
60+
// x has its own shadow memory
61+
assert(__CPROVER_get_field(&(x.B1[2]), "field1") == 0);
62+
__CPROVER_set_field(&(x.B1[2]), "field1", 5);
63+
assert(__CPROVER_get_field(&(x.B1[2]), "field1") == 5);
64+
}
65+
66+
void f_structptr_ptr0(struct STRUCTNAME *x)
67+
{
68+
// we access the argument's shadow memory
69+
assert(__CPROVER_get_field(&(x->B1[2]), "field1") == 255);
70+
__CPROVER_set_field(&(x->B1[2]), "field1", 1);
71+
assert(__CPROVER_get_field(&(x->B1[2]), "field1") == 1);
72+
}
73+
74+
void f_structptr_ptr1(struct STRUCTNAME *x)
75+
{
76+
// we access the argument's shadow memory
77+
assert(__CPROVER_get_field(&(x->B1[2]), "field1") == 1);
78+
__CPROVER_set_field(&(x->B1[2]), "field1", 2);
79+
assert(__CPROVER_get_field(&(x->B1[2]), "field1") == 2);
80+
}
81+
82+
void f_structptr_val(struct STRUCTNAME *x)
83+
{
84+
// x has its own shadow memory
85+
assert(__CPROVER_get_field(&x, "field1") == 0);
86+
__CPROVER_set_field(&x, "field1", 7);
87+
assert(__CPROVER_get_field(&x, "field1") == 7);
88+
}
89+
90+
void f_int_local(int rec, int value)
91+
{
92+
// locals in each recursive call instance have their own shadow memory
93+
int x;
94+
assert(__CPROVER_get_field(&x, "field1") == 0);
95+
__CPROVER_set_field(&x, "field1", value);
96+
assert(__CPROVER_get_field(&x, "field1") == value);
97+
if(rec)
98+
{
99+
f_int_local(0, value + 1);
100+
assert(__CPROVER_get_field(&x, "field1") == value);
101+
}
102+
}
103+
104+
int main()
105+
{
106+
__CPROVER_field_decl_local("field1", (char)0);
107+
int x;
108+
__CPROVER_set_field(&x, "field1", 255);
109+
f_int_val(x);
110+
f_int_val(x);
111+
f_intptr_ptr0(&x);
112+
f_intptr_ptr1(&x);
113+
f_intptr_val(&x);
114+
f_intptr_val(&x);
115+
int y[1];
116+
__CPROVER_set_field(&y[0], "field1", 255);
117+
f_intarray_ptr0(y);
118+
f_intarray_ptr1(y);
119+
struct STRUCTNAME z;
120+
__CPROVER_set_field(&z, "field1", 255);
121+
f_struct_val(z);
122+
f_struct_val(z);
123+
f_structptr_ptr0(&z);
124+
f_structptr_ptr1(&z);
125+
f_structptr_val(&z);
126+
f_structptr_val(&z);
127+
f_int_local(1, 1);
128+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FUTURE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
^Shadow memory: cannot get_field for

0 commit comments

Comments
 (0)