-
Notifications
You must be signed in to change notification settings - Fork 277
Fix local static variables detection in assigns clauses #6447
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:fix-local-statics-detection
Dec 1, 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
31 changes: 31 additions & 0 deletions
31
regression/contracts/assigns_enforce_detect_local_statics/main.c
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,31 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
static int x; | ||
static int xx; | ||
|
||
void foo() | ||
{ | ||
int *y = &x; | ||
int *yy = &xx; | ||
|
||
static int x; | ||
// must pass (modifies local static) | ||
x++; | ||
|
||
// must pass (modifies assignable global static ) | ||
(*y)++; | ||
|
||
// must fail (modifies non-assignable global static) | ||
(*yy)++; | ||
} | ||
|
||
void bar() __CPROVER_assigns(x) | ||
{ | ||
foo(); | ||
} | ||
|
||
int main() | ||
{ | ||
bar(); | ||
} |
14 changes: 14 additions & 0 deletions
14
regression/contracts/assigns_enforce_detect_local_statics/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,14 @@ | ||
CORE | ||
main.c | ||
--enforce-contract bar | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^\[foo.\d+\] line \d+ Check that y is assignable: SUCCESS$ | ||
^\[foo.\d+\] line \d+ Check that foo\$\$1\$\$x is assignable: SUCCESS$ | ||
^\[foo.\d+\] line \d+ Check that \*y is assignable: SUCCESS$ | ||
^\[foo.\d+\] line \d+ Check that \*yy is assignable: FAILURE$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Checks whether static local and global variables are correctly tracked | ||
in assigns clause verification, accross subfunction calls. |
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
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 |
---|---|---|
@@ -1,20 +1,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract f1 _ --unwind 20 --unwinding-assertions | ||
^EXIT=10$ | ||
^file main\.c line 13 function f2: recursion is ignored on call to 'f2'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
^file main.c line \d+ function f2: recursion is ignored on call to \'f2\'$ | ||
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: FAILURE$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Verification: | ||
function | pre-cond | post-cond | ||
---------|----------|---------- | ||
f1 | assumed | asserted | ||
|
||
Test should fail: | ||
The postcondition of f2 is incorrect, considering the recursion particularity. | ||
|
||
Recursion: | ||
The base case for the recursive call to f2 provides different behavior than the common case (given the pre-conditions). | ||
Test should fail because a recursive function is found during inlining. |
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 |
---|---|---|
@@ -1,21 +1,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract f2 _ --unwind 20 --unwinding-assertions | ||
^EXIT=10$ | ||
^file main\.c line 13 function f2: recursion is ignored on call to 'f2'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
^file main.c line \d+ function f2: recursion is ignored on call to \'f2\'$ | ||
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: FAILURE$ | ||
^\[f2.\d+\] line \d+ Check that loc is assignable: SUCCESS$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Verification: | ||
function | pre-cond | post-cond | ||
---------|----------|---------- | ||
f2 | assumed | asserted | ||
|
||
Test should fail: | ||
The postcondition of f2 is incorrect, considering the recursion particularity. | ||
|
||
Recursion: | ||
The base case for the recursive call to f2 provides different behavior than the common case (given the pre-conditions). | ||
Test should fail because a recursive function is found during inlining. |
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 |
---|---|---|
@@ -1,21 +1,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract f1 _ --unwind 20 --unwinding-assertions | ||
^EXIT=10$ | ||
^file main\.c line 19 function f2_in: recursion is ignored on call to 'f2_out'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
^file main.c line \d+ function f2\_in: recursion is ignored on call to \'f2\_out\'$ | ||
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: FAILURE$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Verification: | ||
function | pre-cond | post-cond | ||
---------|----------|---------- | ||
f1 | assumed | asserted | ||
|
||
Test should fail: | ||
The postcondition of f2_out is incorrect, considering the recursion particularity. | ||
|
||
Recursion: | ||
The base case for the recursive call to f2_out provides different behavior | ||
than the general case (given the pre-conditions). | ||
Test should fail because a recursive function is found during inlining. |
22 changes: 6 additions & 16 deletions
22
regression/contracts/function-calls-04-1/test-enf-f2_in.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 |
---|---|---|
@@ -1,22 +1,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract f2_in _ --unwind 20 --unwinding-assertions | ||
^EXIT=10$ | ||
^file main\.c line 13 function f2_out: recursion is ignored on call to 'f2_in'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
^file main.c line \d+ function f2\_out: recursion is ignored on call to \'f2\_in\'$ | ||
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: FAILURE$ | ||
^\[f2\_out.\d+\] line \d+ Check that loc2 is assignable: SUCCESS$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Verification: | ||
function | pre-cond | post-cond | ||
---------|----------|---------- | ||
f2_in | assumed | asserted | ||
|
||
Test should fail: | ||
The postcondition of f2_out is incorrect, considering the recursion particularity. | ||
|
||
Recursion: | ||
The base case for the recursive call to f2_out provides different behavior | ||
than the general case (given the pre-conditions). | ||
Test should fail because a recursive function is found during inlining. |
22 changes: 6 additions & 16 deletions
22
regression/contracts/function-calls-04-1/test-enf-f2_out.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 |
---|---|---|
@@ -1,22 +1,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract f2_out _ --unwind 20 --unwinding-assertions | ||
^EXIT=10$ | ||
^file main\.c line 19 function f2_in: recursion is ignored on call to 'f2_out'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
^file main.c line \d+ function f2\_in: recursion is ignored on call to \'f2\_out\'$ | ||
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: FAILURE$ | ||
^\[f2\_out.\d+\] line \d+ Check that loc2 is assignable: SUCCESS$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
Verification: | ||
function | pre-cond | post-cond | ||
---------|----------|---------- | ||
f2_out | assumed | asserted | ||
|
||
Test should fail: | ||
The postcondition of f2 is incorrect, considering the recursion particularity. | ||
|
||
Recursion: | ||
The base case for the recursive call to f2_out provides different behavior | ||
than the general case (given the pre-conditions). | ||
Test should fail because a recursive function is found during inlining. |
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 @@ | ||
#include <stdlib.h> | ||
|
||
int sum_rec(int i, int acc) | ||
{ | ||
if(i >= 0) | ||
return sum_rec(i - 1, acc + i); | ||
return acc; | ||
} | ||
|
||
int sum(int i) __CPROVER_requires(0 <= i && i <= 50) | ||
__CPROVER_ensures(__CPROVER_return_value >= 0) __CPROVER_assigns() | ||
{ | ||
int j = i; | ||
int res = sum_rec(j, 0); | ||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
int result = sum(10); | ||
__CPROVER_assert(result == 55, "result == 55"); | ||
return 0; | ||
} |
12 changes: 12 additions & 0 deletions
12
regression/contracts/function-calls-recursive-01/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,12 @@ | ||
CORE | ||
main.c | ||
--enforce-contract sum _ --trace | ||
^file main\.c line 6 function sum_rec: recursion is ignored on call to 'sum_rec'$ | ||
^Invariant check failed$ | ||
^Condition: decorated\.get_recursive_function_warnings_count\(\) == 0$ | ||
^Reason: Recursive functions found during inlining$ | ||
^EXIT=(127|134|137)$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
Test should fail because a recursive function is found during inlining. |
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
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 |
---|---|---|
|
@@ -13,6 +13,8 @@ Date: July 2021 | |
|
||
#include "assigns.h" | ||
|
||
#include <analyses/call_graph.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. [nit] this should go above 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. fixed |
||
|
||
#include <langapi/language_util.h> | ||
|
||
#include <util/arith_tools.h> | ||
|
@@ -196,3 +198,31 @@ void havoc_assigns_targetst::append_havoc_code_for_expr( | |
|
||
havoc_utilst::append_havoc_code_for_expr(location, expr, dest); | ||
} | ||
|
||
void assigns_clauset::add_static_locals_to_write_set( | ||
const goto_functionst &functions, | ||
const irep_idt &root_function) | ||
{ | ||
auto call_graph = | ||
call_grapht::create_from_root_function(functions, root_function, true) | ||
.get_directed_graph(); | ||
|
||
for(const auto &sym_pair : symbol_table) | ||
{ | ||
const auto &sym = sym_pair.second; | ||
if(sym.is_static_lifetime) | ||
{ | ||
auto fname = sym.location.get_function(); | ||
if( | ||
!fname.empty() && (fname == root_function || | ||
call_graph.get_node_index(fname).has_value())) | ||
{ | ||
// We found a symbol with | ||
// - a static lifetime | ||
// - non empty location function | ||
// - this function appears in the call graph of the function | ||
add_to_write_set(sym.symbol_expr()); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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: the commit message ...