Repository navigation
DFCC write summaries: do not miss writes - #9562
Draft
tautschnig wants to merge 4 commits into
Draft
tautschnig wants to merge 4 commits into
tautschnig wants to merge 4 commits into
Conversation
The View section sorted the entries of a view by their irep_idt keys, whose operator< compares the indices of the strings in the string container rather than the strings. The expected order thus depended on the order in which the unit test binary first created the strings "i" to "l", and the test failed once the new write-summary unit tests, which are linked before it, created "l" first. Sort by the key names instead. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
diffblue#9392 added about 560 lines of write-summary analysis to an anonymous namespace in dfcc_infer_loop_assigns.cpp. There, nothing outside the loop assigns inference can reach the analysis, so it can only be tested end to end, and the merged regression test passes whether or not any body is replaced by a summary. Most branches of the analysis were not run by any test. Move `originst`, `write_summaryt`, `write_summariest` and `write_summary_bodiest` to dfcc_write_summaries.{h,cpp}, with the class declarations in the header. The code is moved unchanged, apart from splitting the member functions into declarations and out-of-class definitions, turning the static helpers of `write_summary_bodiest` into file-local functions, and the following simplifications: - `write_summariest::parameter_index` was written but never read. - `write_summaryt::operator==` compared the members of `originst` one by one; `originst` now has its own `operator==`, so a member added later cannot be left out of the comparison. - `has_pointers` assumed "no pointers" for a type while visiting it, "for recursive types". That entry could never be read: a type can only refer to itself through a pointer, and the pointer case does not recurse. - In `make_body`, a parameter index out of range cannot happen, as the indices come from the function's own `parameter_identifiers`. Make it an INVARIANT instead of a silent fallback. - `else if(has_pointers(t) || unknown) { if(has_pointers(t)) return false; }` is `else if(has_pointers(t)) return false;`. - Fix a typo in a comment ("boyd"). There is no change in behaviour. Add unit tests that check the computed summaries (writes to globals, through parameters, through locals pointing to a parameter's object, of callees, of recursive functions, of array operations and `havoc_object`; what results may point to; and the cases in which the summary is unknown), and that `write_summary_bodiest` replaces only the bodies of functions DFCC does not instrument, keeps the bodies of functions with unknown summaries, and restores the bodies. Also add a regression test that fails if the inference inlines the original body: `__CPROVER_havoc_slice` is implemented with `__CPROVER_array_replace`, which the inference does not see after inlining, whereas the summary of a function that calls it writes the whole object its parameter points to. Before diffblue#9392, the loop in this test was not havocked and its assertion wrongly held. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
`write_summary_bodiest` took every function that DFCC does not instrument and that has a body as a root of the write-summary analysis. That set includes `__CPROVER__start`, from which the analysis then reached `main` and every function of the program, and the inference constructs a `write_summary_bodiest` once for each function with loops. On a program with 300 functions that each have one loop, all called from `main`, `goto-instrument --dfcc main --apply-loop-contracts` took 5.5 s instead of 2.4 s before diffblue#9392. Only the functions that the copy being inlined may call, directly or through the functions it calls, can be inlined into it, so take only those as roots. The same program now takes 2.5 s. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
The summaries that diffblue#9392 introduced replace the bodies of functions that DFCC does not instrument, so that a write the summary misses can make the loop assigns inference leave out an object that the loop changes, which may prove assertions that do not hold. Two kinds of writes were missed. The analysis handled the OTHER instructions `array_set`, `array_copy`, `array_replace` and `havoc_object`, and took every other OTHER instruction to write nothing. `array_equal`, however, assigns its third operand (see goto_rw.cpp and symex_other.cpp), and statements such as `cpp_delete` write to memory as well. Record the write of `array_equal`, list the statements that write nothing (those that symex ignores or that only read), and make the summary unknown, so that the function keeps its body, for any other statement. The analysis only tracks the origins of values whose type contains pointers: the origins of `(uintptr_t)q` are empty. That is sound as long as a value without pointers never becomes a pointer other than through a cast, which the analysis treats as pointing anywhere. Memory can, however, be read as a different type than it was written: union { int *p; uintptr_t l; } u; u.l = (uintptr_t)q; *u.p = 2; The analysis found that `u.p` points nowhere, so the summary of this function said it writes nothing, rather than that its writes are unknown, and its body was replaced by an empty one. The same happens for `*(uintptr_t *)&p = (uintptr_t)q; *p = 2;`, and for a pointer read from an integer variable through `*(int **)&l`. Today this is masked because the loop assigns inference also drops such writes when it inlines the body (diffblue#9393), but the summaries are meant to be conservative, and they should not depend on that. Treat the contents of an object as unknown when part of it is written as another type (through a union member, byte_extract or typecast lvalue of an object with pointers), and the contents of the locals a pointer points to as unknown when the pointer is cast to a pointer to another type (other than void). Values built with union literals, byte updates, `with` on unions, or byte_extract as another type are unknown as well. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
3 of 4 tasks
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #9562 +/- ##
=========================================
Coverage 81.28% 81.28%
=========================================
Files 1725 1728 +3
Lines 191618 191838 +220
Branches 73 73
=========================================
+ Hits 155751 155942 +191
- Misses 35867 35896 +29 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends-on: #9561
The summaries that #9392 introduced replace the bodies of functions
that DFCC does not instrument, so that a write the summary misses can
make the loop assigns inference leave out an object that the loop
changes, which may prove assertions that do not hold. Two kinds of
writes were missed.
The analysis handled the OTHER instructions
array_set,array_copy,array_replaceandhavoc_object, and took every other OTHERinstruction to write nothing.
array_equal, however, assigns its thirdoperand (see goto_rw.cpp and symex_other.cpp), and statements such as
cpp_deletewrite to memory as well. Record the write ofarray_equal, list the statements that write nothing (those that symexignores or that only read), and make the summary unknown, so that the
function keeps its body, for any other statement.
The analysis only tracks the origins of values whose type contains
pointers: the origins of
(uintptr_t)qare empty. That is sound aslong as a value without pointers never becomes a pointer other than
through a cast, which the analysis treats as pointing anywhere. Memory
can, however, be read as a different type than it was written:
union { int *p; uintptr_t l; } u;
u.l = (uintptr_t)q;
*u.p = 2;
The analysis found that
u.ppoints nowhere, so the summary of thisfunction said it writes nothing, rather than that its writes are
unknown, and its body was replaced by an empty one. The same happens
for
*(uintptr_t *)&p = (uintptr_t)q; *p = 2;, and for a pointer readfrom an integer variable through
*(int **)&l. Today this is maskedbecause the loop assigns inference also drops such writes when it
inlines the body (#9393), but the summaries are meant to be
conservative, and they should not depend on that. Treat the contents of
an object as unknown when part of it is written as another type
(through a union member, byte_extract or typecast lvalue of an object
with pointers), and the contents of the locals a pointer points to as
unknown when the pointer is cast to a pointer to another type (other
than void). Values built with union literals, byte updates,
withonunions, or byte_extract as another type are unknown as well.
Co-authored-by: Kiro kiro-agent@users.noreply.github.com