Skip to content

Commit f7e1845

Browse files
committed
Add enumerative loop invariant synthesizer
Implement the functionality described below. Motivation --- This loop invariant synthesizer use the idea counter-example-guided synthesis (CEGIS) to synthesize loop invariants for programs with only checks instrumented by `goto-instrument` with flag `--pointer-check`. This PR contain the driver of the synthesizer and the verifier we use to check invariant candidates. Verifier --- The verifier take as input a goto program with pointer checks and a map from loop id to loop invariant candidates. It first annotate and apply the loop invariant candidates into the goto model; and then simulate the CBMC api to verify the instrumented goto program. If there are some violations---loop invariants are not inductive, or some pointer checks fail---, it record valuation from trace generated by the back end to construct a formatted counterexample `cext`. Counterexample --- A counterexample `cext` record valuations of variables in the trace, and other information about the violation. The valuation we record including 1. set of live variables upon the entry of the loop. 2. the havoced value of all primitive-typed variables; 3. the havoced offset and the object size of all pointer-typed variables; 4. history values of 2 and 3. The valuations will be used as true positive (history values) and true negative (havcoed valuation) to filter out bad invariant clause with the idea of the Daikon invariant detector in a following PR. However, in this PR we only construct the valuation but not actually use them. Synthesizer --- Loop invariants we synthesize are of the form `` (in_clause || !guard) && (!guard -> pos_clause)`` where `in_clause` and `out_clause` are predicates we store in two different map, and `guard` is the loop guard. The idea is that we separately synthesize the condition `in_clause` that should hold before the execution of the loop body, and condition `pos_clause` that should hold as post-condition of the loop. When the violation happen in the loop, we update `in_clause`. When the violation happen after the loop, we update `pos_clause`. When the invariant candidate it not inductive, we enumerate strengthening clause to make it inductive. To be more efficient, we choose different synthesis strategy for different type of violation * For out-of-boundary violation, we choose to use the violated predicate as the new clause, which is the WLP of the violation if the violation is only dependent on the havocing instruction. **TODO**: to make it more complete, we need to implement a WLP algorithm * For null-pointer violation, we choose `__CPROVER_same_object(ptr, __CPROVER_loop_entry(ptr))` as the new clause. That is, the havoced pointer should points to the same object at the start of every iteration of the loop. It is a heuristic choice. This can be extended with the idea of alias analysis if needed. * For invariant-not-preserved violation, we enumerate strengthening clauses and check that if the invariant will be inductive after strengthening (disjunction with the new clause). The synthesizer works as follow 1. initialize the two invariant maps, 2. verify the current candidates built from the two maps, _a. return the candidate if there is no violation _b. synthesize a new clause to resolve the **first** violation and add it to the correct map, 3. repeat 2.
1 parent 6f40463 commit f7e1845

28 files changed

+1406
-48
lines changed

regression/goto-synthesizer/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ else()
1212
set(gcc_only_string "")
1313
endif()
1414

15-
1615
add_test_pl_tests(
17-
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $<TARGET_FILE:goto-cc> $<TARGET_FILE:goto-synthesizer> $<TARGET_FILE:cbmc> ${is_windows}"
16+
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $<TARGET_FILE:goto-cc> $<TARGET_FILE:goto-instrument> $<TARGET_FILE:goto-synthesizer> $<TARGET_FILE:cbmc> ${is_windows}"
1817
)
1918

2019
## Enabling these causes a very significant increase in the time taken to run the regressions

regression/goto-synthesizer/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ else
1414
endif
1515

1616
test:
17-
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-synthesizer/goto-synthesizer ../../../src/cbmc/cbmc $(is_windows)' -X smt-backend $(GCC_ONLY)
17+
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/goto-synthesizer/goto-synthesizer ../../../src/cbmc/cbmc $(is_windows)' -X smt-backend $(GCC_ONLY)
1818

1919
test-cprover-smt2:
20-
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-synthesizer/goto-synthesizer "../../../src/cbmc/cbmc --cprover-smt2" $(is_windows)' \
20+
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/goto-synthesizer/goto-synthesizer "../../../src/cbmc/cbmc --cprover-smt2" $(is_windows)' \
2121
-X broken-smt-backend -X thorough-smt-backend \
2222
-X broken-cprover-smt-backend -X thorough-cprover-smt-backend \
2323
-s cprover-smt2 $(GCC_ONLY)
2424

