Skip to content

CONTRACTS: Unwind transformed loops after loop contract transformation #7318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2022
Merged
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
4 changes: 2 additions & 2 deletions regression/contracts/invar_assigns_empty/test.desc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ main.c
--apply-loop-contracts
^EXIT=0$
^SIGNAL=0$
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
^\[main.\d+\] .* Check loop invariant before entry: SUCCESS$
^\[main.\d+\] .* Check that loop invariant is preserved: SUCCESS$
^VERIFICATION SUCCESSFUL$
--
--
Expand Down
14 changes: 14 additions & 0 deletions regression/contracts/invar_loop_constant_pass/test_unwind.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE
main.c
--apply-loop-contracts --unwind 1 --unwinding-assertions
^EXIT=0$
^SIGNAL=0$
^\[main\.\d+\] .* Check loop invariant before entry: SUCCESS$
^\[main\.\d+\] .* Check that loop invariant is preserved: SUCCESS$
^\[main.assigns.\d+\] .* Check that s is assignable: SUCCESS$
^\[main.assigns.\d+\] .* Check that r is assignable: SUCCESS$
^\[main\.assertion\.\d+\] .* assertion r == 0: SUCCESS$
^\[main\.assertion\.\d+\] .* assertion s == 1: SUCCESS$
^VERIFICATION SUCCESSFUL$
--
This test checks that there is no loop after contract transforamtion.
19 changes: 19 additions & 0 deletions src/goto-instrument/contracts/contracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Date: February 2016
#include <ansi-c/c_expr.h>
#include <goto-instrument/havoc_utils.h>
#include <goto-instrument/nondet_static.h>
#include <goto-instrument/unwind.h>
#include <goto-instrument/unwindset.h>
#include <langapi/language_util.h>

#include "cfg_info.h"
Expand Down Expand Up @@ -1016,6 +1018,17 @@ void code_contractst::apply_loop_contract(
goto_functions.update();
natural_loops_mutablet updated_loops(goto_function.body);

// We will unwind all transformed loops twice. Store the names of
// to-unwind-loops here and perform the unwinding after transformation done.
// As described in `check_apply_loop_contracts`, loops with loop contracts
// will be transformed to a loop that execute exactly twice: one for base
// case and one for step case. So we unwind them here to avoid users
// incorrectly unwind them only once.
std::string to_unwind = id2string(function_name) + "." +
std::to_string(loop_node.end_target->loop_number) +
":2";
loop_names.push_back(to_unwind);

check_apply_loop_contracts(
function_name,
goto_function,
Expand Down Expand Up @@ -1459,6 +1472,12 @@ void code_contractst::apply_loop_contracts(
"of static/global variables."
<< messaget::eom;
nondet_static(goto_model, to_exclude_from_nondet_init);

// unwind all transformed loops twice.
unwindsett unwindset{goto_model};
unwindset.parse_unwindset(loop_names, log.get_message_handler());
goto_unwindt goto_unwind;
goto_unwind(goto_model, unwindset, goto_unwindt::unwind_strategyt::ASSUME);
}

void code_contractst::enforce_contracts(
Expand Down
3 changes: 3 additions & 0 deletions src/goto-instrument/contracts/contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class code_contractst

std::unordered_set<irep_idt> summarized;

/// Name of loops we are going to unwind.
std::list<std::string> loop_names;

public:
/// Translates a function_pointer_obeys_contract_exprt into an assertion
/// ```
Expand Down