[PATCH] Fix crash when resolving branch conditions for temporary destructor condition blocks.

Manuel Klimek klimek at google.com
Mon May 5 06:38:33 PDT 2014


On Mon, May 5, 2014 at 2:55 PM, Manuel Klimek <klimek at google.com> wrote:

> On Thu, May 1, 2014 at 6:27 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>
>>
>> On Apr 30, 2014, at 10:23 , Manuel Klimek <klimek at google.com> wrote:
>>
>> On Wed, Apr 30, 2014 at 7:16 PM, Jordan Rose <jordan_rose at apple.com>wrote:
>>
>>>
>>> On Apr 30, 2014, at 1:35 , Manuel Klimek <klimek at google.com> wrote:
>>>
>>> On Wed, Apr 30, 2014 at 5:34 AM, Jordan Rose <jordan_rose at apple.com>wrote:
>>>
>>>> What this is basically saying is that we will never be in a situation
>>>> where the condition (or the rightmost branch in the condition) was not
>>>> evaluated in the current CFG block. That seems a bit questionable in
>>>> general but reasonable given the way the CFG is currently constructed.
>>>
>>>
>>> Well, all I've been trying to do here is to a) document and b) encode in
>>> the assert the actual current CFG invariant the analyzer relies on for
>>> correctness.
>>> The problem is that if what is returned from ResolveCondition is *not*
>>> evaluated in the block, we have no guarantee that it was evaluated at all -
>>> but the correctness of all the path analyses relies on it being in the SVal
>>> cache of the state. Otherwise we will assume branches are reachable that
>>> are clearly unreachable because of which parts of the logical operator tree
>>> we *assumed* to be true (so we know the condition must be statically
>>> knowable in the current state).
>>>
>>> I have a hard time putting that into words though, so any help for how
>>> to wordsmith this to be easier to understand would be highly appreciated...
>>>
>>>
>>> "in the SVal cache of the state" is "in the Environment" using analyzer
>>> terminology (it's not really a cache, since it's not reproducible)
>>>
>>
>> exactly :) I learned that today while doing more digging through the
>> code...
>>
>>
>>> , but since things regularly get cleaned *out* of the Environment there
>>> aren't that many cases where a condition would be present but wouldn't have
>>> been evaluated in the previous block.
>>>
>>
>> I don't yet understand how exactly things get cleaned out of the
>> environment (other than losing scope).
>> It seems to me like a lot of the warnings rely on branch conditions
>> always being in the environment at the point of the terminator, otherwise
>> it's easy to produce false positives in unreachable code paths.
>>
>>
>> The presence of bindings in the Environment is controlled by the
>> LiveVariables analysis. Essentially, an expression is live from the point
>> of its evaluation to the point it is consumed.
>>
>
> Can you elaborate on what "consumed" means in this context?
>
>
>>  Unfortunately, it can be a little more complicated than that
>> sometimes...especially with LiveVariables being a flow-sensitive analysis
>> but not a path-sensitive one.
>>
>
> What is the difference of flow vs. path sensitive analysis here?
>
>
>>  (This is one of the reasons for the liveness bug that Pavel uncovered.)
>>
>
> I'm not sure yet where there was a liveness bug - every instance of bug
> I've seen was due to expression not having yet been evaluated (due to the
> logical operator optimization that ResolveCondition relies on). I've not
> seen a case yet where an expression was removed from the Environment after
> it had been in. Do you have an example for that?
>

To go into a little more details. Take this code:
void f() {
  struct Dtor {
    ~Dtor();
    operator bool() const;
  };
  extern bool coin();
  if (coin() || coin() || Dtor()) {
  }
}

It currently leads to this CFG:
void f()
 [B8 (ENTRY)]
   Succs (1): B7

 [B1]
   Preds (1): B2
   Succs (1): B0

 [B2]
   T: if [B4.1]
   Preds (2): B3 B4
   Succs (2): B1 B0

 [B3]
   1: ~Dtor() (Temporary object destructor)
   Preds (1): B4
   Succs (1): B2

 [B4]
   1: [B7.3] || [B6.3] || [B5.6]
   T: (Temp Dtor) [B7.3] || [B6.3] || ...

^^^^ This is the interesting part: The expression B7.3 || B6.3 has never
been executed. The terminator B6.T, which references the same expression,
only works because in that case ResolveCondition sees "oh, we're in a
logical operator branch, so we already know we only need to look at B6.3".
B6.3 has been executed and is in the environment, so everything is fine.

The problem is that we cannot just use the same shortcut logic to decide
which temp destructors to execute - we actually need an evaluation of B7.3
|| B6.3 to decide whether we need to go into B3 (because the temporary
destructors are called in reverse order). B7.3 || B6.3 is never executed
though, so it's not in the environment, so we assume both branches are
always possible, even if it shouldn't be possible in the current
Environment.

My current theory is that putting the condition statement that's needed to
decide the branch in as statement right before the temp dtor terminator
would fix this (but, alas, I don't fully understand this yet ;). In this
case, it would be:
[B4]
1: [B7.3] || [B6.3] || [B5.6]
2: [B7.3] || [B6.3]
T: (Temp Dtor) [B7.3] || [B6.3]
which would put B4.2 into the environment, so we would know its value in
B4.T.

On a tangent, B4.1 is also strangely placed. If you look at B2, you see
that the block is empty and only has a teminator (the if). The condition of
the if is not in the same block. Today, this only works with
ResolveCondition, because I can't think of a way to create this kind of CFG
without the if() having a ExprWithCleanups as its first child, thus exiting
early in ResolveCondition.

   Preds (3): B5 B6 B7
   Succs (2): B2 B3

 [B5]
   1: Dtor() (CXXConstructExpr, struct Dtor)
   2: [B5.1] (BindTemporary)
   3: [B5.2] (ImplicitCastExpr, NoOp, const struct Dtor)
   4: [B5.3].operator bool
   5: [B5.3]
   6: [B5.5] (ImplicitCastExpr, UserDefinedConversion, _Bool)
   Preds (1): B6
   Succs (1): B4

 [B6]
   1: coin
   2: [B6.1] (ImplicitCastExpr, FunctionToPointerDecay, _Bool (*)(void))
   3: [B6.2]()
   T: [B7.3] || [B6.3] || ...
   Preds (1): B7
   Succs (2): B4 B5

 [B7]
   1: coin
   2: [B7.1] (ImplicitCastExpr, FunctionToPointerDecay, _Bool (*)(void))
   3: [B7.2]()
   T: [B7.3] || ...
   Preds (1): B8
   Succs (2): B4 B6

 [B0 (EXIT)]
   Preds (2): B1 B2


>
> Please do commit the change. Thanks for taking the time to work through it!
>>
>
> Done. Thx for the review and taking the time to answer all my questions! :D
>
> Cheers,
> /Manuel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140505/1b05a075/attachment.html>


More information about the cfe-commits mailing list