-
Notifications
You must be signed in to change notification settings - Fork 274
Simplify multiple-of-element size access to arrays #8627
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
Draft
tautschnig
wants to merge
3
commits into
diffblue:develop
Choose a base branch
from
tautschnig:simp_mult_offset
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,14 @@ | ||
int main() | ||
{ | ||
char source[8]; | ||
int int_source[2]; | ||
int target[4]; | ||
int n; | ||
if(n >= 0 && n < 3) | ||
{ | ||
__CPROVER_array_replace(&target[n], source); | ||
__CPROVER_array_replace(&target[n], int_source); | ||
__CPROVER_assert(target[n] == int_source[0], ""); | ||
__CPROVER_assert(target[n + 1] == int_source[1], ""); | ||
} | ||
} |
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,13 @@ | ||
CORE paths-lifo-expected-failure | ||
main.c | ||
--program-only | ||
target!0@1#2 == | ||
target!0@1#3 == | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
byte_update_ | ||
-- | ||
This test demonstrates that we can simplify byte_update expressions to, e.g., | ||
WITH expressions. | ||
Disabled for paths-lifo mode, which does not support --program-only. |
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,9 @@ | ||
CORE | ||
main.c | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
-- |
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,109 @@ | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
|
||
When translating to SMT, structs are represented by algebraic datatypes (ADTs) | ||
and arrays of structs by arrays of ADTs. | ||
|
||
When forming a pointer ranging over an array of struct using a nondeterministic | ||
index, the pointer offset appears completely non-deterministic to CBMC, and | ||
in-place updates made using assignments or __CPROVER_array_replace then expand | ||
into large sequences of byte_updates ranging over the whole array. | ||
|
||
If we could somehow track the fact that a pointer formed using arr[i] is still | ||
aligned on array cell boundaries, i.e. is of the form i*sizeof(T) where T is the | ||
type of the array, across intermediate variables and assignments then we would | ||
be able to encode these cases in SMT more optimally: | ||
|
||
T arr[N]; | ||
size_t i = nondetd_size_t(); | ||
if (i < N) { | ||
T *ai = &arr[i]; | ||
T v = nondet_T(); | ||
*ai = v; | ||
// here we have ai of the form &a[0] + i*sizeof(T) assigned with a value of | ||
size sizeof(T) | ||
// can be modeled as a functional array update at index i. | ||
} | ||
|
||
size_t k = nondet_size_t(); | ||
if (k < N) { | ||
size_t S = N-k; | ||
T nondet[S]; | ||
T *ak = &a[k]; | ||
__CPROVER_array_replace(ak, nondet); | ||
// here we have ai of the form &a[0] + k*sizeof(T) updated in place with a | ||
value of size k*sizeof(T) | ||
// can be modeled as a functional slice update at index k with k elements. | ||
} | ||
|
||
At the moment these constructs blow up with --z3 and --bitwuzla | ||
*/ | ||
|
||
// #define N 4 // use 8, 16, ... to see the blowup | ||
#define K 4 | ||
|
||
typedef struct | ||
{ | ||
int32_t coeffs[N]; | ||
} vec_N; | ||
|
||
typedef struct | ||
{ | ||
vec_N vec[K]; | ||
} vec_K_N; | ||
|
||
unsigned int __VERIFIER_nondet_unsigned_int(); | ||
vec_N __VERIFIER_nondet_vec_N(); | ||
|
||
int main(void) | ||
{ | ||
vec_K_N *v = malloc(sizeof(vec_K_N)); | ||
__CPROVER_assume(v); | ||
|
||
unsigned int i = __VERIFIER_nondet_unsigned_int(); | ||
|
||
// models a nondet loop step from an arbitrary state | ||
if(i < K) | ||
{ | ||
#ifdef ASSIGN_DIRECT | ||
// nondet assignment without indirection through a | ||
// simplifies to a functional update | ||
v->vec[i] = __VERIFIER_nondet_vec_N(); | ||
#endif | ||
|
||
// simulates how symex models argument passing for a function call | ||
vec_N *__arg = &v->vec[i]; | ||
vec_N *a = __arg; | ||
|
||
#ifdef ASSIGN | ||
// nondet assignment with indirection through a | ||
// currently does NOT simplifies to a functional update but ultimately | ||
// should | ||
*a = __VERIFIER_nondet_vec_N(); | ||
#endif | ||
|
||
#ifdef SLICE_BYTES | ||
// Modeled as a byte slice operation without indirection | ||
// does NOT simplify to a functional array update due to lack of pattern | ||
// matching on the pointer offset expression. | ||
// We could realize the pointer offset is of the form i*16 and that the | ||
// new value is of size 16 too but we currently don't. | ||
char nondet[sizeof(vec_N)]; | ||
__CPROVER_array_replace((char *)a, nondet); | ||
#endif | ||
|
||
#ifdef SLICE_TYPED | ||
// Modeled as a typed slice operation without indirection. | ||
// Does NOT simplify to a functional array update due to lack of pattern | ||
// matching on the pointer offset expression and types. | ||
// We could realize the pointer offset is of the form i*16 and that the | ||
// new value is of size 16 too but we currently don't. | ||
vec_N nondet[1]; | ||
__CPROVER_array_replace(a, nondet); | ||
#endif | ||
__CPROVER_assert(a->coeffs[0] > 0, "expected to fail"); | ||
} | ||
return 0; | ||
} |
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,8 @@ | ||
CORE | ||
functional.c | ||
-DN=4 -DASSIGN_DIRECT | ||
^VERIFICATION FAILED$ | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring |
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,10 @@ | ||
CORE | ||
functional.c | ||
-DN=4 -DASSIGN --program-only | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
byte_update | ||
-- | ||
We want these tests not to produce any byte_update expressions, but instead want | ||
updates at specific array indices. |
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,10 @@ | ||
CORE | ||
functional.c | ||
-DN=4 -DASSIGN_DIRECT --program-only | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
byte_update | ||
-- | ||
We want these tests not to produce any byte_update expressions, but instead want | ||
updates at specific array indices. |
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,10 @@ | ||
CORE | ||
functional.c | ||
-DN=4 -DSLICE_BYTES --program-only | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
byte_update | ||
-- | ||
We want these tests not to produce any byte_update expressions, but instead want | ||
updates at specific array indices. |
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,10 @@ | ||
CORE | ||
functional.c | ||
-DN=4 -DSLICE_TYPED --program-only | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
byte_update | ||
-- | ||
We want these tests not to produce any byte_update expressions, but instead want | ||
updates at specific array indices. |
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.