Skip to content

[pulse] model std::condition_variable waits with a predicate - #2195

Open
VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pulse-condition-variable
Open

VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pulse-condition-variable

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

#include <condition_variable>
#include <mutex>

struct Job { int id; };

int wait_ok(std::mutex& m, std::condition_variable& cv, Job*& job) {
  std::unique_lock<std::mutex> lock(m);
  job = nullptr;
  cv.wait(lock, [&] { return job != nullptr; });
  return job->id; // false positive: null dereference reported here
}

Model these overloads of std::condition_variable and std::condition_variable_any, including
the std::stop_token ones, as one unrolling of while (!pred()) wait(lock);: havoc what the
captures reach, then assume pred() (wait) or return it (the others).

Limitations:

  • For [this] predicates, the wait havocs *this but cannot assume or return pred(), as Pulse
    does not apply summaries of [this] lambdas.
  • A predicate that holds on entry adds a path condition, as a call testing the state would, so
    later bugs on that state can become latent.

Test plan

New tests in cpp/pulse-17/condition_variable.cpp and cpp/pulse-20/condition_variable.cpp,
with FP_/FN_ tests for the remaining limitations; without this change, Pulse reports false
positives in most of their _ok cases. The codetoanalyze tests pass.

@meta-cla meta-cla Bot added the CLA Signed label Oct 2, 2026
@VladimirMakaev
VladimirMakaev force-pushed the pulse-condition-variable branch 2 times, most recently from 509c686 to 750b0d1 Compare October 5, 2026 06:54
@VladimirMakaev VladimirMakaev changed the title [DRAFT] [pulse] model std::condition_variable waits with a predicate [pulse] model std::condition_variable waits with a predicate Oct 5, 2026
@VladimirMakaev
VladimirMakaev marked this pull request as ready for review October 5, 2026 15:27
VladimirMakaev added a commit to VladimirMakaev/infer that referenced this pull request Oct 5, 2026
`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
@dulmarod

dulmarod commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

@jvillard can you look at this one?

@VladimirMakaev
VladimirMakaev force-pushed the pulse-condition-variable branch 2 times, most recently from df16adc to aa4ee3c Compare October 7, 2026 15:28

@jvillard jvillard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks ok to me, although the code seems over-complicated in places. I assume the test results make sense.

Comment thread infer/src/pulse/PulseModelsCpp.ml Outdated
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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this seems a little strange and a lot of code:

  • it havocs both the closure closure and its address pred for some reason (line 483)
  • it adds Allocation attributes, not sure why, and does it in two steps instead of one
  • adding UnknownEffect attributes seems unnecessary: we just want to havoc cells so that the predicate doesn't generate a contradiction because values might have changed

@VladimirMakaev
VladimirMakaev force-pushed the pulse-condition-variable branch from aa4ee3c to 123fea1 Compare November 28, 2026 00:17
@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

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.

Comment thread infer/src/pulse/PulseModelsCpp.ml Outdated
Comment on lines +523 to +540
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.
@VladimirMakaev
VladimirMakaev force-pushed the pulse-condition-variable branch from 123fea1 to 65680e8 Compare December 2, 2026 12:30

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants