Skip to content

Commit a584af3

Browse files
committed
First working CEGIS null object refactoring.
1 parent 32aaead commit a584af3

23 files changed

+514
-129
lines changed

src/cegis/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SRC = danger/meta/meta_variable_names.cpp danger/facade/danger_runner.cpp danger
6060
refactor/instructionset/processor_symbols.cpp \
6161
refactor/constraint/constraint_factory.cpp \
6262
refactor/preprocessing/refactor_preprocessing.cpp refactor/preprocessing/collect_counterexamples.cpp \
63-
refactor/learn/refactor_symex_learn.cpp \
63+
refactor/learn/refactor_symex_learn.cpp refactor/learn/instrument_counterexamples.cpp \
6464
refactor/verify/refactor_symex_verify.cpp \
6565
refactor/facade/refactor_runner.cpp \
6666
refactor/options/refactoring_type.cpp refactor/options/refactor_program.cpp \

src/cegis/cegis-util/cbmc_runner.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
#include <cegis/cegis-util/temporary_output_block.h>
1515
#include <cegis/cegis-util/cbmc_runner.h>
1616

17-
// XXX: Debug
18-
#include <iostream>
19-
// XXX: Debug
20-
2117
#define ARGV_MAX_SIZE 5u
2218

2319
namespace

src/cegis/cegis-util/program_helper.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,23 @@ symbolt &create_cegis_symbol(symbol_tablet &st, const std::string &full_name,
235235
return create_local_cegis_symbol(st, full_name, full_name, type);
236236
}
237237

