1 //===- SValBuilder.cpp - Basic class for all SValBuilder implementations --===//
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/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/AST/Stmt.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/Analysis/AnalysisDeclContext.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
30 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
31 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
32 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
33 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
35 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
36 #include "llvm/ADT/APSInt.h"
37 #include "llvm/ADT/None.h"
38 #include "llvm/ADT/Optional.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Compiler.h"
44 using namespace clang;
47 //===----------------------------------------------------------------------===//
48 // Basic SVal creation.
49 //===----------------------------------------------------------------------===//
51 void SValBuilder::anchor() {}
53 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
54 if (Loc::isLocType(type))
57 if (type->isIntegralOrEnumerationType())
58 return makeIntVal(0, type);
60 if (type->isArrayType() || type->isRecordType() || type->isVectorType() ||
61 type->isAnyComplexType())
62 return makeCompoundVal(type, BasicVals.getEmptySValList());
64 // FIXME: Handle floats.
68 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
69 const llvm::APSInt& rhs, QualType type) {
70 // The Environment ensures we always get a persistent APSInt in
71 // BasicValueFactory, so we don't need to get the APSInt from
72 // BasicValueFactory again.
74 assert(!Loc::isLocType(type));
75 return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
78 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
79 BinaryOperator::Opcode op, const SymExpr *rhs,
82 assert(!Loc::isLocType(type));
83 return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
86 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
87 const SymExpr *rhs, QualType type) {
89 assert(!Loc::isLocType(type));
90 return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
93 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
94 QualType fromTy, QualType toTy) {
96 assert(!Loc::isLocType(toTy));
97 return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
100 SVal SValBuilder::convertToArrayIndex(SVal val) {
101 if (val.isUnknownOrUndef())
104 // Common case: we have an appropriately sized integer.
105 if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) {
106 const llvm::APSInt& I = CI->getValue();
107 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
111 return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy);
114 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
115 return makeTruthVal(boolean->getValue());
119 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion *region) {
120 QualType T = region->getValueType();
122 if (T->isNullPtrType())
123 return makeZeroVal(T);
125 if (!SymbolManager::canSymbolicate(T))
128 SymbolRef sym = SymMgr.getRegionValueSymbol(region);
130 if (Loc::isLocType(T))
131 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
133 return nonloc::SymbolVal(sym);
136 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
138 const LocationContext *LCtx,
140 QualType T = Ex->getType();
142 if (T->isNullPtrType())
143 return makeZeroVal(T);
145 // Compute the type of the result. If the expression is not an R-value, the
146 // result should be a location.
147 QualType ExType = Ex->getType();
149 T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
151 return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
154 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
156 const LocationContext *LCtx,
159 if (type->isNullPtrType())
160 return makeZeroVal(type);
162 if (!SymbolManager::canSymbolicate(type))
165 SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
167 if (Loc::isLocType(type))
168 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
170 return nonloc::SymbolVal(sym);
173 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
174 const LocationContext *LCtx,
176 unsigned visitCount) {
177 if (type->isNullPtrType())
178 return makeZeroVal(type);
180 if (!SymbolManager::canSymbolicate(type))
183 SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
185 if (Loc::isLocType(type))
186 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
188 return nonloc::SymbolVal(sym);
192 SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
193 const LocationContext *LCtx,
194 unsigned VisitCount) {
195 QualType T = E->getType();
196 assert(Loc::isLocType(T));
197 assert(SymbolManager::canSymbolicate(T));
198 if (T->isNullPtrType())
199 return makeZeroVal(T);
201 SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount);
202 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
205 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
206 const MemRegion *region,
207 const Expr *expr, QualType type,
208 const LocationContext *LCtx,
210 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
213 SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag);
215 if (Loc::isLocType(type))
216 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
218 return nonloc::SymbolVal(sym);
222 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
223 const TypedValueRegion *region) {
224 QualType T = region->getValueType();
226 if (T->isNullPtrType())
227 return makeZeroVal(T);
229 if (!SymbolManager::canSymbolicate(T))
232 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
234 if (Loc::isLocType(T))
235 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
237 return nonloc::SymbolVal(sym);
240 DefinedSVal SValBuilder::getMemberPointer(const DeclaratorDecl *DD) {
241 assert(!DD || isa<CXXMethodDecl>(DD) || isa<FieldDecl>(DD));
243 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(DD)) {
244 // Sema treats pointers to static member functions as have function pointer
245 // type, so return a function pointer for the method.
246 // We don't need to play a similar trick for static member fields
247 // because these are represented as plain VarDecls and not FieldDecls
250 return getFunctionPointer(MD);
253 return nonloc::PointerToMember(DD);
256 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
257 return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
260 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
262 const LocationContext *locContext,
263 unsigned blockCount) {
264 const BlockCodeRegion *BC =
265 MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext());
266 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
268 return loc::MemRegionVal(BD);
271 /// Return a memory region for the 'this' object reference.
272 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
273 const StackFrameContext *SFC) {
274 return loc::MemRegionVal(getRegionManager().
275 getCXXThisRegion(D->getThisType(getContext()), SFC));
278 /// Return a memory region for the 'this' object reference.
279 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
280 const StackFrameContext *SFC) {
281 const Type *T = D->getTypeForDecl();
282 QualType PT = getContext().getPointerType(QualType(T, 0));
283 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
286 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
287 E = E->IgnoreParens();
289 switch (E->getStmtClass()) {
290 // Handle expressions that we treat differently from the AST's constant
292 case Stmt::AddrLabelExprClass:
293 return makeLoc(cast<AddrLabelExpr>(E));
295 case Stmt::CXXScalarValueInitExprClass:
296 case Stmt::ImplicitValueInitExprClass:
297 return makeZeroVal(E->getType());
299 case Stmt::ObjCStringLiteralClass: {
300 const auto *SL = cast<ObjCStringLiteral>(E);
301 return makeLoc(getRegionManager().getObjCStringRegion(SL));
304 case Stmt::StringLiteralClass: {
305 const auto *SL = cast<StringLiteral>(E);
306 return makeLoc(getRegionManager().getStringRegion(SL));
309 // Fast-path some expressions to avoid the overhead of going through the AST's
310 // constant evaluator
311 case Stmt::CharacterLiteralClass: {
312 const auto *C = cast<CharacterLiteral>(E);
313 return makeIntVal(C->getValue(), C->getType());
316 case Stmt::CXXBoolLiteralExprClass:
317 return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
319 case Stmt::TypeTraitExprClass: {
320 const auto *TE = cast<TypeTraitExpr>(E);
321 return makeTruthVal(TE->getValue(), TE->getType());
324 case Stmt::IntegerLiteralClass:
325 return makeIntVal(cast<IntegerLiteral>(E));
327 case Stmt::ObjCBoolLiteralExprClass:
328 return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
330 case Stmt::CXXNullPtrLiteralExprClass:
333 case Stmt::CStyleCastExprClass:
334 case Stmt::CXXFunctionalCastExprClass:
335 case Stmt::CXXConstCastExprClass:
336 case Stmt::CXXReinterpretCastExprClass:
337 case Stmt::CXXStaticCastExprClass:
338 case Stmt::ImplicitCastExprClass: {
339 const auto *CE = cast<CastExpr>(E);
340 switch (CE->getCastKind()) {
343 case CK_ArrayToPointerDecay:
344 case CK_IntegralToPointer:
347 const Expr *SE = CE->getSubExpr();
348 Optional<SVal> Val = getConstantVal(SE);
351 return evalCast(*Val, CE->getType(), SE->getType());
358 // If we don't have a special case, fall back to the AST's constant evaluator.
360 // Don't try to come up with a value for materialized temporaries.
364 ASTContext &Ctx = getContext();
366 if (E->EvaluateAsInt(Result, Ctx))
367 return makeIntVal(Result);
369 if (Loc::isLocType(E->getType()))
370 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
378 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
379 BinaryOperator::Opcode Op,
380 NonLoc LHS, NonLoc RHS,
382 const SymExpr *symLHS = LHS.getAsSymExpr();
383 const SymExpr *symRHS = RHS.getAsSymExpr();
385 // TODO: When the Max Complexity is reached, we should conjure a symbol
386 // instead of generating an Unknown value and propagate the taint info to it.
387 const unsigned MaxComp = StateMgr.getOwningEngine()
388 ->getAnalysisManager()
389 .options.getMaxSymbolComplexity();
391 if (symLHS && symRHS &&
392 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
393 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
395 if (symLHS && symLHS->computeComplexity() < MaxComp)
396 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
397 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
399 if (symRHS && symRHS->computeComplexity() < MaxComp)
400 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
401 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
406 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
407 SVal lhs, SVal rhs, QualType type) {
408 if (lhs.isUndef() || rhs.isUndef())
409 return UndefinedVal();
411 if (lhs.isUnknown() || rhs.isUnknown())
414 if (lhs.getAs<nonloc::LazyCompoundVal>() ||
415 rhs.getAs<nonloc::LazyCompoundVal>()) {
419 if (Optional<Loc> LV = lhs.getAs<Loc>()) {
420 if (Optional<Loc> RV = rhs.getAs<Loc>())
421 return evalBinOpLL(state, op, *LV, *RV, type);
423 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
426 if (Optional<Loc> RV = rhs.getAs<Loc>()) {
427 // Support pointer arithmetic where the addend is on the left
428 // and the pointer on the right.
429 assert(op == BO_Add);
431 // Commute the operands.
432 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
435 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
439 ConditionTruthVal SValBuilder::areEqual(ProgramStateRef state, SVal lhs,
441 return state->isNonNull(evalEQ(state, lhs, rhs));
444 SVal SValBuilder::evalEQ(ProgramStateRef state, SVal lhs, SVal rhs) {
445 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType());
448 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
449 DefinedOrUnknownSVal lhs,
450 DefinedOrUnknownSVal rhs) {
451 return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs))
452 .castAs<DefinedOrUnknownSVal>();
455 /// Recursively check if the pointer types are equal modulo const, volatile,
456 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
457 /// Assumes the input types are canonical.
458 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
460 while (Context.UnwrapSimilarTypes(ToTy, FromTy)) {
461 Qualifiers Quals1, Quals2;
462 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
463 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
465 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
466 // spaces) are identical.
467 Quals1.removeCVRQualifiers();
468 Quals2.removeCVRQualifiers();
469 if (Quals1 != Quals2)
473 // If we are casting to void, the 'From' value can be used to represent the
476 // FIXME: Doing this after unwrapping the types doesn't make any sense. A
477 // cast from 'int**' to 'void**' is not special in the way that a cast from
478 // 'int*' to 'void*' is.
479 if (ToTy->isVoidType())
488 // Handles casts of type CK_IntegralCast.
489 // At the moment, this function will redirect to evalCast, except when the range
490 // of the original value is known to be greater than the max of the target type.
491 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
492 QualType castTy, QualType originalTy) {
493 // No truncations if target type is big enough.
494 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
495 return evalCast(val, castTy, originalTy);
497 const SymExpr *se = val.getAsSymbolicExpression();
498 if (!se) // Let evalCast handle non symbolic expressions.
499 return evalCast(val, castTy, originalTy);
501 // Find the maximum value of the target type.
502 APSIntType ToType(getContext().getTypeSize(castTy),
503 castTy->isUnsignedIntegerType());
504 llvm::APSInt ToTypeMax = ToType.getMaxValue();
505 NonLoc ToTypeMaxVal =
506 makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
507 : ToTypeMax.getSExtValue(),
510 // Check the range of the symbol being casted against the maximum value of the
512 NonLoc FromVal = val.castAs<NonLoc>();
513 QualType CmpTy = getConditionType();
515 evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
516 ProgramStateRef IsNotTruncated, IsTruncated;
517 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
518 if (!IsNotTruncated && IsTruncated) {
519 // Symbol is truncated so we evaluate it as a cast.
520 NonLoc CastVal = makeNonLoc(se, originalTy, castTy);
523 return evalCast(val, castTy, originalTy);
526 // FIXME: should rewrite according to the cast kind.
527 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
528 castTy = Context.getCanonicalType(castTy);
529 originalTy = Context.getCanonicalType(originalTy);
530 if (val.isUnknownOrUndef() || castTy == originalTy)
533 if (castTy->isBooleanType()) {
534 if (val.isUnknownOrUndef())
536 if (val.isConstant())
537 return makeTruthVal(!val.isZeroConstant(), castTy);
538 if (!Loc::isLocType(originalTy) &&
539 !originalTy->isIntegralOrEnumerationType() &&
540 !originalTy->isMemberPointerType())
542 if (SymbolRef Sym = val.getAsSymbol(true)) {
543 BasicValueFactory &BVF = getBasicValueFactory();
544 // FIXME: If we had a state here, we could see if the symbol is known to
545 // be zero, but we don't.
546 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy);
548 // Loc values are not always true, they could be weakly linked functions.
549 if (Optional<Loc> L = val.getAs<Loc>())
550 return evalCastFromLoc(*L, castTy);
552 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
553 return evalCastFromLoc(L, castTy);
556 // For const casts, casts to void, just propagate the value.
557 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
558 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
559 Context.getPointerType(originalTy)))
562 // Check for casts from pointers to integers.
563 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
564 return evalCastFromLoc(val.castAs<Loc>(), castTy);
566 // Check for casts from integers to pointers.
567 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
568 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
569 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
570 StoreManager &storeMgr = StateMgr.getStoreManager();
571 R = storeMgr.castRegion(R, castTy);
572 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
576 return dispatchCast(val, castTy);
579 // Just pass through function and block pointers.
580 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
581 assert(Loc::isLocType(castTy));
585 // Check for casts from array type to another type.
586 if (const auto *arrayT =
587 dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
588 // We will always decay to a pointer.
589 QualType elemTy = arrayT->getElementType();
590 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
592 // Are we casting from an array to a pointer? If so just pass on
593 // the decayed value.
594 if (castTy->isPointerType() || castTy->isReferenceType())
597 // Are we casting from an array to an integer? If so, cast the decayed
598 // pointer value to an integer.
599 assert(castTy->isIntegralOrEnumerationType());
601 // FIXME: Keep these here for now in case we decide soon that we
602 // need the original decayed type.
603 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
604 // QualType pointerTy = C.getPointerType(elemTy);
605 return evalCastFromLoc(val.castAs<Loc>(), castTy);
608 // Check for casts from a region to a specific type.
609 if (const MemRegion *R = val.getAsRegion()) {
610 // Handle other casts of locations to integers.
611 if (castTy->isIntegralOrEnumerationType())
612 return evalCastFromLoc(loc::MemRegionVal(R), castTy);
614 // FIXME: We should handle the case where we strip off view layers to get
615 // to a desugared type.
616 if (!Loc::isLocType(castTy)) {
617 // FIXME: There can be gross cases where one casts the result of a function
618 // (that returns a pointer) to some other value that happens to fit
619 // within that pointer value. We currently have no good way to
620 // model such operations. When this happens, the underlying operation
621 // is that the caller is reasoning about bits. Conceptually we are
622 // layering a "view" of a location on top of those bits. Perhaps
623 // we need to be more lazy about mutual possible views, even on an
624 // SVal? This may be necessary for bit-level reasoning as well.
628 // We get a symbolic function pointer for a dereference of a function
629 // pointer, but it is of function type. Example:
632 // void (*my_func)(int * x);
637 // int f1_a(struct FPRec* foo) {
639 // (*foo->my_func)(&x);
640 // return bar(x)+1; // no-warning
643 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
644 originalTy->isBlockPointerType() || castTy->isReferenceType());
646 StoreManager &storeMgr = StateMgr.getStoreManager();
648 // Delegate to store manager to get the result of casting a region to a
649 // different type. If the MemRegion* returned is NULL, this expression
650 // Evaluates to UnknownVal.
651 R = storeMgr.castRegion(R, castTy);
652 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
655 return dispatchCast(val, castTy);