Skip to content

Initial reworking of code contracts [depends-on: #3769, blocks: #2677] #2676

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

Closed
wants to merge 3 commits into from

Conversation

qaphla
Copy link

@qaphla qaphla commented Aug 3, 2018

The primary function of this change is to split the --apply-code-contracts pass into two separate passes, one of which uses (without checking) the contracts, and one of which checks and applies the contracts together. More details can be seen in the commit messages.

One key note from the commit messages:
While it is clear that applying contracts without checking them
is unsound, it is nevertheless useful in verifying large pieces
of software in a modular fashion. If we verify a function to
satisfy its specification from an arbitrary environment, then
we can avoid needing to check that function again by using
--apply-code-contracts.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

This PR failed Diffblue compatibility checks (cbmc commit: 10e55d8).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/80887591
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

if(cmdline.isset("check-code-contracts"))
{
status() << "Checking Code Contracts" << eom;
check_code_contracts(goto_model);
}

Copy link
Member

Choose a reason for hiding this comment

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

Some comment for the "apply-code-contracts" case would be helpful -- it's now easy to be confused between apply-code-contracts and check-code-contracts

@@ -107,3 +108,41 @@ void get_modifies(
}
}
}

// TODO handle aliasing at all
void get_uses(
Copy link
Member

Choose a reason for hiding this comment

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

I would rename this to something more verbose, say "get_used_symbols", and also comment that you do include symbols whose address is taken.

const local_may_aliast &local_may_alias,
const loopt &loop,
usest &uses)
{
Copy link
Member

Choose a reason for hiding this comment

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

Also, C++ now has rvalue references, which implies that large data structures can be returned without copy. I'd consider returning directly here.

@@ -17,13 +17,19 @@ Author: Daniel Kroening, [email protected]
class local_may_aliast;

typedef std::set<exprt> modifiest;
typedef std::set<exprt> usest;
typedef const natural_loops_mutablet::natural_loopt loopt;
Copy link
Member

Choose a reason for hiding this comment

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

Make this a set of symbols?

.gitignore Outdated
@@ -19,6 +19,8 @@ src/.settings/*
# Visual Studio
Debug/*
Release/*
#vi(m)
*.swp

Copy link
Collaborator

Choose a reason for hiding this comment

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

In the future I think this kind of thing would be better as an independent PR.

@@ -33,7 +33,7 @@ void foo(bar *x)
__CPROVER_requires(__CPROVER_VALID_MEM(x, sizeof(bar)))
{
x->x += 1;
return
return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a little concerned about why this wasn't caught earlier. Were these tests just not run?

Copy link
Author

Choose a reason for hiding this comment

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

This was a test that was introduced as KNOWNBUG (and presumably copied poorly from the original piece of code that inspired it).

@@ -107,3 +108,41 @@ void get_modifies(
}
}
}

// TODO handle aliasing at all
Copy link
Collaborator

Choose a reason for hiding this comment

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

:-\ This makes me a little nervous...

Copy link
Author

Choose a reason for hiding this comment

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

I'm working on removing this --- this function was introduced very briefly before it was removed and I thought that it hadn't made it into any of the pull requests. Next revision shouldn't have it (or any of the problems that came with it being very temporary).

{
if((*it).id()==ID_symbol)
{
uses.insert(*it);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would goto_rw be useful here?

Copy link
Author

Choose a reason for hiding this comment

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

See #2677 for a replacement of the local_may_alias analysis used in this version with goto_rw.

klaas added 3 commits August 6, 2018 11:20
The previous implementation of code-contracts has a single flag
--apply-code-contracts, which created checks to verify that the
contracts are satisfied and also used the contracts to abstract
out the portions of code which had contracts provided.

This commit splits that process into two parts, one of which
only uses the contracts, without checking that they hold
(--apply-code-contracts) and the other of which (incomplete as of
this commit) both checks and applies, in a similar manner to the
existing method.

While it is clear that applying contracts without checking them
is unsound, it is nevertheless useful in verifying large pieces
of software in a modular fashion. If we verify a function to
satisfy its specification from an arbitrary environment, then
we can avoid needing to check that function again by using
--apply-code-contracts.
This commit completes the work started in the previous commit
by revising the existing contract-handling code into the new pass
--check-code-contracts, which first applies all contracts and then
checks them. By applying the contracts before checking begins, this
checks the relative correctness of each function/loop with respect
to the functions called by/loops contained in it. Since there can
be no infinite sequence of nested functions/loops, these relative
correctness results together imply the correctness of the overall
program. We take this approach rather than checking entirely
without using contracts because it allows significant reuse of
results --- a function that is called many times only needs to be
checked once, and all of its invocations will be abstracted out
by the contract application.
Several functions from the old implementation of code-contracts that
were made dead by the previous commits are removed by this commit.

Additionally, some variables which were formerly in use are no longer
needed, and so are removed.
@kroening
Copy link
Member

kroening commented Aug 6, 2018

Apologies for bugging about the difference between "applying" and "checking" code contracts: How about "assume-code-contracts" -- this would make the difference clearer?

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

This PR failed Diffblue compatibility checks (cbmc commit: 9d249ba).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/81044800
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

This PR failed Diffblue compatibility checks (cbmc commit: c3c6b8b).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/81048563
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

@qaphla
Copy link
Author

qaphla commented Aug 6, 2018

Would you suggest having assume/check as the pair, or assume/assert as a more closely matched pair?

@kroening
Copy link
Member

kroening commented Aug 7, 2018

assume/assert will likely be the most familiar choice for existing users!

@tautschnig tautschnig self-assigned this Nov 10, 2018
@tautschnig tautschnig changed the title Initial reworking of code contracts Initial reworking of code contracts [blocks: #2677] Nov 10, 2018
@tautschnig tautschnig changed the title Initial reworking of code contracts [blocks: #2677] Initial reworking of code contracts [depends-on: #3769, blocks: #2677] Feb 25, 2019
@TGWDB
Copy link
Contributor

TGWDB commented May 3, 2023

Closing due to age (no further comment on PR content), please reopen with rebase on develop if you intent to continue this work.

@TGWDB TGWDB closed this May 3, 2023
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.

6 participants