238-
goto_programt::targett cegis_assign(const symbol_tablet &st,
239-
goto_programt &body, const goto_programt::targett &insert_after_pos,
238+
void cegis_assign(const symbol_tablet &st, goto_programt::instructiont &instr,
240239
const exprt &lhs, const exprt &rhs, const source_locationt &loc)
241240
{
242-
goto_programt::targett assign=body.insert_after(insert_after_pos);
243-
assign->type=goto_program_instruction_typet::ASSIGN;
244-
assign->source_location=loc;
241+
instr.type=goto_program_instruction_typet::ASSIGN;
242+
instr.source_location=loc;
245243
const namespacet ns(st);
246244
const typet &type=lhs.type();
247-
if (type_eq(type, rhs.type(), ns)) assign->code=code_assignt(lhs, rhs);
248-
else assign->code=code_assignt(lhs, typecast_exprt(rhs, type));
245+
if (type_eq(type, rhs.type(), ns)) instr.code=code_assignt(lhs, rhs);
246+
else instr.code=code_assignt(lhs, typecast_exprt(rhs, type));
247+
}
248+
249+
goto_programt::targett cegis_assign(const symbol_tablet &st,
250+
goto_programt &body, const goto_programt::targett &insert_after_pos,
251+
const exprt &lhs, const exprt &rhs, const source_locationt &loc)
252+
{
253+
const goto_programt::targett assign=body.insert_after(insert_after_pos);
254+
cegis_assign(st, *assign, lhs, rhs, loc);
249255
return assign;
250256
}
251257

src/cegis/cegis-util/program_helper.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ goto_programt::targett cegis_assign(const symbol_tablet &st,
244244
goto_functionst &gf, const goto_programt::targett &insert_after_pos,
245245
const exprt &lhs, const exprt &rhs);
246246

247+
/**
248+
* @brief
249+
*
250+
* @details
251+
*
252+
* @param st
253+
* @param instr
254+
* @param lhs
255+
* @param rhs
256+
* @param loc
257+
*/
258+
void cegis_assign(const symbol_tablet &st, goto_programt::instructiont &instr,
259+
const exprt &lhs, const exprt &rhs, const source_locationt &loc);
260+
247261
/**
248262
* @brief
249263
*

src/cegis/cegis-util/temporary_output_block.cpp

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

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

1110
temporary_output_blockt::~temporary_output_blockt()

src/cegis/instrument/literals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
#define CEGIS_FITNESS_TEST_FUNC "__CPROVER_cegis_test_fitness"
2525
#define CPROVER_INIT CPROVER_PREFIX "initialize"
2626
#define CONSTRAINT_CALLER_NAME CEGIS_PREFIX "constraint_caller"
27+
#define CONSTRAINT_CALLER CONSTRAINT_CALLER_NAME ":()V"
28+
#define CONSTRAINT_CALLER_ID "java::" CONSTRAINT_CALLER
2729

2830
#endif /* CEGIS_LITERALS_H_ */

src/cegis/instrument/meta_variables.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ std::string get_local_meta_name(const std::string &func, const std::string &var)
3232

3333
namespace
3434
{
35+
void declare_local_var(goto_programt::instructiont &instr,
36+
const symbolt &symbol)
37+
{
38+
instr.type=goto_program_instruction_typet::DECL;
39+
instr.code=code_declt(symbol.symbol_expr());
40+
instr.source_location=default_cegis_source_location();
41+
}
42+
3543
goto_programt::targett declare_local_var(goto_programt &body,
3644
goto_programt::targett pos, const symbolt &symbol)
3745
{
3846
pos=body.insert_after(pos);
39-
pos->type=goto_program_instruction_typet::DECL;
40-
pos->code=code_declt(symbol.symbol_expr());
41-
pos->source_location=default_cegis_source_location();
47+
declare_local_var(*pos, symbol);
4248
return pos;
4349
}
4450
}
@@ -53,13 +59,19 @@ goto_programt::targett declare_cegis_meta_variable(symbol_tablet &st,
5359
}
5460

5561
goto_programt::targett declare_local_meta_variable(symbol_tablet &st,
56-
const std::string &func_name, goto_programt &body,
57-
const goto_programt::targett &insert_after_pos,
58-
const std::string &base_name, const typet &type)
62+
const std::string &fn, goto_programt &body,
63+
const goto_programt::targett &insert_after_pos, const std::string &bn,
64+
const typet &t)
5965
{
60-
const std::string symbol_name(get_local_meta_name(func_name, base_name));
61-
const symbolt &symbol=create_cegis_symbol(st, symbol_name, type);
62-
return declare_local_var(body, insert_after_pos, symbol);
66+
const symbolt &smb=create_cegis_symbol(st, get_local_meta_name(fn, bn), t);
67+
return declare_local_var(body, insert_after_pos, smb);
68+
}
69+
70+
void declare_local_meta_variable(symbol_tablet &st, const std::string &fn,
71+
goto_programt::instructiont &instr, const std::string &bn, const typet &t)
72+
{
73+
const symbolt &smb=create_cegis_symbol(st, get_local_meta_name(fn, bn), t);
74+
declare_local_var(instr, smb);
6375
}
6476

