Skip to content

CONTRACTS: Error-out on unknown functions (replace/enforce). #6733

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

Conversation

remi-delmas-3000
Copy link
Collaborator

@remi-delmas-3000 remi-delmas-3000 commented Mar 16, 2022

goto-instrument now errors out when attempting to enforce or replace the contract of an unknown function.

replace_calls and enforce_contracts are now checking that functions exist upfront and are failing fast by throwing exceptions from util/exception_utils.h, and are now returning void instead of a boolean error flag.

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@codecov
Copy link

codecov bot commented Mar 16, 2022

Codecov Report

Merging #6733 (69eded9) into develop (56f317a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           develop    #6733   +/-   ##
========================================
  Coverage    76.86%   76.86%           
========================================
  Files         1589     1589           
  Lines       183813   183809    -4     
========================================
+ Hits        141289   141293    +4     
+ Misses       42524    42516    -8     
Impacted Files Coverage Δ
src/goto-instrument/contracts/contracts.h 100.00% <ø> (ø)
src/goto-instrument/contracts/contracts.cpp 93.69% <100.00%> (+1.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6ca79bf...69eded9. Read the comment docs.

Copy link
Contributor

@SaswatPadhi SaswatPadhi left a comment

Choose a reason for hiding this comment

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

LGTM; couple of minor comments below

@@ -0,0 +1,5 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] empty line

Suggested change

@@ -0,0 +1,12 @@
CORE
Copy link
Contributor

@SaswatPadhi SaswatPadhi Mar 17, 2022

Choose a reason for hiding this comment

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

[minor] just to reduce code duplication, we can place this .desc file in enforce-unknown-function as well.

Concretely, we could:

  1. rename enforce-unknown-function -> function-contracts-unknown-function
  2. rename function-contracts-unknown-function/test.desc -> test_enforce.desc
  3. move this test.desc -> function-contracts-unknown-function/test_replace.desc

--enforce-contract goo
^Invariant check failed$
^Condition: Precondition$
^Reason: all_functions_found\(to_enforce\)$
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also test for the message from PRECONDITION_WITH_DIAGNOSTICS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now throwing an exception instead of using a precondition. The guidelines for error handling are: handle incorrect user input gracefully, throwing informative exceptions from util/exception_utils.h, use INVARIANT and PRECONDITION for internal checks that shall never fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good

Copy link
Collaborator

@feliperodri feliperodri left a comment

Choose a reason for hiding this comment

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

Just minor comments on top of @SaswatPadhi suggestions.

@@ -1150,8 +1150,18 @@ void goto_instrument_parse_optionst::instrument_goto_program()
// first replacement then enforcement. We rely on contract replacement
// and inlining of sub-function calls to properly annotate all
// assignments.
contracts.replace_calls(to_replace);
contracts.enforce_contracts(to_enforce);
if(contracts.replace_calls(to_replace))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to keep these if statements here or just modify replace_calls and enforce methods to throw 0 whenever the precondition fail? Is there any case where we actually return false from these methods and use these if statement here? In any case, we could move them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we are now throwing exceptions for incorrect user input I changed the signatures to return void, and got rid of these checks.

@remi-delmas-3000 remi-delmas-3000 force-pushed the contracts-replace-unknown-fix branch 2 times, most recently from 2372e1e to d411e97 Compare March 17, 2022 19:49
We dropped the pattern of returning boolean error flags and accumulating
errors in `replace_calls` and `enforce_contracts`.
We are now checking that all functions exist upfront and
throwing exceptions from `util/exception_utils.h` as soon as the first
unknown function is found.
Subsequent checks of the same conditions are now INVARIANTS.
@remi-delmas-3000 remi-delmas-3000 force-pushed the contracts-replace-unknown-fix branch from d411e97 to e6402ec Compare March 17, 2022 19:49
Copy link
Collaborator

@feliperodri feliperodri left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks, Remi!

@feliperodri feliperodri added bugfix aws Bugs or features of importance to AWS CBMC users Code Contracts Function and loop contracts labels Mar 18, 2022
@remi-delmas-3000 remi-delmas-3000 merged commit 627faee into diffblue:develop Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws Bugs or features of importance to AWS CBMC users bugfix Code Contracts Function and loop contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants