Coverage Report

Created: 2019-07-24 05:18

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