• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SValBuilder.cpp - Basic class for all SValBuilder implementations -*- 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 SValBuilder, the base class for all (complete) SValBuilder
11 //  implementations.
12 //
13 //===----------------------------------------------------------------------===//
14 
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"
22 
23 using namespace clang;
24 using namespace ento;
25 
26 //===----------------------------------------------------------------------===//
27 // Basic SVal creation.
28 //===----------------------------------------------------------------------===//
29 
anchor()30 void SValBuilder::anchor() { }
31 
makeZeroVal(QualType type)32 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
33   if (Loc::isLocType(type))
34     return makeNull();
35 
36   if (type->isIntegralOrEnumerationType())
37     return makeIntVal(0, type);
38 
39   // FIXME: Handle floats.
40   // FIXME: Handle structs.
41   return UnknownVal();
42 }
43 
makeNonLoc(const SymExpr * lhs,BinaryOperator::Opcode op,const llvm::APSInt & rhs,QualType type)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.
49   assert(lhs);
50   assert(!Loc::isLocType(type));
51   return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
52 }
53 
makeNonLoc(const llvm::APSInt & lhs,BinaryOperator::Opcode op,const SymExpr * rhs,QualType type)54 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
55                                BinaryOperator::Opcode op, const SymExpr *rhs,
56                                QualType type) {
57   assert(rhs);
58   assert(!Loc::isLocType(type));
59   return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
60 }
61 
makeNonLoc(const SymExpr * lhs,BinaryOperator::Opcode op,const SymExpr * rhs,QualType type)62 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
63                                const SymExpr *rhs, QualType type) {
64   assert(lhs && rhs);
65   assert(!Loc::isLocType(type));
66   return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
67 }
68 
makeNonLoc(const SymExpr * operand,QualType fromTy,QualType toTy)69 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
70                                QualType fromTy, QualType toTy) {
71   assert(operand);
72   assert(!Loc::isLocType(toTy));
73   return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
74 }
75 
convertToArrayIndex(SVal val)76 SVal SValBuilder::convertToArrayIndex(SVal val) {
77   if (val.isUnknownOrUndef())
78     return val;
79 
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())
84       return val;
85   }
86 
87   return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy);
88 }
89 
makeBoolVal(const CXXBoolLiteralExpr * boolean)90 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
91   return makeTruthVal(boolean->getValue());
92 }
93 
94 DefinedOrUnknownSVal
getRegionValueSymbolVal(const TypedValueRegion * region)95 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
96   QualType T = region->getValueType();
97 
98   if (T->isNullPtrType())
99     return makeZeroVal(T);
100 
101   if (!SymbolManager::canSymbolicate(T))
102     return UnknownVal();
103 
104   SymbolRef sym = SymMgr.getRegionValueSymbol(region);
105 
106   if (Loc::isLocType(T))
107     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
108 
109   return nonloc::SymbolVal(sym);
110 }
111 
conjureSymbolVal(const void * SymbolTag,const Expr * Ex,const LocationContext * LCtx,unsigned Count)112 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
113                                                    const Expr *Ex,
114                                                    const LocationContext *LCtx,
115                                                    unsigned Count) {
116   QualType T = Ex->getType();
117 
118   if (T->isNullPtrType())
119     return makeZeroVal(T);
120 
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();
124   if (Ex->isGLValue())
125     T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
126 
127   return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
128 }
129 
conjureSymbolVal(const void * symbolTag,const Expr * expr,const LocationContext * LCtx,QualType type,unsigned count)130 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
131                                                    const Expr *expr,
132                                                    const LocationContext *LCtx,
133                                                    QualType type,
134                                                    unsigned count) {
135   if (type->isNullPtrType())
136     return makeZeroVal(type);
137 
138   if (!SymbolManager::canSymbolicate(type))
139     return UnknownVal();
140 
141   SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
142 
143   if (Loc::isLocType(type))
144     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
145 
146   return nonloc::SymbolVal(sym);
147 }
148 
149 
conjureSymbolVal(const Stmt * stmt,const LocationContext * LCtx,QualType type,unsigned visitCount)150 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
151                                                    const LocationContext *LCtx,
152                                                    QualType type,
153                                                    unsigned visitCount) {
154   if (type->isNullPtrType())
155     return makeZeroVal(type);
156 
157   if (!SymbolManager::canSymbolicate(type))
158     return UnknownVal();
159 
160   SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
161 
162   if (Loc::isLocType(type))
163     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
164 
165   return nonloc::SymbolVal(sym);
166 }
167 
168 DefinedOrUnknownSVal
getConjuredHeapSymbolVal(const Expr * E,const LocationContext * LCtx,unsigned VisitCount)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);
177 
178   SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount);
179   return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
180 }
181 
getMetadataSymbolVal(const void * symbolTag,const MemRegion * region,const Expr * expr,QualType type,unsigned count)182 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
183                                               const MemRegion *region,
184                                               const Expr *expr, QualType type,
185                                               unsigned count) {
186   assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
187 
188   SymbolRef sym =
189       SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag);
190 
191   if (Loc::isLocType(type))
192     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
193 
194   return nonloc::SymbolVal(sym);
195 }
196 
197 DefinedOrUnknownSVal
getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,const TypedValueRegion * region)198 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
199                                              const TypedValueRegion *region) {
200   QualType T = region->getValueType();
201 
202   if (T->isNullPtrType())
203     return makeZeroVal(T);
204 
205   if (!SymbolManager::canSymbolicate(T))
206     return UnknownVal();
207 
208   SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
209 
210   if (Loc::isLocType(T))
211     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
212 
213   return nonloc::SymbolVal(sym);
214 }
215 
getFunctionPointer(const FunctionDecl * func)216 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
217   return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
218 }
219 
getBlockPointer(const BlockDecl * block,CanQualType locTy,const LocationContext * locContext,unsigned blockCount)220 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
221                                          CanQualType locTy,
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,
227                                                         blockCount);
228   return loc::MemRegionVal(BD);
229 }
230 
231 /// Return a memory region for the 'this' object reference.
getCXXThis(const CXXMethodDecl * D,const StackFrameContext * SFC)232 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
233                                           const StackFrameContext *SFC) {
234   return loc::MemRegionVal(getRegionManager().
235                            getCXXThisRegion(D->getThisType(getContext()), SFC));
236 }
237 
238 /// Return a memory region for the 'this' object reference.
getCXXThis(const CXXRecordDecl * D,const StackFrameContext * SFC)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));
244 }
245 
getConstantVal(const Expr * E)246 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
247   E = E->IgnoreParens();
248 
249   switch (E->getStmtClass()) {
250   // Handle expressions that we treat differently from the AST's constant
251   // evaluator.
252   case Stmt::AddrLabelExprClass:
253     return makeLoc(cast<AddrLabelExpr>(E));
254 
255   case Stmt::CXXScalarValueInitExprClass:
256   case Stmt::ImplicitValueInitExprClass:
257     return makeZeroVal(E->getType());
258 
259   case Stmt::ObjCStringLiteralClass: {
260     const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E);
261     return makeLoc(getRegionManager().getObjCStringRegion(SL));
262   }
263 
264   case Stmt::StringLiteralClass: {
265     const StringLiteral *SL = cast<StringLiteral>(E);
266     return makeLoc(getRegionManager().getStringRegion(SL));
267   }
268 
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());
274   }
275 
276   case Stmt::CXXBoolLiteralExprClass:
277     return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
278 
279   case Stmt::TypeTraitExprClass: {
280     const TypeTraitExpr *TE = cast<TypeTraitExpr>(E);
281     return makeTruthVal(TE->getValue(), TE->getType());
282   }
283 
284   case Stmt::IntegerLiteralClass:
285     return makeIntVal(cast<IntegerLiteral>(E));
286 
287   case Stmt::ObjCBoolLiteralExprClass:
288     return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
289 
290   case Stmt::CXXNullPtrLiteralExprClass:
291     return makeNull();
292 
293   case Stmt::ImplicitCastExprClass: {
294     const CastExpr *CE = cast<CastExpr>(E);
295     switch (CE->getCastKind()) {
296     default:
297       break;
298     case CK_ArrayToPointerDecay:
299     case CK_BitCast: {
300       const Expr *SE = CE->getSubExpr();
301       Optional<SVal> Val = getConstantVal(SE);
302       if (!Val)
303         return None;
304       return evalCast(*Val, CE->getType(), SE->getType());
305     }
306     }
307     // FALLTHROUGH
308   }
309 
310   // If we don't have a special case, fall back to the AST's constant evaluator.
311   default: {
312     // Don't try to come up with a value for materialized temporaries.
313     if (E->isGLValue())
314       return None;
315 
316     ASTContext &Ctx = getContext();
317     llvm::APSInt Result;
318     if (E->EvaluateAsInt(Result, Ctx))
319       return makeIntVal(Result);
320 
321     if (Loc::isLocType(E->getType()))
322       if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
323         return makeNull();
324 
325     return None;
326   }
327   }
328 }
329 
330 //===----------------------------------------------------------------------===//
331 
makeSymExprValNN(ProgramStateRef State,BinaryOperator::Opcode Op,NonLoc LHS,NonLoc RHS,QualType ResultTy)332 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
333                                    BinaryOperator::Opcode Op,
334                                    NonLoc LHS, NonLoc RHS,
335                                    QualType ResultTy) {
336   if (!State->isTainted(RHS) && !State->isTainted(LHS))
337     return UnknownVal();
338 
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
344 
345   if (symLHS && symRHS &&
346       (symLHS->computeComplexity() + symRHS->computeComplexity()) <  MaxComp)
347     return makeNonLoc(symLHS, Op, symRHS, ResultTy);
348 
349   if (symLHS && symLHS->computeComplexity() < MaxComp)
350     if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
351       return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
352 
353   if (symRHS && symRHS->computeComplexity() < MaxComp)
354     if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
355       return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
356 
357   return UnknownVal();
358 }
359 
360 
evalBinOp(ProgramStateRef state,BinaryOperator::Opcode op,SVal lhs,SVal rhs,QualType type)361 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
362                             SVal lhs, SVal rhs, QualType type) {
363 
364   if (lhs.isUndef() || rhs.isUndef())
365     return UndefinedVal();
366 
367   if (lhs.isUnknown() || rhs.isUnknown())
368     return UnknownVal();
369 
370   if (lhs.getAs<nonloc::LazyCompoundVal>() ||
371       rhs.getAs<nonloc::LazyCompoundVal>()) {
372     return UnknownVal();
373   }
374 
375   if (Optional<Loc> LV = lhs.getAs<Loc>()) {
376     if (Optional<Loc> RV = rhs.getAs<Loc>())
377       return evalBinOpLL(state, op, *LV, *RV, type);
378 
379     return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
380   }
381 
382   if (Optional<Loc> RV = rhs.getAs<Loc>()) {
383     // Support pointer arithmetic where the addend is on the left
384     // and the pointer on the right.
385     assert(op == BO_Add);
386 
387     // Commute the operands.
388     return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
389   }
390 
391   return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
392                      type);
393 }
394 
evalEQ(ProgramStateRef state,DefinedOrUnknownSVal lhs,DefinedOrUnknownSVal rhs)395 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
396                                          DefinedOrUnknownSVal lhs,
397                                          DefinedOrUnknownSVal rhs) {
398   return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType())
399       .castAs<DefinedOrUnknownSVal>();
400 }
401 
402 /// Recursively check if the pointer types are equal modulo const, volatile,
403 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
404 /// Assumes the input types are canonical.
shouldBeModeledWithNoOp(ASTContext & Context,QualType ToTy,QualType FromTy)405 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
406                                                          QualType FromTy) {
407   while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) {
408     Qualifiers Quals1, Quals2;
409     ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
410     FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
411 
412     // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
413     // spaces) are identical.
414     Quals1.removeCVRQualifiers();
415     Quals2.removeCVRQualifiers();
416     if (Quals1 != Quals2)
417       return false;
418   }
419 
420   // If we are casting to void, the 'From' value can be used to represent the
421   // 'To' value.
422   if (ToTy->isVoidType())
423     return true;
424 
425   if (ToTy != FromTy)
426     return false;
427 
428   return true;
429 }
430 
431 // Handles casts of type CK_IntegralCast.
432 // At the moment, this function will redirect to evalCast, except when the range
433 // of the original value is known to be greater than the max of the target type.
evalIntegralCast(ProgramStateRef state,SVal val,QualType castTy,QualType originalTy)434 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
435                                    QualType castTy, QualType originalTy) {
436 
437   // No truncations if target type is big enough.
438   if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
439     return evalCast(val, castTy, originalTy);
440 
441   const SymExpr *se = val.getAsSymbolicExpression();
442   if (!se) // Let evalCast handle non symbolic expressions.
443     return evalCast(val, castTy, originalTy);
444 
445   // Find the maximum value of the target type.
446   APSIntType ToType(getContext().getTypeSize(castTy),
447                     castTy->isUnsignedIntegerType());
448   llvm::APSInt ToTypeMax = ToType.getMaxValue();
449   NonLoc ToTypeMaxVal =
450       makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
451                                         : ToTypeMax.getSExtValue(),
452                  castTy)
453           .castAs<NonLoc>();
454   // Check the range of the symbol being casted against the maximum value of the
455   // target type.
456   NonLoc FromVal = val.castAs<NonLoc>();
457   QualType CmpTy = getConditionType();
458   NonLoc CompVal =
459       evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
460   ProgramStateRef IsNotTruncated, IsTruncated;
461   std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
462   if (!IsNotTruncated && IsTruncated) {
463     // Symbol is truncated so we evaluate it as a cast.
464     NonLoc CastVal = makeNonLoc(se, originalTy, castTy);
465     return CastVal;
466   }
467   return evalCast(val, castTy, originalTy);
468 }
469 
470 // FIXME: should rewrite according to the cast kind.
evalCast(SVal val,QualType castTy,QualType originalTy)471 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
472   castTy = Context.getCanonicalType(castTy);
473   originalTy = Context.getCanonicalType(originalTy);
474   if (val.isUnknownOrUndef() || castTy == originalTy)
475     return val;
476 
477   if (castTy->isBooleanType()) {
478     if (val.isUnknownOrUndef())
479       return val;
480     if (val.isConstant())
481       return makeTruthVal(!val.isZeroConstant(), castTy);
482     if (!Loc::isLocType(originalTy) &&
483         !originalTy->isIntegralOrEnumerationType() &&
484         !originalTy->isMemberPointerType())
485       return UnknownVal();
486     if (SymbolRef Sym = val.getAsSymbol(true)) {
487       BasicValueFactory &BVF = getBasicValueFactory();
488       // FIXME: If we had a state here, we could see if the symbol is known to
489       // be zero, but we don't.
490       return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy);
491     }
492     // Loc values are not always true, they could be weakly linked functions.
493     if (Optional<Loc> L = val.getAs<Loc>())
494       return evalCastFromLoc(*L, castTy);
495 
496     Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
497     return evalCastFromLoc(L, castTy);
498   }
499 
500   // For const casts, casts to void, just propagate the value.
501   if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
502     if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
503                                          Context.getPointerType(originalTy)))
504       return val;
505 
506   // Check for casts from pointers to integers.
507   if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
508     return evalCastFromLoc(val.castAs<Loc>(), castTy);
509 
510   // Check for casts from integers to pointers.
511   if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
512     if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
513       if (const MemRegion *R = LV->getLoc().getAsRegion()) {
514         StoreManager &storeMgr = StateMgr.getStoreManager();
515         R = storeMgr.castRegion(R, castTy);
516         return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
517       }
518       return LV->getLoc();
519     }
520     return dispatchCast(val, castTy);
521   }
522 
523   // Just pass through function and block pointers.
524   if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
525     assert(Loc::isLocType(castTy));
526     return val;
527   }
528 
529   // Check for casts from array type to another type.
530   if (const ArrayType *arrayT =
531                       dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
532     // We will always decay to a pointer.
533     QualType elemTy = arrayT->getElementType();
534     val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
535 
536     // Are we casting from an array to a pointer?  If so just pass on
537     // the decayed value.
538     if (castTy->isPointerType() || castTy->isReferenceType())
539       return val;
540 
541     // Are we casting from an array to an integer?  If so, cast the decayed
542     // pointer value to an integer.
543     assert(castTy->isIntegralOrEnumerationType());
544 
545     // FIXME: Keep these here for now in case we decide soon that we
546     // need the original decayed type.
547     //    QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
548     //    QualType pointerTy = C.getPointerType(elemTy);
549     return evalCastFromLoc(val.castAs<Loc>(), castTy);
550   }
551 
552   // Check for casts from a region to a specific type.
553   if (const MemRegion *R = val.getAsRegion()) {
554     // Handle other casts of locations to integers.
555     if (castTy->isIntegralOrEnumerationType())
556       return evalCastFromLoc(loc::MemRegionVal(R), castTy);
557 
558     // FIXME: We should handle the case where we strip off view layers to get
559     //  to a desugared type.
560     if (!Loc::isLocType(castTy)) {
561       // FIXME: There can be gross cases where one casts the result of a function
562       // (that returns a pointer) to some other value that happens to fit
563       // within that pointer value.  We currently have no good way to
564       // model such operations.  When this happens, the underlying operation
565       // is that the caller is reasoning about bits.  Conceptually we are
566       // layering a "view" of a location on top of those bits.  Perhaps
567       // we need to be more lazy about mutual possible views, even on an
568       // SVal?  This may be necessary for bit-level reasoning as well.
569       return UnknownVal();
570     }
571 
572     // We get a symbolic function pointer for a dereference of a function
573     // pointer, but it is of function type. Example:
574 
575     //  struct FPRec {
576     //    void (*my_func)(int * x);
577     //  };
578     //
579     //  int bar(int x);
580     //
581     //  int f1_a(struct FPRec* foo) {
582     //    int x;
583     //    (*foo->my_func)(&x);
584     //    return bar(x)+1; // no-warning
585     //  }
586 
587     assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
588            originalTy->isBlockPointerType() || castTy->isReferenceType());
589 
590     StoreManager &storeMgr = StateMgr.getStoreManager();
591 
592     // Delegate to store manager to get the result of casting a region to a
593     // different type.  If the MemRegion* returned is NULL, this expression
594     // Evaluates to UnknownVal.
595     R = storeMgr.castRegion(R, castTy);
596     return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
597   }
598 
599   return dispatchCast(val, castTy);
600 }
601