xref: /openbsd-src/gnu/llvm/clang/lib/StaticAnalyzer/Core/RangedConstraintManager.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //== RangedConstraintManager.cpp --------------------------------*- C++ -*--==//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This file defines RangedConstraintManager, a class that provides a
10e5dd7070Spatrick //  range-based constraint manager interface.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick 
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
16e5dd7070Spatrick 
17e5dd7070Spatrick namespace clang {
18e5dd7070Spatrick 
19e5dd7070Spatrick namespace ento {
20e5dd7070Spatrick 
~RangedConstraintManager()21e5dd7070Spatrick RangedConstraintManager::~RangedConstraintManager() {}
22e5dd7070Spatrick 
assumeSym(ProgramStateRef State,SymbolRef Sym,bool Assumption)23e5dd7070Spatrick ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
24e5dd7070Spatrick                                                    SymbolRef Sym,
25e5dd7070Spatrick                                                    bool Assumption) {
26a9ac8606Spatrick   Sym = simplify(State, Sym);
27a9ac8606Spatrick 
28e5dd7070Spatrick   // Handle SymbolData.
29a9ac8606Spatrick   if (isa<SymbolData>(Sym))
30e5dd7070Spatrick     return assumeSymUnsupported(State, Sym, Assumption);
31e5dd7070Spatrick 
32e5dd7070Spatrick   // Handle symbolic expression.
33a9ac8606Spatrick   if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) {
34e5dd7070Spatrick     // We can only simplify expressions whose RHS is an integer.
35e5dd7070Spatrick 
36e5dd7070Spatrick     BinaryOperator::Opcode op = SIE->getOpcode();
37e5dd7070Spatrick     if (BinaryOperator::isComparisonOp(op) && op != BO_Cmp) {
38e5dd7070Spatrick       if (!Assumption)
39e5dd7070Spatrick         op = BinaryOperator::negateComparisonOp(op);
40e5dd7070Spatrick 
41e5dd7070Spatrick       return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS());
42e5dd7070Spatrick     }
43e5dd7070Spatrick 
44*12c85518Srobert     // Handle adjustment with non-comparison ops.
45*12c85518Srobert     const llvm::APSInt &Zero = getBasicVals().getValue(0, SIE->getType());
46*12c85518Srobert     return assumeSymRel(State, SIE, (Assumption ? BO_NE : BO_EQ), Zero);
47*12c85518Srobert   }
48*12c85518Srobert 
49*12c85518Srobert   if (const auto *SSE = dyn_cast<SymSymExpr>(Sym)) {
50a9ac8606Spatrick     BinaryOperator::Opcode Op = SSE->getOpcode();
51*12c85518Srobert     if (BinaryOperator::isComparisonOp(Op)) {
52a9ac8606Spatrick 
53a9ac8606Spatrick       // We convert equality operations for pointers only.
54a9ac8606Spatrick       if (Loc::isLocType(SSE->getLHS()->getType()) &&
55a9ac8606Spatrick           Loc::isLocType(SSE->getRHS()->getType())) {
56e5dd7070Spatrick         // Translate "a != b" to "(b - a) != 0".
57e5dd7070Spatrick         // We invert the order of the operands as a heuristic for how loop
58e5dd7070Spatrick         // conditions are usually written ("begin != end") as compared to length
59*12c85518Srobert         // calculations ("end - begin"). The more correct thing to do would be
60*12c85518Srobert         // to canonicalize "a - b" and "b - a", which would allow us to treat
61e5dd7070Spatrick         // "a != b" and "b != a" the same.
62e5dd7070Spatrick 
63a9ac8606Spatrick         SymbolManager &SymMgr = getSymbolManager();
64e5dd7070Spatrick         QualType DiffTy = SymMgr.getContext().getPointerDiffType();
65e5dd7070Spatrick         SymbolRef Subtraction =
66e5dd7070Spatrick             SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
67e5dd7070Spatrick 
68e5dd7070Spatrick         const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
69e5dd7070Spatrick         Op = BinaryOperator::reverseComparisonOp(Op);
70e5dd7070Spatrick         if (!Assumption)
71e5dd7070Spatrick           Op = BinaryOperator::negateComparisonOp(Op);
72e5dd7070Spatrick         return assumeSymRel(State, Subtraction, Op, Zero);
73e5dd7070Spatrick       }
74a9ac8606Spatrick 
75a9ac8606Spatrick       if (BinaryOperator::isEqualityOp(Op)) {
76a9ac8606Spatrick         SymbolManager &SymMgr = getSymbolManager();
77a9ac8606Spatrick 
78a9ac8606Spatrick         QualType ExprType = SSE->getType();
79a9ac8606Spatrick         SymbolRef CanonicalEquality =
80a9ac8606Spatrick             SymMgr.getSymSymExpr(SSE->getLHS(), BO_EQ, SSE->getRHS(), ExprType);
81a9ac8606Spatrick 
82a9ac8606Spatrick         bool WasEqual = SSE->getOpcode() == BO_EQ;
83a9ac8606Spatrick         bool IsExpectedEqual = WasEqual == Assumption;
84a9ac8606Spatrick 
85a9ac8606Spatrick         const llvm::APSInt &Zero = getBasicVals().getValue(0, ExprType);
86a9ac8606Spatrick 
87a9ac8606Spatrick         if (IsExpectedEqual) {
88a9ac8606Spatrick           return assumeSymNE(State, CanonicalEquality, Zero, Zero);
89a9ac8606Spatrick         }
90a9ac8606Spatrick 
91a9ac8606Spatrick         return assumeSymEQ(State, CanonicalEquality, Zero, Zero);
92a9ac8606Spatrick       }
93e5dd7070Spatrick     }
94*12c85518Srobert   }
95e5dd7070Spatrick 
96e5dd7070Spatrick   // If we get here, there's nothing else we can do but treat the symbol as
97e5dd7070Spatrick   // opaque.
98e5dd7070Spatrick   return assumeSymUnsupported(State, Sym, Assumption);
99e5dd7070Spatrick }
100e5dd7070Spatrick 
assumeSymInclusiveRange(ProgramStateRef State,SymbolRef Sym,const llvm::APSInt & From,const llvm::APSInt & To,bool InRange)101e5dd7070Spatrick ProgramStateRef RangedConstraintManager::assumeSymInclusiveRange(
102e5dd7070Spatrick     ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
103e5dd7070Spatrick     const llvm::APSInt &To, bool InRange) {
104a9ac8606Spatrick 
105a9ac8606Spatrick   Sym = simplify(State, Sym);
106a9ac8606Spatrick 
107e5dd7070Spatrick   // Get the type used for calculating wraparound.
108e5dd7070Spatrick   BasicValueFactory &BVF = getBasicVals();
109e5dd7070Spatrick   APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
110e5dd7070Spatrick 
111e5dd7070Spatrick   llvm::APSInt Adjustment = WraparoundType.getZeroValue();
112e5dd7070Spatrick   SymbolRef AdjustedSym = Sym;
113e5dd7070Spatrick   computeAdjustment(AdjustedSym, Adjustment);
114e5dd7070Spatrick 
115e5dd7070Spatrick   // Convert the right-hand side integer as necessary.
116e5dd7070Spatrick   APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
117e5dd7070Spatrick   llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
118e5dd7070Spatrick   llvm::APSInt ConvertedTo = ComparisonType.convert(To);
119e5dd7070Spatrick 
120e5dd7070Spatrick   // Prefer unsigned comparisons.
121e5dd7070Spatrick   if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
122e5dd7070Spatrick       ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
123e5dd7070Spatrick     Adjustment.setIsSigned(false);
124e5dd7070Spatrick 
125e5dd7070Spatrick   if (InRange)
126e5dd7070Spatrick     return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
127e5dd7070Spatrick                                          ConvertedTo, Adjustment);
128e5dd7070Spatrick   return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
129e5dd7070Spatrick                                         ConvertedTo, Adjustment);
130e5dd7070Spatrick }
131e5dd7070Spatrick 
132e5dd7070Spatrick ProgramStateRef
assumeSymUnsupported(ProgramStateRef State,SymbolRef Sym,bool Assumption)133e5dd7070Spatrick RangedConstraintManager::assumeSymUnsupported(ProgramStateRef State,
134e5dd7070Spatrick                                               SymbolRef Sym, bool Assumption) {
135a9ac8606Spatrick   Sym = simplify(State, Sym);
136a9ac8606Spatrick 
137e5dd7070Spatrick   BasicValueFactory &BVF = getBasicVals();
138e5dd7070Spatrick   QualType T = Sym->getType();
139e5dd7070Spatrick 
140e5dd7070Spatrick   // Non-integer types are not supported.
141e5dd7070Spatrick   if (!T->isIntegralOrEnumerationType())
142e5dd7070Spatrick     return State;
143e5dd7070Spatrick 
144e5dd7070Spatrick   // Reverse the operation and add directly to state.
145e5dd7070Spatrick   const llvm::APSInt &Zero = BVF.getValue(0, T);
146e5dd7070Spatrick   if (Assumption)
147e5dd7070Spatrick     return assumeSymNE(State, Sym, Zero, Zero);
148e5dd7070Spatrick   else
149e5dd7070Spatrick     return assumeSymEQ(State, Sym, Zero, Zero);
150e5dd7070Spatrick }
151e5dd7070Spatrick 
assumeSymRel(ProgramStateRef State,SymbolRef Sym,BinaryOperator::Opcode Op,const llvm::APSInt & Int)152e5dd7070Spatrick ProgramStateRef RangedConstraintManager::assumeSymRel(ProgramStateRef State,
153e5dd7070Spatrick                                                       SymbolRef Sym,
154e5dd7070Spatrick                                                       BinaryOperator::Opcode Op,
155e5dd7070Spatrick                                                       const llvm::APSInt &Int) {
156e5dd7070Spatrick   assert(BinaryOperator::isComparisonOp(Op) &&
157e5dd7070Spatrick          "Non-comparison ops should be rewritten as comparisons to zero.");
158e5dd7070Spatrick 
159e5dd7070Spatrick   // Simplification: translate an assume of a constraint of the form
160e5dd7070Spatrick   // "(exp comparison_op expr) != 0" to true into an assume of
161e5dd7070Spatrick   // "exp comparison_op expr" to true. (And similarly, an assume of the form
162e5dd7070Spatrick   // "(exp comparison_op expr) == 0" to true into an assume of
163e5dd7070Spatrick   // "exp comparison_op expr" to false.)
164e5dd7070Spatrick   if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) {
165e5dd7070Spatrick     if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
166e5dd7070Spatrick       if (BinaryOperator::isComparisonOp(SE->getOpcode()))
167e5dd7070Spatrick         return assumeSym(State, Sym, (Op == BO_NE ? true : false));
168e5dd7070Spatrick   }
169e5dd7070Spatrick 
170e5dd7070Spatrick   // Get the type used for calculating wraparound.
171e5dd7070Spatrick   BasicValueFactory &BVF = getBasicVals();
172e5dd7070Spatrick   APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
173e5dd7070Spatrick 
174e5dd7070Spatrick   // We only handle simple comparisons of the form "$sym == constant"
175e5dd7070Spatrick   // or "($sym+constant1) == constant2".
176e5dd7070Spatrick   // The adjustment is "constant1" in the above expression. It's used to
177e5dd7070Spatrick   // "slide" the solution range around for modular arithmetic. For example,
178e5dd7070Spatrick   // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
179e5dd7070Spatrick   // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
180e5dd7070Spatrick   // the subclasses of SimpleConstraintManager to handle the adjustment.
181e5dd7070Spatrick   llvm::APSInt Adjustment = WraparoundType.getZeroValue();
182e5dd7070Spatrick   computeAdjustment(Sym, Adjustment);
183e5dd7070Spatrick 
184e5dd7070Spatrick   // Convert the right-hand side integer as necessary.
185e5dd7070Spatrick   APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
186e5dd7070Spatrick   llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
187e5dd7070Spatrick 
188e5dd7070Spatrick   // Prefer unsigned comparisons.
189e5dd7070Spatrick   if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
190e5dd7070Spatrick       ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
191e5dd7070Spatrick     Adjustment.setIsSigned(false);
192e5dd7070Spatrick 
193e5dd7070Spatrick   switch (Op) {
194e5dd7070Spatrick   default:
195e5dd7070Spatrick     llvm_unreachable("invalid operation not caught by assertion above");
196e5dd7070Spatrick 
197e5dd7070Spatrick   case BO_EQ:
198e5dd7070Spatrick     return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
199e5dd7070Spatrick 
200e5dd7070Spatrick   case BO_NE:
201e5dd7070Spatrick     return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
202e5dd7070Spatrick 
203e5dd7070Spatrick   case BO_GT:
204e5dd7070Spatrick     return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
205e5dd7070Spatrick 
206e5dd7070Spatrick   case BO_GE:
207e5dd7070Spatrick     return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
208e5dd7070Spatrick 
209e5dd7070Spatrick   case BO_LT:
210e5dd7070Spatrick     return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
211e5dd7070Spatrick 
212e5dd7070Spatrick   case BO_LE:
213e5dd7070Spatrick     return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
214e5dd7070Spatrick   } // end switch
215e5dd7070Spatrick }
216e5dd7070Spatrick 
computeAdjustment(SymbolRef & Sym,llvm::APSInt & Adjustment)217e5dd7070Spatrick void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
218e5dd7070Spatrick                                                 llvm::APSInt &Adjustment) {
219e5dd7070Spatrick   // Is it a "($sym+constant1)" expression?
220e5dd7070Spatrick   if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
221e5dd7070Spatrick     BinaryOperator::Opcode Op = SE->getOpcode();
222e5dd7070Spatrick     if (Op == BO_Add || Op == BO_Sub) {
223e5dd7070Spatrick       Sym = SE->getLHS();
224e5dd7070Spatrick       Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
225e5dd7070Spatrick 
226e5dd7070Spatrick       // Don't forget to negate the adjustment if it's being subtracted.
227e5dd7070Spatrick       // This should happen /after/ promotion, in case the value being
228e5dd7070Spatrick       // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
229e5dd7070Spatrick       if (Op == BO_Sub)
230e5dd7070Spatrick         Adjustment = -Adjustment;
231e5dd7070Spatrick     }
232e5dd7070Spatrick   }
233e5dd7070Spatrick }
234e5dd7070Spatrick 
simplifyToSVal(ProgramStateRef State,SymbolRef Sym)235*12c85518Srobert SVal simplifyToSVal(ProgramStateRef State, SymbolRef Sym) {
236a9ac8606Spatrick   SValBuilder &SVB = State->getStateManager().getSValBuilder();
237*12c85518Srobert   return SVB.simplifySVal(State, SVB.makeSymbolVal(Sym));
238*12c85518Srobert }
239*12c85518Srobert 
simplify(ProgramStateRef State,SymbolRef Sym)240*12c85518Srobert SymbolRef simplify(ProgramStateRef State, SymbolRef Sym) {
241*12c85518Srobert   SVal SimplifiedVal = simplifyToSVal(State, Sym);
242a9ac8606Spatrick   if (SymbolRef SimplifiedSym = SimplifiedVal.getAsSymbol())
243a9ac8606Spatrick     return SimplifiedSym;
244a9ac8606Spatrick   return Sym;
245e5dd7070Spatrick }
246e5dd7070Spatrick 
247e5dd7070Spatrick } // end of namespace ento
248e5dd7070Spatrick } // end of namespace clang
249