[cfe-dev] Handling of loops in the Clang Static Analyzer

Sean Eveson via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 27 03:18:36 PST 2017


Hi Venugopal,

> Sean Eveson (cc’d) did some initial work on loop widening to mitigate
this problem.

I started to work on this, but have unfortunately not had time to take the
next steps. There is a mode which does 'loop widening' which is off by
default as the state after the loop is completely cleared which can lead to
false positives. The idea is to improve on the widening, by changing it to
only clear state that might be affected by the loop.

To enable the loop widening pass: "-analyzer-config widen-loops=true".

> Maybe -analyzer-max-loop is interesting. It tells Clang analyzer how many
times you want to go through loops. The bigger value the better analysis,
but slower analysis.

An issue you will run in to when changing analyzer-max-loop is that it also
affects variable-bound loops. If you set the max loop higher to work around
your problem you will see some slow down for the concrete-bound loops, but
you will see a lot more for the variable-bound ones.

Example:
// Where the value of j is unknown.
for (i = 0; i < j; ++i) {
  // ...
}
foo(i);

As far as I remember, with a max loop of 1000 and no other limits on the
analyzer, `foo` will be called for all the values between 0..~1000. This is
because the analyzer will branch each time it reaches the loop condition.

Regards,
Sean Eveson

Sean Eveson
SN Systems - Sony Computer Entertainment Group

On Fri, Feb 24, 2017 at 7:54 PM, Devin Coughlin <dcoughlin at apple.com> wrote:

> Hi Venugopal,
>
> > On Feb 23, 2017, at 7:07 PM, Venugopal Raghavan via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
> >
> > Hi,
> >
> > I am re-sending the question I asked under a different thread so that
> the subject is more relevant to the topic.
> >
> > I did not quite realize it earlier but it seems that the static analyzer
> unrolls a loop up to a certain number of times and then stops exploring
> paths beyond that. In the checker I have written, I get the message "Block
> count exceeded" and then state exploration stops. As a result, my checker
> give false positives and does not achieve what it sets out to do.
>
> The analyzer makes no promises about exhaustivity and in many cases will
> drop flows and stop path exploration. If your analysis depends on full path
> exploration to prevent false positives (as compared to false negatives)
> then it is going to be an uphill battle to eliminate false positives.
>
> As you noted, for concrete-bound loops stopping path exploration can be
> particularly pernicious because after unrolling the loop N times the
> analyze simply stops. Any code dominated by the loop exit will simply not
> be explored, leading to false negatives.
>
> Sean Eveson (cc’d) did some initial work on loop widening to mitigate this
> problem. The basic idea there was rather than simply stopping, the analyzer
> would “forget” any specific information about a particular iteration
> through the loop and proceed analyzing after the loop without any
> assumptions about how many times the loop was unrolled. This would lose
> some precision but gain coverage for code after the loop. This feature not
> complete and is off by default, but you can see the beginnings of it at <
> https://reviews.llvm.org/D12358>
>
> Devin
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170227/e5230cf6/attachment.html>


More information about the cfe-dev mailing list