Skip to content

goto instrumentation for decreases clauses on loops #6197

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 1 commit into from
Jul 2, 2021

Conversation

LongPham7
Copy link

@LongPham7 LongPham7 commented Jun 24, 2021

This pull request contains the full-fledged implementation of one-dimensional loop variants (also known as ranking functions). A loop variant is an integer expression (i) that is bounded below and (ii) strictly decreases after each iteration of a loop. We use loop variants to prove termination of loops.

A loop variant in C is converted to the following statements in the GOTO language:

  1. Declarations of two temporary variables
  2. Assign a loop variant to the first temporary variable at the "beginning" of a loop body
  3. Assign a loop variant to the second temporary variable at the "end" of a loop body
  4. Assert that the second temporary variable < first temporary variable
  5. Discard the two temporary variables by the "dead" statements in the GOTO language.
  • 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.

@LongPham7 LongPham7 force-pushed the Long_internship branch 2 times, most recently from 1422b22 to a0ba201 Compare June 24, 2021 17:54
@codecov
Copy link

codecov bot commented Jun 24, 2021

Codecov Report

Merging #6197 (fa9bb89) into develop (0002950) will increase coverage by 7.50%.
The diff coverage is n/a.

❗ Current head fa9bb89 differs from pull request most recent head 00608b6. Consider uploading reports for the commit 00608b6 to get more accurate results
Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #6197      +/-   ##
===========================================
+ Coverage    67.40%   74.91%   +7.50%     
===========================================
  Files         1157     1456     +299     
  Lines        95236   161233   +65997     
===========================================
+ Hits         64197   120792   +56595     
- Misses       31039    40441    +9402     
Flag Coverage Δ
cproversmt2 ?
regression ?
unit ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/util/string_container.cpp 52.94% <0.00%> (-47.06%) ⬇️
src/solvers/prop/prop.cpp 42.85% <0.00%> (-41.76%) ⬇️
src/solvers/flattening/boolbv_member.cpp 53.65% <0.00%> (-38.65%) ⬇️
src/cpp/cpp_storage_spec.cpp 65.00% <0.00%> (-35.00%) ⬇️
src/util/cmdline.h 66.66% <0.00%> (-33.34%) ⬇️
src/solvers/strings/array_pool.h 66.66% <0.00%> (-33.34%) ⬇️
src/solvers/strings/string_refinement.h 66.66% <0.00%> (-33.34%) ⬇️
...rs/strings/string_concatenation_builtin_function.h 0.00% <0.00%> (-33.34%) ⬇️
src/cbmc/c_test_input_generator.cpp 60.00% <0.00%> (-30.33%) ⬇️
src/ansi-c/c_typecheck_base.cpp 54.39% <0.00%> (-26.51%) ⬇️
... and 1435 more

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 a7d4c3d...00608b6. 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.

Some initial comments below, and could you please:

  • fix the formatting issues? (see the build failures for details)
  • change the message Compare the old and new variant values to Check that loop variant monotonically decreases?

@SaswatPadhi SaswatPadhi added aws Bugs or features of importance to AWS CBMC users Code Contracts Function and loop contracts enhancement labels Jun 24, 2021
@SaswatPadhi SaswatPadhi requested a review from feliperodri June 24, 2021 23:05
@LongPham7 LongPham7 force-pushed the Long_internship branch 6 times, most recently from 6aa3322 to 002c618 Compare June 25, 2021 15:21
@LongPham7 LongPham7 force-pushed the Long_internship branch 2 times, most recently from 57a9915 to 0a227fb Compare June 25, 2021 22:22
Copy link
Collaborator

@martin-cs martin-cs left a comment

Choose a reason for hiding this comment

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

Looks good apart from the things flagged.

// Generate: an assignment to store the loop variant before the loop entry
code_assignt old_variant_assignment{old_variant_symbol, variant};
old_variant_assignment.add_source_location() = loop_head->source_location;
converter.goto_convert(old_variant_assignment, havoc_code, mode);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tautschnig is this use of goto_convert fitting with how and when / where it is supposed to be used?

@LongPham7 LongPham7 force-pushed the Long_internship branch 8 times, most recently from 9c7f67b to 28aff8e Compare June 29, 2021 15:23
@LongPham7 LongPham7 force-pushed the Long_internship branch 2 times, most recently from c86f3f4 to 17e3e0a Compare June 30, 2021 15:24
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.

Couple of minor comments, but otherwise LGTM! Thanks for all the work!

@SaswatPadhi SaswatPadhi changed the title Full implementation of loop variants (aka ranking functions) goto instrumentation for decreases clauses on loops Jun 30, 2021
@LongPham7 LongPham7 force-pushed the Long_internship branch 3 times, most recently from 008519a to 99540f5 Compare June 30, 2021 16:10
@SaswatPadhi SaswatPadhi marked this pull request as ready for review June 30, 2021 16:34
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.

Thank you for tackling all comments!

loop_head->source_location;
converter.goto_convert(monotonic_decreasing_assertion, havoc_code, mode);
havoc_code.instructions.back().source_location.set_comment(
"Check monotonicity of decreases clause");
Copy link
Member

Choose a reason for hiding this comment

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

"monotonicity" is perhaps not the right word. It might not be an intuitive description for a broader audience, and being pedantic, you need strict monotonicity to get termination.
How about "ranking function decreases"?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think "ranking function" might also sound unfamiliar, plus we haven't used ranking function elsewhere (the annotation just reads __CPROVER_decreases).

How about simply, "Check decreases clause on loop iteration"?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks a lot for both of your suggestions. I will change the comment to "Check decreases clause on loop iteration".

@kroening
Copy link
Member

kroening commented Jul 2, 2021

What should happen when there are multiple __CPROVER_decreases clauses for one loop?
You could make that a lexicographic ranking function.

@SaswatPadhi
Copy link
Contributor

SaswatPadhi commented Jul 2, 2021

What should happen when there are multiple __CPROVER_decreases clauses for one loop?
You could make that a lexicographic ranking function.

Yes, this is a good point. Currently we don't allow multiple invariant and decreases annotations, but my proposal was also to treat multiple decreases clause annotations as tuples that would be compared lexicographically.

@SaswatPadhi SaswatPadhi merged commit 3e0007f into diffblue:develop Jul 2, 2021
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 enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants