Skip to content

Commit afdcc7d

Browse files
committed
CEGIS refactor preprocessing & CE extraction.
1 parent 0b96343 commit afdcc7d

31 files changed

+713
-142
lines changed

src/cegis/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ SRC = danger/meta/meta_variable_names.cpp danger/facade/danger_runner.cpp danger
5959
refactor/instructionset/instruction_description.cpp refactor/instructionset/cegis_instruction_factory.cpp \
6060
refactor/instructionset/processor_symbols.cpp \
6161
refactor/constraint/constraint_factory.cpp \
62-
refactor/preprocessing/refactor_preprocessing.cpp \
62+
refactor/preprocessing/refactor_preprocessing.cpp refactor/preprocessing/collect_counterexamples.cpp \
6363
refactor/learn/refactor_symex_learn.cpp \
6464
refactor/verify/refactor_symex_verify.cpp \
6565
refactor/facade/refactor_runner.cpp \
66-
refactor/options/refactoring_type.cpp refactor/options/refactor_program.cpp refactor/value/refactor_solution.cpp \
66+
refactor/options/refactoring_type.cpp refactor/options/refactor_program.cpp \
6767
refactor/nullobject/range_collector.cpp refactor/nullobject/nullable_analysis.cpp \
6868
wordsize/restrict_bv_size.cpp value/program_individual_serialisation.cpp value/assignments_printer.cpp \
6969
seed/literals_seed.cpp \

src/cegis/cegis-util/cbmc_runner.cpp

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <algorithm>
2+
13
#include <util/config.h>
24
#include <util/substitute.h>
35

@@ -11,6 +13,12 @@
1113
#include <cegis/cegis-util/temporary_output_block.h>
1214
#include <cegis/cegis-util/cbmc_runner.h>
1315

16+
// XXX: Debug
17+
#include <iostream>
18+
// XXX: Debug
19+
20+
#define ARGV_MAX_SIZE 5u
21+
1422
namespace
1523
{
1624
bool exists(const std::string &name)
@@ -33,14 +41,48 @@ std::string get_next_goto_file_name()
3341
return get_goto_file_name(index);
3442
}
3543

36-
const char * ARGV[]= { "cbmc", "--stop-on-fail" };
37-
const char * GCC_ARGV[]= { "cbmc", "--stop-on-fail", "-gcc" };
44+
bool is_gcc()
45+
{
46+
return configt::ansi_ct::flavourt::GCC == config.ansi_c.mode;
47+
}
48+
49+
std::vector<std::string> get_args()
50+
{
51+
std::vector<std::string> result= { "cbmc", "--stop-on-fail" };
52+
if (is_gcc()) result.push_back("--gcc");
53+
return result;
54+
}
55+
56+
std::vector<const char *> get_argv(const std::vector<std::string> &args)
57+
{
58+
std::vector<const char *> result;
59+
std::transform(args.begin(), args.end(), std::back_inserter(result),
60+
std::mem_fun_ref(std::string::c_str));
61+
return result;
62+
}
63+
64+
class args_providert
65+
{
66+
const std::vector<std::string> args;
67+
std::vector<const char *> arg_ref;
68+
public:
69+
args_providert() :
70+
args(get_args()), arg_ref(get_argv(args))
71+
{
72+
}
3873

39-
bool is_gcc() { return configt::ansi_ct::flavourt::GCC == config.ansi_c.mode; }
40-
int get_argc() { return is_gcc() ? 3 : 2; }
41-
const char ** get_argv() { return is_gcc() ? GCC_ARGV : ARGV; }
74+
int argc()
75+
{
76+
return arg_ref.size();
77+
}
78+
79+
const char * *argv()
80+
{
81+
return arg_ref.data();
82+
}
83+
};
4284

43-
class cbmc_runnert: public cbmc_parse_optionst
85+
class cbmc_runnert: public args_providert, public cbmc_parse_optionst
4486
{
4587
const symbol_tablet &st;
4688
const goto_functionst &gf;
@@ -49,30 +91,24 @@ class cbmc_runnert: public cbmc_parse_optionst
4991
const bool keep_goto_programs;
5092

5193
public:
52-
cbmc_runnert(
53-
const symbol_tablet &st,
54-
const goto_functionst &gf,
55-
cbmc_resultt &result,
56-
const bool keep_goto_programs) :
57-
cbmc_parse_optionst(get_argc(), get_argv()), st(st), gf(gf), result(
58-
result), bmc_result(safety_checkert::UNSAFE), keep_goto_programs(
59-
keep_goto_programs)
94+
cbmc_runnert(const symbol_tablet &st, const goto_functionst &gf,
95+
cbmc_resultt &result, const bool keep_goto_programs) :
96+
cbmc_parse_optionst(argc(), argv()), st(st), gf(gf), result(result), bmc_result(
97+
safety_checkert::UNSAFE), keep_goto_programs(keep_goto_programs)
6098
{
6199
}
62100

63101
virtual ~cbmc_runnert()=default;
64102

65-
virtual int get_goto_program(
66-
const optionst &options,
67-
bmct &bmc,
68-
goto_functionst &goto_functions)
103+
virtual int get_goto_program(const optionst &options, bmct &bmc,
104+
goto_functionst &goto_functions)
69105
{
70106
symbol_table.clear();
71107
symbol_table=st;
72108
goto_functions.clear();
73109
goto_functions.copy_from(gf);
74-
if(process_goto_program(options, goto_functions)) return 6;
75-
if(keep_goto_programs)
110+
if (process_goto_program(options, goto_functions)) return 6;
111+
if (keep_goto_programs)
76112
{
77113
const std::string path(get_next_goto_file_name());
78114
message_handlert &msg=get_message_handler();
@@ -101,11 +137,9 @@ class cbmc_runnert: public cbmc_parse_optionst
101137
};
102138
}
103139

104-
safety_checkert::resultt run_cbmc(
105-
const symbol_tablet &st,
106-
const goto_functionst &gf,
107-
cbmc_resultt &cbmc_result,
108-
const bool keep_goto_programs)
140+
safety_checkert::resultt run_cbmc(const symbol_tablet &st,
141+
const goto_functionst &gf, cbmc_resultt &cbmc_result,
142+
const bool keep_goto_programs)
109143
{
110144
const temporary_output_blockt disable_output;
111145
cbmc_runnert runner(st, gf, cbmc_result, keep_goto_programs);
@@ -116,11 +150,9 @@ safety_checkert::resultt run_cbmc(
116150
return runner.get_bmc_result();
117151
}
118152

119-
safety_checkert::resultt run_cbmc(
120-
const symbol_tablet &st,
121-
const goto_functionst &gf,
122-
cbmc_resultt &cbmc_result,
123-
const optionst &o)
153+
safety_checkert::resultt run_cbmc(const symbol_tablet &st,
154+
const goto_functionst &gf, cbmc_resultt &cbmc_result, const optionst &o)
124155
{
125-
return run_cbmc(st, gf, cbmc_result, o.get_bool_option(CEGIS_KEEP_GOTO_PROGRAMS));
156+
return run_cbmc(st, gf, cbmc_result,
157+
o.get_bool_option(CEGIS_KEEP_GOTO_PROGRAMS));
126158
}

src/cegis/cegis-util/cbmc_runner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ safety_checkert::resultt run_cbmc(
6161
const class symbol_tablet &st,
6262
const class goto_functionst &gf,
6363
cbmc_resultt &result,
64-
const optionst &options);
64+
const class optionst &options);
6565

6666
#endif /* CEGIS_UTIL_CBMC_RUNNER_H_ */

src/cegis/cegis-util/counterexample_vars.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@
88
#include <cegis/cegis-util/program_helper.h>
99
#include <cegis/cegis-util/counterexample_vars.h>
1010

11-
void collect_counterexample_locations(goto_programt::targetst &locs,
11+
namespace
12+
{
13+
size_t collect_counterexample_locations(goto_programt::targetst &locs,
1214
const char * const marker_prefix, goto_programt &prog,
13-
const std::function<bool(goto_programt::const_targett target)> is_meta)
15+
const std::function<bool(goto_programt::const_targett target)> is_meta,
16+
size_t marker_index)
1417
{
1518
goto_programt::instructionst &body=prog.instructions;
1619
const goto_programt::targett end(body.end());
17-
size_t marker_index=0;
1820
for (goto_programt::targett instr=body.begin(); instr != end; ++instr)
1921
if (is_nondet(instr, end) && !is_meta(instr))
2022
{
2123
assert(instr->labels.empty());
2224
instr->labels.push_back(marker_prefix + std::to_string(marker_index++));
2325
locs.push_back(instr);
2426
}
27+
return marker_index;
28+
}
29+
}
30+
31+
void collect_counterexample_locations(goto_programt::targetst &locs,
32+
const char * const marker_prefix, goto_programt &prog,
33+
const std::function<bool(goto_programt::const_targett target)> is_meta)
34+
{
35+
collect_counterexample_locations(locs, marker_prefix, prog, is_meta, 0);
2536
}
2637

2738
#define TMP_IF_EXPR_PREFIX "tmp_if_expr$"
@@ -58,6 +69,15 @@ void collect_counterexample_locations(goto_programt::targetst &locs,
5869
is_meta);
5970
}
6071

72+
size_t collect_counterexample_locations(goto_programt::targetst &locs,
73+
goto_programt &prog,
74+
const std::function<bool(goto_programt::const_targett target)> is_meta,
75+
const size_t marker_index_offset)
76+
{
77+
return collect_counterexample_locations(locs, DEFAULT_MARKER_LABEL_PREFIX,
78+
prog, is_meta, marker_index_offset);
79+
}
80+
6181
namespace
6282
{
6383
goto_programt::instructiont::labelst::const_iterator find_ce_label(

src/cegis/cegis-util/counterexample_vars.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ void collect_counterexample_locations(
7575
goto_programt &prog,
7676
const std::function<bool(goto_programt::const_targett target)> is_meta);
7777

78+
/**
79+
* @brief
80+
*
81+
* @details
82+
*
83+
* @param locs
84+
* @param prog
85+
* @param is_meta
86+
* @param marker_index_offset
87+
*
88+
* @return
89+
*/
90+
size_t collect_counterexample_locations(
91+
goto_programt::targetst &locs,
92+
goto_programt &prog,
93+
const std::function<bool(goto_programt::const_targett target)> is_meta,
94+
size_t marker_index_offset);
95+
7896
/**
7997
* @brief
8098
*

src/cegis/cegis-util/program_helper.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <algorithm>
2+
#include <functional>
23

34
#include <util/type_eq.h>
45
#include <goto-programs/goto_functions.h>
@@ -315,3 +316,32 @@ goto_programt::targett add_return_assignment(goto_programt &body,
315316
pos->code=code_assignt(get_ret_val_var(func_id, value.type()), value);
316317
return pos;
317318
}
319+
320+
namespace
321+
{
322+
goto_programt::targett insert_preserving_source_location(
323+
goto_programt::targett pos,
324+
const std::function<goto_programt::targett(goto_programt::targett)> &inserter)
325+
{
326+
const source_locationt &loc=pos->source_location;
327+
const irep_idt &func_name=pos->function;
328+
pos=inserter(pos);
329+
pos->source_location=loc;
330+
pos->function=func_name;
331+
return pos;
332+
}
333+
}
334+
335+
goto_programt::targett insert_after_preserving_source_location(
336+
goto_programt &body, goto_programt::targett pos)
337+
{
338+
const auto op=std::bind1st(std::mem_fun(goto_programt::insert_after), &body);
339+
return insert_preserving_source_location(pos, op);
340+
}
341+
342+
goto_programt::targett insert_before_preserving_source_location(
343+
goto_programt &body, goto_programt::targett pos)
344+
{
345+
const auto op=std::bind1st(std::mem_fun(goto_programt::insert_before), &body);
346+
return insert_preserving_source_location(pos, op);
347+
}

src/cegis/cegis-util/program_helper.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,32 @@ goto_programt::targett add_return_assignment(
346346
const irep_idt &func_id,
347347
const exprt &value);
348348

349+
/**
350+
* @brief
351+
*
352+
* @details
353+
*
354+
* @param body
355+
* @param pos
356+
*
357+
* @return
358+
*/
359+
goto_programt::targett insert_after_preserving_source_location(
360+
goto_programt &body,
361+
goto_programt::targett pos);
362+
363+
/**
364+
* @brief
365+
*
366+
* @details
367+
*
368+
* @param body
369+
* @param pos
370+
*
371+
* @return
372+
*/
373+
goto_programt::targett insert_before_preserving_source_location(
374+
goto_programt &body,
375+
goto_programt::targett pos);
376+
349377
#endif /* CEGIS_PROGRAM_HELPER_H_ */

src/cegis/cegis-util/temporary_output_block.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
temporary_output_blockt::temporary_output_blockt()
66
{
7-
std::cout.setstate(std::ios_base::failbit);
7+
// XXX: Debug
8+
//std::cout.setstate(std::ios_base::failbit);
89
}
910

1011
temporary_output_blockt::~temporary_output_blockt()

src/cegis/instrument/literals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
#define CEGIS_CONSTANT_PREFIX "CEGIS_CONSTANT_"
2424
#define CEGIS_FITNESS_TEST_FUNC "__CPROVER_cegis_test_fitness"
2525
#define CPROVER_INIT CPROVER_PREFIX "initialize"
26+
#define CONSTRAINT_CALLER_NAME CEGIS_PREFIX "constraint_caller"
2627

2728
#endif /* CEGIS_LITERALS_H_ */

src/cegis/learn/insert_counterexample.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ plus_exprt increment(const symbol_exprt &symbol)
189189
return plus_exprt(symbol, one);
190190
}
191191

192-
void add_ce_goto(symbol_tablet &st, goto_functionst &gf,
193-
const std::set<irep_idt> &ce_keys, const size_t num_ces,
192+
void add_ce_goto(symbol_tablet &st, goto_functionst &gf, const size_t num_ces,
194193
const goto_programt::targett &begin)
195194
{
196195
goto_programt &body=get_entry_body(gf);
@@ -267,6 +266,6 @@ void insert_counterexamples(symbol_tablet &st, goto_functionst &gf,
267266
const goto_programt::targett begin(find_cprover_initialize(body));
268267
add_array_declarations(st, gf, ces, begin);
269268
add_array_indexes(ce_keys, st, gf);
270-
add_ce_goto(st, gf, ce_keys, ces.size(), begin);
269+
add_ce_goto(st, gf, ces.size(), begin);
271270
assign_ce_values(st, gf, ce_locs);
272271
}

0 commit comments

Comments
 (0)