-
Notifications
You must be signed in to change notification settings - Fork 277
havoc call return value when replacing a call by its contract #6502
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
tautschnig
merged 1 commit into
diffblue:develop
from
remi-delmas-3000:contract-replacement-havoc-return-value
Dec 4, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
int cmp(int i1, int i2) | ||
// clang-format off | ||
__CPROVER_ensures((i1 == i2) ==> (__CPROVER_return_value == 0)) | ||
__CPROVER_ensures((i1 != i2) ==> (__CPROVER_return_value == -1)) | ||
// clang-format on | ||
{ | ||
if(i1 == i2) | ||
return 0; | ||
else | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int ret = -1; | ||
ret = cmp(0, 0); | ||
__CPROVER_assert(ret == 0, "expecting SUCCESS"); | ||
ret = cmp(0, 1); | ||
__CPROVER_assert(ret == 0, "expecting FAILURE"); | ||
__CPROVER_assert(ret == -1, "expecting SUCCESS"); | ||
__CPROVER_assert(0, "expecting FAILURE"); | ||
return 0; | ||
} |
20 changes: 20 additions & 0 deletions
20
regression/contracts/replace-nondet-return-value/test.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
CORE | ||
main.c | ||
--replace-call-with-contract cmp | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^\[main\.assertion\.1\] line \d+ expecting SUCCESS: SUCCESS$ | ||
^\[main\.assertion\.2\] line \d+ expecting FAILURE: FAILURE$ | ||
^\[main\.assertion\.3\] line \d+ expecting SUCCESS: SUCCESS$ | ||
^\[main\.assertion\.4\] line \d+ expecting FAILURE: FAILURE$ | ||
^\*\* 2 of 4 failed | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This test checks that the return value of a replaced function call is made | ||
nondet at each replacement site. | ||
The replaced function is called twice. Each call is expected to have a different | ||
return value. If the return value of the call is not made nondet at each | ||
replacement, it would be subject to contradictory constraints | ||
(from the post conditions) and the assertions expected to fail would | ||
be vacuously satisfied. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ Date: February 2016 | |
#include <goto-instrument/havoc_utils.h> | ||
|
||
#include <goto-programs/goto_inline.h> | ||
#include <goto-programs/goto_program.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is strictly necessary? (Even the |
||
#include <goto-programs/remove_skip.h> | ||
|
||
#include <langapi/language_util.h> | ||
|
@@ -631,6 +632,10 @@ bool code_contractst::apply_function_contract( | |
// with expressions from the call site (e.g. the return value). | ||
// This object tracks replacements that are common to ENSURES and REQUIRES. | ||
replace_symbolt common_replace; | ||
|
||
// keep track of the call's return expression to make it nondet later | ||
optionalt<exprt> call_ret_opt = {}; | ||
|
||
if(type.return_type() != empty_typet()) | ||
{ | ||
// Check whether the function's return value is not disregarded. | ||
|
@@ -640,9 +645,10 @@ bool code_contractst::apply_function_contract( | |
// For example, if foo() ensures that its return value is > 5, then | ||
// rewrite calls to foo as follows: | ||
// x = foo() -> assume(__CPROVER_return_value > 5) -> assume(x > 5) | ||
symbol_exprt ret_val( | ||
CPROVER_PREFIX "return_value", const_target->call_lhs().type()); | ||
common_replace.insert(ret_val, const_target->call_lhs()); | ||
auto &lhs_expr = const_target->call_lhs(); | ||
call_ret_opt = lhs_expr; | ||
symbol_exprt ret_val(CPROVER_PREFIX "return_value", lhs_expr.type()); | ||
common_replace.insert(ret_val, lhs_expr); | ||
} | ||
else | ||
{ | ||
|
@@ -663,7 +669,9 @@ bool code_contractst::apply_function_contract( | |
ns, | ||
symbol_table); | ||
symbol_exprt ret_val(CPROVER_PREFIX "return_value", type.return_type()); | ||
common_replace.insert(ret_val, fresh.symbol_expr()); | ||
auto fresh_sym_expr = fresh.symbol_expr(); | ||
common_replace.insert(ret_val, fresh_sym_expr); | ||
call_ret_opt = fresh_sym_expr; | ||
} | ||
} | ||
} | ||
|
@@ -736,8 +744,10 @@ bool code_contractst::apply_function_contract( | |
targets.add_to_operands(std::move(target)); | ||
common_replace(targets); | ||
|
||
// Create a series of non-deterministic assignments to havoc the variables | ||
// in the assigns clause. | ||
// Create a sequence of non-deterministic assignments... | ||
goto_programt havoc_instructions; | ||
|
||
// ...for assigns clause targets | ||
if(!assigns.empty()) | ||
{ | ||
assigns_clauset assigns_clause( | ||
|
@@ -747,14 +757,25 @@ bool code_contractst::apply_function_contract( | |
modifiest modifies; | ||
modifies.insert(targets.operands().cbegin(), targets.operands().cend()); | ||
|
||
goto_programt assigns_havoc; | ||
havoc_assigns_targetst havoc_gen(modifies, ns); | ||
havoc_gen.append_full_havoc_code(location, assigns_havoc); | ||
havoc_gen.append_full_havoc_code(location, havoc_instructions); | ||
} | ||
|
||
// Insert the non-deterministic assignment immediately before the call site. | ||
insert_before_swap_and_advance(function_body, target, assigns_havoc); | ||
// ...for the return value | ||
if(call_ret_opt.has_value()) | ||
{ | ||
auto &call_ret = call_ret_opt.value(); | ||
auto &loc = call_ret.source_location(); | ||
auto &type = call_ret.type(); | ||
side_effect_expr_nondett expr(type, location); | ||
auto target = havoc_instructions.add( | ||
goto_programt::make_assignment(call_ret, expr, loc)); | ||
target->code_nonconst().add_source_location() = loc; | ||
} | ||
|
||
// Insert havoc instructions immediately before the call site. | ||
insert_before_swap_and_advance(function_body, target, havoc_instructions); | ||
|
||
// To remove the function call, insert statements related to the assumption. | ||
// Then, replace the function call with a SKIP statement. | ||
if(!ensures.is_false()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Random place for comment: can you please add "Fixes: #6483" to the commit message so that the issue is auto-resolved once this PR is merged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go ahead and merge anyway, so this is just for future reference.