2525
test-z3:
26-
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-synthesizer/goto-synthesizer "../../../src/cbmc/cbmc --z3" $(is_windows)' \
26+
@../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/goto-synthesizer/goto-synthesizer "../../../src/cbmc/cbmc --z3" $(is_windows)' \
2727
-X broken-smt-backend -X thorough-smt-backend \
2828
-X broken-z3-smt-backend -X thorough-z3-smt-backend \
2929
-s z3 $(GCC_ONLY)

regression/goto-synthesizer/chain.sh

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
set -e
44

55
goto_cc=$1
6-
goto_synthesizer=$2
7-
cbmc=$3
8-
is_windows=$4
6+
goto_instrument=$2
7+
goto_synthesizer=$3
8+
cbmc=$4
9+
is_windows=$5
910

1011
name=${*:$#}
1112
name=${name%.c}
1213

13-
args=${*:5:$#-5}
14+
args=${*:6:$#-6}
1415
if [[ "$args" != *" _ "* ]]
1516
then
1617
args_inst=$args
@@ -27,7 +28,9 @@ else
2728
fi
2829

2930
rm -f "${name}-mod.gb"
30-
$goto_synthesizer ${args_inst} "${name}.gb" "${name}-mod.gb"
31+
rm -f "${name}-mod-2.gb"
32+
echo "Running goto-instrument: "
33+
$goto_instrument ${args_inst} "${name}.gb" "${name}-mod.gb"
3134
if [ ! -e "${name}-mod.gb" ] ; then
3235
cp "$name.gb" "${name}-mod.gb"
3336
elif echo $args_inst | grep -q -- "--dump-c" ; then
@@ -41,4 +44,7 @@ elif echo $args_inst | grep -q -- "--dump-c" ; then
4144

4245
rm "${name}-mod.c"
4346
fi
44-
$cbmc "${name}-mod.gb" ${args_cbmc}
47+
echo "Running goto-synthesizer: "
48+
$goto_synthesizer "${name}-mod.gb" "${name}-mod-2.gb"
49+
echo "Running CBMC: "
50+
$cbmc "${name}-mod-2.gb" ${args_cbmc}

regression/goto-synthesizer/loop_contracts_synthesis_01/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
3+
--pointer-check
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[main\.\d+\] line 10 Check loop invariant before entry: SUCCESS$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#define SIZE 80
2+
3+
void main()
4+
{
5+
unsigned len;
6+
__CPROVER_assume(len <= SIZE);
7+
__CPROVER_assume(len >= 8);
8+
char *array = malloc(len);
9+
unsigned s = 0;
10+
11+
for(unsigned i = 0; i < SIZE; ++i)
12+
{
13+
if(i == len - 1)
14+
break;
15+
s += array[i];
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.pointer\_dereference.\d+\] .* SUCCESS$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
--
10+
This test shows that loop invariants using range predicates can be correctly
11+
synthesized for programs with only pointer checks but no other assertions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define SIZE 80
2+
3+
void main()
4+
{
5+
unsigned long len;
6+
__CPROVER_assume(len <= SIZE);
7+
__CPROVER_assume(len >= 8);
8+
char *array = malloc(len);
9+
const char *end = array + len;
10+
unsigned s = 0;
11+
12+
while(array != end)
13+
{
14+
s += *array++;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.pointer\_dereference.\d+\] .* SUCCESS$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
--
10+
This test shows that loop invariants using range predicates and same-object
11+
predicates can be correctly synthesized for programs with only pointer
12+
checks but no other assertions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#define SIZE 80
2+
3+
void main()
4+
{
5+
unsigned long len;
6+
__CPROVER_assume(len <= SIZE);
7+
__CPROVER_assume(len >= 8);
8+
char *array = malloc(len);
9+
unsigned long s = 0;
10+
11+
unsigned long j = 0;
12+
for(unsigned long i = 0; i < len; i++)
13+
{
14+
s += array[j];
15+
j++;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.pointer\_dereference.\d+\] .* SUCCESS$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
--
10+
This test shows that the loop-invariant synthesizer can enumerate
11+
strengthening clauses for invariant-not-preserved violation.

src/goto-instrument/contracts/contracts.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void code_contractst::check_apply_loop_contracts(
5959
const irep_idt &mode)
6060
{
6161
const auto loop_head_location = loop_head->source_location();
62+
const unsigned loop_number = loop_end->loop_number;
6263

6364
// Vector representing a (possibly multidimensional) decreases clause
6465
const auto &decreases_clause_exprs = decreases_clause.operands();
@@ -142,7 +143,7 @@ void code_contractst::check_apply_loop_contracts(
142143
// i.e., the loop guard was satisfied.
143144
const auto entered_loop =
144145
new_tmp_symbol(
145-
bool_typet(), loop_head_location, mode, symbol_table, "__entered_loop")
146+
bool_typet(), loop_head_location, mode, symbol_table, ENTERED_LOOP)
146147
.symbol_expr();
147148
pre_loop_head_instrs.add(
148149
goto_programt::make_decl(entered_loop, loop_head_location));
@@ -173,7 +174,7 @@ void code_contractst::check_apply_loop_contracts(
173174
// instrumentation of the loop.
174175
const auto in_base_case =
175176
new_tmp_symbol(
176-
bool_typet(), loop_head_location, mode, symbol_table, "__in_base_case")
177+
bool_typet(), loop_head_location, mode, symbol_table, IN_BASE_CASE)
177178
.symbol_expr();
178179
pre_loop_head_instrs.add(
179180
goto_programt::make_decl(in_base_case, loop_head_location));
@@ -440,6 +441,28 @@ void code_contractst::check_apply_loop_contracts(
440441
loop_end,
441442
add_pragma_disable_assigns_check(pre_loop_end_instrs));
442443

444+
// Record original loop number for some instrumented instructions.
445+
for(goto_programt::const_targett it_instr =
446+
goto_function.body.instructions.begin();
447+
it_instr != goto_function.body.instructions.end();
448+
it_instr++)
449+
{
450+
// Don't override original loop numbers.
451+
if(original_loop_number_map.count(it_instr) != 0)
452+
continue;
453+
454+
// Store loop number for
455+
// ASSIGN ENTERED_LOOP = TRUE
456+
if(
457+
is_assignment_to_instrumented_variable(it_instr, ENTERED_LOOP) &&
458+
it_instr->assign_rhs() == true_exprt())
459+
original_loop_number_map[it_instr] = loop_number;
460+
461+
// Store loop number for loop havoc.
462+
if(is_loop_havoc(*it_instr))
463+
original_loop_number_map[it_instr] = loop_number;
464+
}
465+
443466
// change the back edge into assume(false) or assume(guard)
444467
loop_end->turn_into_assume();
445468
loop_end->condition_nonconst() = boolean_negate(loop_end->condition());

src/goto-instrument/contracts/contracts.h

+13
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ class code_contractst
122122
symbol_tablet &get_symbol_table();
123123
goto_functionst &get_goto_functions();
124124

125+
std::unordered_map<goto_programt::const_targett, unsigned, const_target_hash>
126+
get_original_loop_number_map() const
127+
{
128+
return original_loop_number_map;
129+
}
130+
125131
namespacet ns;
126132

127133
protected:
@@ -137,6 +143,13 @@ class code_contractst
137143
/// Name of loops we are going to unwind.
138144
std::list<std::string> loop_names;
139145

146+
/// Store the map from instrumented instructions for loop contracts to their
147+
/// original loop numbers. Following instrumented instructions are stored.
148+
/// 1. loop-havoc --- begin of transformed loops
149+
/// 2. ASSIGN ENTERED_LOOP = TRUE --- end of transformed loops
150+
std::unordered_map<goto_programt::const_targett, unsigned, const_target_hash>
151+
original_loop_number_map;
152+
140153
public:
141154
/// \brief Enforce contract of a single function
142155
void enforce_contract(const irep_idt &function);

src/goto-instrument/contracts/utils.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,34 @@ void generate_history_variables_initialization(
447447
// Add all the history variable initialization instructions
448448
program.destructive_append(history);
449449
}
450+
451+
bool is_transformed_loop_end(const goto_programt::const_targett &target)
452+
{
453+
// The end of the loop end of transformed loop is
454+
// ASSIGN entered_loop = true
455+
if(!is_assignment_to_instrumented_variable(target, ENTERED_LOOP))
456+
return false;
457+
458+
return target->assign_rhs() == true_exprt();
459+
}
460+
461+
bool is_assignment_to_instrumented_variable(
462+
const goto_programt::const_targett &target,
463+
std::string var_name)
464+
{
465+
INVARIANT(
466+
var_name == IN_BASE_CASE || var_name == ENTERED_LOOP,
467+
"var_name is not of instrumented variables.");
468+
469+
if(!target->is_assign())
470+
return false;
471+
472+
if(can_cast_expr<symbol_exprt>(target->assign_lhs()))
473+
{
474+
const auto &lhs = to_symbol_expr(target->assign_lhs());
475+
return id2string(lhs.get_identifier()).find("::" + var_name) !=
476+
std::string::npos;
477+
}
478+
479+
return false;
480+
}

src/goto-instrument/contracts/utils.h

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Date: September 2021
1717

1818
#include <goto-programs/goto_convert_class.h>
1919

20+
#define IN_BASE_CASE "__in_base_case"
21+
#define ENTERED_LOOP "__entered_loop"
22+
2023
/// \brief A class that overrides the low-level havocing functions in the base
2124
/// utility class, to havoc only when expressions point to valid memory,
2225
/// i.e. if all dereferences within an expression are valid
@@ -205,4 +208,13 @@ void generate_history_variables_initialization(
205208
const irep_idt &mode,
206209
goto_programt &program);
207210

211+
/// Return true if `target` is the loop end of some transformed code.
212+
bool is_transformed_loop_end(const goto_programt::const_targett &target);
213+
214+
/// Return true if `target` is an assignment to an instrumented variable with
215+
/// name `var_name`.
216+
bool is_assignment_to_instrumented_variable(
217+
const goto_programt::const_targett &target,
218+
std::string var_name);
219+
208220
#endif // CPROVER_GOTO_INSTRUMENT_CONTRACTS_UTILS_H

src/goto-instrument/havoc_utils.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ Date: July 2021
1616
#include <util/pointer_expr.h>
1717
#include <util/std_code.h>
1818

19-
#include <goto-programs/goto_program.h>
19+
#define IS_LOOP_HAVOC "Loop havocing instrumented by applying loop contracts."
20+
21+
bool is_loop_havoc(const goto_programt::instructiont &instruction)
22+
{
23+
return id2string(instruction.source_location().get_comment()) ==
24+
IS_LOOP_HAVOC;
25+
}
2026

2127
void havoc_utilst::append_full_havoc_code(
2228
const source_locationt location,
@@ -52,6 +58,8 @@ void havoc_utilst::append_object_havoc_code_for_expr(
5258
havoc.add_source_location() = location;
5359
havoc.add_to_operands(expr);
5460
dest.add(goto_programt::make_other(havoc, location));
61+
dest.instructions.back().source_location_nonconst().set_comment(
62+
IS_LOOP_HAVOC);
5563
}
5664

5765
void havoc_utilst::append_scalar_havoc_code_for_expr(
@@ -62,4 +70,6 @@ void havoc_utilst::append_scalar_havoc_code_for_expr(
6270
side_effect_expr_nondett rhs(expr.type(), location);
6371
dest.add(goto_programt::make_assignment(
6472
code_assignt{expr, std::move(rhs), location}, location));
73+
dest.instructions.back().source_location_nonconst().set_comment(
74+
IS_LOOP_HAVOC);
6575
}

src/goto-instrument/havoc_utils.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ Date: July 2021
1717
#include <util/expr.h>
1818
#include <util/expr_util.h>
1919

20-
#include <set>
20+
#include <goto-programs/goto_program.h>
2121

22-
class goto_programt;
22+
#include <set>
2323

2424
typedef std::set<exprt> assignst;
2525

26+
/// Return true if `instruction` is a loop havoc instruction.
27+
bool is_loop_havoc(const goto_programt::instructiont &instruction);
28+
2629
/// \brief A class containing utility functions for havocing expressions.
2730
class havoc_utils_is_constantt : public is_constantt
2831
{

src/goto-programs/loop_ids.h

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct loop_idt
3131
{
3232
}
3333

34+
loop_idt(const loop_idt &other) = default;
35+
3436
irep_idt function_id;
3537
unsigned int loop_number;
3638

@@ -39,6 +41,11 @@ struct loop_idt
3941
return function_id == o.function_id && loop_number == o.loop_number;
4042
}
4143

44+
bool operator!=(const loop_idt &o) const
45+
{
46+
return !operator==(o);
47+
}
48+
4249
bool operator<(const loop_idt &o) const
4350
{
4451
return function_id < o.function_id ||

0 commit comments

Comments
 (0)