1//=== CastSizeChecker.cpp ---------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// CastSizeChecker checks when casting a malloc'ed symbolic region to type T, 11// whether the size of the symbolic region is a multiple of the size of T. 12// 13//===----------------------------------------------------------------------===// 14#include "ClangSACheckers.h" 15#include "clang/AST/CharUnits.h" 16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 17#include "clang/StaticAnalyzer/Core/Checker.h" 18#include "clang/StaticAnalyzer/Core/CheckerManager.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 20 21using namespace clang; 22using namespace ento; 23 24namespace { 25class CastSizeChecker : public Checker< check::PreStmt<CastExpr> > { 26 mutable std::unique_ptr<BuiltinBug> BT; 27 28public: 29 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const; 30}; 31} 32 33/// Check if we are casting to a struct with a flexible array at the end. 34/// \code 35/// struct foo { 36/// size_t len; 37/// struct bar data[]; 38/// }; 39/// \endcode 40/// or 41/// \code 42/// struct foo { 43/// size_t len; 44/// struct bar data[0]; 45/// } 46/// \endcode 47/// In these cases it is also valid to allocate size of struct foo + a multiple 48/// of struct bar. 49static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize, 50 CharUnits TypeSize, QualType ToPointeeTy) { 51 const RecordType *RT = ToPointeeTy->getAs<RecordType>(); 52 if (!RT) 53 return false; 54 55 const RecordDecl *RD = RT->getDecl(); 56 RecordDecl::field_iterator Iter(RD->field_begin()); 57 RecordDecl::field_iterator End(RD->field_end()); 58 const FieldDecl *Last = nullptr; 59 for (; Iter != End; ++Iter) 60 Last = *Iter; 61 assert(Last && "empty structs should already be handled"); 62 63 const Type *ElemType = Last->getType()->getArrayElementTypeNoTypeQual(); 64 CharUnits FlexSize; 65 if (const ConstantArrayType *ArrayTy = 66 Ctx.getAsConstantArrayType(Last->getType())) { 67 FlexSize = Ctx.getTypeSizeInChars(ElemType); 68 if (ArrayTy->getSize() == 1 && TypeSize > FlexSize) 69 TypeSize -= FlexSize; 70 else if (ArrayTy->getSize() != 0) 71 return false; 72 } else if (RD->hasFlexibleArrayMember()) { 73 FlexSize = Ctx.getTypeSizeInChars(ElemType); 74 } else { 75 return false; 76 } 77 78 if (FlexSize.isZero()) 79 return false; 80 81 CharUnits Left = RegionSize - TypeSize; 82 if (Left.isNegative()) 83 return false; 84 85 return Left % FlexSize == 0; 86} 87 88void CastSizeChecker::checkPreStmt(const CastExpr *CE,CheckerContext &C) const { 89 const Expr *E = CE->getSubExpr(); 90 ASTContext &Ctx = C.getASTContext(); 91 QualType ToTy = Ctx.getCanonicalType(CE->getType()); 92 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); 93 94 if (!ToPTy) 95 return; 96 97 QualType ToPointeeTy = ToPTy->getPointeeType(); 98 99 // Only perform the check if 'ToPointeeTy' is a complete type. 100 if (ToPointeeTy->isIncompleteType()) 101 return; 102 103 ProgramStateRef state = C.getState(); 104 const MemRegion *R = state->getSVal(E, C.getLocationContext()).getAsRegion(); 105 if (!R) 106 return; 107 108 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); 109 if (!SR) 110 return; 111 112 SValBuilder &svalBuilder = C.getSValBuilder(); 113 SVal extent = SR->getExtent(svalBuilder); 114 const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent); 115 if (!extentInt) 116 return; 117 118 CharUnits regionSize = CharUnits::fromQuantity(extentInt->getSExtValue()); 119 CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy); 120 121 // Ignore void, and a few other un-sizeable types. 122 if (typeSize.isZero()) 123 return; 124 125 if (regionSize % typeSize == 0) 126 return; 127 128 if (evenFlexibleArraySize(Ctx, regionSize, typeSize, ToPointeeTy)) 129 return; 130 131 if (ExplodedNode *errorNode = C.generateErrorNode()) { 132 if (!BT) 133 BT.reset(new BuiltinBug(this, "Cast region with wrong size.", 134 "Cast a region whose size is not a multiple" 135 " of the destination type size.")); 136 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), errorNode); 137 R->addRange(CE->getSourceRange()); 138 C.emitReport(std::move(R)); 139 } 140} 141 142void ento::registerCastSizeChecker(CheckerManager &mgr) { 143 mgr.registerChecker<CastSizeChecker>(); 144} 145