blob: 2afcd3e847cddbd82b71d517d3981f12e5cbcac7 [file] [log] [blame]
Ted Kremeneke8391722009-06-26 00:25:05 +00001// SimpleSValuator.cpp - A basic SValuator ------------------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines SimpleSValuator, a basic implementation of SValuator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/SValuator.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremeneke8391722009-06-26 00:25:05 +000016
17using namespace clang;
18
19namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000020class SimpleSValuator : public SValuator {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000021protected:
Mike Stump1eb44332009-09-09 15:08:12 +000022 virtual SVal EvalCastNL(NonLoc val, QualType castTy);
23 virtual SVal EvalCastL(Loc val, QualType castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +000024
Ted Kremeneke8391722009-06-26 00:25:05 +000025public:
26 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
27 virtual ~SimpleSValuator() {}
Mike Stump1eb44332009-09-09 15:08:12 +000028
29 virtual SVal EvalMinus(NonLoc val);
30 virtual SVal EvalComplement(NonLoc val);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +000031 virtual SVal EvalBinOpNN(const GRState *state, BinaryOperator::Opcode op,
32 NonLoc lhs, NonLoc rhs, QualType resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +000033 virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
34 QualType resultTy);
35 virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
36 Loc lhs, NonLoc rhs, QualType resultTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037};
Ted Kremeneke8391722009-06-26 00:25:05 +000038} // end anonymous namespace
39
40SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
41 return new SimpleSValuator(valMgr);
42}
43
44//===----------------------------------------------------------------------===//
45// Transfer function for Casts.
46//===----------------------------------------------------------------------===//
47
Ted Kremenekdd661142009-07-20 21:39:27 +000048SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekdd661142009-07-20 21:39:27 +000050 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek9031dd72009-07-21 00:12:07 +000052 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
53 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000054 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000055
56 ASTContext &Ctx = ValMgr.getContext();
57
Ted Kremenek9031dd72009-07-21 00:12:07 +000058 // FIXME: Support promotions/truncations.
59 if (Ctx.getTypeSize(castTy) == Ctx.getTypeSize(Ctx.VoidPtrTy))
60 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek9031dd72009-07-21 00:12:07 +000062 return UnknownVal();
63 }
64
65 if (const SymExpr *se = val.getAsSymbolicExpression()) {
66 ASTContext &Ctx = ValMgr.getContext();
67 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
68 if (T == Ctx.getCanonicalType(castTy))
69 return val;
Ted Kremenek80417472009-09-25 00:18:15 +000070
71 // FIXME: Remove this hack when we support symbolic truncation/extension.
72 // HACK: If both castTy and T are integers, ignore the cast. This is
73 // not a permanent solution. Eventually we want to precisely handle
74 // extension/truncation of symbolic integers. This prevents us from losing
75 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
76 // different integer types.
77 if (T->isIntegerType() && castTy->isIntegerType())
78 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek9031dd72009-07-21 00:12:07 +000080 return UnknownVal();
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremeneke8391722009-06-26 00:25:05 +000083 if (!isa<nonloc::ConcreteInt>(val))
84 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremeneke8391722009-06-26 00:25:05 +000086 // Only handle casts from integers to integers.
87 if (!isLocType && !castTy->isIntegerType())
88 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremeneke8391722009-06-26 00:25:05 +000090 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
91 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
92 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
93
94 if (isLocType)
95 return ValMgr.makeIntLocVal(i);
96 else
97 return ValMgr.makeIntVal(i);
98}
99
Ted Kremenek46537392009-07-16 01:33:37 +0000100SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremeneke8391722009-06-26 00:25:05 +0000102 // Casts from pointers -> pointers, just return the lval.
103 //
104 // Casts from pointers -> references, just return the lval. These
105 // can be introduced by the frontend for corner cases, e.g
106 // casting from va_list* to __builtin_va_list&.
107 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000108 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
109 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremeneke8391722009-06-26 00:25:05 +0000111 // FIXME: Handle transparent unions where a value can be "transparently"
112 // lifted into a union type.
113 if (castTy->isUnionType())
114 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremeneke8391722009-06-26 00:25:05 +0000116 assert(castTy->isIntegerType());
117 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
118
119 if (!isa<loc::ConcreteInt>(val))
120 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremeneke8391722009-06-26 00:25:05 +0000122 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
123 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
124 i.extOrTrunc(BitWidth);
125 return ValMgr.makeIntVal(i);
126}
127
128//===----------------------------------------------------------------------===//
129// Transfer function for unary operators.
130//===----------------------------------------------------------------------===//
131
132SVal SimpleSValuator::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000134 case nonloc::ConcreteIntKind:
135 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
136 default:
137 return UnknownVal();
138 }
139}
140
141SVal SimpleSValuator::EvalComplement(NonLoc X) {
142 switch (X.getSubKind()) {
143 case nonloc::ConcreteIntKind:
144 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
145 default:
146 return UnknownVal();
147 }
148}
149
150//===----------------------------------------------------------------------===//
151// Transfer function for binary operators.
152//===----------------------------------------------------------------------===//
153
154static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
155 switch (op) {
156 default:
157 assert(false && "Invalid opcode.");
158 case BinaryOperator::LT: return BinaryOperator::GE;
159 case BinaryOperator::GT: return BinaryOperator::LE;
160 case BinaryOperator::LE: return BinaryOperator::GT;
161 case BinaryOperator::GE: return BinaryOperator::LT;
162 case BinaryOperator::EQ: return BinaryOperator::NE;
163 case BinaryOperator::NE: return BinaryOperator::EQ;
164 }
165}
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167// Equality operators for Locs.
Ted Kremeneke8391722009-06-26 00:25:05 +0000168// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
169// implemented.
170
171static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
172 QualType resultTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneke8391722009-06-26 00:25:05 +0000174 switch (lhs.getSubKind()) {
175 default:
176 assert(false && "EQ/NE not implemented for this Loc.");
177 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremeneke8391722009-06-26 00:25:05 +0000179 case loc::ConcreteIntKind: {
180 if (SymbolRef rSym = rhs.getAsSymbol())
181 return ValMgr.makeNonLoc(rSym,
182 isEqual ? BinaryOperator::EQ
183 : BinaryOperator::NE,
184 cast<loc::ConcreteInt>(lhs).getValue(),
185 resultTy);
186 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000187 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000188 case loc::MemRegionKind: {
189 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
190 if (isa<loc::ConcreteInt>(rhs)) {
191 return ValMgr.makeNonLoc(lSym,
192 isEqual ? BinaryOperator::EQ
193 : BinaryOperator::NE,
194 cast<loc::ConcreteInt>(rhs).getValue(),
195 resultTy);
196 }
197 }
198 break;
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremeneke8391722009-06-26 00:25:05 +0000201 case loc::GotoLabelKind:
202 break;
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremeneke8391722009-06-26 00:25:05 +0000205 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
206}
207
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000208SVal SimpleSValuator::EvalBinOpNN(const GRState *state,
209 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000210 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000211 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000212 // Handle trivial case where left-side and right-side are the same.
213 if (lhs == rhs)
214 switch (op) {
215 default:
216 break;
217 case BinaryOperator::EQ:
218 case BinaryOperator::LE:
219 case BinaryOperator::GE:
220 return ValMgr.makeTruthVal(true, resultTy);
221 case BinaryOperator::LT:
222 case BinaryOperator::GT:
223 case BinaryOperator::NE:
224 return ValMgr.makeTruthVal(false, resultTy);
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremeneke8391722009-06-26 00:25:05 +0000227 while (1) {
228 switch (lhs.getSubKind()) {
229 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000230 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000231 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000232 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000233 switch (rhs.getSubKind()) {
234 case nonloc::LocAsIntegerKind:
235 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000236 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000237 case nonloc::ConcreteIntKind: {
238 // Transform the integer into a location and compare.
239 ASTContext& Ctx = ValMgr.getContext();
240 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
241 i.setIsUnsigned(true);
242 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
243 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000246 switch (op) {
247 case BinaryOperator::EQ:
248 return ValMgr.makeTruthVal(false, resultTy);
249 case BinaryOperator::NE:
250 return ValMgr.makeTruthVal(true, resultTy);
251 default:
252 // This case also handles pointer arithmetic.
253 return UnknownVal();
254 }
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000257 case nonloc::SymExprValKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000258 // Logical not?
Ted Kremeneke8391722009-06-26 00:25:05 +0000259 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
260 return UnknownVal();
261
262 const SymExpr *symExpr =
263 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremeneke8391722009-06-26 00:25:05 +0000265 // Only handle ($sym op constant) for now.
266 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
267 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
268 switch (opc) {
269 case BinaryOperator::LAnd:
270 case BinaryOperator::LOr:
271 assert(false && "Logical operators handled by branching logic.");
272 return UnknownVal();
273 case BinaryOperator::Assign:
274 case BinaryOperator::MulAssign:
275 case BinaryOperator::DivAssign:
276 case BinaryOperator::RemAssign:
277 case BinaryOperator::AddAssign:
278 case BinaryOperator::SubAssign:
279 case BinaryOperator::ShlAssign:
280 case BinaryOperator::ShrAssign:
281 case BinaryOperator::AndAssign:
282 case BinaryOperator::XorAssign:
283 case BinaryOperator::OrAssign:
284 case BinaryOperator::Comma:
285 assert(false && "'=' and ',' operators handled by GRExprEngine.");
286 return UnknownVal();
287 case BinaryOperator::PtrMemD:
288 case BinaryOperator::PtrMemI:
289 assert(false && "Pointer arithmetic not handled here.");
290 return UnknownVal();
291 case BinaryOperator::Mul:
292 case BinaryOperator::Div:
293 case BinaryOperator::Rem:
294 case BinaryOperator::Add:
295 case BinaryOperator::Sub:
296 case BinaryOperator::Shl:
297 case BinaryOperator::Shr:
298 case BinaryOperator::And:
299 case BinaryOperator::Xor:
300 case BinaryOperator::Or:
301 // Not handled yet.
302 return UnknownVal();
303 case BinaryOperator::LT:
304 case BinaryOperator::GT:
305 case BinaryOperator::LE:
306 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000307 case BinaryOperator::EQ:
Ted Kremeneke8391722009-06-26 00:25:05 +0000308 case BinaryOperator::NE:
309 opc = NegateComparison(opc);
310 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
311 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
312 symIntExpr->getRHS(), resultTy);
313 }
314 }
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316 case nonloc::ConcreteIntKind: {
Ted Kremeneke8391722009-06-26 00:25:05 +0000317 if (isa<nonloc::ConcreteInt>(rhs)) {
318 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
319 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
320 }
321 else {
322 // Swap the left and right sides and flip the operator if doing so
323 // allows us to better reason about the expression (this is a form
324 // of expression canonicalization).
325 NonLoc tmp = rhs;
326 rhs = lhs;
327 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremeneke8391722009-06-26 00:25:05 +0000329 switch (op) {
330 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
331 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
332 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
333 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
334 case BinaryOperator::EQ:
335 case BinaryOperator::NE:
336 case BinaryOperator::Add:
337 case BinaryOperator::Mul:
338 continue;
339 default:
340 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000341 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000342 }
343 }
344 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000345 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
346 SymbolRef Sym = slhs->getSymbol();
347
Ted Kremenek9b020342009-10-17 07:39:35 +0000348 // Does the symbol simplify to a constant? If so, "fold" the constant
349 // by setting 'lhs' to a ConcreteInt and try again.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000350 if (Sym->getType(ValMgr.getContext())->isIntegerType())
351 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
Ted Kremenek9b020342009-10-17 07:39:35 +0000352 // The symbol evaluates to a constant. If necessary, promote the
353 // folded constant (LHS) to the result type.
354 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
355 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
356 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000357
Ted Kremenek9b020342009-10-17 07:39:35 +0000358 // Also promote the RHS (if necessary).
359
360 // For shifts, it necessary promote the RHS to the result type.
361 if (BinaryOperator::isShiftOp(op))
362 continue;
363
364 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000365 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000366 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenek9b020342009-10-17 07:39:35 +0000367 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
Ted Kremenekb1d04222009-10-06 03:44:49 +0000368 }
Ted Kremenek9b020342009-10-17 07:39:35 +0000369
370 continue;
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000371 }
372
Ted Kremeneke8391722009-06-26 00:25:05 +0000373 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000374 return ValMgr.makeNonLoc(slhs->getSymbol(), op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000375 cast<nonloc::ConcreteInt>(rhs).getValue(),
376 resultTy);
377 }
378
379 return UnknownVal();
380 }
381 }
382 }
383}
384
385SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000386 QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000387 switch (op) {
388 default:
389 return UnknownVal();
390 case BinaryOperator::EQ:
391 case BinaryOperator::NE:
392 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000393 case BinaryOperator::LT:
394 case BinaryOperator::GT:
395 // FIXME: Generalize. For now, just handle the trivial case where
396 // the two locations are identical.
397 if (lhs == rhs)
398 return ValMgr.makeTruthVal(false, resultTy);
399 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000400 }
401}
402
403SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
404 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000405 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000406 // Special case: 'rhs' is an integer that has the same width as a pointer and
407 // we are using the integer location in a comparison. Normally this cannot be
408 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
409 // can generate comparisons that trigger this code.
410 // FIXME: Are all locations guaranteed to have pointer width?
411 if (BinaryOperator::isEqualityOp(op)) {
412 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
413 const llvm::APSInt *x = &rhsInt->getValue();
414 ASTContext &ctx = ValMgr.getContext();
415 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
416 // Convert the signedness of the integer (if necessary).
417 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000418 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000419
420 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
421 }
422 }
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremeneke8391722009-06-26 00:25:05 +0000425 // Delegate pointer arithmetic to the StoreManager.
426 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
427 rhs, resultTy);
428}