Skip to content

Commit a374400

Browse files
zhixing-xutautschnig
authored andcommitted
Add the method to cbmc
1 parent 983d667 commit a374400

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#define ROW 10
2+
#define COL 10
3+
4+
void main()
5+
{
6+
int sum;
7+
int image[ROW][COL];
8+
9+
// shrinkable
10+
for(int j = 0; j < COL; j++)
11+
image[0][j] = 0;
12+
13+
sum = 0;
14+
// outer loop unshrinkable
15+
// inner loop shrinkable
16+
for(int i = 0; i < ROW; i++)
17+
{
18+
image[i][0] = 0;
19+
sum = sum + i;
20+
for(int j = 0; j < COL; j++)
21+
image[sum][j] = 0;
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
main.c
3+
--bounds-check --pointer-check --abstract-loops --show-goto-functions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
ASSUME j < 10
7+
ASSUME j >= 0
8+
--
9+
ASSUME i < 10
10+
ASSUME i >= 0
11+
^warning: ignoring
12+
--
13+
This test ensures that the loop with loop variable i is not shrinked (so it
14+
appears in unwinding). And loops with loop variable j are shrinked. It also
15+
checks the constraints to the loop variable is correctly applied.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define ROW 3
2+
#define COL 10
3+
4+
void boo(int r)
5+
{
6+
int val = 0;
7+
int image[ROW][COL];;
8+
// shrinkable, and set as shrink target
9+
for(int j = 0; j < COL; j++)
10+
{
11+
int c = j + val;
12+
image[r][c] = val;
13+
}
14+
}
15+
16+
17+
void main()
18+
{
19+
int r;
20+
int buffer[ROW];
21+
// not shrinkable since r is used in mem-safe assertion in boo()
22+
// and r update itself in loop
23+
for(int i = 0; i < ROW; i++)
24+
{
25+
r = r + i;
26+
boo(r);
27+
buffer[i];
28+
}
29+
30+
// shrinkable
31+
for(int i = 0; i < ROW; i++)
32+
{
33+
boo(i);
34+
buffer[i];
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CORE
2+
main.c
3+
--bounds-check --pointer-check --abstract-loops --abstractset boo.0,main.0 --show-goto-functions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
ASSUME j < 10
7+
ASSUME j >= 0
8+
--
9+
ASSUME i < 10
10+
ASSUME i >= 0
11+
^warning: ignoring
12+
--
13+
This test ensures that the loops with loop variable i are not shrinked (so
14+
they appears in unwinding). Although the second loop in main is shrinkable, but
15+
it's not in the target loop set provided by --abstractset, so it shouldn't be
16+
shrinked.
17+
It also ensures the loop in boo() with loop variable j is shrinked and the
18+
constraints are correctly applied.

src/cbmc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ OBJ += ../ansi-c/ansi-c$(LIBEXT) \
4040
../goto-instrument/reachability_slicer$(OBJEXT) \
4141
../goto-instrument/nondet_static$(OBJEXT) \
4242
../goto-instrument/full_slicer$(OBJEXT) \
43+
../goto-instrument/abstract_loops$(OBJEXT) \
4344
../goto-instrument/unwindset$(OBJEXT) \
4445
../analyses/analyses$(LIBEXT) \
4546
../langapi/langapi$(LIBEXT) \

src/cbmc/cbmc_parse_options.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Author: Daniel Kroening, [email protected]
6969

7070
#include <goto-instrument/reachability_slicer.h>
7171
#include <goto-instrument/full_slicer.h>
72+
#include <goto-instrument/abstract_loops.h>
7273
#include <goto-instrument/nondet_static.h>
7374
#include <goto-instrument/cover.h>
7475

@@ -159,6 +160,12 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options)
159160
if(cmdline.isset("full-slice"))
160161
options.set_option("full-slice", true);
161162

163+
if(cmdline.isset("abstract-loops"))
164+
options.set_option("abstract-loops", true);
165+
166+
if(cmdline.isset("abstractset"))
167+
options.set_option("abstractset", cmdline.get_value("abstractset"));
168+
162169
if(cmdline.isset("show-symex-strategies"))
163170
{
164171
status() << show_path_strategies() << eom;
@@ -892,6 +899,18 @@ bool cbmc_parse_optionst::process_goto_program(
892899
reachability_slicer(goto_model);
893900
}
894901

902+
if(options.get_bool_option("abstract-loops"))
903+
{
904+
// prevent other flag from messing up with location number
905+
goto_model.goto_functions.update();
906+
log.status() << "Abstracting loops" << eom;
907+
loop_mapt target_loop_map;
908+
if(options.is_set("abstractset"))
909+
if(parse_absloopset(options.get_option("abstractset"), target_loop_map))
910+
throw "Cannot understand input loop";
911+
abstract_loops(goto_model, target_loop_map);
912+
}
913+
895914
// full slice?
896915
if(options.get_bool_option("full-slice"))
897916
{
@@ -990,6 +1009,8 @@ void cbmc_parse_optionst::help()
9901009
HELP_REACHABILITY_SLICER_FB
9911010
" --full-slice run full slicer (experimental)\n" // NOLINT(*)
9921011
" --drop-unused-functions drop functions trivially unreachable from main function\n" // NOLINT(*)
1012+
" --abstract-loops shrink loop into single iter if the assertions are based on single iter (experimental)\n"
1013+
" --abstractset L,... only shrink loop L, shrink every loop if the flag is not given\n"
9931014
"\n"
9941015
"Semantic transformations:\n"
9951016
// NOLINTNEXTLINE(whitespace/line_length)

src/cbmc/cbmc_parse_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class optionst;
4242
OPT_BMC \
4343
"(preprocess)(slice-by-trace):" \
4444
OPT_FUNCTIONS \
45-
"(no-simplify)(full-slice)" \
45+
"(no-simplify)(full-slice)(abstract-loops)(abstractset):" \
4646
OPT_REACHABILITY_SLICER \
4747
"(debug-level):(no-propagation)(no-simplify-if)" \
4848
"(document-subgoals)(outfile):(test-preprocessor)" \

0 commit comments

Comments
 (0)