[clang] [analyzer] Regularize conjured sym for return area of entrypoint (PR #244444)

Donát Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 20 06:01:20 PST 2026


NagyDonat wrote:

_(Replying to your big inline comment https://github.com/llvm/llvm-project/pull/244444#discussion_r4641641605 here because this is _the_ discussion of this PR.)_

> I think I need a bit more context. I read the PR summary like 2 times, and the tests too.

Thank you for investing so much effort into this review!

> So I think ultimately, the current implementation is right and changing it would be a mistake.

Do I understand correctly that you mean "current implementation" = before the changes in this PR?

> I feel like there is something missing here.

I think what I failed to emphasize is that **this is essentially an NFCI change**. I do not promise functional improvements, instead I'm arguing that "this PR does not change the behavior of the analyzer in a meaningful way, and it removes the only exception from a widely held invariant".

In the rest of this post I try to detail (1) what is touched my change (2) why is the status quo inelegant and (3) why is my change NFC. (I'm sorry for the length of this post, but this is a very complicated corner of the analyzer...)

-----

## Clarifying the limited scope of my change

> So `SF` was passed into this function. And I suppose, `SF` was essentially the top-most frame of the StackFrames - thus the analysis entry point.

In general this function is recursively calling itself with different `SF` values because it is trying to find the "final destination" of the construction process. When the analyzer sees the full picture, this will be a concrete variable, or other concrete location – and not a symbolic region.

As the recursive calls check the current stack frame, its parent, the parent of its parent etc. it tries to locate the non-symbolic target region in that frame – and the analyzer only introduce a symbolic region as a fallback in the case when the construction process "breaks the fourth wall" and goes outside of the entry point function.

> That was done because ideally, it would be the imaginary stack frame of the caller of the entry point [...]

Ideally we wouldn't have a conjured symbol at all, instead we would have a concrete variable etc. in the stack frame of the caller of the entry point, or in the stack frame of the caller of the caller of the entry point etc.

------

## Role of the `StackFrame` in the `SymbolConjured` – why is the status quo inelegant?

Note that when a `SymbolConjured` identifies a symbolic memory region, its `StackFrame` data member does not imply that this symbolic memory region is contained in that stack frame. (In fact, it is very rare – although not impossible – that a symbolic memory region with a conjured symbol is on the stack, and it is even more rare that the analyzer realizes or assumes this relationship.)

Instead, the purpose of this `StackFrame` member is that it contextualizes the other two members (the `ConstCFGElementRef` and the `unsigned` visit count) an, together with those two other members, uniquely identifies the analysis step when the symbol was conjured:
- The `ConstCFGElementRef` specifies the `Stmt *` (or something like that) which was analyzed when the symbol was conjured. (I'd say that this is the most specific part of the profile.)
- The `StackFrame` specifies the stack frame whose CFG contains the referenced CFG element, and is an important disambiguating factor when the stack contains multiple recursive calls of the same function. (Without recursion, the `Stmt *` or other similar AST node would – at least usually – distinguish symbols conjured in different stack frames.)
- The `unsigned` visit count is the number of times the analysis has visited the CFG block containing the CFG element where the symbol was conjured. This distinguishes symbols that were conjured at the same statement in the same stack frame, but within different iterations of a loop (including `goto`-based looping).

Note that `ExprEngine` calculates the visit count as
```c++
unsigned getNumVisited(const StackFrame *SF, const CFGBlock *Block) const { 
  return Engine.WList->getBlockCounter().getNumVisited(SF,                  
                                                       Block->getBlockID());
}                                                                           

```
– this takes both a `StackFrame` and a `CFGBlock` as parameters. (Note that `ConstCFGElementRef` is a pair of a `CFGBlock` and an integer index within that block.)

Notice that this "conjure a memory region where the return value of the entry point will be constructed" call already uses `getCFGElementRef()` (a CFG element of the function invoked in the current stack frame) and `getNumVisitedCurrent()`, which is defined as `getNumVisited(getCurrStackFrame(), getCurrBlock())`, so it is inconsistent when it uses a stack frame `SF` that is distinct from `getCurrStackFrame()`.

------

## Behavior on your code example – why is my change essentially NFCI?

> Consider this scenario:
> https://godbolt.org/z/jKP7z3Gd4
> Here x is reused across loop iterations, thus its address is fixed. Sometimes its' NRVO initialized from inner sometimes from middle, and I think we should differentiate them - don't we?

In your example code `main()` is present, so the analyzer sees the "final destination" of the construction process (the memory region of the local variable `Obj x` in `main()`), so it does not reach the "conjure a symbolic region where the return value of the entry point will be constructed" logic that I'm tweaking. (I don't have an opinion about the correctness in this case; but I think this is out of scope for this PR.)

If you omit `main()` from that code, then `entrypoint()` is analyzed as the entry point. The execution path is split several times, and the "conjure a symbolic region where the return value of the entry point will be constructed" logic is invoked on each path (in different moments) to conjure regions that will act as the "target" for each of the different construction processes.

This PR makes the `StackFrame` members of these conjured symbols distinct; but they already differ in their `ConstCFGElementRef` members, so this PR does not effect this scenario (your code minus `main()`).

Differentiating the `StackFrame`s of the conjured symbols is only significant when the CFG elements are already identical, i.e. the conjurations happen in different stack frames corresponding to the same recursive function (and the same statement in that function).

As the different "construction target area of the entry point" symbols appear on different execution paths, the only thing that their (in)equality can influence is whether the execution paths can be merged (assuming that all other things are equal between the two paths).

In a _very_ contrived corner case my patch could theoretically cause that two execution paths remain separate, consume analysis budget separately and therefore influence the result set – but this is absolutely negligible.

https://github.com/llvm/llvm-project/pull/244444


More information about the cfe-commits mailing list