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