Coverage Report

Created: 2020-02-25 14:32

/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
12.6k
RangedConstraintManager::~RangedConstraintManager() {}
22
23
ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
24
                                                   SymbolRef Sym,
25
177k
                                                   bool Assumption) {
26
177k
  // Handle SymbolData.
27
177k
  if (isa<SymbolData>(Sym)) {
28
3.98k
    return assumeSymUnsupported(State, Sym, Assumption);
29
3.98k
30
3.98k
    // Handle symbolic expression.
31
173k
  } else if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) {
32
172k
    // We can only simplify expressions whose RHS is an integer.
33
172k
34
172k
    BinaryOperator::Opcode op = SIE->getOpcode();
35
172k
    if (BinaryOperator::isComparisonOp(op) && 
op != BO_Cmp172k
) {
36
172k
      if (!Assumption)
37
83.9k
        op = BinaryOperator::negateComparisonOp(op);
38
172k
39
172k
      return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS());
40
172k
    }
41
693
42
693
  } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
43
693
    // Translate "a != b" to "(b - a) != 0".
44
693
    // We invert the order of the operands as a heuristic for how loop
45
693
    // conditions are usually written ("begin != end") as compared to length
46
693
    // calculations ("end - begin"). The more correct thing to do would be to
47
693
    // canonicalize "a - b" and "b - a", which would allow us to treat
48
693
    // "a != b" and "b != a" the same.
49
693
    SymbolManager &SymMgr = getSymbolManager();
50
693
    BinaryOperator::Opcode Op = SSE->getOpcode();
51
693
    assert(BinaryOperator::isComparisonOp(Op));
52
693
53
693
    // For now, we only support comparing pointers.
54
693
    if (Loc::isLocType(SSE->getLHS()->getType()) &&
55
693
        
Loc::isLocType(SSE->getRHS()->getType())685
) {
56
685
      QualType DiffTy = SymMgr.getContext().getPointerDiffType();
57
685
      SymbolRef Subtraction =
58
685
          SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
59
685
60
685
      const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
61
685
      Op = BinaryOperator::reverseComparisonOp(Op);
62
685
      if (!Assumption)
63
330
        Op = BinaryOperator::negateComparisonOp(Op);
64
685
      return assumeSymRel(State, Subtraction, Op, Zero);
65
685
    }
66
44
  }
67
44
68
44
  // If we get here, there's nothing else we can do but treat the symbol as
69
44
  // opaque.
70
44
  return assumeSymUnsupported(State, Sym, Assumption);
71
44
}
72
73
ProgramStateRef RangedConstraintManager::assumeSymInclusiveRange(
74
    ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
75
1.96k
    const llvm::APSInt &To, bool InRange) {
76
1.96k
  // Get the type used for calculating wraparound.
77
1.96k
  BasicValueFactory &BVF = getBasicVals();
78
1.96k
  APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
79
1.96k
80
1.96k
  llvm::APSInt Adjustment = WraparoundType.getZeroValue();
81
1.96k
  SymbolRef AdjustedSym = Sym;
82
1.96k
  computeAdjustment(AdjustedSym, Adjustment);
83
1.96k
84
1.96k
  // Convert the right-hand side integer as necessary.
85
1.96k
  APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
86
1.96k
  llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
87
1.96k
  llvm::APSInt ConvertedTo = ComparisonType.convert(To);
88
1.96k
89
1.96k
  // Prefer unsigned comparisons.
90
1.96k
  if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
91
1.96k
      
ComparisonType.isUnsigned()1.95k
&&
!WraparoundType.isUnsigned()116
)
92
0
    Adjustment.setIsSigned(false);
93
1.96k
94
1.96k
  if (InRange)
95
490
    return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
96
490
                                         ConvertedTo, Adjustment);
97
1.47k
  return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
98
1.47k
                                        ConvertedTo, Adjustment);
