19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/ADT/ImmutableSet.h" 21 #include "llvm/Support/raw_ostream.h" 23 using namespace clang;
26 void RangeSet::IntersectInRange(BasicValueFactory &BV, Factory &F,
27 const llvm::APSInt &Lower,
const llvm::APSInt &Upper,
28 PrimRangeSet &newRanges, PrimRangeSet::iterator &
i,
29 PrimRangeSet::iterator &e)
const {
39 if (i->To() < Lower) {
42 if (i->From() > Upper) {
46 if (i->Includes(Lower)) {
47 if (i->Includes(Upper)) {
49 F.add(newRanges, Range(BV.getValue(Lower), BV.getValue(Upper)));
52 newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
54 if (i->Includes(Upper)) {
55 newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
58 newRanges = F.add(newRanges, *i);
63 const llvm::APSInt &RangeSet::getMinValue()
const {
65 return ranges.begin()->From();
68 bool RangeSet::pin(llvm::APSInt &Lower, llvm::APSInt &Upper)
const {
74 APSIntType
Type(getMinValue());
88 Lower =
Type.getMinValue();
89 Upper =
Type.getMaxValue();
93 Lower =
Type.getMinValue();
98 Lower =
Type.getMinValue();
99 Upper =
Type.getMaxValue();
108 Upper =
Type.getMaxValue();
118 Upper =
Type.getMaxValue();
129 Lower =
Type.getMinValue();
139 Lower =
Type.getMinValue();
140 Upper =
Type.getMaxValue();
157 llvm::APSInt Lower, llvm::APSInt Upper)
const {
158 if (!pin(Lower, Upper))
159 return F.getEmptySet();
161 PrimRangeSet newRanges = F.getEmptySet();
163 PrimRangeSet::iterator i =
begin(), e =
end();
165 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
170 IntersectInRange(BV, F, BV.
getMinValue(Upper), Upper, newRanges,
i, e);
171 IntersectInRange(BV, F, Lower, BV.
getMaxValue(Lower), newRanges,
i, e);
181 PrimRangeSet newRanges = F.getEmptySet();
186 newRanges = F.add(newRanges, *j);
197 PrimRangeSet newRanges = F.getEmptySet();
200 const llvm::APSInt &from = i->From(), &to = i->To();
201 const llvm::APSInt &newTo = (from.isMinSignedValue() ?
203 BV.getValue(- from));
204 if (to.isMaxSignedValue() && !newRanges.isEmpty() &&
205 newRanges.begin()->From().isMinSignedValue()) {
206 assert(newRanges.begin()->To().isMinSignedValue() &&
207 "Ranges should not overlap");
208 assert(!from.isMinSignedValue() &&
"Ranges should not overlap");
209 const llvm::APSInt &newFrom = newRanges.
begin()->From();
211 F.add(F.remove(newRanges, *newRanges.begin()),
Range(newFrom, newTo));
212 }
else if (!to.isMinSignedValue()) {
213 const llvm::APSInt &newFrom = BV.getValue(- to);
214 newRanges = F.add(newRanges,
Range(newFrom, newTo));
216 if (from.isMinSignedValue()) {
234 os <<
'[' << i->From().toString(10) <<
", " << i->To().toString(10)
255 bool canReasonAbout(
SVal X)
const override;
265 void printJson(raw_ostream &Out,
ProgramStateRef State,
const char *NL =
"\n",
266 unsigned int Space = 0,
bool IsDot =
false)
const override;
273 const llvm::APSInt &
V,
274 const llvm::APSInt &Adjustment)
override;
277 const llvm::APSInt &V,
278 const llvm::APSInt &Adjustment)
override;
281 const llvm::APSInt &V,
282 const llvm::APSInt &Adjustment)
override;
285 const llvm::APSInt &V,
286 const llvm::APSInt &Adjustment)
override;
289 const llvm::APSInt &V,
290 const llvm::APSInt &Adjustment)
override;
293 const llvm::APSInt &V,
294 const llvm::APSInt &Adjustment)
override;
298 const llvm::APSInt &To,
const llvm::APSInt &Adjustment)
override;
302 const llvm::APSInt &To,
const llvm::APSInt &Adjustment)
override;
312 const llvm::APSInt &Int,
313 const llvm::APSInt &Adjustment);
315 const llvm::APSInt &Int,
316 const llvm::APSInt &Adjustment);
318 const llvm::APSInt &Int,
319 const llvm::APSInt &Adjustment);
321 const llvm::APSInt &Int,
322 const llvm::APSInt &Adjustment);
324 const llvm::APSInt &Int,
325 const llvm::APSInt &Adjustment);
331 std::unique_ptr<ConstraintManager>
333 return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.
getSValBuilder());
336 bool RangeConstraintManager::canReasonAbout(
SVal X)
const {
338 if (SymVal && SymVal->isExpression()) {
339 const SymExpr *SE = SymVal->getSymbol();
341 if (
const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
342 switch (SIE->getOpcode()) {
362 if (
const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
406 const llvm::APSInt *RangeConstraintManager::getSymVal(
ProgramStateRef St,
409 return T ? T->getConcreteValue() :
nullptr;
417 bool Changed =
false;
419 ConstraintRangeTy::Factory &CRFactory = State->get_context<
ConstraintRange>();
421 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
423 if (SymReaper.
isDead(Sym)) {
425 CR = CRFactory.remove(CR, Sym);
434 BasicValueFactory &BV,
438 APSIntType IntType = BV.getAPSIntType(Sym->getType());
439 return Domain.Intersect(BV, F, ++IntType.getZeroValue(),
440 --IntType.getZeroValue());
452 BasicValueFactory &BV,
455 const SymIntExpr* SIE) {
458 const llvm::APSInt &RHS = SIE->getRHS();
459 const llvm::APSInt &Zero = BV.getAPSIntType(T).getZeroValue();
463 if (Operator == BO_Or && IsUnsigned)
464 return Input.Intersect(BV, F, RHS, BV.getMaxValue(T));
467 if (Operator == BO_Or && RHS != Zero)
473 if (Operator == BO_And && (IsUnsigned || RHS >= Zero))
474 return Input.Intersect(BV, F, BV.getMinValue(T), RHS);
481 ConstraintRangeTy::data_type *
V = State->get<ConstraintRange>(Sym);
485 BasicValueFactory &BV = getBasicVals();
486 const RangeSet *R = getRangeForMinusSymbol(State, Sym);
492 return V->Intersect(BV, F, R->Negate(BV, F));
496 return R->Negate(BV, F);
502 RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
509 if (
const SymIntExpr* SIE = dyn_cast<SymIntExpr>(Sym))
523 if (
const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
524 if (SSE->getOpcode() == BO_Sub) {
526 SymbolManager &SymMgr = State->getSymbolManager();
527 SymbolRef negSym = SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub,
529 if (
const RangeSet *negV = State->get<ConstraintRange>(negSym)) {
531 if ((negV->getConcreteValue() &&
532 (*negV->getConcreteValue() == 0)) ||
555 const llvm::APSInt &Int,
556 const llvm::APSInt &Adjustment) {
558 APSIntType AdjustmentType(Adjustment);
562 llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
563 llvm::APSInt Upper = Lower;
569 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
570 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
575 const llvm::APSInt &Int,
576 const llvm::APSInt &Adjustment) {
578 APSIntType AdjustmentType(Adjustment);
583 llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
584 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
585 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
590 const llvm::APSInt &Int,
591 const llvm::APSInt &Adjustment) {
593 APSIntType AdjustmentType(Adjustment);
594 switch (AdjustmentType.testInRange(Int,
true)) {
596 return F.getEmptySet();
600 return getRange(St, Sym);
604 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
605 llvm::APSInt Min = AdjustmentType.getMinValue();
606 if (ComparisonVal == Min)
607 return F.getEmptySet();
609 llvm::APSInt Lower = Min - Adjustment;
610 llvm::APSInt Upper = ComparisonVal - Adjustment;
613 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
618 const llvm::APSInt &Int,
619 const llvm::APSInt &Adjustment) {
620 RangeSet New = getSymLTRange(St, Sym, Int, Adjustment);
621 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
626 const llvm::APSInt &Int,
627 const llvm::APSInt &Adjustment) {
629 APSIntType AdjustmentType(Adjustment);
630 switch (AdjustmentType.testInRange(Int,
true)) {
632 return getRange(St, Sym);
636 return F.getEmptySet();
640 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
641 llvm::APSInt Max = AdjustmentType.getMaxValue();
642 if (ComparisonVal == Max)
643 return F.getEmptySet();
645 llvm::APSInt Lower = ComparisonVal - Adjustment;
646 llvm::APSInt Upper = Max - Adjustment;
649 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
654 const llvm::APSInt &Int,
655 const llvm::APSInt &Adjustment) {
656 RangeSet New = getSymGTRange(St, Sym, Int, Adjustment);
657 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
662 const llvm::APSInt &Int,
663 const llvm::APSInt &Adjustment) {
665 APSIntType AdjustmentType(Adjustment);
666 switch (AdjustmentType.testInRange(Int,
true)) {
668 return getRange(St, Sym);
672 return F.getEmptySet();
676 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
677 llvm::APSInt Min = AdjustmentType.getMinValue();
678 if (ComparisonVal == Min)
679 return getRange(St, Sym);
681 llvm::APSInt Max = AdjustmentType.getMaxValue();
682 llvm::APSInt Lower = ComparisonVal - Adjustment;
683 llvm::APSInt Upper = Max - Adjustment;
685 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
690 const llvm::APSInt &Int,
691 const llvm::APSInt &Adjustment) {
692 RangeSet New = getSymGERange(St, Sym, Int, Adjustment);
693 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
696 RangeSet RangeConstraintManager::getSymLERange(
698 const llvm::APSInt &Int,
699 const llvm::APSInt &Adjustment) {
701 APSIntType AdjustmentType(Adjustment);
702 switch (AdjustmentType.testInRange(Int,
true)) {
704 return F.getEmptySet();
712 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
713 llvm::APSInt Max = AdjustmentType.getMaxValue();
714 if (ComparisonVal == Max)
717 llvm::APSInt Min = AdjustmentType.getMinValue();
718 llvm::APSInt Lower = Min - Adjustment;
719 llvm::APSInt Upper = ComparisonVal - Adjustment;
721 return RS().Intersect(getBasicVals(), F, Lower, Upper);
726 const llvm::APSInt &Int,
727 const llvm::APSInt &Adjustment) {
728 return getSymLERange([&] {
return getRange(St, Sym); }, Int, Adjustment);
733 const llvm::APSInt &Int,
734 const llvm::APSInt &Adjustment) {
735 RangeSet New = getSymLERange(St, Sym, Int, Adjustment);
736 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
741 const llvm::APSInt &To,
const llvm::APSInt &Adjustment) {
742 RangeSet New = getSymGERange(State, Sym, From, Adjustment);
745 RangeSet Out = getSymLERange([&] {
return New; }, To, Adjustment);
746 return Out.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, Out);
749 ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange(
751 const llvm::APSInt &To,
const llvm::APSInt &Adjustment) {
752 RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment);
753 RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment);
754 RangeSet New(RangeLT.addRange(F, RangeGT));
755 return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New);
762 void RangeConstraintManager::printJson(raw_ostream &Out,
ProgramStateRef State,
763 const char *NL,
unsigned int Space,
767 Indent(Out, Space, IsDot) <<
"\"constraints\": ";
768 if (Constraints.isEmpty()) {
769 Out <<
"null," << NL;
775 for (ConstraintRangeTy::iterator I = Constraints.begin();
776 I != Constraints.end(); ++I) {
778 <<
"{ \"symbol\": \"" << I.getKey() <<
"\", \"range\": \"";
779 I.getData().print(Out);
782 if (std::next(I) != Constraints.end())
788 Indent(Out, Space, IsDot) <<
"]," << NL;
A (possibly-)qualified type.
Value is less than the minimum representable value.
bool isDead(SymbolRef sym)
Returns whether or not a symbol has been confirmed dead.
const SymExpr * SymbolRef
std::unique_ptr< ConstraintManager > CreateRangeConstraintManager(ProgramStateManager &statemgr, SubEngine *subengine)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The base class of the type hierarchy.
A Range represents the closed range [from, to].
llvm::ImmutableMap< SymbolRef, RangeSet > ConstraintRangeTy
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
RangeSet contains a set of ranges.
bool isEqualityOp() const
bool isReferenceType() const
SValBuilder & getSValBuilder()
RangeSet Negate(BasicValueFactory &BV, Factory &F) const
static bool isLocType(QualType T)
PrimRangeSet::Factory Factory
Value is representable using this type.
A record of the "type" of an APSInt, used for conversions.
Represents a symbolic expression like 'x' + 3.
bool isRelationalOp() const
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
virtual QualType getType() const =0
void print(raw_ostream &os) const
const llvm::APSInt * getConcreteValue() const
getConcreteValue - If a symbol is contrained to equal a specific integer constant then this method re...
static RangeSet assumeNonZero(BasicValueFactory &BV, RangeSet::Factory &F, SymbolRef Sym, RangeSet Domain)
Return a range set subtracting zero from Domain.
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
A class responsible for cleaning up unused symbols.
Value is greater than the maximum representable value.
RangeTestResultKind
Used to classify whether a value is representable using this type.
Dataflow Directional Tag Classes.
RangeSet(PrimRangeSet RS)
Represents symbolic expression that isn't a location.
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
RangeSet Intersect(BasicValueFactory &BV, Factory &F, llvm::APSInt Lower, llvm::APSInt Upper) const
PrimRangeSet::iterator iterator
static RangeSet applyBitwiseConstraints(BasicValueFactory &BV, RangeSet::Factory &F, RangeSet Input, const SymIntExpr *SIE)
Apply implicit constraints for bitwise OR- and AND-.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Represents a symbolic expression like 'x' + 'y'.