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 if (type->isArrayType() || type->isRecordType() || type->isVectorType() ||
40 type->isAnyComplexType())
41 return makeCompoundVal(type, BasicVals.getEmptySValList());
43 // FIXME: Handle floats.
47 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
48 const llvm::APSInt& rhs, QualType type) {
49 // The Environment ensures we always get a persistent APSInt in
50 // BasicValueFactory, so we don't need to get the APSInt from
51 // BasicValueFactory again.
53 assert(!Loc::isLocType(type));
54 return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
57 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
58 BinaryOperator::Opcode op, const SymExpr *rhs,
61 assert(!Loc::isLocType(type));
62 return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
65 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
66 const SymExpr *rhs, QualType type) {
68 assert(!Loc::isLocType(type));
69 return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
72 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
73 QualType fromTy, QualType toTy) {
75 assert(!Loc::isLocType(toTy));
76 return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
79 SVal SValBuilder::convertToArrayIndex(SVal val) {
80 if (val.isUnknownOrUndef())
83 // Common case: we have an appropriately sized integer.
84 if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) {
85 const llvm::APSInt& I = CI->getValue();
86 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
90 return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy);
93 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
94 return makeTruthVal(boolean->getValue());
98 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
99 QualType T = region->getValueType();
101 if (T->isNullPtrType())
102 return makeZeroVal(T);
104 if (!SymbolManager::canSymbolicate(T))
107 SymbolRef sym = SymMgr.getRegionValueSymbol(region);
109 if (Loc::isLocType(T))
110 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
112 return nonloc::SymbolVal(sym);
115 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
117 const LocationContext *LCtx,
119 QualType T = Ex->getType();
121 if (T->isNullPtrType())
122 return makeZeroVal(T);
124 // Compute the type of the result. If the expression is not an R-value, the
125 // result should be a location.
126 QualType ExType = Ex->getType();
128 T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
130 return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
133 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
135 const LocationContext *LCtx,
138 if (type->isNullPtrType())
139 return makeZeroVal(type);
141 if (!SymbolManager::canSymbolicate(type))
144 SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
146 if (Loc::isLocType(type))
147 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
149 return nonloc::SymbolVal(sym);
153 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
154 const LocationContext *LCtx,
156 unsigned visitCount) {
157 if (type->isNullPtrType())
158 return makeZeroVal(type);
160 if (!SymbolManager::canSymbolicate(type))
163 SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
165 if (Loc::isLocType(type))
166 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
168 return nonloc::SymbolVal(sym);
172 SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
173 const LocationContext *LCtx,
174 unsigned VisitCount) {
175 QualType T = E->getType();
176 assert(Loc::isLocType(T));
177 assert(SymbolManager::canSymbolicate(T));
178 if (T->isNullPtrType())
179 return makeZeroVal(T);
181 SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount);
182 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
185 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
186 const MemRegion *region,
187 const Expr *expr, QualType type,
188 const LocationContext *LCtx,
190 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
193 SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag);
195 if (Loc::isLocType(type))
196 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
198 return nonloc::SymbolVal(sym);
202 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
203 const TypedValueRegion *region) {
204 QualType T = region->getValueType();
206 if (T->isNullPtrType())
207 return makeZeroVal(T);
209 if (!SymbolManager::canSymbolicate(T))
212 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
214 if (Loc::isLocType(T))
215 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
217 return nonloc::SymbolVal(sym);
220 DefinedSVal SValBuilder::getMemberPointer(const DeclaratorDecl* DD) {
221 assert(!DD || isa<CXXMethodDecl>(DD) || isa<FieldDecl>(DD));
223 if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(DD)) {
224 // Sema treats pointers to static member functions as have function pointer
225 // type, so return a function pointer for the method.
226 // We don't need to play a similar trick for static member fields
227 // because these are represented as plain VarDecls and not FieldDecls
230 return getFunctionPointer(MD);
233 return nonloc::PointerToMember(DD);
236 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
237 return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
240 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
242 const LocationContext *locContext,
243 unsigned blockCount) {
244 const BlockCodeRegion *BC =
245 MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext());
246 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
248 return loc::MemRegionVal(BD);
251 /// Return a memory region for the 'this' object reference.
252 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
253 const StackFrameContext *SFC) {
254 return loc::MemRegionVal(getRegionManager().
255 getCXXThisRegion(D->getThisType(getContext()), SFC));
258 /// Return a memory region for the 'this' object reference.
259 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
260 const StackFrameContext *SFC) {
261 const Type *T = D->getTypeForDecl();
262 QualType PT = getContext().getPointerType(QualType(T, 0));
263 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
266 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
267 E = E->IgnoreParens();
269 switch (E->getStmtClass()) {
270 // Handle expressions that we treat differently from the AST's constant
272 case Stmt::AddrLabelExprClass:
273 return makeLoc(cast<AddrLabelExpr>(E));
275 case Stmt::CXXScalarValueInitExprClass:
276 case Stmt::ImplicitValueInitExprClass:
277 return makeZeroVal(E->getType());
279 case Stmt::ObjCStringLiteralClass: {
280 const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E);
281 return makeLoc(getRegionManager().getObjCStringRegion(SL));
284 case Stmt::StringLiteralClass: {
285 const StringLiteral *SL = cast<StringLiteral>(E);
286 return makeLoc(getRegionManager().getStringRegion(SL));
289 // Fast-path some expressions to avoid the overhead of going through the AST's
290 // constant evaluator
291 case Stmt::CharacterLiteralClass: {
292 const CharacterLiteral *C = cast<CharacterLiteral>(E);
293 return makeIntVal(C->getValue(), C->getType());
296 case Stmt::CXXBoolLiteralExprClass:
297 return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
299 case Stmt::TypeTraitExprClass: {
300 const TypeTraitExpr *TE = cast<TypeTraitExpr>(E);
301 return makeTruthVal(TE->getValue(), TE->getType());
304 case Stmt::IntegerLiteralClass:
305 return makeIntVal(cast<IntegerLiteral>(E));
307 case Stmt::ObjCBoolLiteralExprClass:
308 return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
310 case Stmt::CXXNullPtrLiteralExprClass:
313 case Stmt::ImplicitCastExprClass: {
314 const CastExpr *CE = cast<CastExpr>(E);
315 switch (CE->getCastKind()) {
318 case CK_ArrayToPointerDecay:
320 const Expr *SE = CE->getSubExpr();
321 Optional<SVal> Val = getConstantVal(SE);
324 return evalCast(*Val, CE->getType(), SE->getType());
331 // If we don't have a special case, fall back to the AST's constant evaluator.
333 // Don't try to come up with a value for materialized temporaries.
337 ASTContext &Ctx = getContext();
339 if (E->EvaluateAsInt(Result, Ctx))
340 return makeIntVal(Result);
342 if (Loc::isLocType(E->getType()))
343 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
351 //===----------------------------------------------------------------------===//
353 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
354 BinaryOperator::Opcode Op,
355 NonLoc LHS, NonLoc RHS,
357 if (!State->isTainted(RHS) && !State->isTainted(LHS))
360 const SymExpr *symLHS = LHS.getAsSymExpr();
361 const SymExpr *symRHS = RHS.getAsSymExpr();
362 // TODO: When the Max Complexity is reached, we should conjure a symbol
363 // instead of generating an Unknown value and propagate the taint info to it.
364 const unsigned MaxComp = 10000; // 100000 28X
366 if (symLHS && symRHS &&
367 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
368 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
370 if (symLHS && symLHS->computeComplexity() < MaxComp)
371 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
372 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
374 if (symRHS && symRHS->computeComplexity() < MaxComp)
375 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
376 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
382 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
383 SVal lhs, SVal rhs, QualType type) {
385 if (lhs.isUndef() || rhs.isUndef())
386 return UndefinedVal();
388 if (lhs.isUnknown() || rhs.isUnknown())
391 if (lhs.getAs<nonloc::LazyCompoundVal>() ||
392 rhs.getAs<nonloc::LazyCompoundVal>()) {
396 if (Optional<Loc> LV = lhs.getAs<Loc>()) {
397 if (Optional<Loc> RV = rhs.getAs<Loc>())
398 return evalBinOpLL(state, op, *LV, *RV, type);
400 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
403 if (Optional<Loc> RV = rhs.getAs<Loc>()) {
404 // Support pointer arithmetic where the addend is on the left
405 // and the pointer on the right.
406 assert(op == BO_Add);
408 // Commute the operands.
409 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
412 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
416 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
417 DefinedOrUnknownSVal lhs,
418 DefinedOrUnknownSVal rhs) {
419 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType())
420 .castAs<DefinedOrUnknownSVal>();
423 /// Recursively check if the pointer types are equal modulo const, volatile,
424 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
425 /// Assumes the input types are canonical.
426 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
428 while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) {
429 Qualifiers Quals1, Quals2;
430 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
431 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
433 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
434 // spaces) are identical.
435 Quals1.removeCVRQualifiers();
436 Quals2.removeCVRQualifiers();
437 if (Quals1 != Quals2)
441 // If we are casting to void, the 'From' value can be used to represent the
443 if (ToTy->isVoidType())
452 // Handles casts of type CK_IntegralCast.
453 // At the moment, this function will redirect to evalCast, except when the range
454 // of the original value is known to be greater than the max of the target type.
455 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
456 QualType castTy, QualType originalTy) {
458 // No truncations if target type is big enough.
459 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
460 return evalCast(val, castTy, originalTy);
462 const SymExpr *se = val.getAsSymbolicExpression();
463 if (!se) // Let evalCast handle non symbolic expressions.
464 return evalCast(val, castTy, originalTy);
466 // Find the maximum value of the target type.
467 APSIntType ToType(getContext().getTypeSize(castTy),
468 castTy->isUnsignedIntegerType());
469 llvm::APSInt ToTypeMax = ToType.getMaxValue();
470 NonLoc ToTypeMaxVal =
471 makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
472 : ToTypeMax.getSExtValue(),
475 // Check the range of the symbol being casted against the maximum value of the
477 NonLoc FromVal = val.castAs<NonLoc>();
478 QualType CmpTy = getConditionType();
480 evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
481 ProgramStateRef IsNotTruncated, IsTruncated;
482 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
483 if (!IsNotTruncated && IsTruncated) {
484 // Symbol is truncated so we evaluate it as a cast.
485 NonLoc CastVal = makeNonLoc(se, originalTy, castTy);
488 return evalCast(val, castTy, originalTy);
491 // FIXME: should rewrite according to the cast kind.
492 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
493 castTy = Context.getCanonicalType(castTy);
494 originalTy = Context.getCanonicalType(originalTy);
495 if (val.isUnknownOrUndef() || castTy == originalTy)
498 if (castTy->isBooleanType()) {
499 if (val.isUnknownOrUndef())
501 if (val.isConstant())
502 return makeTruthVal(!val.isZeroConstant(), castTy);
503 if (!Loc::isLocType(originalTy) &&
504 !originalTy->isIntegralOrEnumerationType() &&
505 !originalTy->isMemberPointerType())
507 if (SymbolRef Sym = val.getAsSymbol(true)) {
508 BasicValueFactory &BVF = getBasicValueFactory();
509 // FIXME: If we had a state here, we could see if the symbol is known to
510 // be zero, but we don't.
511 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy);
513 // Loc values are not always true, they could be weakly linked functions.
514 if (Optional<Loc> L = val.getAs<Loc>())
515 return evalCastFromLoc(*L, castTy);
517 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
518 return evalCastFromLoc(L, castTy);
521 // For const casts, casts to void, just propagate the value.
522 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
523 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
524 Context.getPointerType(originalTy)))
527 // Check for casts from pointers to integers.
528 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
529 return evalCastFromLoc(val.castAs<Loc>(), castTy);
531 // Check for casts from integers to pointers.
532 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
533 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
534 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
535 StoreManager &storeMgr = StateMgr.getStoreManager();
536 R = storeMgr.castRegion(R, castTy);
537 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
541 return dispatchCast(val, castTy);
544 // Just pass through function and block pointers.
545 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
546 assert(Loc::isLocType(castTy));
550 // Check for casts from array type to another type.
551 if (const ArrayType *arrayT =
552 dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
553 // We will always decay to a pointer.
554 QualType elemTy = arrayT->getElementType();
555 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
557 // Are we casting from an array to a pointer? If so just pass on
558 // the decayed value.
559 if (castTy->isPointerType() || castTy->isReferenceType())
562 // Are we casting from an array to an integer? If so, cast the decayed
563 // pointer value to an integer.
564 assert(castTy->isIntegralOrEnumerationType());
566 // FIXME: Keep these here for now in case we decide soon that we
567 // need the original decayed type.
568 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
569 // QualType pointerTy = C.getPointerType(elemTy);
570 return evalCastFromLoc(val.castAs<Loc>(), castTy);
573 // Check for casts from a region to a specific type.
574 if (const MemRegion *R = val.getAsRegion()) {
575 // Handle other casts of locations to integers.
576 if (castTy->isIntegralOrEnumerationType())
577 return evalCastFromLoc(loc::MemRegionVal(R), castTy);
579 // FIXME: We should handle the case where we strip off view layers to get
580 // to a desugared type.
581 if (!Loc::isLocType(castTy)) {
582 // FIXME: There can be gross cases where one casts the result of a function
583 // (that returns a pointer) to some other value that happens to fit
584 // within that pointer value. We currently have no good way to
585 // model such operations. When this happens, the underlying operation
586 // is that the caller is reasoning about bits. Conceptually we are
587 // layering a "view" of a location on top of those bits. Perhaps
588 // we need to be more lazy about mutual possible views, even on an
589 // SVal? This may be necessary for bit-level reasoning as well.
593 // We get a symbolic function pointer for a dereference of a function
594 // pointer, but it is of function type. Example:
597 // void (*my_func)(int * x);
602 // int f1_a(struct FPRec* foo) {
604 // (*foo->my_func)(&x);
605 // return bar(x)+1; // no-warning
608 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
609 originalTy->isBlockPointerType() || castTy->isReferenceType());
611 StoreManager &storeMgr = StateMgr.getStoreManager();
613 // Delegate to store manager to get the result of casting a region to a
614 // different type. If the MemRegion* returned is NULL, this expression
615 // Evaluates to UnknownVal.
616 R = storeMgr.castRegion(R, castTy);
617 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
620 return dispatchCast(val, castTy);