Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions regression/contracts-dfcc/contracts_ensures_no_assertions/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int f(int x) __CPROVER_requires(x >= 0 && x < 100)
__CPROVER_ensures(__CPROVER_return_value == x + 2)
{
return x + 1;
}

int main()
{
int x;
__CPROVER_assume(x >= 0 && x < 100);
return f(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--dfcc main --enforce-contract f _ --no-assertions
^EXIT=10$
^SIGNAL=0$
^\[f\.postcondition\.\d+\] line 2 Check ensures clause.*: FAILURE$
^VERIFICATION FAILED$
--
--
The check of a contract's ensures clause (here f returns x + 1, not x + 2)
is not one of the user's assertions: --no-assertions keeps it.
11 changes: 11 additions & 0 deletions regression/contracts-dfcc/contracts_requires_no_assertions/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int f(int x) __CPROVER_requires(x >= 0 && x < 100)
__CPROVER_ensures(__CPROVER_return_value == x + 1)
{
return x + 1;
}

int main()
{
int x;
return f(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
main.c
--dfcc main --replace-call-with-contract f _ --no-assertions
^EXIT=10$
^SIGNAL=0$
^\[f\.precondition\.\d+\] line \d+ Check requires clause.*: FAILURE$
^VERIFICATION FAILED$
--
--
The check of a contract's requires clause where a call is replaced by the
contract (here main passes an arbitrary x) is not one of the user's
assertions: --no-assertions keeps it.
11 changes: 11 additions & 0 deletions regression/contracts-dfcc/loop_contracts_no_assertions/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int main()
{
int i = 0;
while(i < 10)
__CPROVER_loop_invariant(i == 0)
{
i++;
}
int x = 2147483647 + i;
return x;
}
13 changes: 13 additions & 0 deletions regression/contracts-dfcc/loop_contracts_no_assertions/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
main.c
--dfcc main --apply-loop-contracts _ --signed-overflow-check --no-assertions
^EXIT=10$
^SIGNAL=0$
^\[main\.(loop_invariant_step\.)?\d+\] line 4 Check (invariant after step for loop main\.0|that loop invariant is preserved): FAILURE$
^VERIFICATION FAILED$
--
--
The check that the loop invariant is inductive (here it is not) is a check
of the loop's contract, not one of the user's assertions: --no-assertions
keeps it. (goto_convert marked it user-provided, and the invariant, which
the loop then assumed, made the overflow after the loop unreachable.)
4 changes: 4 additions & 0 deletions src/goto-instrument/contracts/contracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ void code_contractst::check_apply_loop_contracts(

goto_convertt converter(symbol_table, log.get_message_handler());
converter.goto_convert(assertion, pre_loop_head_instrs, mode);
mark_contract_checks(pre_loop_head_instrs);
}

// Insert the first block of pre_loop_head_instrs,
Expand Down Expand Up @@ -439,6 +440,7 @@ void code_contractst::check_apply_loop_contracts(

goto_convertt converter(symbol_table, log.get_message_handler());
converter.goto_convert(assertion, pre_loop_end_instrs, mode);
mark_contract_checks(pre_loop_end_instrs);
}

// Generate: assignments to store the multidimensional decreases clause's
Expand Down Expand Up @@ -468,6 +470,7 @@ void code_contractst::check_apply_loop_contracts(
goto_convertt converter(symbol_table, log.get_message_handler());
converter.goto_convert(
monotonic_decreasing_assertion, pre_loop_end_instrs, mode);
mark_contract_checks(pre_loop_end_instrs);

// Discard the temporary variables that store decreases clause's value
for(size_t i = 0; i < old_decreases_vars.size(); i++)
Expand Down Expand Up @@ -570,6 +573,7 @@ static void generate_contract_constraints(
code_assertt assertion(instantiated_clause);
assertion.add_source_location() = location;
converter.goto_convert(assertion, constraint, mode);
mark_contract_checks(constraint);
}
is_fresh_update(constraint);
throw_on_unsupported(constraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ void dfcc_instrument_loopt::add_body_instructions(
assertion.add_source_location() = check_location;
converter.goto_convert(assertion, pre_loop_latch_instrs, language_mode);
}
mark_contract_checks(pre_loop_latch_instrs);
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ void dfcc_wrapper_programt::encode_requires_clauses()
codet requires_statement(statement_type, {std::move(requires_lmbd)}, sl);
converter.goto_convert(requires_statement, requires_program, language_mode);
}
mark_contract_checks(requires_program);

// fix calls to user-defined memory predicates
memory_predicates.fix_calls(requires_program);
Expand Down Expand Up @@ -738,6 +739,7 @@ void dfcc_wrapper_programt::encode_ensures_clauses()
codet ensures_statement(statement_type, {std::move(ensures)}, sl);
converter.goto_convert(ensures_statement, ensures_program, language_mode);
}
mark_contract_checks(ensures_program);

// When checking an ensures clause we link the contract write set to the
// ensures write set to know what was deallocated by the function so that
Expand Down
9 changes: 9 additions & 0 deletions src/goto-instrument/contracts/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ void simplify_gotos(goto_programt &goto_program, const namespacet &ns)
}
}

void mark_contract_checks(goto_programt &program)
{
for(auto &instruction : program.instructions)
{
if(instruction.is_assert())
instruction.source_location_nonconst().set("user-provided", false);
}
}

bool is_loop_free(
const goto_programt &goto_program,
const namespacet &ns,
Expand Down
6 changes: 6 additions & 0 deletions src/goto-instrument/contracts/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ void insert_before_and_update_jumps(
/// statically simplifies to `false` into SKIP instructions.
void simplify_gotos(goto_programt &goto_program, const namespacet &ns);

/// Turns the assertions of \p program, the checks of a contract (such as
/// that a loop invariant is inductive), into checks that are not the
/// user's: goto_convert marks the assertions that it converts as
/// user-provided, and --no-assertions would drop them.
void mark_contract_checks(goto_programt &program);

/// Returns true iff the given program is loop-free,
/// i.e. if each SCC of its CFG contains a single element.
bool is_loop_free(
Expand Down
Loading