Skip to content

Commit 97f1619

Browse files
authored
Merge pull request #4578 from xbauch/feature/dynamic-memory-snapshot
Dynamic memory snapshot
2 parents 058e775 + ba8beb5 commit 97f1619

File tree

19 files changed

+661
-146
lines changed

19 files changed

+661
-146
lines changed

regression/memory-analyzer/pointer_03/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ CORE
22
main.gb
33
--breakpoint checkpoint --symbols x,p
44
x = 3;
5-
p = &x;
5+
p = \(void \*\)\&x;
66
^EXIT=0$
77
^SIGNAL=0$
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
CORE
22
main.gb
33
--breakpoint checkpoint --symbols p
4-
struct S tmp;
5-
tmp = \{ \.next=\(\(struct S \*\)0\) \};
6-
p = \&tmp;
4+
st = \{ .next=\&st \};
5+
p = \&st;
76
^EXIT=0$
87
^SIGNAL=0$
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
CORE
22
main.gb
33
--breakpoint checkpoint --symbols st
4-
signed int tmp;
5-
tmp = 3;
6-
st = \{ .c1=1, .c2=&tmp \};
4+
i = 3;
5+
st = \{ .c1=1, .c2=\&i \};
76
^EXIT=0$
87
^SIGNAL=0$
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <assert.h>
2+
3+
int array[] = {1, 2, 3};
4+
int *p;
5+
int *q;
6+
7+
void initialize()
8+
{
9+
p = &(array[1]);
10+
q = array + 1;
11+
array[0] = 4;
12+
}
13+
14+
void checkpoint()
15+
{
16+
}
17+
18+
int main()
19+
{
20+
initialize();
21+
checkpoint();
22+
23+
assert(p == q);
24+
assert(*p == *q);
25+
*p = 4;
26+
q = q - 1;
27+
assert(*q == *p);
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
p,q --harness-type initialise-with-memory-snapshot --initial-goto-location main:4
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
\[main.assertion.1\] line [0-9]+ assertion p == q: SUCCESS
7+
\[main.assertion.2\] line [0-9]+ assertion \*p == \*q: SUCCESS
8+
\[main.assertion.3\] line [0-9]+ assertion \*q == \*p: SUCCESS
9+
VERIFICATION SUCCESSFUL
10+
--
11+
^warning: ignoring
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <assert.h>
2+
#include <malloc.h>
3+
4+
int *array;
5+
int *iterator1;
6+
int *iterator2;
7+
int *iterator3;
8+
9+
void initialize()
10+
{
11+
array = (int *)malloc(sizeof(int) * 10);
12+
array[0] = 1;
13+
array[1] = 2;
14+
array[2] = 3;
15+
array[3] = 4;
16+
array[4] = 5;
17+
array[5] = 6;
18+
array[6] = 7;
19+
array[7] = 8;
20+
array[8] = 9;
21+
array[9] = 10;
22+
iterator1 = (int *)array;
23+
iterator2 = &array[1];
24+
iterator3 = array + 1;
25+
}
26+
27+
void checkpoint()
28+
{
29+
}
30+
31+
int main()
32+
{
33+
initialize();
34+
checkpoint();
35+
36+
assert(*iterator1 == 1);
37+
assert(iterator1 != iterator2);
38+
assert(iterator2 == iterator3);
39+
assert(iterator2 == &array[1]);
40+
assert(*iterator3 == array[1]);
41+
assert(*iterator3 == 2);
42+
iterator3 = &array[9];
43+
iterator3++;
44+
assert(*iterator3 == 0);
45+
46+
return 0;
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
main.c
3+
array,iterator1,iterator2,iterator3 --harness-type initialise-with-memory-snapshot --initial-goto-location main:4
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
\[main.assertion.1\] line [0-9]+ assertion \*iterator1 == 1: SUCCESS
7+
\[main.assertion.2\] line [0-9]+ assertion iterator1 != iterator2: SUCCESS
8+
\[main.assertion.3\] line [0-9]+ assertion iterator2 == iterator3: SUCCESS
9+
\[main.assertion.4\] line [0-9]+ assertion iterator2 == \&array\[1\]: SUCCESS
10+
\[main.assertion.5\] line [0-9]+ assertion \*iterator3 == array\[1\]: SUCCESS
11+
\[main.assertion.6\] line [0-9]+ assertion \*iterator3 == 2: SUCCESS
12+
\[main.pointer_dereference.27\] line [0-9]+ dereference failure: pointer outside object bounds in \*iterator3: FAILURE
13+
\[main.assertion.7\] line [0-9]+ assertion \*iterator3 == 0: FAILURE
14+
VERIFICATION FAILED
15+
--
16+
unwinding assertion loop \d+: FAILURE

regression/snapshot-harness/pointer-to-array-int/main.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ int main()
3131
checkpoint();
3232

3333
assert(first == second);
34-
// The following assertions will be check in the following PR once
35-
// dynamically allocated snapshots are properly implemented.
36-
/* assert(array_size >= prefix_size); */
37-
/* assert(prefix_size >= 0); */
38-
/* assert(second[prefix_size] != 6); */
39-
/* assert(second[4] == 4); */
34+
assert(array_size >= prefix_size);
35+
assert(prefix_size >= 0);
36+
assert(second[prefix_size] != 6);
37+
assert(second[4] == 4);
4038

41-
/* for(int i = 0; i < prefix_size; i++) */
42-
/* assert(second[i] != prefix[i]); */
4339
return 0;
4440
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <assert.h>
2+
3+
float array[10];
4+
float *iterator1;
5+
float *iterator2;
6+
7+
void initialize()
8+
{
9+
array[0] = 1.11;
10+
array[8] = 9.999;
11+
array[9] = 10.0;
12+
iterator1 = (float *)array;
13+
iterator2 = &array[9];
14+
}
15+
16+
void checkpoint()
17+
{
18+
}
19+
20+
int main()
21+
{
22+
initialize();
23+
checkpoint();
24+
25+
assert(*iterator1 >= 1.10 && *iterator1 <= 1.12);
26+
assert(iterator1 != iterator2);
27+
assert(iterator2 == &array[9]);
28+
iterator2++;
29+
assert(*iterator2 == 0.0);
30+
31+
return 0;
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FUTURE
2+
main.c
3+
array,iterator1,iterator2 --harness-type initialise-with-memory-snapshot --initial-goto-location main:4
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
\[main.assertion.1\] line [0-9]+ assertion \*iterator1 \>= 1.10 \&\& \*iterator1 \<= 1.12: SUCCESS
7+
\[main.assertion.2\] line [0-9]+ assertion iterator1 != iterator2: SUCCESS
8+
\[main.assertion.3\] line [0-9]+ assertion iterator2 == \&array\[9\]: SUCCESS
9+
\[main.pointer_dereference.13\] line [0-9]+ dereference failure: pointer outside object bounds in \*iterator2: FAILURE
10+
\[main.assertion.4\] line [0-9]+ assertion \*iterator2 == 0: FAILURE
11+
VERIFICATION FAILED
12+
--
13+
unwinding assertion loop \d+: FAILURE
14+
--
15+
memory analyzer does not yet allow extract floating point values
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <assert.h>
2+
3+
int array[10];
4+
int *iterator1;
5+
int *iterator2;
6+
7+
void initialize()
8+
{
9+
array[0] = 1;
10+
array[8] = 9;
11+
array[9] = 10;
12+
iterator1 = (int *)array;
13+
iterator2 = &array[9];
14+
}
15+
16+
void checkpoint()
17+
{
18+
}
19+
20+
int main()
21+
{
22+
initialize();
23+
checkpoint();
24+
25+
assert(*iterator1 == 1);
26+
assert(iterator1 != iterator2);
27+
assert(iterator2 == &array[9]);
28+
iterator2++;
29+
assert(*iterator2 == 0);
30+
31+
return 0;
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
array,iterator1,iterator2 --harness-type initialise-with-memory-snapshot --initial-goto-location main:4
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
\[main.assertion.1\] line [0-9]+ assertion \*iterator1 == 1: SUCCESS
7+
\[main.assertion.2\] line [0-9]+ assertion iterator1 != iterator2: SUCCESS
8+
\[main.assertion.3\] line [0-9]+ assertion iterator2 == \&array\[9\]: SUCCESS
9+
\[main.pointer_dereference.13\] line [0-9]+ dereference failure: pointer outside object bounds in \*iterator2: FAILURE
10+
\[main.assertion.4\] line [0-9]+ assertion \*iterator2 == 0: FAILURE
11+
VERIFICATION FAILED
12+
--
13+
unwinding assertion loop \d+: FAILURE

src/goto-cc/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ INCLUDES= -I ..
4040

4141
LIBS =
4242

43-
CLEANFILES = goto-cc$(EXEEXT) goto-cl$(EXEEXT)
43+
CLEANFILES = goto-cc$(EXEEXT) goto-gcc$(EXEEXT) goto-cl$(EXEEXT)
4444

4545
include ../config.inc
4646
include ../common
4747

4848
ifeq ($(BUILD_ENV_),MSVC)
4949
all: goto-cl$(EXEEXT)
50+
else
51+
all: goto-gcc$(EXEEXT)
5052
endif
51-
all: goto-cc$(EXEEXT)
5253

5354
ifneq ($(wildcard ../jsil/Makefile),)
5455
OBJ += ../jsil/jsil$(LIBEXT)
@@ -57,6 +58,9 @@ endif
5758

5859
###############################################################################
5960

61+
goto-gcc$(EXEEXT): goto-cc$(EXEEXT)
62+
ln -sf goto-cc$(EXEEXT) goto-gcc$(EXEEXT)
63+
6064
goto-cc$(EXEEXT): $(OBJ)
6165
$(LINKBIN)
6266

src/goto-harness/memory_snapshot_harness_generator.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ size_t memory_snapshot_harness_generatort::pointer_depth(const typet &t) const
207207
return pointer_depth(t.subtype()) + 1;
208208
}
209209

210+
bool memory_snapshot_harness_generatort::refers_to(
211+
const exprt &expr,
212+
const irep_idt &name) const
213+
{
214+
if(expr.id() == ID_symbol)
215+
return to_symbol_expr(expr).get_identifier() == name;
216+
return std::any_of(
217+
expr.operands().begin(),
218+
expr.operands().end(),
219+
[this, name](const exprt &subexpr) { return refers_to(subexpr, name); });
220+
}
221+
210222
code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
211223
const symbol_tablet &snapshot,
212224
goto_modelt &goto_model) const
@@ -230,8 +242,11 @@ code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
230242
ordered_snapshot_symbols.begin(),
231243
ordered_snapshot_symbols.end(),
232244
[this](const snapshot_pairt &left, const snapshot_pairt &right) {
233-
return pointer_depth(left.second.symbol_expr().type()) <
234-
pointer_depth(right.second.symbol_expr().type());
245+
if(refers_to(right.second.value, left.first))
246+
return true;
247+
else
248+
return pointer_depth(left.second.symbol_expr().type()) <
249+
pointer_depth(right.second.symbol_expr().type());
235250
});
236251

237252
code_blockt code;

src/goto-harness/memory_snapshot_harness_generator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ class memory_snapshot_harness_generatort : public goto_harness_generatort
244244
/// \return pointer depth of type \p t
245245
size_t pointer_depth(const typet &t) const;
246246

247+
/// Recursively test pointer reference
248+
/// \param expr: expression to be tested
249+
/// \param name: name to be located
250+
/// \return true if \p expr refers to an object named \p name
251+
bool refers_to(const exprt &expr, const irep_idt &name) const;
252+
247253
/// data to store the command-line options
248254
std::string memory_snapshot_file;
249255
std::string initial_goto_location_line;

0 commit comments

Comments
 (0)