<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 3, 2016, at 2:54 PM, McDowell, Raymond C. via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">I am trying to construct a constraint that says 0 <= Offset <= Size, for two SVals Offset and Size. Here’s what I did:<o:p class=""></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> ASTContext &ACtx = ChCtx.getASTContext();<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> SValBuilder &SVBldr = St->getStateManager().getSValBuilder();<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> SVal Zero = SVBldr.makeZeroVal(ACtx.getSizeType());<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> SVal OffsetIsNonNeg = SVBldr.evalBinOpNN(St, BO_LE, Zero.castAs<NonLoc>(), Offset.castAs<NonLoc>(), ACtx.BoolTy);<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> SVal OffsetIsWithinSize = SVBldr.evalBinOpNN(St, BO_LE, Offset.castAs<NonLoc>(), BuffSize.castAs<NonLoc>(), ACtx.BoolTy);<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 10pt; font-family: 'Lucida Console';" class=""> SVal OffsetIsWithinBounds = SVBldr.evalBinOpNN(St, BO_LAnd, OffsetIsNonNeg.castAs<NonLoc>(), OffsetIsWithinSize.castAs<NonLoc>(), ACtx.BoolTy);<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">When I run this code, it fails with an error: Assertion `false && “Invalid Opcode.”’ failed. This is coming from evalAPSInt in BasicValueFactory.cpp. This function contains a comment that says “Land, LOr, Comma are handled specially by higher-level logic.” Land does not seem to be handled by higher-level logic in this case. I intend to work around this by asserting these as two separate constraints instead of combining them into one. But this looks like a bug to me, so thought I should raise the issue here.</div></div></div></blockquote><div><br class=""></div><div>Logical And and Logical Or have consequences for control flow (they are short-circuiting) and so are represented in the control flow graph. You can see how they are evaluated in ExprEngine::VisitLogicalExpr(). This is the “higher-level logic” referred to in the comment.</div><div><br class=""></div><div>As you noted, you can apply a constraint that is a logical conjunction of OffsetIsNonNeg and OffsetIsWithinSize by using ConstraintManager::assume() to first apply the assumption that OffsetIsNonNeg is true to the current state. Then you can call assume() again on the resultant state to add the second assumption that OffsetIsWithinSize is true.</div><div><br class=""></div><div>This also reflects an overall design decision in the analyzer to keep constraints relatively simple so they can be reasoned about very quickly. Rather than track complex, logical symbolic constraints within a single state, the analyzer will eagerly split into multiple states — each of which has simpler, range-based constraints.</div><div><br class=""></div><div>For example, if you have:</div><div><br class=""></div><div>void foo(int a, int b, int c) {</div><div> assert((a == 1 || b == 2) || c == 3);</div><div> // (*)</div><div>}</div><div><br class=""></div><div>Rather than produce a single state at (*) with the complex constraint “ ((‘$a’ equals 1 OR ‘$b’ equals ‘2’) OR ‘$c’ equals ‘3’)“ it will produce three separate states: </div><div><br class=""></div><div>1) </div><div>$a: [1,1]</div><div><br class=""></div><div>2)</div><div>$a: [INT_MIN, 0], [2,INT_MAX]</div><div>$b: [2,2]</div><div><br class=""></div><div>3)</div><div><div>$a: [INT_MIN, 0], [2,INT_MAX]</div><div>$b: [INT_MIN, 1], [3,INT_MAX]</div><div>$c: [3,3]</div><div><br class=""></div></div><div>The analyzer will then explore the path from these three states separately. In effect the analyzer has moved the disjunction for the complex constraint out to the state level.</div><div><br class=""></div><div>This approach keeps the constraint manager simple and *very* fast, at the cost of eager state splits and imprecision when reasoning about relationships. In particular, the analyzer currently has no way to reason about symbolic constraints involving relationships between unknowns like "a <= b < c” — which would very useful for things like array-bounds checking!</div><div><br class=""></div><div>Ryan Govostes and Bhargava Shastry have been working on improving the analyzer’s reasoning about symbolic relational constraints with an SMT solver (STP, in this case). You can see their work at <<a href="http://reviews.llvm.org/D15609" class="">http://reviews.llvm.org/D15609</a>>.</div><div><br class=""></div><div>Devin</div><div><br class=""></div></div></div></body></html>