Skip to content

CONTRACTS: Add goto-level loop-contract annotation #7788

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

Conversation

qinheping
Copy link
Collaborator

This PR allows us to parse and annotate loop contracts from JSON files to GOTO models.

We first parse the JSON file to get the configuration.

  • Function configurations consist of function id (regex) and a list of loop-contract configurations.
  • Loop-contract configurations consist of:
    • Loop number of the loop we are annotating to.
    • Loop invariants as strings.
    • (optional) Loop assigns as strings.
    • (optional) Loop decreases as strings.
    • (optional) A symbol map that map symbol names in the loop contracts strings to their name in the symbol table of the goto model.

Loop contracts mangling consists of four steps.

  1. Construct a fake function with a fake loop that contains the loop contracts for parsing the contracts.
  2. Parse the fake function and extract the contracts as exprt.
  3. Substitute symbols in the extracted exprt with the symbols in the symbol table of the goto model according to the symbol map provided by the user.
  4. Typecheck all contract exprt.
  5. Annotate all contract exprt to the corresponding loop.
  • 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 Jun 30, 2023

Codecov Report

Patch coverage: 92.10% and project coverage change: +0.07 🎉

Comparison is base (da48fba) 78.58% compared to head (b543536) 78.65%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #7788      +/-   ##
===========================================
+ Coverage    78.58%   78.65%   +0.07%     
===========================================
  Files         1693     1699       +6     
  Lines       193381   193857     +476     
===========================================
+ Hits        151962   152477     +515     
+ Misses       41419    41380      -39     
Impacted Files Coverage Δ
src/ansi-c/c_typecheck_base.h 100.00% <ø> (ø)
src/goto-instrument/contracts/contracts.h 100.00% <ø> (ø)
src/goto-instrument/contracts/utils.h 100.00% <ø> (ø)
...rc/goto-instrument/goto_instrument_parse_options.h 100.00% <ø> (ø)
...c/goto-instrument/contracts/contracts_wrangler.cpp 90.72% <90.72%> (ø)
src/goto-instrument/contracts/contracts_wrangler.h 100.00% <100.00%> (ø)
src/goto-instrument/contracts/utils.cpp 92.63% <100.00%> (+0.55%) ⬆️
.../goto-instrument/goto_instrument_parse_options.cpp 72.27% <100.00%> (+0.12%) ⬆️

... and 20 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch from 22ad623 to 47bccae Compare June 30, 2023 21:35
@feliperodri feliperodri added aws Bugs or features of importance to AWS CBMC users Code Contracts Function and loop contracts labels Jul 3, 2023
@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch 5 times, most recently from 8286ec0 to 188d439 Compare July 5, 2023 23:17
Copy link
Collaborator

@tautschnig tautschnig left a comment

Choose a reason for hiding this comment

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

Several minor issues.

@@ -0,0 +1,9 @@
CORE gcc-only
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you elaborate as to why this test is GCC-specific?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We use void pointer in this benchmark. Typechecking void pointer will trigger the error https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/c_typecheck_expr.cpp#L4152 under VISUAL_STUDIO.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we just use a char pointer instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, using char pointers will lead to a bug of DFCC.

156: File: D:\a\cbmc\cbmc\src\goto-instrument\contracts\dynamic-frames\dfcc_instrument.cpp:330 function: dfcc_instrumentt::instrument_function
156: Condition: Precondition
156: Reason: found != goto_model.goto_functions.function_map.end()
156: << EXTRA DIAGNOSTICS >>
156: Function '_errno' must exist in the model.
156: << END EXTRA DIAGNOSTICS >>

There is a function _errno in the symbol table but not in the function map. I will keep this test as gcc-only and open an issue to track this bug.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch 4 times, most recently from d84e62d to 84d83cc Compare July 10, 2023 13:14
@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch 2 times, most recently from ea4d440 to d7f4ba1 Compare July 12, 2023 21:02
@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch from d7f4ba1 to 6cc179a Compare July 12, 2023 21:04
This commit allows us to parse and annotate loop contracts from JSON files to GOTO models.

We first parse the JSON file to get the configuration.

    Function configurations consist of function id (regex) and a list of loop-contract configurations.
    Loop-contract configurations consist of:
        Loop number of the loop we are annotating to.
        Loop invariants as strings.
        (optional) Loop assigns as strings.
        (optional) Loop decreases as strings.
        (optional) A symbol map that map symbol names in the loop contracts strings to their name in the symbol table of the goto model.

Loop contracts mangling consists of four steps.

    Construct a fake function with a fake loop that contains the loop contracts for parsing the contracts.
    Parse the fake function and extract the contracts as exprt.
    Substitute symbols in the extracted exprt with the symbols in the symbol table of the goto model according to the symbol map provided by the user.
    Typecheck all contract exprt.
    Annotate all contract exprt to the corresponding loop.
@qinheping qinheping force-pushed the features/goto-level-loop-contracts branch from 6cc179a to b543536 Compare July 13, 2023 06:41
@qinheping qinheping enabled auto-merge July 13, 2023 06:43
@qinheping qinheping merged commit ba0c1bb into diffblue:develop Jul 13, 2023
@qinheping qinheping deleted the features/goto-level-loop-contracts branch October 18, 2023 21:58
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 Code Contracts Function and loop contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants