Skip to content

Fix irrelevant loss of const throwing off the const analysis #1007

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 3 commits into from
Jul 17, 2017

Conversation

thk123
Copy link
Contributor

@thk123 thk123 commented Jun 13, 2017

There is a bug in does_remove_const analysis where code like this:

const int a = 4;
int b = a;

Would cause it to think the const-correctness had been lost (as detailed in diffblue/cbmc-toyota#127 🔒)

This fixes this by applying a more subtle condition when checking whether an assign statement actually violates const correctness:

if the type is a pointer:
  then the target subtype should be at least as const as the source subtype.
  also if the subtype is a pointer then this check should be recursively applied on this subtype
if the type is not a pointer we no longer require it to be at least as const

Added unit tests to document exactly what the two methods do as the difference is quite subtle. The unit tests are a little difficult to read (and I'd ideally like to test int** type assignments). I will come back to this later to structure them into a table format so can be easily verified by others that they are doing what they expect.

Though this example program appears to lose const-ness, since it is a
primitive it is a copy so it is irrelevant.
@thk123
Copy link
Contributor Author

thk123 commented Jun 13, 2017

Requesting reviews from: @NlightNFotis @martin-cs

thk123 added 2 commits June 13, 2017 18:02
Previously the check for loss of const-correctness was to look at each
assignment and check whether in either the left/right hand sides of the
assignment or somewhere in the right hand side expression tree the
following condition was met:

Any type or any of its subtypes lost const-ness

This condition is now replaced by a more subtle condition:

if they are both pointers then:
there subtypes should not directly lose const-ness
if the subtype is a pointer, we recursively apply this check on the
subtypes
if they are not pointers:
we don't care about const-qualification loss at this level since it is
a copy

This fixes diffblue/cbmc-toyota#127 where an `const int` being assigned
to an
`int` would trigger disabling the function pointer removal.

Pulled out the private access class into a util file

Updating

New implementation of  is_type_at_least_as_const_as only checks one
depth so we
don't need to pass in naked pointers. Updated the comment to reflect
methods new
purpose. Added unit tests to document exactly what the method is testing

Added tests to demonstrate the does_type_preserve_const_correctness

This is useful documentation of how it is different to the
is_type_at_least_as_const_as.

Added more examples of spurious const loss
@thk123 thk123 force-pushed the bugfix/irrelevant-const-removal branch from e56e15c to fcd4e42 Compare June 13, 2017 17:03
Copy link
Contributor

@NlightNFotis NlightNFotis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants