<br><br><div class="gmail_quote">On Wed, Jun 9, 2010 at 7:14 AM, Ted Kremenek <span dir="ltr"><<a href="mailto:kremenek@apple.com">kremenek@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Response inline!<br>
<div class="im"><br>
On Jun 6, 2010, at 9:41 PM, Jordy Rose wrote:<br>
<br>
> On Sat, 5 Jun 2010 23:43:42 -0700, Ted Kremenek <<a href="mailto:kremenek@apple.com">kremenek@apple.com</a>><br>
> wrote:<br>
>> On Jun 5, 2010, at 6:01 PM, Jordy Rose wrote:<br>
>>> public:<br>
>>>  enum OverflowKind {<br>
>>>    NoOverflow = 0,<br>
>>>    AdditionOverflow = 1,<br>
>>>    AdditionUnderflow = -AdditionOverflow<br>
>>>  };<br>
>><br>
>> Is there a reason we want to make AdditionUnderflow negative?  Why not<br>
>> just make it 2?  From the algorithm in SimpleConstraintManager, this<br>
> looks<br>
>> intended.  If so, please comment.<br>
><br>
> Yes; the idea is that a negative overflow followed by a positive overflow<br>
> is no overflow at all, and so the two should cancel out. This occurs in<br>
> situations like these:<br>
><br>
> void doSomething (unsigned x, unsigned length) {<br>
>  // pretend this is a branch being Assumed true<br>
>  assert(length == 0);<br>
>  --x;<br>
>  if (x+1 <= length) {<br>
>    // actually do work<br>
>  }<br>
> }<br>
><br>
> Here, the condition reduces to "(($x-1)+1) <= 0", which has an<br>
> intermediate step of "($x-1) <= -1".<br>
<br>
</div>Okay, makes sense!<br>
<div><div></div><div class="h5"><br>
>> @@ -316,8 +304,18 @@<br>
>>             assert(symIntExpr->getType(ValMgr.getContext()) ==<br>
> resultTy);<br>
>>             return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,<br>
>>                                      symIntExpr->getRHS(), resultTy);<br>
>> +          }<br>
>>         }<br>
>>       }<br>
>> +<br>
>> +      // for now, only handle ($sym op constant) here as well<br>
>><br>
>> Minor nit: Please keep this comment in the LLVM coding style (make it a<br>
>> sentence).  It's also probably worth pointing out what cases these<br>
> handle,<br>
>> and why Mul, Div, and friends are handled here.  Brief comments like<br>
> this<br>
>> are helpful, especially when the logic is broken up so recursively.<br>
> Also,<br>
>> can we really handle 'Mul', 'Div', 'Shl', etc., in this case?  In<br>
>> SimpleConstraintManager::canReasonAbout, we still return false for these<br>
>> cases for SymIntExpr.<br>
><br>
> I wasn't reading the section quite right at first, but actually it seems<br>
> correct. There are two things going on here:<br>
><br>
> - !(expr) is represented as (expr) == 0. For comparison ops, we can<br>
> simplify the expression by negating the comparison and returning that. The<br>
> original code only allowed this and returned UnknownVal otherwise,<br>
> disallowing things like ($a*3 == 0)<br>
> - The new code also allows "(expr) op constant". Previously, this would<br>
> have been UnknownVal(). Here, it's a SymExprVal that probably still can't<br>
> be evaluated. It's less efficent, but there's no harm done.<br>
<br>
</div></div>Okay, got it.<br>
<br>
But don't we still have the problem that 'canReasonAbout' is not accurate? (e.g., for the opcodes not handled)<br>
<div class="im"><br>
><br>
> It's worth noting that $sym*3 is not currently considered an UnknownVal;<br>
> the case for a SymbolVal LHS and ConcreteInt RHS also just makes a<br>
> SymExprVal.<br>
<br>
</div>Right.<br>
<div class="im"><br>
> Do you think it's worth checking the expression type in the SymbolVal<br>
> case? That would let the UnknownVal bubble up and probably save a bit of<br>
> work later, but it'd be more stuff to keep in sync with the constraint<br>
> manager.<br>
<br>
</div>Are you specifically referring to removing the following code?<br>
<br>
      // Does the symbol simplify to a constant?  If so, "fold" the constant<br>
      // by setting 'lhs' to a ConcreteInt and try again.<br>
      if (Sym->getType(ValMgr.getContext())->isIntegerType())<br>
        if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {<br>
          // The symbol evaluates to a constant. If necessary, promote the<br>
          // folded constant (LHS) to the result type.<br>
<div class="im"><br>
><br>
> I'm going back to clean up this section. Wondering if I can actually push<br>
> all this simplification into SimpleSValuator -- it's all arithmetic stuff<br>
> anyway. Going to try tonight.<br>
<br>
</div>Yes; keeping this all in SimpleSValuator makes the most sense to me, and probably would be more efficient.<br>
<div class="im"><br>
><br>
><br>
>> +      const llvm::APSInt *RHS = &SE->getRHS();<br>
>> +      int TotalOverflow = 0;<br>
>><br>
>> Should TotalOverflow by an APInt, or a uint64_t?  Seems like this could<br>
>> overflow itself in some cases.<br>
><br>
> TotalOverflow is not measuring a quantity, but the net number of<br>
> overflows, where positive overflows cancel out negative overflows.<br>
> Commented and renamed to NetOverflow in my local version.<br>
><br>
> John brought up an important point, though: "(x+1) <= 0" has a solution at<br>
> x = UINT_MAX, since unsigned overflow is defined in C and C++, and people<br>
> are used to signed wraparound as well.<br>
<br>
</div>Do they depend on it?  If not, and if it is truly undefined for most compilers, we should eventually check this case and issue a warning.<br>
<div class="im"><br>
> Worse, "(x+1) <= 1" has two<br>
> solutions: x = UINT_MAX and x = 0. How can we handle these wraparound<br>
> cases?<br>
<br>
</div>This requires modeling a disjunction over the values of x.  We could extend the ConstraintManager API to allow one to specify such constraints without bifurcating the state.  RangeConstraintManager, for example, should be able to model this case.<br>

<div class="im"><br>
><br>
> Until that's resolved, I'm not sure it's a good idea to commit, though<br>
> leaving it as is (pretending wraparound doesn't exist) might be the least<br>
> troublesome mechanism for now.<br>
<br>
</div>What you have seems monotonically better than what we have now, and the general approach seems okay to me.  When we consider doing precise integer overflow checking, we will need to be more precise.  For now, just add a big note to the code documenting the caveats/assumptions about the semantics.<br>

<br>
Our requirements with the static analyzer are different than the compiler.  For the compiler we need conservative semantics.  For the analyzer we would like that too, but can slide on that when it benefits us to find more bugs/less false positives.<br>

<br>
Zhongxing: What do you think?<br></blockquote><div><br>The tricky part is this: <br><br>C1 + C2 > MAX => MAX - C1 < C2 <br></div></div><br>and <br><br>C1 + C2 < MIN => MIN - C1 > C2<br><br>Since we don't have general infrastructure for handling overflow, I think this is reasonable for now. But I'd like it to be kept as local as possible.<br>
<br>And I think it's possible to completely remove it if we are just going to fix the bug.<br>