1 // SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- 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 SimpleSValBuilder, a basic implementation of SValBuilder.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17
18 using namespace clang;
19 using namespace ento;
20
21 namespace {
22 class SimpleSValBuilder : public SValBuilder {
23 protected:
24 virtual SVal dispatchCast(SVal val, QualType castTy);
25 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy);
26 virtual SVal evalCastFromLoc(Loc val, QualType castTy);
27
28 public:
SimpleSValBuilder(llvm::BumpPtrAllocator & alloc,ASTContext & context,ProgramStateManager & stateMgr)29 SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
30 ProgramStateManager &stateMgr)
31 : SValBuilder(alloc, context, stateMgr) {}
~SimpleSValBuilder()32 virtual ~SimpleSValBuilder() {}
33
34 virtual SVal evalMinus(NonLoc val);
35 virtual SVal evalComplement(NonLoc val);
36 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
37 NonLoc lhs, NonLoc rhs, QualType resultTy);
38 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
39 Loc lhs, Loc rhs, QualType resultTy);
40 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
41 Loc lhs, NonLoc rhs, QualType resultTy);
42
43 /// getKnownValue - evaluates a given SVal. If the SVal has only one possible
44 /// (integer) value, that value is returned. Otherwise, returns NULL.
45 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal V);
46
47 SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
48 const llvm::APSInt &RHS, QualType resultTy);
49 };
50 } // end anonymous namespace
51
createSimpleSValBuilder(llvm::BumpPtrAllocator & alloc,ASTContext & context,ProgramStateManager & stateMgr)52 SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
53 ASTContext &context,
54 ProgramStateManager &stateMgr) {
55 return new SimpleSValBuilder(alloc, context, stateMgr);
56 }
57
58 //===----------------------------------------------------------------------===//
59 // Transfer function for Casts.
60 //===----------------------------------------------------------------------===//
61
dispatchCast(SVal Val,QualType CastTy)62 SVal SimpleSValBuilder::dispatchCast(SVal Val, QualType CastTy) {
63 assert(Val.getAs<Loc>() || Val.getAs<NonLoc>());
64 return Val.getAs<Loc>() ? evalCastFromLoc(Val.castAs<Loc>(), CastTy)
65 : evalCastFromNonLoc(Val.castAs<NonLoc>(), CastTy);
66 }
67
evalCastFromNonLoc(NonLoc val,QualType castTy)68 SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
69
70 bool isLocType = Loc::isLocType(castTy);
71
72 if (Optional<nonloc::LocAsInteger> LI = val.getAs<nonloc::LocAsInteger>()) {
73 if (isLocType)
74 return LI->getLoc();
75
76 // FIXME: Correctly support promotions/truncations.
77 unsigned castSize = Context.getTypeSize(castTy);
78 if (castSize == LI->getNumBits())
79 return val;
80 return makeLocAsInteger(LI->getLoc(), castSize);
81 }
82
83 if (const SymExpr *se = val.getAsSymbolicExpression()) {
84 QualType T = Context.getCanonicalType(se->getType());
85 // If types are the same or both are integers, ignore the cast.
86 // FIXME: Remove this hack when we support symbolic truncation/extension.
87 // HACK: If both castTy and T are integers, ignore the cast. This is
88 // not a permanent solution. Eventually we want to precisely handle
89 // extension/truncation of symbolic integers. This prevents us from losing
90 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
91 // different integer types.
92 if (haveSameType(T, castTy))
93 return val;
94
95 if (!isLocType)
96 return makeNonLoc(se, T, castTy);
97 return UnknownVal();
98 }
99
100 // If value is a non integer constant, produce unknown.
101 if (!val.getAs<nonloc::ConcreteInt>())
102 return UnknownVal();
103
104 // Handle casts to a boolean type.
105 if (castTy->isBooleanType()) {
106 bool b = val.castAs<nonloc::ConcreteInt>().getValue().getBoolValue();
107 return makeTruthVal(b, castTy);
108 }
109
110 // Only handle casts from integers to integers - if val is an integer constant
111 // being cast to a non integer type, produce unknown.
112 if (!isLocType && !castTy->isIntegerType())
113 return UnknownVal();
114
115 llvm::APSInt i = val.castAs<nonloc::ConcreteInt>().getValue();
116 BasicVals.getAPSIntType(castTy).apply(i);
117
118 if (isLocType)
119 return makeIntLocVal(i);
120 else
121 return makeIntVal(i);
122 }
123
evalCastFromLoc(Loc val,QualType castTy)124 SVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) {
125
126 // Casts from pointers -> pointers, just return the lval.
127 //
128 // Casts from pointers -> references, just return the lval. These
129 // can be introduced by the frontend for corner cases, e.g
130 // casting from va_list* to __builtin_va_list&.
131 //
132 if (Loc::isLocType(castTy) || castTy->isReferenceType())
133 return val;
134
135 // FIXME: Handle transparent unions where a value can be "transparently"
136 // lifted into a union type.
137 if (castTy->isUnionType())
138 return UnknownVal();
139
140 if (castTy->isIntegerType()) {
141 unsigned BitWidth = Context.getTypeSize(castTy);
142
143 if (!val.getAs<loc::ConcreteInt>())
144 return makeLocAsInteger(val, BitWidth);
145
146 llvm::APSInt i = val.castAs<loc::ConcreteInt>().getValue();
147 BasicVals.getAPSIntType(castTy).apply(i);
148 return makeIntVal(i);
149 }
150
151 // All other cases: return 'UnknownVal'. This includes casting pointers
152 // to floats, which is probably badness it itself, but this is a good
153 // intermediate solution until we do something better.
154 return UnknownVal();
155 }
156
157 //===----------------------------------------------------------------------===//
158 // Transfer function for unary operators.
159 //===----------------------------------------------------------------------===//
160
evalMinus(NonLoc val)161 SVal SimpleSValBuilder::evalMinus(NonLoc val) {
162 switch (val.getSubKind()) {
163 case nonloc::ConcreteIntKind:
164 return val.castAs<nonloc::ConcreteInt>().evalMinus(*this);
165 default:
166 return UnknownVal();
167 }
168 }
169
evalComplement(NonLoc X)170 SVal SimpleSValBuilder::evalComplement(NonLoc X) {
171 switch (X.getSubKind()) {
172 case nonloc::ConcreteIntKind:
173 return X.castAs<nonloc::ConcreteInt>().evalComplement(*this);
174 default:
175 return UnknownVal();
176 }
177 }
178
179 //===----------------------------------------------------------------------===//
180 // Transfer function for binary operators.
181 //===----------------------------------------------------------------------===//
182
NegateComparison(BinaryOperator::Opcode op)183 static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
184 switch (op) {
185 default:
186 llvm_unreachable("Invalid opcode.");
187 case BO_LT: return BO_GE;
188 case BO_GT: return BO_LE;
189 case BO_LE: return BO_GT;
190 case BO_GE: return BO_LT;
191 case BO_EQ: return BO_NE;
192 case BO_NE: return BO_EQ;
193 }
194 }
195
ReverseComparison(BinaryOperator::Opcode op)196 static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) {
197 switch (op) {
198 default:
199 llvm_unreachable("Invalid opcode.");
200 case BO_LT: return BO_GT;
201 case BO_GT: return BO_LT;
202 case BO_LE: return BO_GE;
203 case BO_GE: return BO_LE;
204 case BO_EQ:
205 case BO_NE:
206 return op;
207 }
208 }
209
MakeSymIntVal(const SymExpr * LHS,BinaryOperator::Opcode op,const llvm::APSInt & RHS,QualType resultTy)210 SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS,
211 BinaryOperator::Opcode op,
212 const llvm::APSInt &RHS,
213 QualType resultTy) {
214 bool isIdempotent = false;
215
216 // Check for a few special cases with known reductions first.
217 switch (op) {
218 default:
219 // We can't reduce this case; just treat it normally.
220 break;
221 case BO_Mul:
222 // a*0 and a*1
223 if (RHS == 0)
224 return makeIntVal(0, resultTy);
225 else if (RHS == 1)
226 isIdempotent = true;
227 break;
228 case BO_Div:
229 // a/0 and a/1
230 if (RHS == 0)
231 // This is also handled elsewhere.
232 return UndefinedVal();
233 else if (RHS == 1)
234 isIdempotent = true;
235 break;
236 case BO_Rem:
237 // a%0 and a%1
238 if (RHS == 0)
239 // This is also handled elsewhere.
240 return UndefinedVal();
241 else if (RHS == 1)
242 return makeIntVal(0, resultTy);
243 break;
244 case BO_Add:
245 case BO_Sub:
246 case BO_Shl:
247 case BO_Shr:
248 case BO_Xor:
249 // a+0, a-0, a<<0, a>>0, a^0
250 if (RHS == 0)
251 isIdempotent = true;
252 break;
253 case BO_And:
254 // a&0 and a&(~0)
255 if (RHS == 0)
256 return makeIntVal(0, resultTy);
257 else if (RHS.isAllOnesValue())
258 isIdempotent = true;
259 break;
260 case BO_Or:
261 // a|0 and a|(~0)
262 if (RHS == 0)
263 isIdempotent = true;
264 else if (RHS.isAllOnesValue()) {
265 const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS);
266 return nonloc::ConcreteInt(Result);
267 }
268 break;
269 }
270
271 // Idempotent ops (like a*1) can still change the type of an expression.
272 // Wrap the LHS up in a NonLoc again and let evalCastFromNonLoc do the
273 // dirty work.
274 if (isIdempotent)
275 return evalCastFromNonLoc(nonloc::SymbolVal(LHS), resultTy);
276
277 // If we reach this point, the expression cannot be simplified.
278 // Make a SymbolVal for the entire expression, after converting the RHS.
279 const llvm::APSInt *ConvertedRHS = &RHS;
280 if (BinaryOperator::isComparisonOp(op)) {
281 // We're looking for a type big enough to compare the symbolic value
282 // with the given constant.
283 // FIXME: This is an approximation of Sema::UsualArithmeticConversions.
284 ASTContext &Ctx = getContext();
285 QualType SymbolType = LHS->getType();
286 uint64_t ValWidth = RHS.getBitWidth();
287 uint64_t TypeWidth = Ctx.getTypeSize(SymbolType);
288
289 if (ValWidth < TypeWidth) {
290 // If the value is too small, extend it.
291 ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
292 } else if (ValWidth == TypeWidth) {
293 // If the value is signed but the symbol is unsigned, do the comparison
294 // in unsigned space. [C99 6.3.1.8]
295 // (For the opposite case, the value is already unsigned.)
296 if (RHS.isSigned() && !SymbolType->isSignedIntegerOrEnumerationType())
297 ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
298 }
299 } else
300 ConvertedRHS = &BasicVals.Convert(resultTy, RHS);
301
302 return makeNonLoc(LHS, op, *ConvertedRHS, resultTy);
303 }
304
evalBinOpNN(ProgramStateRef state,BinaryOperator::Opcode op,NonLoc lhs,NonLoc rhs,QualType resultTy)305 SVal SimpleSValBuilder::evalBinOpNN(ProgramStateRef state,
306 BinaryOperator::Opcode op,
307 NonLoc lhs, NonLoc rhs,
308 QualType resultTy) {
309 NonLoc InputLHS = lhs;
310 NonLoc InputRHS = rhs;
311
312 // Handle trivial case where left-side and right-side are the same.
313 if (lhs == rhs)
314 switch (op) {
315 default:
316 break;
317 case BO_EQ:
318 case BO_LE:
319 case BO_GE:
320 return makeTruthVal(true, resultTy);
321 case BO_LT:
322 case BO_GT:
323 case BO_NE:
324 return makeTruthVal(false, resultTy);
325 case BO_Xor:
326 case BO_Sub:
327 if (resultTy->isIntegralOrEnumerationType())
328 return makeIntVal(0, resultTy);
329 return evalCastFromNonLoc(makeIntVal(0, /*Unsigned=*/false), resultTy);
330 case BO_Or:
331 case BO_And:
332 return evalCastFromNonLoc(lhs, resultTy);
333 }
334
335 while (1) {
336 switch (lhs.getSubKind()) {
337 default:
338 return makeSymExprValNN(state, op, lhs, rhs, resultTy);
339 case nonloc::LocAsIntegerKind: {
340 Loc lhsL = lhs.castAs<nonloc::LocAsInteger>().getLoc();
341 switch (rhs.getSubKind()) {
342 case nonloc::LocAsIntegerKind:
343 return evalBinOpLL(state, op, lhsL,
344 rhs.castAs<nonloc::LocAsInteger>().getLoc(),
345 resultTy);
346 case nonloc::ConcreteIntKind: {
347 // Transform the integer into a location and compare.
348 llvm::APSInt i = rhs.castAs<nonloc::ConcreteInt>().getValue();
349 BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i);
350 return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy);
351 }
352 default:
353 switch (op) {
354 case BO_EQ:
355 return makeTruthVal(false, resultTy);
356 case BO_NE:
357 return makeTruthVal(true, resultTy);
358 default:
359 // This case also handles pointer arithmetic.
360 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
361 }
362 }
363 }
364 case nonloc::ConcreteIntKind: {
365 llvm::APSInt LHSValue = lhs.castAs<nonloc::ConcreteInt>().getValue();
366
367 // If we're dealing with two known constants, just perform the operation.
368 if (const llvm::APSInt *KnownRHSValue = getKnownValue(state, rhs)) {
369 llvm::APSInt RHSValue = *KnownRHSValue;
370 if (BinaryOperator::isComparisonOp(op)) {
371 // We're looking for a type big enough to compare the two values.
372 // FIXME: This is not correct. char + short will result in a promotion
373 // to int. Unfortunately we have lost types by this point.
374 APSIntType CompareType = std::max(APSIntType(LHSValue),
375 APSIntType(RHSValue));
376 CompareType.apply(LHSValue);
377 CompareType.apply(RHSValue);
378 } else if (!BinaryOperator::isShiftOp(op)) {
379 APSIntType IntType = BasicVals.getAPSIntType(resultTy);
380 IntType.apply(LHSValue);
381 IntType.apply(RHSValue);
382 }
383
384 const llvm::APSInt *Result =
385 BasicVals.evalAPSInt(op, LHSValue, RHSValue);
386 if (!Result)
387 return UndefinedVal();
388
389 return nonloc::ConcreteInt(*Result);
390 }
391
392 // Swap the left and right sides and flip the operator if doing so
393 // allows us to better reason about the expression (this is a form
394 // of expression canonicalization).
395 // While we're at it, catch some special cases for non-commutative ops.
396 switch (op) {
397 case BO_LT:
398 case BO_GT:
399 case BO_LE:
400 case BO_GE:
401 op = ReverseComparison(op);
402 // FALL-THROUGH
403 case BO_EQ:
404 case BO_NE:
405 case BO_Add:
406 case BO_Mul:
407 case BO_And:
408 case BO_Xor:
409 case BO_Or:
410 std::swap(lhs, rhs);
411 continue;
412 case BO_Shr:
413 // (~0)>>a
414 if (LHSValue.isAllOnesValue() && LHSValue.isSigned())
415 return evalCastFromNonLoc(lhs, resultTy);
416 // FALL-THROUGH
417 case BO_Shl:
418 // 0<<a and 0>>a
419 if (LHSValue == 0)
420 return evalCastFromNonLoc(lhs, resultTy);
421 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
422 default:
423 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
424 }
425 }
426 case nonloc::SymbolValKind: {
427 // We only handle LHS as simple symbols or SymIntExprs.
428 SymbolRef Sym = lhs.castAs<nonloc::SymbolVal>().getSymbol();
429
430 // LHS is a symbolic expression.
431 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(Sym)) {
432
433 // Is this a logical not? (!x is represented as x == 0.)
434 if (op == BO_EQ && rhs.isZeroConstant()) {
435 // We know how to negate certain expressions. Simplify them here.
436
437 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
438 switch (opc) {
439 default:
440 // We don't know how to negate this operation.
441 // Just handle it as if it were a normal comparison to 0.
442 break;
443 case BO_LAnd:
444 case BO_LOr:
445 llvm_unreachable("Logical operators handled by branching logic.");
446 case BO_Assign:
447 case BO_MulAssign:
448 case BO_DivAssign:
449 case BO_RemAssign:
450 case BO_AddAssign:
451 case BO_SubAssign:
452 case BO_ShlAssign:
453 case BO_ShrAssign:
454 case BO_AndAssign:
455 case BO_XorAssign:
456 case BO_OrAssign:
457 case BO_Comma:
458 llvm_unreachable("'=' and ',' operators handled by ExprEngine.");
459 case BO_PtrMemD:
460 case BO_PtrMemI:
461 llvm_unreachable("Pointer arithmetic not handled here.");
462 case BO_LT:
463 case BO_GT:
464 case BO_LE:
465 case BO_GE:
466 case BO_EQ:
467 case BO_NE:
468 // Negate the comparison and make a value.
469 opc = NegateComparison(opc);
470 assert(symIntExpr->getType() == resultTy);
471 return makeNonLoc(symIntExpr->getLHS(), opc,
472 symIntExpr->getRHS(), resultTy);
473 }
474 }
475
476 // For now, only handle expressions whose RHS is a constant.
477 if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs)) {
478 // If both the LHS and the current expression are additive,
479 // fold their constants and try again.
480 if (BinaryOperator::isAdditiveOp(op)) {
481 BinaryOperator::Opcode lop = symIntExpr->getOpcode();
482 if (BinaryOperator::isAdditiveOp(lop)) {
483 // Convert the two constants to a common type, then combine them.
484
485 // resultTy may not be the best type to convert to, but it's
486 // probably the best choice in expressions with mixed type
487 // (such as x+1U+2LL). The rules for implicit conversions should
488 // choose a reasonable type to preserve the expression, and will
489 // at least match how the value is going to be used.
490 APSIntType IntType = BasicVals.getAPSIntType(resultTy);
491 const llvm::APSInt &first = IntType.convert(symIntExpr->getRHS());
492 const llvm::APSInt &second = IntType.convert(*RHSValue);
493
494 const llvm::APSInt *newRHS;
495 if (lop == op)
496 newRHS = BasicVals.evalAPSInt(BO_Add, first, second);
497 else
498 newRHS = BasicVals.evalAPSInt(BO_Sub, first, second);
499
500 assert(newRHS && "Invalid operation despite common type!");
501 rhs = nonloc::ConcreteInt(*newRHS);
502 lhs = nonloc::SymbolVal(symIntExpr->getLHS());
503 op = lop;
504 continue;
505 }
506 }
507
508 // Otherwise, make a SymIntExpr out of the expression.
509 return MakeSymIntVal(symIntExpr, op, *RHSValue, resultTy);
510 }
511
512
513 } else if (isa<SymbolData>(Sym)) {
514 // Does the symbol simplify to a constant? If so, "fold" the constant
515 // by setting 'lhs' to a ConcreteInt and try again.
516 if (const llvm::APSInt *Constant = state->getConstraintManager()
517 .getSymVal(state, Sym)) {
518 lhs = nonloc::ConcreteInt(*Constant);
519 continue;
520 }
521
522 // Is the RHS a constant?
523 if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs))
524 return MakeSymIntVal(Sym, op, *RHSValue, resultTy);
525 }
526
527 // Give up -- this is not a symbolic expression we can handle.
528 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
529 }
530 }
531 }
532 }
533
534 // FIXME: all this logic will change if/when we have MemRegion::getLocation().
evalBinOpLL(ProgramStateRef state,BinaryOperator::Opcode op,Loc lhs,Loc rhs,QualType resultTy)535 SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state,
536 BinaryOperator::Opcode op,
537 Loc lhs, Loc rhs,
538 QualType resultTy) {
539 // Only comparisons and subtractions are valid operations on two pointers.
540 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
541 // However, if a pointer is casted to an integer, evalBinOpNN may end up
542 // calling this function with another operation (PR7527). We don't attempt to
543 // model this for now, but it could be useful, particularly when the
544 // "location" is actually an integer value that's been passed through a void*.
545 if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub))
546 return UnknownVal();
547
548 // Special cases for when both sides are identical.
549 if (lhs == rhs) {
550 switch (op) {
551 default:
552 llvm_unreachable("Unimplemented operation for two identical values");
553 case BO_Sub:
554 return makeZeroVal(resultTy);
555 case BO_EQ:
556 case BO_LE:
557 case BO_GE:
558 return makeTruthVal(true, resultTy);
559 case BO_NE:
560 case BO_LT:
561 case BO_GT:
562 return makeTruthVal(false, resultTy);
563 }
564 }
565
566 switch (lhs.getSubKind()) {
567 default:
568 llvm_unreachable("Ordering not implemented for this Loc.");
569
570 case loc::GotoLabelKind:
571 // The only thing we know about labels is that they're non-null.
572 if (rhs.isZeroConstant()) {
573 switch (op) {
574 default:
575 break;
576 case BO_Sub:
577 return evalCastFromLoc(lhs, resultTy);
578 case BO_EQ:
579 case BO_LE:
580 case BO_LT:
581 return makeTruthVal(false, resultTy);
582 case BO_NE:
583 case BO_GT:
584 case BO_GE:
585 return makeTruthVal(true, resultTy);
586 }
587 }
588 // There may be two labels for the same location, and a function region may
589 // have the same address as a label at the start of the function (depending
590 // on the ABI).
591 // FIXME: we can probably do a comparison against other MemRegions, though.
592 // FIXME: is there a way to tell if two labels refer to the same location?
593 return UnknownVal();
594
595 case loc::ConcreteIntKind: {
596 // If one of the operands is a symbol and the other is a constant,
597 // build an expression for use by the constraint manager.
598 if (SymbolRef rSym = rhs.getAsLocSymbol()) {
599 // We can only build expressions with symbols on the left,
600 // so we need a reversible operator.
601 if (!BinaryOperator::isComparisonOp(op))
602 return UnknownVal();
603
604 const llvm::APSInt &lVal = lhs.castAs<loc::ConcreteInt>().getValue();
605 return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
606 }
607
608 // If both operands are constants, just perform the operation.
609 if (Optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) {
610 SVal ResultVal =
611 lhs.castAs<loc::ConcreteInt>().evalBinOp(BasicVals, op, *rInt);
612 if (Optional<Loc> Result = ResultVal.getAs<Loc>())
613 return evalCastFromLoc(*Result, resultTy);
614 else
615 return UnknownVal();
616 }
617
618 // Special case comparisons against NULL.
619 // This must come after the test if the RHS is a symbol, which is used to
620 // build constraints. The address of any non-symbolic region is guaranteed
621 // to be non-NULL, as is any label.
622 assert(rhs.getAs<loc::MemRegionVal>() || rhs.getAs<loc::GotoLabel>());
623 if (lhs.isZeroConstant()) {
624 switch (op) {
625 default:
626 break;
627 case BO_EQ:
628 case BO_GT:
629 case BO_GE:
630 return makeTruthVal(false, resultTy);
631 case BO_NE:
632 case BO_LT:
633 case BO_LE:
634 return makeTruthVal(true, resultTy);
635 }
636 }
637
638 // Comparing an arbitrary integer to a region or label address is
639 // completely unknowable.
640 return UnknownVal();
641 }
642 case loc::MemRegionKind: {
643 if (Optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) {
644 // If one of the operands is a symbol and the other is a constant,
645 // build an expression for use by the constraint manager.
646 if (SymbolRef lSym = lhs.getAsLocSymbol())
647 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
648
649 // Special case comparisons to NULL.
650 // This must come after the test if the LHS is a symbol, which is used to
651 // build constraints. The address of any non-symbolic region is guaranteed
652 // to be non-NULL.
653 if (rInt->isZeroConstant()) {
654 switch (op) {
655 default:
656 break;
657 case BO_Sub:
658 return evalCastFromLoc(lhs, resultTy);
659 case BO_EQ:
660 case BO_LT:
661 case BO_LE:
662 return makeTruthVal(false, resultTy);
663 case BO_NE:
664 case BO_GT:
665 case BO_GE:
666 return makeTruthVal(true, resultTy);
667 }
668 }
669
670 // Comparing a region to an arbitrary integer is completely unknowable.
671 return UnknownVal();
672 }
673
674 // Get both values as regions, if possible.
675 const MemRegion *LeftMR = lhs.getAsRegion();
676 assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
677
678 const MemRegion *RightMR = rhs.getAsRegion();
679 if (!RightMR)
680 // The RHS is probably a label, which in theory could address a region.
681 // FIXME: we can probably make a more useful statement about non-code
682 // regions, though.
683 return UnknownVal();
684
685 const MemSpaceRegion *LeftMS = LeftMR->getMemorySpace();
686 const MemSpaceRegion *RightMS = RightMR->getMemorySpace();
687 const MemSpaceRegion *UnknownMS = MemMgr.getUnknownRegion();
688 const MemRegion *LeftBase = LeftMR->getBaseRegion();
689 const MemRegion *RightBase = RightMR->getBaseRegion();
690
691 // If the two regions are from different known memory spaces they cannot be
692 // equal. Also, assume that no symbolic region (whose memory space is
693 // unknown) is on the stack.
694 if (LeftMS != RightMS &&
695 ((LeftMS != UnknownMS && RightMS != UnknownMS) ||
696 (isa<StackSpaceRegion>(LeftMS) || isa<StackSpaceRegion>(RightMS)))) {
697 switch (op) {
698 default:
699 return UnknownVal();
700 case BO_EQ:
701 return makeTruthVal(false, resultTy);
702 case BO_NE:
703 return makeTruthVal(true, resultTy);
704 }
705 }
706
707 // If both values wrap regions, see if they're from different base regions.
708 // Note, heap base symbolic regions are assumed to not alias with
709 // each other; for example, we assume that malloc returns different address
710 // on each invocation.
711 if (LeftBase != RightBase &&
712 ((!isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) ||
713 (isa<HeapSpaceRegion>(LeftMS) || isa<HeapSpaceRegion>(RightMS))) ){
714 switch (op) {
715 default:
716 return UnknownVal();
717 case BO_EQ:
718 return makeTruthVal(false, resultTy);
719 case BO_NE:
720 return makeTruthVal(true, resultTy);
721 }
722 }
723
724 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
725 // ElementRegion path and the FieldRegion path below should be unified.
726 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
727 // First see if the right region is also an ElementRegion.
728 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
729 if (!RightER)
730 return UnknownVal();
731
732 // Next, see if the two ERs have the same super-region and matching types.
733 // FIXME: This should do something useful even if the types don't match,
734 // though if both indexes are constant the RegionRawOffset path will
735 // give the correct answer.
736 if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
737 LeftER->getElementType() == RightER->getElementType()) {
738 // Get the left index and cast it to the correct type.
739 // If the index is unknown or undefined, bail out here.
740 SVal LeftIndexVal = LeftER->getIndex();
741 Optional<NonLoc> LeftIndex = LeftIndexVal.getAs<NonLoc>();
742 if (!LeftIndex)
743 return UnknownVal();
744 LeftIndexVal = evalCastFromNonLoc(*LeftIndex, ArrayIndexTy);
745 LeftIndex = LeftIndexVal.getAs<NonLoc>();
746 if (!LeftIndex)
747 return UnknownVal();
748
749 // Do the same for the right index.
750 SVal RightIndexVal = RightER->getIndex();
751 Optional<NonLoc> RightIndex = RightIndexVal.getAs<NonLoc>();
752 if (!RightIndex)
753 return UnknownVal();
754 RightIndexVal = evalCastFromNonLoc(*RightIndex, ArrayIndexTy);
755 RightIndex = RightIndexVal.getAs<NonLoc>();
756 if (!RightIndex)
757 return UnknownVal();
758
759 // Actually perform the operation.
760 // evalBinOpNN expects the two indexes to already be the right type.
761 return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
762 }
763
764 // If the element indexes aren't comparable, see if the raw offsets are.
765 RegionRawOffset LeftOffset = LeftER->getAsArrayOffset();
766 RegionRawOffset RightOffset = RightER->getAsArrayOffset();
767
768 if (LeftOffset.getRegion() != NULL &&
769 LeftOffset.getRegion() == RightOffset.getRegion()) {
770 CharUnits left = LeftOffset.getOffset();
771 CharUnits right = RightOffset.getOffset();
772
773 switch (op) {
774 default:
775 return UnknownVal();
776 case BO_LT:
777 return makeTruthVal(left < right, resultTy);
778 case BO_GT:
779 return makeTruthVal(left > right, resultTy);
780 case BO_LE:
781 return makeTruthVal(left <= right, resultTy);
782 case BO_GE:
783 return makeTruthVal(left >= right, resultTy);
784 case BO_EQ:
785 return makeTruthVal(left == right, resultTy);
786 case BO_NE:
787 return makeTruthVal(left != right, resultTy);
788 }
789 }
790
791 // If we get here, we have no way of comparing the ElementRegions.
792 return UnknownVal();
793 }
794
795 // See if both regions are fields of the same structure.
796 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
797 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
798 // Only comparisons are meaningful here!
799 if (!BinaryOperator::isComparisonOp(op))
800 return UnknownVal();
801
802 // First see if the right region is also a FieldRegion.
803 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
804 if (!RightFR)
805 return UnknownVal();
806
807 // Next, see if the two FRs have the same super-region.
808 // FIXME: This doesn't handle casts yet, and simply stripping the casts
809 // doesn't help.
810 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
811 return UnknownVal();
812
813 const FieldDecl *LeftFD = LeftFR->getDecl();
814 const FieldDecl *RightFD = RightFR->getDecl();
815 const RecordDecl *RD = LeftFD->getParent();
816
817 // Make sure the two FRs are from the same kind of record. Just in case!
818 // FIXME: This is probably where inheritance would be a problem.
819 if (RD != RightFD->getParent())
820 return UnknownVal();
821
822 // We know for sure that the two fields are not the same, since that
823 // would have given us the same SVal.
824 if (op == BO_EQ)
825 return makeTruthVal(false, resultTy);
826 if (op == BO_NE)
827 return makeTruthVal(true, resultTy);
828
829 // Iterate through the fields and see which one comes first.
830 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
831 // members and the units in which bit-fields reside have addresses that
832 // increase in the order in which they are declared."
833 bool leftFirst = (op == BO_LT || op == BO_LE);
834 for (RecordDecl::field_iterator I = RD->field_begin(),
835 E = RD->field_end(); I!=E; ++I) {
836 if (*I == LeftFD)
837 return makeTruthVal(leftFirst, resultTy);
838 if (*I == RightFD)
839 return makeTruthVal(!leftFirst, resultTy);
840 }
841
842 llvm_unreachable("Fields not found in parent record's definition");
843 }
844
845 // If we get here, we have no way of comparing the regions.
846 return UnknownVal();
847 }
848 }
849 }
850
evalBinOpLN(ProgramStateRef state,BinaryOperator::Opcode op,Loc lhs,NonLoc rhs,QualType resultTy)851 SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,
852 BinaryOperator::Opcode op,
853 Loc lhs, NonLoc rhs, QualType resultTy) {
854
855 // Special case: rhs is a zero constant.
856 if (rhs.isZeroConstant())
857 return lhs;
858
859 // Special case: 'rhs' is an integer that has the same width as a pointer and
860 // we are using the integer location in a comparison. Normally this cannot be
861 // triggered, but transfer functions like those for OSCompareAndSwapBarrier32
862 // can generate comparisons that trigger this code.
863 // FIXME: Are all locations guaranteed to have pointer width?
864 if (BinaryOperator::isComparisonOp(op)) {
865 if (Optional<nonloc::ConcreteInt> rhsInt =
866 rhs.getAs<nonloc::ConcreteInt>()) {
867 const llvm::APSInt *x = &rhsInt->getValue();
868 ASTContext &ctx = Context;
869 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
870 // Convert the signedness of the integer (if necessary).
871 if (x->isSigned())
872 x = &getBasicValueFactory().getValue(*x, true);
873
874 return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
875 }
876 }
877 return UnknownVal();
878 }
879
880 // We are dealing with pointer arithmetic.
881
882 // Handle pointer arithmetic on constant values.
883 if (Optional<nonloc::ConcreteInt> rhsInt = rhs.getAs<nonloc::ConcreteInt>()) {
884 if (Optional<loc::ConcreteInt> lhsInt = lhs.getAs<loc::ConcreteInt>()) {
885 const llvm::APSInt &leftI = lhsInt->getValue();
886 assert(leftI.isUnsigned());
887 llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true);
888
889 // Convert the bitwidth of rightI. This should deal with overflow
890 // since we are dealing with concrete values.
891 rightI = rightI.extOrTrunc(leftI.getBitWidth());
892
893 // Offset the increment by the pointer size.
894 llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true);
895 rightI *= Multiplicand;
896
897 // Compute the adjusted pointer.
898 switch (op) {
899 case BO_Add:
900 rightI = leftI + rightI;
901 break;
902 case BO_Sub:
903 rightI = leftI - rightI;
904 break;
905 default:
906 llvm_unreachable("Invalid pointer arithmetic operation");
907 }
908 return loc::ConcreteInt(getBasicValueFactory().getValue(rightI));
909 }
910 }
911
912 // Handle cases where 'lhs' is a region.
913 if (const MemRegion *region = lhs.getAsRegion()) {
914 rhs = convertToArrayIndex(rhs).castAs<NonLoc>();
915 SVal index = UnknownVal();
916 const MemRegion *superR = 0;
917 QualType elementType;
918
919 if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) {
920 assert(op == BO_Add || op == BO_Sub);
921 index = evalBinOpNN(state, op, elemReg->getIndex(), rhs,
922 getArrayIndexType());
923 superR = elemReg->getSuperRegion();
924 elementType = elemReg->getElementType();
925 }
926 else if (isa<SubRegion>(region)) {
927 superR = region;
928 index = rhs;
929 if (resultTy->isAnyPointerType())
930 elementType = resultTy->getPointeeType();
931 }
932
933 if (Optional<NonLoc> indexV = index.getAs<NonLoc>()) {
934 return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV,
935 superR, getContext()));
936 }
937 }
938 return UnknownVal();
939 }
940
getKnownValue(ProgramStateRef state,SVal V)941 const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state,
942 SVal V) {
943 if (V.isUnknownOrUndef())
944 return NULL;
945
946 if (Optional<loc::ConcreteInt> X = V.getAs<loc::ConcreteInt>())
947 return &X->getValue();
948
949 if (Optional<nonloc::ConcreteInt> X = V.getAs<nonloc::ConcreteInt>())
950 return &X->getValue();
951
952 if (SymbolRef Sym = V.getAsSymbol())
953 return state->getConstraintManager().getSymVal(state, Sym);
954
955 // FIXME: Add support for SymExprs.
956 return NULL;
957 }
958