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());
330 // If we don't have a special case, fall back to the AST's constant evaluator.
332 // Don't try to come up with a value for materialized temporaries.
336 ASTContext &Ctx = getContext();
338 if (E->EvaluateAsInt(Result, Ctx))
339 return makeIntVal(Result);
341 if (Loc::isLocType(E->getType()))
342 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
350 //===----------------------------------------------------------------------===//
352 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
353 BinaryOperator::Opcode Op,
354 NonLoc LHS, NonLoc RHS,
356 if (!State->isTainted(RHS) && !State->isTainted(LHS))
359 const SymExpr *symLHS = LHS.getAsSymExpr();
360 const SymExpr *symRHS = RHS.getAsSymExpr();
361 // TODO: When the Max Complexity is reached, we should conjure a symbol
362 // instead of generating an Unknown value and propagate the taint info to it.
363 const unsigned MaxComp = 10000; // 100000 28X
365 if (symLHS && symRHS &&
366 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
367 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
369 if (symLHS && symLHS->computeComplexity() < MaxComp)
370 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
371 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
373 if (symRHS && symRHS->computeComplexity() < MaxComp)
374 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
375 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
381 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
382 SVal lhs, SVal rhs, QualType type) {
384 if (lhs.isUndef() || rhs.isUndef())
385 return UndefinedVal();
387 if (lhs.isUnknown() || rhs.isUnknown())
390 if (lhs.getAs<nonloc::LazyCompoundVal>() ||
391 rhs.getAs<nonloc::LazyCompoundVal>()) {
395 if (Optional<Loc> LV = lhs.getAs<Loc>()) {
396 if (Optional<Loc> RV = rhs.getAs<Loc>())
397 return evalBinOpLL(state, op, *LV, *RV, type);
399 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
402 if (Optional<Loc> RV = rhs.getAs<Loc>()) {
403 // Support pointer arithmetic where the addend is on the left
404 // and the pointer on the right.
405 assert(op == BO_Add);
407 // Commute the operands.
408 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
411 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
415 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
416 DefinedOrUnknownSVal lhs,
417 DefinedOrUnknownSVal rhs) {
418 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType())
419 .castAs<DefinedOrUnknownSVal>();
422 /// Recursively check if the pointer types are equal modulo const, volatile,
423 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
424 /// Assumes the input types are canonical.
425 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
427 while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) {
428 Qualifiers Quals1, Quals2;
429 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
430 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
432 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
433 // spaces) are identical.
434 Quals1.removeCVRQualifiers();
435 Quals2.removeCVRQualifiers();
436 if (Quals1 != Quals2)
440 // If we are casting to void, the 'From' value can be used to represent the
442 if (ToTy->isVoidType())
451 // Handles casts of type CK_IntegralCast.
452 // At the moment, this function will redirect to evalCast, except when the range
453 // of the original value is known to be greater than the max of the target type.
454 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
455 QualType castTy, QualType originalTy) {
457 // No truncations if target type is big enough.
458 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
459 return evalCast(val, castTy, originalTy);
461 const SymExpr *se = val.getAsSymbolicExpression();
462 if (!se) // Let evalCast handle non symbolic expressions.
463 return evalCast(val, castTy, originalTy);
465 // Find the maximum value of the target type.
466 APSIntType ToType(getContext().getTypeSize(castTy),
467 castTy->isUnsignedIntegerType());
468 llvm::APSInt ToTypeMax = ToType.getMaxValue();
469 NonLoc ToTypeMaxVal =
470 makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
471 : ToTypeMax.getSExtValue(),
474 // Check the range of the symbol being casted against the maximum value of the
476 NonLoc FromVal = val.castAs<NonLoc>();
477 QualType CmpTy = getConditionType();
479 evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
480 ProgramStateRef IsNotTruncated, IsTruncated;
481 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
482 if (!IsNotTruncated && IsTruncated) {
483 // Symbol is truncated so we evaluate it as a cast.
484 NonLoc CastVal = makeNonLoc(se, originalTy, castTy);
487 return evalCast(val, castTy, originalTy);
490 // FIXME: should rewrite according to the cast kind.
491 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
492 castTy = Context.getCanonicalType(castTy);
493 originalTy = Context.getCanonicalType(originalTy);
494 if (val.isUnknownOrUndef() || castTy == originalTy)
497 if (castTy->isBooleanType()) {
498 if (val.isUnknownOrUndef())
500 if (val.isConstant())
501 return makeTruthVal(!val.isZeroConstant(), castTy);
502 if (!Loc::isLocType(originalTy) &&
503 !originalTy->isIntegralOrEnumerationType() &&
504 !originalTy->isMemberPointerType())
506 if (SymbolRef Sym = val.getAsSymbol(true)) {
507 BasicValueFactory &BVF = getBasicValueFactory();
508 // FIXME: If we had a state here, we could see if the symbol is known to
509 // be zero, but we don't.
510 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy);
512 // Loc values are not always true, they could be weakly linked functions.
513 if (Optional<Loc> L = val.getAs<Loc>())
514 return evalCastFromLoc(*L, castTy);
516 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
517 return evalCastFromLoc(L, castTy);
520 // For const casts, casts to void, just propagate the value.
521 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
522 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
523 Context.getPointerType(originalTy)))
526 // Check for casts from pointers to integers.
527 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
528 return evalCastFromLoc(val.castAs<Loc>(), castTy);
530 // Check for casts from integers to pointers.
531 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
532 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
533 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
534 StoreManager &storeMgr = StateMgr.getStoreManager();
535 R = storeMgr.castRegion(R, castTy);
536 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
540 return dispatchCast(val, castTy);
543 // Just pass through function and block pointers.
544 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
545 assert(Loc::isLocType(castTy));
549 // Check for casts from array type to another type.
550 if (const ArrayType *arrayT =
551 dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
552 // We will always decay to a pointer.
553 QualType elemTy = arrayT->getElementType();
554 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
556 // Are we casting from an array to a pointer? If so just pass on
557 // the decayed value.
558 if (castTy->isPointerType() || castTy->isReferenceType())
561 // Are we casting from an array to an integer? If so, cast the decayed
562 // pointer value to an integer.
563 assert(castTy->isIntegralOrEnumerationType());
565 // FIXME: Keep these here for now in case we decide soon that we
566 // need the original decayed type.
567 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
568 // QualType pointerTy = C.getPointerType(elemTy);
569 return evalCastFromLoc(val.castAs<Loc>(), castTy);
572 // Check for casts from a region to a specific type.
573 if (const MemRegion *R = val.getAsRegion()) {
574 // Handle other casts of locations to integers.
575 if (castTy->isIntegralOrEnumerationType())
576 return evalCastFromLoc(loc::MemRegionVal(R), castTy);
578 // FIXME: We should handle the case where we strip off view layers to get
579 // to a desugared type.
580 if (!Loc::isLocType(castTy)) {
581 // FIXME: There can be gross cases where one casts the result of a function
582 // (that returns a pointer) to some other value that happens to fit
583 // within that pointer value. We currently have no good way to
584 // model such operations. When this happens, the underlying operation
585 // is that the caller is reasoning about bits. Conceptually we are
586 // layering a "view" of a location on top of those bits. Perhaps
587 // we need to be more lazy about mutual possible views, even on an
588 // SVal? This may be necessary for bit-level reasoning as well.
592 // We get a symbolic function pointer for a dereference of a function
593 // pointer, but it is of function type. Example:
596 // void (*my_func)(int * x);
601 // int f1_a(struct FPRec* foo) {
603 // (*foo->my_func)(&x);
604 // return bar(x)+1; // no-warning
607 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
608 originalTy->isBlockPointerType() || castTy->isReferenceType());
610 StoreManager &storeMgr = StateMgr.getStoreManager();
612 // Delegate to store manager to get the result of casting a region to a
613 // different type. If the MemRegion* returned is NULL, this expression
614 // Evaluates to UnknownVal.
615 R = storeMgr.castRegion(R, castTy);
616 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
619 return dispatchCast(val, castTy);