1 // SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines SValBuilder, the base class for all (complete) SValBuilder
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
23 using namespace clang;
26 //===----------------------------------------------------------------------===//
27 // Basic SVal creation.
28 //===----------------------------------------------------------------------===//
30 void SValBuilder::anchor() { }
32 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
33 if (Loc::isLocType(type))
36 if (type->isIntegralOrEnumerationType())
37 return makeIntVal(0, type);
39 // FIXME: Handle floats.
40 // FIXME: Handle structs.
44 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
45 const llvm::APSInt& rhs, QualType type) {
46 // The Environment ensures we always get a persistent APSInt in
47 // BasicValueFactory, so we don't need to get the APSInt from
48 // BasicValueFactory again.
50 assert(!Loc::isLocType(type));
51 return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
54 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
55 BinaryOperator::Opcode op, const SymExpr *rhs,
58 assert(!Loc::isLocType(type));
59 return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
62 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
63 const SymExpr *rhs, QualType type) {
65 assert(!Loc::isLocType(type));
66 return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
69 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
70 QualType fromTy, QualType toTy) {
72 assert(!Loc::isLocType(toTy));
73 return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
76 SVal SValBuilder::convertToArrayIndex(SVal val) {
77 if (val.isUnknownOrUndef())
80 // Common case: we have an appropriately sized integer.
81 if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) {
82 const llvm::APSInt& I = CI->getValue();
83 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
87 return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy);
90 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
91 return makeTruthVal(boolean->getValue());
95 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
96 QualType T = region->getValueType();
98 if (T->isNullPtrType())
99 return makeZeroVal(T);
101 if (!SymbolManager::canSymbolicate(T))
104 SymbolRef sym = SymMgr.getRegionValueSymbol(region);
106 if (Loc::isLocType(T))
107 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
109 return nonloc::SymbolVal(sym);
112 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
114 const LocationContext *LCtx,
116 QualType T = Ex->getType();
118 if (T->isNullPtrType())
119 return makeZeroVal(T);
121 // Compute the type of the result. If the expression is not an R-value, the
122 // result should be a location.
123 QualType ExType = Ex->getType();
125 T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
127 return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
130 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
132 const LocationContext *LCtx,
135 if (type->isNullPtrType())
136 return makeZeroVal(type);
138 if (!SymbolManager::canSymbolicate(type))
141 SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
143 if (Loc::isLocType(type))
144 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
146 return nonloc::SymbolVal(sym);
150 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
151 const LocationContext *LCtx,
153 unsigned visitCount) {
154 if (type->isNullPtrType())
155 return makeZeroVal(type);
157 if (!SymbolManager::canSymbolicate(type))
160 SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
162 if (Loc::isLocType(type))
163 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
165 return nonloc::SymbolVal(sym);
169 SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
170 const LocationContext *LCtx,
171 unsigned VisitCount) {
172 QualType T = E->getType();
173 assert(Loc::isLocType(T));
174 assert(SymbolManager::canSymbolicate(T));
175 if (T->isNullPtrType())
176 return makeZeroVal(T);
178 SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount);
179 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
182 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
183 const MemRegion *region,
184 const Expr *expr, QualType type,
186 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
189 SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag);
191 if (Loc::isLocType(type))
192 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
194 return nonloc::SymbolVal(sym);
198 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
199 const TypedValueRegion *region) {
200 QualType T = region->getValueType();
202 if (T->isNullPtrType())
203 return makeZeroVal(T);
205 if (!SymbolManager::canSymbolicate(T))
208 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
210 if (Loc::isLocType(T))
211 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
213 return nonloc::SymbolVal(sym);
216 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
217 return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
220 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
222 const LocationContext *locContext,
223 unsigned blockCount) {
224 const BlockCodeRegion *BC =
225 MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext());
226 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
228 return loc::MemRegionVal(BD);
231 /// Return a memory region for the 'this' object reference.
232 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
233 const StackFrameContext *SFC) {
234 return loc::MemRegionVal(getRegionManager().
235 getCXXThisRegion(D->getThisType(getContext()), SFC));
238 /// Return a memory region for the 'this' object reference.
239 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
240 const StackFrameContext *SFC) {
241 const Type *T = D->getTypeForDecl();
242 QualType PT = getContext().getPointerType(QualType(T, 0));
243 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
246 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
247 E = E->IgnoreParens();
249 switch (E->getStmtClass()) {
250 // Handle expressions that we treat differently from the AST's constant
252 case Stmt::AddrLabelExprClass:
253 return makeLoc(cast<AddrLabelExpr>(E));
255 case Stmt::CXXScalarValueInitExprClass:
256 case Stmt::ImplicitValueInitExprClass:
257 return makeZeroVal(E->getType());
259 case Stmt::ObjCStringLiteralClass: {
260 const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E);
261 return makeLoc(getRegionManager().getObjCStringRegion(SL));
264 case Stmt::StringLiteralClass: {
265 const StringLiteral *SL = cast<StringLiteral>(E);
266 return makeLoc(getRegionManager().getStringRegion(SL));
269 // Fast-path some expressions to avoid the overhead of going through the AST's
270 // constant evaluator
271 case Stmt::CharacterLiteralClass: {
272 const CharacterLiteral *C = cast<CharacterLiteral>(E);
273 return makeIntVal(C->getValue(), C->getType());
276 case Stmt::CXXBoolLiteralExprClass:
277 return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
279 case Stmt::TypeTraitExprClass: {
280 const TypeTraitExpr *TE = cast<TypeTraitExpr>(E);
281 return makeTruthVal(TE->getValue(), TE->getType());
284 case Stmt::IntegerLiteralClass:
285 return makeIntVal(cast<IntegerLiteral>(E));
287 case Stmt::ObjCBoolLiteralExprClass:
288 return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
290 case Stmt::CXXNullPtrLiteralExprClass:
293 case Stmt::ImplicitCastExprClass: {
294 const CastExpr *CE = cast<CastExpr>(E);
295 switch (CE->getCastKind()) {
298 case CK_ArrayToPointerDecay:
300 const Expr *SE = CE->getSubExpr();
301 Optional<SVal> Val = getConstantVal(SE);
304 return evalCast(*Val, CE->getType(), SE->getType());
310 // If we don't have a special case, fall back to the AST's constant evaluator.
312 // Don't try to come up with a value for materialized temporaries.
316 ASTContext &Ctx = getContext();
318 if (E->EvaluateAsInt(Result, Ctx))
319 return makeIntVal(Result);
321 if (Loc::isLocType(E->getType()))
322 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
330 //===----------------------------------------------------------------------===//
332 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
333 BinaryOperator::Opcode Op,
334 NonLoc LHS, NonLoc RHS,
336 if (!State->isTainted(RHS) && !State->isTainted(LHS))
339 const SymExpr *symLHS = LHS.getAsSymExpr();
340 const SymExpr *symRHS = RHS.getAsSymExpr();
341 // TODO: When the Max Complexity is reached, we should conjure a symbol
342 // instead of generating an Unknown value and propagate the taint info to it.
343 const unsigned MaxComp = 10000; // 100000 28X
345 if (symLHS && symRHS &&
346 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
347 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
349 if (symLHS && symLHS->computeComplexity() < MaxComp)
350 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
351 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
353 if (symRHS && symRHS->computeComplexity() < MaxComp)
354 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
355 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
361 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
362 SVal lhs, SVal rhs, QualType type) {
364 if (lhs.isUndef() || rhs.isUndef())
365 return UndefinedVal();
367 if (lhs.isUnknown() || rhs.isUnknown())
370 if (Optional<Loc> LV = lhs.getAs<Loc>()) {
371 if (Optional<Loc> RV = rhs.getAs<Loc>())
372 return evalBinOpLL(state, op, *LV, *RV, type);
374 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
377 if (Optional<Loc> RV = rhs.getAs<Loc>()) {
378 // Support pointer arithmetic where the addend is on the left
379 // and the pointer on the right.
380 assert(op == BO_Add);
382 // Commute the operands.
383 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
386 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
390 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
391 DefinedOrUnknownSVal lhs,
392 DefinedOrUnknownSVal rhs) {
393 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType())
394 .castAs<DefinedOrUnknownSVal>();
397 /// Recursively check if the pointer types are equal modulo const, volatile,
398 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
399 /// Assumes the input types are canonical.
400 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
402 while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) {
403 Qualifiers Quals1, Quals2;
404 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
405 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
407 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
408 // spaces) are identical.
409 Quals1.removeCVRQualifiers();
410 Quals2.removeCVRQualifiers();
411 if (Quals1 != Quals2)
415 // If we are casting to void, the 'From' value can be used to represent the
417 if (ToTy->isVoidType())
426 // Handles casts of type CK_IntegralCast.
427 // At the moment, this function will redirect to evalCast, except when the range
428 // of the original value is known to be greater than the max of the target type.
429 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
430 QualType castTy, QualType originalTy) {
432 // No truncations if target type is big enough.
433 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
434 return evalCast(val, castTy, originalTy);
436 const SymExpr *se = val.getAsSymbolicExpression();
437 if (!se) // Let evalCast handle non symbolic expressions.
438 return evalCast(val, castTy, originalTy);
440 // Find the maximum value of the target type.
441 APSIntType ToType(getContext().getTypeSize(castTy),
442 castTy->isUnsignedIntegerType());
443 llvm::APSInt ToTypeMax = ToType.getMaxValue();
444 NonLoc ToTypeMaxVal =
445 makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
446 : ToTypeMax.getSExtValue(),
449 // Check the range of the symbol being casted against the maximum value of the
451 NonLoc FromVal = val.castAs<NonLoc>();
452 QualType CmpTy = getConditionType();
454 evalBinOpNN(state, BO_LT, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
455 ProgramStateRef IsNotTruncated, IsTruncated;
456 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
457 if (!IsNotTruncated && IsTruncated) {
458 // Symbol is truncated so we evaluate it as a cast.
459 NonLoc CastVal = makeNonLoc(se, originalTy, castTy);
462 return evalCast(val, castTy, originalTy);
465 // FIXME: should rewrite according to the cast kind.
466 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
467 castTy = Context.getCanonicalType(castTy);
468 originalTy = Context.getCanonicalType(originalTy);
469 if (val.isUnknownOrUndef() || castTy == originalTy)
472 if (castTy->isBooleanType()) {
473 if (val.isUnknownOrUndef())
475 if (val.isConstant())
476 return makeTruthVal(!val.isZeroConstant(), castTy);
477 if (!Loc::isLocType(originalTy) &&
478 !originalTy->isIntegralOrEnumerationType() &&
479 !originalTy->isMemberPointerType())
481 if (SymbolRef Sym = val.getAsSymbol(true)) {
482 BasicValueFactory &BVF = getBasicValueFactory();
483 // FIXME: If we had a state here, we could see if the symbol is known to
484 // be zero, but we don't.
485 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy);
487 // Loc values are not always true, they could be weakly linked functions.
488 if (Optional<Loc> L = val.getAs<Loc>())
489 return evalCastFromLoc(*L, castTy);
491 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
492 return evalCastFromLoc(L, castTy);
495 // For const casts, casts to void, just propagate the value.
496 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
497 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
498 Context.getPointerType(originalTy)))
501 // Check for casts from pointers to integers.
502 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
503 return evalCastFromLoc(val.castAs<Loc>(), castTy);
505 // Check for casts from integers to pointers.
506 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
507 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
508 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
509 StoreManager &storeMgr = StateMgr.getStoreManager();
510 R = storeMgr.castRegion(R, castTy);
511 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
515 return dispatchCast(val, castTy);
518 // Just pass through function and block pointers.
519 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
520 assert(Loc::isLocType(castTy));
524 // Check for casts from array type to another type.
525 if (const ArrayType *arrayT =
526 dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
527 // We will always decay to a pointer.
528 QualType elemTy = arrayT->getElementType();
529 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
531 // Are we casting from an array to a pointer? If so just pass on
532 // the decayed value.
533 if (castTy->isPointerType() || castTy->isReferenceType())
536 // Are we casting from an array to an integer? If so, cast the decayed
537 // pointer value to an integer.
538 assert(castTy->isIntegralOrEnumerationType());
540 // FIXME: Keep these here for now in case we decide soon that we
541 // need the original decayed type.
542 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
543 // QualType pointerTy = C.getPointerType(elemTy);
544 return evalCastFromLoc(val.castAs<Loc>(), castTy);
547 // Check for casts from a region to a specific type.
548 if (const MemRegion *R = val.getAsRegion()) {
549 // Handle other casts of locations to integers.
550 if (castTy->isIntegralOrEnumerationType())
551 return evalCastFromLoc(loc::MemRegionVal(R), castTy);
553 // FIXME: We should handle the case where we strip off view layers to get
554 // to a desugared type.
555 if (!Loc::isLocType(castTy)) {
556 // FIXME: There can be gross cases where one casts the result of a function
557 // (that returns a pointer) to some other value that happens to fit
558 // within that pointer value. We currently have no good way to
559 // model such operations. When this happens, the underlying operation
560 // is that the caller is reasoning about bits. Conceptually we are
561 // layering a "view" of a location on top of those bits. Perhaps
562 // we need to be more lazy about mutual possible views, even on an
563 // SVal? This may be necessary for bit-level reasoning as well.
567 // We get a symbolic function pointer for a dereference of a function
568 // pointer, but it is of function type. Example:
571 // void (*my_func)(int * x);
576 // int f1_a(struct FPRec* foo) {
578 // (*foo->my_func)(&x);
579 // return bar(x)+1; // no-warning
582 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
583 originalTy->isBlockPointerType() || castTy->isReferenceType());
585 StoreManager &storeMgr = StateMgr.getStoreManager();
587 // Delegate to store manager to get the result of casting a region to a
588 // different type. If the MemRegion* returned is NULL, this expression
589 // Evaluates to UnknownVal.
590 R = storeMgr.castRegion(R, castTy);
591 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
594 return dispatchCast(val, castTy);