6577
goto_programt::targett assign_cegis_meta_variable(const symbol_tablet &st,

src/cegis/instrument/meta_variables.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ goto_programt::targett declare_local_meta_variable(symbol_tablet &st,
6969
const goto_programt::targett &insert_after_pos,
7070
const std::string &base_name, const typet &type);
7171

72+
/**
73+
* @brief
74+
*
75+
* @details
76+
*
77+
* @param st
78+
* @param func_name
79+
* @param instr
80+
* @param base_name
81+
* @param type
82+
*/
83+
void declare_local_meta_variable(symbol_tablet &st,
84+
const std::string &func_name, goto_programt::instructiont &instr,
85+
const std::string &base_name, const typet &type);
86+
7287
/**
7388
* @brief
7489
*

src/cegis/learn/constraint_helper.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
#include <goto-programs/goto_functions.h>
2+
13
#include <cegis/cegis-util/program_helper.h>
24

35
void transform_asserts_to_assumes(goto_functionst &gf)
46
{
5-
goto_programt &body=get_entry_body(gf);
6-
for (goto_programt::instructiont &instr : body.instructions)
7-
if (goto_program_instruction_typet::ASSERT == instr.type)
8-
instr.type=goto_program_instruction_typet::ASSUME;
7+
typedef goto_functionst::function_mapt fmapt;
8+
fmapt &fmap=gf.function_map;
9+
for (fmapt::value_type &entry : fmap)
10+
{
11+
if (!entry.second.body_available()) continue;
12+
for (goto_programt::instructiont &instr : entry.second.body.instructions)
13+
if (ASSERT == instr.type) instr.type=ASSUME;
14+
}
915
}

src/cegis/learn/insert_counterexample.cpp

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@
1414
#include <cegis/learn/insert_counterexample.h>
1515

1616
#define CE_ARRAY_PREFIX CPROVER_PREFIX "ce_array_"
17-
#define CE_ARRAY_INDEX CPROVER_PREFIX "ce_array_index"
1817
#define CE_VALUES_INDEX_PREFIX CPROVER_PREFIX "ce_values_index_"
1918

20-
namespace
21-
{
22-
typedef std::map<const irep_idt, exprt> zero_valuest;
23-
24-
zero_valuest get_zero_values(const symbol_tablet &st, const goto_functionst &gf,
19+
zero_valuest get_zero_values(const symbol_tablet &st,
2520
const goto_programt::targetst &ce_locs)
2621
{
2722
std::map<const irep_idt, exprt> zero_values;
28-
const goto_programt::instructionst &body=get_entry_body(gf).instructions;
2923
const source_locationt loc(default_cegis_source_location());
3024
const namespacet ns(st);
3125
null_message_handlert msg;
@@ -39,16 +33,7 @@ zero_valuest get_zero_values(const symbol_tablet &st, const goto_functionst &gf,
3933
return zero_values;
4034
}
4135

42-
std::set<irep_idt> get_all_keys(const zero_valuest &zv)
43-
{
44-
std::set<irep_idt> result;
45-
std::transform(zv.begin(), zv.end(), std::inserter(result, result.end()),
46-
[](const zero_valuest::value_type &v)
47-
{ return v.first;});
48-
return result;
49-
}
50-
51-
void normalise(const std::set<irep_idt> ce_keys, const zero_valuest &zv,
36+
void normalise(const std::set<irep_idt> &ce_keys, const zero_valuest &zv,
5237
labelled_counterexamplest &ces)
5338
{
5439
const exprt::operandst no_values;
@@ -89,7 +74,16 @@ void normalise(const std::set<irep_idt> ce_keys, const zero_valuest &zv,
8974
}
9075
}
9176

92-
typedef std::map<labelled_assignmentst::key_type, array_exprt> array_valuest;
77+
namespace
78+
{
79+
std::set<irep_idt> get_all_keys(const zero_valuest &zv)
80+
{
81+
std::set<irep_idt> result;
82+
std::transform(zv.begin(), zv.end(), std::inserter(result, result.end()),
83+
[](const zero_valuest::value_type &v)
84+
{ return v.first;});
85+
return result;
86+
}
9387

9488
array_exprt to_values(const exprt::operandst &ops)
9589
{
@@ -101,6 +95,7 @@ array_exprt to_values(const exprt::operandst &ops)
10195
copy(ops.begin(), ops.end(), std::back_inserter(result.operands()));
10296
return result;
10397
}
98+
}
10499

105100
array_valuest get_array_values(const labelled_counterexamplest &ces)
106101
{
@@ -123,12 +118,20 @@ array_valuest get_array_values(const labelled_counterexamplest &ces)
123118
return result;
124119
}
125120

126-
std::string get_array_name(const irep_idt &loc_id)
121+
std::string get_ce_array_name(const irep_idt &loc_id)
127122
{
128123
std::string base_name(CE_ARRAY_PREFIX);
129124
return base_name+=id2string(loc_id);
130125
}
131126

127+
std::string get_ce_value_index_name(const irep_idt &loc)
128+
{
129+
std::string label(CE_VALUES_INDEX_PREFIX);
130+
return label+=id2string(loc);
131+
}
132+
133+
namespace
134+
{
132135
void add_array_declarations(symbol_tablet &st, goto_functionst &gf,
133136
const labelled_counterexamplest &ces, const goto_programt::targett &begin)
134137
{
@@ -141,25 +144,16 @@ void add_array_declarations(symbol_tablet &st, goto_functionst &gf,
141144
for (const labelled_counterexamplest::value_type::value_type &value : prototype)
142145
{
143146
const labelled_assignmentst::value_type::first_type loc_id=value.first;
144-
const typet &org_type=value.second.front().type();
145147
const array_valuest::const_iterator array_val=array_values.find(loc_id);
146148
assert(array_values.end() != array_val);
147149
const array_exprt &array_expr=array_val->second;
148-
const typet &array_type=array_expr.type();
149-
const typet &element_type=array_type.subtype();
150-
const std::string base_name(get_array_name(loc_id));
151-
pos=declare_cegis_meta_variable(st, gf, pos, base_name, array_type);
150+
const std::string base_name(get_ce_array_name(loc_id));
151+
pos=declare_cegis_meta_variable(st, gf, pos, base_name, array_expr.type());
152152
assert(array_expr.operands().size() == ces.size());
153-
pos=assign_cegis_meta_variable(st, gf, pos, base_name, array_val->second);
153+
pos=assign_cegis_meta_variable(st, gf, pos, base_name, array_expr);
154154
}
155155
}
156156

157-
std::string get_value_index(const irep_idt &loc)
158-
{
159-
std::string label(CE_VALUES_INDEX_PREFIX);
160-
return label+=id2string(loc);
161-
}
162-
163157
void add_array_indexes(const std::set<irep_idt> &ce_keys, symbol_tablet &st,
164158
goto_functionst &gf)
165159
{
@@ -176,7 +170,7 @@ void add_array_indexes(const std::set<irep_idt> &ce_keys, symbol_tablet &st,
176170
pos=cprover_init;
177171
for (const irep_idt &key : ce_keys)
178172
{
179-
const std::string label(get_value_index(key));
173+
const std::string label(get_ce_value_index_name(key));
180174
pos=declare_cegis_meta_variable(st, gf, pos, label, type);
181175
pos=assign_cegis_meta_variable(st, gf, pos, label, zero);
182176
}
@@ -218,10 +212,10 @@ const index_exprt get_array_val_expr(const symbol_tablet &st,
218212
{
219213
const std::string index_name(get_cegis_meta_name(CE_ARRAY_INDEX));
220214
const symbol_exprt index(st.lookup(index_name).symbol_expr());
221-
const std::string array_name(get_cegis_meta_name(get_array_name(loc)));
215+
const std::string array_name(get_cegis_meta_name(get_ce_array_name(loc)));
222216
const symbol_exprt array(st.lookup(array_name).symbol_expr());
223217
const index_exprt ce(array, index);
224-
const std::string value_index(get_cegis_meta_name(get_value_index(loc)));
218+
const std::string value_index(get_cegis_meta_name(get_ce_value_index_name(loc)));
225219
const symbol_exprt value_index_expr(st.lookup(value_index).symbol_expr());
226220
return index_exprt(ce, value_index_expr);
227221
}
@@ -247,7 +241,7 @@ void assign_ce_values(symbol_tablet &st, goto_functionst &gf,
247241
default:
248242
assert(!"Unsupported counterexample location type.");
249243
}
250-
const std::string value_index(get_cegis_meta_name(get_value_index(label)));
244+
const std::string value_index(get_cegis_meta_name(get_ce_value_index_name(label)));
251245
const symbol_exprt value_index_expr(st.lookup(value_index).symbol_expr());
252246
const plus_exprt inc(increment(value_index_expr));
253247
cegis_assign(st, gf, pos, value_index_expr, inc);
@@ -259,7 +253,7 @@ void insert_counterexamples(symbol_tablet &st, goto_functionst &gf,
259253
labelled_counterexamplest ces, const goto_programt::targetst &ce_locs)
260254
{
261255
assert(!ces.empty());
262-
const zero_valuest zero_values(get_zero_values(st, gf, ce_locs));
256+
const zero_valuest zero_values(get_zero_values(st, ce_locs));
263257
const std::set<irep_idt> ce_keys(get_all_keys(zero_values));
264258
normalise(ce_keys, zero_values, ces);
265259
goto_programt &body=get_entry_body(gf);

0 commit comments

Comments
 (0)