99
1.47k
}
100
101
ProgramStateRef
102
RangedConstraintManager::assumeSymUnsupported(ProgramStateRef State,
103
7.77k
                                              SymbolRef Sym, bool Assumption) {
104
7.77k
  BasicValueFactory &BVF = getBasicVals();
105
7.77k
  QualType T = Sym->getType();
106
7.77k
107
7.77k
  // Non-integer types are not supported.
108
7.77k
  if (!T->isIntegralOrEnumerationType())
109
4
    return State;
110
7.77k
111
7.77k
  // Reverse the operation and add directly to state.
112
7.77k
  const llvm::APSInt &Zero = BVF.getValue(0, T);
113
7.77k
  if (Assumption)
114
4.06k
    return assumeSymNE(State, Sym, Zero, Zero);
115
3.71k
  else
116
3.71k
    return assumeSymEQ(State, Sym, Zero, Zero);
117
7.77k
}
118
119
ProgramStateRef RangedConstraintManager::assumeSymRel(ProgramStateRef State,
120
                                                      SymbolRef Sym,
121
                                                      BinaryOperator::Opcode Op,
122
173k
                                                      const llvm::APSInt &Int) {
123
173k
  assert(BinaryOperator::isComparisonOp(Op) &&
124
173k
         "Non-comparison ops should be rewritten as comparisons to zero.");
125
173k
126
173k
  // Simplification: translate an assume of a constraint of the form
127
173k
  // "(exp comparison_op expr) != 0" to true into an assume of
128
173k
  // "exp comparison_op expr" to true. (And similarly, an assume of the form
129
173k
  // "(exp comparison_op expr) == 0" to true into an assume of
130
173k
  // "exp comparison_op expr" to false.)
131
173k
  if (Int == 0 && 
(151k
Op == BO_EQ151k
||
Op == BO_NE78.2k
)) {
132
148k
    if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
133
37.3k
      if (BinaryOperator::isComparisonOp(SE->getOpcode()))
134
161
        return assumeSym(State, Sym, (Op == BO_NE ? 
true83
:
false78
));
135
173k
  }
136
173k
137
173k
  // Get the type used for calculating wraparound.
138
173k
  BasicValueFactory &BVF = getBasicVals();
139
173k
  APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
140
173k
141
173k
  // We only handle simple comparisons of the form "$sym == constant"
142
173k
  // or "($sym+constant1) == constant2".
143
173k
  // The adjustment is "constant1" in the above expression. It's used to
144
173k
  // "slide" the solution range around for modular arithmetic. For example,
145
173k
  // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
146
173k
  // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
147
173k
  // the subclasses of SimpleConstraintManager to handle the adjustment.
148
173k
  llvm::APSInt Adjustment = WraparoundType.getZeroValue();
149
173k
  computeAdjustment(Sym, Adjustment);
150
173k
151
173k
  // Convert the right-hand side integer as necessary.
152
173k
  APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
153
173k
  llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
154
173k
155
173k
  // Prefer unsigned comparisons.
156
173k
  if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
157
173k
      
ComparisonType.isUnsigned()172k
&&
!WraparoundType.isUnsigned()107k
)
158
362
    Adjustment.setIsSigned(false);
159
173k
160
173k
  switch (Op) {
161
0
  default:
162
0
    llvm_unreachable("invalid operation not caught by assertion above");
163
0
164
76.0k
  case BO_EQ:
165
76.0k
    return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
166
0
167
77.0k
  case BO_NE:
168
77.0k
    return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
169
0
170
6.38k
  case BO_GT:
171
6.38k
    return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
172
0
173
3.44k
  case BO_GE:
174
3.44k
    return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
175
0
176
4.62k
  case BO_LT:
177
4.62k
    return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
178
0
179
5.87k
  case BO_LE:
180
5.87k
    return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
181
173k
  } // end switch
182
173k
}
183
184
void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
185
175k
                                                llvm::APSInt &Adjustment) {
186
175k
  // Is it a "($sym+constant1)" expression?
187
175k
  if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
188
37.1k
    BinaryOperator::Opcode Op = SE->getOpcode();
189
37.1k
    if (Op == BO_Add || 
Op == BO_Sub36.5k
) {
190
973
      Sym = SE->getLHS();
191
973
      Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
192
973
193
973
      // Don't forget to negate the adjustment if it's being subtracted.
194
973
      // This should happen /after/ promotion, in case the value being
195
973
      // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
196
973
      if (Op == BO_Sub)
197
410
        Adjustment = -Adjustment;
198
973
    }
199
37.1k
  }
200
175k
}
201
202
1.83M
void *ProgramStateTrait<ConstraintRange>::GDMIndex() {
203
1.83M
  static int Index;
204
1.83M
  return &Index;
205
1.83M
}
206
207
} // end of namespace ento
208
209
} // end of namespace clang