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