Skip to content

Commit 632e76d

Browse files
Add tests for shadow memory for string constants
Const char * parameters and pointers into string constants.
1 parent 3d9ff65 commit 632e76d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <assert.h>
2+
3+
void test(const char *str)
4+
{
5+
// Set shadow memory for some characters
6+
for(int i = 1; i < 6; ++i)
7+
{
8+
__CPROVER_set_field(&str[i], "field1", 1);
9+
}
10+
11+
// Copy string constant into buffer
12+
char buffer[10];
13+
for(int i = 0; i < 7; ++i)
14+
{
15+
buffer[i] = str[i];
16+
__CPROVER_set_field(
17+
&buffer[i], "field1", __CPROVER_get_field(&str[i], "field1"));
18+
}
19+
20+
// Check that shadow memory has not been copied
21+
for(int i = 0; i < 7; ++i)
22+
{
23+
assert(__CPROVER_get_field(&buffer[0], "field1") == 0);
24+
}
25+
26+
// Copy shadow memory
27+
for(int i = 0; i < 7; ++i)
28+
{
29+
__CPROVER_set_field(
30+
&buffer[i], "field1", __CPROVER_get_field(&str[i], "field1"));
31+
}
32+
33+
// Check that shadow memory has been copied
34+
assert(__CPROVER_get_field(&buffer[0], "field1") == 0);
35+
assert(__CPROVER_get_field(&buffer[1], "field1") == 1);
36+
assert(__CPROVER_get_field(&buffer[2], "field1") == 1);
37+
assert(__CPROVER_get_field(&buffer[3], "field1") == 1);
38+
assert(__CPROVER_get_field(&buffer[4], "field1") == 1);
39+
assert(__CPROVER_get_field(&buffer[5], "field1") == 1);
40+
assert(__CPROVER_get_field(&buffer[6], "field1") == 0);
41+
assert(__CPROVER_get_field(&buffer[7], "field1") == 0);
42+
assert(__CPROVER_get_field(&buffer[8], "field1") == 0);
43+
assert(__CPROVER_get_field(&buffer[9], "field1") == 0);
44+
}
45+
46+
int main()
47+
{
48+
__CPROVER_field_decl_local("field1", (_Bool)0);
49+
// Needed because the string constant is in the global pool.
50+
__CPROVER_field_decl_global("field1", (_Bool)0);
51+
52+
test("!Hello!");
53+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FUTURE
2+
main.c
3+
--unwind 11
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
^Generated 11 VCC\(s\), 0 remaining after simplification$
8+
--
9+
^warning: ignoring
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
__CPROVER_field_decl_local("field1", (_Bool)0);
6+
// Needed because the string constant is in the global pool.
7+
__CPROVER_field_decl_global("field1", (_Bool)0);
8+
9+
const char *str = "!Hello!";
10+
11+
// Set shadow memory for some characters
12+
for(int i = 1; i < 6; ++i)
13+
{
14+
__CPROVER_set_field(&str[i], "field1", 1);
15+
}
16+
17+
// Populate with pointers into string constant
18+
char *pointers[10];
19+
for(int i = 0; i < 7; ++i)
20+
{
21+
pointers[i] = &str[i];
22+
}
23+
24+
// Check that we can read the string constant's shadow memory
25+
// through the pointers.
26+
assert(__CPROVER_get_field(pointers[0], "field1") == 0);
27+
assert(__CPROVER_get_field(pointers[1], "field1") == 1);
28+
assert(__CPROVER_get_field(pointers[2], "field1") == 1);
29+
assert(__CPROVER_get_field(pointers[3], "field1") == 1);
30+
assert(__CPROVER_get_field(pointers[4], "field1") == 1);
31+
assert(__CPROVER_get_field(pointers[5], "field1") == 1);
32+
assert(__CPROVER_get_field(pointers[6], "field1") == 0);
33+
assert(__CPROVER_get_field(pointers[7], "field1") == 0);
34+
assert(__CPROVER_get_field(pointers[8], "field1") == 0);
35+
assert(__CPROVER_get_field(pointers[9], "field1") == 0);
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FUTURE
2+
main.c
3+
--unwind 11
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

0 commit comments

Comments
 (0)