Repository navigation
[pulse] model std::condition_variable waits with a predicate - #2195
VladimirMakaev wants to merge 1 commit into
Conversation
509c686 to
750b0d1
Compare
`cv.wait(lock, pred)` and the predicate overloads of `wait_for`/`wait_until` are untranslated header templates. As unknown calls taking the predicate by value, they neither assume `pred()` nor havoc what it reads, so Pulse reports false positives after waits. Upstream-PR: meta#2195 PR-Head: 750b0d1 PR-Base: 9cc2641
|
@jvillard can you look at this one? |
df16adc to
aa4ee3c
Compare
jvillard
left a comment
There was a problem hiding this comment.
Looks ok to me, although the code seems over-complicated in places. I assume the test results make sense.
| captured object points to it, directly or not: that object may own it, and the havoc drops the | ||
| reference. [this] is captured by reference, as the address of the [this] variable, which | ||
| cannot be re-bound: we start from the object it points to and keep the variable. *) | ||
| let havoc_predicate_state ~desc hist pred astate = |
There was a problem hiding this comment.
this seems a little strange and a lot of code:
- it havocs both the closure
closureand its addresspredfor some reason (line 483) - it adds
Allocationattributes, not sure why, and does it in two steps instead of one - adding
UnknownEffectattributes seems unnecessary: we just want to havoc cells so that the predicate doesn't generate a contradiction because values might have changed
aa4ee3c to
123fea1
Compare
|
Thanks, I've removed the double closure/address havoc, allocation restoration, and UnknownEffect attributes. The helper is longer because captured-this scalar fields are now materialized explicitly, where UnknownEffect previously covered them. Without that, a callee summary leaves fields known only to its caller unchanged; call_wait_for_job_ok exposes this. The full C++17 and C++20 Pulse suites still pass with unchanged expectations. I'm happy to shorten the model if you prefer a different trade-off here. |
| let cells = AbductiveDomain.reachable_addresses_from (Stdlib.List.to_seq roots) astate `Post in | ||
| let havoc_filter addr _ _ = not (AbstractValue.Set.mem addr kept) in | ||
| AbstractValue.Set.fold | ||
| (fun cell state -> | ||
| if AbstractValue.Set.mem cell kept then state | ||
| else | ||
| match Memory.find_edge_opt cell Dereference astate with | ||
| | None -> | ||
| state | ||
| | Some (old, _) -> | ||
| let state = AbductiveDomain.apply_unknown_effect ~havoc_filter hist old state in | ||
| let state = | ||
| Memory.add_edge path (cell, hist) Dereference | ||
| (AbstractValue.mk_fresh (), hist) | ||
| location state | ||
| in | ||
| AddressAttributes.initialize cell state ) | ||
| cells astate |
There was a problem hiding this comment.
this is strange: we compute the reachable addresses from roots, then for each of them call apply_unknown_effect, which also applies on the reachable addresses from its argument. I think this is just AbductiveDomain.apply_unknown_effect ~havoc_filter hist root astate for each root?
There was a problem hiding this comment.
You're right that collecting the reachable cells first was redundant. A bare apply_unknown_effect ~havoc_filter hist root per root also forgets the allocation of the root itself, and that loses pointer_captured_by_value_leak_bad: the closure still holds its copy of ready. The model now applies it to each edge target of a root instead and gives the root's own contents a fresh value, without changing the domain API. The filter keeps only dereference edges so that field addresses stay put, which keeps a pointer to a nested field such as &o.flags.ready aliasing it. On this branch the C++17 and C++20 Pulse suites give the same reports as before, traces included, and no expected-output file changes. Removing the captured-this materialization still adds a null dereference in call_wait_for_job_ok, so that part stays.
The predicate overloads of wait, wait_for and wait_until are untranslated header templates. As unknown calls taking the predicate by value, they neither assume the predicate nor account for the state that can change while the lock is released. Evaluate the predicate and overwrite mutable captured state on the wake-up path, keeping capture bindings stable. Materialize captured-this scalar fields so callee summaries also update values known only to the caller. Tested both C++17 and C++20 Pulse regression suites with unchanged expectations, plus direct and interprocedural mutable-pointer checks and a no-havoc control.
123fea1 to
65680e8
Compare
cv.wait(lock, pred)and the predicate overloads ofwait_for/wait_untilare untranslatedheader templates. As unknown calls taking the predicate by value, they neither assume
pred()norhavoc what it reads, so Pulse reports false positives after waits.
Model these overloads of
std::condition_variableandstd::condition_variable_any, includingthe
std::stop_tokenones, as one unrolling ofwhile (!pred()) wait(lock);: havoc what thecaptures reach, then assume
pred()(wait) or return it (the others).Limitations:
[this]predicates, the wait havocs*thisbut cannot assume or returnpred(), as Pulsedoes not apply summaries of
[this]lambdas.later bugs on that state can become latent.
Test plan
New tests in
cpp/pulse-17/condition_variable.cppandcpp/pulse-20/condition_variable.cpp,with
FP_/FN_tests for the remaining limitations; without this change, Pulse reports falsepositives in most of their
_okcases. The codetoanalyze tests pass.