Coverage Report

Created: 2020-11-24 06:42

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