steakhal wrote:

I skimmed the PR and I'm not thrilled by the use of shared_ptrs. I know it's 
out of policy but I really don't have time right now - but I was reached out 
for my opinion. As such, I outlined my rough ideas to claude to implement and 
test it.

I have not looked at the results beyond skimming it and asked it to 
differential fuzz it against this PR, and an oracle of truth.
If you prefer, feel free to ignore my branch and what claude wants to say. I'm 
fine with that. This is what I had the time for - this also extends to other 
webkit checker reviews until January - mainly because of some internal work and 
the upcoming -Wtype-ownership warnings (aka. self-contained inference and 
checking @rniwa)

This is what claude has to say:

---

> @iidmsa @rniwa, thanks for the test, it pins down the problem nicely.
> I'd like to push back on the `std::shared_ptr` approach though, because I 
> think the problem has a much smaller fix.
> Here is what I mean, as a commit on top of main, with your test unchanged:
> https://github.com/steakhal/llvm-project/commit/c93f80191988
> 
> ## Why I'd rather not have `shared_ptr` here
> 
> `shared_ptr` is for shared ownership of something whose lifetime isn't known 
> in advance.
> There is no such lifetime problem here: everything lives in the cache, which 
> lives as long as the checker.
> What the PR really needs is a union-find to record "this result is only as 
> good as that pending one", and `shared_ptr` is used as the pointer type for 
> it.
> The cost is that every cache entry becomes a heap-allocated, ref-counted 
> node, plus a pointer chase on each lookup.
> That applies to every analyzed function and statement, even though only 
> mutually recursive ones ever get merged.
> It also changes the cache's type in the header for what is an implementation 
> detail of the visitor.
> 
> ## The root cause
> 
> The traversal itself was fine.
> Assuming a function that is still being analyzed is trivial gives the right 
> answer for the function being asked about.
> The bug is that results relying on that assumption were cached as if they 
> were facts:
> 
> ```c++
> void first(RefCounted* obj) { second(obj); unknownFunction(); }
> void second(RefCounted* obj) { first(obj); }
> ```
> 
> When querying `first`, `second` sees `first` in progress, assumes it is 
> trivial, finishes as trivial, and gets cached.
> Then `first` hits `unknownFunction()`.
> `second` was only ever trivial *if `first` is*, but its cache entry says 
> trivial unconditionally.
> 
> ## The fix: don't cache "trivial" until it's certain
> 
> ```c++
> // Treat what is still being analyzed as trivial until proven otherwise.
> size_t Mark = TentativelyTrivial.size();
> if (!TentativelyTrivial.insert(S))
>   return true;
> 
> if (!Function()) {
>   // Being non-trivial is final.
>   // What was found trivial since S was inserted might have relied on S.
>   while (TentativelyTrivial.size() > Mark)
>     TentativelyTrivial.pop_back();
>   Cache[S] = false;
>   return false;
> }
> 
> // The outermost one is trivial, so everything tentative turned out to be.
> if (Mark == 0) {
>   for (auto Key : TentativelyTrivial)
>     Cache[Key] = true;
>   TentativelyTrivial.clear();
> }
> return true;
> ```
> 
> `TentativelyTrivial` is a `SetVector` that replaces `RecursiveFn`.
> The cache stays a plain `bool` map, and the header doesn't change.
> 
> Why it's sound:
> - **"Non-trivial" is always final.**
>   The only assumption ever made is "trivial", so a `false` can only come from 
> something that really is non-trivial.
>   It can be cached immediately.
>   This also removes the need to mark everything on the stack non-trivial when 
> something fails; the frames that depend on the failure return `false` on 
> their own.
> - **"Trivial" is committed only once the outermost query succeeds.**
>   At that point every tentative entry was shown trivial assuming only that 
> other tentative entries are trivial.
>   That is exactly the greatest fixed point that "trivial until proven 
> otherwise" was always aiming for.
> - **The truncation on failure is exact.**
>   Only entries inserted after `S` can have relied on `S`, and those are the 
> ones dropped.
>   This only matters if a caller recovers from a nested failure, like trying 
> another candidate constructor.
>   Today that only happens at the top level, but it keeps the scheme sound 
> without relying on that.
> 
> This is the same SCC grouping that the PR builds with merged `shared_ptr`s, 
> except that here it falls out of insertion order.
> 
> ## Testing
> 
> - Your test fails on main (8 missing warnings) and passes with this change, 
> as do all 82 tests under `clang/test/Analysis/Checkers/WebKit`.
> - I also differentially fuzzed this change against this PR, with main as a 
> control, on 1500 random call graphs.
>   The graphs had cycles, self-calls, calls nested in `if`s, and shuffled 
> query and definition orders.
>   Each binary's warnings were compared with an independently computed ground 
> truth: a function is non-trivial iff it reaches a non-trivial call.
>   - main: 158 mismatches.
>   - This PR: 0 mismatches.
>   - My change: 0 mismatches.
> 
> So to be clear: I believe this PR is correct.
> My objection is to the complexity, not the results.
> 
> ## The trade-off: simpler, but more work
> 
> My change does more work than this PR, at the expense of being simpler.
> When the outermost query turns out non-trivial, every tentative trivial 
> result computed under it is discarded and recomputed by later queries.
> That includes results that never relied on anything in progress, which this 
> PR keeps.
> I haven't measured whether that matters on WebKit.
> If it does, a small refinement recovers the common case: cache an entry right 
> away if nothing in its analysis touched a tentative entry.
> Non-recursive helpers would then be cached immediately, and only results 
> involved in recursion would wait for the outermost query.
> I'd only add that if the numbers ask for it.
> 
> Feel free to take the commit into this PR as is, or let me know if you'd 
> prefer I open it separately.
> 


https://github.com/llvm/llvm-project/pull/242104
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to