1 //===- SValBuilder.cpp - Basic class for all SValBuilder implementations --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines SValBuilder, the base class for all (complete) SValBuilder
10 //  implementations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/ExprObjC.h"
20 #include "clang/AST/Stmt.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Analysis/AnalysisDeclContext.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.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/SymExpr.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
35 #include "llvm/ADT/APSInt.h"
36 #include "llvm/ADT/None.h"
37 #include "llvm/ADT/Optional.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/Compiler.h"
40 #include <cassert>
41 #include <tuple>
42 
43 using namespace clang;
44 using namespace ento;
45 
46 //===----------------------------------------------------------------------===//
47 // Basic SVal creation.
48 //===----------------------------------------------------------------------===//
49 
50 void SValBuilder::anchor() {}
51 
52 SValBuilder::SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
53                          ProgramStateManager &stateMgr)
54     : Context(context), BasicVals(context, alloc),
55       SymMgr(context, BasicVals, alloc), MemMgr(context, alloc),
56       StateMgr(stateMgr),
57       AnOpts(
58           stateMgr.getOwningEngine().getAnalysisManager().getAnalyzerOptions()),
59       ArrayIndexTy(context.LongLongTy),
60       ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
61 
62 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
63   if (Loc::isLocType(type))
64     return makeNullWithType(type);
65 
66   if (type->isIntegralOrEnumerationType())
67     return makeIntVal(0, type);
68 
69   if (type->isArrayType() || type->isRecordType() || type->isVectorType() ||
70       type->isAnyComplexType())
71     return makeCompoundVal(type, BasicVals.getEmptySValList());
72 
73   // FIXME: Handle floats.
74   return UnknownVal();
75 }
76 
77 nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
78                                           BinaryOperator::Opcode op,
79                                           const llvm::APSInt &rhs,
80                                           QualType type) {
81   // The Environment ensures we always get a persistent APSInt in
82   // BasicValueFactory, so we don't need to get the APSInt from
83   // BasicValueFactory again.
84   assert(lhs);
85   assert(!Loc::isLocType(type));
86   return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
87 }
88 
89 nonloc::SymbolVal SValBuilder::makeNonLoc(const llvm::APSInt &lhs,
90                                           BinaryOperator::Opcode op,
91                                           const SymExpr *rhs, QualType type) {
92   assert(rhs);
93   assert(!Loc::isLocType(type));
94   return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
95 }
96 
97 nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
98                                           BinaryOperator::Opcode op,
99                                           const SymExpr *rhs, QualType type) {
100   assert(lhs && rhs);
101   assert(!Loc::isLocType(type));
102   return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
103 }
104 
105 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand, UnaryOperator::Opcode op,
106                                QualType type) {
107   assert(operand);
108   assert(!Loc::isLocType(type));
109   return nonloc::SymbolVal(SymMgr.getUnarySymExpr(operand, op, type));
110 }
111 
112 nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *operand,
113                                           QualType fromTy, QualType toTy) {
114   assert(operand);
115   assert(!Loc::isLocType(toTy));
116   return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
117 }
118 
119 SVal SValBuilder::convertToArrayIndex(SVal val) {
120   if (val.isUnknownOrUndef())
121     return val;
122 
123   // Common case: we have an appropriately sized integer.
124   if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) {
125     const llvm::APSInt& I = CI->getValue();
126     if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
127       return val;
128   }
129 
130   return evalCast(val, ArrayIndexTy, QualType{});
131 }
132 
133 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
134   return makeTruthVal(boolean->getValue());
135 }
136 
137 DefinedOrUnknownSVal
138 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion *region) {
139   QualType T = region->getValueType();
140 
141   if (T->isNullPtrType())
142     return makeZeroVal(T);
143 
144   if (!SymbolManager::canSymbolicate(T))
145     return UnknownVal();
146 
147   SymbolRef sym = SymMgr.getRegionValueSymbol(region);
148 
149   if (Loc::isLocType(T))
150     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
151 
152   return nonloc::SymbolVal(sym);
153 }
154 
155 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
156                                                    const Expr *Ex,
157                                                    const LocationContext *LCtx,
158                                                    unsigned Count) {
159   QualType T = Ex->getType();
160 
161   if (T->isNullPtrType())
162     return makeZeroVal(T);
163 
164   // Compute the type of the result. If the expression is not an R-value, the
165   // result should be a location.
166   QualType ExType = Ex->getType();
167   if (Ex->isGLValue())
168     T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
169 
170   return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
171 }
172 
173 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
174                                                    const Expr *expr,
175                                                    const LocationContext *LCtx,
176                                                    QualType type,
177                                                    unsigned count) {
178   if (type->isNullPtrType())
179     return makeZeroVal(type);
180 
181   if (!SymbolManager::canSymbolicate(type))
182     return UnknownVal();
183 
184   SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
185 
186   if (Loc::isLocType(type))
187     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
188 
189   return nonloc::SymbolVal(sym);
190 }
191 
192 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
193                                                    const LocationContext *LCtx,
194                                                    QualType type,
195                                                    unsigned visitCount) {
196   if (type->isNullPtrType())
197     return makeZeroVal(type);
198 
199   if (!SymbolManager::canSymbolicate(type))
200     return UnknownVal();
201 
202   SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
203 
204   if (Loc::isLocType(type))
205     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
206 
207   return nonloc::SymbolVal(sym);
208 }
209 
210 DefinedOrUnknownSVal
211 SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
212                                       const LocationContext *LCtx,
213                                       unsigned VisitCount) {
214   QualType T = E->getType();
215   return getConjuredHeapSymbolVal(E, LCtx, T, VisitCount);
216 }
217 
218 DefinedOrUnknownSVal
219 SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
220                                       const LocationContext *LCtx,
221                                       QualType type, unsigned VisitCount) {
222   assert(Loc::isLocType(type));
223   assert(SymbolManager::canSymbolicate(type));
224   if (type->isNullPtrType())
225     return makeZeroVal(type);
226 
227   SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, type, VisitCount);
228   return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
229 }
230 
231 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
232                                               const MemRegion *region,
233                                               const Expr *expr, QualType type,
234                                               const LocationContext *LCtx,
235                                               unsigned count) {
236   assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
237 
238   SymbolRef sym =
239       SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag);
240 
241   if (Loc::isLocType(type))
242     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
243 
244   return nonloc::SymbolVal(sym);
245 }
246 
247 DefinedOrUnknownSVal
248 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
249                                              const TypedValueRegion *region) {
250   QualType T = region->getValueType();
251 
252   if (T->isNullPtrType())
253     return makeZeroVal(T);
254 
255   if (!SymbolManager::canSymbolicate(T))
256     return UnknownVal();
257 
258   SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
259 
260   if (Loc::isLocType(T))
261     return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
262 
263   return nonloc::SymbolVal(sym);
264 }
265 
266 DefinedSVal SValBuilder::getMemberPointer(const NamedDecl *ND) {
267   assert(!ND || (isa<CXXMethodDecl, FieldDecl, IndirectFieldDecl>(ND)));
268 
269   if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
270     // Sema treats pointers to static member functions as have function pointer
271     // type, so return a function pointer for the method.
272     // We don't need to play a similar trick for static member fields
273     // because these are represented as plain VarDecls and not FieldDecls
274     // in the AST.
275     if (MD->isStatic())
276       return getFunctionPointer(MD);
277   }
278 
279   return nonloc::PointerToMember(ND);
280 }
281 
282 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
283   return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
284 }
285 
286 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
287                                          CanQualType locTy,
288                                          const LocationContext *locContext,
289                                          unsigned blockCount) {
290   const BlockCodeRegion *BC =
291     MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext());
292   const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
293                                                         blockCount);
294   return loc::MemRegionVal(BD);
295 }
296 
297 Optional<loc::MemRegionVal>
298 SValBuilder::getCastedMemRegionVal(const MemRegion *R, QualType Ty) {
299   if (auto OptR = StateMgr.getStoreManager().castRegion(R, Ty))
300     return loc::MemRegionVal(*OptR);
301   return None;
302 }
303 
304 /// Return a memory region for the 'this' object reference.
305 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
306                                           const StackFrameContext *SFC) {
307   return loc::MemRegionVal(
308       getRegionManager().getCXXThisRegion(D->getThisType(), SFC));
309 }
310 
311 /// Return a memory region for the 'this' object reference.
312 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
313                                           const StackFrameContext *SFC) {
314   const Type *T = D->getTypeForDecl();
315   QualType PT = getContext().getPointerType(QualType(T, 0));
316   return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
317 }
318 
319 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
320   E = E->IgnoreParens();
321 
322   switch (E->getStmtClass()) {
323   // Handle expressions that we treat differently from the AST's constant
324   // evaluator.
325   case Stmt::AddrLabelExprClass:
326     return makeLoc(cast<AddrLabelExpr>(E));
327 
328   case Stmt::CXXScalarValueInitExprClass:
329   case Stmt::ImplicitValueInitExprClass:
330     return makeZeroVal(E->getType());
331 
332   case Stmt::ObjCStringLiteralClass: {
333     const auto *SL = cast<ObjCStringLiteral>(E);
334     return makeLoc(getRegionManager().getObjCStringRegion(SL));
335   }
336 
337   case Stmt::StringLiteralClass: {
338     const auto *SL = cast<StringLiteral>(E);
339     return makeLoc(getRegionManager().getStringRegion(SL));
340   }
341 
342   case Stmt::PredefinedExprClass: {
343     const auto *PE = cast<PredefinedExpr>(E);
344     assert(PE->getFunctionName() &&
345            "Since we analyze only instantiated functions, PredefinedExpr "
346            "should have a function name.");
347     return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName()));
348   }
349 
350   // Fast-path some expressions to avoid the overhead of going through the AST's
351   // constant evaluator
352   case Stmt::CharacterLiteralClass: {
353     const auto *C = cast<CharacterLiteral>(E);
354     return makeIntVal(C->getValue(), C->getType());
355   }
356 
357   case Stmt::CXXBoolLiteralExprClass:
358     return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
359 
360   case Stmt::TypeTraitExprClass: {
361     const auto *TE = cast<TypeTraitExpr>(E);
362     return makeTruthVal(TE->getValue(), TE->getType());
363   }
364 
365   case Stmt::IntegerLiteralClass:
366     return makeIntVal(cast<IntegerLiteral>(E));
367 
368   case Stmt::ObjCBoolLiteralExprClass:
369     return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
370 
371   case Stmt::CXXNullPtrLiteralExprClass:
372     return makeNullWithType(E->getType());
373 
374   case Stmt::CStyleCastExprClass:
375   case Stmt::CXXFunctionalCastExprClass:
376   case Stmt::CXXConstCastExprClass:
377   case Stmt::CXXReinterpretCastExprClass:
378   case Stmt::CXXStaticCastExprClass:
379   case Stmt::ImplicitCastExprClass: {
380     const auto *CE = cast<CastExpr>(E);
381     switch (CE->getCastKind()) {
382     default:
383       break;
384     case CK_ArrayToPointerDecay:
385     case CK_IntegralToPointer:
386     case CK_NoOp:
387     case CK_BitCast: {
388       const Expr *SE = CE->getSubExpr();
389       Optional<SVal> Val = getConstantVal(SE);
390       if (!Val)
391         return None;
392       return evalCast(*Val, CE->getType(), SE->getType());
393     }
394     }
395     // FALLTHROUGH
396     LLVM_FALLTHROUGH;
397   }
398 
399   // If we don't have a special case, fall back to the AST's constant evaluator.
400   default: {
401     // Don't try to come up with a value for materialized temporaries.
402     if (E->isGLValue())
403       return None;
404 
405     ASTContext &Ctx = getContext();
406     Expr::EvalResult Result;
407     if (E->EvaluateAsInt(Result, Ctx))
408       return makeIntVal(Result.Val.getInt());
409 
410     if (Loc::isLocType(E->getType()))
411       if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
412         return makeNullWithType(E->getType());
413 
414     return None;
415   }
416   }
417 }
418 
419 SVal SValBuilder::makeSymExprValNN(BinaryOperator::Opcode Op,
420                                    NonLoc LHS, NonLoc RHS,
421                                    QualType ResultTy) {
422   SymbolRef symLHS = LHS.getAsSymbol();
423   SymbolRef symRHS = RHS.getAsSymbol();
424 
425   // TODO: When the Max Complexity is reached, we should conjure a symbol
426   // instead of generating an Unknown value and propagate the taint info to it.
427   const unsigned MaxComp = AnOpts.MaxSymbolComplexity;
428 
429   if (symLHS && symRHS &&
430       (symLHS->computeComplexity() + symRHS->computeComplexity()) <  MaxComp)
431     return makeNonLoc(symLHS, Op, symRHS, ResultTy);
432 
433   if (symLHS && symLHS->computeComplexity() < MaxComp)
434     if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
435       return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
436 
437   if (symRHS && symRHS->computeComplexity() < MaxComp)
438     if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
439       return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
440 
441   return UnknownVal();
442 }
443 
444 SVal SValBuilder::evalUnaryOp(ProgramStateRef state, UnaryOperator::Opcode opc,
445                  SVal operand, QualType type) {
446   auto OpN = operand.getAs<NonLoc>();
447   if (!OpN)
448     return UnknownVal();
449 
450   if (opc == UO_Minus)
451     return evalMinus(*OpN);
452   if (opc == UO_Not)
453     return evalComplement(*OpN);
454   llvm_unreachable("Unexpected unary operator");
455 }
456 
457 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
458                             SVal lhs, SVal rhs, QualType type) {
459   if (lhs.isUndef() || rhs.isUndef())
460     return UndefinedVal();
461 
462   if (lhs.isUnknown() || rhs.isUnknown())
463     return UnknownVal();
464 
465   if (lhs.getAs<nonloc::LazyCompoundVal>() ||
466       rhs.getAs<nonloc::LazyCompoundVal>()) {
467     return UnknownVal();
468   }
469 
470   if (op == BinaryOperatorKind::BO_Cmp) {
471     // We can't reason about C++20 spaceship operator yet.
472     //
473     // FIXME: Support C++20 spaceship operator.
474     //        The main problem here is that the result is not integer.
475     return UnknownVal();
476   }
477 
478   if (Optional<Loc> LV = lhs.getAs<Loc>()) {
479     if (Optional<Loc> RV = rhs.getAs<Loc>())
480       return evalBinOpLL(state, op, *LV, *RV, type);
481 
482     return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
483   }
484 
485   if (const Optional<Loc> RV = rhs.getAs<Loc>()) {
486     const auto IsCommutative = [](BinaryOperatorKind Op) {
487       return Op == BO_Mul || Op == BO_Add || Op == BO_And || Op == BO_Xor ||
488              Op == BO_Or;
489     };
490 
491     if (IsCommutative(op)) {
492       // Swap operands.
493       return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
494     }
495 
496     // If the right operand is a concrete int location then we have nothing
497     // better but to treat it as a simple nonloc.
498     if (auto RV = rhs.getAs<loc::ConcreteInt>()) {
499       const nonloc::ConcreteInt RhsAsLoc = makeIntVal(RV->getValue());
500       return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), RhsAsLoc, type);
501     }
502   }
503 
504   return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
505                      type);
506 }
507 
508 ConditionTruthVal SValBuilder::areEqual(ProgramStateRef state, SVal lhs,
509                                         SVal rhs) {
510   return state->isNonNull(evalEQ(state, lhs, rhs));
511 }
512 
513 SVal SValBuilder::evalEQ(ProgramStateRef state, SVal lhs, SVal rhs) {
514   return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType());
515 }
516 
517 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
518                                          DefinedOrUnknownSVal lhs,
519                                          DefinedOrUnknownSVal rhs) {
520   return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs))
521       .castAs<DefinedOrUnknownSVal>();
522 }
523 
524 /// Recursively check if the pointer types are equal modulo const, volatile,
525 /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
526 /// Assumes the input types are canonical.
527 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
528                                                          QualType FromTy) {
529   while (Context.UnwrapSimilarTypes(ToTy, FromTy)) {
530     Qualifiers Quals1, Quals2;
531     ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
532     FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
533 
534     // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
535     // spaces) are identical.
536     Quals1.removeCVRQualifiers();
537     Quals2.removeCVRQualifiers();
538     if (Quals1 != Quals2)
539       return false;
540   }
541 
542   // If we are casting to void, the 'From' value can be used to represent the
543   // 'To' value.
544   //
545   // FIXME: Doing this after unwrapping the types doesn't make any sense. A
546   // cast from 'int**' to 'void**' is not special in the way that a cast from
547   // 'int*' to 'void*' is.
548   if (ToTy->isVoidType())
549     return true;
550 
551   if (ToTy != FromTy)
552     return false;
553 
554   return true;
555 }
556 
557 // Handles casts of type CK_IntegralCast.
558 // At the moment, this function will redirect to evalCast, except when the range
559 // of the original value is known to be greater than the max of the target type.
560 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
561                                    QualType castTy, QualType originalTy) {
562   // No truncations if target type is big enough.
563   if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
564     return evalCast(val, castTy, originalTy);
565 
566   SymbolRef se = val.getAsSymbol();
567   if (!se) // Let evalCast handle non symbolic expressions.
568     return evalCast(val, castTy, originalTy);
569 
570   // Find the maximum value of the target type.
571   APSIntType ToType(getContext().getTypeSize(castTy),
572                     castTy->isUnsignedIntegerType());
573   llvm::APSInt ToTypeMax = ToType.getMaxValue();
574   NonLoc ToTypeMaxVal =
575       makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
576                                         : ToTypeMax.getSExtValue(),
577                  castTy)
578           .castAs<NonLoc>();
579   // Check the range of the symbol being casted against the maximum value of the
580   // target type.
581   NonLoc FromVal = val.castAs<NonLoc>();
582   QualType CmpTy = getConditionType();
583   NonLoc CompVal =
584       evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
585   ProgramStateRef IsNotTruncated, IsTruncated;
586   std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
587   if (!IsNotTruncated && IsTruncated) {
588     // Symbol is truncated so we evaluate it as a cast.
589     return makeNonLoc(se, originalTy, castTy);
590   }
591   return evalCast(val, castTy, originalTy);
592 }
593 
594 //===----------------------------------------------------------------------===//
595 // Cast methods.
596 // `evalCast` is the main method
597 // `evalCastKind` and `evalCastSubKind` are helpers
598 //===----------------------------------------------------------------------===//
599 
600 /// Cast a given SVal to another SVal using given QualType's.
601 /// \param V -- SVal that should be casted.
602 /// \param CastTy -- QualType that V should be casted according to.
603 /// \param OriginalTy -- QualType which is associated to V. It provides
604 /// additional information about what type the cast performs from.
605 /// \returns the most appropriate casted SVal.
606 /// Note: Many cases don't use an exact OriginalTy. It can be extracted
607 /// from SVal or the cast can performs unconditionaly. Always pass OriginalTy!
608 /// It can be crucial in certain cases and generates different results.
609 /// FIXME: If `OriginalTy.isNull()` is true, then cast performs based on CastTy
610 /// only. This behavior is uncertain and should be improved.
611 SVal SValBuilder::evalCast(SVal V, QualType CastTy, QualType OriginalTy) {
612   if (CastTy.isNull())
613     return V;
614 
615   CastTy = Context.getCanonicalType(CastTy);
616 
617   const bool IsUnknownOriginalType = OriginalTy.isNull();
618   if (!IsUnknownOriginalType) {
619     OriginalTy = Context.getCanonicalType(OriginalTy);
620 
621     if (CastTy == OriginalTy)
622       return V;
623 
624     // FIXME: Move this check to the most appropriate
625     // evalCastKind/evalCastSubKind function. For const casts, casts to void,
626     // just propagate the value.
627     if (!CastTy->isVariableArrayType() && !OriginalTy->isVariableArrayType())
628       if (shouldBeModeledWithNoOp(Context, Context.getPointerType(CastTy),
629                                   Context.getPointerType(OriginalTy)))
630         return V;
631   }
632 
633   // Cast SVal according to kinds.
634   switch (V.getBaseKind()) {
635   case SVal::UndefinedValKind:
636     return evalCastKind(V.castAs<UndefinedVal>(), CastTy, OriginalTy);
637   case SVal::UnknownValKind:
638     return evalCastKind(V.castAs<UnknownVal>(), CastTy, OriginalTy);
639   case SVal::LocKind:
640     return evalCastKind(V.castAs<Loc>(), CastTy, OriginalTy);
641   case SVal::NonLocKind:
642     return evalCastKind(V.castAs<NonLoc>(), CastTy, OriginalTy);
643   }
644 
645   llvm_unreachable("Unknown SVal kind");
646 }
647 
648 SVal SValBuilder::evalCastKind(UndefinedVal V, QualType CastTy,
649                                QualType OriginalTy) {
650   return V;
651 }
652 
653 SVal SValBuilder::evalCastKind(UnknownVal V, QualType CastTy,
654                                QualType OriginalTy) {
655   return V;
656 }
657 
658 SVal SValBuilder::evalCastKind(Loc V, QualType CastTy, QualType OriginalTy) {
659   switch (V.getSubKind()) {
660   case loc::ConcreteIntKind:
661     return evalCastSubKind(V.castAs<loc::ConcreteInt>(), CastTy, OriginalTy);
662   case loc::GotoLabelKind:
663     return evalCastSubKind(V.castAs<loc::GotoLabel>(), CastTy, OriginalTy);
664   case loc::MemRegionValKind:
665     return evalCastSubKind(V.castAs<loc::MemRegionVal>(), CastTy, OriginalTy);
666   }
667 
668   llvm_unreachable("Unknown SVal kind");
669 }
670 
671 SVal SValBuilder::evalCastKind(NonLoc V, QualType CastTy, QualType OriginalTy) {
672   switch (V.getSubKind()) {
673   case nonloc::CompoundValKind:
674     return evalCastSubKind(V.castAs<nonloc::CompoundVal>(), CastTy, OriginalTy);
675   case nonloc::ConcreteIntKind:
676     return evalCastSubKind(V.castAs<nonloc::ConcreteInt>(), CastTy, OriginalTy);
677   case nonloc::LazyCompoundValKind:
678     return evalCastSubKind(V.castAs<nonloc::LazyCompoundVal>(), CastTy,
679                            OriginalTy);
680   case nonloc::LocAsIntegerKind:
681     return evalCastSubKind(V.castAs<nonloc::LocAsInteger>(), CastTy,
682                            OriginalTy);
683   case nonloc::SymbolValKind:
684     return evalCastSubKind(V.castAs<nonloc::SymbolVal>(), CastTy, OriginalTy);
685   case nonloc::PointerToMemberKind:
686     return evalCastSubKind(V.castAs<nonloc::PointerToMember>(), CastTy,
687                            OriginalTy);
688   }
689 
690   llvm_unreachable("Unknown SVal kind");
691 }
692 
693 SVal SValBuilder::evalCastSubKind(loc::ConcreteInt V, QualType CastTy,
694                                   QualType OriginalTy) {
695   // Pointer to bool.
696   if (CastTy->isBooleanType())
697     return makeTruthVal(V.getValue().getBoolValue(), CastTy);
698 
699   // Pointer to integer.
700   if (CastTy->isIntegralOrEnumerationType()) {
701     llvm::APSInt Value = V.getValue();
702     BasicVals.getAPSIntType(CastTy).apply(Value);
703     return makeIntVal(Value);
704   }
705 
706   // Pointer to any pointer.
707   if (Loc::isLocType(CastTy)) {
708     llvm::APSInt Value = V.getValue();
709     BasicVals.getAPSIntType(CastTy).apply(Value);
710     return loc::ConcreteInt(BasicVals.getValue(Value));
711   }
712 
713   // Pointer to whatever else.
714   return UnknownVal();
715 }
716 
717 SVal SValBuilder::evalCastSubKind(loc::GotoLabel V, QualType CastTy,
718                                   QualType OriginalTy) {
719   // Pointer to bool.
720   if (CastTy->isBooleanType())
721     // Labels are always true.
722     return makeTruthVal(true, CastTy);
723 
724   // Pointer to integer.
725   if (CastTy->isIntegralOrEnumerationType()) {
726     const unsigned BitWidth = Context.getIntWidth(CastTy);
727     return makeLocAsInteger(V, BitWidth);
728   }
729 
730   const bool IsUnknownOriginalType = OriginalTy.isNull();
731   if (!IsUnknownOriginalType) {
732     // Array to pointer.
733     if (isa<ArrayType>(OriginalTy))
734       if (CastTy->isPointerType() || CastTy->isReferenceType())
735         return UnknownVal();
736   }
737 
738   // Pointer to any pointer.
739   if (Loc::isLocType(CastTy))
740     return V;
741 
742   // Pointer to whatever else.
743   return UnknownVal();
744 }
745 
746 static bool hasSameUnqualifiedPointeeType(QualType ty1, QualType ty2) {
747   return ty1->getPointeeType().getCanonicalType().getTypePtr() ==
748          ty2->getPointeeType().getCanonicalType().getTypePtr();
749 }
750 
751 SVal SValBuilder::evalCastSubKind(loc::MemRegionVal V, QualType CastTy,
752                                   QualType OriginalTy) {
753   // Pointer to bool.
754   if (CastTy->isBooleanType()) {
755     const MemRegion *R = V.getRegion();
756     if (const FunctionCodeRegion *FTR = dyn_cast<FunctionCodeRegion>(R))
757       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FTR->getDecl()))
758         if (FD->isWeak())
759           // FIXME: Currently we are using an extent symbol here,
760           // because there are no generic region address metadata
761           // symbols to use, only content metadata.
762           return nonloc::SymbolVal(SymMgr.getExtentSymbol(FTR));
763 
764     if (const SymbolicRegion *SymR = R->getSymbolicBase()) {
765       SymbolRef Sym = SymR->getSymbol();
766       QualType Ty = Sym->getType();
767       // This change is needed for architectures with varying
768       // pointer widths. See the amdgcn opencl reproducer with
769       // this change as an example: solver-sym-simplification-ptr-bool.cl
770       if (!Ty->isReferenceType())
771         return makeNonLoc(Sym, BO_NE, BasicVals.getZeroWithTypeSize(Ty),
772                           CastTy);
773     }
774     // Non-symbolic memory regions are always true.
775     return makeTruthVal(true, CastTy);
776   }
777 
778   const bool IsUnknownOriginalType = OriginalTy.isNull();
779   // Try to cast to array
780   const auto *ArrayTy =
781       IsUnknownOriginalType
782           ? nullptr
783           : dyn_cast<ArrayType>(OriginalTy.getCanonicalType());
784 
785   // Pointer to integer.
786   if (CastTy->isIntegralOrEnumerationType()) {
787     SVal Val = V;
788     // Array to integer.
789     if (ArrayTy) {
790       // We will always decay to a pointer.
791       QualType ElemTy = ArrayTy->getElementType();
792       Val = StateMgr.ArrayToPointer(V, ElemTy);
793       // FIXME: Keep these here for now in case we decide soon that we
794       // need the original decayed type.
795       //    QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
796       //    QualType pointerTy = C.getPointerType(elemTy);
797     }
798     const unsigned BitWidth = Context.getIntWidth(CastTy);
799     return makeLocAsInteger(Val.castAs<Loc>(), BitWidth);
800   }
801 
802   // Pointer to pointer.
803   if (Loc::isLocType(CastTy)) {
804 
805     if (IsUnknownOriginalType) {
806       // When retrieving symbolic pointer and expecting a non-void pointer,
807       // wrap them into element regions of the expected type if necessary.
808       // It is necessary to make sure that the retrieved value makes sense,
809       // because there's no other cast in the AST that would tell us to cast
810       // it to the correct pointer type. We might need to do that for non-void
811       // pointers as well.
812       // FIXME: We really need a single good function to perform casts for us
813       // correctly every time we need it.
814       const MemRegion *R = V.getRegion();
815       if (CastTy->isPointerType() && !CastTy->isVoidPointerType()) {
816         if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
817           QualType SRTy = SR->getSymbol()->getType();
818           if (!hasSameUnqualifiedPointeeType(SRTy, CastTy)) {
819             if (auto OptMemRegV = getCastedMemRegionVal(SR, CastTy))
820               return *OptMemRegV;
821           }
822         }
823       }
824       // Next fixes pointer dereference using type different from its initial
825       // one. See PR37503 and PR49007 for details.
826       if (const auto *ER = dyn_cast<ElementRegion>(R)) {
827         if (auto OptMemRegV = getCastedMemRegionVal(ER, CastTy))
828           return *OptMemRegV;
829       }
830 
831       return V;
832     }
833 
834     if (OriginalTy->isIntegralOrEnumerationType() ||
835         OriginalTy->isBlockPointerType() || OriginalTy->isFunctionPointerType())
836       return V;
837 
838     // Array to pointer.
839     if (ArrayTy) {
840       // Are we casting from an array to a pointer?  If so just pass on
841       // the decayed value.
842       if (CastTy->isPointerType() || CastTy->isReferenceType()) {
843         // We will always decay to a pointer.
844         QualType ElemTy = ArrayTy->getElementType();
845         return StateMgr.ArrayToPointer(V, ElemTy);
846       }
847       // Are we casting from an array to an integer?  If so, cast the decayed
848       // pointer value to an integer.
849       assert(CastTy->isIntegralOrEnumerationType());
850     }
851 
852     // Other pointer to pointer.
853     assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
854            CastTy->isReferenceType());
855 
856     // We get a symbolic function pointer for a dereference of a function
857     // pointer, but it is of function type. Example:
858 
859     //  struct FPRec {
860     //    void (*my_func)(int * x);
861     //  };
862     //
863     //  int bar(int x);
864     //
865     //  int f1_a(struct FPRec* foo) {
866     //    int x;
867     //    (*foo->my_func)(&x);
868     //    return bar(x)+1; // no-warning
869     //  }
870 
871     // Get the result of casting a region to a different type.
872     const MemRegion *R = V.getRegion();
873     if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
874       return *OptMemRegV;
875   }
876 
877   // Pointer to whatever else.
878   // FIXME: There can be gross cases where one casts the result of a
879   // function (that returns a pointer) to some other value that happens to
880   // fit within that pointer value.  We currently have no good way to model
881   // such operations.  When this happens, the underlying operation is that
882   // the caller is reasoning about bits.  Conceptually we are layering a
883   // "view" of a location on top of those bits.  Perhaps we need to be more
884   // lazy about mutual possible views, even on an SVal?  This may be
885   // necessary for bit-level reasoning as well.
886   return UnknownVal();
887 }
888 
889 SVal SValBuilder::evalCastSubKind(nonloc::CompoundVal V, QualType CastTy,
890                                   QualType OriginalTy) {
891   // Compound to whatever.
892   return UnknownVal();
893 }
894 
895 SVal SValBuilder::evalCastSubKind(nonloc::ConcreteInt V, QualType CastTy,
896                                   QualType OriginalTy) {
897   auto CastedValue = [V, CastTy, this]() {
898     llvm::APSInt Value = V.getValue();
899     BasicVals.getAPSIntType(CastTy).apply(Value);
900     return Value;
901   };
902 
903   // Integer to bool.
904   if (CastTy->isBooleanType())
905     return makeTruthVal(V.getValue().getBoolValue(), CastTy);
906 
907   // Integer to pointer.
908   if (CastTy->isIntegralOrEnumerationType())
909     return makeIntVal(CastedValue());
910 
911   // Integer to pointer.
912   if (Loc::isLocType(CastTy))
913     return makeIntLocVal(CastedValue());
914 
915   // Pointer to whatever else.
916   return UnknownVal();
917 }
918 
919 SVal SValBuilder::evalCastSubKind(nonloc::LazyCompoundVal V, QualType CastTy,
920                                   QualType OriginalTy) {
921   // Compound to whatever.
922   return UnknownVal();
923 }
924 
925 SVal SValBuilder::evalCastSubKind(nonloc::LocAsInteger V, QualType CastTy,
926                                   QualType OriginalTy) {
927   Loc L = V.getLoc();
928 
929   // Pointer as integer to bool.
930   if (CastTy->isBooleanType())
931     // Pass to Loc function.
932     return evalCastKind(L, CastTy, OriginalTy);
933 
934   const bool IsUnknownOriginalType = OriginalTy.isNull();
935   // Pointer as integer to pointer.
936   if (!IsUnknownOriginalType && Loc::isLocType(CastTy) &&
937       OriginalTy->isIntegralOrEnumerationType()) {
938     if (const MemRegion *R = L.getAsRegion())
939       if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
940         return *OptMemRegV;
941     return L;
942   }
943 
944   // Pointer as integer with region to integer/pointer.
945   const MemRegion *R = L.getAsRegion();
946   if (!IsUnknownOriginalType && R) {
947     if (CastTy->isIntegralOrEnumerationType())
948       return evalCastSubKind(loc::MemRegionVal(R), CastTy, OriginalTy);
949 
950     if (Loc::isLocType(CastTy)) {
951       assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
952              CastTy->isReferenceType());
953       // Delegate to store manager to get the result of casting a region to a
954       // different type. If the MemRegion* returned is NULL, this expression
955       // Evaluates to UnknownVal.
956       if (auto OptMemRegV = getCastedMemRegionVal(R, CastTy))
957         return *OptMemRegV;
958     }
959   } else {
960     if (Loc::isLocType(CastTy)) {
961       if (IsUnknownOriginalType)
962         return evalCastSubKind(loc::MemRegionVal(R), CastTy, OriginalTy);
963       return L;
964     }
965 
966     SymbolRef SE = nullptr;
967     if (R) {
968       if (const SymbolicRegion *SR =
969               dyn_cast<SymbolicRegion>(R->StripCasts())) {
970         SE = SR->getSymbol();
971       }
972     }
973 
974     if (!CastTy->isFloatingType() || !SE || SE->getType()->isFloatingType()) {
975       // FIXME: Correctly support promotions/truncations.
976       const unsigned CastSize = Context.getIntWidth(CastTy);
977       if (CastSize == V.getNumBits())
978         return V;
979 
980       return makeLocAsInteger(L, CastSize);
981     }
982   }
983 
984   // Pointer as integer to whatever else.
985   return UnknownVal();
986 }
987 
988 SVal SValBuilder::evalCastSubKind(nonloc::SymbolVal V, QualType CastTy,
989                                   QualType OriginalTy) {
990   SymbolRef SE = V.getSymbol();
991 
992   const bool IsUnknownOriginalType = OriginalTy.isNull();
993   // Symbol to bool.
994   if (!IsUnknownOriginalType && CastTy->isBooleanType()) {
995     // Non-float to bool.
996     if (Loc::isLocType(OriginalTy) ||
997         OriginalTy->isIntegralOrEnumerationType() ||
998         OriginalTy->isMemberPointerType()) {
999       BasicValueFactory &BVF = getBasicValueFactory();
1000       return makeNonLoc(SE, BO_NE, BVF.getValue(0, SE->getType()), CastTy);
1001     }
1002   } else {
1003     // Symbol to integer, float.
1004     QualType T = Context.getCanonicalType(SE->getType());
1005 
1006     // Produce SymbolCast if CastTy and T are different integers.
1007     // NOTE: In the end the type of SymbolCast shall be equal to CastTy.
1008     if (T->isIntegralOrUnscopedEnumerationType() &&
1009         CastTy->isIntegralOrUnscopedEnumerationType()) {
1010       AnalyzerOptions &Opts =
1011           StateMgr.getOwningEngine().getAnalysisManager().getAnalyzerOptions();
1012       // If appropriate option is disabled, ignore the cast.
1013       // NOTE: ShouldSupportSymbolicIntegerCasts is `false` by default.
1014       if (!Opts.ShouldSupportSymbolicIntegerCasts)
1015         return V;
1016       return simplifySymbolCast(V, CastTy);
1017     }
1018     if (!Loc::isLocType(CastTy))
1019       if (!IsUnknownOriginalType || !CastTy->isFloatingType() ||
1020           T->isFloatingType())
1021         return makeNonLoc(SE, T, CastTy);
1022   }
1023 
1024   // Symbol to pointer and whatever else.
1025   return UnknownVal();
1026 }
1027 
1028 SVal SValBuilder::evalCastSubKind(nonloc::PointerToMember V, QualType CastTy,
1029                                   QualType OriginalTy) {
1030   // Member pointer to whatever.
1031   return V;
1032 }
1033 
1034 nonloc::SymbolVal SValBuilder::simplifySymbolCast(nonloc::SymbolVal V,
1035                                                   QualType CastTy) {
1036   // We use seven conditions to recognize a simplification case.
1037   // For the clarity let `CastTy` be `C`, SE->getType() - `T`, root type - `R`,
1038   // prefix `u` for unsigned, `s` for signed, no prefix - any sign:
1039   // E.g. (char)(short)(uint x)
1040   //      ( sC )( sT  )( uR  x)
1041   //
1042   // C === R (the same type)
1043   //  (char)(char x) -> (char x)
1044   //  (long)(long x) -> (long x)
1045   // Note: Comparisons operators below are for bit width.
1046   // C == T
1047   //  (short)(short)(int x) -> (short)(int x)
1048   //  (int)(long)(char x) -> (int)(char x) (sizeof(long) == sizeof(int))
1049   //  (long)(ullong)(char x) -> (long)(char x) (sizeof(long) == sizeof(ullong))
1050   // C < T
1051   //  (short)(int)(char x) -> (short)(char x)
1052   //  (char)(int)(short x) -> (char)(short x)
1053   //  (short)(int)(short x) -> (short x)
1054   // C > T > uR
1055   //  (int)(short)(uchar x) -> (int)(uchar x)
1056   //  (uint)(short)(uchar x) -> (uint)(uchar x)
1057   //  (int)(ushort)(uchar x) -> (int)(uchar x)
1058   // C > sT > sR
1059   //  (int)(short)(char x) -> (int)(char x)
1060   //  (uint)(short)(char x) -> (uint)(char x)
1061   // C > sT == sR
1062   //  (int)(char)(char x) -> (int)(char x)
1063   //  (uint)(short)(short x) -> (uint)(short x)
1064   // C > uT == uR
1065   //  (int)(uchar)(uchar x) -> (int)(uchar x)
1066   //  (uint)(ushort)(ushort x) -> (uint)(ushort x)
1067   //  (llong)(ulong)(uint x) -> (llong)(uint x) (sizeof(ulong) == sizeof(uint))
1068 
1069   SymbolRef SE = V.getSymbol();
1070   QualType T = Context.getCanonicalType(SE->getType());
1071 
1072   if (T == CastTy)
1073     return V;
1074 
1075   if (!isa<SymbolCast>(SE))
1076     return makeNonLoc(SE, T, CastTy);
1077 
1078   SymbolRef RootSym = cast<SymbolCast>(SE)->getOperand();
1079   QualType RT = RootSym->getType().getCanonicalType();
1080 
1081   BasicValueFactory &BVF = getBasicValueFactory();
1082   APSIntType CTy = BVF.getAPSIntType(CastTy);
1083   APSIntType TTy = BVF.getAPSIntType(T);
1084 
1085   const auto WC = CTy.getBitWidth();
1086   const auto WT = TTy.getBitWidth();
1087 
1088   if (WC <= WT) {
1089     const bool isSameType = (RT == CastTy);
1090     if (isSameType)
1091       return nonloc::SymbolVal(RootSym);
1092     return makeNonLoc(RootSym, RT, CastTy);
1093   }
1094 
1095   APSIntType RTy = BVF.getAPSIntType(RT);
1096   const auto WR = RTy.getBitWidth();
1097   const bool UT = TTy.isUnsigned();
1098   const bool UR = RTy.isUnsigned();
1099 
1100   if (((WT > WR) && (UR || !UT)) || ((WT == WR) && (UT == UR)))
1101     return makeNonLoc(RootSym, RT, CastTy);
1102 
1103   return makeNonLoc(SE, T, CastTy);
1104 }
1105